-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.py
366 lines (328 loc) · 11.3 KB
/
ast.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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# Copyright (C) 2009 Corrado Zoccolo
# This file is part of py_llvm_compile.
# py_llvm_compile is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# py_llvm_compile is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with py_llvm_compile. If not, see <http://www.gnu.org/licenses/>.
from __future__ import division
class expression(object):
@staticmethod
def _loadbinops():
binops=('add','and','div','divmod','floordiv','lshift','mod','mul','or','pow','radd','rand','rdiv','rdivmod','rfloordiv','rlshift','rmod','rmul','ror','rpow','rrshift','rshift','rsub','rtruediv','rxor','sub','truediv','xor','getitem')
cmpops=('eq','ge','gt','le','lt','ne')
for i in binops+cmpops:
setattr(expression,'__'+i+'__',lambda s,o,ii=i:binop(ii,s,box(o)))
@staticmethod
def _loadunops():
unops=('abs','invert','neg','pos')
for i in unops:
setattr(expression,'__'+i+'__',lambda s,ii=i:unaryop(ii,s))
def __coerce__(self,other):
return (self,box(other))
def __call__(self,*arg):
return callexpr(self,arg)
def __str__(self):
pass
def __repr__(self):return str(self)
def _accept(self,v):
pass
def displ(self,v):
return binop('displ',self,box(v))
expression._loadbinops()
expression._loadunops()
# not overloaded:
# __cmp__
# __class__
# __delattr__
# __new__
# __doc__
# __getattribute__
# __getnewargs__
# __setattr__
# __hash__
# __str__
# __init__
# __index__
# basic type constructors (int, float, long)
# hex, oct
# nonzero
class callexpr(expression):
def __init__(self, fun, args):
self.__fun=fun
self.__args=[box(i) for i in args]
def __str__(self):
return str(self.__fun)+str(self.__args)
def _accept(self,v):
return v.visitCall(self.__fun,self.__args)
class binop(expression):
def __init__(self, op, left, right):
self.__op=op
self.__left=left
self.__right=right
def __str__(self):
return self.__op+'('+str(self.__left)+','+str(self.__right)+')'
def _accept(self,v):
return v.visit2(self.__op,self.__left,self.__right)
class unaryop(expression):
def __init__(self, op, left):
self.__op=op
self.__left=left
def __str__(self):
return self.__op+'('+str(self.__left)+')'
def _accept(self,v):
return v.visit1(self.__op,self.__left)
class variable(expression):
def __init__(self, n=None):
self.__name=n if not n is None else '_var_$'+str(id(self))
def __str__(self):
return self.__name
def _accept(self,v):
return v.visitN(self.__name)
def name(self):
return self.__name
class const(expression):
def __init__(self, v):
self.__value=v
def __str__(self):
return str(self.__value)
def _accept(self,v):
return v.visitC(self.__value)
class condition(expression):
def __init__(self, cond, on_true, on_false):
self.__cond=box(cond)
self.__left=box(on_true)
self.__right=box(on_false)
def __str__(self):
return str(self.__cond)+'?'+str(self.__left)+':'+str(self.__right)
def _accept(self,v):
return v.visitIf(self.__cond,self.__left,self.__right)
class extern(expression):
def __init__(self, name, return_type, types):
self.__name = name
self.__return_ty = return_type
self.__types = types
def __str__(self):
return 'extern '+str(self.__return_ty)+' '+self.__name+'('+str(self.__types)+')'
def _accept(self,v):
return v.visitExtern(self.__name,self.__return_ty,self.__types)
class function(expression):
def __init__(self, name, vars, types, expr):
self.__name = name
self.__var_types = zip(vars,types)
self.__vars = vars
self.__types = types
self.__expr = expr
def __str__(self):
return 'function '+self.__name+'('+str(self.__var_types)+','+str(self.__expr)+')'
def _accept(self,v):
return v.visitFunc(self.__name,self.__vars,self.__types,self.__expr)
class lamb(expression):
def __init__(self, vars, types, expr):
self.__var_types = zip(vars,types)
self.__vars = vars
self.__types = types
self.__expr = expr
def __str__(self):
return 'lamb('+str(self.__var_types)+','+str(self.__expr)+')'
def _accept(self,v,fixVarName=None):
return v.visitLambda(self.__vars,self.__types,self.__expr,fixVarName=fixVarName)
def lamb_int(self):
return (self.__var_types,self.__expr)
class fix(expression):
def __init__(self, var, expr):
self.__var=var.name()
self.__expr=expr
def __str__(self):
return 'fix('+str(self.__var)+','+str(self.__expr)+')'
def _accept(self,v):
return v.visitFix(self.__var,self.__expr)
def box(expr):
if isinstance(expr,expression):
return expr
else:
return const(expr)
class visitor(object):
def visitC(self,v):
pass
def visitN(self,name):
pass
def visit1(self,op,l):
pass
def visit2(self,op,l,r):
pass
def visitIf(self,cond,left,right):
pass
def visitFix(self,varName,expr):
pass
def visitExtern(self,name,rt,types):
pass
def visitFunc(self,name,vars,types,expr):
pass
def visitLambda(self,vars,types,expr):
pass
def visitCall(self,fun,args):
pass
class eval_visitor(visitor):
def __init__(self,vars,ops):
self.vars=vars
self.ops=ops
def new_scope(self,assignments):
nv=eval_visitor(self.vars.copy(),self.ops)
for (name,val) in assignments:
nv.vars[name]=val
return nv
def __str__(self):
return 'scope('+str(self.vars)+','+str(self.ops)+')'
def visitC(self,v):
return v
def visitN(self,name):
return self.vars[name]
def visit1(self,op,l):
return self.ops[op](l._accept(self))
def visit2(self,op,l,r):
return self.ops[op](l._accept(self),r._accept(self))
def visitIf(self,cond,left,right):
return left._accept(self) if cond._accept(self) else right._accept(self)
def visitLambda(self,vars,types,expr):
vars=[v.name() for v in vars]
return (lambda ev=self,v=vars,ty=types,ex=expr:lambda *args: ex._accept(ev.new_scope(zip(v,(t(a) for (t,a) in zip(ty,args))))))()
def visitExtern(self,name,rt,types):
return self.ops['$ext.'+name]
def visitFunc(self,name,vars,types,expr):
#imperfect emulation: cannot export function definition
fun=self.visitLambda(vars,types,expr)
self.ops['$ext.'+name]=fun
return fun
def visitFix(self,varName,expr):
revisitor=eval_visitor(self.vars.copy(),self.ops)
revisitor.vars[varName]=lambda *args: expr._accept(revisitor)(*args)
return revisitor.vars[varName]
def visitCall(self,fun,args):
return self.ops['call'](fun._accept(self),(i._accept(self) for i in args))
def __ni(x,y):raise(NotImplementedError())
python_ops={'add':lambda x,y:x + y,
'and':lambda x,y:x and y,
'div':lambda x,y:x / y,
'divmod':lambda x,y:divmod(x, y),
'floordiv':lambda x,y:x // y,
'lshift':lambda x,y:x << y,
'mod':lambda x,y:x % y,
'mul':lambda x,y:x * y,
'or':lambda x,y:x or y,
'pow':lambda x,y:x ** y,
'rshift':lambda x,y:x >> y,
'sub':lambda x,y:x - y,
'truediv':lambda x,y:x / y,
'xor':lambda x,y:x ^ y,
'radd':lambda y,x:x + y,
'rand':lambda y,x:x and y,
'rdiv':lambda y,x:x / y,
'rdivmod':lambda y,x:divmod(x, y),
'rfloordiv':lambda y,x:x // y,
'rlshift':lambda y,x:x << y,
'rmod':lambda y,x:x % y,
'rmul':lambda y,x:x * y,
'ror':lambda y,x:x or y,
'rpow':lambda y,x:x ** y,
'rrshift':lambda y,x:x >> y,
'rsub':lambda y,x:x - y,
'rtruediv':lambda y,x:x / y,
'rxor':lambda y,x:x ^ y,
'getitem':lambda x,y:x[y],
'call':lambda x,y:x(*y),
'eq':lambda x,y: x == y,
'ge':lambda x,y: x >= y,
'gt':lambda x,y: x > y,
'le':lambda x,y: x <= y,
'lt':lambda x,y: x < y,
'ne':lambda x,y: x != y,
'invert':lambda x: ~x,
'abs':lambda x: abs(x),
'neg':lambda x: -x,
'pos':lambda x: +x
}
def eval(expression, vars, ops=python_ops):
return expression._accept(eval_visitor(vars,ops))
class type_visitor(object):
def integer(self,bits): pass
def real(self,bits): pass
def pointer(self, type): pass
def array(self, type, l): pass
def struct(self, types): pass
class type(object):
def __call__(self,x):
return x
def __str__(self):return 'type'
def __repr__(self):return str(self)
def _accept(self,v):
pass
class integer(type):
def __init__(self,bits=32):
self.bits=bits
def __call__(self,x):
if self.bits==1: return 1 if x else 0
return int(x) & ((1 << self.bits) - 1)
def __str__(self):
return 'bool_t' if self.bits==1 else 'int'+str(self.bits)+'_t'
def _accept(self,v):
return v.integer(self.bits)
class real(type):
def __init__(self,bits=64):
self.bits=bits
def __call__(self,x):
return float(x)
def __str__(self):
return 'float_t' if self.bits==32 else 'double_t' if self.bits==64 else 'real_'+str(self.bits)+'_t'
def _accept(self,v):
return v.real(self.bits)
class pointer(type):
def __init__(self,pointee):
self.element=pointee
def __str__(self):
return str(self.element)+'*'
def _accept(self,v):
return v.pointer(self.element)
class array(type):
def __init__(self,element,l):
self.element=element
self.len=l
def __str__(self):
return str(self.element)+'['+str(self.len)+'+'
def _accept(self,v):
return v.array(self.element,self.len)
class struct(type):
def __init__(self,types):
self.types=types
def __call__(self,xs):
return [t(x) for (x,t) in zip(xs,self.types)]
def __str__(self):
return str(self.types)
def _accept(self,v):
return v.struct(self.types)
bool_t = integer(1)
int8_t = integer(8)
int32_t = integer(32)
int64_t = integer(64)
float_t = real(32)
double_t = real(64)
cstring = pointer(int8_t)
def _test():
x=variable('x')
y=variable('y')
f=variable('f')
e=condition(x,on_true=1+x,on_false=x+2)
print e
v={'x':0}
print eval(e,v)
print eval(f(x,y),{'f':lambda z,w:z+w,'x':1,'y':2})
e=fix(f,lamb([x],[int32_t],condition(x>0,x*f(x-1),1)))
print e(y)
e=function('z',[x,y],[int32_t,int32_t],x+y)
print e(1,2)+extern('z',int32_t,[int32_t,int32_t])(2,3)
print eval(e(1,2)+extern('z',int32_t,[int32_t,int32_t])(2,3),{'y':7})