-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathastree.py
More file actions
263 lines (153 loc) · 5.41 KB
/
astree.py
File metadata and controls
263 lines (153 loc) · 5.41 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
#
# ASTree version 1.03
#
from enum import Enum
from typing import Optional
class Operator(Enum):
Or = 0
And = 1
Not = 2
Eq = 3
NotEq = 4
Lt = 5
Gt = 6
LtEq = 7
GtEq = 8
Is = 9
Plus = 10
Minus = 11
Mult = 12
IntDivide = 13
Modulus = 14
class Node:
pass
class IdentifierNode(Node):
def __init__(self, name: str):
self.name = name
#######################################################
class ExprNode:
pass
class LiteralExprNode(ExprNode):
pass
class NoneLiteralExprNode(LiteralExprNode):
pass
class StringLiteralExprNode(LiteralExprNode):
def __init__(self, value: str):
self.value = value
class IntegerLiteralExprNode(LiteralExprNode):
def __init__(self, value: int):
self.value = value
class BooleanLiteralExprNode(LiteralExprNode):
def __init__(self, value: bool):
self.value = value
class IdentifierExprNode(ExprNode):
def __init__(self, identifier: IdentifierNode):
self.identifier = identifier
class BinaryOpExprNode(ExprNode):
def __init__(self, op: Operator, lhs: ExprNode, rhs: ExprNode):
self.op = op
self.lhs = lhs
self.rhs = rhs
class UnaryOpExprNode(ExprNode):
def __init__(self, op: Operator, operand: ExprNode):
self.op = op
self.operand = operand
class IfExprNode(ExprNode):
def __init__(self, condition: ExprNode, then_expr: ExprNode, else_expr: ExprNode):
self.condition = condition
self.then_expr = then_expr
self.else_expr = else_expr
class IndexExprNode(ExprNode):
def __init__(self, list_expr: ExprNode, index: ExprNode):
self.list_expr = list_expr
self.index = index
class MemberExprNode(ExprNode):
def __init__(self, expr_object: ExprNode, member: IdentifierNode):
self.expr_object = expr_object
self.member = member
class FunctionCallExprNode(ExprNode):
def __init__(self, identifier: IdentifierNode, args: list[ExprNode]):
self.identifier = identifier
self.args = args
class MethodCallExprNode(ExprNode):
def __init__(self, member: MemberExprNode, args: list[Optional[ExprNode]]):
self.member = member
self.args = args
class ListExprNode(ExprNode):
def __init__(self, elements: list[ExprNode]):
self.elements = elements
#######################################################
class StmtNode:
pass
class ExprStmt(StmtNode):
def __init__(self, expr: ExprNode):
self.expr = expr
class PassStmtNode(StmtNode):
def __init__(self):
pass
class ReturnStmtNode(StmtNode):
def __init__(self, expr: Optional[ExprNode]):
self.expr = expr
class AssignStmtNode(StmtNode):
def __init__(self, targets: list[ExprNode], expr: ExprNode):
self.targets = targets
self.expr = expr
class IfStmtNode(StmtNode):
def __init__(self, condition: ExprNode, then_body: list[StmtNode],
elifs: list[Optional[tuple[ExprNode, list[StmtNode]]]], else_body: list[StmtNode]):
self.condition = condition
self.then_body = then_body
self.elifs = elifs
self.else_body = else_body
class WhileStmtNode(StmtNode):
def __init__(self, condition: ExprNode, body: list[StmtNode]):
self.condition = condition
self.body = body
class ForStmtNode(StmtNode):
def __init__(self, identifier: IdentifierNode, iterable: ExprNode, body: list[StmtNode]):
self.identifier = identifier
self.iterable = iterable
self.body = body
#######################################################
class TypeAnnotationNode(Node):
pass
class ClassTypeAnnotationNode(TypeAnnotationNode):
def __init__(self, name: str):
self.name = name
class ListTypeAnnotationNode(TypeAnnotationNode):
def __init__(self, elem_type: TypeAnnotationNode):
self.elem_type = elem_type
class TypedVarNode(Node):
def __init__(self, identifier: IdentifierNode, id_type: TypeAnnotationNode):
self.identifier = identifier
self.id_type = id_type
class DeclarationNode(Node):
pass
class VarDefNode(DeclarationNode):
def __init__(self, var: TypedVarNode, value: LiteralExprNode):
self.var = var
self.value = value
class GlobalDeclNode(DeclarationNode):
def __init__(self, variable: IdentifierNode):
self.variable = variable
class NonLocalDeclNode(DeclarationNode):
def __init__(self, variable: IdentifierNode):
self.variable = variable
class ClassDefNode(DeclarationNode):
def __init__(self, name: IdentifierNode, super_class: IdentifierNode, declarations: list[DeclarationNode]):
self.name = name
self.super_class = super_class
self.declarations = declarations
class FuncDefNode(DeclarationNode):
def __init__(self, name: IdentifierNode, params: list[Optional[TypedVarNode]],
return_type: Optional[TypeAnnotationNode], declarations: list[Optional[DeclarationNode]],
statements: list[StmtNode]):
self.name = name
self.params = params
self.return_type = return_type
self.declarations = declarations
self.statements = statements
class ProgramNode(Node):
def __init__(self, declarations: list[Optional[DeclarationNode]], statements: list[Optional[StmtNode]]):
self.declarations = declarations
self.statements = statements