-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
296 lines (251 loc) · 6.71 KB
/
model.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
from tokens import *
class Node:
'''
The parent class for every node in the AST
'''
pass
class Expr(Node):
'''
Expressions evaluate to a result, like x + (3 * y) >= 6
'''
pass
class Stmt(Node):
'''
Statements perform an action
'''
pass
class Decl(Stmt):
'''
Declarations are statements to declare a new name (in our case, functions)
'''
pass
class Integer(Expr):
'''
Example: 17
'''
def __init__(self, value, line):
assert isinstance(value, int), value
self.value = value
self.line = line
def __repr__(self):
return f'Integer[{self.value}]'
class Float(Expr):
'''
Example: 3.141592
'''
def __init__(self, value, line):
assert isinstance(value, float), value
self.value = value
self.line = line
def __repr__(self):
return f'Float[{self.value}]'
class Bool(Expr):
'''
Example: true, false
'''
def __init__(self, value, line):
assert isinstance(value, bool), value
self.value = value
self.line = line
def __repr__(self):
return f'Bool[{self.value}]'
class String(Expr):
'''
Example: 'this is a string'
'''
def __init__(self, value, line):
assert isinstance(value, str), value
self.value = value
self.line = line
def __repr__(self):
return f'String[{self.value}]'
class UnOp(Expr):
'''
Example: -operand
'''
def __init__(self, op: Token, operand: Expr, line):
assert isinstance(op, Token), op
assert isinstance(operand, Expr), operand
self.op = op
self.operand = operand
self.line = line
def __repr__(self):
return f'UnOp({self.op.lexeme!r}, {self.operand})'
class BinOp(Expr):
'''
Example: x + y
'''
def __init__(self, op: Token, left: Expr, right: Expr, line):
assert isinstance(op, Token), op
assert isinstance(left, Expr), left
assert isinstance(right, Expr), right
self.op = op
self.left = left
self.right = right
self.line = line
def __repr__(self):
return f'BinOp({self.op.lexeme!r}, {self.left}, {self.right})'
class LogicalOp(Expr):
'''
Example: x and y, x or y
'''
def __init__(self, op: Token, left: Expr, right: Expr, line):
assert isinstance(op, Token), op
assert isinstance(left, Expr), left
assert isinstance(right, Expr), right
self.op = op
self.left = left
self.right = right
self.line = line
def __repr__(self):
return f'LogicalOp({self.op.lexeme!r}, {self.left}, {self.right})'
class Grouping(Expr):
'''
Example: ( <expr> )
'''
def __init__(self, value, line):
assert isinstance(value, Expr), value
self.value = value
self.line = line
def __repr__(self):
return f'Grouping({self.value})'
class Identifier(Expr):
'''
Example: x, PI, _score, numLives, start_vel
'''
def __init__(self, name, line):
assert isinstance(name, str), name
self.name = name
self.line = line
def __repr__(self):
return f'Identifier[{self.name!r}]'
class Stmts(Node):
'''
A list of statements
'''
def __init__(self, stmts, line):
assert all(isinstance(stmt, Stmt) for stmt in stmts), stmts
self.stmts = stmts
self.line = line
def __repr__(self):
return f'Stmts({self.stmts})'
class PrintStmt(Stmt):
'''
Example: print value, println value
'''
def __init__(self, value, end, line):
assert isinstance(value, Expr), value
self.value = value
self.end = end
self.line = line
def __repr__(self):
return f'PrintStmt({self.value}, end={self.end!r})'
class IfStmt(Stmt):
'''
"if" <expr> "then" <then_stmts> ("else" <else_stmts>)? "end"
'''
def __init__(self, test, then_stmts, else_stmts, line):
assert isinstance(test, Expr), test
assert isinstance(then_stmts, Stmts), then_stmts
assert else_stmts is None or isinstance(else_stmts, Stmts), else_stmts
self.test = test
self.then_stmts = then_stmts
self.else_stmts = else_stmts
self.line = line
def __repr__(self):
return f'IfStmt({self.test}, then:{self.then_stmts}, else:{self.else_stmts})'
class WhileStmt(Stmt):
'''
"while" <expr> "do" <body_stmts> "end"
'''
def __init__(self, test, body_stmts, line):
assert isinstance(test, Expr), test
assert isinstance(body_stmts, Stmts), body_stmts
self.test = test
self.body_stmts = body_stmts
self.line = line
def __repr__(self):
return f'WhileStmt({self.test}, {self.body_stmts})'
class Assignment(Stmt):
'''
left := right
'''
def __init__(self, left, right, line):
assert isinstance(left, Expr), left
assert isinstance(right, Expr), right
self.left = left
self.right = right
self.line = line
def __repr__(self):
return f'Assignment({self.left}, {self.right})'
class ForStmt(Stmt):
'''
"for" <identifier> ":=" <start> "," <end> ("," <step>)? "do" <body_stmts> "end"
'''
def __init__(self, ident, start, end, step, body_stmts, line):
assert isinstance(ident, Identifier), ident
assert isinstance(start, Expr), start
assert isinstance(end, Expr), end
assert isinstance(step, Expr) or step is None, step
assert isinstance(body_stmts, Stmts), body_stmts
self.ident = ident
self.start = start
self.end = end
self.step = step
self.body_stmts = body_stmts
self.line = line
def __repr__(self):
return f'ForStmt({self.ident}, {self.start}, {self.end}, {self.step}, {self.body_stmts})'
class FuncDecl(Decl):
'''
"func" <name> "(" <params>? ")" <body_stmts> "end"
'''
def __init__(self, name, params, body_stmts, line):
assert isinstance(name, str), name
assert all(isinstance(param, Param) for param in params), params
self.name = name
self.params = params
self.body_stmts = body_stmts
self.line = line
def __repr__(self):
return f'FuncDecl({self.name!r}, {self.params}, {self.body_stmts})'
class Param(Decl):
'''
A single function parameter
'''
def __init__(self, name, line):
assert isinstance(name, str), name
self.name = name
self.line = line
def __repr__(self):
return f'Param[{self.name!r}]'
class FuncCall(Expr):
'''
<func_call> ::= <name> "(" <args>? ")"
<args> ::= <expr> ( ',' <expr> )*
'''
def __init__(self, name, args, line):
self.name = name
self.args = args
self.line = line
def __repr__(self):
return f'FuncCall({self.name!r}, {self.args})'
class FuncCallStmt(Stmt):
'''
A special type of statement used to wrap FuncCall expressions
'''
def __init__(self, expr):
assert isinstance(expr, FuncCall), expr
self.expr = expr
def __repr__(self):
return f'FuncCallStmt({self.expr})'
class RetStmt(Stmt):
'''
"ret" <expr>
'''
def __init__(self, value, line):
assert isinstance(value, Expr), value
self.value = value
self.line = line
def __repr__(self):
return f'RetStmt[{self.value}]'