-
Notifications
You must be signed in to change notification settings - Fork 0
/
siementic.py
679 lines (554 loc) · 21.3 KB
/
siementic.py
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
import anytree
import visitor
from prettytable import PrettyTable
#####################################
# Node class
class Node(anytree.Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
self.moonVarName = ""
self.type = ""
def accept(self, visitor):
# allow for pre visit children node function calls
method_name = 'pre_visit_' + type(self).__name__
visit_method = getattr(visitor, method_name, None)
if visit_method:
visit_method(self)
# otherwise accept the children the standard way
for child in self.children:
child.accept(visitor)
visitor.visit(self)
#####################################
# Leaf nodes
class IdNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
self.type = ""
class IntNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
self.type = "integer"
class FloatNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
self.type = "float"
class TypeNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
class AddNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
class MultNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
class RelationNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
class VisibilityNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
#####################################
# Subtree nodes
class ArraySizeNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
class FuncListNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
class ImplNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
self.symbol_table = PrettyTable()
self.table_output = ""
self.struct_name = ""
class StructNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
self.symbol_table = PrettyTable()
self.symbol_table.field_names = ['Name', 'Kind', 'Type', 'Offset', 'Link']
self.symbol_data = []
self.table_entry = []
self.table_output = ""
self.struct_name = ""
self.curr_offset = 0
class AssignNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
self.type = ""
class VarDeclNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
self.table_entry = []
self.mem_size = 0
class MemberDeclNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
self.table_entry = []
class FunctionNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
self.symbol_table = PrettyTable()
self.symbol_table.field_names = ['Name', 'Kind', 'Type', 'Offset', 'Link']
self.symbol_data = []
self.table_entry = []
self.table_output = ""
self.curr_offset = 0
class InheritNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
class ProgramNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
self.symbol_table = PrettyTable()
self.symbol_table.field_names = ['Name', 'Kind', 'Type', 'Offset', 'Link']
self.symbol_data = []
class MemberListNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
class FuncDeclNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
self.table_entry = []
class FuncParamsNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
class ReturnTypeNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
class FuncBodyNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
class IfNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
class ThenNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
class ElseNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
class WhileNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
class ReadNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
class WriteNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
class ReturnNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
self.type = ""
class DotNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
self.type = ""
class FuncCallNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
self.type = ""
class IndiceListNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
class VariableNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
self.type = ""
self.indices = []
class StatBlockNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
class RelExprNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
self.type = ""
class ArithExprNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
self.type = ""
class AddOpNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
self.type = ""
class MultOpNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
self.type = ""
class NotNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
class SignNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
self.type = ""
class EmptySizeNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
class VoidNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
class ParamNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
self.table_entry = []
class ArgParamNode(Node):
def __init__(self, name, parent=None, children=None, **kwargs):
super().__init__(name, parent, children, **kwargs)
#####################################
# Default Visitor Pattern and empty visit members
class Visitor:
@visitor.on('node')
def visit(self, node):
pass
@visitor.when(IdNode)
def visit(self, node):
pass
@visitor.when(IntNode)
def visit(self, node):
pass
@visitor.when(FloatNode)
def visit(self, node):
pass
@visitor.when(TypeNode)
def visit(self, node):
pass
@visitor.when(AddNode)
def visit(self, node):
pass
@visitor.when(MultNode)
def visit(self, node):
pass
@visitor.when(RelationNode)
def visit(self, node):
pass
@visitor.when(VisibilityNode)
def visit(self, node):
pass
@visitor.when(ArraySizeNode)
def visit(self, node):
pass
@visitor.when(FuncListNode)
def visit(self, node):
pass
@visitor.when(ImplNode)
def visit(self, node):
pass
@visitor.when(StructNode)
def visit(self, node):
pass
@visitor.when(AssignNode)
def visit(self, node):
pass
@visitor.when(VarDeclNode)
def visit(self, node):
pass
@visitor.when(MemberDeclNode)
def visit(self, node):
pass
@visitor.when(FunctionNode)
def visit(self, node):
pass
@visitor.when(InheritNode)
def visit(self, node):
pass
@visitor.when(ProgramNode)
def visit(self, node):
pass
@visitor.when(MemberListNode)
def visit(self, node):
pass
@visitor.when(FuncDeclNode)
def visit(self, node):
pass
@visitor.when(FuncParamsNode)
def visit(self, node):
pass
@visitor.when(ReturnTypeNode)
def visit(self, node):
pass
@visitor.when(FuncBodyNode)
def visit(self, node):
pass
@visitor.when(IfNode)
def visit(self, node):
pass
@visitor.when(ThenNode)
def visit(self, node):
pass
@visitor.when(ElseNode)
def visit(self, node):
pass
@visitor.when(WhileNode)
def visit(self, node):
pass
@visitor.when(ReadNode)
def visit(self, node):
pass
@visitor.when(WriteNode)
def visit(self, node):
pass
@visitor.when(ReturnNode)
def visit(self, node):
pass
@visitor.when(DotNode)
def visit(self, node):
pass
@visitor.when(FuncCallNode)
def visit(self, node):
pass
@visitor.when(IndiceListNode)
def visit(self, node):
pass
@visitor.when(VariableNode)
def visit(self, node):
pass
@visitor.when(StatBlockNode)
def visit(self, node):
pass
@visitor.when(RelExprNode)
def visit(self, node):
pass
@visitor.when(ArithExprNode)
def visit(self, node):
pass
@visitor.when(AddOpNode)
def visit(self, node):
pass
@visitor.when(MultOpNode)
def visit(self, node):
pass
@visitor.when(NotNode)
def visit(self, node):
pass
@visitor.when(SignNode)
def visit(self, node):
pass
@visitor.when(EmptySizeNode)
def visit(self, node):
pass
@visitor.when(VoidNode)
def visit(self, node):
pass
@visitor.when(ParamNode)
def visit(self, node):
pass
@visitor.when(ArgParamNode)
def visit(self, node):
pass
#####################################
# Symbol Table Concrete Visitor
class SymbolTableVisitor(Visitor):
def __init__(self):
super().__init__()
self.output = ""
self.errors = ""
self.warnings = ""
@visitor.on('node')
def visit(self, node):
pass
@visitor.when(ProgramNode)
def visit(self, node):
struct_dict = {}
for c in node.children:
if c.name == "struct":
node.symbol_data.append(c.table_entry)
if c.struct_name in struct_dict:
self.errors += "Multiply declared struct: " + c.struct_name + "\n"
struct_dict[c.struct_name] = c.table_output
elif c.name == "impl":
try:
self.output += struct_dict[c.struct_name] + c.table_output
except:
self.errors += "Must define struct before impl for struct: " + c.struct_name + "\n"
else:
node.symbol_data.append(c.table_entry)
self.output += c.table_output
# update symbol table with data
node.symbol_table.add_rows(node.symbol_data)
# update output
title = "Global table"
self.output = node.symbol_table.get_string(title=title) + "\n" + self.output
@visitor.when(StructNode)
def visit(self, node):
id, inherits, memberList = node.children
# add inherits string to title if it exists
inherit_string = inherits.children[0].name if inherits.children else ""
# add members to struct symbol table
for m in memberList.children:
node.symbol_data.append(m.table_entry)
# update symbol table with data
node.symbol_table.add_rows(node.symbol_data)
# update output and table entry
node.table_entry = [id.name, "struct", None, 0, "Table " + id.name]
node.struct_name = id.name
title = "Table Name: " + id.name + ", Inherits: " + inherit_string
node.table_output += node.symbol_table.get_string(title=title) + "\n"
@visitor.when(FuncDeclNode)
def visit(self, node):
id, params, returnType = node.children
node.table_entry = [id.name, "funcDecl", returnType.children[0].name, 0, None]
@visitor.when(FunctionNode)
def visit(self, node):
id, params, returnType, body = node.children
# add func params to the symbol table
for p in params.children:
node.symbol_data.append(p.table_entry)
# add variable declarations to the symbol table
for v in body.children:
if v.name == "varDecl":
# update both the current offset and the table entry
node.curr_offset = node.curr_offset + v.mem_size
# create copy to avoid referencing the variable's entry
row = v.table_entry.copy()
row[3] = node.curr_offset
node.symbol_data.append(row)
# update symbol table with data
node.symbol_table.add_rows(node.symbol_data)
# update function node's symbol table and append to output
node.table_entry = [id.name, "function", returnType.children[0].name, 0, "Table " + id.name]
title = "Table Name: " + id.name + ", Returns: " + returnType.children[0].name
node.table_output += node.symbol_table.get_string(title=title) + "\n"
@visitor.when(ImplNode)
def visit(self, node):
id, funcList = node.children
node.struct_name = id.name
for f in funcList.children:
node.table_output += f.table_output
@visitor.when(VarDeclNode)
def visit(self, node):
v_name, v_type, v_array = node.children
v_arraySize = ""
if v_type.name == "integer":
node.mem_size = 4
elif v_type.name == "float":
node.mem_size = 8
for s in v_array.children:
if s.name == "emptySize":
v_arraySize += "[]"
else:
v_arraySize += "[" + s.name + "]"
node.mem_size *= int(s.name)
node.table_entry = [v_name.name, "variable", v_type.name + v_arraySize, node.mem_size, None]
@visitor.when(MemberDeclNode)
def visit(self, node):
visib, decl = node.children
# add visibility to members for the kind column
new_entry = decl.table_entry
new_entry[1] = visib.name + " " + new_entry[1]
node.table_entry = new_entry
@visitor.when(ParamNode)
def visit(self, node):
p_name, p_type, p_array = node.children
p_arraySize = ""
for s in p_array.children:
p_arraySize += "[]" if s.name == "emptySize" else "[" + s.name + "]"
node.table_entry = [p_name.name, "parameter", p_type.name + p_arraySize, 0, None]
#####################################
# Type Checking Concrete Visitor
class TypeCheckingVisitor(Visitor):
def __init__(self):
super().__init__()
self.errors = ""
self.global_scope = []
self.struct_scope = []
self.func_scope = []
@visitor.on('node')
def visit(self, node):
pass
# pre visit nodes to get symbol table information
def pre_visit_ProgramNode(self, node):
self.global_scope = node.symbol_data
def pre_visit_StructNode(self, node):
self.struct_scope = node.symbol_data
def pre_visit_FunctionNode(self, node):
self.func_scope = node.symbol_data
def pre_visit_ImplNode(self, node):
pass
# helper function to get type of variable in data
def get_var_dtype(self, name):
# check for matching dtype in function scope first
for row in self.func_scope:
if row[0] == name:
return row[2]
# check for matching dtype in struct scope second
for row in self.struct_scope:
if row[0] == name:
return row[2]
# finally check for matching dtype in global scope
for row in self.global_scope:
if row[0] == name:
return row[2]
return "variable undeclared error"
# type checking visitors
@visitor.when(MultOpNode)
def visit(self, node):
left, _, right = node.children
if left.type == right.type:
node.type = left.type
else:
node.type = "typeerror"
self.errors += "error on line: " + str(node.line) + "\n"
self.errors += "mismatch between nodes of type: " + left.name + right.name + "\n"
@visitor.when(AddOpNode)
def visit(self, node):
left, _, right = node.children
if left.type == right.type:
node.type = left.type
else:
node.type = "typeerror"
self.errors += "error on line: " + str(node.line) + "\n"
self.errors += "mismatch between nodes of type: " + left.name + right.name + "\n"
@visitor.when(ArithExprNode)
def visit(self, node):
node.type = node.children[0].type
@visitor.when(RelExprNode)
def visit(self, node):
left, _, right = node.children
if left.type == right.type:
node.type = left.type
else:
node.type = "typeerror"
self.errors += "error on line: " + str(node.line) + "\n"
self.errors += "mismatch between nodes of type: " + left.name + right.name + "\n"
@visitor.when(VariableNode)
def visit(self, node):
id, indiceList = node.children
node.type = self.get_var_dtype(id.name)
if node.type == "variable undeclared error":
self.errors += "error on line: " + str(node.line) + "\n"
self.errors += "use of undeclared variable in the local scope\n"
for _ in indiceList.children:
# cut out one dimension of array type
if node.type:
node.type = node.type[:-3]
# check if dimensions match
if node.type and "]" in node.type:
self.errors += "error on line: " + str(node.line) + "\n"
self.errors += "invalid dimension of variable access for " + node.type + "\n"
@visitor.when(AssignNode)
def visit(self, node):
left, right = node.children
if left.type == right.type:
node.type = left.type
else:
node.type = "typeerror"
self.errors += "error on line: " + str(node.line) + "\n"
self.errors += "mismatch between nodes of type: " + left.name + right.name + "\n"
@visitor.when(DotNode)
def visit(self, node):
left, right = node.children
node.type = self.get_var_dtype(right.name)
if node.type == "variable undeclared error":
self.errors += "error on line: " + str(node.line) + "\n"
self.errors += "invalid access of member " + right.name + " for the class: " + left.name + "\n"
@visitor.when(IdNode)
def visit(self, node):
node.type = self.get_var_dtype(node.name)
@visitor.when(FuncCallNode)
def visit(self, node):
func, paramList = node.children
node.type = self.get_var_dtype(func.name)
if node.type == "variable undeclared error":
self.errors += "error on line: " + str(node.line) + "\n"
self.errors += "use of undeclared function in the global scope\n"