jolt/test/integration/fallback-zero-test.janet
Dmitri Sotnikov 11fb5a7de6
Stage2 task2 tier5 (#16)
* core: Stage 2 Task 2 tier 5a — compile defmulti + defmethod

defmulti/defmethod become macros (30-macros) over ctx-capturing
clojure.core fns (defmulti-setup/defmethod-setup, interned by
install-stateful-fns!):
- defmulti: (defmulti name dispatch & opts) -> (defmulti-setup 'name
  dispatch ~@opts). name quoted; dispatch + opts (:default/:hierarchy)
  evaluated. defmulti-setup builds the dispatch closure over the method
  table and interns the var (same hierarchy/default/cache behavior).
- defmethod: (defmethod mm dval & fn-tail) -> (defmethod-setup 'mm dval
  (fn ~@fn-tail)). The method impl is now a COMPILED (fn …) (was an
  interpreted fn* eval). Auto-creates the multimethod if missing.
- removed their special-symbol? entries + eval-list arms, and dropped them
  from host_iface special-names + loader stateful-head?.

Both compile + interpret as plain invokes; dispatch incl. :default and
derive/hierarchy works in both modes.

Tests: evaluator-test (defmulti case) + namespace-test now use init (these
forms are overlay macros now, so a bare make-ctx lacks them).

Gate green: conformance 269x3, fallback-zero 38/4, bootstrap-fixpoint
stage1==2==3, self-host, staged-bootstrap, sci-bootstrap, clojure-test-suite
>=4034/67, features 78/78, all unit + spec (multimethods 16/16).

* core: Stage 2 Task 2 tier 5b — compile deftype + defrecord

deftype becomes a macro (30-macros) over make-deftype-ctor (a ctx-capturing
clojure.core fn that bakes the ns-qualified type tag at def time) plus
extend-type for any inline protocol methods — so it compiles as a plain (do …).
Mirrors defrecord's existing field-let/protocol-grouping pattern.
- make-deftype-ctor-impl (evaluator) builds the ctor; interned as a closure.
- removed the deftype special-symbol? entry + eval-list arm; dropped deftype/
  defrecord from host_iface special-names + loader stateful-head?.
- defrecord no longer redefines ->name via (Name. …) interop (frozen) — deftype
  already provides ->name, so defrecord compiles too (map->name builds via it).
- field-kws spliced into a vector LITERAL ([~@…]) so the analyzer sees a vector
  form, not a runtime pvec; type name + fields are unwrapped of ^meta (the reader
  yields (with-meta sym m) forms, e.g. sci's (deftype ^{:doc …} Var …)).

With tier 5a, all of deftype/defrecord/defmulti/defmethod compile. The loader's
interpret-only set is now just the frozen host-coupled forms: defmacro/set!/./
new/eval.

Tests: evaluator-test deftype case uses init (deftype is an overlay macro now);
fallback-zero moves deftype off must-punt, adds deftype/defrecord/defmulti/
defmethod to must-compile (43/3).

Gate green (full jpm build + jpm test): conformance 269x3, fallback-zero 43/3,
bootstrap-fixpoint stage1==2==3, self-host, staged-bootstrap, sci-bootstrap
422/0, clojure-test-suite >=4034/67, all unit + spec.

---------

Co-authored-by: Yogthos <yogthos@gmail.com>
2026-06-10 08:13:42 +08:00

76 lines
3.5 KiB
Text

# Fallback-zero verification (Stage 1 Task 3).
#
# self-host-test.janet checks observable RESULTS but not WHICH path ran — a form
# that silently fell back to the interpreter still "passes" there. This harness
# checks the path: it runs the portable analyzer (jolt.analyzer/analyze, via
# backend/analyze-form) on a corpus of NON-STATEFUL forms and asserts NONE raise
# :jolt/uncompilable — i.e. the self-hosted analyzer actually COMPILED them.
#
# As analyzer↔compiler.janet parity grows (Stage 1), move forms from the
# "intentional fallback" sanity list into the must-compile corpus. The day the
# fallback set equals the frozen intentional stateful set, the Janet bootstrap
# compiler is retireable.
#
# Mechanism: backend/analyze-form throws (a "jolt/uncompilable: …" string) for a
# punted form; (protect …) turns that into [false msg]. [true ir] == compiled.
(import ../../src/jolt/backend :as backend)
(use ../../src/jolt/api)
(use ../../src/jolt/reader)
(def ctx (init))
(defn- analyzes? [s]
# true if the analyzer produced IR (compiled), false if it punted/uncompilable.
(def r (protect (backend/analyze-form ctx (parse-string s))))
(and (r 0) true))
# --- Must compile: pure, non-stateful value production. NONE may punt. ---
(def must-compile
[# set literals (Task 1)
"#{1 2 3}" "#{}" "#{:a :b :c}" "#{(inc 0) 2}" "(conj #{1 2} 3)"
"[#{1 2} {:s #{3}}]" "(let [x 5] #{x (inc x)})"
# other literals
"[1 2 3]" "{:a 1 :b 2}" "{:k (inc 0)}" "[[1] [2 3]]" "42" ":kw" "\"str\""
# control flow + binding
"(+ 1 2)" "(if true 1 2)" "(do 1 2 3)" "(let [a 1 b 2] (+ a b))"
"(fn [x] (* x x))" "(fn ([a] a) ([a b] (+ a b)))"
"(loop [i 0] (if (< i 3) (recur (inc i)) i))"
"(quote (a b c))" "(throw (ex-info \"x\" {}))"
"(try (inc 1) (catch :default e e))"
# def + calls into core
"(def answer 42)" "(map inc [1 2 3])" "(reduce + 0 [1 2 3])"
"(get {:a 1} :a)" "(vec (range 5))"
# set?/disj are plain fns now, not special forms (jolt-g3h)
"(set? #{1 2})" "(disj #{1 2 3} 2)"
# Stage 2 (jolt-eaa): stateful forms moved onto the compile path. (binding only
# compiles over an INTERNED var; the built-in dynamic vars aren't interned yet,
# so it's exercised end-to-end in the state spec instead.)
"(require (quote [clojure.string :as s]))" "(in-ns (quote foo.bar))"
"(ns foo.bar (:require [clojure.string :as s]))"
"(defprotocol P (m [x]))" "(extend-type Long P (m [x] x))"
"(reify P (m [this] 1))" "(var map)"
# Stage 2 tier 5: type/dispatch definitional forms compile too
"(deftype Pt [x y])" "(deftype Sq [s] P (m [this] s))"
"(defrecord Rec [a b])" "(defmulti mf :k)" "(defmethod mf :a [x] x)"])
# --- Intentional fallback (sanity sample): these SHOULD punt to the interpreter.
# The remaining frozen/uncompiled set keeps the harness honest in the punt
# direction: defmacro + set! (frozen host-coupled), and letfn (needs letrec IR).
(def must-punt
["(defmacro m [x] x)"
"(set! *warn-on-reflection* true)" "(letfn [(f [n] (g n)) (g [n] (f n))] (f 1))"])
(var fails @[])
(each s must-compile
(unless (analyzes? s) (array/push fails (string "FALLBACK (should compile): " s))))
(each s must-punt
(when (analyzes? s) (array/push fails (string "COMPILED (should punt): " s))))
(printf "fallback-zero: %d must-compile + %d must-punt — %d failures"
(length must-compile) (length must-punt) (length fails))
(when (> (length fails) 0)
(print "\nFailures:")
(each f fails (printf " %s" f))
(os/exit 1))
(print "fallback-zero: OK (analyzer compiled the full non-stateful corpus)")