diff --git a/hy/compiler/ast27.py b/hy/compiler/ast27.py index 702d068..63da4d0 100644 --- a/hy/compiler/ast27.py +++ b/hy/compiler/ast27.py @@ -2,6 +2,7 @@ import ast from hy.lang.expression import HYExpression +from hy.lang.number import HYNumber from hy.lang.string import HYString from hy.lang.builtins import builtins @@ -23,8 +24,25 @@ def _ast_print(node, children): ) +def _ast_binop(node, children): + inv = node.get_invocation() + ops = { + "+": ast.Add + } + op = ops[inv['function']] + + left = children.pop(0) + calc = None + + for child in children: + calc = ast.BinOp(left=left, op=op(), right=child) + left = calc + + return calc + special_cases = { - "print": _ast_print + "print": _ast_print, + "+": _ast_binop } @@ -32,14 +50,15 @@ class AST27Converter(object): def __init__(self): self.table = { HYString: self.render_string, - HYExpression: self.render_expression + HYExpression: self.render_expression, + HYNumber: self.render_number } def render_string(self, node): return ast.Str(s=node) def render_number(self, node): - pass + return ast.Num(n=node) def render_expression(self, node): c = [] @@ -68,3 +87,4 @@ def forge_ast(name, forest): statements.append(conv.render(tree)) print [ast.dump(x) for x in statements] + return ast.fix_missing_locations(ast.Module(body=statements))