Chez Phase 1 (increment 3f): quoted literals
Emit a :quote node by reconstructing the raw reader form as RT constructor calls: symbol -> jolt-symbol, list (array) -> jolt-list, vector (tuple) -> jolt-vector, map -> jolt-hash-map, set -> jolt-hash-set, scalars via emit-const. The runtime value of a quote is just that literal data (the interpreter returns the reader form verbatim). Quote exposed a latent seq.ss bug: empty map/filter results were jolt-nil, but Clojure's (map f []) is an empty seq, so (= () (map f [])) must be true. Return jolt-empty-list (which seqs back to nil, so it's still a valid lazy-tail terminator) instead — matching jolt-take/drop/rest/list. Prelude emit reach 334 -> 342/355. Subset probe 632 -> 664/664 compiled, 0 divergences (quote + the seq fix pull 32 corpus cases into the subset). emit-test 110/110 (added 16 quote cases). corpus.edn regenerated (the 3 malformed-catch spec rows). Full gate green.
This commit is contained in:
parent
d35783bf1b
commit
930800f9a6
6 changed files with 81 additions and 19 deletions
|
|
@ -138,6 +138,35 @@
|
|||
(string "(integer->char " (get v :ch) ")")
|
||||
(errorf "emit-const: unsupported literal %p" v)))
|
||||
|
||||
# Quoted literals (jolt-u8j7). A :quote node's :form is the RAW reader form (a
|
||||
# Janet value): scalars are Janet natives, a symbol is {:jolt/type :symbol …}, a
|
||||
# list is an array, a vector a tuple, a map a struct/phm, a set a tagged struct.
|
||||
# Reconstruct each as the matching Chez RT constructor — the runtime value of a
|
||||
# quote is just that literal data (the interpreter returns the reader form
|
||||
# verbatim; the Janet backend Janet-quotes it; here we rebuild it on the RT).
|
||||
(var emit-quoted nil)
|
||||
(defn- emit-quoted-map [m]
|
||||
(def flat @[])
|
||||
(eachp [k v] m (array/push flat (emit-quoted k)) (array/push flat (emit-quoted v)))
|
||||
(string "(jolt-hash-map " (string/join flat " ") ")"))
|
||||
(set emit-quoted (fn emit-quoted [form]
|
||||
(cond
|
||||
# scalars emit-const already lowers (nil/bool/number/string/keyword/char)
|
||||
(or (nil? form) (boolean? form) (number? form) (string? form) (keyword? form))
|
||||
(emit-const form)
|
||||
(and (struct? form) (= :symbol (get form :jolt/type)))
|
||||
(let [ns (get form :ns)]
|
||||
(string "(jolt-symbol " (if ns (string/format "%j" ns) "#f") " "
|
||||
(string/format "%j" (get form :name)) ")"))
|
||||
(and (struct? form) (= :jolt/char (get form :jolt/type))) (emit-const form)
|
||||
(and (struct? form) (= :jolt/set (get form :jolt/type)))
|
||||
(string "(jolt-hash-set " (string/join (map emit-quoted (get form :value)) " ") ")")
|
||||
(array? form) (string "(jolt-list " (string/join (map emit-quoted form) " ") ")")
|
||||
(tuple? form) (string "(jolt-vector " (string/join (map emit-quoted form) " ") ")")
|
||||
(phm/phm? form) (emit-quoted-map (phm/phm-to-struct form))
|
||||
(or (struct? form) (table? form)) (emit-quoted-map form)
|
||||
(errorf "emit-quoted: unsupported quoted form %p" form))))
|
||||
|
||||
(defn- emit-binding [b]
|
||||
(def b (vv b))
|
||||
(string "(" (munge (get b 0)) " " (emit (get b 1)) ")"))
|
||||
|
|
@ -331,6 +360,7 @@
|
|||
:recur (emit-recur node)
|
||||
:throw (string "(jolt-throw " (emit (get node :expr)) ")")
|
||||
:try (emit-try node)
|
||||
:quote (emit-quoted (get node :form))
|
||||
:fn (emit-fn node)
|
||||
:def (string "(def-var! " (string/format "%j" (get node :ns)) " "
|
||||
(string/format "%j" (get node :name)) " " (emit (get node :init)) ")")
|
||||
|
|
|
|||
|
|
@ -107,11 +107,15 @@
|
|||
;; map / filter / reduce / into / remove + range / take / concat / apply
|
||||
;; ============================================================================
|
||||
(define (any-nil? seqs) (and (pair? seqs) (or (jolt-nil? (car seqs)) (any-nil? (cdr seqs)))))
|
||||
;; An EMPTY seq result is () (jolt-empty-list), NOT nil — Clojure's (map f []) is
|
||||
;; an empty seq, so (= () (map f [])) is true and (nil? (map f [])) is false.
|
||||
;; jolt-empty-list seqs back to nil, so it stays a valid lazy-tail terminator for
|
||||
;; the non-empty case (printing / seq= / reduce all walk via jolt-seq).
|
||||
(define (map-seq f s)
|
||||
(if (jolt-nil? s) jolt-nil
|
||||
(if (jolt-nil? s) jolt-empty-list
|
||||
(cseq-lazy (jolt-invoke f (seq-first s)) (lambda () (map-seq f (jolt-seq (seq-more s)))))))
|
||||
(define (map-seq* f seqs) ; multi-collection map; stops at the shortest
|
||||
(if (any-nil? seqs) jolt-nil
|
||||
(if (any-nil? seqs) jolt-empty-list
|
||||
(cseq-lazy (apply jolt-invoke f (map seq-first seqs))
|
||||
(lambda () (map-seq* f (map (lambda (s) (jolt-seq (seq-more s))) seqs))))))
|
||||
(define (jolt-map f . colls)
|
||||
|
|
@ -121,7 +125,7 @@
|
|||
|
||||
(define (filter-seq pred s keep)
|
||||
(let loop ((s s))
|
||||
(cond ((jolt-nil? s) jolt-nil)
|
||||
(cond ((jolt-nil? s) jolt-empty-list) ; empty result is () (see map-seq)
|
||||
((eq? keep (jolt-truthy? (jolt-invoke pred (seq-first s))))
|
||||
(cseq-lazy (seq-first s) (lambda () (filter-seq pred (jolt-seq (seq-more s)) keep))))
|
||||
(else (loop (jolt-seq (seq-more s)))))))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue