forked from smohammadfy/Compiler_PLY
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.py
190 lines (123 loc) · 4.1 KB
/
ast.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
class AST(object):
_fields = []
def __init__(self, *args, **kwargs):
assert len(args) == len(self._fields)
for name, value in zip(self._fields, args):
setattr(self, name, value)
# Assign additional keyword arguments if supplied
for name, value in kwargs.items():
setattr(self, name, value)
def __repr__(self):
excluded = {"lineno"}
return "{}[{}]".format(self.__class__.__name__,
{key: value
for key, value in vars(self).items()
if not key.startswith("_") and not key in excluded})
class Literal(AST):
_fields = ['value']
class Typename(AST):
_fields = ['name']
class Location(AST):
_fields = ['name']
class LoadLocation(AST):
_fields = ['location']
class Unaryop(AST):
_fields = ['op', 'expr']
class Binop(AST):
_fields = ['op', 'left', 'right']
class Relop(AST):
_fields = ['op', 'left', 'right']
class AssignmentStatement(AST):
_fields = ['location', 'expr']
class PrintStatement(AST):
_fields = ['expr']
class Statements(AST):
_fields = ['statements']
def append(self, stmt):
self.statements.append(stmt)
def __len__(self):
return len(self.statements)
class Program(AST):
_fields = ['statements']
class VarDeclaration(AST):
_fields = ['name', 'typename', 'expr']
class ConstDeclaration(AST):
_fields = ['name', 'expr']
class IfStatement(AST):
_fields = ['expr', 'truebranch', 'falsebranch']
class WhileStatement(AST):
_fields = ['expr', 'truebranch']
class FuncStatement(AST):
_fields = ['name', 'returntype', 'parameters', 'expr']
class FuncParameterList(AST):
_fields = ['parameters']
def append(self, stmt):
self.parameters.append(stmt)
def __len__(self):
return len(self.parameters)
class FuncParameter(VarDeclaration):
pass
class FuncCall(AST):
_fields = ['name', 'arguments']
class FuncCallArguments(AST):
_fields = ['arguments']
def append(self, stmt):
self.arguments.append(stmt)
def __len__(self):
return len(self.arguments)
class FuncCallArgument(AST):
_fields = ['expr']
class ReturnStatement(AST):
_fields = ['expr']
class NodeVisitor(object):
def visit(self, node):
if node:
method = 'visit_' + node.__class__.__name__
visitor = getattr(self, method, self.generic_visit)
return visitor(node)
else:
return None
def generic_visit(self, node):
for field in getattr(node, "_fields"):
value = getattr(node, field, None)
if isinstance(value, list):
for item in value:
if isinstance(item, AST):
self.visit(item)
elif isinstance(value, AST):
self.visit(value)
class NodeTransformer(NodeVisitor):
def generic_visit(self, node):
for field in getattr(node, "_fields"):
value = getattr(node, field, None)
if isinstance(value, list):
newvalues = []
for item in value:
if isinstance(item, AST):
newnode = self.visit(item)
if newnode is not None:
newvalues.append(newnode)
else:
newvalues.append()
value[:] = newvalues
elif isinstance(value, AST):
newnode = self.visit(value)
if newnode is None:
delattr(node, field)
else:
setattr(node, field, newnode)
return node
# DO NOT MODIFY
def flatten(top):
class Flattener(NodeVisitor):
def __init__(self):
self.depth = 0
self.nodes = []
def generic_visit(self, node):
self.nodes.append((self.depth, node))
self.depth += 1
NodeVisitor.generic_visit(self, node)
self.depth -= 1
d = Flattener()
d.visit(top)
return d.nodes