-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathast.py
More file actions
227 lines (164 loc) · 5.67 KB
/
Copy pathast.py
File metadata and controls
227 lines (164 loc) · 5.67 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
from __future__ import unicode_literals
from littlepython.tokenizer import Token, TokenTypes
TAB_WIDTH = 2
TAB = " "*TAB_WIDTH
def _var(n):
return Var(Token(TokenTypes.VAR, n))
class AST(object):
def __eq__(self, other):
if type(self) != type(other):
return False
possible_attrs = ["token", "left", "right", "children", "sig", "block", "ifs", "else_block", "ctrl", "params",
"expr", "function", "arglist", "vals"]
for attr in possible_attrs:
if hasattr(self, attr) != hasattr(other, attr):
return False
if hasattr(self, attr) and getattr(self, attr) != getattr(other, attr):
return False
return True
def __ne__(self, other):
return not self.__eq__(other)
def __str__(self):
raise NotImplementedError("To be an AST you need to implement this.")
def __repr__(self):
return self.__str__()
class NoOp(AST):
def __str__(self):
return ""
class Int(AST):
def __init__(self, token):
self.token = token
self.value = token.value
def __str__(self):
return str(self.value)
class Array(AST):
def __init__(self, vals):
self.vals = vals
def __str__(self):
return "[{}]".format(", ".join(map(str, self.vals)))
class Var(AST):
def __init__(self, token):
self.token = token
self.value = token.value
def __str__(self):
return str(self.value)
class UnaryOp(AST):
def __init__(self, op, right):
self.token = self.op = op
self.right = right
def __str__(self):
return self.token.value + "(" + str(self.right) + ")"
class BinaryOp(AST):
def __init__(self, op, left, right):
self.token = self.op = op
self.left = left
self.right = right
def __str__(self):
# TODO: create better to string.
s = ""
if isinstance(self.left, BinaryOp):
s += "(" + str(self.left) + ")"
else:
s += str(self.left)
s += " " + self.token.value + " "
if isinstance(self.right, BinaryOp):
s += "(" + str(self.right) + ")"
else:
s += str(self.right)
# s += ")"
return s
class FunctionSig(AST):
# TODO: add return value to this
def __init__(self, params):
# Params should be a list of Vars.
self.params = params
def __str__(self):
return "("+", ".join(map(str, self.params))+")"
class Function(AST):
def __init__(self, sig, block):
assert isinstance(sig, FunctionSig)
self.sig = sig
self.block = block
def __str__(self):
return "lambda {sig}:{block}".format(sig=self.sig, block=self.block)
class FunctionDef(AST):
def __init__(self, name, function):
assert isinstance(name, Var)
assert isinstance(function, Function)
self.name = name
self.function = function
def __str__(self):
return "func {name}{sig} {block}".format(name=self.name, sig=self.function.sig, block=self.function.block)
class Assign(AST):
def __init__(self, op, left, right):
assert isinstance(left, Var)
self.token = self.op = op
self.left = left
self.right = right
def __str__(self):
return str(self.left) + " " + self.token.value + " " + str(self.right)
class Block(AST):
def __init__(self, children=None):
if children is None:
children = []
self.children = children
def __str__(self):
return "{\n" + "\n".join(map(lambda x: TAB + str(x), self.children)) + "\n}"
class If(AST):
def __init__(self, ctrl, block):
self.ctrl = ctrl
self.block = block
def __str__(self):
return "if " + str(self.ctrl) + " " + str(self.block)
class ForLoop(AST):
def __init__(self, init, ctrl, inc, block):
self.init = init
self.ctrl = ctrl
self.inc = inc
self.block = block
def __str__(self):
return "for " + "; ".join(map(str, (self.init, self.ctrl, self.inc))) + " " + str(self.block)
class ControlBlock(AST):
def __init__(self, ifs, else_block=None):
# This control must contain at least one if.
assert len(ifs) > 0
if else_block is None:
else_block = Block()
self.ifs = ifs
self.else_block = else_block
def __str__(self):
s = "if " + str(self.ifs[0].ctrl) + " " + str(self.ifs[0].block)
for _if in self.ifs[1:]:
s += " elif " + str(_if.ctrl) + " " + str(_if.block)
s += " else " + str(self.else_block)
return s
# Built-in functions
class GetArrayItem(Function):
def __init__(self, left, right):
sig = FunctionSig((_var("index"),))
super(GetArrayItem, self).__init__(sig, Block())
self.left = left
self.right = right
def __str__(self):
return str(self.left) + "[" + str(self.right) + "]"
class SetArrayItem(Function):
def __init__(self, left, right, expr):
sig = FunctionSig((_var("index"), _var("value")))
super(SetArrayItem, self).__init__(sig, Block())
self.left = left
self.right = right
self.expr = expr
def __str__(self):
return str(self.left) + "[" + str(self.right) + "] = " + str(self.expr)
class Call(AST):
def __init__(self, func, arglist):
assert isinstance(func, Var)
self.func = func
self.arglist = arglist
def __str__(self):
return "{}({})".format(self.func, ", ".join(map(str, self.arglist)))
class Return(AST):
def __init__(self, expr):
self.expr = expr
def __str__(self):
return "return " + str(self.expr)