git @ Cat's Eye Technologies Velo / 3a83622
Implement conditional, after juggling things around a tad. catseye 12 years ago
4 changed file(s) with 49 addition(s) and 23 deletion(s). Raw diff Collapse all Expand all
3535 Strings as Blocks
3636 -----------------
3737
38 Conditional statements are implemented by a method on strings.
39
40 | extend IO
41 | {5 > 4}.if {print {Yes}}, {print {No}}
38 Conditional statements are implemented by a method called `if`.
39 This method is on the root object, called `Object`, from which all
40 objects, including your script, inherit.
41
42 | if ({X}.equals {X}), {IO.print {Yes}}, {IO.print {No}}
4243 = Yes
4344
45 | if ({X}.equals {Y}), {IO.print {Yes}}, {IO.print {No}}
46 = No
47
4448 To try to hammer this block-is-a-string thing home, you can just
45 as easily call a method on a string variable as a string literal.
46
47 | extend IO
48 | a = {4 > 5}
49 | a.if {print {Yes}}, {print {No}}
49 as easily call this method with arguments that are string variables,
50 rather than string literals (that look like blocks of code.)
51
52 | yes = {IO.print {Yes}}
53 | no = {IO.print {No}}
54 | if ({X}.equals {Y}), yes, no
5055 = No
5156
5257 You may be thinking that this is basically an implicit and
5964 can't predict what it will be until you evaluate the surrounding code,
6065 that you necessarily take a performance hit.
6166
62 | extend IO
63 | a = {7}.concat {>}.concat {5}
64 | a.if {print {Yes}}, {print {No}}
67 | p = {extend IO; print }
68 | yes = p.concat {{Yes}}
69 | no = p.concat {{No}}
70 | if ({X}.equals {X}), yes, no
6571 = Yes
6672
6773 Scripts as Classes
0 if ({X}.equals {Y}), {IO.print {Yes}}, {IO.print {No}}
1 if ({X}.equals {X}), {IO.print {Yes}}, {IO.print {No}}
136136
137137 def eval obj
138138 debug "eval #{self}"
139 o = VeloObject.new "{#{@text}}"
140 o.extend $String
141 o.contents = @text
142 o
139 make_string_literal @text
143140 end
144141
145142 def to_s
9191 end
9292 end
9393
94 def make_string_literal text
95 o = VeloObject.new "#{@text}"
96 o.extend $String
97 o.contents = text
98 o
99 end
100
94101 ### establish the objectbase ###
95102
96103 $Object = VeloObject.new 'Object'
103110 $Object.set 'new', VeloMethod.new('new', proc { |obj, args|
104111 raise VeloMethodNotImplemented
105112 })
113 $Object.set 'if', VeloMethod.new('if', proc { |obj, args|
114 debug args
115 method = nil
116 choice = args[0].contents.empty? ? 2 : 1
117 method = args[choice].lookup 'eval'
118 method.run args[choice], []
119 })
106120
107121 $String = VeloObject.new 'String'
108122 $String.set 'concat', VeloMethod.new('concat', proc { |obj, args|
109123 debug "concat #{obj} #{args[0]}"
110 obj.contents = obj.contents + args[0].contents
111 # XXX should this create a new object, maybe?
112 obj
124 make_string_literal(obj.contents + args[0].contents)
113125 })
114126 $String.set 'class', VeloMethod.new('class', proc { |obj, args|
115127 raise VeloMethodNotImplemented
123135 s.eval obj
124136 })
125137 })
126 $String.set 'if', VeloMethod.new('if', proc { |obj, args|
127 raise VeloMethodNotImplemented
138 $String.set 'equals', VeloMethod.new('equals', proc { |obj, args|
139 if obj.contents == args[0].contents
140 make_string_literal "true"
141 else
142 make_string_literal ""
143 end
144 })
145 $String.set 'eval', VeloMethod.new('eval', proc { |obj, args|
146 p = Parser.new obj.contents
147 s = p.script
148 s.eval obj
128149 })
129150
130151 $IO = VeloObject.new 'IO'
148169 })
149170
150171 velo_Shimmy = VeloObject.new 'Shimmy'
151 velo_Shimmy.call 'extend', [$String]
152 velo_Shimmy.call 'bar', [1,2,3]
172 velo_Shimmy.extend $String
173 (velo_Shimmy.lookup 'bar').run velo_Shimmy, [1,2,3]
153174 end