From 99f62fb8b3080156b27334085bbdd4cdd79f17bc Mon Sep 17 00:00:00 2001 From: Nicolas Dandrimont Date: Thu, 4 Apr 2013 09:29:21 +0200 Subject: [PATCH 1/4] Add some tests for defn --- tests/native_tests/language.hy | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index 3e92e1e..0bb9f77 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -229,3 +229,19 @@ [2 4])) (assert (= (list-comp (, x y) (x (range 2) y (range 2))) [(, 0 0) (, 0 1) (, 1 0) (, 1 1)]))) + +(defn test-defn-order [] + "NATIVE: test defn evaluation order" + (setv acc []) + (defn my-fun [] + (.append acc "Foo") + (.append acc "Bar") + (.append acc "Baz")) + (my-fun) + (assert (= acc ["Foo" "Bar" "Baz"]))) + +(defn test-defn-return [] + "NATIVE: test defn return" + (defn my-fun [x] + (+ x 1)) + (assert (= 43 (my-fun 42)))) From 940afcafa92fa45c718499c5412be28a2cacef0d Mon Sep 17 00:00:00 2001 From: Nicolas Dandrimont Date: Thu, 4 Apr 2013 08:51:59 +0200 Subject: [PATCH 2/4] Allow (do) inside (defn) Closes #59 --- hy/compiler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hy/compiler.py b/hy/compiler.py index 5514263..7294008 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -67,7 +67,7 @@ class HyASTCompiler(object): if self.returnable and len(tree) > 0: el = tree[0] - if not isinstance(el, ast.stmt): + if not isinstance(el, (ast.stmt, list)): el = tree.pop(0) ret.append(ast.Return(value=el, lineno=el.lineno, From 06511fe303aa2d3aff82d5b689fe21b592d64d80 Mon Sep 17 00:00:00 2001 From: Nicolas Dandrimont Date: Thu, 4 Apr 2013 11:06:03 +0200 Subject: [PATCH 3/4] Add do-in-defn tests --- tests/native_tests/language.hy | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index 0bb9f77..0538f44 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -245,3 +245,22 @@ (defn my-fun [x] (+ x 1)) (assert (= 43 (my-fun 42)))) + +(defn test-defn-do [] + "NATIVE: test defn evaluation order with do" + (setv acc []) + (defn my-fun [] + (do + (.append acc "Foo") + (.append acc "Bar") + (.append acc "Baz"))) + (my-fun) + (assert (= acc ["Foo" "Bar" "Baz"]))) + +(defn test-defn-do-return [] + "NATIVE: test defn return with do" + (defn my-fun [x] + (do + (+ x 42) ; noop + (+ x 1))) + (assert (= 43 (my-fun 42)))) From ba021ed7bf8e40584c45721b880567c6afd4c5e3 Mon Sep 17 00:00:00 2001 From: Nicolas Dandrimont Date: Thu, 4 Apr 2013 11:20:10 +0200 Subject: [PATCH 4/4] Count the number of expressions executed in do --- tests/native_tests/language.hy | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index 0538f44..79fa344 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -85,6 +85,17 @@ (assert (= 1 1)) (assert (= 1 1))))) +(defn test-branching-expr-count-with-do [] + "NATIVE: make sure we execute the right number of expressions in the branch" + (setv counter 0) + (if false + (assert (= 2 1)) + (do + (setv counter (+ counter 1)) + (setv counter (+ counter 1)) + (setv counter (+ counter 1)))) + (assert (= counter 3))) + (defn test-cond [] "NATIVE: test if cond sorta works."