diff --git a/host/chez/emit.janet b/host/chez/emit.janet index 30efec4..26a361a 100644 --- a/host/chez/emit.janet +++ b/host/chez/emit.janet @@ -503,6 +503,10 @@ # 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 " (chez-str-lit (get node :source)) ")") + # #inst / #uuid literals -> a runtime inst / uuid value (inst-time.ss / + # natives-misc.ss). The source string round-trips through chez-str-lit. + :inst (string "(jolt-inst-from-string " (chez-str-lit (get node :source)) ")") + :uuid (string "(jolt-uuid-from-string " (chez-str-lit (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, diff --git a/host/chez/inst-time.ss b/host/chez/inst-time.ss new file mode 100644 index 0000000..658939a --- /dev/null +++ b/host/chez/inst-time.ss @@ -0,0 +1,293 @@ +;; #inst values + a java.time formatting shim (jolt-at0a, inc X). +;; +;; A #inst literal lowers (analyzer :inst node -> emit) to (jolt-inst-from-string +;; "…"); this file parses the RFC3339 string to epoch-ms and models the value as a +;; `jinst` record (one flonum field, ms). Equality / map-key hashing are by the +;; INSTANT (offset-normalized), matching the seed (types_ctx.janet parse-inst / +;; inst->rfc3339). The overlay inst?/inst-ms read (get x :jolt/type)/(get x :ms), +;; so jolt-get answers those off a jinst — the overlay fns then work unchanged. +;; +;; The java.time surface (DateTimeFormatter/Instant/ZoneId/LocalDateTime/ +;; FormatStyle/Locale + the .format/.atZone/.toInstant/… methods) is the Chez port +;; of java_base.janet, registered through host-static.ss's class-statics / host- +;; methods registries — so this loads LAST in rt.ss, after host-static.ss and io.ss. + +;; --- civil <-> days since the Unix epoch (Howard Hinnant's algorithms) ------- +;; No portable UTC mktime on Chez, so compute epoch days directly from y/m/d. +(define (days-from-civil y m d) + (let* ((y2 (if (<= m 2) (- y 1) y)) + (era (quotient (if (>= y2 0) y2 (- y2 399)) 400)) + (yoe (- y2 (* era 400))) + (doy (+ (quotient (+ (* 153 (+ m (if (> m 2) -3 9))) 2) 5) (- d 1))) + (doe (+ (* yoe 365) (quotient yoe 4) (- (quotient yoe 100)) doy))) + (+ (* era 146097) doe -719468))) + +(define (civil-from-days z) ; -> (values year month day) + (let* ((z2 (+ z 719468)) + (era (quotient (if (>= z2 0) z2 (- z2 146096)) 146097)) + (doe (- z2 (* era 146097))) + (yoe (quotient (+ doe (- (quotient doe 1460)) (quotient doe 36524) (- (quotient doe 146096))) 365)) + (y (+ yoe (* era 400))) + (doy (- doe (+ (* 365 yoe) (quotient yoe 4) (- (quotient yoe 100))))) + (mp (quotient (+ (* 5 doy) 2) 153)) + (d (+ (- doy (quotient (+ (* 153 mp) 2) 5)) 1)) + (m (+ mp (if (< mp 10) 3 -9)))) + (values (if (<= m 2) (+ y 1) y) m d))) + +;; --- RFC3339 parse: yyyy[-MM[-dd[Thh[:mm[:ss[.fff]]]]]][Z|±hh:mm] -> ms ------- +(define-record-type jinst (fields ms) (nongenerative chez-jinst-v1)) + +(define (digit? c) (and (char>=? c #\0) (char<=? c #\9))) +(define (digits-at s i n) ; n digits from i -> integer, or #f + (and (<= (+ i n) (string-length s)) + (let loop ((j i) (acc 0)) + (if (= j (+ i n)) + acc + (and (digit? (string-ref s j)) + (loop (+ j 1) (+ (* acc 10) (- (char->integer (string-ref s j)) 48)))))))) + +(define (jolt-inst-from-string ts) + (define len (string-length ts)) + (define (fail) (error #f (string-append "Unrecognized #inst timestamp: " ts))) + (let* ((year (or (digits-at ts 0 4) (fail))) + (i 4) (month 1) (day 1) (hh 0) (mm 0) (ss 0) (frac-ms 0) (off-s 0)) + ;; -MM + (when (and (< i len) (char=? (string-ref ts i) #\-) (digits-at ts (+ i 1) 2)) + (set! month (digits-at ts (+ i 1) 2)) (set! i (+ i 3))) + ;; -dd + (when (and (< i len) (char=? (string-ref ts i) #\-) (digits-at ts (+ i 1) 2)) + (set! day (digits-at ts (+ i 1) 2)) (set! i (+ i 3))) + ;; Thh + (when (and (< i len) (or (char=? (string-ref ts i) #\T) (char=? (string-ref ts i) #\t)) + (digits-at ts (+ i 1) 2)) + (set! hh (digits-at ts (+ i 1) 2)) (set! i (+ i 3)) + ;; :mm + (when (and (< i len) (char=? (string-ref ts i) #\:) (digits-at ts (+ i 1) 2)) + (set! mm (digits-at ts (+ i 1) 2)) (set! i (+ i 3)) + ;; :ss + (when (and (< i len) (char=? (string-ref ts i) #\:) (digits-at ts (+ i 1) 2)) + (set! ss (digits-at ts (+ i 1) 2)) (set! i (+ i 3)) + ;; .fff (truncate beyond 3) + (when (and (< i len) (char=? (string-ref ts i) #\.)) + (let loop ((j (+ i 1)) (k 0) (acc 0)) + (if (and (< j len) (digit? (string-ref ts j))) + (loop (+ j 1) (+ k 1) (if (< k 3) (+ (* acc 10) (- (char->integer (string-ref ts j)) 48)) acc)) + (begin + (set! frac-ms (* acc (expt 10 (max 0 (- 3 k))))) + (set! i j)))))))) + ;; offset Z | ±hh:mm + (when (< i len) + (let ((c (string-ref ts i))) + (cond + ((or (char=? c #\Z) (char=? c #\z)) (set! i (+ i 1))) + ((or (char=? c #\+) (char=? c #\-)) + (let ((oh (digits-at ts (+ i 1) 2)) (om (digits-at ts (+ i 4) 2))) + (unless (and oh om (char=? (string-ref ts (+ i 3)) #\:)) (fail)) + (set! off-s (* (if (char=? c #\-) -1 1) (+ (* oh 3600) (* om 60)))) + (set! i (+ i 6)))) + (else (fail))))) + (unless (= i len) (fail)) + (let ((base-s (+ (* (days-from-civil year month day) 86400) (* hh 3600) (* mm 60) ss))) + (make-jinst (exact->inexact (- (+ (* base-s 1000) frac-ms) (* off-s 1000))))))) + +;; --- canonical print form: yyyy-MM-ddThh:mm:ss.fff-00:00 (UTC) --------------- +(define (pad2 n) (if (< n 10) (string-append "0" (number->string n)) (number->string n))) +(define (pad4 n) (let ((s (number->string n))) (string-append (make-string (max 0 (- 4 (string-length s))) #\0) s))) +(define (pad3 n) (let ((s (number->string n))) (string-append (make-string (max 0 (- 3 (string-length s))) #\0) s))) +(define (floor-div a b) (let ((q (quotient a b)) (r (remainder a b))) (if (and (not (= r 0)) (< (* a b) 0)) (- q 1) q))) +(define (floor-mod a b) (- a (* (floor-div a b) b))) + +(define (inst-fields ms) ; -> list (y mo d hh mm ss frac dow) + (let* ((total-s (floor-div (exact (truncate ms)) 1000)) + (frac (- (exact (truncate ms)) (* total-s 1000))) + (days (floor-div total-s 86400)) + (sod (floor-mod total-s 86400)) + (hh (quotient sod 3600)) (mm (quotient (remainder sod 3600) 60)) (ss (remainder sod 60)) + (dow (floor-mod (+ days 4) 7))) ; 1970-01-01 = Thursday; 0=Sunday + (call-with-values (lambda () (civil-from-days days)) + (lambda (y mo d) (list y mo d hh mm ss frac dow))))) + +(define (inst-rfc3339 inst) + (let ((f (inst-fields (jinst-ms inst)))) + (string-append (pad4 (list-ref f 0)) "-" (pad2 (list-ref f 1)) "-" (pad2 (list-ref f 2)) + "T" (pad2 (list-ref f 3)) ":" (pad2 (list-ref f 4)) ":" (pad2 (list-ref f 5)) + "." (pad3 (list-ref f 6)) "-00:00"))) + +;; --- DateTimeFormatter pattern engine (port of java_base.janet format-ms) ----- +(define month-names (vector "January" "February" "March" "April" "May" "June" "July" + "August" "September" "October" "November" "December")) +(define day-names (vector "Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday")) + +(define (format-ms pattern ms) + (let ((f (inst-fields ms)) (n (string-length pattern)) (out (open-output-string))) + (let ((y (list-ref f 0)) (mo (list-ref f 1)) (d (list-ref f 2)) + (hh (list-ref f 3)) (mi (list-ref f 4)) (se (list-ref f 5)) (dow (list-ref f 7))) + (define (run-len i c) (let loop ((j i)) (if (and (< j n) (char=? (string-ref pattern j) c)) (loop (+ j 1)) (- j i)))) + (let loop ((i 0)) + (when (< i n) + (let* ((c (string-ref pattern i)) (k (run-len i c))) + (cond + ((char=? c #\') + (if (and (< (+ i 1) n) (char=? (string-ref pattern (+ i 1)) #\')) + (begin (write-char #\' out) (loop (+ i 2))) + (let close ((j (+ i 1))) + (cond ((>= j n) (loop j)) + ((char=? (string-ref pattern j) #\') (loop (+ j 1))) + (else (write-char (string-ref pattern j) out) (close (+ j 1))))))) + ((char=? c #\y) (display (if (>= k 4) (number->string y) (pad2 (modulo y 100))) out) (loop (+ i k))) + ((char=? c #\M) + (display (cond ((= k 1) (number->string mo)) ((= k 2) (pad2 mo)) + ((= k 3) (substring (vector-ref month-names (- mo 1)) 0 3)) + (else (vector-ref month-names (- mo 1)))) out) + (loop (+ i k))) + ((char=? c #\d) (display (if (= k 1) (number->string d) (pad2 d)) out) (loop (+ i k))) + ((char=? c #\E) + (display (if (>= k 4) (vector-ref day-names dow) (substring (vector-ref day-names dow) 0 3)) out) + (loop (+ i k))) + ((char=? c #\H) (display (if (= k 1) (number->string hh) (pad2 hh)) out) (loop (+ i k))) + ((char=? c #\h) + (let ((h12 (let ((h (modulo hh 12))) (if (= h 0) 12 h)))) + (display (if (= k 1) (number->string h12) (pad2 h12)) out)) (loop (+ i k))) + ((char=? c #\m) (display (if (= k 1) (number->string mi) (pad2 mi)) out) (loop (+ i k))) + ((char=? c #\s) (display (if (= k 1) (number->string se) (pad2 se)) out) (loop (+ i k))) + ((char=? c #\a) (display (if (< hh 12) "AM" "PM") out) (loop (+ i k))) + (else (write-char c out) (loop (+ i 1))))))) + (get-output-string out)))) + +;; --- value integration: get / = / hash / pr / type / instance? -------------- +(define kw-jolt-type (keyword "jolt" "type")) +(define kw-ms (keyword #f "ms")) +(define inst-type-kw (keyword "jolt" "inst")) + +(define %it-get jolt-get) +(set! jolt-get (case-lambda + ((coll k) (jolt-get coll k jolt-nil)) + ((coll k d) (if (jinst? coll) + (cond ((jolt=2 k kw-jolt-type) inst-type-kw) + ((jolt=2 k kw-ms) (jinst-ms coll)) + (else d)) + (%it-get coll k d))))) + +(define %it-=2 jolt=2) +(set! jolt=2 (lambda (a b) + (cond ((jinst? a) (and (jinst? b) (= (jinst-ms a) (jinst-ms b)))) + ((jinst? b) #f) + (else (%it-=2 a b))))) + +(define %it-hash jolt-hash) +(set! jolt-hash (lambda (x) (if (jinst? x) (jolt-hash (jinst-ms x)) (%it-hash x)))) + +(define (inst-pr i) (string-append "#inst \"" (inst-rfc3339 i) "\"")) +(define %it-pr-str jolt-pr-str) +(set! jolt-pr-str (lambda (x) (if (jinst? x) (inst-pr x) (%it-pr-str x)))) +(define %it-pr-readable jolt-pr-readable) +(set! jolt-pr-readable (lambda (x) (if (jinst? x) (inst-pr x) (%it-pr-readable x)))) +(define %it-str-render jolt-str-render-one) +(set! jolt-str-render-one (lambda (x) (if (jinst? x) (inst-rfc3339 x) (%it-str-render x)))) + +(define %it-type jolt-type) +(set! jolt-type (lambda (x) (if (jinst? x) inst-type-kw (%it-type x)))) +(def-var! "clojure.core" "type" jolt-type) + +;; instance? java.util.Date -> a jinst; java.time.Instant/LocalDateTime -> the +;; matching jhost tag. The instance? macro passes the class-name symbol. +(define (class-short tn) (let loop ((i (- (string-length tn) 1))) + (cond ((< i 0) tn) ((char=? (string-ref tn i) #\.) (substring tn (+ i 1) (string-length tn))) (else (loop (- i 1)))))) +(define %it-instance-check instance-check) +(set! instance-check + (lambda (type-sym val) + (let ((tn (class-short (symbol-t-name type-sym)))) + (cond + ((jinst? val) (if (string=? tn "Date") #t (%it-instance-check type-sym val))) + ((and (jhost? val) (string=? (jhost-tag val) "instant")) (if (string=? tn "Instant") #t (%it-instance-check type-sym val))) + ((and (jhost? val) (string=? (jhost-tag val) "local-dt")) (if (string=? tn "LocalDateTime") #t (%it-instance-check type-sym val))) + (else (%it-instance-check type-sym val)))))) +(def-var! "clojure.core" "instance-check" instance-check) + +;; inst-ms* is a seed native (the overlay inst-ms reads (get x :ms), now answered). +(def-var! "clojure.core" "inst-ms*" (lambda (i) (jinst-ms i))) + +;; --- java.time shim values (jhost objects over host-static.ss registries) ----- +(define (ms-of d) + (cond ((number? d) d) + ((jinst? d) (jinst-ms d)) + ((and (jhost? d) (member (jhost-tag d) '("instant" "zoned-dt" "local-dt"))) + (vector-ref (jhost-state d) 0)) + (else (error #f "not a date value" d)))) +(define (mk-instant ms) (make-jhost "instant" (vector ms))) +(define (mk-zoned ms) (make-jhost "zoned-dt" (vector ms))) +(define (mk-local ms) (make-jhost "local-dt" (vector ms))) +(define (mk-formatter pat) (make-jhost "dt-formatter" (vector pat))) +(define (fmt-pat f) (vector-ref (jhost-state f) 0)) +(define (now-ms) (exact->inexact (now-millis))) ; now-millis from host-static.ss + +(register-host-methods! "instant" + (list (cons "atZone" (lambda (self zone) (mk-zoned (ms-of self)))) + (cons "toEpochMilli" (lambda (self) (ms-of self))) + (cons "toString" (lambda (self) (inst-rfc3339 (make-jinst (ms-of self))))))) +(register-host-methods! "zoned-dt" + (list (cons "toLocalDateTime" (lambda (self) (mk-local (ms-of self)))) + (cons "toInstant" (lambda (self) (mk-instant (ms-of self)))))) +(register-host-methods! "local-dt" + (list (cons "atZone" (lambda (self zone) (mk-zoned (ms-of self)))))) +(register-host-methods! "dt-formatter" + (list (cons "withLocale" (lambda (self locale) (mk-formatter (fmt-pat self)))) + (cons "format" (lambda (self d) (format-ms (fmt-pat self) (ms-of d)))))) + +;; FormatStyle approximations (no locale DB on this host). +(define style-patterns + '((date . ((short . "M/d/yy") (medium . "MMM d, yyyy") (long . "MMMM d, yyyy") (full . "EEEE, MMMM d, yyyy"))) + (time . ((short . "h:mm a") (medium . "h:mm:ss a") (long . "h:mm:ss a") (full . "h:mm:ss a"))) + (datetime . ((short . "M/d/yy, h:mm a") (medium . "MMM d, yyyy, h:mm:ss a") + (long . "MMMM d, yyyy, h:mm:ss a") (full . "EEEE, MMMM d, yyyy, h:mm:ss a"))))) +(define (style-of fs) (vector-ref (jhost-state fs) 0)) ; a symbol: short/medium/long/full +(define (style-fmt kind fs) + (mk-formatter (or (let ((row (assq kind style-patterns))) (and row (let ((e (assq (style-of fs) (cdr row)))) (and e (cdr e))))) + "yyyy-MM-dd HH:mm:ss"))) + +(register-class-statics! "FormatStyle" + (list (cons "SHORT" (make-jhost "format-style" (vector 'short))) + (cons "MEDIUM" (make-jhost "format-style" (vector 'medium))) + (cons "LONG" (make-jhost "format-style" (vector 'long))) + (cons "FULL" (make-jhost "format-style" (vector 'full))))) +(register-class-statics! "DateTimeFormatter" + (list (cons "ofPattern" (lambda (p . _) (mk-formatter p))) + (cons "ISO_LOCAL_DATE" (mk-formatter "yyyy-MM-dd")) + (cons "ISO_LOCAL_DATE_TIME" (mk-formatter "yyyy-MM-dd'T'HH:mm:ss")) + (cons "ofLocalizedDate" (lambda (fs) (style-fmt 'date fs))) + (cons "ofLocalizedTime" (lambda (fs) (style-fmt 'time fs))) + (cons "ofLocalizedDateTime" (lambda (fs) (style-fmt 'datetime fs))))) +(register-class-statics! "Instant" + (list (cons "ofEpochMilli" (lambda (ms) (mk-instant (exact->inexact ms)))) + (cons "now" (lambda () (mk-instant (now-ms)))))) +(register-class-statics! "ZoneId" + (list (cons "systemDefault" (lambda () (make-jhost "zone-id" (vector "system")))) + (cons "of" (lambda (id) (make-jhost "zone-id" (vector id)))))) +(register-class-statics! "LocalDateTime" + (list (cons "ofInstant" (lambda (inst zone) (mk-local (ms-of inst)))) + (cons "now" (lambda () (mk-local (now-ms)))))) +(let ((locale-ctor (lambda (id . _) (make-jhost "locale" (vector (if (string? id) id (jolt-str-render-one id))))))) + (register-class-ctor! "Locale" locale-ctor) + (register-class-ctor! "java.util.Locale" locale-ctor)) +(register-class-statics! "Locale" + (list (cons "getDefault" (lambda () (make-jhost "locale" (vector "default")))) + (cons "ENGLISH" (make-jhost "locale" (vector "en"))) + (cons "US" (make-jhost "locale" (vector "en-US"))) + (cons "ROOT" (make-jhost "locale" (vector "root"))))) + +;; java.util.Date: #inst's class. (Date. ms) and the no-arg now form -> a jinst, +;; so .getTime / inst? / instance? Date work; ctor + .getTime mirror host_io.janet. +(register-class-ctor! "Date" (lambda args (make-jinst (if (null? args) (now-ms) (exact->inexact (car args)))))) +(register-class-ctor! "java.util.Date" (lambda args (make-jinst (if (null? args) (now-ms) (exact->inexact (car args)))))) + +;; a jinst's java.util.Date method surface (record-method-dispatch arm). +(define %it-rmd record-method-dispatch) +(set! record-method-dispatch + (lambda (obj method-name rest-args) + (cond + ((jinst? obj) + (cond ((string=? method-name "getTime") (jinst-ms obj)) + ((string=? method-name "toInstant") (mk-instant (jinst-ms obj))) + ((string=? method-name "toString") (inst-rfc3339 obj)) + (else (error #f (string-append "No method " method-name " on Date"))))) + (else (%it-rmd obj method-name rest-args))))) diff --git a/host/chez/jolt-chez.janet b/host/chez/jolt-chez.janet index 4977fe6..35cc199 100644 --- a/host/chez/jolt-chez.janet +++ b/host/chez/jolt-chez.janet @@ -23,6 +23,7 @@ "host/chez/ns.ss" "host/chez/post-prelude.ss" "host/chez/natives-meta.ss" "host/chez/natives-str.ss" "host/chez/records.ss" "host/chez/host-class.ss" "host/chez/io.ss" + "host/chez/inst-time.ss" "host/chez/host-static.ss" "host/chez/dot-forms.ss" "src/jolt/clojure/string.clj" "src/jolt/clojure/walk.clj" "src/jolt/clojure/template.clj"] diff --git a/host/chez/natives-misc.ss b/host/chez/natives-misc.ss index 02fad88..e6b5af9 100644 --- a/host/chez/natives-misc.ss +++ b/host/chez/natives-misc.ss @@ -26,6 +26,10 @@ (loop (fx+ i 1))))))) (define (jolt-random-uuid) (make-juuid (random-uuid-str))) +;; #uuid literal -> a uuid value (emit.janet lowers the :uuid node to this). The +;; reader already validated the shape; lowercase for value equality. +(define (jolt-uuid-from-string s) (make-juuid (string-downcase s))) + ;; parse-uuid: validate the canonical shape (8-4-4-4-12 hex), lowercase, -> uuid; ;; nil if the string doesn't conform (Clojure parse-uuid), error on a non-string. (define (hex-char? c) (or (and (char>=? c #\0) (char<=? c #\9)) diff --git a/host/chez/rt.ss b/host/chez/rt.ss index baa824d..765a1fe 100644 --- a/host/chez/rt.ss +++ b/host/chez/rt.ss @@ -293,3 +293,9 @@ ;; arm wraps the fully-built record-method-dispatch and the str/type/instance-check ;; extensions sit over every prior shim. (load "host/chez/io.ss") + +;; #inst values + java.time formatting (jolt-at0a inc X): jinst (RFC3339 ms) + +;; DateTimeFormatter/Instant/ZoneId/LocalDateTime/FormatStyle/Locale/Date. Loads +;; LAST — it extends record-method-dispatch / jolt-get / jolt= / jolt-hash / +;; jolt-pr-str / jolt-type / instance-check and uses host-static.ss's registries. +(load "host/chez/inst-time.ss") diff --git a/jolt-core/jolt/analyzer.clj b/jolt-core/jolt/analyzer.clj index ec597ef..ac4f757 100644 --- a/jolt-core/jolt/analyzer.clj +++ b/jolt-core/jolt/analyzer.clj @@ -22,6 +22,7 @@ form-literal? form-elements form-vec-items form-map-pairs form-set-items form-special? compile-ns form-regex? form-regex-source + form-inst? form-inst-source form-uuid? form-uuid-source form-macro? form-expand-1 resolve-global form-sym-meta host-intern! form-syntax-quote-lower record-type? record-ctor-key form-position late-bind?]])) @@ -431,4 +432,9 @@ ;; (interpreter compiles via the seed PEG engine); the Chez back end emits a ;; jolt-regex value over the vendored irregex. (form-regex? form) {:op :regex :source (form-regex-source form)} + ;; #inst / #uuid literals -> :inst / :uuid IR leaves. Like :regex, the Janet + ;; back end punts (the interpreter's data-readers parse them); the Chez back + ;; end emits a runtime inst/uuid value (host/chez/inst-time.ss). + (form-inst? form) {:op :inst :source (form-inst-source form)} + (form-uuid? form) {:op :uuid :source (form-uuid-source form)} :else (uncompilable "unsupported form")))) diff --git a/src/jolt/backend.janet b/src/jolt/backend.janet index 63888d3..0b7b2fd 100644 --- a/src/jolt/backend.janet +++ b/src/jolt/backend.janet @@ -791,6 +791,11 @@ # regex literal: the back end doesn't compile patterns — punt to the # interpreter (the seed compiles #"…" to a Janet PEG). Chez emits jolt-regex. :regex (error "jolt/uncompilable: regex literal") + # #inst / #uuid literals: the back end doesn't construct them — punt to the + # interpreter (its data-readers parse the timestamp/uuid). Chez emits a + # runtime jolt-inst-from-string / jolt-uuid-from-string value. + :inst (error "jolt/uncompilable: inst literal") + :uuid (error "jolt/uncompilable: uuid literal") (error (string "backend: unhandled op " (node :op)))))) (defn emit-ir diff --git a/src/jolt/host_iface.janet b/src/jolt/host_iface.janet index f0cc374..42b0d2a 100644 --- a/src/jolt/host_iface.janet +++ b/src/jolt/host_iface.janet @@ -54,6 +54,17 @@ (and (struct? form) (= :jolt/tagged (form :jolt/type)) (= :regex (form :tag)))) (defn h-regex-source [form] (form :form)) +# #inst / #uuid literals read as tagged forms whose :tag keyword keeps the leading +# # (the data-readers convention; (keyword "#inst") = :#inst). The analyzer lowers +# them to :inst / :uuid IR leaves: the Chez back end emits a runtime inst/uuid +# value; the Janet back end punts to the interpreter (its data-readers parse them). +(defn h-inst? [form] + (and (struct? form) (= :jolt/tagged (form :jolt/type)) (= (keyword "#inst") (form :tag)))) +(defn h-inst-source [form] (form :form)) +(defn h-uuid? [form] + (and (struct? form) (= :jolt/tagged (form :jolt/type)) (= (keyword "#uuid") (form :tag)))) +(defn h-uuid-source [form] (form :form)) + (defn h-literal? [form] (or (nil? form) (boolean? form) (number? form) (string? form) (keyword? form) (h-char? form))) @@ -270,6 +281,8 @@ "form-list?" h-list? "form-vec?" h-vector? "form-map?" h-map? "form-set?" h-set? "form-char?" h-char? "form-literal?" h-literal? "form-regex?" h-regex? "form-regex-source" h-regex-source + "form-inst?" h-inst? "form-inst-source" h-inst-source + "form-uuid?" h-uuid? "form-uuid-source" h-uuid-source "form-elements" h-elements "form-vec-items" h-vector-items "form-map-pairs" h-map-pairs "form-set-items" h-set-items "form-special?" h-special? "compile-ns" h-current-ns "form-macro?" h-macro? diff --git a/test/chez/_insttime.janet b/test/chez/_insttime.janet new file mode 100644 index 0000000..fc02e55 --- /dev/null +++ b/test/chez/_insttime.janet @@ -0,0 +1,77 @@ +# jolt-at0a (inc X) — #inst / #uuid literals + java.time formatting on Chez. +# #inst lowers (analyzer :inst node) to a jinst value (RFC3339 ms, partial +# defaults + offsets); #uuid to a juuid; the java.time shim (DateTimeFormatter/ +# Instant/ZoneId/LocalDateTime/FormatStyle/Locale/Date) ports java_base.janet. +# Mirrors the :regex IR-leaf precedent: Janet back end punts to the interpreter's +# data-readers, Chez emits the runtime value (host/chez/inst-time.ss). +# Oracle = build/jolt. +# +# janet test/chez/_insttime.janet +(def jolt-bin (or (os/getenv "JOLT_BIN") "bin/jolt-chez")) + +(def cases + [# --- #inst reading + identity --- + ["(inst? #inst \"2020-01-01T00:00:00Z\")" "true"] + ["(inst-ms #inst \"1970-01-01T00:00:01Z\")" "1000"] + ["(inst-ms #inst \"1970-01-01T00:00:00.123Z\")" "123"] + ["(inst-ms #inst \"2020-01-01T00:00:00Z\")" "1577836800000"] + ["(inst-ms* #inst \"1970-01-01T00:00:00Z\")" "0"] + ["(some? #inst \"2020-01-01T00:00:00Z\")" "true"] + ["(str (type #inst \"2020-01-01T00:00:00Z\"))" ":jolt/inst"] + # --- partial timestamps + offsets (value equality by instant) --- + ["(= #inst \"2020\" #inst \"2020-01-01T00:00:00.000Z\")" "true"] + ["(= #inst \"2020-03\" #inst \"2020-03-01T00:00:00Z\")" "true"] + ["(= #inst \"2020-03-15\" #inst \"2020-03-15T00:00:00Z\")" "true"] + ["(= #inst \"2020-01-01T01:00:00+01:00\" #inst \"2020-01-01T00:00:00Z\")" "true"] + ["(= #inst \"2019-12-31T23:00:00-01:00\" #inst \"2020-01-01T00:00:00Z\")" "true"] + ["(= #inst \"2020-01-01T00:00:00Z\" #inst \"2020-01-01T00:00:01Z\")" "false"] + # --- map key + pr-str round trip --- + ["(get {#inst \"2020-01-01T00:00:00Z\" :v} #inst \"2020-01-01T00:00:00.000Z\")" ":v"] + ["(pr-str #inst \"2020-01-01T00:00:00Z\")" "#inst \"2020-01-01T00:00:00.000-00:00\""] + ["(str #inst \"2020-01-01T00:00:00Z\")" "2020-01-01T00:00:00.000-00:00"] + # --- #uuid --- + ["(uuid? #uuid \"550e8400-e29b-41d4-a716-446655440000\")" "true"] + ["(= #uuid \"550E8400-E29B-41D4-A716-446655440000\" #uuid \"550e8400-e29b-41d4-a716-446655440000\")" "true"] + ["(pr-str #uuid \"550e8400-e29b-41d4-a716-446655440000\")" "#uuid \"550e8400-e29b-41d4-a716-446655440000\""] + # --- java.util.Date / java.time.Instant interop --- + ["(.getTime #inst \"1970-01-01T00:00:00Z\")" "0"] + ["(instance? java.util.Date #inst \"2020-01-01T00:00:00Z\")" "true"] + ["(instance? java.sql.Timestamp #inst \"2020-01-01T00:00:00Z\")" "false"] + ["(.toEpochMilli (Instant/ofEpochMilli 1234))" "1234"] + ["(instance? java.time.Instant (Instant/ofEpochMilli 0))" "true"] + ["(> (.toEpochMilli (Instant/now)) 1500000000000)" "true"] + ["(instance? LocalDateTime (-> #inst \"2020-03-05T13:45:30Z\" (.toInstant) (.atZone (ZoneId/systemDefault)) (.toLocalDateTime)))" "true"] + # --- DateTimeFormatter pattern engine --- + ["(.format (DateTimeFormatter/ofPattern \"yyyy-MM-dd\") #inst \"2020-03-05T13:45:30Z\")" "2020-03-05"] + ["(boolean (re-matches #\"[A-Z][a-z]{2} \\d{1,2}, 2020 \\d{1,2}:\\d{2} [AP]M\" (.format (DateTimeFormatter/ofPattern \"MMM d, yyyy h:mm a\") #inst \"2020-03-05T13:45:30Z\")))" "true"] + ["(boolean (re-matches #\"\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\" (.format DateTimeFormatter/ISO_LOCAL_DATE_TIME #inst \"2020-03-05T13:45:30Z\")))" "true"] + ["(string? (.format (DateTimeFormatter/ofLocalizedDate FormatStyle/MEDIUM) #inst \"2020-03-05T13:45:30Z\"))" "true"] + ["(string? (.format (.withLocale (DateTimeFormatter/ofPattern \"yyyy\") (java.util.Locale. \"en\")) #inst \"2020-01-01T00:00:00Z\"))" "true"]]) + +(defn run-capture [bin expr] + (def proc (os/spawn [bin "-e" expr] :p {:out :pipe :err :pipe})) + (def out (ev/read (proc :out) 0x100000)) + (def err (ev/read (proc :err) 0x100000)) + (def code (os/proc-wait proc)) + (def lines (filter (fn [l] (not (empty? l))) + (string/split "\n" (string/trim (if out (string out) ""))))) + [code (if (empty? lines) "" (last lines)) (string/trim (if err (string err) ""))]) + +(var pass 0) +(def fails @[]) +(each [expr expected] cases + (def [ocode oracle _] (run-capture "build/jolt" expr)) + (def [code got err] (run-capture jolt-bin expr)) + (cond + (not= ocode 0) (array/push fails [expr (string "ORACLE FAILED exit " ocode)]) + (not= oracle expected) (array/push fails [expr (string "ORACLE MISMATCH want `" expected "` got `" oracle "`")]) + (not= code 0) (array/push fails [expr (string "exit " code "; err: " err)]) + (= got expected) (++ pass) + (array/push fails [expr (string "want `" expected "`, got `" got "`")]))) + +(printf "\n_insttime parity [%s]: %d/%d passed" jolt-bin pass (length cases)) +(when (> (length fails) 0) + (printf "%d FAIL(s):" (length fails)) + (each [e m] fails (printf " FAIL %s\n %s" e m))) +(flush) +(os/exit (if (empty? fails) 0 1)) diff --git a/test/chez/run-corpus-prelude.janet b/test/chez/run-corpus-prelude.janet index 6933814..4ead9b1 100644 --- a/test/chez/run-corpus-prelude.janet +++ b/test/chez/run-corpus-prelude.janet @@ -261,8 +261,16 @@ # (slurp/string/char[]/File), File .toURL/.toURI + a url jhost, slurp draining a # StringReader, char-array, and with-open's __close seam over jhost readers + plain # :close maps; all in host/chez/io.ss, no analyzer change) 2202. +# jolt-at0a inc X (#inst / #uuid literals + java.time formatting — the analyzer +# lowers a #inst/#uuid tagged form to a :inst/:uuid IR leaf (mirroring :regex): +# the Janet back end punts to the interpreter's data-readers, the Chez back end +# emits jolt-inst-from-string / jolt-uuid-from-string. host/chez/inst-time.ss is +# the jinst value (RFC3339 ms via Hinnant civil/days math, partial defaults + +# offsets, = / hash / pr-str / get-as-overlay-seam) plus the DateTimeFormatter +# pattern engine + Instant/ZoneId/LocalDateTime/FormatStyle/Locale/Date shims. +# This cleared the whole "unsupported form" emit-fail bucket) 2238. # Strided runs scale down. -(def base-floor (scan-number (or (os/getenv "JOLT_CHEZ_PRELUDE_FLOOR") "2202"))) +(def base-floor (scan-number (or (os/getenv "JOLT_CHEZ_PRELUDE_FLOOR") "2238"))) (def floor (if (os/getenv "JOLT_CORPUS_LIMIT") 0 base-floor)) (when (or (> (length diverged) 0) (< pass floor)) (printf "REGRESSION: pass %d < floor %d or %d new divergence(s)" pass floor (length diverged)))