from pprint import pprint
from copy import copy
from ply import lex, yacc
+import astor
##### Lexer ######
#import lex
'MINUS',
'MULT',
'DIV',
+ 'APPEND',
'RETURN',
'WS',
'NEWLINE',
t_DIV = r'/'
t_COMMA = r','
t_SEMICOLON = r';'
+t_APPEND = r'\|\|'
# Ply nicely documented how to do this.
binary_ops = {
- "+": ast.Add,
- "-": ast.Sub,
+ "+": ast.Add(),
+ "-": ast.Sub(),
"*": ast.Mult(),
- "/": ast.Div,
+ "/": ast.Div(),
"<": make_lt_compare,
">": make_gt_compare,
"==": make_eq_compare,
("left", "MULT", "DIV"),
)
+def check_concat(node): # checks if the comparison is already a concat
+ print (node)
+ if not isinstance(node, ast.Call):
+ return [node]
+ if node[0].id != 'concat':
+ return node
+ return node[1]
+
def p_comparison(p):
"""comparison : comparison PLUS comparison
| comparison MINUS comparison
| comparison GT comparison
| PLUS comparison
| MINUS comparison
+ | comparison APPEND comparison
| power"""
if len(p) == 4:
- p[0] = ast.BinOp(p[1], binary_ops[p[2]], p[3])
+ print (list(p))
+ if p[2] == '||':
+ l = check_concat(p[1]) + check_concat(p[3])
+ p[0] = ast.Call(ast.Name("concat"), l, [])
+ else:
+ p[0] = ast.BinOp(p[1], binary_ops[p[2]], p[3])
elif len(p) == 3:
p[0] = unary_ops[p[1]](p[2])
else:
#raise AssertionError("not implemented %s" % p[2][0])
subs = p[2][1]
if len(subs) == 1:
- p[0] = ast.Subscript(p[1], 'OP_APPLY', subs[0])
+ idx = subs[0]
else:
- p[0] = ast.Slice(p[1], 'OP_APPLY', subs[0], subs[1])
+ idx = ast.Slice(subs[0], subs[1], None)
+ p[0] = ast.Subscript(p[1], idx)
def p_atom_name(p):
"""atom : NAME"""
bpermd = r"""
perm <- [0] * 8
#index <- (RS)[8*i:8*i+7]
-RA <- [0]*56 # || perm[0:7]
+RA <- [0]*56 || perm[0:7]
print (RA)
"""
tree = ast.fix_missing_locations(tree)
print ( ast.dump(tree) )
-import astor
print ("astor dump")
print (astor.dump_tree(tree))
print ("to source")