-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
338 lines (268 loc) · 8.82 KB
/
Copy pathmodel.py
File metadata and controls
338 lines (268 loc) · 8.82 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
332
333
334
335
336
337
338
# code object here.
from tokens import Token
class Node:
'''
The parent class for every Node in AST.
'''
class Expr(Node):
'''
Expresion evaluate to a result
'''
class Stmt(Node):
'''
Statement perform an action
'''
class Decl(Stmt):
'''
a statement for decleration.
'''
class Integer(Expr):
def __init__(self ,value , line):
assert isinstance(value , int) , value
self.value = value
self.line = line
def __repr__(self) -> str:
return f"Integer [{self.value}]"
class Float(Expr):
def __init__(self , value , line):
assert isinstance(value , float) , value
self.value = value
self.line = line
def __repr__(self) -> str:
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) -> str:
return f"Bool[{self.value}]"
class String(Expr):
'''
Example : 'string' , "string"
'''
def __init__(self , value , line) -> None:
assert isinstance(value , str) , value
self.value = value
self.line = line
def __repr__(self) -> str:
return f"String[{self.value}]"
class UnOp(Expr):
'''
Example : -20
'''
def __init__(self , op : Token , value : Expr , line):
assert isinstance(op , Token) , op
assert isinstance(value , Expr) , value
self.op = op
self.value = value
self.line = line
def __repr__(self) -> str:
return f"Unary Operator ({self.op.lexeme},{self.value})"
class BinOp(Expr):
'''
Example : 10 + 12 * 3 / 4
'''
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) -> str:
return f"Binary Operator (Operator: [{self.op.lexeme}] , LHS: [{self.left}] , RHS: [{self.right}])"
class LogicalOp(Expr):
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) -> str:
return f"Logical Operator (Operator: [{self.op.lexeme}] , LHS: [{self.left}] , RHS: [{self.right}])"
class Grouping(Expr):
'''
Example : ( <expr> )
'''
def __init__(self , value : Expr , line):
assert isinstance(value , Expr) , value
self.value = value
self.line = line
def __repr__(self) -> str:
return f"Grouping({self.value})"
class Identifier(Expr):
'''
Example: x , PI , _score , some_value , _forNow
'''
def __init__(self , lexeme , line):
assert isinstance(lexeme , str) , lexeme
self.lexeme = lexeme
self.line = line
def __repr__(self) -> str:
return f"Identifier({self.lexeme})"
class Stmts(Node):
'''
contain the list of statements
'''
def __init__(self , stmts , line):
assert all(isinstance(stmt , Stmt) for stmt in stmts)
self.stmts = stmts
self.line = line
def __repr__(self) -> str:
return f"Statements({self.stmts})"
class Assignment(Stmt):
'''
Example: x := 12 , y := x + 1
'''
def __init__(self , left , right , line):
assert isinstance(left , Identifier), left
assert isinstance(right , Expr), right
self.left =left
self.right =right
self.line = line
def __repr__(self) -> str:
return f"Assigmnet({self.left} , {self.right})"
class LocalAssignment(Stmt):
'''
Example: local x := 12 , local y := x + 1
'''
def __init__(self , left , right , line):
assert isinstance(left , Identifier), left
assert isinstance(right , Expr), right
self.left =left
self.right =right
self.line = line
def __repr__(self) -> str:
return f"LocalAssigmnet({self.left} , {self.right})"
class PrintStmt(Stmt):
'''
print statement model
'''
def __init__(self , val , line , end):
assert isinstance(val , Expr) , val
self.value = val
self.line = line
self.end = end
def __repr__(self) -> str:
return f"PrintStmt({self.value} , end:{self.end!r})"
class PrintLnStmt(Stmt):
'''
print line statement model
'''
def __init__(self , val , line):
assert isinstance(val , Expr) , val
self.value = val
self.line = line
def __repr__(self) -> str:
return f"PrintLnStmt({self.value})"
class WhileStmt(Stmt):
'''
while <test_expr> do
<stmts>
end
'''
def __init__(self , test_expr , while_stmts , line):
assert isinstance(test_expr , Expr), test_expr
assert isinstance(while_stmts , Stmts), while_stmts
self.test_expr = test_expr
self.while_stmts = while_stmts
self.line = line
def __repr__(self) -> str:
return f"WhileStmt(test: [{self.test_expr}] , Stmts: [{self.while_stmts}])"
class IfStmt(Stmt):
'''
<ifStmt> ::= "if" <test_expr> "then" <then_stmt> ("else" <else_stmt>)? "end"
'''
def __init__(self , test_expr , then_stmt , else_stmt , line):
assert isinstance(test_expr , Expr) , test_expr
assert isinstance(then_stmt , Stmts) , then_stmt
assert else_stmt is None or isinstance(else_stmt , Stmts) , else_stmt
self.test_expr = test_expr
self.then_stmt = then_stmt
self.else_stmt = else_stmt
self.line = line
def __repr__(self):
return f"IfStmt(test:[{self.test_expr}] , then:[{self.then_stmt}] , else:[{self.else_stmt}])"
class ForStmt(Stmt):
'''
for <assignment> , <end> , (<stepper>)? do
<stmts>
end
'''
def __init__(self , identifier , start_expr , end_expr , stepper_expr , for_stmts , line):
assert isinstance(identifier , Identifier) , identifier
assert isinstance(start_expr , Expr) , start_expr
assert isinstance(end_expr , Expr) , end_expr
assert stepper_expr is None or isinstance(stepper_expr , Expr) , stepper_expr
assert isinstance(for_stmts , Stmts) , for_stmts
self.identifier = identifier
self.start_expr = start_expr
self.end_expr = end_expr
self.stepper_expr = stepper_expr
self.for_stmts = for_stmts
self.line = line
def __repr__(self) -> str:
return f"ForStmt(StartAssignment: [{self.identifier}], StartExpr: [{self.start_expr}] , EndExpr: [{self.end_expr}] , StepperExpr: [{self.stepper_expr}] , Stmts: [{self.for_stmts}])"
class FuncDecl(Decl):
'''
<func_decl> ::= "func" <name> "(" <params>? ")" <body_stmts> "end"
'''
def __init__(self , name , params , body_stmts , line):
assert isinstance(name , str), name
assert all(isinstance(param , Decl) for param in params), params
assert isinstance(body_stmts , Stmts) , body_stmts
self.name = name
self.params = params
self.body_stmts = body_stmts
self.line = line
def __repr__(self) -> str:
return f"FuncDecl(name: [{self.name}] , params: [{self.params}] , bodyStmts: [{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) -> str:
return f"Param({self.name})"
class FuncCall(Expr):
'''
functions also can work as Stmt and return no value.
<func_call> ::= <name> "(" <args>? ")"
<args> ::= <expr> ( "," <expr> )*
'''
def __init__(self , name , args , line) -> None:
assert isinstance(name , str) , name
self.name = name
self.args = args
self.line = line
def __repr__(self) -> str:
return f"FuncCall(Name: [{self.name}] , Args: [{self.args}])"
class FuncCallStmt(Stmt):
'''
a type of function call that wraps a function call expression in a statement
'''
def __init__(self , expr) -> None:
assert isinstance(expr , Expr) , expr
self.expr = expr
def __repr__(self) -> str:
return f"FuncCallStmt(Expr: [{self.expr}])"
class RetStmt(Stmt):
'''
return something or nothing. ret None , ret Expr
'''
def __init__(self , expr , line) -> None:
assert isinstance(expr , Expr) , expr
self.expr = expr
self.line = line
def __repr__(self) -> str:
return f"RetStmt({self.expr})"