Skip to content

Commit

Permalink
Reimplement variadic if without recursion (closes hylang#842)
Browse files Browse the repository at this point in the history
  • Loading branch information
refi64 committed Aug 7, 2017
1 parent 7e5d483 commit 04c7d8f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion hy/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,7 @@ def compile_if(self, expression):
if expression:
orel_expr = expression.pop(0)
if isinstance(orel_expr, HyExpression) and isinstance(orel_expr[0],
HySymbol) and orel_expr[0] == 'if*':
HySymbol) and orel_expr[0] in ('if*', 'if'):
# Nested ifs: don't waste temporaries
root = self.temp_if is None
nested = True
Expand Down
13 changes: 8 additions & 5 deletions hy/core/bootstrap.hy
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
"if with elif"
(setv n (len args))
(if* n
(if* (= n 1)
(get args 0)
`(if* ~(get args 0)
~(get args 1)
(if ~@(cut args 2))))))
(do
(setv args (list args))
(setv else-branch (if* (% n 2) (.pop args) `None))
(.reverse args)
(reduce (fn [current-else [then-branch cond]]
`(if* ~cond ~then-branch ~current-else))
(zip (cut args None None 2) (cut args 1 None 2))
else-branch))))

(defmacro macro-error [location reason]
"error out properly within a macro"
Expand Down

0 comments on commit 04c7d8f

Please sign in to comment.