-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodes.rb
304 lines (231 loc) · 6.11 KB
/
nodes.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/env ruby
module Paxl
module Nodes
class Node
end
class PStatementList < Node
def initialize(statement)
if statement.kind_of? Array
@statement_list = statement
else
@statement_list = [statement]
end
end
def +(statement_list)
self.class.new @statement_list + statement_list.list
end
def list
@statement_list
end
def eval(scope)
result = nil
@statement_list.each do |statement|
result = statement.eval(scope)
end
return result
end
end
class PForLoop < Node
def initialize(control, statements)
@control, @statements = control, statements
end
def eval(scope)
my_scope = Paxl::Scope.new(scope)
init_stmt, test_stmt, iter_stmt = @control.list
init_stmt.eval(my_scope)
return_value = nil
while test_stmt.eval(my_scope) do
return_value = @statements.eval(my_scope)
iter_stmt.eval(my_scope)
end
return_value
end
end
class PIfStatement < Node
def initialize(test, if_true, if_false)
@test, @if_true, @if_false = test, if_true, if_false
end
def eval(scope)
test_result = @test.eval(scope)
if test_result
@if_true.eval(scope)
else
if @if_false
@if_false.eval(scope)
else
nil
end
end
end
end
class PBlockDefinition < Node
attr_reader :parameters, :statement_list, :block_scope
def initialize(parameters, statements)
@parameters, @statement_list = parameters, PStatementList.new(statements)
end
def eval(scope)
@block_scope = scope.clone
@block_scope["this"] = self
self
end
end
class PBlockCall < Node
def initialize(identifier, arguments)
@identifier, @arguments = identifier, arguments
end
def eval(scope)
block_def = scope[@identifier]
if not block_def.kind_of? Paxl::Nodes::PBlockDefinition
return "Error! Attempting to use a non-block as a block."
end
my_scope = block_def.block_scope.clone
if @arguments.kind_of? Paxl::Nodes::PStatementList
arguments = Array.new(@arguments.list)
end
block_def.parameters.each do |param|
my_scope[param] = arguments.pop().eval(scope)
end
block_def.statement_list.eval(my_scope)
end
end
class PVariableAssignment < Node
def initialize(identifier, value)
@identifier, @value = identifier, value
end
def eval(scope)
scope[@identifier] = @value.eval(scope)
end
end
class PVariableReference < Node
def initialize(identifier)
@identifier = identifier
end
def eval(scope)
scope[@identifier]
end
end
class PLogicalExpression < Node
def initialize(a, op, b)
@a, @op, @b = a, op, b
end
def eval(scope)
a, b = @a.eval(scope), @b.eval(scope)
case @op
when "and"
return PBoolean.new( (a and b) ).eval(scope)
when "or"
return PBoolean.new( (a or b) ).eval(scope)
when "not"
return PBoolean.new( (not a) ).eval(scope)
end
end
end
class PComparison < Node
def initialize(a, op, b)
@a, @op, @b = a, op, b
end
def eval(scope)
a, b = @a.eval(scope), @b.eval(scope)
case @op
when "=="
return PBoolean.new( (a == b) ).eval(scope)
when "!="
return PBoolean.new( (a != b) ).eval(scope)
when "<="
return PBoolean.new( (a <= b) ).eval(scope)
when ">="
return PBoolean.new( (a >= b) ).eval(scope)
when "<"
return PBoolean.new( (a < b) ).eval(scope)
when ">"
return PBoolean.new( (a > b) ).eval(scope)
end
end
end
class PMultiplication < Node
def initialize(a, b)
@a, @b = a, b
end
def eval(scope)
@a.eval(scope) * @b.eval(scope)
end
end
class PDivision < Node
def initialize(a, b)
@a, @b = a, b
end
def eval(scope)
@a.eval(scope) / @b.eval(scope)
end
end
class PAddition < Node
def initialize(a, b)
@a, @b = a, b
end
def eval(scope)
@a.eval(scope) + @b.eval(scope)
end
end
class PSubtraction < Node
def initialize(a, b)
@a, @b = a, b
end
def eval(scope)
@a.eval(scope) - @b.eval(scope)
end
end
class PInteger < Node
def initialize(value)
@value = value.to_i
end
def eval(scope)
@value
end
end
class PFloat < Node
def initialize(value)
@value = value.to_f
end
def eval(scope)
@value
end
end
class PBoolean < Node
def initialize(value)
if value == true or value == false
@value = value
else
@value = (value != 0) # false if value == 0, true otherwise
end
end
def eval(scope)
@value
end
end
end
class Scope < Hash
def initialize(parent)
super
@parent = parent
end
def [](key)
if self.has_key? key
return super(key)
else
if @parent.nil?
return nil
else
return @parent[key]
end
end
end
def []=(key, value)
current_value = self[key]
if self.has_key? key or current_value.nil?
super(key, value)
else
@parent[key] = value
end
end
end
end