/
translate.py
340 lines (242 loc) · 8.7 KB
/
translate.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
# http://cran.r-project.org/doc/manuals/r-release/R-lang.html
import ast
from StringIO import StringIO
import subprocess
from more_itertools import chunked
from reader import reader
from unparse import Unparser
class UnknownError(Exception): pass
class CueGeneric(ast.AST):
"""
Represents a node in a tree.
A tree node has a single parent, a list of children,
and a reference to the data it contains.
"""
def __init__(self, level, type_, recs):
self.level = level
self.type = type_
self.recs = recs
self.children = []
self.content = None
for rec in recs:
if rec.name == 'content':
self.content = rec.value
def __repr__(self):
return '{}({})'.format(self.__class__.__name__, self.type)
class CueNull(ast.AST): pass
class CueBody(ast.AST):
_fields = ['exprs']
class CueExpression(ast.AST):
_fields = ['children']
class CuePairlist(ast.AST):
_fields = ['argslist']
def __init__(self, recs):
self.argslist = []
# TODO make these separate nodes
rec_values = (rec.value for rec in recs)
for name, value, type_ in chunked(rec_values, 3):
self.argslist.append((name, value, type_))
class CueLanguage(ast.AST):
_fields = ['children']
class CueSymbol(ast.AST):
_fields = ['content']
class CueFunction(ast.AST):
_fields = ['args', 'body']
def __init__(self, args, body, dontknow):
self.args = args
self.body = [body]
class CueAssign(ast.AST):
_fields = ['left', 'right']
class CueAssignOp(ast.operator): pass
def add_simple_ops():
# Simple CueOp nodes
_g = globals()
for name in ['Function', 'If', 'Return', 'Block', 'Paren']:
name = 'Cue' + name + 'Op'
_g[name] = type(name, (ast.AST,), {})
add_simple_ops()
class CueError(Exception): pass
def run_cue(text):
p = subprocess.Popen(['Rscript', 'cue.r'], stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate(text)
if err:
raise CueError(err)
return out
class Transformer(ast.NodeTransformer):
def visit_CueGeneric(self, node):
if node.type == 'symbol':
newnode = CueSymbol(node.content)
elif node.type == 'language':
newnode = CueLanguage(node.children)
elif node.type == 'expression':
newnode = CueExpression(node.children)
elif node.type == 'NULL':
newnode = CueNull()
elif node.type == 'double':
# TODO could be float()?
newnode = ast.Num(int(node.content))
elif node.type == 'pairlist':
newnode = CuePairlist(node.recs)
elif node.type == 'character':
newnode = ast.Str(node.content)
return self.visit(newnode)
def visit_CueExpression(self, node):
body = [self.visit(child) for child in node.children]
return ast.Module(body)
def visit_CueSymbol(self, node):
op_str = node.content
op_map = {
# Binary
'<-': CueAssignOp,
'=': CueAssignOp,
'*': ast.Mult,
# TODO this probably isn't true, unless __future__.division is always
# imported. R also has '%/%' for integer division
'/': ast.Div,
'+': ast.Add,
'-': ast.Sub,
'%%': ast.Mod,
'^': ast.Pow,
'**': ast.Pow,
# Comparison
'<': ast.Lt,
'<=': ast.LtE,
'>': ast.Gt,
'>=': ast.GtE,
'==': ast.Eq,
'!=': ast.NotEq,
'%in%': ast.In,
# R doesn't have a simple equivalent to 'is'
# http://stackoverflow.com/questions/10912729/r-object-identity
# Unary
'!': ast.Not,
# Boolean
# TODO there's a difference between vectorized and not
# TODO not handling the vectorized ops '&', '|', etc.
'&&': ast.And,
'||': ast.Or,
# Indexing
'[[': ast.Subscript,
# Other
'function': CueFunctionOp,
'if': CueIfOp,
'{': CueBlockOp,
'(': CueParenOp,
'return': CueReturnOp,
}
try:
return op_map[op_str]()
except KeyError:
return ast.Name(op_str, ast.Load())
def visit_CueLanguage(self, node):
children = [self.visit(child) for child in node.children]
assert len(children) > 0
op = children[0]
rest = children[1:]
if isinstance(op, ast.operator):
assert len(rest) == 2
left, right = rest
# Assignment is special in R because it could either be
# simple assignment, or a function definition, or ...
if isinstance(op, CueAssignOp):
newnode = CueAssign(left, right)
else:
newnode = ast.BinOp(left, op, right)
elif isinstance(op, ast.cmpop):
assert len(rest) == 2
left, right = rest
newnode = ast.Compare(left, [op], [right])
elif isinstance(op, ast.unaryop):
assert len(rest) == 1
operand = rest[0]
newnode = ast.UnaryOp(op, operand)
elif isinstance(op, ast.boolop):
assert len(rest) == 2
left, right = rest
newnode = ast.BoolOp(op, [left, right])
elif isinstance(op, CueFunctionOp):
assert len(rest) == 3
args, body, dontknow = rest
newnode = CueFunction(args, body, dontknow)
elif isinstance(op, CueIfOp):
assert len(rest) == 2
cond, body = rest
if isinstance(body, ast.expr):
body = ast.Expr(body)
elif isinstance(body, CueBody):
body = [ast.Expr(e) for e in body.exprs]
newnode = ast.If(rest[0], body, None)
elif isinstance(op, CueBlockOp):
newnode = CueBody(rest)
elif isinstance(op, CueParenOp):
print rest
newnode = rest[0]
elif isinstance(op, CueReturnOp):
if not rest:
newnode = ast.Return(value=None)
else:
assert len(rest) == 1
value = rest[0]
newnode = ast.Return(value=value)
elif isinstance(op, ast.Subscript):
assert len(rest) == 2
op.value, op.slice = rest
newnode = op
elif isinstance(op, ast.Name) or isinstance(op, ast.Call):
# TODO this allows invalid function names,
# such as 'read.csv'
# TODO wouldn't support foo(1)(2)
args = []
for r in rest:
assert isinstance(r, ast.expr)
args.append(r)
newnode = ast.Call(op, args, [], None, None)
else:
raise UnknownError(op)
return self.visit(newnode)
def visit_CueAssign(self, node):
newleft = ast.Name(node.left.id, ast.Store())
if isinstance(node.right, ast.FunctionDef):
node.right.name = node.left.id
return node.right
return ast.Assign([newleft], node.right)
def visit_CueFunction(self, node):
arguments = []
if isinstance(node.args, CuePairlist):
argslist = node.args.argslist
args = [ast.Name(name, ast.Param()) for name, value, type_ in argslist]
arguments = ast.arguments(args, None, None, [])
body = []
for n in node.body:
if isinstance(n, ast.stmt):
body.append(n)
elif isinstance(n, ast.expr):
n = ast.Expr(n)
body.append(n)
elif isinstance(n, CueBody):
for expr in n.exprs:
body.append(ast.Expr(expr))
return ast.FunctionDef(None, arguments, body, [])
def visit_CueAdd(self, node):
return ast.BinOp(node.left, ast.Add(), node.right)
def visit_CueMult(self, node):
return ast.BinOp(node.left, ast.Mult(), node.right)
def translate_cue_code(cue_code):
nodes = reader(cue_code.split('\n'), CueGeneric)
root = nodes.next()
tree = Transformer().visit(root)
print ast.dump(tree)
out = StringIO()
Unparser(tree, out)
return out.getvalue()
def translate(raw):
cue_out = run_cue(raw)
print cue_out
return translate_cue_code(cue_out)
if __name__ == '__main__':
#print translate('1 %in% foo')
#print translate('funcname <- function(x) return()')
#print translate('foo <- bar')
#print translate('foo(1)(bar())')
print translate('foo[[1]]')