Fix early returns. Close #83

This commit is contained in:
Paul R. Tagliamonte 2013-04-07 19:29:45 -04:00
parent 5ff40c5e30
commit 5336b2f71a
2 changed files with 29 additions and 3 deletions

View File

@ -762,12 +762,27 @@ class HyASTCompiler(object):
expression.pop(0) # fn expression.pop(0) # fn
ret_status = self.returnable ret_status = self.returnable
self.returnable = True
self.anon_fn_count += 1 self.anon_fn_count += 1
name = "_hy_anon_fn_%d" % (self.anon_fn_count) name = "_hy_anon_fn_%d" % (self.anon_fn_count)
sig = expression.pop(0) sig = expression.pop(0)
body = []
if expression != []:
self.returnable = True
tailop = self.compile(expression.pop(-1))
self.returnable = False
for el in expression:
body.append(self.compile(el))
body.append(tailop)
if body == []:
body = [ast.Pass(lineno=expression.start_line,
col_offset=expression.start_column)]
self.returnable = True
body = self._code_branch(body)
ret = ast.FunctionDef( ret = ast.FunctionDef(
name=name, name=name,
lineno=expression.start_line, lineno=expression.start_line,
@ -785,8 +800,7 @@ class HyASTCompiler(object):
kwonlyargs=[], kwonlyargs=[],
kw_defaults=[], kw_defaults=[],
defaults=[]), defaults=[]),
body=self._code_branch([ body=body,
self.compile(x) for x in expression]),
decorator_list=[]) decorator_list=[])
self.returnable = ret_status self.returnable = ret_status

View File

@ -427,6 +427,7 @@
(assert (= and123 3)) (assert (= and123 3))
(assert (= and-false False)))) (assert (= and-false False))))
(defn test-or [] (defn test-or []
"NATIVE: test the or function" "NATIVE: test the or function"
(let [[or-all-true (or 1 2 3 True "string")] (let [[or-all-true (or 1 2 3 True "string")]
@ -436,6 +437,17 @@
(assert (= or-some-true "hello")) (assert (= or-some-true "hello"))
(assert (= or-none-true False)))) (assert (= or-none-true False))))
(defn test-if-return-branching []
"NATIVE: test the if return branching"
; thanks, algernon
(assert (= 1 (let [[x 1]
[y 2]]
(if true
2)
1))))
; FEATURE: native hy-eval ; FEATURE: native hy-eval
; ;
; - related to bug #64 ; - related to bug #64