Split eval in two steps to allow eval'ing statements

We compile the evaluated operand, separating the body (statements)
from the expression. We then eval() those separately, and return
the evaluated expression.
This commit is contained in:
Nicolas Dandrimont 2013-05-03 17:49:45 +02:00
parent 4387b947b3
commit 387bc0d9f2

View File

@ -91,8 +91,22 @@ def hy_eval(hytree, namespace):
foo.start_column = 0
foo.end_column = 0
hytree.replace(foo)
_ast = hy_compile(hytree, root=ast.Expression)
return eval(ast_compile(_ast, "<eval>", "eval"), namespace)
_ast, expr = hy_compile(hytree, get_expr=True)
# Spoof the positions in the generated ast...
for node in ast.walk(_ast):
node.lineno = 1
node.col_offset = 1
for node in ast.walk(expr):
node.lineno = 1
node.col_offset = 1
# Two-step eval: eval() the body of the exec call
eval(ast_compile(_ast, "<eval_body>", "exec"), namespace)
# Then eval the expression context and return that
return eval(ast_compile(expr, "<eval>", "eval"), namespace)
def write_hy_as_pyc(fname):