diff --git a/docs/language/api.rst b/docs/language/api.rst index bbc2868..5562c1d 100644 --- a/docs/language/api.rst +++ b/docs/language/api.rst @@ -639,7 +639,10 @@ doto 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 diff --git a/hy/compiler.py b/hy/compiler.py index 31fba2b..e3429ff 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -696,15 +696,22 @@ class HyASTCompiler(object): "`%s' can't be used at the top-level" % expr[0]) @builds("eval") - @checkargs(exact=1) + @checkargs(min=1, max=3) def compile_eval(self, expr): expr.pop(0) - ret = self.compile(HyExpression( - [HySymbol("hy_eval")] + expr + - [HyExpression([HySymbol("locals")])] + - [HyString(self.module_name)]).replace(expr) - ) + elist = [HySymbol("hy_eval")] + [expr[0]] + if len(expr) >= 2: + elist.append(expr[1]) + 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"])