Chez Phase 1 (increment 3g): letfn + declare/def-no-init
Closes the last two non-host-interop prelude emit gaps. letfn now analyzes to a :let node flagged :letrec — the binding fns are bound into the env together before any spec is analyzed, so siblings and self resolve. The Chez back end lowers it to letrec*; the Janet back end punts it at emit (its sequential let* can't express the mutual recursion — same interpreter fallback as before, just decided at emit-ir instead of analyze). (def x) with no init (declare) analyzes to a :def with :no-init instead of punting. Chez reserves the var cell via declare-var! (which doesn't clobber an existing root — (do (def x 7) (def x) x) => 7); the Janet back end still punts to the interpreter, which interns a genuinely-unbound var. fallback-zero-test now checks emit-ir too, not just analyze-form, so the real compile-vs-interpret decision is what it asserts (letfn/def-no-init analyze but the Janet back end punts them). letfn stays in must-punt with an updated note. Prelude emit reach 342 -> 348/355 (40-lazy now 13/13); Chez subset 664 -> 672, 0 divergences; emit-test 110 -> 117. Full gate green.
This commit is contained in:
parent
930800f9a6
commit
0f7d2753a8
8 changed files with 119 additions and 31 deletions
|
|
@ -259,6 +259,28 @@
|
|||
(ok (string "quote: " src) (and (= code 0) (= out want))
|
||||
(string "chez=" out " janet=" want " | " err))))
|
||||
|
||||
# 3k) letfn + declare/def-no-init (inc 3g). letfn lowers to a Scheme `letrec*`
|
||||
# (mutual recursion between the named local fns — a plain let* can't forward-
|
||||
# ref a sibling). declare/(def x) with no init pre-creates the var cell so a
|
||||
# forward reference resolves; the real def runs before any call.
|
||||
(each src [# single local fn
|
||||
"(letfn [(twice [x] (* x 2))] (twice 5))"
|
||||
# self-recursion within a local fn
|
||||
"(letfn [(fact [n] (if (zero? n) 1 (* n (fact (dec n)))))] (fact 5))"
|
||||
# MUTUAL recursion — the letrec semantics a sequential let* lacks
|
||||
"(letfn [(ev? [n] (if (zero? n) true (od? (dec n)))) (od? [n] (if (zero? n) false (ev? (dec n))))] (ev? 10))"
|
||||
"(letfn [(ev? [n] (if (zero? n) true (od? (dec n)))) (od? [n] (if (zero? n) false (ev? (dec n))))] (od? 7))"
|
||||
# local fn passed to a higher-order fn
|
||||
"(letfn [(sq [x] (* x x))] (map sq [1 2 3]))"
|
||||
# declare + forward reference (the canonical mutually-recursive top-level use)
|
||||
"(declare is-ev) (defn is-od [n] (if (zero? n) false (is-ev (dec n)))) (defn is-ev [n] (if (zero? n) true (is-od (dec n)))) (is-ev 10)"
|
||||
# declare then redefine: the real def overwrites the reserved cell
|
||||
"(declare foo) (def foo 10) foo"]
|
||||
(let [[code out err] (d/run-on-chez ctx src)
|
||||
want (cli-oracle src)]
|
||||
(ok (string "letfn/declare: " src) (and (= code 0) (= out want))
|
||||
(string "chez=" out " janet=" want " | " err))))
|
||||
|
||||
# 3h) prelude mode (inc 3d): emitting clojure.core ITSELF, a core->core ref must
|
||||
# lower to a runtime var-deref instead of being rejected as "out of subset".
|
||||
# `frequencies` is a core fn but not a native-op, so it exercises the switch.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue