Provide both eval-and-compile and eval-when-compile

This commit is contained in:
Konrad Hinsen 2013-06-05 11:41:58 +02:00
parent 80105d9731
commit 6eacf63b53
2 changed files with 19 additions and 2 deletions

View File

@ -1664,8 +1664,8 @@ class HyASTCompiler(object):
return ret return ret
@builds("eval_when_compile") @builds("eval_and_compile")
def compile_eval_when_compile(self, expression): def compile_eval_and_compile(self, expression):
expression[0] = HySymbol("progn") expression[0] = HySymbol("progn")
hy.importer.hy_eval(expression, hy.importer.hy_eval(expression,
compile_time_ns(self.module_name), compile_time_ns(self.module_name),
@ -1673,6 +1673,14 @@ class HyASTCompiler(object):
expression.pop(0) expression.pop(0)
return self._compile_branch(expression) return self._compile_branch(expression)
@builds("eval_when_compile")
def compile_eval_when_compile(self, expression):
expression[0] = HySymbol("progn")
hy.importer.hy_eval(expression,
compile_time_ns(self.module_name),
self.module_name)
return Result()
@builds(HyInteger) @builds(HyInteger)
def compile_integer(self, number): def compile_integer(self, number):
return ast.Num(n=int(number), return ast.Num(n=int(number),

View File

@ -45,3 +45,12 @@
(defn test-fn-calling-macro [] (defn test-fn-calling-macro []
"NATIVE: test macro calling a plain function" "NATIVE: test macro calling a plain function"
(assert (= 3 (bar 1 2)))) (assert (= 3 (bar 1 2))))
; Macro that checks a variable defined at compile or load time
(setv phase "load")
(eval-when-compile
(setv phase "compile"))
(defmacro phase-when-compiling [] phase)
(assert (= phase "load"))
(assert (= (phase-when-compiling) "compile"))