Commits

Anonymous committed 939f3ad

implement recur for fns

Comments (0)

Files changed (2)

           (parameterize ((local-scope (append locals (local-scope))))
             (map clojure-compile body)))))
 
+;; TODO: only define ~recur when it appears in the expression?
 (define (compile-fn* form)
   (match form
     (((? clojure-symbol? name) rest ...)
-     `(~rec
-       ,(clojure-symbol->var-name name)
-       ,(compile-fn* rest)))
+     `(~rec ,(clojure-symbol->var-name name) ,(compile-fn* rest)))
     (((? clojure-vector? args) body ...)
-     `(~lambda . ,(compile-fn*-tail args body)))
+     `(~rec ~recur (~lambda . ,(compile-fn*-tail args body))))
     ((((? clojure-vector? args) body ...) ...)
-     `(~case-lambda
-       . ,(map compile-fn*-tail args body)))
+     `(~rec ~recur (~case-lambda . ,(map compile-fn*-tail args body))))
     (else (error 'clojure-compile
                  "invalid fn* syntax"
                  (cons 'fn* form)))))
            "(list (bar) (bar 2) (bar 2 3))"))
   (test-group "named"
     (test* 4 "((fn* self ([x] (self x x)) ([x y] (+ x y))) 2)"))
+  (test-group "recur"
+    (test* "ok" "((fn* [x] (if (scheme/= x 0) \"ok\" (recur (- x 1)))) 10)"))
   (test* '(1 2)
          "(def x 1)"
          "((fn* [x] (list user/x x)) 2)"))