Implement conditional, after juggling things around a tad.
catseye
12 years ago
35 | 35 | Strings as Blocks |
36 | 36 | ----------------- |
37 | 37 | |
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}} | |
42 | 43 | = Yes |
43 | 44 | |
45 | | if ({X}.equals {Y}), {IO.print {Yes}}, {IO.print {No}} | |
46 | = No | |
47 | ||
44 | 48 | 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 | |
50 | 55 | = No |
51 | 56 | |
52 | 57 | You may be thinking that this is basically an implicit and |
59 | 64 | can't predict what it will be until you evaluate the surrounding code, |
60 | 65 | that you necessarily take a performance hit. |
61 | 66 | |
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 | |
65 | 71 | = Yes |
66 | 72 | |
67 | 73 | 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}} |
136 | 136 | |
137 | 137 | def eval obj |
138 | 138 | debug "eval #{self}" |
139 | o = VeloObject.new "{#{@text}}" | |
140 | o.extend $String | |
141 | o.contents = @text | |
142 | o | |
139 | make_string_literal @text | |
143 | 140 | end |
144 | 141 | |
145 | 142 | def to_s |
91 | 91 | end |
92 | 92 | end |
93 | 93 | |
94 | def make_string_literal text | |
95 | o = VeloObject.new "#{@text}" | |
96 | o.extend $String | |
97 | o.contents = text | |
98 | o | |
99 | end | |
100 | ||
94 | 101 | ### establish the objectbase ### |
95 | 102 | |
96 | 103 | $Object = VeloObject.new 'Object' |
103 | 110 | $Object.set 'new', VeloMethod.new('new', proc { |obj, args| |
104 | 111 | raise VeloMethodNotImplemented |
105 | 112 | }) |
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 | }) | |
106 | 120 | |
107 | 121 | $String = VeloObject.new 'String' |
108 | 122 | $String.set 'concat', VeloMethod.new('concat', proc { |obj, args| |
109 | 123 | 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) | |
113 | 125 | }) |
114 | 126 | $String.set 'class', VeloMethod.new('class', proc { |obj, args| |
115 | 127 | raise VeloMethodNotImplemented |
123 | 135 | s.eval obj |
124 | 136 | }) |
125 | 137 | }) |
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 | |
128 | 149 | }) |
129 | 150 | |
130 | 151 | $IO = VeloObject.new 'IO' |
148 | 169 | }) |
149 | 170 | |
150 | 171 | 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] | |
153 | 174 | end |