From 5a74fff7e649d75c10e8535f59e0c5ee96d26116 Mon Sep 17 00:00:00 2001 From: Nicolas Dandrimont Date: Sat, 11 May 2013 20:32:35 +0200 Subject: [PATCH] Fix up the native-macros to use compiler imports --- hy/compiler.py | 49 +++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/hy/compiler.py b/hy/compiler.py index 78c5d94..ea3c5de 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -343,15 +343,24 @@ class HyASTCompiler(object): """Convert the Result's imports to statements""" ret = Result() for module, names in self.imports.items(): - ret += self.compile([ - HyExpression([ - HySymbol("import"), - HyList([ + if None in names: + ret += self.compile([ + HyExpression([ + HySymbol("import"), HySymbol(module), - HyList([HySymbol(name) for name in sorted(names)]) - ]) - ]).replace(expr) - ]) + ]).replace(expr) + ]) + names = sorted(name for name in names if name) + if names: + ret += self.compile([ + HyExpression([ + HySymbol("import"), + HyList([ + HySymbol(module), + HyList([HySymbol(name) for name in names]) + ]) + ]).replace(expr) + ]) self.imports = defaultdict(set) return ret.stmts @@ -1608,19 +1617,19 @@ class HyASTCompiler(object): "for macro name" % type(name).__name__)) name = HyString(name).replace(name) new_expression = HyExpression([ - HySymbol("do"), - HyExpression([ - HySymbol("import"), - HySymbol("hy.macros"), - ]), - HyExpression([ - HySymbol("with_decorator"), - HyExpression([HySymbol("hy.macros.macro"), name]), - HyExpression([HySymbol("fn")] + expression), - ]), + HySymbol("with_decorator"), + HyExpression([HySymbol("hy.macros.macro"), name]), + HyExpression([HySymbol("fn")] + expression), ]).replace(expression) - hy.importer.hy_eval(new_expression, {}) - return self.compile(new_expression) + + # Compile-time hack: we want to get our new macro now + hy.importer.hy_eval(new_expression, {'hy': hy}) + + # We really want to have a `hy` import to get hy.macro in + ret = self.compile(new_expression) + ret.add_imports('hy', [None]) + + return ret @builds(HyInteger) def compile_integer(self, number):