Chez Phase 1 (increment 3i): regex via vendored irregex

Closes the last clojure.core prelude emit gap (parse-uuid): the whole
non-macro core now lowers to Scheme (prelude reach 355/355).

A #"..." literal analyzes to a :regex IR node. The Chez back end emits
a jolt-regex value over irregex (Alex Shinn, BSD), vendored as the
vendor/irregex submodule -- a portable Scheme regex with PCRE/Java-style
string patterns and first-class Chez support. host/chez/regex.ss wraps
jolt's re-* surface over it: irregex-match -> re-matches (anchored),
irregex-search -> re-find, groups as Clojure [whole g1 ...] vectors,
re-seq as a jolt seq. re-pattern/re-matches/re-find/re-seq/regex? are
def-var!'d into clojure.core so prelude / -e code resolves them.

They stay OUT of the subset native-ops on purpose: irregex's
Unicode/property-class semantics differ from the seed's byte-PEG
approximation, so keeping them prelude-only avoids dragging
engine-difference divergences into the subset-parity corpus. The Janet
back end punts :regex to the interpreter (the seed compiles #"..." to a
Janet PEG), so the main language is unchanged.

Only two adaptations for Chez's top level: a cond-expand shim (Chez's is
library-only) and a normalizing error wrapper (silences irregex's 1-arg
error warnings). rt.ss load is ~0.18s.

emit-test 131/131 (regex literal + re-* parity vs the CLI oracle);
prelude reach 355/355; Chez subset 672/672, 0 divergences; full gate
green.
This commit is contained in:
Yogthos 2026-06-17 19:44:18 -04:00
parent b1cdfd1c9b
commit 37c433bd4a
11 changed files with 164 additions and 14 deletions

View file

@ -369,6 +369,11 @@
:throw (string "(jolt-throw " (emit (get node :expr)) ")")
:try (emit-try node)
:quote (emit-quoted (get node :form))
# regex literal #"…" -> a jolt-regex value (regex.ss compiles the source via
# the vendored irregex). %j quotes+escapes the source; a backslash in the
# pattern becomes \\ in the Scheme string literal -> the 1-char backslash
# irregex expects (same escaping emit-const uses for strings).
:regex (string "(jolt-regex " (string/format "%j" (get node :source)) ")")
# host interop (jolt-0kf5): (.method target arg*) -> (jolt-host-call "method"
# target arg*). Only the methods the RT dispatcher (rt.ss) actually shims are
# IN the subset; any other method is out of subset (a clean emit-time reject,

81
host/chez/regex.ss Normal file
View file

