From 04c7d8f100fb840a6f62a97e8ca48ab829cadf19 Mon Sep 17 00:00:00 2001 From: Ryan Gonzalez Date: Mon, 7 Aug 2017 18:04:47 -0500 Subject: [PATCH] Reimplement variadic if without recursion (closes #842) --- hy/compiler.py | 2 +- hy/core/bootstrap.hy | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/hy/compiler.py b/hy/compiler.py index 4e30954bd..724fb157f 100755 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -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 diff --git a/hy/core/bootstrap.hy b/hy/core/bootstrap.hy index 4ecb243e3..8ed64aabb 100644 --- a/hy/core/bootstrap.hy +++ b/hy/core/bootstrap.hy @@ -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"