host/chez/multimethods.ss implements the multimethod runtime: defmulti/defmethod expand to defmulti-setup/defmethod-setup calls (+ get-method/methods/ remove-method/prefer-method/prefers). A jolt-multifn record carries its dispatch fn and a jolt=-keyed method table; jolt-invoke dispatches it (exact match, then isa?/hierarchy with prefer-method, then :default), reusing the overlay's isa?/derive/make-hierarchy. The multifn's ns comes from a runtime chez-current-ns (default user; the prelude load sets clojure.core for print-method/print-dup). Two emit-side changes were needed: - late-bind (:late-bind-unresolved? ctx flag, default OFF): defmulti expands to a bare-symbol setup call, so the analyzer doesn't intern the name and a forward reference '(area ...)' after '(defmulti area ...)' in one form was 'Unable to resolve symbol'. The strict compiler punts these to the interpreter; the Chez back end has none, so the flag lowers an unresolved symbol to a var-ref against the compile ns (open-world -e semantics). Set only by the Chez make-ctx / jolt-chez; the main compiler keeps strict resolution (host_iface late-bind? defaults nil). - a :var call head now routes through jolt-invoke, since a late-bound var can hold a multifn (or keyword/coll IFn), not just a procedure. Transparent for procedures; the hot self-recursive call is a :local known-proc, stays direct. Class-based dispatch ((class x)/String) deferred (needs deftype/class subsystem). Parity 1506 -> 1530/2497, 0 new divergences. emit-test 302/302. Full janet gate green (the analyzer flag is off there; suite flakiness under parallel load only).
52 lines
2.3 KiB
Text
52 lines
2.3 KiB
Text
# -e-capable jolt-chez (jolt-9ziu): the Option-2 back end as a runnable CLI.
|
|
#
|
|
# Analysis runs on Janet (the portable analyzer); EXECUTION runs on Chez with the
|
|
# full clojure.core assembled as a Scheme prelude (driver/emit-core-prelude). The
|
|
# prelude is assembled once and cached on disk keyed by a fingerprint of the core
|
|
# sources + the Chez RT/emitter, so repeated invocations (e.g. the run-corpus.janet
|
|
# gate, one subprocess per case) reuse it.
|
|
#
|
|
# Usage (the run-corpus.janet boundary): jolt-chez -e "EXPR"
|
|
# Run from the repo root (the prelude loads host/chez/rt.ss by relative path).
|
|
(import ../../src/jolt/api :as api)
|
|
(import ./driver :as d)
|
|
|
|
(defn- fingerprint []
|
|
# Hash the inputs that shape the prelude: the core tiers + the emitter + the
|
|
# Chez RT shims. Any change invalidates the cached prelude.
|
|
(def parts @[])
|
|
(each tf d/core-tier-files
|
|
(array/push parts (slurp (string "jolt-core/clojure/core/" tf ".clj"))))
|
|
(each f ["host/chez/emit.janet" "host/chez/driver.janet" "host/chez/rt.ss"
|
|
"host/chez/values.ss" "host/chez/collections.ss" "host/chez/seq.ss"
|
|
"host/chez/atoms.ss" "host/chez/predicates.ss" "host/chez/regex.ss"]
|
|
(array/push parts (slurp f)))
|
|
(string/slice (string (hash (string/join parts))) 0))
|
|
|
|
(defn- ensure-prelude [ctx]
|
|
(def dir (or (os/getenv "JOLT_IMAGE_CACHE_DIR") (os/getenv "TMPDIR") "/tmp"))
|
|
(def path (string dir "/jolt-chez-prelude-" (fingerprint) ".ss"))
|
|
(unless (os/stat path)
|
|
(def [scm _ _] (d/emit-core-prelude ctx))
|
|
(spit path scm))
|
|
path)
|
|
|
|
(defn main [& argv]
|
|
# argv: [script "-e" EXPR]
|
|
(def args (drop 1 argv))
|
|
(unless (and (= (length args) 2) (= (first args) "-e"))
|
|
(eprint "usage: jolt-chez -e EXPR")
|
|
(os/exit 2))
|
|
(def src (in args 1))
|
|
(def ctx (api/init-cached {:compile? true}))
|
|
# late-bind unresolved symbols (no interpreter to punt to) so defmulti/defmethod
|
|
# forward references lower to a var-deref (jolt-9ls5), matching d/make-ctx.
|
|
(put (get ctx :env) :late-bind-unresolved? true)
|
|
(def prelude-path (ensure-prelude ctx))
|
|
(def [code out err] (d/eval-e-with-prelude ctx src prelude-path))
|
|
(when (= code :emit-err)
|
|
(eprint "jolt-chez: cannot compile: " out)
|
|
(os/exit 1))
|
|
(unless (= "" out) (print out))
|
|
(unless (= "" err) (eprint err))
|
|
(os/exit code))
|