-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.rb
464 lines (383 loc) · 8.13 KB
/
ast.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
module AST
class SemanticError < StandardError ; end
class Node
attr_reader :name
attr_reader :node_type
# symbol tables
attr_accessor :vars
attr_accessor :types
attr_accessor :procs
@@indent = 0
def initialize(name, node_type = nil)
@name = name
@node_type = node_type
end
def to_s(child_nodes = nil)
if !child_nodes.nil?
s = "(" + @name
for child in child_nodes
s = s + " " + child.to_s
end
s += close_node
else
@name.to_s
end
end
def indent(i = 0)
s = ""
for i in 0..(@@indent * 2 + i - 1)
s += " "
end
s
end
def close_node(is_indented = false)
s = ""
if is_indented
s += indent
end
s += ")"
end
def walk_ast(fn)
fn.call(self)
end
end
class TopNode < Node
attr_reader :proc_nodes
attr_reader :stm_nodes
def initialize(proc_nodes, stm_nodes)
super("program")
@proc_nodes = proc_nodes
@stm_nodes = stm_nodes
end
def to_s
s = "(\n"
@@indent += 1
s += indent + @proc_nodes.to_s + "\n"
s += indent + @stm_nodes.to_s + "\n"
@@indent -= 1
s = s + indent + ")"
s
end
def walk_ast(fn)
super(fn)
@proc_nodes.walk_ast(fn)
@stm_nodes.walk_ast(fn)
end
end
class Sequence < Node
attr_reader :nodes
def initialize(nodes)
super("seq")
@nodes = nodes
end
def to_s
s = "(\n"
@@indent += 1
for node in @nodes
s += indent + node.to_s + "\n" if !node.nil?
end
@@indent -= 1
s = s + indent + ")"
s
end
def walk_ast(fn)
super(fn)
@nodes.each {|node| node.walk_ast(fn) if !node.nil?}
end
end
class Proc < Node
attr_reader :proc
attr_reader :stms
def initialize(proc, stms)
super(proc.name)
@proc = proc
@stms = stms
end
def to_s
s = "(" + @name
@@indent += 1
s += " " + @stms.to_s
@@indent -= 1
s += "\n" + close_node(true)
end
def walk_ast(fn)
super(fn)
@stms.walk_ast(fn)
end
end
class Statement < Node
end
class BinaryExpression < Statement
attr_reader :lexp
attr_reader :rexp
def initialize(name, lexp, rexp, node_type)
super(name, node_type)
@lexp = lexp
@rexp = rexp
# determine what the valid types are for the expressions on the right and left
valid_types = []
case name
when "-", "/", "%"
valid_types = [:int]
when "+", "*"
valid_types = [:int, :bool]
when "=", "!="
valid_types = [:int, :bool]
when ">", "<", ">=", "<="
valid_types = [:int]
end
valid = false
for valid_type in valid_types
if valid_type == :int
if @lexp.node_type.is_int && @rexp.node_type.is_int
valid = true
break
end
elsif valid_type == :bool
if @lexp.node_type.is_bool && @rexp.node_type.is_bool
valid = true
break
end
elsif valid_type == :string
if @lexp.node_type.is_string && @rexp.node_type.is_string
valid = true
break
end
end
end
if !valid
raise SemanticError, "incompatible types (" + @lexp.node_type.to_s + ", " + @rexp.node_type.to_s + ") in expression for '#{name}' operator"
end
end
def to_s
super([@lexp, @rexp])
end
def walk_ast(fn)
super(fn)
@lexp.walk_ast(fn)
@rexp.walk_ast(fn)
end
end
class UnaryExpression < Statement
attr_reader :exp
def initialize(name, exp, node_type)
super(name, node_type)
@exp = exp
valid = false
case name
when "-"
valid = true if @exp.node_type.is_int || @exp.node_type.is_bool
when "?"
valid = true if @exp.node_type.is_bool
end
if !valid
raise SemanticError, "invalid type (" + @exp.node_type.to_s + ") in expression for unary '#{name}' operator"
end
end
def to_s
super([@exp])
end
def walk_ast(fn)
super(fn)
@exp.walk_ast(fn)
end
end
class Write < Statement
attr_reader :exp
def initialize(name, exp)
super(name)
@exp = exp
valid = true if @exp.node_type.is_string || @exp.node_type.is_int
if !valid
raise SemanticError, "invalid type (" + @exp.node_type.to_s + ") in expression for unary '#{name}' operator"
end
end
def to_s
super([@exp])
end
def walk_ast(fn)
super(fn)
@exp.walk_ast(fn)
end
end
class Literal < Statement
attr_reader :value
attr_accessor :mem_address # for strings only
def initialize(node_type, value)
super(node_type.to_s, node_type)
@value = value
end
def to_s
if @node_type.is_string
super(["\"" + @value + "\"" ])
else
super([@value])
end
end
end
class Control < Statement
end
class Call < Statement
attr_reader :proc
attr_reader :args
def initialize(proc, args, node_type)
super("call", node_type)
@proc = proc
@args = args
end
def to_s
s = "(call " + proc.name + " ("
for arg in args
s = s + arg.to_s
end
s += close_node
s += ")"
end
def walk_ast(fn)
super(fn)
args.each {|arg| arg.walk_ast(fn)}
end
end
class ForLoop < Statement
attr_reader :id
attr_reader :begin_range
attr_reader :end_range
attr_reader :statements
def initialize(name, id, begin_range, end_range, statements)
super(name)
@id = id
@begin_range = begin_range
@end_range = end_range
@statements = statements
end
def to_s
s = "(for " + @id.to_s + " = " + @begin_range.to_s + ".." + @end_range.to_s
@@indent += 1
s = s + " " + @statements.to_s
@@indent -= 1
end
s += "\n" + close_node(true)
end
def walk_ast(fn)
super(fn)
@id.walk_ast(fn)
@begin_range.walk_ast(fn)
@end_range.walk_ast(fn)
@statements.walk_ast(fn)
end
end
class WhileLoop < Statement
attr_reader :exp
attr_reader :statements
def initialize(name, exp, stms)
super(name)
@exp = exp
@statements = stms
end
def to_s
s = "(while " + @exp.to_s
@@indent += 1
s = s + " " + @statements.to_s
@@indent -= 1
end
s += "\n" + close_node(true)
end
def walk_ast(fn)
super(fn)
@exp.walk_ast(fn)
@statements.walk_ast(fn)
end
end
class Read < Statement
end
class Assignment < Statement
attr_reader :id
attr_reader :exp
def initialize(name, id, exp)
@id = id
@exp = exp
super(name)
valid = false
if @id.node_type.is_int && @exp.node_type.is_int
valid = true
elsif @id.node_type.is_bool && @exp.node_type.is_bool
valid = true
elsif @id.node_type.is_string && @exp.node_type.is_string
valid = true
end
if !valid
raise SemanticError, "incompatible types (" + @id.node_type.to_s + ", " + @exp.node_type.to_s + ") in expression for '#{name}' operator"
end
end
def to_s
super([@id, @exp])
end
def walk_ast(fn)
super(fn)
@id.walk_ast(fn)
@exp.walk_ast(fn)
end
end
class Identifier < Statement
attr_reader :var
def initialize(var)
super("id", var.var_type)
@var = var
end
def to_s
super([@var.name])
end
end
class Indice < Statement
attr_reader :id
attr_reader :exp
attr_reader :array_size
def initialize(name, id, exp, array_size, node_type)
super(name, node_type)
@id = id
@exp = exp
@array_size = array_size
end
def to_s
super([@id, @exp])
end
def walk_ast(fn)
super(fn)
@id.walk_ast(fn)
@exp.walk_ast(fn)
end
end
class IfElse < Statement
attr_reader :exp
attr_reader :statements
attr_reader :else_node
def initialize(name, exp, statements, else_node = nil)
super(name)
@exp = exp
@statements = statements
@else_node = else_node
end
def to_s
# if or elseif
s = "(" + @name
# expression
s += " " + exp.to_s + " "
@@indent += 1
# if/elseif true statements
s += @statements.to_s
# else false statements
s += "\n" + indent + @else_node.to_s if !@else_node.nil?
@@indent -= 1
s += "\n" + close_node(true)
s
end
def walk_ast(fn)
super(fn)
@exp.walk_ast(fn)
@statements.walk_ast(fn)
@else_node.walk_ast(fn) if !@else_node.nil?
end
end
end