Repository navigation
Expand file tree
/
Copy pathnode.py
More file actions
418 lines (354 loc) · 10.9 KB
/
Copy pathnode.py
File metadata and controls
418 lines (354 loc) · 10.9 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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# SMOP compiler -- Simple Matlab/Octave to Python compiler
# Copyright 2011-2013 Victor Leikehman
from __future__ import print_function
from collections import namedtuple
import copy,sys,inspect
from . recipes import recordtype
from . import options
# def preorder(u):
# if isinstance(u,traversable):
# yield u
# for n in u:
# for t in preorder(n):
# yield t
def decode(self):
r = ""
s = self.name
while s:
if len(s) >= 2 and s[0] == "_":
r += s[1].upper()
s = s[2:]
else:
r += s[0].lower()
s = s[1:]
return r
def encode(s):
return "".join(c+"_" if c.isupper() or c=="_" else c.upper() for c in s)
def postorder(u):
if isinstance(u,node):
for v in u:
for t in postorder(v):
yield t
yield u # returns only traversible objects
def extend(cls):
return lambda f: (setattr(cls,f.__name__,f) or f)
def exceptions(f):
return f
def wrapper(self,*args,**kwargs):
try:
return f(self,*args,**kwargs)
except:
print("%s.%s()" % (self.__class__.__name__, f.__name__))
raise
wrapper.__name__ = f.__name__
wrapper.__doc__ = f.__doc__
return wrapper
class node(object):
def become(self,other):
class Wrapper(self.__class__):
def __copy__(self):
other = object.__getattribute__(self,"other")
return copy.copy(other)
def __getattribute__(self,name):
other = object.__getattribute__(self,"other")
return getattr(other,name)
def __setattr__(self,name,value):
other = object.__getattribute__(self,"other")
return setattr(other,name,value)
def __iter__(self):
other = object.__getattribute__(self,"other")
return iter(other)
#def __hash__(self):
# other = object.__getattribute__(self,"other")
# return id(other)
def __repr__(self):
other = object.__getattribute__(self,"other")
return repr(other)
def __len__(self):
other = object.__getattribute__(self,"other")
return len(other)
assert self != other
self.other = other
self.__class__ = Wrapper
def _type(self):
raise AttributeError("_type")
######### LISTS
class concat_list(node,list):
pass
class global_list(node,list):
"""space-separated list of variables used in GLOBAL statement"""
pass
class expr_list(node,list):
def __str__(self):
return ",".join([str(t) for t in self])
def __repr__(self):
return "expr_list(%s)" % list.__repr__(self)
class stmt_list(node,list):
def __str__(self):
return "\n".join([str(t) for t in self])
def __repr__(self):
return "stmt_list(%s)" % list.__repr__(self)
def append(self,s):
assert isinstance(s,node), s.__class__
list.append(self,s)
#####################
#
# ATOMS
class atom(node): pass
class string(atom,recordtype("string", "value lineno lexpos", default=None)):
def __str__(self):
return "'%s'" % self.value
class logical(atom,recordtype("logical", "value lineno lexpos", default=None)):
pass
class number(atom,recordtype("number","value lineno lexpos",default=None)):
def __str__(self):
return str(self.value)
class ident(atom,recordtype("ident","name lineno column lexpos defs props init",
default=None)):
def __str__(self):
return self.name
class param(ident):
pass
###########################
#
# STATEMENTS
#
class stmt(node): pass
# class call_stmt(stmt,recordtype("call_stmt","func_expr args ret")):
# """Sometimes called multiple assignment, call statements represent
# something like [x,y]=foo(a,b,c); Note the square brackets around
# the lhs.
# SEE ALSO: funcall,let
# """
# def __str__(self):
# return "%s=%s(%s)" % (str(self.ret),
# str(self.func_expr),
# str(self.args))
class let(stmt,recordtype("let",
"ret args lineno lexpos nargout",
default=None)):
"""Assignment statement, except [x,y]=foo(x,y,z),
which is handled by call_stmt."""
def __str__(self):
return "%s=%s" % (str(self.ret), str(self.args))
class func_stmt(stmt,recordtype("func_stmt",
"""
ident
ret
args
stmt_list
use_nargin
""",
default=None)):
pass
class lambda_expr(func_stmt):
pass
class function(stmt,recordtype("function","head body")):
pass
class for_stmt(stmt,recordtype("for_stmt","ident expr stmt_list")):
pass
class DO_STMT(stmt,recordtype("DO_STMT","ident start stop stmt_list")):
pass
# We generate where_stmt to implement A(B==C) = D
class where_stmt(stmt,recordtype("where_stmt","cond_expr stmt_list")):
pass
class if_stmt(stmt,recordtype("if_stmt","cond_expr then_stmt else_stmt")):
pass
class global_stmt(stmt,recordtype("global_stmt","global_list")):
def __str__(self):
return "global %s" % str(self.global_list)
class persistent_stmt(stmt,recordtype("persistent_stmt","global_list")):
def __str__(self):
return "global %s" % str(self.global_list)
class return_stmt(stmt,namedtuple("return_stmt","ret")):
def __str__(self):
return "return"
class comment_stmt(stmt,namedtuple("comment_stmt","value")):
def __str__(self):
return self.value
class end_stmt(stmt,namedtuple("end_stmt","dummy")):
def __str__(self):
return "end"
class continue_stmt(stmt,namedtuple("continue_stmt","dummy")):
def __str__(self):
return "continue"
class break_stmt(stmt,namedtuple("break_stmt","dummy")):
def __str__(self):
return "break"
class pass_stmt(stmt,namedtuple("pass_stmt","")):
def __str__(self):
return "pass"
class null_stmt(stmt,namedtuple("null_stmt","")):
def __str__(self):
return ";"
class expr_stmt(stmt,node,recordtype("expr_stmt","expr")):
def __str__(self):
return str(self.expr)
class while_stmt(stmt,node,recordtype("while_stmt","cond_expr stmt_list")):
pass
class try_catch(stmt,recordtype("try_catch","try_stmt catch_stmt finally_stmt")):
pass
class allocate_stmt(stmt,recordtype("allocate_stmt",
"ident args")):
pass
#######################################333
#
# FUNCALL
class funcall(node,recordtype("funcall","func_expr args nargout",default=None)):
"""Funcall instances represent
(a) Array references, both lhs and rhs
(b) Function call expressions
"""
def __str__(self):
return "%s(%s)" % (str(self.func_expr),
str(self.args))
class builtins(funcall):
"""
Builtin functions are represented as subclasses
of class builtins. Application of a function to
specific arguments is represented as its instance.
For example, builtin function foo is represented
as class foo(builtins), and foo(x) is represented
as foo(x).
"""
def __init__(self,*args,**kwargs):
"""
If a built-in function _foo takes three arguments
a, b, and c, we can just say _foo(a,b,c) and let
the constructor take care of proper structuring of
the node (like expr_list around the arguments, etc.
"""
funcall.__init__(self,
func_expr=None,
args=expr_list(args),
**kwargs)
#import pdb; pdb.set_trace()
#self.func_expr.defs = set(self.func_expr)
def __repr__(self):
return "np.%s%s" % (self.__class__,repr(self.args))
def __str__(self):
return "np.%s(%s)" % (self.__class__.__name__,
str(self.args))
class arrayref(funcall):
def __repr__(self):
return "%s%s[%s]" % (self.__class__,
self.func_expr,
self.args)
########################## EXPR
class expr(node,recordtype("expr","op args")):
def __str__(self):
if self.op == ".":
return "%s%s" % (str(self.args[0]),self.args[1])
if self.op == "parens":
return "(%s)" % str(self.args[0])
if not self.args:
return str(self.op)
if len(self.args) == 1:
return "%s%s" % (self.op,self.args[0])
if len(self.args) == 2:
return "%s%s%s" % (self.args[0]._backend(),
self.op,
self.args[1]._backend())
ret = "%s=" % str(self.ret) if self.ret else ""
return ret+"%s(%s)" % (self.op,
",".join([str(t) for t in self.args]))
# names in caps correspond to fortran funcs
builtins_list = [
# "ABS",
# "ALL",
# "ANY",
# "CEILING",
# "FIND",
# "ISNAN",
# "MAXVAL",
# "MINVAL",
# "MODULO",
# "RAND",
# "RESHAPE",
# "SHAPE",
# "SIGN",
# "SIZE",
# "SUM",
#"abs",
"add", # synthetic opcode
#"all",
#"any",
"cellfun",
#"ceil",
"clazz",
#"cumprod",
#"cumsum",
#"diff",
#"dot", # Exists in numpy. Implements matlab .*
#"exist",
"false",
#"fclose",
#"find",
#"findone", # same as find, but returns ONE result
#"floor",
#"fopen",
"getfield",
"inf", "inf0",
#"isempty",
#"isequal",
"isinf",
"isnan",
#"length",
#"load",
#"lower",
#"max",
#"min",
#"mod",
#"nnz",
#"numel",
#"ones",
#"rand",
#"range_", # synthetic opcode
"ravel", # synthetic opcode
#"rem",
#"save",
"setfield",
#"sign",
#"size",
#"sort",
#"strcmp",
#"strcmpi",
"sub", # synthetic opcode for subtract
#"sum",
"transpose",
#"true",
#"zeros",
]
for name in builtins_list:
globals()[name] = type(name, (builtins,), {})
#class cellarrayref(node,recordtype("cellarrayref","ident args")):
class cellarrayref(funcall):
pass
class cellarray(expr):
pass
class matrix(builtins):
"""
Anything enclosed in square brackets counts as matrix
>>> print matrix([1,2,3])
[1,2,3]
>>> print matrix()
[]
"""
# def __init__(self,args=expr_list()):
# expr.__init__(self,op="[]",args=args)
# def __str__(self):
# return "[%s]" % ",".join([str(t) for t in self.args])
@extend(node)
def is_const(self):
return False
@extend(number)
@extend(string)
def is_const(self):
return True
@extend(expr_list)
def is_const(self):
return all(t.is_const() for t in self)
@extend(matrix)
def is_const(self):
return not self.args or self.args[0].is_const()
# vim: ts=8:sw=4:et