@ -0,0 +1,81 @@
;; Phase 1 (jolt-cf1q.2) — regex on Chez via vendored irregex (jolt-i0s3).
;;
;; jolt's seed regex (src/jolt/regex.janet) compiles patterns to Janet's PEG
;; engine; Chez has no regex at all. Rather than re-host that engine, we vendor
;; Alex Shinn's irregex (vendor/irregex, BSD) — a portable Scheme regex with
;; PCRE/Java-style STRING patterns — and wrap jolt's re-* surface over it.
;;
;; irregex maps cleanly onto the Clojure fns: irregex-match is an anchored
;; whole-string match (= re-matches), irregex-search finds the first match
;; anywhere (= re-find), irregex-match-substring extracts group N (0 = whole).
;; Results follow Clojure shape: a 0-group match is the whole string; a grouped
;; match is a jolt VECTOR [whole g1 ...] (a non-participating group is nil); a nil
;; result is jolt-nil; re-seq is a jolt seq (nil when there are no matches).
;;
;; The re-* fns are def-var!'d into clojure.core so prelude / -e code resolves
;; them at runtime (they're NOT subset native-ops: irregex's Unicode/property-
;; class semantics differ from the seed's byte-PEG approximation, so they stay out
;; of the subset-parity corpus). Loaded from rt.ss after def-var! is defined.
;; irregex.scm is portable R[457]RS; two small adaptations for Chez's top level:
;; a cond-expand at expression position (Chez's is library-only), and `error`
;; called with a lone string (Chez's error wants who+msg). The wrapper normalizes
;; both without changing behavior for valid patterns.
(define-syntax cond-expand
(syntax-rules (else)
((_ (else e ...)) (begin e ...))
((_ (else e ...) c ...) (begin e ...))
((_ (req e ...) c ...) (cond-expand c ...))
((_) (if #f #f))))
(define %chez-error error)
(define (error . args)
(if (and (pair? args) (string? (car args)))
(apply %chez-error #f args)
(apply %chez-error args)))
(load "vendor/irregex/irregex.scm")
;; A jolt regex value: the source string (for printing / str) + the compiled
;; irregex. regex? recognizes it; the printer renders #"source".
(define-record-type regex-t (fields source irx) (nongenerative jolt-regex-v1))
(define (jolt-regex source) (make-regex-t source (irregex source)))
(define (jolt-regex? x) (regex-t? x))
(define (jolt-re-pattern x) (if (regex-t? x) x (jolt-regex x)))
;; An irregex match -> the Clojure result: whole string (no groups) or the
;; [whole g1 ... gn] vector (nil for a non-participating group).
(define (irx-result m)
(let ((n (irregex-match-num-submatches m)))
(if (= n 0)
(irregex-match-substring m 0)
(let loop ((i n) (acc '()))
(if (< i 0)
(apply jolt-vector acc)
(let ((s (irregex-match-substring m i)))
(loop (- i 1) (cons (if s s jolt-nil) acc))))))))
(define (jolt-re-matches re s)
(let ((m (irregex-match (regex-t-irx (jolt-re-pattern re)) s)))
(if m (irx-result m) jolt-nil)))
(define (jolt-re-find re s)
(let ((m (irregex-search (regex-t-irx (jolt-re-pattern re)) s)))
(if m (irx-result m) jolt-nil)))
;; All non-overlapping matches, left to right. Advance past each match end (or by
;; one on a zero-width match). nil when there are no matches (Clojure: seq-able as
;; nil, so (if-let [m (re-seq ...)] ...) works), matching the seed.
(define (jolt-re-seq re s)
(let ((irx (regex-t-irx (jolt-re-pattern re)))
(len (string-length s)))
(let loop ((start 0) (acc '()))
(let ((m (and (<= start len) (irregex-search irx s start))))
(if m
(let ((e (irregex-match-end-index m 0)))
(loop (if (> e start) e (+ start 1)) (cons (irx-result m) acc)))
(list->cseq (reverse acc)))))))
(def-var! "clojure.core" "re-pattern" jolt-re-pattern)
(def-var! "clojure.core" "re-matches" jolt-re-matches)
(def-var! "clojure.core" "re-find" jolt-re-find)
(def-var! "clojure.core" "re-seq" jolt-re-seq)
(def-var! "clojure.core" "regex?" jolt-regex?)

View file

@ -79,6 +79,11 @@
(unless (hashtable-ref var-table k #f)
(hashtable-set! var-table k (make-var-cell ns name jolt-unbound)))))
;; regex (jolt-i0s3): defines regex-t + the re-* fns (def-var!'d into
;; clojure.core), so it loads after def-var! and before the printer below (which
;; renders a regex-t as #"source").
(load "host/chez/regex.ss")
;; --- jolt number printing ----------------------------------------------------
;; jolt models every number as a Clojure double: integer-valued values print
;; without a ".0" (the Janet host prints (* 1.0 5) as "5", (/ 1 2) as "0.5").
@ -115,6 +120,7 @@
((jolt-symbol? x) (let ((ns (symbol-t-ns x)))
(if (or (jolt-nil? ns) (not ns) (eq? ns '())) (symbol-t-name x)
(string-append ns "/" (symbol-t-name x)))))
((regex-t? x) (string-append "#\"" (regex-t-source x) "\""))
((pvec? x) (let ((acc '())) (let loop ((i (fx- (pvec-count x) 1)))
(when (fx>=? i 0) (set! acc (cons (jolt-pr-str (pvec-nth-d x i jolt-nil)) acc)) (loop (fx- i 1))))
(string-append "[" (jolt-str-join acc) "]")))