futzing with condlike things.
This commit is contained in:
parent
ce3e7a2d37
commit
ab2cf5beda
@ -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)
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user