From b380b330cdf20a2c75700fb8c0c8ba3c87040f86 Mon Sep 17 00:00:00 2001 From: Nicolas Dandrimont Date: Sat, 11 May 2013 09:10:34 +0200 Subject: [PATCH] Add a defmacro builtin --- hy/compiler.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/hy/compiler.py b/hy/compiler.py index c6264ac..7602b30 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -1589,6 +1589,35 @@ class HyASTCompiler(object): bases=bases_expr, body=body.stmts) + @builds("defmacro") + @checkargs(min=1) + def compile_macro(self, expression): + expression.pop(0) + name = expression.pop(0) + if not isinstance(name, HySymbol): + raise HyTypeError(name, + ("received a `%s' instead of a symbol " + "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), + ]), + ]).replace(expression) + + return self.compile(new_expression) + @builds(HyInteger) def compile_integer(self, number): return ast.Num(n=int(number),