diff --git a/hy/compiler.py b/hy/compiler.py index 0b79f5b..861d745 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -94,10 +94,15 @@ class HyASTCompiler(object): @builds("if") def compile_if_expression(self, expr): expr.pop(0) + test = self.compile(expr.pop(0)) + body = self._code_branch(self.compile(expr.pop(0))) + orel = [] + if len(expr) > 0: + orel = self._code_branch(self.compile(expr.pop(0))) - return ast.If(test=self.compile(expr.pop(0)), - body=self._code_branch(self.compile(expr.pop(0))), - orelse=self._code_branch(self.compile(expr.pop(0))), + return ast.If(test=test, + body=body, + orelse=orel, lineno=expr.start_line, col_offset=expr.start_column) diff --git a/hy/core/bootstrap.py b/hy/core/bootstrap.py index 9ca002d..2e7dd3d 100644 --- a/hy/core/bootstrap.py +++ b/hy/core/bootstrap.py @@ -28,3 +28,20 @@ from hy.models.symbol import HySymbol def defn_macro(tree): return HyExpression([HySymbol("def"), tree[1], HyExpression([HySymbol("fn")] + tree[2:])]) + + +@macro("cond") +def cond_macro(tree): + tree.pop(0) # cond flag + it = iter(tree) + conds = iter(zip(it, it)) + test, branch = next(conds) + + root = HyExpression([HySymbol("if"), test, branch]) + ret = root + for (test, branch) in conds: + n = HyExpression([HySymbol("if"), test, branch]) + ret.append(n) + ret = n + + return root