Skip to content

Commit

Permalink
[#113] fix function return value
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Oct 17, 2019
1 parent da91beb commit 6780bf7
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@borkdude/sci",
"version": "0.0.10",
"version": "0.0.11-alpha.1",
"description": "Small Clojure Interpreter.",
"homepage": "https://github.com/borkdude/sci",
"author": "Michiel Borkent (https://github.com/borkdude)",
Expand Down
2 changes: 1 addition & 1 deletion resources/SCI_VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.10
0.0.11-alpha.1
4 changes: 3 additions & 1 deletion src/sci/impl/fns.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@
(apply f args)
(throw (new #?(:clj Exception
:cljs js/Error) (str "Cannot call " fn-name " with " arg-count " arguments.")))))))
#(assoc % :sci/macro macro?))]
#(assoc %
:sci.impl/fn true
:sci/macro macro?))]
(reset! self-ref f)
f))

Expand Down
12 changes: 9 additions & 3 deletions src/sci/impl/interpreter.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@
(find bindings sym)
(find namespaces/clojure-core sym)
(when-let [ns (namespace sym)]
;; (prn "NS>" ns)
(when (or (= "clojure.core" ns)
(= "cljs.core" ns))
(find namespaces/clojure-core (symbol (name sym)))))
Expand Down Expand Up @@ -366,10 +365,17 @@
:deny (not-empty (reduce into #{} [(:deny preset) deny]))
:realize-max (or realize-max (:realize-max preset))
:start-expression s}
edn-vals (p/parse-string-all s)]
(eval-do ctx (cons 'do edn-vals)))))
edn-vals (p/parse-string-all s)
ret (eval-do ctx (cons 'do edn-vals))]
(if (and (fn? ret)
(some-> (meta ret) :sci.impl/fn))
(fn [& args]
;; we have to wrap because of recur and make the function behave well in JS
(apply-fn ctx ret args))
ret))))

;;;; Scratch

(comment
(eval-string "((fn f [x] (if (< x 3) (recur (inc x)) x)) 0)")
)
6 changes: 5 additions & 1 deletion test/sci/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,11 @@

(deftest recur-test
(is (= 10000 (tu/eval* "(defn hello [x] (if (< x 10000) (recur (inc x)) x)) (hello 0)"
{}))))
{})))
(testing "function with recur may be returned"
(when-not tu/native?
(let [f (eval* "(fn f [x] (if (< x 3) (recur (inc x)) x))")]
(f 0)))))

(deftest loop-test
(is (= 2 (tu/eval* "(loop [[x y] [1 2]] (if (= x 3) y (recur [(inc x) y])))"
Expand Down

0 comments on commit 6780bf7

Please sign in to comment.