Allow specification of global table and module name for (eval ...)

This commit is contained in:
Gregor Best 2015-05-20 19:39:14 +02:00
parent 685688f04c
commit 4adddbbf25
2 changed files with 17 additions and 7 deletions

View File

@ -639,7 +639,10 @@ doto
eval eval
---- ----
``eval`` evaluates a quoted expression and returns the value. ``eval`` evaluates a quoted expression and returns the value. The optional
second and third arguments specify the dictionary of globals to use and the
module name. The globals dictionary defaults to ``(local)`` and the module name
defaults to the name of the current module.
.. code-block:: clj .. code-block:: clj

View File

@ -696,15 +696,22 @@ class HyASTCompiler(object):
"`%s' can't be used at the top-level" % expr[0]) "`%s' can't be used at the top-level" % expr[0])
@builds("eval") @builds("eval")
@checkargs(exact=1) @checkargs(min=1, max=3)
def compile_eval(self, expr): def compile_eval(self, expr):
expr.pop(0) expr.pop(0)
ret = self.compile(HyExpression( elist = [HySymbol("hy_eval")] + [expr[0]]
[HySymbol("hy_eval")] + expr + if len(expr) >= 2:
[HyExpression([HySymbol("locals")])] + elist.append(expr[1])
[HyString(self.module_name)]).replace(expr) else:
) elist.append(HyExpression([HySymbol("locals")]))
if len(expr) == 3:
elist.append(expr[2])
else:
elist.append(HyString(self.module_name))
ret = self.compile(HyExpression(elist).replace(expr))
ret.add_imports("hy.importer", ["hy_eval"]) ret.add_imports("hy.importer", ["hy_eval"])