-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAst.py
More file actions
331 lines (225 loc) · 7.54 KB
/
Ast.py
File metadata and controls
331 lines (225 loc) · 7.54 KB
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
from Type import intType, floatType, stringType, unitType
class Ast:
def __init__(self, type, children=None, lineno=0):
self.type = type
self.lineno = lineno
self.children = children if children is not None else []
def __str__(self):
return str(self.type)
class For(Ast):
def __init__(self, id, range, body, lineno=0):
super().__init__('For', children=[id, range, body], lineno=lineno)
def id(self):
return self.children[0]
def range(self):
return self.children[1]
def body(self):
return self.children[2]
class While(Ast):
def __init__(self, condition, block, lineno=0):
super().__init__('While', children=[condition, block], lineno=lineno)
def condition(self):
return self.children[0]
def body(self):
return self.children[1]
class Break(Ast):
def __init__(self, lineno=0):
super().__init__('Break', lineno=lineno)
class Continue(Ast):
def __init__(self, lineno=0):
super().__init__('Continue', lineno=lineno)
class Return(Ast):
def __init__(self, expression, lineno=0):
super().__init__('Return', children=[expression], lineno=lineno)
class FunctionCall(Ast):
"""
Built-in functions:
- transpose (matrix transpose) -> take vector<>
- negative (numerical negative) -> float, int, vector
- zeros -> (int...) -> zeros(1, 2, 3)
- ones -> (int...) -> ones(1, 2, 3)
And also objectCalls like:
- .+
- .-
- .*
- ./
"""
def __init__(self, functionName, arglist, lineno=0):
super().__init__('FunctionCall', children=[
Leaf(functionName)] + arglist, lineno=lineno)
def args(self):
return self.children[1:]
def functionName(self):
return self.children[0].value()
class Vector(Ast):
def __init__(self, elements, lineno=0):
super().__init__('Vector', children=elements, lineno=lineno)
def elements(self):
return self.children
def dimensions(self):
node = self
dims = []
while type(node) is Vector:
dims.append(len(node.children))
if len(node.children) > 0:
node = node.children[0]
else:
break
return dims
def hasNestedVectors(self):
return len(self.children) > 0 and type(self.children[0]) is Vector
class SlicedVector(Ast):
def __init__(self, id, ranges, lineno=0):
super().__init__('VectorSlice', children=[id] + ranges, lineno=lineno)
def ranges(self):
return self.children[1:]
def id(self):
return self.children[0]
class Bind(Ast):
def __init__(self, id, assignOperator, expression, lineno=0):
super().__init__(assignOperator, children=[
id, expression], lineno=lineno)
def name(self):
return self.children[0].name()
def expression(self):
return self.children[1]
def operator(self):
return self.type
class BindWithSlice(Ast):
def __init__(self, slicedVector, assignOperator, expression, lineno=0):
super().__init__(assignOperator, children=[
slicedVector, expression], lineno=lineno)
def operator(self):
return self.type
def slicedVector(self):
return self.children[0]
def expression(self):
return self.children[1]
class Range(Ast):
"""
For example:
'2:5'
"""
def __init__(self, begin, end, lineno=0):
super().__init__('Range', children=[begin, end], lineno=lineno)
def begin(self):
return self.children[0]
def end(self):
return self.children[1]
def rangeTypeIdentifier(self):
return 1
class FromStartRange(Ast):
"""
For example:
' :5'
"""
def __init__(self, end, lineno=0):
super().__init__('FromStartRange', children=[end], lineno=lineno)
def end(self):
return self.children[0]
class EndlessRange(Ast):
"""
For example:
'5:'
"""
def __init__(self, begin, lineno=0):
super().__init__('EndlessRange', children=[begin], lineno=lineno)
def begin(self):
return self.children[0]
class SimpleRange(Ast):
"""
This is a single index.
"""
def __init__(self, idx, lineno=0):
super().__init__('SimpleRange', children=[idx], lineno=lineno)
def idx(self):
return self.children[0]
def rangeTypeIdentifier(self):
return 0
class BinaryOp(Ast):
def __init__(self, operator, left, right, lineno=0):
super().__init__(operator, children=[left, right], lineno=lineno)
def operator(self):
return self.type
def left(self):
return self.children[0]
def right(self):
return self.children[1]
class If(Ast):
def __init__(self, condition, trueBlock, lineno=0):
super().__init__('If', children=[condition, trueBlock], lineno=lineno)
def condition(self):
return self.children[0]
def trueBlock(self):
return self.children[1]
class IfElse(Ast):
def __init__(self, condition, trueBlock, falseBlock, lineno=0):
super().__init__('IfElse', children=[
condition, trueBlock, falseBlock], lineno=lineno)
def condition(self):
return self.children[0]
def trueBlock(self):
return self.children[1]
def falseBlock(self):
return self.children[2]
class Identifier(Ast):
def __init__(self, id, lineno=0):
super().__init__('ID', children=[Leaf(id)], lineno=lineno)
def name(self):
return self.children[0].value()
class Primitive(Ast):
"""
We might wanna create separate class for each primitive but for now this works.
"""
def __init__(self, type, value, lineno=0):
super().__init__(type, children=[Leaf(value)], lineno=lineno)
def value(self):
return self.children[0].value()
class Leaf(Ast):
def __init__(self, value, lineno=0):
super().__init__(value, children=[], lineno=lineno)
def value(self):
return self.type
class CodeBlock(Ast):
def __init__(self, statements, lineno=0):
"""
Constructor avoids nesting code blocks
"""
if len(statements) == 1 and type(statements[0]) is CodeBlock:
nestedCodeBlock = statements[0]
statements = nestedCodeBlock.children
super().__init__('CodeBlock', children=statements, lineno=lineno)
class Prototype(Ast):
def __init__(self, name, ret, args):
super().__init__('Prototype')
self.name = name
self.ret = ret
self.args = args
def name(self):
return self.name
def retType(self):
return self.ret
def args(self):
return self.args
class Function(Ast):
_anonymous_function_counter = 0
def __init__(self, proto, body):
super().__init__('Function')
self.proto = proto
self.body = body
@classmethod
def anonymous(cls, body):
cls._anonymous_function_counter += 1
proto = Prototype(
f'_anon_{cls._anonymous_function_counter}', unitType, [])
return cls(proto, body)
def isAnonymous(self):
return self.proto.name().startswith('_anon_')
def String(value, lineno=0):
return Primitive(stringType, value, lineno=lineno)
def Int(value, lineno=0):
return Primitive(intType, value, lineno=lineno)
def Float(value, lineno=0):
return Primitive(floatType, value, lineno=lineno)
def emptyVector():
return Vector([])