-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbolizer.py
165 lines (118 loc) · 3.86 KB
/
symbolizer.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
from visitor import Visitor
class Symbol:
def __init__(self, id_, type_, scope):
self.id_ = id_
self.type_ = type_
self.scope = scope
def __str__(self):
return "<{} {} {}>".format(self.id_, self.type_, self.scope)
def copy(self):
return Symbol(self.id_, self.type_, self.scope)
class Symbols:
def __init__(self):
self.symbols = {}
def put(self, id_, type_, scope):
self.symbols[id_] = Symbol(id_, type_, scope)
def get(self, id_):
return self.symbols[id_]
def contains(self, id_):
return id_ in self.symbols
def remove(self, id_):
del self.symbols[id_]
def __len__(self):
return len(self.symbols)
def __str__(self):
out = ""
for _, value in self.symbols.items():
if len(out) > 0:
out += "\n"
out += str(value)
return out
def __iter__(self):
return iter(self.symbols.values())
def __next__(self):
return next(self.symbols.values())
class Symbolizer(Visitor):
def __init__(self, ast):
self.ast = ast
def visit_Program(self, parent, node):
node.symbols = Symbols()
for n in node.nodes:
self.visit(node, n)
def visit_Decl(self, parent, node):
parent.symbols.put(node.id_.value, node.type_.value, id(parent))
def visit_ArrayDecl(self, parent, node):
node.symbols = Symbols()
parent.symbols.put(node.id_.value, node.type_.value, id(parent))
def visit_ArrayElem(self, parent, node):
pass
def visit_Assign(self, parent, node):
pass
def visit_If(self, parent, node):
self.visit(node, node.true)
if node.false is not None:
self.visit(node, node.false)
def visit_While(self, parent, node):
self.visit(node, node.block)
def visit_For(self, parent, node):
self.visit(node, node.block)
def visit_RepeatUntil(self, parent, node):
self.visit(node, node.block)
def visit_FuncImpl(self, parent, node):
parent.symbols.put(node.id_.value, node.type_.value, id(parent))
self.visit(node, node.block)
self.visit(node, node.params)
def visit_ProcImpl(self, parent, node):
parent.symbols.put(node.id_.value, None, id(parent))
self.visit(node, node.block)
self.visit(node, node.params)
def visit_FuncCall(self, parent, node):
pass
def visit_ProcCall(self, parent, node):
pass
def visit_Block(self, parent, node):
if node.is_main:
parent.symbols.put('main', '', id(parent))
node.symbols = Symbols()
if node.var_decls is not None:
self.visit(node, node.var_decls)
for n in node.nodes:
self.visit(node, n)
def visit_VarDecl(self, parent, node):
for n in node.decls:
self.visit(parent, n)
def visit_Params(self, parent, node):
node.symbols = Symbols()
for p in node.params:
self.visit(node, p)
self.visit(parent.block, p)
def visit_Args(self, parent, node):
pass
def visit_Elems(self, parent, node):
pass
def visit_Break(self, parent, node):
pass
def visit_Continue(self, parent, node):
pass
def visit_Exit(self, parent, node):
pass
def visit_Type(self, parent, node):
pass
def visit_Int(self, parent, node):
pass
def visit_Char(self, parent, node):
pass
def visit_String(self, parent, node):
pass
def visit_Real(self, parent, node):
pass
def visit_Boolean(self, parent, node):
pass
def visit_Id(self, parent, node):
pass
def visit_BinOp(self, parent, node):
pass
def visit_UnOp(self, parent, node):
pass
def symbolize(self):
self.visit(None, self.ast)