-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAST.py
More file actions
266 lines (186 loc) · 7.86 KB
/
AST.py
File metadata and controls
266 lines (186 loc) · 7.86 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
from fractions import Fraction
class Expr:
def __repr__(self):
return str(self)
class Add(Expr):
def __init__(self, addends):
self.terms = addends
def __str__(self):
return "(+, " + ", ".join(map(str, self.terms)) + ")"
def __eq__(self, other):
return isinstance(other, Add) and self.terms == other.terms
def contains(self, expr):
return self == expr or any(map(lambda e: e.contains(expr), self.terms))
def sub(self, find, replace):
return replace if self == find else Add([c.sub(find, replace) for c in self.terms])
def map(self, fun):
return fun(Add([x.map(fun) for x in self.terms]))
class Sub(Expr):
def __init__(self, left, right):
self.left = left
self.right = right
def __str__(self):
return "(-, " + str(self.left) + ", " + str(self.right) + ")"
def __eq__(self, other):
return isinstance(other, Sub) and self.left == other.left and self.right == other.right
def contains(self, expr):
return self == expr or self.left.contains(expr) or self.right.contains(expr)
def sub(self, find, replace):
return replace if self == find else Sub(self.left.sub(find, replace), self.right.sub(find, replace))
def map(self, fun):
return fun(Sub(self.left.map(fun), self.right.map(fun)))
class Mul(Expr):
def __init__(self, factors):
self.factors = factors
def __str__(self):
return "(*, " + ", ".join(map(str, self.factors)) + ")"
def __eq__(self, other):
return isinstance(other, Mul) and self.factors == other.factors
def contains(self, expr):
return self == expr or any(map(lambda e: e.contains(expr), self.factors))
def sub(self, find, replace):
return replace if self == find else Mul([c.sub(find, replace) for c in self.factors])
def map(self, fun):
return fun(Mul([x.map(fun) for x in self.factors]))
class Div(Expr):
def __init__(self, top, bottom):
self.top = top
self.bottom = bottom
def __str__(self):
return "(/, " + str(self.top) + ", " + str(self.bottom) + ")"
def __eq__(self, other):
return isinstance(other, Div) and self.top == other.top and self.bottom == other.bottom
def contains(self, expr):
return self == expr or self.top.contains(expr) or self.bottom.contains(expr)
def sub(self, find, replace):
return replace if self == find else Div(self.top.sub(find, replace), self.bottom.sub(find, replace))
def map(self, fun):
return fun(Div(self.top.map(fun), self.bottom.map(fun)))
class Pow(Expr):
def __init__(self, base, exp):
self.base = base
self.exp = exp
def __str__(self):
return "(^, " + str(self.base) + ", " + str(self.exp) + ")"
def __eq__(self, other):
return isinstance(other, Pow) and self.base == other.base and self.exp == other.exp
def contains(self, expr):
return self == expr or self.base.contains(expr) or self.exp.contains(expr)
def sub(self, find, replace):
return replace if self == find else Pow(self.base.sub(find, replace), self.exp.sub(find, replace))
def map(self, fun):
return fun(Pow(self.base.map(fun), self.exp.map(fun)))
class Neg(Expr):
def __init__(self, exp):
self.exp = exp
def __str__(self):
return "(-, " + str(self.exp) + ")"
def __eq__(self, other):
return isinstance(other, Neg) and self.exp == other.exp
def contains(self, expr):
return self == expr or self.exp.contains(expr)
def sub(self, find, replace):
return replace if self == find else Neg(self.exp.sub(find, replace))
def map(self, fun):
return fun(Neg(self.exp.map(fun)))
class Num(Expr):
def __init__(self, val):
try:
self.val = Fraction(val)
except:
self.val = val
def __str__(self):
return str(self.val)
def __eq__(self, other):
return isinstance(other, Num) and self.val == other.val
def contains(self, expr):
return False
def sub(self, find, replace):
return replace if self == find else self
def map(self, fun):
return fun(self)
class Var(Expr):
def __init__(self, sym):
self.sym = sym
def __str__(self):
return self.sym
def __eq__(self, other):
return isinstance(other, Var) and self.sym == other.sym
def contains(self, expr):
return self == expr
def sub(self, find, replace):
return replace if self == find else self
def map(self, fun):
return fun(self)
class Deriv(Expr):
def __init__(self, expr, sym):
self.sym = sym
self.expr = expr
def __str__(self):
return "(D, " + str(self.expr) + ", " + str(self.sym) + ")"
def __eq__(self, other):
return isinstance(other, Deriv) and self.expr == other.expr and self.sym == other.sym
def contains(self, expr):
return self == expr or self.sym.contains(expr) or self.expr.contains(expr)
def sub(self, find, replace):
return replace if self == find else Deriv(self.expr.sub(find, replace), self.sym.sub(find, replace))
def map(self, fun):
# should we modify the sym?
return fun(Deriv(self.expr.map(fun), self.sym.map(fun)))
class Int(Expr):
def __init__(self, expr, sym):
self.sym = sym
self.expr = expr
def __str__(self):
return "(I, " + str(self.expr) + ", " + str(self.sym) + ")"
def __eq__(self, other):
return isinstance(other, Int) and self.expr == other.expr and self.sym == other.sym
def contains(self, expr):
return self == expr or self.sym.contains(expr) or self.expr.contains(expr)
def sub(self, find, replace):
return replace if self == find else Int(self.expr.sub(find, replace), self.sym.sub(find, replace))
def map(self, fun):
# should we modify the sym?
return fun(Int(self.expr.map(fun), self.sym.map(fun)))
class DefInt(Expr):
def __init__(self, expr, sym, lower, upper):
self.sym = sym
self.expr = expr
self.lower = lower
self.upper = upper
def __str__(self):
return "(I, " + ", ".join(map(str, [self.expr, self.sym, self.lower, self.upper])) + ")"
def __eq__(self, other):
return isinstance(other, DefInt) and [self.expr, self.sym, self.lower, self.upper] == [other.expr, other.sym, other.lower, other.upper]
def contains(self, expr):
return self == expr or self.sym.contains(expr) or self.expr.contains(expr)
def sub(self, find, replace):
# TODO: handle when subbing in something involving the variable we're integrating wrt
return replace if self == find else DefInt(self.expr.sub(find, replace), self.sym.sub(find, replace), \
self.lower.sub(find, replace), self.upper.sub(find, replace))
def map(self, fun):
# should we modify the sym?
return fun(DefInt(self.expr.map(fun), self.sym.map(fun)), self.lower.map(fun), self.upper.map(fun))
class Apply(Expr):
def __init__(self, fun, expr):
self.fun = fun
self.expr = expr
def __str__(self):
return "(" + str(self.fun) + ", " + str(self.expr) + ")"
def __eq__(self, other):
return isinstance(other, Apply) and self.expr == other.expr and self.fun == other.fun
def contains(self, expr):
return self == expr or self.expr.contains(expr)
def sub(self, find, replace):
return replace if self == find else Apply(self.fun, self.expr.sub(find, replace))
def map(self, fun):
return fun(Apply(self.fun.map(fun), self.expr.map(fun)))
class Fun:
def __init__(self, sym):
self.sym = sym
def __str__(self):
return self.sym
def __eq__(self, other):
return isinstance(other, Fun) and self.sym == other.sym
def map(self, fun):
return fun(self)