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:
parent
4387b947b3
commit
387bc0d9f2
@ -91,8 +91,22 @@ def hy_eval(hytree, namespace):
|
|||||||
foo.start_column = 0
|
foo.start_column = 0
|
||||||
foo.end_column = 0
|
foo.end_column = 0
|
||||||
hytree.replace(foo)
|
hytree.replace(foo)
|
||||||
_ast = hy_compile(hytree, root=ast.Expression)
|
_ast, expr = hy_compile(hytree, get_expr=True)
|
||||||
return eval(ast_compile(_ast, "<eval>", "eval"), namespace)
|
|
||||||
|
# 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):
|
def write_hy_as_pyc(fname):
|
||||||
|
Loading…
Reference in New Issue
Block a user