Skip to content

Commit 46d8277

Browse files
fix compiler
1 parent 12cacc6 commit 46d8277

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

pixie/vm/compiler.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -539,8 +539,8 @@ def compile_fn_body(name, args, body, ctx):
539539
compile_form(body, new_ctx)
540540
else:
541541
while body is not nil:
542-
if rt.next(body) is nil:
543-
new_ctx.enable_tail_call()
542+
#if rt.next(body) is nil:
543+
# new_ctx.enable_tail_call()
544544
compile_form(rt.first(body), new_ctx)
545545
body = rt.next(body)
546546
if body is not nil:
@@ -635,7 +635,7 @@ def compile_quote(form, ctx):
635635

636636
def compile_recur(form, ctx):
637637
form = form.next()
638-
#affirm(ctx.can_tail_call, u"Can't recur in non-tail position")
638+
affirm(ctx.can_tail_call, u"Can't recur in non-tail position")
639639
ctc = ctx.can_tail_call
640640
ctx.disable_tail_call()
641641
args = 0
@@ -697,7 +697,7 @@ def compile_loop(form, ctx):
697697
bindings = rt.first(form)
698698
affirm(isinstance(bindings, PersistentVector), u"Loop bindings must be a vector")
699699
body = rt.next(form)
700-
700+
ctx.enable_tail_call()
701701
ctc = ctx.can_tail_call
702702
ctx.disable_tail_call()
703703

tests/pixie/tests/test-compiler.pxi

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,39 @@
3535

3636
(t/deftest test-deftype-mutables
3737
(mutate! (->Foo 0)))
38+
39+
(t/deftest test-recur-must-tail
40+
(t/assert-throws?
41+
(eval
42+
'(loop [n 0]
43+
(inc (recur n)))))
44+
45+
(t/assert-throws?
46+
(eval
47+
'(fn [n]
48+
(if (zero? n)
49+
1
50+
(* n (recur (dec n)))))))
51+
52+
(t/assert-throws?
53+
(eval
54+
'(fn [n]
55+
(if (zero? n)
56+
(* n (recur (dec n)))
57+
1))))
58+
59+
(t/assert=
60+
(eval
61+
'(loop [n 0]
62+
(if (= 10 n)
63+
n
64+
(recur (inc n)))))
65+
10)
66+
67+
(t/assert=
68+
(eval
69+
'(loop [n 0]
70+
(if (not= 10 n)
71+
(recur (inc n))
72+
n)))
73+
10))

0 commit comments

Comments
 (0)