Compare commits
8 Commits
loops/js
...
loops/ruby
| Author | SHA1 | Date | |
|---|---|---|---|
| f6dfc034d1 | |||
| fa3274c394 | |||
| 15eb133311 | |||
| 96019e9fe8 | |||
| 3316d402fd | |||
| fb72c4ab9c | |||
| e52c209c3d | |||
| 6a00df2609 |
251
lib/js/lexer.sx
251
lib/js/lexer.sx
@@ -29,16 +29,6 @@
|
|||||||
(and (>= c "a") (<= c "f"))
|
(and (>= c "a") (<= c "f"))
|
||||||
(and (>= c "A") (<= c "F")))))
|
(and (>= c "A") (<= c "F")))))
|
||||||
|
|
||||||
(define
|
|
||||||
js-hex-value
|
|
||||||
(fn
|
|
||||||
(c)
|
|
||||||
(cond
|
|
||||||
((and (>= c "0") (<= c "9")) (- (char-code c) 48))
|
|
||||||
((and (>= c "a") (<= c "f")) (- (char-code c) 87))
|
|
||||||
((and (>= c "A") (<= c "F")) (- (char-code c) 55))
|
|
||||||
(else 0))))
|
|
||||||
|
|
||||||
(define
|
(define
|
||||||
js-letter?
|
js-letter?
|
||||||
(fn (c) (or (and (>= c "a") (<= c "z")) (and (>= c "A") (<= c "Z")))))
|
(fn (c) (or (and (>= c "a") (<= c "z")) (and (>= c "A") (<= c "Z")))))
|
||||||
@@ -47,9 +37,9 @@
|
|||||||
|
|
||||||
(define js-ident-char? (fn (c) (or (js-ident-start? c) (js-digit? c))))
|
(define js-ident-char? (fn (c) (or (js-ident-start? c) (js-digit? c))))
|
||||||
|
|
||||||
;; ── Reserved words ────────────────────────────────────────────────
|
|
||||||
(define js-ws? (fn (c) (or (= c " ") (= c "\t") (= c "\n") (= c "\r"))))
|
(define js-ws? (fn (c) (or (= c " ") (= c "\t") (= c "\n") (= c "\r"))))
|
||||||
|
|
||||||
|
;; ── Reserved words ────────────────────────────────────────────────
|
||||||
(define
|
(define
|
||||||
js-keywords
|
js-keywords
|
||||||
(list
|
(list
|
||||||
@@ -96,18 +86,15 @@
|
|||||||
"await"
|
"await"
|
||||||
"of"))
|
"of"))
|
||||||
|
|
||||||
;; ── Main tokenizer ────────────────────────────────────────────────
|
|
||||||
(define js-keyword? (fn (word) (contains? js-keywords word)))
|
(define js-keyword? (fn (word) (contains? js-keywords word)))
|
||||||
|
|
||||||
|
;; ── Main tokenizer ────────────────────────────────────────────────
|
||||||
(define
|
(define
|
||||||
js-tokenize
|
js-tokenize
|
||||||
(fn
|
(fn
|
||||||
(src)
|
(src)
|
||||||
(let
|
(let
|
||||||
((tokens (list))
|
((tokens (list)) (pos 0) (src-len (len src)))
|
||||||
(pos 0)
|
|
||||||
(src-len (len src))
|
|
||||||
(nl-before false))
|
|
||||||
(define
|
(define
|
||||||
js-peek
|
js-peek
|
||||||
(fn
|
(fn
|
||||||
@@ -122,7 +109,11 @@
|
|||||||
(let
|
(let
|
||||||
((sl (len s)))
|
((sl (len s)))
|
||||||
(and (<= (+ pos sl) src-len) (= (slice src pos (+ pos sl)) s)))))
|
(and (<= (+ pos sl) src-len) (= (slice src pos (+ pos sl)) s)))))
|
||||||
(define js-emit! (fn (type value start) (append! tokens {:nl nl-before :type type :value value :pos start})))
|
(define
|
||||||
|
js-emit!
|
||||||
|
(fn
|
||||||
|
(type value start)
|
||||||
|
(append! tokens (js-make-token type value start))))
|
||||||
(define
|
(define
|
||||||
skip-line-comment!
|
skip-line-comment!
|
||||||
(fn
|
(fn
|
||||||
@@ -145,13 +136,7 @@
|
|||||||
()
|
()
|
||||||
(cond
|
(cond
|
||||||
((>= pos src-len) nil)
|
((>= pos src-len) nil)
|
||||||
((js-ws? (cur))
|
((js-ws? (cur)) (do (advance! 1) (skip-ws!)))
|
||||||
(do
|
|
||||||
(when
|
|
||||||
(or (= (cur) "\n") (= (cur) "\r"))
|
|
||||||
(set! nl-before true))
|
|
||||||
(advance! 1)
|
|
||||||
(skip-ws!)))
|
|
||||||
((and (= (cur) "/") (< (+ pos 1) src-len) (= (js-peek 1) "/"))
|
((and (= (cur) "/") (< (+ pos 1) src-len) (= (js-peek 1) "/"))
|
||||||
(do (advance! 2) (skip-line-comment!) (skip-ws!)))
|
(do (advance! 2) (skip-line-comment!) (skip-ws!)))
|
||||||
((and (= (cur) "/") (< (+ pos 1) src-len) (= (js-peek 1) "*"))
|
((and (= (cur) "/") (< (+ pos 1) src-len) (= (js-peek 1) "*"))
|
||||||
@@ -269,55 +254,11 @@
|
|||||||
((= ch "b") (append! chars "\\b"))
|
((= ch "b") (append! chars "\\b"))
|
||||||
((= ch "f") (append! chars "\\f"))
|
((= ch "f") (append! chars "\\f"))
|
||||||
((= ch "v") (append! chars "\\v"))
|
((= ch "v") (append! chars "\\v"))
|
||||||
((= ch "u")
|
|
||||||
(if
|
|
||||||
(and
|
|
||||||
(< (+ pos 4) src-len)
|
|
||||||
(js-hex-digit? (js-peek 1))
|
|
||||||
(js-hex-digit? (js-peek 2))
|
|
||||||
(js-hex-digit? (js-peek 3))
|
|
||||||
(js-hex-digit? (js-peek 4)))
|
|
||||||
(do
|
|
||||||
(append!
|
|
||||||
chars
|
|
||||||
(char-from-code
|
|
||||||
(+
|
|
||||||
(*
|
|
||||||
4096
|
|
||||||
(js-hex-value
|
|
||||||
(js-peek 1)))
|
|
||||||
(*
|
|
||||||
256
|
|
||||||
(js-hex-value
|
|
||||||
(js-peek 2)))
|
|
||||||
(*
|
|
||||||
16
|
|
||||||
(js-hex-value
|
|
||||||
(js-peek 3)))
|
|
||||||
(js-hex-value (js-peek 4)))))
|
|
||||||
(advance! 4))
|
|
||||||
(append! chars ch)))
|
|
||||||
((= ch "x")
|
|
||||||
(if
|
|
||||||
(and
|
|
||||||
(< (+ pos 2) src-len)
|
|
||||||
(js-hex-digit? (js-peek 1))
|
|
||||||
(js-hex-digit? (js-peek 2)))
|
|
||||||
(do
|
|
||||||
(append!
|
|
||||||
chars
|
|
||||||
(char-from-code
|
|
||||||
(+
|
|
||||||
(* 16 (js-hex-value (js-peek 1)))
|
|
||||||
(js-hex-value (js-peek 2)))))
|
|
||||||
(advance! 2))
|
|
||||||
(append! chars ch)))
|
|
||||||
(else (append! chars ch)))
|
(else (append! chars ch)))
|
||||||
(advance! 1))))
|
(advance! 1))))
|
||||||
(loop)))
|
(loop)))
|
||||||
((= (cur) quote-char) (advance! 1))
|
((= (cur) quote-char) (advance! 1))
|
||||||
(else
|
(else (do (append! chars (cur)) (advance! 1) (loop))))))
|
||||||
(do (append! chars (cur)) (advance! 1) (loop))))))
|
|
||||||
(loop)
|
(loop)
|
||||||
(join "" chars))))
|
(join "" chars))))
|
||||||
(define
|
(define
|
||||||
@@ -348,8 +289,7 @@
|
|||||||
()
|
()
|
||||||
(cond
|
(cond
|
||||||
((>= pos src-len) nil)
|
((>= pos src-len) nil)
|
||||||
((and (= (cur) "}") (= depth 1))
|
((and (= (cur) "}") (= depth 1)) (advance! 1))
|
||||||
(advance! 1))
|
|
||||||
((= (cur) "}")
|
((= (cur) "}")
|
||||||
(do
|
(do
|
||||||
(append! buf (cur))
|
(append! buf (cur))
|
||||||
@@ -385,9 +325,7 @@
|
|||||||
(advance! 1)))
|
(advance! 1)))
|
||||||
(sloop)))
|
(sloop)))
|
||||||
((= (cur) q)
|
((= (cur) q)
|
||||||
(do
|
(do (append! buf (cur)) (advance! 1)))
|
||||||
(append! buf (cur))
|
|
||||||
(advance! 1)))
|
|
||||||
(else
|
(else
|
||||||
(do
|
(do
|
||||||
(append! buf (cur))
|
(append! buf (cur))
|
||||||
@@ -396,10 +334,7 @@
|
|||||||
(sloop)
|
(sloop)
|
||||||
(expr-loop))))
|
(expr-loop))))
|
||||||
(else
|
(else
|
||||||
(do
|
(do (append! buf (cur)) (advance! 1) (expr-loop))))))
|
||||||
(append! buf (cur))
|
|
||||||
(advance! 1)
|
|
||||||
(expr-loop))))))
|
|
||||||
(expr-loop)
|
(expr-loop)
|
||||||
(join "" buf))))
|
(join "" buf))))
|
||||||
(define
|
(define
|
||||||
@@ -441,17 +376,14 @@
|
|||||||
(else (append! chars ch)))
|
(else (append! chars ch)))
|
||||||
(advance! 1))))
|
(advance! 1))))
|
||||||
(loop)))
|
(loop)))
|
||||||
(else
|
(else (do (append! chars (cur)) (advance! 1) (loop))))))
|
||||||
(do (append! chars (cur)) (advance! 1) (loop))))))
|
|
||||||
(loop)
|
(loop)
|
||||||
(flush-chars!)
|
(flush-chars!)
|
||||||
(if
|
(if
|
||||||
(= (len parts) 0)
|
(= (len parts) 0)
|
||||||
""
|
""
|
||||||
(if
|
(if
|
||||||
(and
|
(and (= (len parts) 1) (= (nth (nth parts 0) 0) "str"))
|
||||||
(= (len parts) 1)
|
|
||||||
(= (nth (nth parts 0) 0) "str"))
|
|
||||||
(nth (nth parts 0) 1)
|
(nth (nth parts 0) 1)
|
||||||
parts)))))
|
parts)))))
|
||||||
(define
|
(define
|
||||||
@@ -467,7 +399,7 @@
|
|||||||
((ty (dict-get tk "type")) (vv (dict-get tk "value")))
|
((ty (dict-get tk "type")) (vv (dict-get tk "value")))
|
||||||
(cond
|
(cond
|
||||||
((= ty "punct")
|
((= ty "punct")
|
||||||
(and (not (= vv ")")) (not (= vv "]")) (not (= vv "}"))))
|
(and (not (= vv ")")) (not (= vv "]"))))
|
||||||
((= ty "op") true)
|
((= ty "op") true)
|
||||||
((= ty "keyword")
|
((= ty "keyword")
|
||||||
(contains?
|
(contains?
|
||||||
@@ -521,13 +453,9 @@
|
|||||||
(append! buf (cur))
|
(append! buf (cur))
|
||||||
(advance! 1)
|
(advance! 1)
|
||||||
(body-loop)))
|
(body-loop)))
|
||||||
((and (= (cur) "/") (not in-class))
|
((and (= (cur) "/") (not in-class)) (advance! 1))
|
||||||
(advance! 1))
|
|
||||||
(else
|
(else
|
||||||
(begin
|
(begin (append! buf (cur)) (advance! 1) (body-loop))))))
|
||||||
(append! buf (cur))
|
|
||||||
(advance! 1)
|
|
||||||
(body-loop))))))
|
|
||||||
(body-loop)
|
(body-loop)
|
||||||
(let
|
(let
|
||||||
((flags-buf (list)))
|
((flags-buf (list)))
|
||||||
@@ -542,7 +470,7 @@
|
|||||||
(advance! 1)
|
(advance! 1)
|
||||||
(flags-loop)))))
|
(flags-loop)))))
|
||||||
(flags-loop)
|
(flags-loop)
|
||||||
{:flags (join "" flags-buf) :pattern (join "" buf)}))))
|
{:pattern (join "" buf) :flags (join "" flags-buf)}))))
|
||||||
(define
|
(define
|
||||||
try-op-4!
|
try-op-4!
|
||||||
(fn
|
(fn
|
||||||
@@ -582,113 +510,64 @@
|
|||||||
(fn
|
(fn
|
||||||
(start)
|
(start)
|
||||||
(cond
|
(cond
|
||||||
((at? "==")
|
((at? "==") (do (js-emit! "op" "==" start) (advance! 2) true))
|
||||||
(do (js-emit! "op" "==" start) (advance! 2) true))
|
((at? "!=") (do (js-emit! "op" "!=" start) (advance! 2) true))
|
||||||
((at? "!=")
|
((at? "<=") (do (js-emit! "op" "<=" start) (advance! 2) true))
|
||||||
(do (js-emit! "op" "!=" start) (advance! 2) true))
|
((at? ">=") (do (js-emit! "op" ">=" start) (advance! 2) true))
|
||||||
((at? "<=")
|
((at? "&&") (do (js-emit! "op" "&&" start) (advance! 2) true))
|
||||||
(do (js-emit! "op" "<=" start) (advance! 2) true))
|
((at? "||") (do (js-emit! "op" "||" start) (advance! 2) true))
|
||||||
((at? ">=")
|
((at? "??") (do (js-emit! "op" "??" start) (advance! 2) true))
|
||||||
(do (js-emit! "op" ">=" start) (advance! 2) true))
|
((at? "=>") (do (js-emit! "op" "=>" start) (advance! 2) true))
|
||||||
((at? "&&")
|
((at? "**") (do (js-emit! "op" "**" start) (advance! 2) true))
|
||||||
(do (js-emit! "op" "&&" start) (advance! 2) true))
|
((at? "<<") (do (js-emit! "op" "<<" start) (advance! 2) true))
|
||||||
((at? "||")
|
((at? ">>") (do (js-emit! "op" ">>" start) (advance! 2) true))
|
||||||
(do (js-emit! "op" "||" start) (advance! 2) true))
|
((at? "++") (do (js-emit! "op" "++" start) (advance! 2) true))
|
||||||
((at? "??")
|
((at? "--") (do (js-emit! "op" "--" start) (advance! 2) true))
|
||||||
(do (js-emit! "op" "??" start) (advance! 2) true))
|
((at? "+=") (do (js-emit! "op" "+=" start) (advance! 2) true))
|
||||||
((at? "=>")
|
((at? "-=") (do (js-emit! "op" "-=" start) (advance! 2) true))
|
||||||
(do (js-emit! "op" "=>" start) (advance! 2) true))
|
((at? "*=") (do (js-emit! "op" "*=" start) (advance! 2) true))
|
||||||
((at? "**")
|
((at? "/=") (do (js-emit! "op" "/=" start) (advance! 2) true))
|
||||||
(do (js-emit! "op" "**" start) (advance! 2) true))
|
((at? "%=") (do (js-emit! "op" "%=" start) (advance! 2) true))
|
||||||
((at? "<<")
|
((at? "&=") (do (js-emit! "op" "&=" start) (advance! 2) true))
|
||||||
(do (js-emit! "op" "<<" start) (advance! 2) true))
|
((at? "|=") (do (js-emit! "op" "|=" start) (advance! 2) true))
|
||||||
((at? ">>")
|
((at? "^=") (do (js-emit! "op" "^=" start) (advance! 2) true))
|
||||||
(do (js-emit! "op" ">>" start) (advance! 2) true))
|
((at? "?.") (do (js-emit! "op" "?." start) (advance! 2) true))
|
||||||
((at? "++")
|
|
||||||
(do (js-emit! "op" "++" start) (advance! 2) true))
|
|
||||||
((at? "--")
|
|
||||||
(do (js-emit! "op" "--" start) (advance! 2) true))
|
|
||||||
((at? "+=")
|
|
||||||
(do (js-emit! "op" "+=" start) (advance! 2) true))
|
|
||||||
((at? "-=")
|
|
||||||
(do (js-emit! "op" "-=" start) (advance! 2) true))
|
|
||||||
((at? "*=")
|
|
||||||
(do (js-emit! "op" "*=" start) (advance! 2) true))
|
|
||||||
((at? "/=")
|
|
||||||
(do (js-emit! "op" "/=" start) (advance! 2) true))
|
|
||||||
((at? "%=")
|
|
||||||
(do (js-emit! "op" "%=" start) (advance! 2) true))
|
|
||||||
((at? "&=")
|
|
||||||
(do (js-emit! "op" "&=" start) (advance! 2) true))
|
|
||||||
((at? "|=")
|
|
||||||
(do (js-emit! "op" "|=" start) (advance! 2) true))
|
|
||||||
((at? "^=")
|
|
||||||
(do (js-emit! "op" "^=" start) (advance! 2) true))
|
|
||||||
((at? "?.")
|
|
||||||
(do (js-emit! "op" "?." start) (advance! 2) true))
|
|
||||||
(else false))))
|
(else false))))
|
||||||
(define
|
(define
|
||||||
emit-one-op!
|
emit-one-op!
|
||||||
(fn
|
(fn
|
||||||
(ch start)
|
(ch start)
|
||||||
(cond
|
(cond
|
||||||
((= ch "(")
|
((= ch "(") (do (js-emit! "punct" "(" start) (advance! 1)))
|
||||||
(do (js-emit! "punct" "(" start) (advance! 1)))
|
((= ch ")") (do (js-emit! "punct" ")" start) (advance! 1)))
|
||||||
((= ch ")")
|
((= ch "[") (do (js-emit! "punct" "[" start) (advance! 1)))
|
||||||
(do (js-emit! "punct" ")" start) (advance! 1)))
|
((= ch "]") (do (js-emit! "punct" "]" start) (advance! 1)))
|
||||||
((= ch "[")
|
((= ch "{") (do (js-emit! "punct" "{" start) (advance! 1)))
|
||||||
(do (js-emit! "punct" "[" start) (advance! 1)))
|
((= ch "}") (do (js-emit! "punct" "}" start) (advance! 1)))
|
||||||
((= ch "]")
|
((= ch ",") (do (js-emit! "punct" "," start) (advance! 1)))
|
||||||
(do (js-emit! "punct" "]" start) (advance! 1)))
|
((= ch ";") (do (js-emit! "punct" ";" start) (advance! 1)))
|
||||||
((= ch "{")
|
((= ch ":") (do (js-emit! "punct" ":" start) (advance! 1)))
|
||||||
(do (js-emit! "punct" "{" start) (advance! 1)))
|
((= ch ".") (do (js-emit! "punct" "." start) (advance! 1)))
|
||||||
((= ch "}")
|
((= ch "?") (do (js-emit! "op" "?" start) (advance! 1)))
|
||||||
(do (js-emit! "punct" "}" start) (advance! 1)))
|
((= ch "+") (do (js-emit! "op" "+" start) (advance! 1)))
|
||||||
((= ch ",")
|
((= ch "-") (do (js-emit! "op" "-" start) (advance! 1)))
|
||||||
(do (js-emit! "punct" "," start) (advance! 1)))
|
((= ch "*") (do (js-emit! "op" "*" start) (advance! 1)))
|
||||||
((= ch ";")
|
((= ch "/") (do (js-emit! "op" "/" start) (advance! 1)))
|
||||||
(do (js-emit! "punct" ";" start) (advance! 1)))
|
((= ch "%") (do (js-emit! "op" "%" start) (advance! 1)))
|
||||||
((= ch ":")
|
((= ch "=") (do (js-emit! "op" "=" start) (advance! 1)))
|
||||||
(do (js-emit! "punct" ":" start) (advance! 1)))
|
((= ch "<") (do (js-emit! "op" "<" start) (advance! 1)))
|
||||||
((= ch ".")
|
((= ch ">") (do (js-emit! "op" ">" start) (advance! 1)))
|
||||||
(do (js-emit! "punct" "." start) (advance! 1)))
|
((= ch "!") (do (js-emit! "op" "!" start) (advance! 1)))
|
||||||
((= ch "?")
|
((= ch "&") (do (js-emit! "op" "&" start) (advance! 1)))
|
||||||
(do (js-emit! "op" "?" start) (advance! 1)))
|
((= ch "|") (do (js-emit! "op" "|" start) (advance! 1)))
|
||||||
((= ch "+")
|
((= ch "^") (do (js-emit! "op" "^" start) (advance! 1)))
|
||||||
(do (js-emit! "op" "+" start) (advance! 1)))
|
((= ch "~") (do (js-emit! "op" "~" start) (advance! 1)))
|
||||||
((= ch "-")
|
|
||||||
(do (js-emit! "op" "-" start) (advance! 1)))
|
|
||||||
((= ch "*")
|
|
||||||
(do (js-emit! "op" "*" start) (advance! 1)))
|
|
||||||
((= ch "/")
|
|
||||||
(do (js-emit! "op" "/" start) (advance! 1)))
|
|
||||||
((= ch "%")
|
|
||||||
(do (js-emit! "op" "%" start) (advance! 1)))
|
|
||||||
((= ch "=")
|
|
||||||
(do (js-emit! "op" "=" start) (advance! 1)))
|
|
||||||
((= ch "<")
|
|
||||||
(do (js-emit! "op" "<" start) (advance! 1)))
|
|
||||||
((= ch ">")
|
|
||||||
(do (js-emit! "op" ">" start) (advance! 1)))
|
|
||||||
((= ch "!")
|
|
||||||
(do (js-emit! "op" "!" start) (advance! 1)))
|
|
||||||
((= ch "&")
|
|
||||||
(do (js-emit! "op" "&" start) (advance! 1)))
|
|
||||||
((= ch "|")
|
|
||||||
(do (js-emit! "op" "|" start) (advance! 1)))
|
|
||||||
((= ch "^")
|
|
||||||
(do (js-emit! "op" "^" start) (advance! 1)))
|
|
||||||
((= ch "~")
|
|
||||||
(do (js-emit! "op" "~" start) (advance! 1)))
|
|
||||||
((= ch "\\")
|
|
||||||
(error "Unexpected char '\\' in source"))
|
|
||||||
(else (advance! 1)))))
|
(else (advance! 1)))))
|
||||||
(define
|
(define
|
||||||
scan!
|
scan!
|
||||||
(fn
|
(fn
|
||||||
()
|
()
|
||||||
(do
|
(do
|
||||||
(set! nl-before false)
|
|
||||||
(skip-ws!)
|
(skip-ws!)
|
||||||
(when
|
(when
|
||||||
(< pos src-len)
|
(< pos src-len)
|
||||||
|
|||||||
253
lib/js/parser.sx
253
lib/js/parser.sx
@@ -153,32 +153,6 @@
|
|||||||
(do (jp-advance! st) (list (quote js-ident) "this")))
|
(do (jp-advance! st) (list (quote js-ident) "this")))
|
||||||
((and (= (get t :type) "keyword") (= (get t :value) "new"))
|
((and (= (get t :type) "keyword") (= (get t :value) "new"))
|
||||||
(do (jp-advance! st) (jp-parse-new-expr st)))
|
(do (jp-advance! st) (jp-parse-new-expr st)))
|
||||||
((and (= (get t :type) "keyword") (= (get t :value) "function"))
|
|
||||||
(do
|
|
||||||
(jp-advance! st)
|
|
||||||
(let
|
|
||||||
((nm
|
|
||||||
(if
|
|
||||||
(= (get (jp-peek st) :type) "ident")
|
|
||||||
(let ((n (get (jp-peek st) :value))) (do (jp-advance! st) n))
|
|
||||||
nil)))
|
|
||||||
(let
|
|
||||||
((params (jp-parse-param-list st)))
|
|
||||||
(let
|
|
||||||
((body (jp-parse-fn-body st)))
|
|
||||||
(list (quote js-funcexpr) nm params body))))))
|
|
||||||
((and (= (get t :type) "keyword") (= (get t :value) "true"))
|
|
||||||
(do (jp-advance! st) (list (quote js-bool) true)))
|
|
||||||
((and (= (get t :type) "keyword") (= (get t :value) "false"))
|
|
||||||
(do (jp-advance! st) (list (quote js-bool) false)))
|
|
||||||
((and (= (get t :type) "keyword") (= (get t :value) "null"))
|
|
||||||
(do (jp-advance! st) (list (quote js-null))))
|
|
||||||
((and (= (get t :type) "keyword") (= (get t :value) "undefined"))
|
|
||||||
(do (jp-advance! st) (list (quote js-undef))))
|
|
||||||
((= (get t :type) "number")
|
|
||||||
(do (jp-advance! st) (list (quote js-num) (get t :value))))
|
|
||||||
((= (get t :type) "string")
|
|
||||||
(do (jp-advance! st) (list (quote js-str) (get t :value))))
|
|
||||||
((and (= (get t :type) "punct") (= (get t :value) "("))
|
((and (= (get t :type) "punct") (= (get t :value) "("))
|
||||||
(jp-parse-paren-or-arrow st))
|
(jp-parse-paren-or-arrow st))
|
||||||
(else
|
(else
|
||||||
@@ -237,7 +211,7 @@
|
|||||||
(let
|
(let
|
||||||
((params (jp-parse-param-list st)))
|
((params (jp-parse-param-list st)))
|
||||||
(let
|
(let
|
||||||
((body (jp-parse-fn-body st)))
|
((body (jp-parse-block st)))
|
||||||
(list (quote js-funcexpr-async) nm params body))))))
|
(list (quote js-funcexpr-async) nm params body))))))
|
||||||
((= (get t :type) "ident")
|
((= (get t :type) "ident")
|
||||||
(do
|
(do
|
||||||
@@ -389,7 +363,7 @@
|
|||||||
(let
|
(let
|
||||||
((params (jp-parse-param-list st)))
|
((params (jp-parse-param-list st)))
|
||||||
(let
|
(let
|
||||||
((body (jp-parse-fn-body st)))
|
((body (jp-parse-block st)))
|
||||||
(list (quote js-funcexpr) nm params body))))))
|
(list (quote js-funcexpr) nm params body))))))
|
||||||
((= (get t :type) "ident")
|
((= (get t :type) "ident")
|
||||||
(do
|
(do
|
||||||
@@ -444,51 +418,16 @@
|
|||||||
(dict-set! st :idx saved)
|
(dict-set! st :idx saved)
|
||||||
(jp-advance! st)
|
(jp-advance! st)
|
||||||
(let
|
(let
|
||||||
((e (jp-parse-comma-seq st)))
|
((e (jp-parse-assignment st)))
|
||||||
(jp-expect! st "punct" ")")
|
(jp-expect! st "punct" ")")
|
||||||
(jp-paren-wrap e))))
|
e)))
|
||||||
(do
|
(do
|
||||||
(dict-set! st :idx saved)
|
(dict-set! st :idx saved)
|
||||||
(jp-advance! st)
|
(jp-advance! st)
|
||||||
(let
|
(let
|
||||||
((e (jp-parse-comma-seq st)))
|
((e (jp-parse-assignment st)))
|
||||||
(jp-expect! st "punct" ")")
|
(jp-expect! st "punct" ")")
|
||||||
(jp-paren-wrap e))))))))
|
e)))))))
|
||||||
|
|
||||||
(define
|
|
||||||
jp-paren-wrap
|
|
||||||
(fn
|
|
||||||
(e)
|
|
||||||
(cond
|
|
||||||
((and (list? e) (= (first e) (quote js-unop)))
|
|
||||||
(list (quote js-paren) e))
|
|
||||||
(else e))))
|
|
||||||
|
|
||||||
(define
|
|
||||||
jp-parse-comma-seq
|
|
||||||
(fn
|
|
||||||
(st)
|
|
||||||
(let
|
|
||||||
((first-expr (jp-parse-assignment st)))
|
|
||||||
(if
|
|
||||||
(jp-at? st "punct" ",")
|
|
||||||
(jp-parse-comma-seq-rest st (list first-expr))
|
|
||||||
first-expr))))
|
|
||||||
|
|
||||||
(define
|
|
||||||
jp-parse-comma-seq-rest
|
|
||||||
(fn
|
|
||||||
(st acc)
|
|
||||||
(do
|
|
||||||
(jp-advance! st)
|
|
||||||
(let
|
|
||||||
((next-expr (jp-parse-assignment st)))
|
|
||||||
(let
|
|
||||||
((acc2 (append acc (list next-expr))))
|
|
||||||
(if
|
|
||||||
(jp-at? st "punct" ",")
|
|
||||||
(jp-parse-comma-seq-rest st acc2)
|
|
||||||
(cons (quote js-comma) (list acc2))))))))
|
|
||||||
|
|
||||||
(define
|
(define
|
||||||
jp-collect-params
|
jp-collect-params
|
||||||
@@ -546,11 +485,6 @@
|
|||||||
(st elems)
|
(st elems)
|
||||||
(cond
|
(cond
|
||||||
((jp-at? st "punct" "]") nil)
|
((jp-at? st "punct" "]") nil)
|
||||||
((jp-at? st "punct" ",")
|
|
||||||
(begin
|
|
||||||
(append! elems (list (quote js-undef)))
|
|
||||||
(jp-advance! st)
|
|
||||||
(jp-array-loop st elems)))
|
|
||||||
(else
|
(else
|
||||||
(begin
|
(begin
|
||||||
(cond
|
(cond
|
||||||
@@ -624,20 +558,6 @@
|
|||||||
(jp-advance! st)
|
(jp-advance! st)
|
||||||
(jp-expect! st "punct" ":")
|
(jp-expect! st "punct" ":")
|
||||||
(append! kvs {:value (jp-parse-assignment st) :key (get t :value)})))
|
(append! kvs {:value (jp-parse-assignment st) :key (get t :value)})))
|
||||||
((and (= (get t :type) "punct") (= (get t :value) "["))
|
|
||||||
(do
|
|
||||||
(jp-advance! st)
|
|
||||||
(let
|
|
||||||
((key-expr (jp-parse-assignment st)))
|
|
||||||
(jp-expect! st "punct" "]")
|
|
||||||
(jp-expect! st "punct" ":")
|
|
||||||
(append!
|
|
||||||
kvs
|
|
||||||
{:value (jp-parse-assignment st) :computed-key key-expr :key ""}))))
|
|
||||||
((and (= (get t :type) "punct") (= (get t :value) "..."))
|
|
||||||
(do
|
|
||||||
(jp-advance! st)
|
|
||||||
(append! kvs {:spread (jp-parse-assignment st)})))
|
|
||||||
(else (error (str "Unexpected in object: " (get t :type))))))))
|
(else (error (str "Unexpected in object: " (get t :type))))))))
|
||||||
|
|
||||||
(define
|
(define
|
||||||
@@ -709,7 +629,7 @@
|
|||||||
st
|
st
|
||||||
(list (quote js-optchain-member) left (get t :value))))
|
(list (quote js-optchain-member) left (get t :value))))
|
||||||
(error "expected ident, [ or ( after ?.")))))))
|
(error "expected ident, [ or ( after ?.")))))))
|
||||||
((and (or (jp-at? st "op" "++") (jp-at? st "op" "--")) (not (jp-token-nl? st)))
|
((or (jp-at? st "op" "++") (jp-at? st "op" "--"))
|
||||||
(let
|
(let
|
||||||
((op (get (jp-peek st) :value)))
|
((op (get (jp-peek st) :value)))
|
||||||
(jp-advance! st)
|
(jp-advance! st)
|
||||||
@@ -762,12 +682,6 @@
|
|||||||
(cond
|
(cond
|
||||||
((< prec 0) left)
|
((< prec 0) left)
|
||||||
((< prec min-prec) left)
|
((< prec min-prec) left)
|
||||||
((and (= op "**") (list? left) (= (first left) (quote js-unop)))
|
|
||||||
(error
|
|
||||||
(str
|
|
||||||
"SyntaxError: Unary operator '"
|
|
||||||
(nth left 1)
|
|
||||||
"' used immediately before exponentiation expression")))
|
|
||||||
(else
|
(else
|
||||||
(do
|
(do
|
||||||
(jp-advance! st)
|
(jp-advance! st)
|
||||||
@@ -921,12 +835,6 @@
|
|||||||
jp-eat-semi
|
jp-eat-semi
|
||||||
(fn (st) (if (jp-at? st "punct" ";") (do (jp-advance! st) nil) nil)))
|
(fn (st) (if (jp-at? st "punct" ";") (do (jp-advance! st) nil) nil)))
|
||||||
|
|
||||||
(define
|
|
||||||
jp-token-nl?
|
|
||||||
(fn
|
|
||||||
(st)
|
|
||||||
(let ((tok (jp-peek st))) (if tok (= (get tok :nl) true) false))))
|
|
||||||
|
|
||||||
(define
|
(define
|
||||||
jp-parse-vardecl
|
jp-parse-vardecl
|
||||||
(fn
|
(fn
|
||||||
@@ -1144,63 +1052,15 @@
|
|||||||
((c (jp-parse-assignment st)))
|
((c (jp-parse-assignment st)))
|
||||||
(do
|
(do
|
||||||
(jp-expect! st "punct" ")")
|
(jp-expect! st "punct" ")")
|
||||||
(jp-disallow-decl-stmt! st "if")
|
|
||||||
(let
|
(let
|
||||||
((t (jp-parse-stmt st)))
|
((t (jp-parse-stmt st)))
|
||||||
(if
|
(if
|
||||||
(jp-at? st "keyword" "else")
|
(jp-at? st "keyword" "else")
|
||||||
(do
|
(do
|
||||||
(jp-advance! st)
|
(jp-advance! st)
|
||||||
(jp-disallow-decl-stmt! st "else")
|
|
||||||
(list (quote js-if) c t (jp-parse-stmt st)))
|
(list (quote js-if) c t (jp-parse-stmt st)))
|
||||||
(list (quote js-if) c t nil))))))))
|
(list (quote js-if) c t nil))))))))
|
||||||
|
|
||||||
(define
|
|
||||||
jp-disallow-decl-stmt!
|
|
||||||
(fn
|
|
||||||
(st context)
|
|
||||||
(let
|
|
||||||
((t (jp-peek st)))
|
|
||||||
(cond
|
|
||||||
((and (= (get t :type) "keyword")
|
|
||||||
(or (= (get t :value) "let")
|
|
||||||
(= (get t :value) "const")
|
|
||||||
(= (get t :value) "function")
|
|
||||||
(= (get t :value) "class")))
|
|
||||||
(cond
|
|
||||||
((and (= (get t :value) "let")
|
|
||||||
(or (= (get (jp-peek-at st 1) :type) "ident")
|
|
||||||
(and (= (get (jp-peek-at st 1) :type) "punct")
|
|
||||||
(or (= (get (jp-peek-at st 1) :value) "[")
|
|
||||||
(= (get (jp-peek-at st 1) :value) "{")))))
|
|
||||||
(error
|
|
||||||
(str
|
|
||||||
"SyntaxError: Lexical declaration cannot appear in single-statement context: "
|
|
||||||
context)))
|
|
||||||
((or (= (get t :value) "const")
|
|
||||||
(= (get t :value) "function")
|
|
||||||
(= (get t :value) "class"))
|
|
||||||
(error
|
|
||||||
(str
|
|
||||||
"SyntaxError: "
|
|
||||||
(get t :value)
|
|
||||||
" declaration cannot appear in single-statement context: "
|
|
||||||
context)))
|
|
||||||
(else nil)))
|
|
||||||
(else nil)))))
|
|
||||||
|
|
||||||
(define
|
|
||||||
jp-bump!
|
|
||||||
(fn
|
|
||||||
(st key)
|
|
||||||
(dict-set! st key (+ (get st key) 1))))
|
|
||||||
|
|
||||||
(define
|
|
||||||
jp-decr!
|
|
||||||
(fn
|
|
||||||
(st key)
|
|
||||||
(dict-set! st key (- (get st key) 1))))
|
|
||||||
|
|
||||||
(define
|
(define
|
||||||
jp-parse-while-stmt
|
jp-parse-while-stmt
|
||||||
(fn
|
(fn
|
||||||
@@ -1212,11 +1072,7 @@
|
|||||||
((c (jp-parse-assignment st)))
|
((c (jp-parse-assignment st)))
|
||||||
(do
|
(do
|
||||||
(jp-expect! st "punct" ")")
|
(jp-expect! st "punct" ")")
|
||||||
(jp-disallow-decl-stmt! st "while")
|
(let ((body (jp-parse-stmt st))) (list (quote js-while) c body)))))))
|
||||||
(jp-bump! st :loop-depth)
|
|
||||||
(let ((body (jp-parse-stmt st)))
|
|
||||||
(jp-decr! st :loop-depth)
|
|
||||||
(list (quote js-while) c body)))))))
|
|
||||||
|
|
||||||
(define
|
(define
|
||||||
jp-parse-do-while-stmt
|
jp-parse-do-while-stmt
|
||||||
@@ -1224,11 +1080,8 @@
|
|||||||
(st)
|
(st)
|
||||||
(do
|
(do
|
||||||
(jp-advance! st)
|
(jp-advance! st)
|
||||||
(jp-disallow-decl-stmt! st "do")
|
|
||||||
(jp-bump! st :loop-depth)
|
|
||||||
(let
|
(let
|
||||||
((body (jp-parse-stmt st)))
|
((body (jp-parse-stmt st)))
|
||||||
(jp-decr! st :loop-depth)
|
|
||||||
(do
|
(do
|
||||||
(if
|
(if
|
||||||
(jp-at? st "keyword" "while")
|
(jp-at? st "keyword" "while")
|
||||||
@@ -1273,11 +1126,8 @@
|
|||||||
(let
|
(let
|
||||||
((iter (jp-parse-assignment st)))
|
((iter (jp-parse-assignment st)))
|
||||||
(jp-expect! st "punct" ")")
|
(jp-expect! st "punct" ")")
|
||||||
(jp-disallow-decl-stmt! st "for-of/in")
|
|
||||||
(jp-bump! st :loop-depth)
|
|
||||||
(let
|
(let
|
||||||
((body (jp-parse-stmt st)))
|
((body (jp-parse-stmt st)))
|
||||||
(jp-decr! st :loop-depth)
|
|
||||||
(list (quote js-for-of-in) iter-kind ident iter body)))))))
|
(list (quote js-for-of-in) iter-kind ident iter body)))))))
|
||||||
(else
|
(else
|
||||||
(let
|
(let
|
||||||
@@ -1288,11 +1138,8 @@
|
|||||||
(let
|
(let
|
||||||
((step (if (jp-at? st "punct" ")") nil (jp-parse-assignment st))))
|
((step (if (jp-at? st "punct" ")") nil (jp-parse-assignment st))))
|
||||||
(jp-expect! st "punct" ")")
|
(jp-expect! st "punct" ")")
|
||||||
(jp-disallow-decl-stmt! st "for")
|
|
||||||
(jp-bump! st :loop-depth)
|
|
||||||
(let
|
(let
|
||||||
((body (jp-parse-stmt st)))
|
((body (jp-parse-stmt st)))
|
||||||
(jp-decr! st :loop-depth)
|
|
||||||
(list (quote js-for) init cond-ast step body)))))))))))
|
(list (quote js-for) init cond-ast step body)))))))))))
|
||||||
|
|
||||||
(define
|
(define
|
||||||
@@ -1315,14 +1162,10 @@
|
|||||||
(st)
|
(st)
|
||||||
(do
|
(do
|
||||||
(jp-advance! st)
|
(jp-advance! st)
|
||||||
(when
|
|
||||||
(= (get st :fn-depth) 0)
|
|
||||||
(error "SyntaxError: Illegal return statement"))
|
|
||||||
(if
|
(if
|
||||||
(or
|
(or
|
||||||
(jp-at? st "punct" ";")
|
(jp-at? st "punct" ";")
|
||||||
(jp-at? st "punct" "}")
|
(jp-at? st "punct" "}")
|
||||||
(jp-token-nl? st)
|
|
||||||
(jp-at? st "eof" nil))
|
(jp-at? st "eof" nil))
|
||||||
(do (jp-eat-semi st) (list (quote js-return) nil))
|
(do (jp-eat-semi st) (list (quote js-return) nil))
|
||||||
(let
|
(let
|
||||||
@@ -1345,7 +1188,7 @@
|
|||||||
(let
|
(let
|
||||||
((params (jp-parse-param-list st)))
|
((params (jp-parse-param-list st)))
|
||||||
(let
|
(let
|
||||||
((body (jp-parse-fn-body st)))
|
((body (jp-parse-block st)))
|
||||||
(list (quote js-funcdecl) nm params body))))))))
|
(list (quote js-funcdecl) nm params body))))))))
|
||||||
|
|
||||||
(define
|
(define
|
||||||
@@ -1364,7 +1207,7 @@
|
|||||||
(let
|
(let
|
||||||
((params (jp-parse-param-list st)))
|
((params (jp-parse-param-list st)))
|
||||||
(let
|
(let
|
||||||
((body (jp-parse-fn-body st)))
|
((body (jp-parse-block st)))
|
||||||
(list (quote js-funcdecl-async) nm params body))))))))
|
(list (quote js-funcdecl-async) nm params body))))))))
|
||||||
|
|
||||||
(define
|
(define
|
||||||
@@ -1413,7 +1256,7 @@
|
|||||||
(let
|
(let
|
||||||
((params (jp-parse-param-list st)))
|
((params (jp-parse-param-list st)))
|
||||||
(let
|
(let
|
||||||
((body (jp-parse-fn-body st)))
|
((body (jp-parse-block st)))
|
||||||
(list
|
(list
|
||||||
(quote js-method)
|
(quote js-method)
|
||||||
(if static? "static" "instance")
|
(if static? "static" "instance")
|
||||||
@@ -1441,11 +1284,9 @@
|
|||||||
((disc (jp-parse-assignment st)))
|
((disc (jp-parse-assignment st)))
|
||||||
(jp-expect! st "punct" ")")
|
(jp-expect! st "punct" ")")
|
||||||
(jp-expect! st "punct" "{")
|
(jp-expect! st "punct" "{")
|
||||||
(jp-bump! st :switch-depth)
|
|
||||||
(let
|
(let
|
||||||
((cases (list)))
|
((cases (list)))
|
||||||
(jp-parse-switch-cases st cases)
|
(jp-parse-switch-cases st cases)
|
||||||
(jp-decr! st :switch-depth)
|
|
||||||
(jp-expect! st "punct" "}")
|
(jp-expect! st "punct" "}")
|
||||||
(list (quote js-switch) disc cases)))))
|
(list (quote js-switch) disc cases)))))
|
||||||
|
|
||||||
@@ -1521,40 +1362,9 @@
|
|||||||
((jp-at? st "keyword" "for") (jp-parse-for-stmt st))
|
((jp-at? st "keyword" "for") (jp-parse-for-stmt st))
|
||||||
((jp-at? st "keyword" "return") (jp-parse-return-stmt st))
|
((jp-at? st "keyword" "return") (jp-parse-return-stmt st))
|
||||||
((jp-at? st "keyword" "break")
|
((jp-at? st "keyword" "break")
|
||||||
(do
|
(do (jp-advance! st) (jp-eat-semi st) (list (quote js-break))))
|
||||||
(jp-advance! st)
|
|
||||||
(cond
|
|
||||||
((= (get (jp-peek st) :type) "ident")
|
|
||||||
(do (jp-advance! st) (jp-eat-semi st) (list (quote js-break))))
|
|
||||||
(else
|
|
||||||
(do
|
|
||||||
(when
|
|
||||||
(and (= (get st :loop-depth) 0) (= (get st :switch-depth) 0))
|
|
||||||
(error "SyntaxError: Illegal break statement"))
|
|
||||||
(jp-eat-semi st)
|
|
||||||
(list (quote js-break)))))))
|
|
||||||
((jp-at? st "keyword" "continue")
|
((jp-at? st "keyword" "continue")
|
||||||
(do
|
(do (jp-advance! st) (jp-eat-semi st) (list (quote js-continue))))
|
||||||
(jp-advance! st)
|
|
||||||
(cond
|
|
||||||
((= (get (jp-peek st) :type) "ident")
|
|
||||||
(do (jp-advance! st) (jp-eat-semi st) (list (quote js-continue))))
|
|
||||||
(else
|
|
||||||
(do
|
|
||||||
(when
|
|
||||||
(= (get st :loop-depth) 0)
|
|
||||||
(error "SyntaxError: Illegal continue statement"))
|
|
||||||
(jp-eat-semi st)
|
|
||||||
(list (quote js-continue)))))))
|
|
||||||
((and
|
|
||||||
(= (get (jp-peek st) :type) "ident")
|
|
||||||
(= (get (jp-peek-at st 1) :type) "punct")
|
|
||||||
(= (get (jp-peek-at st 1) :value) ":"))
|
|
||||||
(do
|
|
||||||
(jp-advance! st)
|
|
||||||
(jp-advance! st)
|
|
||||||
(jp-disallow-decl-stmt! st "label")
|
|
||||||
(jp-parse-stmt st)))
|
|
||||||
((jp-at? st "keyword" "class") (jp-parse-class-decl st))
|
((jp-at? st "keyword" "class") (jp-parse-class-decl st))
|
||||||
((jp-at? st "keyword" "throw") (jp-parse-throw-stmt st))
|
((jp-at? st "keyword" "throw") (jp-parse-throw-stmt st))
|
||||||
((jp-at? st "keyword" "try") (jp-parse-try-stmt st))
|
((jp-at? st "keyword" "try") (jp-parse-try-stmt st))
|
||||||
@@ -1564,7 +1374,7 @@
|
|||||||
((jp-at? st "keyword" "switch") (jp-parse-switch-stmt st))
|
((jp-at? st "keyword" "switch") (jp-parse-switch-stmt st))
|
||||||
(else
|
(else
|
||||||
(let
|
(let
|
||||||
((e (jp-parse-comma-seq st)))
|
((e (jp-parse-assignment st)))
|
||||||
(do (jp-eat-semi st) (list (quote js-exprstmt) e)))))))
|
(do (jp-eat-semi st) (list (quote js-exprstmt) e)))))))
|
||||||
|
|
||||||
(define
|
(define
|
||||||
@@ -1590,33 +1400,10 @@
|
|||||||
jp-parse-arrow-body
|
jp-parse-arrow-body
|
||||||
(fn
|
(fn
|
||||||
(st)
|
(st)
|
||||||
(jp-bump! st :fn-depth)
|
(if
|
||||||
(let
|
(jp-at? st "punct" "{")
|
||||||
((saved-loop (get st :loop-depth)) (saved-switch (get st :switch-depth)))
|
(jp-parse-block st)
|
||||||
(dict-set! st :loop-depth 0)
|
(jp-parse-assignment st))))
|
||||||
(dict-set! st :switch-depth 0)
|
|
||||||
(let
|
|
||||||
((body (if (jp-at? st "punct" "{") (jp-parse-block st) (jp-parse-assignment st))))
|
|
||||||
(jp-decr! st :fn-depth)
|
|
||||||
(dict-set! st :loop-depth saved-loop)
|
|
||||||
(dict-set! st :switch-depth saved-switch)
|
|
||||||
body))))
|
|
||||||
|
|
||||||
(define
|
|
||||||
jp-parse-fn-body
|
|
||||||
(fn
|
|
||||||
(st)
|
|
||||||
(jp-bump! st :fn-depth)
|
|
||||||
(let
|
|
||||||
((saved-loop (get st :loop-depth)) (saved-switch (get st :switch-depth)))
|
|
||||||
(dict-set! st :loop-depth 0)
|
|
||||||
(dict-set! st :switch-depth 0)
|
|
||||||
(let
|
|
||||||
((body (jp-parse-block st)))
|
|
||||||
(jp-decr! st :fn-depth)
|
|
||||||
(dict-set! st :loop-depth saved-loop)
|
|
||||||
(dict-set! st :switch-depth saved-switch)
|
|
||||||
body))))
|
|
||||||
|
|
||||||
(define
|
(define
|
||||||
js-parse
|
js-parse
|
||||||
@@ -1627,7 +1414,7 @@
|
|||||||
(= (len tokens) 0)
|
(= (len tokens) 0)
|
||||||
(and (= (len tokens) 1) (= (get (nth tokens 0) :type) "eof")))
|
(and (= (len tokens) 1) (= (get (nth tokens 0) :type) "eof")))
|
||||||
(list (quote js-program) (list))
|
(list (quote js-program) (list))
|
||||||
(let ((st {:idx 0 :tokens tokens :arrow-candidate true :loop-depth 0 :switch-depth 0 :fn-depth 0})) (jp-parse-program st)))))
|
(let ((st {:idx 0 :tokens tokens :arrow-candidate true})) (jp-parse-program st)))))
|
||||||
|
|
||||||
(define
|
(define
|
||||||
js-parse-expr
|
js-parse-expr
|
||||||
@@ -1640,4 +1427,4 @@
|
|||||||
(= (len tokens) 0)
|
(= (len tokens) 0)
|
||||||
(and (= (len tokens) 1) (= (get (nth tokens 0) :type) "eof")))
|
(and (= (len tokens) 1) (= (get (nth tokens 0) :type) "eof")))
|
||||||
(list)
|
(list)
|
||||||
(let ((st {:idx 0 :tokens tokens :arrow-candidate true :loop-depth 0 :switch-depth 0 :fn-depth 0})) (jp-parse-assignment st))))))
|
(let ((st {:idx 0 :tokens tokens :arrow-candidate true})) (jp-parse-assignment st))))))
|
||||||
|
|||||||
4067
lib/js/runtime.sx
4067
lib/js/runtime.sx
File diff suppressed because it is too large
Load Diff
@@ -1323,25 +1323,6 @@ cat > "$TMPFILE" << 'EPOCHS'
|
|||||||
(epoch 3505)
|
(epoch 3505)
|
||||||
(eval "(js-eval \"var a = {length: 3, 0: 10, 1: 20, 2: 30}; var sum = 0; Array.prototype.forEach.call(a, function(x){sum += x;}); sum\")")
|
(eval "(js-eval \"var a = {length: 3, 0: 10, 1: 20, 2: 30}; var sum = 0; Array.prototype.forEach.call(a, function(x){sum += x;}); sum\")")
|
||||||
|
|
||||||
;; ── Phase 1.ASI: automatic semicolon insertion ─────────────────
|
|
||||||
(epoch 4200)
|
|
||||||
(eval "(js-eval \"function f() { return\n42\n} f()\")")
|
|
||||||
(epoch 4201)
|
|
||||||
(eval "(js-eval \"function g() { return 42 } g()\")")
|
|
||||||
(epoch 4202)
|
|
||||||
(eval "(let ((toks (js-tokenize \"a\nb\"))) (get (nth toks 1) :nl))")
|
|
||||||
(epoch 4203)
|
|
||||||
(eval "(let ((toks (js-tokenize \"a b\"))) (get (nth toks 1) :nl))")
|
|
||||||
|
|
||||||
(epoch 4300)
|
|
||||||
(eval "(js-eval \"var x = 5; x\")")
|
|
||||||
(epoch 4301)
|
|
||||||
(eval "(js-eval \"function f() { return x; var x = 42; } f()\")")
|
|
||||||
(epoch 4302)
|
|
||||||
(eval "(js-eval \"function f() { var y = 7; return y; } f()\")")
|
|
||||||
(epoch 4303)
|
|
||||||
(eval "(js-eval \"function f() { var z; z = 3; return z; } f()\")")
|
|
||||||
|
|
||||||
EPOCHS
|
EPOCHS
|
||||||
|
|
||||||
|
|
||||||
@@ -2061,17 +2042,6 @@ check 3503 "indexOf.call arrLike" '1'
|
|||||||
check 3504 "filter.call arrLike" '"2,3"'
|
check 3504 "filter.call arrLike" '"2,3"'
|
||||||
check 3505 "forEach.call arrLike sum" '60'
|
check 3505 "forEach.call arrLike sum" '60'
|
||||||
|
|
||||||
# ── Phase 1.ASI: automatic semicolon insertion ────────────────────
|
|
||||||
check 4200 "return+newline → undefined" '"js-undefined"'
|
|
||||||
check 4201 "return+space+val → val" '42'
|
|
||||||
check 4202 "nl-before flag set after newline" 'true'
|
|
||||||
check 4203 "nl-before flag false on same line" 'false'
|
|
||||||
|
|
||||||
check 4300 "var decl program-level" '5'
|
|
||||||
check 4301 "var hoisted before use → undef" '"js-undefined"'
|
|
||||||
check 4302 "var in function body" '7'
|
|
||||||
check 4303 "var then set in function" '3'
|
|
||||||
|
|
||||||
TOTAL=$((PASS + FAIL))
|
TOTAL=$((PASS + FAIL))
|
||||||
if [ $FAIL -eq 0 ]; then
|
if [ $FAIL -eq 0 ]; then
|
||||||
echo "✓ $PASS/$TOTAL JS-on-SX tests passed"
|
echo "✓ $PASS/$TOTAL JS-on-SX tests passed"
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ UPSTREAM = REPO / "lib" / "js" / "test262-upstream"
|
|||||||
TEST_ROOT = UPSTREAM / "test"
|
TEST_ROOT = UPSTREAM / "test"
|
||||||
HARNESS_DIR = UPSTREAM / "harness"
|
HARNESS_DIR = UPSTREAM / "harness"
|
||||||
|
|
||||||
DEFAULT_PER_TEST_TIMEOUT_S = 15.0
|
DEFAULT_PER_TEST_TIMEOUT_S = 5.0
|
||||||
DEFAULT_BATCH_TIMEOUT_S = 120
|
DEFAULT_BATCH_TIMEOUT_S = 120
|
||||||
|
|
||||||
# Cache dir for precomputed SX source of harness JS (one file per Python run).
|
# Cache dir for precomputed SX source of harness JS (one file per Python run).
|
||||||
@@ -134,9 +134,6 @@ var verifyProperty = function (obj, name, desc, opts) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
var verifyPrimordialProperty = verifyProperty;
|
var verifyPrimordialProperty = verifyProperty;
|
||||||
var verifyEqualTo = function (obj, name, value) {
|
|
||||||
assert.sameValue(obj[name], value, name + " equals");
|
|
||||||
};
|
|
||||||
var verifyNotEnumerable = function (o, n, v, w, x) { };
|
var verifyNotEnumerable = function (o, n, v, w, x) { };
|
||||||
var verifyNotWritable = function (o, n, v, w, x) { };
|
var verifyNotWritable = function (o, n, v, w, x) { };
|
||||||
var verifyNotConfigurable = function (o, n, v, w, x) { };
|
var verifyNotConfigurable = function (o, n, v, w, x) { };
|
||||||
@@ -149,50 +146,6 @@ var isConstructor = function (f) {
|
|||||||
// Best-effort: built-in functions and arrows aren't; declared `function` decls are.
|
// Best-effort: built-in functions and arrows aren't; declared `function` decls are.
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
// $DONE / asyncTest — async-flag tests call $DONE(err) to signal completion.
|
|
||||||
// Since we drain microtasks synchronously, $DONE is just a final-assertion sink.
|
|
||||||
var $DONE = function (err) {
|
|
||||||
if (err) { throw new Test262Error((err && err.message) || err); }
|
|
||||||
};
|
|
||||||
var asyncTest = function (testFunc) {
|
|
||||||
Promise.resolve(testFunc()).then(function () { $DONE(); }, function (e) { $DONE(e); });
|
|
||||||
};
|
|
||||||
// promiseHelper.js include — used by Promise.all/race tests for ordering checks.
|
|
||||||
var checkSequence = function (arr, message) {
|
|
||||||
for (var i = 0; i < arr.length; i = i + 1) {
|
|
||||||
if (arr[i] !== (i + 1)) {
|
|
||||||
throw new Test262Error((message || "Sequence") + " expected " + (i+1) + " at index " + i + " but got " + arr[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
var checkSettledPromises = function (settleds, expected, message) {
|
|
||||||
var msg = message ? message + " " : "";
|
|
||||||
if (settleds.length !== expected.length) {
|
|
||||||
throw new Test262Error(msg + "lengths differ: " + settleds.length + " vs " + expected.length);
|
|
||||||
}
|
|
||||||
for (var i = 0; i < settleds.length; i = i + 1) {
|
|
||||||
if (settleds[i].status !== expected[i].status) {
|
|
||||||
throw new Test262Error(msg + "status[" + i + "]: " + settleds[i].status + " vs " + expected[i].status);
|
|
||||||
}
|
|
||||||
if (expected[i].status === "fulfilled" && settleds[i].value !== expected[i].value) {
|
|
||||||
throw new Test262Error(msg + "value[" + i + "]: " + settleds[i].value + " vs " + expected[i].value);
|
|
||||||
}
|
|
||||||
if (expected[i].status === "rejected" && settleds[i].reason !== expected[i].reason) {
|
|
||||||
throw new Test262Error(msg + "reason[" + i + "]: " + settleds[i].reason + " vs " + expected[i].reason);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
// decimalToHexString.js include — used by URI/escape tests.
|
|
||||||
var decimalToHexString = function (n) {
|
|
||||||
var hex = "0123456789ABCDEF";
|
|
||||||
if (n < 0) { n = n + 65536; }
|
|
||||||
return hex[(n >> 12) & 15] + hex[(n >> 8) & 15] + hex[(n >> 4) & 15] + hex[n & 15];
|
|
||||||
};
|
|
||||||
var decimalToPercentHexString = function (n) {
|
|
||||||
var hex = "0123456789ABCDEF";
|
|
||||||
return "%" + hex[(n >> 4) & 15] + hex[n & 15];
|
|
||||||
};
|
|
||||||
// Trivial helper for tests that use Array.isArray-like functionality
|
// Trivial helper for tests that use Array.isArray-like functionality
|
||||||
// (many tests reach for it via compareArray)
|
// (many tests reach for it via compareArray)
|
||||||
"""
|
"""
|
||||||
@@ -405,8 +358,6 @@ def classify_negative_result(fm: Frontmatter, kind: str, payload: str):
|
|||||||
or ("expected" in low and "got" in low)
|
or ("expected" in low and "got" in low)
|
||||||
or "js-transpile-unop" in low
|
or "js-transpile-unop" in low
|
||||||
or "js-transpile-binop" in low
|
or "js-transpile-binop" in low
|
||||||
or "js-transpile-assign" in low
|
|
||||||
or "js-transpile" in low
|
|
||||||
or "js-compound-update" in low
|
or "js-compound-update" in low
|
||||||
or "parse" in low
|
or "parse" in low
|
||||||
):
|
):
|
||||||
@@ -1060,45 +1011,11 @@ def _worker_run(args):
|
|||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
_HARNESS_INCLUDE_CACHE: dict = {}
|
|
||||||
|
|
||||||
# Only inline these small harness files per-test. Large ones like propertyHelper.js
|
|
||||||
# multiply js-eval/JIT cost by ~5-10x and push tests over the per-test timeout.
|
|
||||||
_INLINE_INCLUDES = {"nans.js", "sta.js", "byteConversionValues.js", "compareArray.js"}
|
|
||||||
|
|
||||||
|
|
||||||
def _load_harness_include(name: str) -> str:
|
|
||||||
"""Read an upstream harness include file (e.g. nans.js).
|
|
||||||
Returns empty string if the file isn't present.
|
|
||||||
"""
|
|
||||||
if name in _HARNESS_INCLUDE_CACHE:
|
|
||||||
return _HARNESS_INCLUDE_CACHE[name]
|
|
||||||
path = HARNESS_DIR / name
|
|
||||||
try:
|
|
||||||
src = path.read_text()
|
|
||||||
except OSError:
|
|
||||||
src = ""
|
|
||||||
_HARNESS_INCLUDE_CACHE[name] = src
|
|
||||||
return src
|
|
||||||
|
|
||||||
|
|
||||||
def assemble_source(t):
|
def assemble_source(t):
|
||||||
"""Return JS source to feed to js-eval. Harness is preloaded, so we only
|
"""Return JS source to feed to js-eval. Harness is preloaded, so we only
|
||||||
append the test source (plus a small allowlist of per-test includes).
|
append the test source (plus negative-test prep if needed).
|
||||||
"""
|
"""
|
||||||
if not getattr(t.fm, "includes", None):
|
return t.src
|
||||||
return t.src
|
|
||||||
parts = []
|
|
||||||
for inc in t.fm.includes:
|
|
||||||
if inc not in _INLINE_INCLUDES:
|
|
||||||
continue
|
|
||||||
chunk = _load_harness_include(inc)
|
|
||||||
if chunk:
|
|
||||||
parts.append(chunk)
|
|
||||||
if not parts:
|
|
||||||
return t.src
|
|
||||||
parts.append(t.src)
|
|
||||||
return "\n".join(parts)
|
|
||||||
|
|
||||||
|
|
||||||
def aggregate(results):
|
def aggregate(results):
|
||||||
@@ -1276,7 +1193,7 @@ def main(argv):
|
|||||||
shards = [[] for _ in range(n_workers)]
|
shards = [[] for _ in range(n_workers)]
|
||||||
for i, t in enumerate(tests):
|
for i, t in enumerate(tests):
|
||||||
shards[i % n_workers].append(
|
shards[i % n_workers].append(
|
||||||
(t.rel, t.category, assemble_source(t), t.fm.negative_phase, t.fm.negative_type)
|
(t.rel, t.category, t.src, t.fm.negative_phase, t.fm.negative_type)
|
||||||
)
|
)
|
||||||
|
|
||||||
t_run_start = time.monotonic()
|
t_run_start = time.monotonic()
|
||||||
|
|||||||
@@ -1,53 +1,137 @@
|
|||||||
{
|
{
|
||||||
"totals": {
|
"totals": {
|
||||||
"pass": 4,
|
"pass": 162,
|
||||||
"fail": 10,
|
"fail": 128,
|
||||||
"skip": 16,
|
"skip": 1597,
|
||||||
"timeout": 0,
|
"timeout": 10,
|
||||||
"total": 30,
|
"total": 1897,
|
||||||
"runnable": 14,
|
"runnable": 300,
|
||||||
"pass_rate": 28.6
|
"pass_rate": 54.0
|
||||||
},
|
},
|
||||||
"categories": [
|
"categories": [
|
||||||
{
|
{
|
||||||
"category": "built-ins/Function",
|
"category": "built-ins/Math",
|
||||||
"total": 30,
|
"total": 327,
|
||||||
"pass": 4,
|
"pass": 43,
|
||||||
"fail": 10,
|
"fail": 56,
|
||||||
"skip": 16,
|
"skip": 227,
|
||||||
"timeout": 0,
|
"timeout": 1,
|
||||||
"pass_rate": 28.6,
|
"pass_rate": 43.0,
|
||||||
"top_failures": [
|
"top_failures": [
|
||||||
[
|
[
|
||||||
"SyntaxError (parse/unsupported syntax)",
|
"TypeError: not a function",
|
||||||
|
36
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"Test262Error (assertion failed)",
|
||||||
|
20
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"Timeout",
|
||||||
|
1
|
||||||
|
]
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"category": "built-ins/Number",
|
||||||
|
"total": 340,
|
||||||
|
"pass": 77,
|
||||||
|
"fail": 19,
|
||||||
|
"skip": 240,
|
||||||
|
"timeout": 4,
|
||||||
|
"pass_rate": 77.0,
|
||||||
|
"top_failures": [
|
||||||
|
[
|
||||||
|
"Test262Error (assertion failed)",
|
||||||
|
19
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"Timeout",
|
||||||
4
|
4
|
||||||
|
]
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"category": "built-ins/String",
|
||||||
|
"total": 1223,
|
||||||
|
"pass": 42,
|
||||||
|
"fail": 53,
|
||||||
|
"skip": 1123,
|
||||||
|
"timeout": 5,
|
||||||
|
"pass_rate": 42.0,
|
||||||
|
"top_failures": [
|
||||||
|
[
|
||||||
|
"Test262Error (assertion failed)",
|
||||||
|
44
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"Timeout",
|
||||||
|
5
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
"ReferenceError (undefined symbol)",
|
"ReferenceError (undefined symbol)",
|
||||||
3
|
2
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
"TypeError (other)",
|
"Unhandled: Not callable: {:__proto__ {:toLowerCase <lambda(&rest, args)",
|
||||||
3
|
2
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"Unhandled: Not callable: \\\\\\",
|
||||||
|
2
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"category": "built-ins/StringIteratorPrototype",
|
||||||
|
"total": 7,
|
||||||
|
"pass": 0,
|
||||||
|
"fail": 0,
|
||||||
|
"skip": 7,
|
||||||
|
"timeout": 0,
|
||||||
|
"pass_rate": 0.0,
|
||||||
|
"top_failures": []
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"top_failure_modes": [
|
"top_failure_modes": [
|
||||||
[
|
[
|
||||||
"SyntaxError (parse/unsupported syntax)",
|
"Test262Error (assertion failed)",
|
||||||
4
|
83
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"TypeError: not a function",
|
||||||
|
36
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"Timeout",
|
||||||
|
10
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
"ReferenceError (undefined symbol)",
|
"ReferenceError (undefined symbol)",
|
||||||
3
|
2
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
"TypeError (other)",
|
"Unhandled: Not callable: {:__proto__ {:toLowerCase <lambda(&rest, args)",
|
||||||
3
|
2
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"Unhandled: Not callable: \\\\\\",
|
||||||
|
2
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"SyntaxError (parse/unsupported syntax)",
|
||||||
|
1
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"Unhandled: Not callable: {:__proto__ {:valueOf <lambda()> :propertyIsEn",
|
||||||
|
1
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"Unhandled: js-transpile-binop: unsupported op: >>>\\",
|
||||||
|
1
|
||||||
]
|
]
|
||||||
],
|
],
|
||||||
"pinned_commit": "d5e73fc8d2c663554fb72e2380a8c2bc1a318a33",
|
"pinned_commit": "d5e73fc8d2c663554fb72e2380a8c2bc1a318a33",
|
||||||
"elapsed_seconds": 11.2,
|
"elapsed_seconds": 274.5,
|
||||||
"workers": 1
|
"workers": 1
|
||||||
}
|
}
|
||||||
@@ -1,26 +1,47 @@
|
|||||||
# test262 scoreboard
|
# test262 scoreboard
|
||||||
|
|
||||||
Pinned commit: `d5e73fc8d2c663554fb72e2380a8c2bc1a318a33`
|
Pinned commit: `d5e73fc8d2c663554fb72e2380a8c2bc1a318a33`
|
||||||
Wall time: 11.2s
|
Wall time: 274.5s
|
||||||
|
|
||||||
**Total:** 4/14 runnable passed (28.6%). Raw: pass=4 fail=10 skip=16 timeout=0 total=30.
|
**Total:** 162/300 runnable passed (54.0%). Raw: pass=162 fail=128 skip=1597 timeout=10 total=1897.
|
||||||
|
|
||||||
## Top failure modes
|
## Top failure modes
|
||||||
|
|
||||||
- **4x** SyntaxError (parse/unsupported syntax)
|
- **83x** Test262Error (assertion failed)
|
||||||
- **3x** ReferenceError (undefined symbol)
|
- **36x** TypeError: not a function
|
||||||
- **3x** TypeError (other)
|
- **10x** Timeout
|
||||||
|
- **2x** ReferenceError (undefined symbol)
|
||||||
|
- **2x** Unhandled: Not callable: {:__proto__ {:toLowerCase <lambda(&rest, args)
|
||||||
|
- **2x** Unhandled: Not callable: \\\
|
||||||
|
- **1x** SyntaxError (parse/unsupported syntax)
|
||||||
|
- **1x** Unhandled: Not callable: {:__proto__ {:valueOf <lambda()> :propertyIsEn
|
||||||
|
- **1x** Unhandled: js-transpile-binop: unsupported op: >>>\
|
||||||
|
|
||||||
## Categories (worst pass-rate first, min 10 runnable)
|
## Categories (worst pass-rate first, min 10 runnable)
|
||||||
|
|
||||||
| Category | Pass | Fail | Skip | Timeout | Total | Pass % |
|
| Category | Pass | Fail | Skip | Timeout | Total | Pass % |
|
||||||
|---|---:|---:|---:|---:|---:|---:|
|
|---|---:|---:|---:|---:|---:|---:|
|
||||||
| built-ins/Function | 4 | 10 | 16 | 0 | 30 | 28.6% |
|
| built-ins/String | 42 | 53 | 1123 | 5 | 1223 | 42.0% |
|
||||||
|
| built-ins/Math | 43 | 56 | 227 | 1 | 327 | 43.0% |
|
||||||
|
| built-ins/Number | 77 | 19 | 240 | 4 | 340 | 77.0% |
|
||||||
|
|
||||||
## Per-category top failures (min 10 runnable, worst first)
|
## Per-category top failures (min 10 runnable, worst first)
|
||||||
|
|
||||||
### built-ins/Function (4/14 — 28.6%)
|
### built-ins/String (42/100 — 42.0%)
|
||||||
|
|
||||||
- **4x** SyntaxError (parse/unsupported syntax)
|
- **44x** Test262Error (assertion failed)
|
||||||
- **3x** ReferenceError (undefined symbol)
|
- **5x** Timeout
|
||||||
- **3x** TypeError (other)
|
- **2x** ReferenceError (undefined symbol)
|
||||||
|
- **2x** Unhandled: Not callable: {:__proto__ {:toLowerCase <lambda(&rest, args)
|
||||||
|
- **2x** Unhandled: Not callable: \\\
|
||||||
|
|
||||||
|
### built-ins/Math (43/100 — 43.0%)
|
||||||
|
|
||||||
|
- **36x** TypeError: not a function
|
||||||
|
- **20x** Test262Error (assertion failed)
|
||||||
|
- **1x** Timeout
|
||||||
|
|
||||||
|
### built-ins/Number (77/100 — 77.0%)
|
||||||
|
|
||||||
|
- **19x** Test262Error (assertion failed)
|
||||||
|
- **4x** Timeout
|
||||||
|
|||||||
@@ -98,7 +98,6 @@
|
|||||||
(list (js-sym "js-regex-new") (nth ast 1) (nth ast 2)))
|
(list (js-sym "js-regex-new") (nth ast 1) (nth ast 2)))
|
||||||
((js-tag? ast "js-null") nil)
|
((js-tag? ast "js-null") nil)
|
||||||
((js-tag? ast "js-undef") (list (js-sym "quote") :js-undefined))
|
((js-tag? ast "js-undef") (list (js-sym "quote") :js-undefined))
|
||||||
((js-tag? ast "js-paren") (js-transpile (nth ast 1)))
|
|
||||||
((js-tag? ast "js-ident") (js-transpile-ident (nth ast 1)))
|
((js-tag? ast "js-ident") (js-transpile-ident (nth ast 1)))
|
||||||
((js-tag? ast "js-unop")
|
((js-tag? ast "js-unop")
|
||||||
(js-transpile-unop (nth ast 1) (nth ast 2)))
|
(js-transpile-unop (nth ast 1) (nth ast 2)))
|
||||||
@@ -117,8 +116,7 @@
|
|||||||
((js-tag? ast "js-arrow")
|
((js-tag? ast "js-arrow")
|
||||||
(js-transpile-arrow (nth ast 1) (nth ast 2)))
|
(js-transpile-arrow (nth ast 1) (nth ast 2)))
|
||||||
((js-tag? ast "js-program") (js-transpile-stmts (nth ast 1)))
|
((js-tag? ast "js-program") (js-transpile-stmts (nth ast 1)))
|
||||||
((js-tag? ast "js-block")
|
((js-tag? ast "js-block") (js-transpile-stmts (nth ast 1)))
|
||||||
(cons (js-sym "begin") (js-transpile-stmt-list (nth ast 1))))
|
|
||||||
((js-tag? ast "js-exprstmt") (js-transpile (nth ast 1)))
|
((js-tag? ast "js-exprstmt") (js-transpile (nth ast 1)))
|
||||||
((js-tag? ast "js-empty") nil)
|
((js-tag? ast "js-empty") nil)
|
||||||
((js-tag? ast "js-var")
|
((js-tag? ast "js-var")
|
||||||
@@ -166,8 +164,6 @@
|
|||||||
(js-transpile-new (nth ast 1) (nth ast 2)))
|
(js-transpile-new (nth ast 1) (nth ast 2)))
|
||||||
((js-tag? ast "js-class")
|
((js-tag? ast "js-class")
|
||||||
(js-transpile-class (nth ast 1) (nth ast 2) (nth ast 3)))
|
(js-transpile-class (nth ast 1) (nth ast 2) (nth ast 3)))
|
||||||
((js-tag? ast "js-comma")
|
|
||||||
(cons (js-sym "begin") (map js-transpile (nth ast 1))))
|
|
||||||
((js-tag? ast "js-throw") (js-transpile-throw (nth ast 1)))
|
((js-tag? ast "js-throw") (js-transpile-throw (nth ast 1)))
|
||||||
((js-tag? ast "js-try")
|
((js-tag? ast "js-try")
|
||||||
(js-transpile-try (nth ast 1) (nth ast 2) (nth ast 3)))
|
(js-transpile-try (nth ast 1) (nth ast 2) (nth ast 3)))
|
||||||
@@ -225,23 +221,7 @@
|
|||||||
(js-sym "js-delete-prop")
|
(js-sym "js-delete-prop")
|
||||||
(js-transpile (nth arg 1))
|
(js-transpile (nth arg 1))
|
||||||
(js-transpile (nth arg 2))))
|
(js-transpile (nth arg 2))))
|
||||||
((js-tag? arg "js-ident") false)
|
|
||||||
((js-tag? arg "js-paren") (js-transpile-unop op (nth arg 1)))
|
|
||||||
(else true)))
|
(else true)))
|
||||||
((and (= op "typeof") (js-tag? arg "js-ident"))
|
|
||||||
(let
|
|
||||||
((name (nth arg 1)))
|
|
||||||
(list
|
|
||||||
(js-sym "if")
|
|
||||||
(list
|
|
||||||
(js-sym "or")
|
|
||||||
(list
|
|
||||||
(js-sym "env-has?")
|
|
||||||
(list (js-sym "current-env"))
|
|
||||||
name)
|
|
||||||
(list (js-sym "dict-has?") (js-sym "js-global") name))
|
|
||||||
(list (js-sym "js-typeof") (js-transpile arg))
|
|
||||||
"undefined")))
|
|
||||||
(else
|
(else
|
||||||
(let
|
(let
|
||||||
((a (js-transpile arg)))
|
((a (js-transpile arg)))
|
||||||
@@ -251,8 +231,7 @@
|
|||||||
((= op "!") (list (js-sym "js-not") a))
|
((= op "!") (list (js-sym "js-not") a))
|
||||||
((= op "~") (list (js-sym "js-bitnot") a))
|
((= op "~") (list (js-sym "js-bitnot") a))
|
||||||
((= op "typeof") (list (js-sym "js-typeof") a))
|
((= op "typeof") (list (js-sym "js-typeof") a))
|
||||||
((= op "void")
|
((= op "void") (list (js-sym "quote") :js-undefined))
|
||||||
(list (js-sym "begin") a (list (js-sym "quote") :js-undefined)))
|
|
||||||
(else (error (str "js-transpile-unop: unsupported op: " op)))))))))
|
(else (error (str "js-transpile-unop: unsupported op: " op)))))))))
|
||||||
|
|
||||||
;; ── Array literal ─────────────────────────────────────────────────
|
;; ── Array literal ─────────────────────────────────────────────────
|
||||||
@@ -316,21 +295,6 @@
|
|||||||
(list (js-sym "js-undefined?") (js-sym "_a")))
|
(list (js-sym "js-undefined?") (js-sym "_a")))
|
||||||
(js-transpile r)
|
(js-transpile r)
|
||||||
(js-sym "_a"))))
|
(js-sym "_a"))))
|
||||||
((= op ">>>")
|
|
||||||
(list
|
|
||||||
(js-sym "js-unsigned-rshift")
|
|
||||||
(js-transpile l)
|
|
||||||
(js-transpile r)))
|
|
||||||
((= op "<<")
|
|
||||||
(list (js-sym "js-shl") (js-transpile l) (js-transpile r)))
|
|
||||||
((= op ">>")
|
|
||||||
(list (js-sym "js-shr") (js-transpile l) (js-transpile r)))
|
|
||||||
((= op "&")
|
|
||||||
(list (js-sym "js-bitand") (js-transpile l) (js-transpile r)))
|
|
||||||
((= op "|")
|
|
||||||
(list (js-sym "js-bitor") (js-transpile l) (js-transpile r)))
|
|
||||||
((= op "^")
|
|
||||||
(list (js-sym "js-bitxor") (js-transpile l) (js-transpile r)))
|
|
||||||
(else (error (str "js-transpile-binop: unsupported op: " op))))))
|
(else (error (str "js-transpile-binop: unsupported op: " op))))))
|
||||||
|
|
||||||
;; ── Object literal ────────────────────────────────────────────────
|
;; ── Object literal ────────────────────────────────────────────────
|
||||||
@@ -409,19 +373,7 @@
|
|||||||
(list
|
(list
|
||||||
(js-sym "js-new-call")
|
(js-sym "js-new-call")
|
||||||
(js-transpile callee)
|
(js-transpile callee)
|
||||||
(cond
|
(cons (js-sym "list") (map js-transpile args)))))
|
||||||
((js-has-spread? args)
|
|
||||||
(cons
|
|
||||||
(js-sym "js-array-spread-build")
|
|
||||||
(map
|
|
||||||
(fn
|
|
||||||
(e)
|
|
||||||
(if
|
|
||||||
(js-tag? e "js-spread")
|
|
||||||
(list (js-sym "list") "js-spread" (js-transpile (nth e 1)))
|
|
||||||
(list (js-sym "list") "js-value" (js-transpile e))))
|
|
||||||
args)))
|
|
||||||
(else (cons (js-sym "js-args") (map js-transpile args)))))))
|
|
||||||
|
|
||||||
(define
|
(define
|
||||||
js-transpile-array
|
js-transpile-array
|
||||||
@@ -439,7 +391,7 @@
|
|||||||
(list (js-sym "list") "js-spread" (js-transpile (nth e 1)))
|
(list (js-sym "list") "js-spread" (js-transpile (nth e 1)))
|
||||||
(list (js-sym "list") "js-value" (js-transpile e))))
|
(list (js-sym "list") "js-value" (js-transpile e))))
|
||||||
elts))
|
elts))
|
||||||
(cons (js-sym "js-make-list") (map js-transpile elts)))))
|
(cons (js-sym "list") (map js-transpile elts)))))
|
||||||
|
|
||||||
(define
|
(define
|
||||||
js-has-spread?
|
js-has-spread?
|
||||||
@@ -469,7 +421,7 @@
|
|||||||
(list (js-sym "list") "js-spread" (js-transpile (nth e 1)))
|
(list (js-sym "list") "js-spread" (js-transpile (nth e 1)))
|
||||||
(list (js-sym "list") "js-value" (js-transpile e))))
|
(list (js-sym "list") "js-value" (js-transpile e))))
|
||||||
args))
|
args))
|
||||||
(cons (js-sym "js-args") (map js-transpile args)))))
|
(cons (js-sym "list") (map js-transpile args)))))
|
||||||
|
|
||||||
;; Transpile a JS expression string to SX source text (for inspection
|
;; Transpile a JS expression string to SX source text (for inspection
|
||||||
;; in tests). Useful for asserting the exact emitted tree.
|
;; in tests). Useful for asserting the exact emitted tree.
|
||||||
@@ -479,28 +431,18 @@
|
|||||||
(entries)
|
(entries)
|
||||||
(list
|
(list
|
||||||
(js-sym "let")
|
(js-sym "let")
|
||||||
(list (list (js-sym "_obj") (list (js-sym "js-make-obj"))))
|
(list (list (js-sym "_obj") (list (js-sym "dict"))))
|
||||||
(cons
|
(cons
|
||||||
(js-sym "begin")
|
(js-sym "begin")
|
||||||
(append
|
(append
|
||||||
(map
|
(map
|
||||||
(fn
|
(fn
|
||||||
(entry)
|
(entry)
|
||||||
(cond
|
(list
|
||||||
((contains? (keys entry) :spread)
|
(js-sym "dict-set!")
|
||||||
(list
|
(js-sym "_obj")
|
||||||
(js-sym "js-obj-spread!")
|
(get entry :key)
|
||||||
(js-sym "_obj")
|
(js-transpile (get entry :value))))
|
||||||
(js-transpile (get entry :spread))))
|
|
||||||
(else
|
|
||||||
(list
|
|
||||||
(js-sym "js-obj-set!")
|
|
||||||
(js-sym "_obj")
|
|
||||||
(if
|
|
||||||
(contains? (keys entry) :computed-key)
|
|
||||||
(list (js-sym "js-to-string") (js-transpile (get entry :computed-key)))
|
|
||||||
(get entry :key))
|
|
||||||
(js-transpile (get entry :value))))))
|
|
||||||
entries)
|
entries)
|
||||||
(list (js-sym "_obj")))))))
|
(list (js-sym "_obj")))))))
|
||||||
|
|
||||||
@@ -544,95 +486,6 @@
|
|||||||
(append inits (list (js-transpile body))))))))
|
(append inits (list (js-transpile body))))))))
|
||||||
(list (js-sym "fn") param-syms body-tr))))
|
(list (js-sym "fn") param-syms body-tr))))
|
||||||
|
|
||||||
(define
|
|
||||||
js-collect-var-decl-names
|
|
||||||
(fn
|
|
||||||
(decls)
|
|
||||||
(cond
|
|
||||||
((empty? decls) (list))
|
|
||||||
((js-tag? (first decls) "js-vardecl")
|
|
||||||
(cons
|
|
||||||
(nth (first decls) 1)
|
|
||||||
(js-collect-var-decl-names (rest decls))))
|
|
||||||
(else (js-collect-var-decl-names (rest decls))))))
|
|
||||||
|
|
||||||
(define
|
|
||||||
js-collect-var-names
|
|
||||||
(fn
|
|
||||||
(stmts)
|
|
||||||
(cond
|
|
||||||
((empty? stmts) (list))
|
|
||||||
(else
|
|
||||||
(append
|
|
||||||
(js-collect-var-names-stmt (first stmts))
|
|
||||||
(js-collect-var-names (rest stmts)))))))
|
|
||||||
|
|
||||||
(define
|
|
||||||
js-collect-var-names-stmt
|
|
||||||
(fn
|
|
||||||
(stmt)
|
|
||||||
(cond
|
|
||||||
((not (list? stmt)) (list))
|
|
||||||
((and (js-tag? stmt "js-var") (= (nth stmt 1) "var"))
|
|
||||||
(js-collect-var-decl-names (nth stmt 2)))
|
|
||||||
((js-tag? stmt "js-block") (js-collect-var-names (nth stmt 1)))
|
|
||||||
((js-tag? stmt "js-for")
|
|
||||||
(append
|
|
||||||
(js-collect-var-names-stmt (nth stmt 1))
|
|
||||||
(js-collect-var-names-stmt (nth stmt 4))))
|
|
||||||
((js-tag? stmt "js-for-of-in")
|
|
||||||
(js-collect-var-names-stmt (nth stmt 4)))
|
|
||||||
((js-tag? stmt "js-while")
|
|
||||||
(js-collect-var-names-stmt (nth stmt 2)))
|
|
||||||
((js-tag? stmt "js-do-while")
|
|
||||||
(js-collect-var-names-stmt (nth stmt 1)))
|
|
||||||
((js-tag? stmt "js-if")
|
|
||||||
(append
|
|
||||||
(js-collect-var-names-stmt (nth stmt 2))
|
|
||||||
(if (>= (len stmt) 4) (js-collect-var-names-stmt (nth stmt 3)) (list))))
|
|
||||||
((js-tag? stmt "js-try")
|
|
||||||
(append
|
|
||||||
(js-collect-var-names-stmt (nth stmt 1))
|
|
||||||
(if (and (>= (len stmt) 3) (list? (nth stmt 2)))
|
|
||||||
(js-collect-var-names-stmt (nth (nth stmt 2) 2))
|
|
||||||
(list))
|
|
||||||
(if (>= (len stmt) 4) (js-collect-var-names-stmt (nth stmt 3)) (list))))
|
|
||||||
((js-tag? stmt "js-switch")
|
|
||||||
(js-collect-var-names-cases (nth stmt 2)))
|
|
||||||
(else (list)))))
|
|
||||||
|
|
||||||
(define
|
|
||||||
js-collect-var-names-cases
|
|
||||||
(fn
|
|
||||||
(cases)
|
|
||||||
(cond
|
|
||||||
((empty? cases) (list))
|
|
||||||
(else
|
|
||||||
(append
|
|
||||||
(js-collect-var-names (nth (first cases) 2))
|
|
||||||
(js-collect-var-names-cases (rest cases)))))))
|
|
||||||
|
|
||||||
(define
|
|
||||||
js-dedup-names
|
|
||||||
(fn
|
|
||||||
(names seen)
|
|
||||||
(cond
|
|
||||||
((empty? names) (list))
|
|
||||||
((some (fn (s) (= s (first names))) seen)
|
|
||||||
(js-dedup-names (rest names) seen))
|
|
||||||
(else
|
|
||||||
(cons
|
|
||||||
(first names)
|
|
||||||
(js-dedup-names (rest names) (cons (first names) seen)))))))
|
|
||||||
|
|
||||||
(define
|
|
||||||
js-var-hoist-forms
|
|
||||||
(fn
|
|
||||||
(names)
|
|
||||||
(map
|
|
||||||
(fn (name) (list (js-sym "define") (js-sym name) :js-undefined))
|
|
||||||
names)))
|
|
||||||
|
|
||||||
(define
|
(define
|
||||||
js-transpile-tpl
|
js-transpile-tpl
|
||||||
(fn
|
(fn
|
||||||
@@ -724,12 +577,6 @@
|
|||||||
(list (js-sym "js-undefined?") lhs-expr))
|
(list (js-sym "js-undefined?") lhs-expr))
|
||||||
rhs-expr
|
rhs-expr
|
||||||
lhs-expr))
|
lhs-expr))
|
||||||
((= op "<<=") (list (js-sym "js-shl") lhs-expr rhs-expr))
|
|
||||||
((= op ">>=") (list (js-sym "js-shr") lhs-expr rhs-expr))
|
|
||||||
((= op ">>>=") (list (js-sym "js-unsigned-rshift") lhs-expr rhs-expr))
|
|
||||||
((= op "&=") (list (js-sym "js-bitand") lhs-expr rhs-expr))
|
|
||||||
((= op "|=") (list (js-sym "js-bitor") lhs-expr rhs-expr))
|
|
||||||
((= op "^=") (list (js-sym "js-bitxor") lhs-expr rhs-expr))
|
|
||||||
(else (error (str "js-compound-update: unsupported op: " op))))))
|
(else (error (str "js-compound-update: unsupported op: " op))))))
|
||||||
|
|
||||||
(define
|
(define
|
||||||
@@ -959,7 +806,7 @@
|
|||||||
(if
|
(if
|
||||||
(= iter-kind "of")
|
(= iter-kind "of")
|
||||||
(list (js-sym "js-iterable-to-list") iter-sx)
|
(list (js-sym "js-iterable-to-list") iter-sx)
|
||||||
(list (js-sym "js-for-in-keys") iter-sx))))
|
(list (js-sym "js-object-keys") iter-sx))))
|
||||||
(list
|
(list
|
||||||
(js-sym "for-each")
|
(js-sym "for-each")
|
||||||
(list
|
(list
|
||||||
@@ -988,7 +835,7 @@
|
|||||||
(fn
|
(fn
|
||||||
(params)
|
(params)
|
||||||
(cond
|
(cond
|
||||||
((empty? params) (list (js-sym "&rest") (js-sym "__extra_args__")))
|
((empty? params) (list))
|
||||||
((and (list? (first params)) (js-tag? (first params) "js-rest"))
|
((and (list? (first params)) (js-tag? (first params) "js-rest"))
|
||||||
(list (js-sym "&rest") (js-sym (nth (first params) 1))))
|
(list (js-sym "&rest") (js-sym (nth (first params) 1))))
|
||||||
(else
|
(else
|
||||||
@@ -996,27 +843,6 @@
|
|||||||
(js-param-sym (first params))
|
(js-param-sym (first params))
|
||||||
(js-build-param-list (rest params)))))))
|
(js-build-param-list (rest params)))))))
|
||||||
|
|
||||||
(define
|
|
||||||
js-arguments-build-form
|
|
||||||
(fn
|
|
||||||
(params)
|
|
||||||
(list (js-sym "js-list-copy") (js-arguments-build-form-raw params))))
|
|
||||||
|
|
||||||
(define
|
|
||||||
js-arguments-build-form-raw
|
|
||||||
(fn
|
|
||||||
(params)
|
|
||||||
(cond
|
|
||||||
((empty? params)
|
|
||||||
(js-sym "__extra_args__"))
|
|
||||||
((and (list? (first params)) (js-tag? (first params) "js-rest"))
|
|
||||||
(js-sym (nth (first params) 1)))
|
|
||||||
(else
|
|
||||||
(list
|
|
||||||
(js-sym "cons")
|
|
||||||
(js-param-sym (first params))
|
|
||||||
(js-arguments-build-form-raw (rest params)))))))
|
|
||||||
|
|
||||||
(define
|
(define
|
||||||
js-param-init-forms
|
js-param-init-forms
|
||||||
(fn
|
(fn
|
||||||
@@ -1050,7 +876,7 @@
|
|||||||
(fn
|
(fn
|
||||||
(stmts)
|
(stmts)
|
||||||
(let
|
(let
|
||||||
((hoisted (append (js-var-hoist-forms (js-dedup-names (js-collect-var-names stmts) (list))) (js-collect-funcdecls stmts))))
|
((hoisted (js-collect-funcdecls stmts)))
|
||||||
(let
|
(let
|
||||||
((rest-stmts (js-transpile-stmt-list stmts)))
|
((rest-stmts (js-transpile-stmt-list stmts)))
|
||||||
(cons (js-sym "begin") (append hoisted rest-stmts))))))
|
(cons (js-sym "begin") (append hoisted rest-stmts))))))
|
||||||
@@ -1109,12 +935,12 @@
|
|||||||
|
|
||||||
(define
|
(define
|
||||||
js-transpile-var
|
js-transpile-var
|
||||||
(fn (kind decls) (cons (js-sym "begin") (js-vardecl-forms decls (= kind "var")))))
|
(fn (kind decls) (cons (js-sym "begin") (js-vardecl-forms decls))))
|
||||||
|
|
||||||
(define
|
(define
|
||||||
js-vardecl-forms
|
js-vardecl-forms
|
||||||
(fn
|
(fn
|
||||||
(decls is-var)
|
(decls)
|
||||||
(cond
|
(cond
|
||||||
((empty? decls) (list))
|
((empty? decls) (list))
|
||||||
(else
|
(else
|
||||||
@@ -1124,10 +950,10 @@
|
|||||||
((js-tag? d "js-vardecl")
|
((js-tag? d "js-vardecl")
|
||||||
(cons
|
(cons
|
||||||
(list
|
(list
|
||||||
(js-sym (if is-var "set!" "define"))
|
(js-sym "define")
|
||||||
(js-sym (nth d 1))
|
(js-sym (nth d 1))
|
||||||
(js-transpile (nth d 2)))
|
(js-transpile (nth d 2)))
|
||||||
(js-vardecl-forms (rest decls) is-var)))
|
(js-vardecl-forms (rest decls))))
|
||||||
((js-tag? d "js-vardecl-obj")
|
((js-tag? d "js-vardecl-obj")
|
||||||
(let
|
(let
|
||||||
((names (nth d 1))
|
((names (nth d 1))
|
||||||
@@ -1138,7 +964,7 @@
|
|||||||
(js-vardecl-obj-forms
|
(js-vardecl-obj-forms
|
||||||
names
|
names
|
||||||
tmp-sym
|
tmp-sym
|
||||||
(js-vardecl-forms (rest decls) is-var)))))
|
(js-vardecl-forms (rest decls))))))
|
||||||
((js-tag? d "js-vardecl-arr")
|
((js-tag? d "js-vardecl-arr")
|
||||||
(let
|
(let
|
||||||
((names (nth d 1))
|
((names (nth d 1))
|
||||||
@@ -1150,7 +976,7 @@
|
|||||||
names
|
names
|
||||||
tmp-sym
|
tmp-sym
|
||||||
0
|
0
|
||||||
(js-vardecl-forms (rest decls) is-var)))))
|
(js-vardecl-forms (rest decls))))))
|
||||||
(else (error "js-vardecl-forms: unexpected decl"))))))))
|
(else (error "js-vardecl-forms: unexpected decl"))))))))
|
||||||
|
|
||||||
(define
|
(define
|
||||||
@@ -1450,28 +1276,7 @@
|
|||||||
(let
|
(let
|
||||||
((body-tr (js-transpile body)))
|
((body-tr (js-transpile body)))
|
||||||
(let
|
(let
|
||||||
((with-catch
|
((with-catch (cond ((= catch-part nil) body-tr) (else (let ((pname (nth catch-part 0)) (cbody (nth catch-part 1))) (list (js-sym "guard") (list (if (= pname nil) (js-sym "__exc__") (js-sym pname)) (list (js-sym "else") (js-transpile cbody))) body-tr))))))
|
||||||
(cond
|
|
||||||
((= catch-part nil) body-tr)
|
|
||||||
(else
|
|
||||||
(let
|
|
||||||
((pname (nth catch-part 0))
|
|
||||||
(cbody (nth catch-part 1))
|
|
||||||
(raw-sym (js-sym "__raw_exc__")))
|
|
||||||
(list
|
|
||||||
(js-sym "guard")
|
|
||||||
(list
|
|
||||||
raw-sym
|
|
||||||
(list
|
|
||||||
(js-sym "else")
|
|
||||||
(cond
|
|
||||||
((= pname nil) (js-transpile cbody))
|
|
||||||
(else
|
|
||||||
(list
|
|
||||||
(js-sym "let")
|
|
||||||
(list (list (js-sym pname) (list (js-sym "js-wrap-exn") raw-sym)))
|
|
||||||
(js-transpile cbody))))))
|
|
||||||
body-tr))))))
|
|
||||||
(cond
|
(cond
|
||||||
((= finally-part nil) with-catch)
|
((= finally-part nil) with-catch)
|
||||||
(else
|
(else
|
||||||
@@ -1492,7 +1297,7 @@
|
|||||||
(if
|
(if
|
||||||
(and (list? body) (js-tag? body "js-block"))
|
(and (list? body) (js-tag? body "js-block"))
|
||||||
(let
|
(let
|
||||||
((hoisted (append (js-var-hoist-forms (js-dedup-names (js-collect-var-names (nth body 1)) (list))) (js-collect-funcdecls (nth body 1)))))
|
((hoisted (js-collect-funcdecls (nth body 1))))
|
||||||
(append hoisted (js-transpile-stmt-list (nth body 1))))
|
(append hoisted (js-transpile-stmt-list (nth body 1))))
|
||||||
(list (js-transpile body)))))
|
(list (js-transpile body)))))
|
||||||
(list
|
(list
|
||||||
@@ -1500,9 +1305,7 @@
|
|||||||
param-syms
|
param-syms
|
||||||
(list
|
(list
|
||||||
(js-sym "let")
|
(js-sym "let")
|
||||||
(list
|
(list (list (js-sym "this") (list (js-sym "js-this"))))
|
||||||
(list (js-sym "this") (list (js-sym "js-this")))
|
|
||||||
(list (js-sym "arguments") (js-arguments-build-form params)))
|
|
||||||
(list
|
(list
|
||||||
(js-sym "let")
|
(js-sym "let")
|
||||||
(list
|
(list
|
||||||
@@ -1513,7 +1316,7 @@
|
|||||||
(list
|
(list
|
||||||
(js-sym "fn")
|
(js-sym "fn")
|
||||||
(list (js-sym "__return__"))
|
(list (js-sym "__return__"))
|
||||||
(cons (js-sym "begin") (append (append inits body-forms) (list nil)))))))
|
(cons (js-sym "begin") (append inits body-forms))))))
|
||||||
(list
|
(list
|
||||||
(js-sym "if")
|
(js-sym "if")
|
||||||
(list (js-sym "=") (js-sym "__r__") nil)
|
(list (js-sym "=") (js-sym "__r__") nil)
|
||||||
@@ -1530,7 +1333,7 @@
|
|||||||
(if
|
(if
|
||||||
(and (list? body) (js-tag? body "js-block"))
|
(and (list? body) (js-tag? body "js-block"))
|
||||||
(let
|
(let
|
||||||
((hoisted (append (js-var-hoist-forms (js-dedup-names (js-collect-var-names (nth body 1)) (list))) (js-collect-funcdecls (nth body 1)))))
|
((hoisted (js-collect-funcdecls (nth body 1))))
|
||||||
(append hoisted (js-transpile-stmt-list (nth body 1))))
|
(append hoisted (js-transpile-stmt-list (nth body 1))))
|
||||||
(list (js-transpile body)))))
|
(list (js-transpile body)))))
|
||||||
(list
|
(list
|
||||||
@@ -1598,7 +1401,7 @@
|
|||||||
(fn
|
(fn
|
||||||
(src)
|
(src)
|
||||||
(let
|
(let
|
||||||
((result (eval-expr (list (quote let) (list (list (js-sym "this") (list (js-sym "js-this")))) (js-transpile (js-parse (js-tokenize src)))))))
|
((result (eval-expr (js-transpile (js-parse (js-tokenize src))))))
|
||||||
(js-drain-microtasks!)
|
(js-drain-microtasks!)
|
||||||
result)))
|
result)))
|
||||||
|
|
||||||
|
|||||||
984
lib/ruby/parser.sx
Normal file
984
lib/ruby/parser.sx
Normal file
@@ -0,0 +1,984 @@
|
|||||||
|
;; Ruby parser: token list → AST.
|
||||||
|
;; Entry: (rb-parse tokens) or (rb-parse-str src)
|
||||||
|
;; AST nodes: dicts with :type plus type-specific fields.
|
||||||
|
|
||||||
|
(define rb-parse
|
||||||
|
(fn (tokens)
|
||||||
|
(let ((pos 0) (tok-count (len tokens)))
|
||||||
|
|
||||||
|
(define rb-p-cur
|
||||||
|
(fn () (nth tokens pos)))
|
||||||
|
(define rb-p-peek
|
||||||
|
(fn (n)
|
||||||
|
(if (< (+ pos n) tok-count)
|
||||||
|
(nth tokens (+ pos n))
|
||||||
|
{:type "eof" :value nil :line 0 :col 0})))
|
||||||
|
(define rb-p-advance!
|
||||||
|
(fn () (set! pos (+ pos 1))))
|
||||||
|
(define rb-p-type
|
||||||
|
(fn () (get (rb-p-cur) :type)))
|
||||||
|
(define rb-p-val
|
||||||
|
(fn () (get (rb-p-cur) :value)))
|
||||||
|
(define rb-p-sep?
|
||||||
|
(fn () (or (= (rb-p-type) "newline") (= (rb-p-type) "semi"))))
|
||||||
|
(define rb-p-skip-seps!
|
||||||
|
(fn ()
|
||||||
|
(when (rb-p-sep?)
|
||||||
|
(do (rb-p-advance!) (rb-p-skip-seps!)))))
|
||||||
|
(define rb-p-skip-newlines!
|
||||||
|
(fn ()
|
||||||
|
(when (= (rb-p-type) "newline")
|
||||||
|
(do (rb-p-advance!) (rb-p-skip-newlines!)))))
|
||||||
|
(define rb-p-expect!
|
||||||
|
(fn (type)
|
||||||
|
(if (= (rb-p-type) type)
|
||||||
|
(let ((tok (rb-p-cur)))
|
||||||
|
(rb-p-advance!)
|
||||||
|
tok)
|
||||||
|
{:type "error"
|
||||||
|
:msg (join "" (list "expected " type " got " (rb-p-type)))})))
|
||||||
|
(define rb-p-expect-kw!
|
||||||
|
(fn (kw)
|
||||||
|
(when (and (= (rb-p-type) "keyword") (= (rb-p-val) kw))
|
||||||
|
(rb-p-advance!))))
|
||||||
|
|
||||||
|
;; Block: do |params| body end or { |params| body }
|
||||||
|
(define rb-p-parse-block-params
|
||||||
|
(fn ()
|
||||||
|
(if (= (rb-p-type) "pipe")
|
||||||
|
(do
|
||||||
|
(rb-p-advance!)
|
||||||
|
(let ((params (list)))
|
||||||
|
(define rb-p-bp-loop
|
||||||
|
(fn ()
|
||||||
|
(when (not (or (= (rb-p-type) "pipe") (= (rb-p-type) "eof")))
|
||||||
|
(do
|
||||||
|
(cond
|
||||||
|
((and (= (rb-p-type) "op") (= (rb-p-val) "**"))
|
||||||
|
(do
|
||||||
|
(rb-p-advance!)
|
||||||
|
(append! params {:type "param-kwrest" :name (rb-p-val)})
|
||||||
|
(rb-p-advance!)))
|
||||||
|
((and (= (rb-p-type) "op") (= (rb-p-val) "*"))
|
||||||
|
(do
|
||||||
|
(rb-p-advance!)
|
||||||
|
(if (= (rb-p-type) "ident")
|
||||||
|
(do
|
||||||
|
(append! params {:type "param-rest" :name (rb-p-val)})
|
||||||
|
(rb-p-advance!))
|
||||||
|
(append! params {:type "param-rest" :name nil}))))
|
||||||
|
(:else
|
||||||
|
(do
|
||||||
|
(append! params {:type "param-req" :name (rb-p-val)})
|
||||||
|
(rb-p-advance!))))
|
||||||
|
(when (= (rb-p-type) "comma") (rb-p-advance!))
|
||||||
|
(rb-p-bp-loop)))))
|
||||||
|
(rb-p-bp-loop)
|
||||||
|
(rb-p-expect! "pipe")
|
||||||
|
params))
|
||||||
|
(list))))
|
||||||
|
|
||||||
|
(define rb-p-parse-block
|
||||||
|
(fn ()
|
||||||
|
(cond
|
||||||
|
((and (= (rb-p-type) "keyword") (= (rb-p-val) "do"))
|
||||||
|
(do
|
||||||
|
(rb-p-advance!)
|
||||||
|
(let ((params (rb-p-parse-block-params)))
|
||||||
|
(rb-p-skip-seps!)
|
||||||
|
(let ((body (rb-p-parse-stmts (list "end"))))
|
||||||
|
(rb-p-expect-kw! "end")
|
||||||
|
{:type "block" :params params :body body}))))
|
||||||
|
((= (rb-p-type) "lbrace")
|
||||||
|
(do
|
||||||
|
(rb-p-advance!)
|
||||||
|
(let ((params (rb-p-parse-block-params)))
|
||||||
|
(rb-p-skip-seps!)
|
||||||
|
(let ((body (rb-p-parse-stmts (list "rbrace"))))
|
||||||
|
(rb-p-expect! "rbrace")
|
||||||
|
{:type "block" :params params :body body}))))
|
||||||
|
(:else nil))))
|
||||||
|
|
||||||
|
;; Method def params
|
||||||
|
(define rb-p-parse-def-params
|
||||||
|
(fn ()
|
||||||
|
(let ((params (list)))
|
||||||
|
(define rb-p-dp-one
|
||||||
|
(fn ()
|
||||||
|
(cond
|
||||||
|
((and (= (rb-p-type) "op") (= (rb-p-val) "&"))
|
||||||
|
(do
|
||||||
|
(rb-p-advance!)
|
||||||
|
(append! params {:type "param-block" :name (rb-p-val)})
|
||||||
|
(rb-p-advance!)))
|
||||||
|
((and (= (rb-p-type) "op") (= (rb-p-val) "**"))
|
||||||
|
(do
|
||||||
|
(rb-p-advance!)
|
||||||
|
(append! params {:type "param-kwrest" :name (rb-p-val)})
|
||||||
|
(rb-p-advance!)))
|
||||||
|
((and (= (rb-p-type) "op") (= (rb-p-val) "*"))
|
||||||
|
(do
|
||||||
|
(rb-p-advance!)
|
||||||
|
(if (= (rb-p-type) "ident")
|
||||||
|
(do
|
||||||
|
(append! params {:type "param-rest" :name (rb-p-val)})
|
||||||
|
(rb-p-advance!))
|
||||||
|
(append! params {:type "param-rest" :name nil}))))
|
||||||
|
((and (= (rb-p-type) "ident")
|
||||||
|
(= (get (rb-p-peek 1) :type) "colon"))
|
||||||
|
(do
|
||||||
|
(let ((name (rb-p-val)))
|
||||||
|
(rb-p-advance!)
|
||||||
|
(rb-p-advance!)
|
||||||
|
(if (or (rb-p-sep?) (= (rb-p-type) "comma")
|
||||||
|
(= (rb-p-type) "rparen") (= (rb-p-type) "eof"))
|
||||||
|
(append! params {:type "param-kw" :name name :default nil})
|
||||||
|
(append! params {:type "param-kw" :name name
|
||||||
|
:default (rb-p-parse-assign)})))))
|
||||||
|
(:else
|
||||||
|
(let ((name (rb-p-val)))
|
||||||
|
(rb-p-advance!)
|
||||||
|
(if (and (= (rb-p-type) "op") (= (rb-p-val) "="))
|
||||||
|
(do
|
||||||
|
(rb-p-advance!)
|
||||||
|
(append! params {:type "param-opt" :name name
|
||||||
|
:default (rb-p-parse-assign)}))
|
||||||
|
(append! params {:type "param-req" :name name})))))))
|
||||||
|
(define rb-p-dp-loop
|
||||||
|
(fn ()
|
||||||
|
(when (not (or (= (rb-p-type) "rparen") (rb-p-sep?)
|
||||||
|
(= (rb-p-type) "eof")))
|
||||||
|
(do
|
||||||
|
(rb-p-dp-one)
|
||||||
|
(when (= (rb-p-type) "comma")
|
||||||
|
(do (rb-p-advance!) (rb-p-skip-newlines!)))
|
||||||
|
(rb-p-dp-loop)))))
|
||||||
|
(rb-p-dp-loop)
|
||||||
|
params)))
|
||||||
|
|
||||||
|
;; def [recv.] name [(params)] body end
|
||||||
|
(define rb-p-parse-def
|
||||||
|
(fn ()
|
||||||
|
(rb-p-advance!)
|
||||||
|
(let ((recv nil) (name nil))
|
||||||
|
(cond
|
||||||
|
((and (= (rb-p-type) "keyword") (= (rb-p-val) "self")
|
||||||
|
(= (get (rb-p-peek 1) :type) "dot"))
|
||||||
|
(do
|
||||||
|
(set! recv {:type "self"})
|
||||||
|
(rb-p-advance!)
|
||||||
|
(rb-p-advance!)
|
||||||
|
(set! name (rb-p-val))
|
||||||
|
(rb-p-advance!)))
|
||||||
|
((and (= (rb-p-type) "ident")
|
||||||
|
(= (get (rb-p-peek 1) :type) "dot"))
|
||||||
|
(do
|
||||||
|
(set! recv {:type "lvar" :name (rb-p-val)})
|
||||||
|
(rb-p-advance!)
|
||||||
|
(rb-p-advance!)
|
||||||
|
(set! name (rb-p-val))
|
||||||
|
(rb-p-advance!)))
|
||||||
|
(:else
|
||||||
|
(do
|
||||||
|
(set! name (rb-p-val))
|
||||||
|
(rb-p-advance!))))
|
||||||
|
(let ((params (list)))
|
||||||
|
(cond
|
||||||
|
((= (rb-p-type) "lparen")
|
||||||
|
(do
|
||||||
|
(rb-p-advance!)
|
||||||
|
(rb-p-skip-newlines!)
|
||||||
|
(set! params (rb-p-parse-def-params))
|
||||||
|
(rb-p-expect! "rparen")))
|
||||||
|
((not (or (rb-p-sep?) (= (rb-p-type) "eof")))
|
||||||
|
(set! params (rb-p-parse-def-params)))
|
||||||
|
(:else nil))
|
||||||
|
(rb-p-skip-seps!)
|
||||||
|
(let ((body (rb-p-parse-stmts (list "end"))))
|
||||||
|
(rb-p-expect-kw! "end")
|
||||||
|
{:type "method-def" :recv recv :name name
|
||||||
|
:params params :body body})))))
|
||||||
|
|
||||||
|
;; class [<<obj | Name [<Super]] body end
|
||||||
|
(define rb-p-parse-class
|
||||||
|
(fn ()
|
||||||
|
(rb-p-advance!)
|
||||||
|
(if (and (= (rb-p-type) "op") (= (rb-p-val) "<<"))
|
||||||
|
(do
|
||||||
|
(rb-p-advance!)
|
||||||
|
(let ((obj (rb-p-parse-primary)))
|
||||||
|
(rb-p-skip-seps!)
|
||||||
|
(let ((body (rb-p-parse-stmts (list "end"))))
|
||||||
|
(rb-p-expect-kw! "end")
|
||||||
|
{:type "sclass" :obj obj :body body})))
|
||||||
|
(let ((name (rb-p-parse-const-path)))
|
||||||
|
(let ((super nil))
|
||||||
|
(when (and (= (rb-p-type) "op") (= (rb-p-val) "<"))
|
||||||
|
(do
|
||||||
|
(rb-p-advance!)
|
||||||
|
(set! super (rb-p-parse-const-path))))
|
||||||
|
(rb-p-skip-seps!)
|
||||||
|
(let ((body (rb-p-parse-stmts (list "end"))))
|
||||||
|
(rb-p-expect-kw! "end")
|
||||||
|
{:type "class-def" :name name :super super :body body}))))))
|
||||||
|
|
||||||
|
;; module Name body end
|
||||||
|
(define rb-p-parse-module
|
||||||
|
(fn ()
|
||||||
|
(rb-p-advance!)
|
||||||
|
(let ((name (rb-p-parse-const-path)))
|
||||||
|
(rb-p-skip-seps!)
|
||||||
|
(let ((body (rb-p-parse-stmts (list "end"))))
|
||||||
|
(rb-p-expect-kw! "end")
|
||||||
|
{:type "module-def" :name name :body body}))))
|
||||||
|
|
||||||
|
;; Const or Const::Const::...
|
||||||
|
(define rb-p-parse-const-path
|
||||||
|
(fn ()
|
||||||
|
(let ((node {:type "const" :name (rb-p-val)}))
|
||||||
|
(rb-p-advance!)
|
||||||
|
(define rb-p-cp-loop
|
||||||
|
(fn ()
|
||||||
|
(when (= (rb-p-type) "dcolon")
|
||||||
|
(do
|
||||||
|
(rb-p-advance!)
|
||||||
|
(let ((name (rb-p-val)))
|
||||||
|
(rb-p-advance!)
|
||||||
|
(set! node {:type "const-path" :left node :name name})
|
||||||
|
(rb-p-cp-loop))))))
|
||||||
|
(rb-p-cp-loop)
|
||||||
|
node)))
|
||||||
|
|
||||||
|
;; [e, *e, ...]
|
||||||
|
(define rb-p-parse-array
|
||||||
|
(fn ()
|
||||||
|
(rb-p-advance!)
|
||||||
|
(rb-p-skip-newlines!)
|
||||||
|
(let ((elems (list)))
|
||||||
|
(define rb-p-arr-loop
|
||||||
|
(fn ()
|
||||||
|
(when (not (or (= (rb-p-type) "rbracket") (= (rb-p-type) "eof")))
|
||||||
|
(do
|
||||||
|
(if (and (= (rb-p-type) "op") (= (rb-p-val) "*"))
|
||||||
|
(do
|
||||||
|
(rb-p-advance!)
|
||||||
|
(append! elems {:type "splat" :value (rb-p-parse-assign)}))
|
||||||
|
(append! elems (rb-p-parse-assign)))
|
||||||
|
(rb-p-skip-newlines!)
|
||||||
|
(when (= (rb-p-type) "comma")
|
||||||
|
(do (rb-p-advance!) (rb-p-skip-newlines!)))
|
||||||
|
(rb-p-arr-loop)))))
|
||||||
|
(rb-p-arr-loop)
|
||||||
|
(rb-p-expect! "rbracket")
|
||||||
|
{:type "array" :elems elems})))
|
||||||
|
|
||||||
|
;; {k: v, k => v, ...}
|
||||||
|
(define rb-p-parse-hash
|
||||||
|
(fn ()
|
||||||
|
(rb-p-advance!)
|
||||||
|
(rb-p-skip-newlines!)
|
||||||
|
(let ((pairs (list)))
|
||||||
|
(define rb-p-hash-loop
|
||||||
|
(fn ()
|
||||||
|
(when (not (or (= (rb-p-type) "rbrace") (= (rb-p-type) "eof")))
|
||||||
|
(do
|
||||||
|
(let ((key nil) (val nil) (style nil))
|
||||||
|
(cond
|
||||||
|
((and (or (= (rb-p-type) "ident") (= (rb-p-type) "const"))
|
||||||
|
(= (get (rb-p-peek 1) :type) "colon"))
|
||||||
|
(do
|
||||||
|
(set! key {:type "lit-sym" :value (rb-p-val)})
|
||||||
|
(set! style "colon")
|
||||||
|
(rb-p-advance!)
|
||||||
|
(rb-p-advance!)))
|
||||||
|
(:else
|
||||||
|
(do
|
||||||
|
(set! key (rb-p-parse-assign))
|
||||||
|
(set! style "rocket")
|
||||||
|
(when (and (= (rb-p-type) "op") (= (rb-p-val) "=>"))
|
||||||
|
(rb-p-advance!)))))
|
||||||
|
(rb-p-skip-newlines!)
|
||||||
|
(set! val (rb-p-parse-assign))
|
||||||
|
(append! pairs {:key key :val val :style style}))
|
||||||
|
(rb-p-skip-newlines!)
|
||||||
|
(when (= (rb-p-type) "comma")
|
||||||
|
(do (rb-p-advance!) (rb-p-skip-newlines!)))
|
||||||
|
(rb-p-hash-loop)))))
|
||||||
|
(rb-p-hash-loop)
|
||||||
|
(rb-p-expect! "rbrace")
|
||||||
|
{:type "hash" :pairs pairs})))
|
||||||
|
|
||||||
|
;; (a, *b, **c, &d)
|
||||||
|
(define rb-p-parse-args-parens
|
||||||
|
(fn ()
|
||||||
|
(rb-p-advance!)
|
||||||
|
(rb-p-skip-newlines!)
|
||||||
|
(let ((args (list)))
|
||||||
|
(define rb-p-ap-loop
|
||||||
|
(fn ()
|
||||||
|
(when (not (or (= (rb-p-type) "rparen") (= (rb-p-type) "eof")))
|
||||||
|
(do
|
||||||
|
(cond
|
||||||
|
((and (= (rb-p-type) "op") (= (rb-p-val) "**"))
|
||||||
|
(do (rb-p-advance!)
|
||||||
|
(append! args {:type "dsplat" :value (rb-p-parse-assign)})))
|
||||||
|
((and (= (rb-p-type) "op") (= (rb-p-val) "*"))
|
||||||
|
(do (rb-p-advance!)
|
||||||
|
(append! args {:type "splat" :value (rb-p-parse-assign)})))
|
||||||
|
((and (= (rb-p-type) "op") (= (rb-p-val) "&"))
|
||||||
|
(do (rb-p-advance!)
|
||||||
|
(append! args {:type "block-pass" :value (rb-p-parse-assign)})))
|
||||||
|
(:else (append! args (rb-p-parse-assign))))
|
||||||
|
(rb-p-skip-newlines!)
|
||||||
|
(when (= (rb-p-type) "comma")
|
||||||
|
(do (rb-p-advance!) (rb-p-skip-newlines!)))
|
||||||
|
(rb-p-ap-loop)))))
|
||||||
|
(rb-p-ap-loop)
|
||||||
|
(rb-p-expect! "rparen")
|
||||||
|
args)))
|
||||||
|
|
||||||
|
;; No-paren arg list up to sep/end-keyword
|
||||||
|
(define rb-p-parse-args-bare
|
||||||
|
(fn ()
|
||||||
|
(let ((args (list)) (going true))
|
||||||
|
(define rb-p-ab-loop
|
||||||
|
(fn ()
|
||||||
|
(when (and going
|
||||||
|
(not (rb-p-sep?))
|
||||||
|
(not (= (rb-p-type) "eof"))
|
||||||
|
(not (= (rb-p-type) "rparen"))
|
||||||
|
(not (= (rb-p-type) "rbracket"))
|
||||||
|
(not (= (rb-p-type) "rbrace"))
|
||||||
|
(not (and (= (rb-p-type) "keyword")
|
||||||
|
(contains? (list "end" "else" "elsif" "when"
|
||||||
|
"rescue" "ensure" "then" "do")
|
||||||
|
(rb-p-val)))))
|
||||||
|
(do
|
||||||
|
(cond
|
||||||
|
((and (= (rb-p-type) "op") (= (rb-p-val) "*"))
|
||||||
|
(do (rb-p-advance!)
|
||||||
|
(append! args {:type "splat" :value (rb-p-parse-assign)})))
|
||||||
|
((and (= (rb-p-type) "op") (= (rb-p-val) "**"))
|
||||||
|
(do (rb-p-advance!)
|
||||||
|
(append! args {:type "dsplat" :value (rb-p-parse-assign)})))
|
||||||
|
((and (= (rb-p-type) "op") (= (rb-p-val) "&"))
|
||||||
|
(do (rb-p-advance!)
|
||||||
|
(append! args {:type "block-pass" :value (rb-p-parse-assign)})))
|
||||||
|
(:else (append! args (rb-p-parse-assign))))
|
||||||
|
(if (= (rb-p-type) "comma")
|
||||||
|
(do (rb-p-advance!) (rb-p-skip-newlines!) (rb-p-ab-loop))
|
||||||
|
(set! going false))))))
|
||||||
|
(rb-p-ab-loop)
|
||||||
|
args)))
|
||||||
|
|
||||||
|
;; Primary expression
|
||||||
|
(define rb-p-parse-primary
|
||||||
|
(fn ()
|
||||||
|
(cond
|
||||||
|
((= (rb-p-type) "int")
|
||||||
|
(let ((v (rb-p-val))) (rb-p-advance!) {:type "lit-int" :value v}))
|
||||||
|
((= (rb-p-type) "float")
|
||||||
|
(let ((v (rb-p-val))) (rb-p-advance!) {:type "lit-float" :value v}))
|
||||||
|
((= (rb-p-type) "string")
|
||||||
|
(let ((v (rb-p-val))) (rb-p-advance!) {:type "lit-str" :value v}))
|
||||||
|
((= (rb-p-type) "symbol")
|
||||||
|
(let ((v (rb-p-val))) (rb-p-advance!) {:type "lit-sym" :value v}))
|
||||||
|
((= (rb-p-type) "words")
|
||||||
|
(let ((v (rb-p-val))) (rb-p-advance!) {:type "lit-words" :elems v}))
|
||||||
|
((= (rb-p-type) "isymbols")
|
||||||
|
(let ((v (rb-p-val))) (rb-p-advance!) {:type "lit-isyms" :elems v}))
|
||||||
|
((= (rb-p-type) "ivar")
|
||||||
|
(let ((v (rb-p-val))) (rb-p-advance!) {:type "ivar" :name v}))
|
||||||
|
((= (rb-p-type) "cvar")
|
||||||
|
(let ((v (rb-p-val))) (rb-p-advance!) {:type "cvar" :name v}))
|
||||||
|
((= (rb-p-type) "gvar")
|
||||||
|
(let ((v (rb-p-val))) (rb-p-advance!) {:type "gvar" :name v}))
|
||||||
|
((= (rb-p-type) "const")
|
||||||
|
(rb-p-parse-const-path))
|
||||||
|
((= (rb-p-type) "ident")
|
||||||
|
(let ((name (rb-p-val)))
|
||||||
|
(rb-p-advance!)
|
||||||
|
(if (= (rb-p-type) "lparen")
|
||||||
|
(let ((args (rb-p-parse-args-parens))
|
||||||
|
(blk (rb-p-parse-block)))
|
||||||
|
{:type "send" :name name :args args :block blk})
|
||||||
|
{:type "send" :name name :args (list) :block nil})))
|
||||||
|
((= (rb-p-type) "keyword")
|
||||||
|
(cond
|
||||||
|
((= (rb-p-val) "nil")
|
||||||
|
(do (rb-p-advance!) {:type "lit-nil"}))
|
||||||
|
((= (rb-p-val) "true")
|
||||||
|
(do (rb-p-advance!) {:type "lit-bool" :value true}))
|
||||||
|
((= (rb-p-val) "false")
|
||||||
|
(do (rb-p-advance!) {:type "lit-bool" :value false}))
|
||||||
|
((= (rb-p-val) "self")
|
||||||
|
(do (rb-p-advance!) {:type "self"}))
|
||||||
|
((= (rb-p-val) "super")
|
||||||
|
(do
|
||||||
|
(rb-p-advance!)
|
||||||
|
(let ((args (if (= (rb-p-type) "lparen")
|
||||||
|
(rb-p-parse-args-parens) (list)))
|
||||||
|
(blk (rb-p-parse-block)))
|
||||||
|
{:type "send" :name "super" :args args :block blk})))
|
||||||
|
(:else
|
||||||
|
{:type "error"
|
||||||
|
:msg (join "" (list "unexpected kw " (rb-p-val)))})))
|
||||||
|
((= (rb-p-type) "lbracket")
|
||||||
|
(rb-p-parse-array))
|
||||||
|
((= (rb-p-type) "lbrace")
|
||||||
|
(rb-p-parse-hash))
|
||||||
|
((= (rb-p-type) "lparen")
|
||||||
|
(do
|
||||||
|
(rb-p-advance!)
|
||||||
|
(rb-p-skip-seps!)
|
||||||
|
(let ((node (rb-p-parse-expr)))
|
||||||
|
(rb-p-skip-seps!)
|
||||||
|
(rb-p-expect! "rparen")
|
||||||
|
node)))
|
||||||
|
(:else
|
||||||
|
(do
|
||||||
|
(rb-p-advance!)
|
||||||
|
{:type "error"
|
||||||
|
:msg (join "" (list "unexpected " (rb-p-type)
|
||||||
|
" '" (or (rb-p-val) "") "'"))})))))
|
||||||
|
|
||||||
|
;; .method ::Const [index] chains
|
||||||
|
(define rb-p-parse-postfix
|
||||||
|
(fn ()
|
||||||
|
(let ((node (rb-p-parse-primary)))
|
||||||
|
(define rb-p-pf-loop
|
||||||
|
(fn ()
|
||||||
|
(cond
|
||||||
|
((= (rb-p-type) "dot")
|
||||||
|
(do
|
||||||
|
(rb-p-advance!)
|
||||||
|
(let ((method (rb-p-val)))
|
||||||
|
(rb-p-advance!)
|
||||||
|
(let ((args (if (= (rb-p-type) "lparen")
|
||||||
|
(rb-p-parse-args-parens) (list)))
|
||||||
|
(blk (rb-p-parse-block)))
|
||||||
|
(set! node {:type "call" :recv node :method method
|
||||||
|
:args args :block blk})
|
||||||
|
(rb-p-pf-loop)))))
|
||||||
|
((= (rb-p-type) "dcolon")
|
||||||
|
(do
|
||||||
|
(rb-p-advance!)
|
||||||
|
(let ((name (rb-p-val)))
|
||||||
|
(rb-p-advance!)
|
||||||
|
(if (= (rb-p-type) "lparen")
|
||||||
|
(let ((args (rb-p-parse-args-parens))
|
||||||
|
(blk (rb-p-parse-block)))
|
||||||
|
(set! node {:type "call" :recv node :method name
|
||||||
|
:args args :block blk}))
|
||||||
|
(set! node {:type "const-path" :left node :name name}))
|
||||||
|
(rb-p-pf-loop))))
|
||||||
|
((= (rb-p-type) "lbracket")
|
||||||
|
(do
|
||||||
|
(rb-p-advance!)
|
||||||
|
(rb-p-skip-newlines!)
|
||||||
|
(let ((idxargs (list)))
|
||||||
|
(define rb-p-idx-loop
|
||||||
|
(fn ()
|
||||||
|
(when (not (or (= (rb-p-type) "rbracket") (= (rb-p-type) "eof")))
|
||||||
|
(do
|
||||||
|
(append! idxargs (rb-p-parse-assign))
|
||||||
|
(when (= (rb-p-type) "comma")
|
||||||
|
(do (rb-p-advance!) (rb-p-skip-newlines!)))
|
||||||
|
(rb-p-idx-loop)))))
|
||||||
|
(rb-p-idx-loop)
|
||||||
|
(rb-p-expect! "rbracket")
|
||||||
|
(set! node {:type "index" :recv node :args idxargs})
|
||||||
|
(rb-p-pf-loop))))
|
||||||
|
(:else nil))))
|
||||||
|
(rb-p-pf-loop)
|
||||||
|
node)))
|
||||||
|
|
||||||
|
(define rb-p-parse-unary
|
||||||
|
(fn ()
|
||||||
|
(cond
|
||||||
|
((and (= (rb-p-type) "op") (= (rb-p-val) "!"))
|
||||||
|
(do (rb-p-advance!)
|
||||||
|
{:type "unop" :op "!" :value (rb-p-parse-unary)}))
|
||||||
|
((and (= (rb-p-type) "op") (= (rb-p-val) "~"))
|
||||||
|
(do (rb-p-advance!)
|
||||||
|
{:type "unop" :op "~" :value (rb-p-parse-unary)}))
|
||||||
|
((and (= (rb-p-type) "op") (= (rb-p-val) "-"))
|
||||||
|
(do (rb-p-advance!)
|
||||||
|
{:type "unop" :op "-" :value (rb-p-parse-unary)}))
|
||||||
|
((and (= (rb-p-type) "op") (= (rb-p-val) "+"))
|
||||||
|
(do (rb-p-advance!) (rb-p-parse-unary)))
|
||||||
|
(:else (rb-p-parse-postfix)))))
|
||||||
|
|
||||||
|
(define rb-p-parse-power
|
||||||
|
(fn ()
|
||||||
|
(let ((node (rb-p-parse-unary)))
|
||||||
|
(if (and (= (rb-p-type) "op") (= (rb-p-val) "**"))
|
||||||
|
(do (rb-p-advance!)
|
||||||
|
{:type "binop" :op "**" :left node :right (rb-p-parse-power)})
|
||||||
|
node))))
|
||||||
|
|
||||||
|
(define rb-p-parse-mul
|
||||||
|
(fn ()
|
||||||
|
(let ((node (rb-p-parse-power)))
|
||||||
|
(define rb-p-mul-loop
|
||||||
|
(fn ()
|
||||||
|
(if (and (= (rb-p-type) "op")
|
||||||
|
(or (= (rb-p-val) "*") (= (rb-p-val) "/") (= (rb-p-val) "%")))
|
||||||
|
(let ((op (rb-p-val)))
|
||||||
|
(rb-p-advance!)
|
||||||
|
(set! node {:type "binop" :op op :left node :right (rb-p-parse-power)})
|
||||||
|
(rb-p-mul-loop))
|
||||||
|
node)))
|
||||||
|
(rb-p-mul-loop))))
|
||||||
|
|
||||||
|
(define rb-p-parse-add
|
||||||
|
(fn ()
|
||||||
|
(let ((node (rb-p-parse-mul)))
|
||||||
|
(define rb-p-add-loop
|
||||||
|
(fn ()
|
||||||
|
(if (and (= (rb-p-type) "op")
|
||||||
|
(or (= (rb-p-val) "+") (= (rb-p-val) "-")))
|
||||||
|
(let ((op (rb-p-val)))
|
||||||
|
(rb-p-advance!)
|
||||||
|
(set! node {:type "binop" :op op :left node :right (rb-p-parse-mul)})
|
||||||
|
(rb-p-add-loop))
|
||||||
|
node)))
|
||||||
|
(rb-p-add-loop))))
|
||||||
|
|
||||||
|
(define rb-p-parse-shift
|
||||||
|
(fn ()
|
||||||
|
(let ((node (rb-p-parse-add)))
|
||||||
|
(define rb-p-sh-loop
|
||||||
|
(fn ()
|
||||||
|
(if (and (= (rb-p-type) "op")
|
||||||
|
(or (= (rb-p-val) "<<") (= (rb-p-val) ">>")))
|
||||||
|
(let ((op (rb-p-val)))
|
||||||
|
(rb-p-advance!)
|
||||||
|
(set! node {:type "binop" :op op :left node :right (rb-p-parse-add)})
|
||||||
|
(rb-p-sh-loop))
|
||||||
|
node)))
|
||||||
|
(rb-p-sh-loop))))
|
||||||
|
|
||||||
|
(define rb-p-parse-bitand
|
||||||
|
(fn ()
|
||||||
|
(let ((node (rb-p-parse-shift)))
|
||||||
|
(define rb-p-ba-loop
|
||||||
|
(fn ()
|
||||||
|
(if (and (= (rb-p-type) "op") (= (rb-p-val) "&"))
|
||||||
|
(do
|
||||||
|
(rb-p-advance!)
|
||||||
|
(set! node {:type "binop" :op "&" :left node :right (rb-p-parse-shift)})
|
||||||
|
(rb-p-ba-loop))
|
||||||
|
node)))
|
||||||
|
(rb-p-ba-loop))))
|
||||||
|
|
||||||
|
;; | is "pipe" token (not "op")
|
||||||
|
(define rb-p-parse-bitor
|
||||||
|
(fn ()
|
||||||
|
(let ((node (rb-p-parse-bitand)))
|
||||||
|
(define rb-p-bo-loop
|
||||||
|
(fn ()
|
||||||
|
(cond
|
||||||
|
((= (rb-p-type) "pipe")
|
||||||
|
(do
|
||||||
|
(rb-p-advance!)
|
||||||
|
(set! node {:type "binop" :op "|" :left node :right (rb-p-parse-bitand)})
|
||||||
|
(rb-p-bo-loop)))
|
||||||
|
((and (= (rb-p-type) "op") (= (rb-p-val) "^"))
|
||||||
|
(do
|
||||||
|
(rb-p-advance!)
|
||||||
|
(set! node {:type "binop" :op "^" :left node :right (rb-p-parse-bitand)})
|
||||||
|
(rb-p-bo-loop)))
|
||||||
|
(:else node))))
|
||||||
|
(rb-p-bo-loop))))
|
||||||
|
|
||||||
|
(define rb-p-parse-comparison
|
||||||
|
(fn ()
|
||||||
|
(let ((node (rb-p-parse-bitor)))
|
||||||
|
(if (and (= (rb-p-type) "op")
|
||||||
|
(contains? (list "==" "!=" "<" ">" "<=" ">="
|
||||||
|
"<=>" "===" "=~" "!~") (rb-p-val)))
|
||||||
|
(let ((op (rb-p-val)))
|
||||||
|
(rb-p-advance!)
|
||||||
|
{:type "binop" :op op :left node :right (rb-p-parse-bitor)})
|
||||||
|
node))))
|
||||||
|
|
||||||
|
(define rb-p-parse-not
|
||||||
|
(fn ()
|
||||||
|
(if (and (= (rb-p-type) "keyword") (= (rb-p-val) "not"))
|
||||||
|
(do (rb-p-advance!)
|
||||||
|
{:type "not" :value (rb-p-parse-not)})
|
||||||
|
(rb-p-parse-comparison))))
|
||||||
|
|
||||||
|
(define rb-p-parse-and
|
||||||
|
(fn ()
|
||||||
|
(let ((node (rb-p-parse-not)))
|
||||||
|
(define rb-p-and-loop
|
||||||
|
(fn ()
|
||||||
|
(cond
|
||||||
|
((and (= (rb-p-type) "op") (= (rb-p-val) "&&"))
|
||||||
|
(do
|
||||||
|
(rb-p-advance!)
|
||||||
|
(set! node {:type "binop" :op "&&" :left node :right (rb-p-parse-not)})
|
||||||
|
(rb-p-and-loop)))
|
||||||
|
((and (= (rb-p-type) "keyword") (= (rb-p-val) "and"))
|
||||||
|
(do
|
||||||
|
(rb-p-advance!)
|
||||||
|
(set! node {:type "binop" :op "and" :left node :right (rb-p-parse-not)})
|
||||||
|
(rb-p-and-loop)))
|
||||||
|
(:else node))))
|
||||||
|
(rb-p-and-loop))))
|
||||||
|
|
||||||
|
(define rb-p-parse-or
|
||||||
|
(fn ()
|
||||||
|
(let ((node (rb-p-parse-and)))
|
||||||
|
(define rb-p-or-loop
|
||||||
|
(fn ()
|
||||||
|
(cond
|
||||||
|
((and (= (rb-p-type) "op") (= (rb-p-val) "||"))
|
||||||
|
(do
|
||||||
|
(rb-p-advance!)
|
||||||
|
(set! node {:type "binop" :op "||" :left node :right (rb-p-parse-and)})
|
||||||
|
(rb-p-or-loop)))
|
||||||
|
((and (= (rb-p-type) "keyword") (= (rb-p-val) "or"))
|
||||||
|
(do
|
||||||
|
(rb-p-advance!)
|
||||||
|
(set! node {:type "binop" :op "or" :left node :right (rb-p-parse-and)})
|
||||||
|
(rb-p-or-loop)))
|
||||||
|
(:else node))))
|
||||||
|
(rb-p-or-loop))))
|
||||||
|
|
||||||
|
(define rb-p-parse-range
|
||||||
|
(fn ()
|
||||||
|
(let ((node (rb-p-parse-or)))
|
||||||
|
(cond
|
||||||
|
((= (rb-p-type) "dotdot")
|
||||||
|
(do (rb-p-advance!)
|
||||||
|
{:type "range" :from node :to (rb-p-parse-or) :exclusive false}))
|
||||||
|
((= (rb-p-type) "dotdotdot")
|
||||||
|
(do (rb-p-advance!)
|
||||||
|
{:type "range" :from node :to (rb-p-parse-or) :exclusive true}))
|
||||||
|
(:else node)))))
|
||||||
|
|
||||||
|
(define rb-p-parse-assign
|
||||||
|
(fn ()
|
||||||
|
(let ((node (rb-p-parse-range)))
|
||||||
|
(cond
|
||||||
|
((and (= (rb-p-type) "op") (= (rb-p-val) "="))
|
||||||
|
(do (rb-p-advance!)
|
||||||
|
{:type "assign" :target node :value (rb-p-parse-assign)}))
|
||||||
|
((and (= (rb-p-type) "op")
|
||||||
|
(contains? (list "+=" "-=" "*=" "/=" "%=" "**="
|
||||||
|
"<<=" ">>=" "&=" "|=" "^=" "&&=" "||=")
|
||||||
|
(rb-p-val)))
|
||||||
|
(let ((op (substring (rb-p-val) 0 (- (len (rb-p-val)) 1))))
|
||||||
|
(rb-p-advance!)
|
||||||
|
{:type "op-assign" :target node :op op :value (rb-p-parse-assign)}))
|
||||||
|
(:else node)))))
|
||||||
|
|
||||||
|
(define rb-p-parse-expr
|
||||||
|
(fn () (rb-p-parse-assign)))
|
||||||
|
|
||||||
|
;; e, e, ... → single node or array
|
||||||
|
(define rb-p-parse-multi-val
|
||||||
|
(fn ()
|
||||||
|
(let ((vals (list)))
|
||||||
|
(define rb-p-mv-loop
|
||||||
|
(fn ()
|
||||||
|
(append! vals (rb-p-parse-assign))
|
||||||
|
(when (= (rb-p-type) "comma")
|
||||||
|
(do (rb-p-advance!) (rb-p-skip-newlines!) (rb-p-mv-loop)))))
|
||||||
|
(rb-p-mv-loop)
|
||||||
|
(if (= (len vals) 1)
|
||||||
|
(nth vals 0)
|
||||||
|
{:type "array" :elems vals}))))
|
||||||
|
|
||||||
|
;; a, b, *c = rhs
|
||||||
|
(define rb-p-parse-massign
|
||||||
|
(fn ()
|
||||||
|
(let ((targets (list)))
|
||||||
|
(define rb-p-ma-loop
|
||||||
|
(fn ()
|
||||||
|
(cond
|
||||||
|
((and (= (rb-p-type) "op") (= (rb-p-val) "*"))
|
||||||
|
(do
|
||||||
|
(rb-p-advance!)
|
||||||
|
(if (= (rb-p-type) "ident")
|
||||||
|
(do
|
||||||
|
(append! targets {:type "splat-target" :name (rb-p-val)})
|
||||||
|
(rb-p-advance!))
|
||||||
|
(append! targets {:type "splat-target" :name nil}))))
|
||||||
|
((= (rb-p-type) "ident")
|
||||||
|
(do (append! targets {:type "lvar" :name (rb-p-val)}) (rb-p-advance!)))
|
||||||
|
((= (rb-p-type) "ivar")
|
||||||
|
(do (append! targets {:type "ivar" :name (rb-p-val)}) (rb-p-advance!)))
|
||||||
|
((= (rb-p-type) "cvar")
|
||||||
|
(do (append! targets {:type "cvar" :name (rb-p-val)}) (rb-p-advance!)))
|
||||||
|
((= (rb-p-type) "gvar")
|
||||||
|
(do (append! targets {:type "gvar" :name (rb-p-val)}) (rb-p-advance!)))
|
||||||
|
((= (rb-p-type) "const")
|
||||||
|
(do (append! targets {:type "const" :name (rb-p-val)}) (rb-p-advance!)))
|
||||||
|
(:else nil))
|
||||||
|
(when (= (rb-p-type) "comma")
|
||||||
|
(do (rb-p-advance!) (rb-p-skip-newlines!) (rb-p-ma-loop)))))
|
||||||
|
(rb-p-ma-loop)
|
||||||
|
(rb-p-advance!)
|
||||||
|
{:type "massign" :targets targets :value (rb-p-parse-multi-val)})))
|
||||||
|
|
||||||
|
|
||||||
|
;; if cond [then] body [elsif cond [then] body ...] [else body] end
|
||||||
|
(define rb-p-parse-if
|
||||||
|
(fn ()
|
||||||
|
(rb-p-advance!)
|
||||||
|
(let ((cond-val (rb-p-parse-expr)))
|
||||||
|
(when (and (= (rb-p-type) "keyword") (= (rb-p-val) "then"))
|
||||||
|
(rb-p-advance!))
|
||||||
|
(rb-p-skip-seps!)
|
||||||
|
(let ((then-body (rb-p-parse-stmts (list "end" "else" "elsif"))))
|
||||||
|
(let ((elsifs (list)) (else-body nil))
|
||||||
|
(define rb-p-elsif-loop
|
||||||
|
(fn ()
|
||||||
|
(when (and (= (rb-p-type) "keyword") (= (rb-p-val) "elsif"))
|
||||||
|
(do
|
||||||
|
(rb-p-advance!)
|
||||||
|
(let ((ec (rb-p-parse-expr)))
|
||||||
|
(when (and (= (rb-p-type) "keyword") (= (rb-p-val) "then"))
|
||||||
|
(rb-p-advance!))
|
||||||
|
(rb-p-skip-seps!)
|
||||||
|
(let ((eb (rb-p-parse-stmts (list "end" "else" "elsif"))))
|
||||||
|
(append! elsifs {:cond ec :then eb})
|
||||||
|
(rb-p-elsif-loop)))))))
|
||||||
|
(rb-p-elsif-loop)
|
||||||
|
(when (and (= (rb-p-type) "keyword") (= (rb-p-val) "else"))
|
||||||
|
(do
|
||||||
|
(rb-p-advance!)
|
||||||
|
(rb-p-skip-seps!)
|
||||||
|
(set! else-body (rb-p-parse-stmts (list "end")))))
|
||||||
|
(rb-p-expect-kw! "end")
|
||||||
|
{:type "if" :cond cond-val :then then-body
|
||||||
|
:elsif elsifs :else else-body})))))
|
||||||
|
|
||||||
|
;; unless cond [then] body [else body] end
|
||||||
|
(define rb-p-parse-unless
|
||||||
|
(fn ()
|
||||||
|
(rb-p-advance!)
|
||||||
|
(let ((cond-val (rb-p-parse-expr)))
|
||||||
|
(when (and (= (rb-p-type) "keyword") (= (rb-p-val) "then"))
|
||||||
|
(rb-p-advance!))
|
||||||
|
(rb-p-skip-seps!)
|
||||||
|
(let ((then-body (rb-p-parse-stmts (list "end" "else"))))
|
||||||
|
(let ((else-body nil))
|
||||||
|
(when (and (= (rb-p-type) "keyword") (= (rb-p-val) "else"))
|
||||||
|
(do
|
||||||
|
(rb-p-advance!)
|
||||||
|
(rb-p-skip-seps!)
|
||||||
|
(set! else-body (rb-p-parse-stmts (list "end")))))
|
||||||
|
(rb-p-expect-kw! "end")
|
||||||
|
{:type "unless" :cond cond-val :then then-body :else else-body})))))
|
||||||
|
|
||||||
|
;; while cond [do] body end
|
||||||
|
(define rb-p-parse-while
|
||||||
|
(fn ()
|
||||||
|
(rb-p-advance!)
|
||||||
|
(let ((cond-val (rb-p-parse-expr)))
|
||||||
|
(when (and (= (rb-p-type) "keyword") (= (rb-p-val) "do"))
|
||||||
|
(rb-p-advance!))
|
||||||
|
(rb-p-skip-seps!)
|
||||||
|
(let ((body (rb-p-parse-stmts (list "end"))))
|
||||||
|
(rb-p-expect-kw! "end")
|
||||||
|
{:type "while" :cond cond-val :body body}))))
|
||||||
|
|
||||||
|
;; until cond [do] body end
|
||||||
|
(define rb-p-parse-until
|
||||||
|
(fn ()
|
||||||
|
(rb-p-advance!)
|
||||||
|
(let ((cond-val (rb-p-parse-expr)))
|
||||||
|
(when (and (= (rb-p-type) "keyword") (= (rb-p-val) "do"))
|
||||||
|
(rb-p-advance!))
|
||||||
|
(rb-p-skip-seps!)
|
||||||
|
(let ((body (rb-p-parse-stmts (list "end"))))
|
||||||
|
(rb-p-expect-kw! "end")
|
||||||
|
{:type "until" :cond cond-val :body body}))))
|
||||||
|
|
||||||
|
;; for var in iter [do] body end
|
||||||
|
(define rb-p-parse-for
|
||||||
|
(fn ()
|
||||||
|
(rb-p-advance!)
|
||||||
|
(let ((var (rb-p-val)))
|
||||||
|
(rb-p-advance!)
|
||||||
|
(rb-p-expect-kw! "in")
|
||||||
|
(let ((iter (rb-p-parse-expr)))
|
||||||
|
(when (and (= (rb-p-type) "keyword") (= (rb-p-val) "do"))
|
||||||
|
(rb-p-advance!))
|
||||||
|
(rb-p-skip-seps!)
|
||||||
|
(let ((body (rb-p-parse-stmts (list "end"))))
|
||||||
|
(rb-p-expect-kw! "end")
|
||||||
|
{:type "for" :var var :iter iter :body body})))))
|
||||||
|
|
||||||
|
;; case [expr] when val[, val] [then] body... [else body] end
|
||||||
|
(define rb-p-parse-case
|
||||||
|
(fn ()
|
||||||
|
(rb-p-advance!)
|
||||||
|
(let ((case-val (if (rb-p-sep?) nil (rb-p-parse-expr))))
|
||||||
|
(rb-p-skip-seps!)
|
||||||
|
(let ((whens (list)) (else-body nil))
|
||||||
|
(define rb-p-when-loop
|
||||||
|
(fn ()
|
||||||
|
(when (and (= (rb-p-type) "keyword") (= (rb-p-val) "when"))
|
||||||
|
(do
|
||||||
|
(rb-p-advance!)
|
||||||
|
(let ((wvals (list)))
|
||||||
|
(define rb-p-wval-loop
|
||||||
|
(fn ()
|
||||||
|
(append! wvals (rb-p-parse-assign))
|
||||||
|
(when (= (rb-p-type) "comma")
|
||||||
|
(do (rb-p-advance!) (rb-p-skip-newlines!) (rb-p-wval-loop)))))
|
||||||
|
(rb-p-wval-loop)
|
||||||
|
(when (and (= (rb-p-type) "keyword") (= (rb-p-val) "then"))
|
||||||
|
(rb-p-advance!))
|
||||||
|
(rb-p-skip-seps!)
|
||||||
|
(let ((wb (rb-p-parse-stmts (list "end" "else" "when"))))
|
||||||
|
(append! whens {:values wvals :body wb})
|
||||||
|
(rb-p-when-loop)))))))
|
||||||
|
(rb-p-when-loop)
|
||||||
|
(when (and (= (rb-p-type) "keyword") (= (rb-p-val) "else"))
|
||||||
|
(do
|
||||||
|
(rb-p-advance!)
|
||||||
|
(rb-p-skip-seps!)
|
||||||
|
(set! else-body (rb-p-parse-stmts (list "end")))))
|
||||||
|
(rb-p-expect-kw! "end")
|
||||||
|
{:type "case" :value case-val :whens whens :else else-body}))))
|
||||||
|
|
||||||
|
(define rb-p-parse-stmt
|
||||||
|
(fn ()
|
||||||
|
(let ((node
|
||||||
|
(cond
|
||||||
|
((and (= (rb-p-type) "keyword") (= (rb-p-val) "def"))
|
||||||
|
(rb-p-parse-def))
|
||||||
|
((and (= (rb-p-type) "keyword") (= (rb-p-val) "class"))
|
||||||
|
(rb-p-parse-class))
|
||||||
|
((and (= (rb-p-type) "keyword") (= (rb-p-val) "module"))
|
||||||
|
(rb-p-parse-module))
|
||||||
|
((and (= (rb-p-type) "keyword") (= (rb-p-val) "if"))
|
||||||
|
(rb-p-parse-if))
|
||||||
|
((and (= (rb-p-type) "keyword") (= (rb-p-val) "unless"))
|
||||||
|
(rb-p-parse-unless))
|
||||||
|
((and (= (rb-p-type) "keyword") (= (rb-p-val) "while"))
|
||||||
|
(rb-p-parse-while))
|
||||||
|
((and (= (rb-p-type) "keyword") (= (rb-p-val) "until"))
|
||||||
|
(rb-p-parse-until))
|
||||||
|
((and (= (rb-p-type) "keyword") (= (rb-p-val) "case"))
|
||||||
|
(rb-p-parse-case))
|
||||||
|
((and (= (rb-p-type) "keyword") (= (rb-p-val) "for"))
|
||||||
|
(rb-p-parse-for))
|
||||||
|
((and (= (rb-p-type) "keyword") (= (rb-p-val) "return"))
|
||||||
|
(do (rb-p-advance!)
|
||||||
|
{:type "return"
|
||||||
|
:value (if (or (rb-p-sep?) (= (rb-p-type) "eof"))
|
||||||
|
nil (rb-p-parse-multi-val))}))
|
||||||
|
((and (= (rb-p-type) "keyword") (= (rb-p-val) "yield"))
|
||||||
|
(do (rb-p-advance!)
|
||||||
|
{:type "yield"
|
||||||
|
:args (cond
|
||||||
|
((= (rb-p-type) "lparen") (rb-p-parse-args-parens))
|
||||||
|
((or (rb-p-sep?) (= (rb-p-type) "eof")) (list))
|
||||||
|
(:else (rb-p-parse-args-bare)))}))
|
||||||
|
((and (= (rb-p-type) "keyword") (= (rb-p-val) "break"))
|
||||||
|
(do (rb-p-advance!)
|
||||||
|
{:type "break"
|
||||||
|
:value (if (or (rb-p-sep?) (= (rb-p-type) "eof"))
|
||||||
|
nil (rb-p-parse-expr))}))
|
||||||
|
((and (= (rb-p-type) "keyword") (= (rb-p-val) "next"))
|
||||||
|
(do (rb-p-advance!)
|
||||||
|
{:type "next"
|
||||||
|
:value (if (or (rb-p-sep?) (= (rb-p-type) "eof"))
|
||||||
|
nil (rb-p-parse-expr))}))
|
||||||
|
((and (= (rb-p-type) "keyword") (= (rb-p-val) "redo"))
|
||||||
|
(do (rb-p-advance!) {:type "redo"}))
|
||||||
|
((and (= (rb-p-type) "keyword") (= (rb-p-val) "raise"))
|
||||||
|
(do (rb-p-advance!)
|
||||||
|
{:type "raise"
|
||||||
|
:value (if (or (rb-p-sep?) (= (rb-p-type) "eof"))
|
||||||
|
nil (rb-p-parse-expr))}))
|
||||||
|
;; Massign: token followed by comma
|
||||||
|
((and (or (= (rb-p-type) "ident") (= (rb-p-type) "ivar")
|
||||||
|
(= (rb-p-type) "cvar") (= (rb-p-type) "gvar")
|
||||||
|
(= (rb-p-type) "const"))
|
||||||
|
(= (get (rb-p-peek 1) :type) "comma"))
|
||||||
|
(rb-p-parse-massign))
|
||||||
|
(:else
|
||||||
|
(let ((nd (rb-p-parse-assign)))
|
||||||
|
(if (and (= (get nd :type) "send")
|
||||||
|
(= (len (get nd :args)) 0)
|
||||||
|
(nil? (get nd :block)))
|
||||||
|
(cond
|
||||||
|
((or (and (= (rb-p-type) "keyword") (= (rb-p-val) "do"))
|
||||||
|
(= (rb-p-type) "lbrace"))
|
||||||
|
(let ((blk (rb-p-parse-block)))
|
||||||
|
{:type "send" :name (get nd :name)
|
||||||
|
:args (list) :block blk}))
|
||||||
|
((and (not (rb-p-sep?))
|
||||||
|
(not (= (rb-p-type) "eof"))
|
||||||
|
(not (= (rb-p-type) "op"))
|
||||||
|
(not (= (rb-p-type) "dot"))
|
||||||
|
(not (= (rb-p-type) "dcolon"))
|
||||||
|
(not (= (rb-p-type) "rparen"))
|
||||||
|
(not (= (rb-p-type) "rbracket"))
|
||||||
|
(not (= (rb-p-type) "rbrace"))
|
||||||
|
(not (= (rb-p-type) "lbrace"))
|
||||||
|
(not (and (= (rb-p-type) "keyword")
|
||||||
|
(contains? (list "end" "else" "elsif"
|
||||||
|
"when" "rescue" "ensure"
|
||||||
|
"then" "do" "and" "or"
|
||||||
|
"not" "if" "unless"
|
||||||
|
"while" "until")
|
||||||
|
(rb-p-val)))))
|
||||||
|
(let ((args (rb-p-parse-args-bare))
|
||||||
|
(blk (rb-p-parse-block)))
|
||||||
|
(if (> (len args) 0)
|
||||||
|
{:type "send" :name (get nd :name)
|
||||||
|
:args args :block blk}
|
||||||
|
nd)))
|
||||||
|
(:else nd))
|
||||||
|
nd))))))
|
||||||
|
;; Postfix modifiers
|
||||||
|
(cond
|
||||||
|
((and (= (rb-p-type) "keyword") (= (rb-p-val) "if"))
|
||||||
|
(do (rb-p-advance!)
|
||||||
|
{:type "postfix-if" :cond (rb-p-parse-expr) :body node}))
|
||||||
|
((and (= (rb-p-type) "keyword") (= (rb-p-val) "unless"))
|
||||||
|
(do (rb-p-advance!)
|
||||||
|
{:type "postfix-unless" :cond (rb-p-parse-expr) :body node}))
|
||||||
|
((and (= (rb-p-type) "keyword") (= (rb-p-val) "while"))
|
||||||
|
(do (rb-p-advance!)
|
||||||
|
{:type "postfix-while" :cond (rb-p-parse-expr) :body node}))
|
||||||
|
((and (= (rb-p-type) "keyword") (= (rb-p-val) "until"))
|
||||||
|
(do (rb-p-advance!)
|
||||||
|
{:type "postfix-until" :cond (rb-p-parse-expr) :body node}))
|
||||||
|
(:else node)))))
|
||||||
|
|
||||||
|
(define rb-p-parse-stmts
|
||||||
|
(fn (terminators)
|
||||||
|
(let ((stmts (list)))
|
||||||
|
(define rb-p-at-term?
|
||||||
|
(fn ()
|
||||||
|
(or (= (rb-p-type) "eof")
|
||||||
|
(and (= (rb-p-type) "keyword")
|
||||||
|
(contains? terminators (rb-p-val)))
|
||||||
|
(and (= (rb-p-type) "rbrace")
|
||||||
|
(contains? terminators "rbrace")))))
|
||||||
|
(define rb-p-ps-loop
|
||||||
|
(fn ()
|
||||||
|
(rb-p-skip-seps!)
|
||||||
|
(when (not (rb-p-at-term?))
|
||||||
|
(do
|
||||||
|
(append! stmts (rb-p-parse-stmt))
|
||||||
|
(rb-p-skip-seps!)
|
||||||
|
(rb-p-ps-loop)))))
|
||||||
|
(rb-p-ps-loop)
|
||||||
|
stmts)))
|
||||||
|
|
||||||
|
{:type "program" :stmts (rb-p-parse-stmts (list))})))
|
||||||
|
|
||||||
|
(define rb-parse-str
|
||||||
|
(fn (src) (rb-parse (rb-tokenize src))))
|
||||||
92
lib/ruby/test.sh
Executable file
92
lib/ruby/test.sh
Executable file
@@ -0,0 +1,92 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# Ruby-on-SX test runner.
|
||||||
|
# Usage:
|
||||||
|
# bash lib/ruby/test.sh # run all tests
|
||||||
|
# bash lib/ruby/test.sh -v # verbose
|
||||||
|
# bash lib/ruby/test.sh tests/parse.sx # single file
|
||||||
|
|
||||||
|
set -euo pipefail
|
||||||
|
cd "$(git rev-parse --show-toplevel)"
|
||||||
|
|
||||||
|
SX_SERVER="hosts/ocaml/_build/default/bin/sx_server.exe"
|
||||||
|
if [ ! -x "$SX_SERVER" ]; then
|
||||||
|
MAIN_ROOT=$(git worktree list | head -1 | awk '{print $1}')
|
||||||
|
if [ -x "$MAIN_ROOT/$SX_SERVER" ]; then
|
||||||
|
SX_SERVER="$MAIN_ROOT/$SX_SERVER"
|
||||||
|
else
|
||||||
|
echo "ERROR: sx_server.exe not found."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
VERBOSE=""
|
||||||
|
FILES=()
|
||||||
|
for arg in "$@"; do
|
||||||
|
case "$arg" in
|
||||||
|
-v|--verbose) VERBOSE=1 ;;
|
||||||
|
*) FILES+=("$arg") ;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
if [ ${#FILES[@]} -eq 0 ]; then
|
||||||
|
mapfile -t FILES < <(find lib/ruby/tests -maxdepth 2 -name '*.sx' | sort)
|
||||||
|
fi
|
||||||
|
|
||||||
|
TOTAL_PASS=0
|
||||||
|
TOTAL_FAIL=0
|
||||||
|
FAILED_FILES=()
|
||||||
|
|
||||||
|
for FILE in "${FILES[@]}"; do
|
||||||
|
[ -f "$FILE" ] || { echo "skip $FILE (not found)"; continue; }
|
||||||
|
TMPFILE=$(mktemp)
|
||||||
|
# Build epoch sequence: load runtime files, then test file, then eval summary.
|
||||||
|
{
|
||||||
|
echo "(epoch 1)"
|
||||||
|
echo "(load \"lib/ruby/tokenizer.sx\")"
|
||||||
|
if [ -f "lib/ruby/parser.sx" ]; then
|
||||||
|
echo "(epoch 2)"
|
||||||
|
echo "(load \"lib/ruby/parser.sx\")"
|
||||||
|
fi
|
||||||
|
echo "(epoch 3)"
|
||||||
|
echo "(load \"$FILE\")"
|
||||||
|
echo "(epoch 4)"
|
||||||
|
echo "(eval \"(list rb-test-pass rb-test-fail)\")"
|
||||||
|
} > "$TMPFILE"
|
||||||
|
|
||||||
|
OUTPUT=$(timeout 60 "$SX_SERVER" < "$TMPFILE" 2>&1 || true)
|
||||||
|
rm -f "$TMPFILE"
|
||||||
|
|
||||||
|
# Extract epoch 4 result: (ok-len 4 N)\n<val> or (ok 4 <val>)
|
||||||
|
LINE=$(printf '%s\n' "$OUTPUT" | awk '/^\(ok-len 4 / {getline; print; exit}')
|
||||||
|
if [ -z "$LINE" ]; then
|
||||||
|
LINE=$(printf '%s\n' "$OUTPUT" \
|
||||||
|
| grep -E '^\(ok 4 \([0-9]+ [0-9]+\)\)' | tail -1 \
|
||||||
|
| sed -E 's/^\(ok 4 //; s/\)$//')
|
||||||
|
fi
|
||||||
|
if [ -z "$LINE" ]; then
|
||||||
|
echo "✗ $FILE: could not extract summary"
|
||||||
|
printf '%s\n' "$OUTPUT" | grep -v '^(ok ' | tail -10
|
||||||
|
TOTAL_FAIL=$((TOTAL_FAIL + 1))
|
||||||
|
FAILED_FILES+=("$FILE")
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
P=$(printf '%s\n' "$LINE" | sed -E 's/^\(([0-9]+) ([0-9]+)\).*/\1/')
|
||||||
|
F=$(printf '%s\n' "$LINE" | sed -E 's/^\(([0-9]+) ([0-9]+)\).*/\2/')
|
||||||
|
TOTAL_PASS=$((TOTAL_PASS + P))
|
||||||
|
TOTAL_FAIL=$((TOTAL_FAIL + F))
|
||||||
|
if [ "$F" -gt 0 ]; then
|
||||||
|
FAILED_FILES+=("$FILE")
|
||||||
|
printf '✗ %-40s %d/%d\n' "$FILE" "$P" "$((P+F))"
|
||||||
|
elif [ "$VERBOSE" = "1" ]; then
|
||||||
|
printf '✓ %-40s %d passed\n' "$FILE" "$P"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
TOTAL=$((TOTAL_PASS + TOTAL_FAIL))
|
||||||
|
if [ $TOTAL_FAIL -eq 0 ]; then
|
||||||
|
echo "✓ $TOTAL_PASS/$TOTAL ruby-on-sx tests passed"
|
||||||
|
else
|
||||||
|
echo "✗ $TOTAL_PASS/$TOTAL passed, $TOTAL_FAIL failed in: ${FAILED_FILES[*]}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
[ $TOTAL_FAIL -eq 0 ]
|
||||||
439
lib/ruby/tests/parse.sx
Normal file
439
lib/ruby/tests/parse.sx
Normal file
@@ -0,0 +1,439 @@
|
|||||||
|
;; Parser tests for Ruby 2.7 subset.
|
||||||
|
|
||||||
|
(define rb-deep=?
|
||||||
|
(fn (a b)
|
||||||
|
(cond
|
||||||
|
((= a b) true)
|
||||||
|
((and (dict? a) (dict? b))
|
||||||
|
(let ((ak (keys a)) (bk (keys b)))
|
||||||
|
(if (not (= (len ak) (len bk)))
|
||||||
|
false
|
||||||
|
(every?
|
||||||
|
(fn (k)
|
||||||
|
(and (has-key? b k) (rb-deep=? (get a k) (get b k))))
|
||||||
|
ak))))
|
||||||
|
((and (list? a) (list? b))
|
||||||
|
(if (not (= (len a) (len b)))
|
||||||
|
false
|
||||||
|
(let ((i 0) (ok true))
|
||||||
|
(define rb-de-loop
|
||||||
|
(fn ()
|
||||||
|
(when (and ok (< i (len a)))
|
||||||
|
(do
|
||||||
|
(when (not (rb-deep=? (nth a i) (nth b i)))
|
||||||
|
(set! ok false))
|
||||||
|
(set! i (+ i 1))
|
||||||
|
(rb-de-loop)))))
|
||||||
|
(rb-de-loop)
|
||||||
|
ok)))
|
||||||
|
(:else false))))
|
||||||
|
|
||||||
|
(define rb-test-pass 0)
|
||||||
|
(define rb-test-fail 0)
|
||||||
|
(define rb-test-fails (list))
|
||||||
|
|
||||||
|
(define rb-test
|
||||||
|
(fn (name actual expected)
|
||||||
|
(if (rb-deep=? actual expected)
|
||||||
|
(set! rb-test-pass (+ rb-test-pass 1))
|
||||||
|
(do
|
||||||
|
(set! rb-test-fail (+ rb-test-fail 1))
|
||||||
|
(append! rb-test-fails {:name name :actual actual :expected expected})))))
|
||||||
|
|
||||||
|
;; Shorthand: parse src and extract :stmts list
|
||||||
|
(define rb-p-stmts
|
||||||
|
(fn (src)
|
||||||
|
(get (rb-parse-str src) :stmts)))
|
||||||
|
|
||||||
|
;; Shorthand: parse and get first statement
|
||||||
|
(define rb-p-first
|
||||||
|
(fn (src)
|
||||||
|
(nth (rb-p-stmts src) 0)))
|
||||||
|
|
||||||
|
;; ── Literals ─────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
(rb-test "int literal"
|
||||||
|
(rb-p-first "42")
|
||||||
|
{:type "lit-int" :value 42})
|
||||||
|
|
||||||
|
(rb-test "negative int"
|
||||||
|
(rb-p-first "-7")
|
||||||
|
{:type "unop" :op "-" :value {:type "lit-int" :value 7}})
|
||||||
|
|
||||||
|
(rb-test "float literal"
|
||||||
|
(rb-p-first "3.14")
|
||||||
|
{:type "lit-float" :value "3.14"})
|
||||||
|
|
||||||
|
(rb-test "string literal"
|
||||||
|
(rb-p-first "\"hello\"")
|
||||||
|
{:type "lit-str" :value "hello"})
|
||||||
|
|
||||||
|
(rb-test "symbol literal"
|
||||||
|
(rb-p-first ":foo")
|
||||||
|
{:type "lit-sym" :value "foo"})
|
||||||
|
|
||||||
|
(rb-test "nil literal"
|
||||||
|
(rb-p-first "nil")
|
||||||
|
{:type "lit-nil"})
|
||||||
|
|
||||||
|
(rb-test "true literal"
|
||||||
|
(rb-p-first "true")
|
||||||
|
{:type "lit-bool" :value true})
|
||||||
|
|
||||||
|
(rb-test "false literal"
|
||||||
|
(rb-p-first "false")
|
||||||
|
{:type "lit-bool" :value false})
|
||||||
|
|
||||||
|
(rb-test "self"
|
||||||
|
(rb-p-first "self")
|
||||||
|
{:type "self"})
|
||||||
|
|
||||||
|
(rb-test "%w[] words"
|
||||||
|
(rb-p-first "%w[a b c]")
|
||||||
|
{:type "lit-words" :elems (list "a" "b" "c")})
|
||||||
|
|
||||||
|
(rb-test "%i[] isymbols"
|
||||||
|
(rb-p-first "%i[x y]")
|
||||||
|
{:type "lit-isyms" :elems (list "x" "y")})
|
||||||
|
|
||||||
|
;; ── Variables ─────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
(rb-test "local var / send"
|
||||||
|
(rb-p-first "x")
|
||||||
|
{:type "send" :name "x" :args (list) :block nil})
|
||||||
|
|
||||||
|
(rb-test "ivar"
|
||||||
|
(rb-p-first "@foo")
|
||||||
|
{:type "ivar" :name "@foo"})
|
||||||
|
|
||||||
|
(rb-test "cvar"
|
||||||
|
(rb-p-first "@@count")
|
||||||
|
{:type "cvar" :name "@@count"})
|
||||||
|
|
||||||
|
(rb-test "gvar"
|
||||||
|
(rb-p-first "$stdout")
|
||||||
|
{:type "gvar" :name "$stdout"})
|
||||||
|
|
||||||
|
(rb-test "constant"
|
||||||
|
(rb-p-first "Foo")
|
||||||
|
{:type "const" :name "Foo"})
|
||||||
|
|
||||||
|
(rb-test "const path"
|
||||||
|
(rb-p-first "Foo::Bar")
|
||||||
|
{:type "const-path"
|
||||||
|
:left {:type "const" :name "Foo"}
|
||||||
|
:name "Bar"})
|
||||||
|
|
||||||
|
(rb-test "triple const path"
|
||||||
|
(rb-p-first "A::B::C")
|
||||||
|
{:type "const-path"
|
||||||
|
:left {:type "const-path"
|
||||||
|
:left {:type "const" :name "A"}
|
||||||
|
:name "B"}
|
||||||
|
:name "C"})
|
||||||
|
|
||||||
|
;; ── Arrays and Hashes ─────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
(rb-test "empty array"
|
||||||
|
(rb-p-first "[]")
|
||||||
|
{:type "array" :elems (list)})
|
||||||
|
|
||||||
|
(rb-test "array literal"
|
||||||
|
(rb-p-first "[1, 2, 3]")
|
||||||
|
{:type "array" :elems (list {:type "lit-int" :value 1}
|
||||||
|
{:type "lit-int" :value 2}
|
||||||
|
{:type "lit-int" :value 3})})
|
||||||
|
|
||||||
|
(rb-test "hash colon style"
|
||||||
|
(get (rb-p-first "{a: 1}") :type)
|
||||||
|
"hash")
|
||||||
|
|
||||||
|
(rb-test "hash pair style"
|
||||||
|
(get (nth (get (rb-p-first "{a: 1}") :pairs) 0) :style)
|
||||||
|
"colon")
|
||||||
|
|
||||||
|
(rb-test "hash symbol key"
|
||||||
|
(get (get (nth (get (rb-p-first "{a: 1}") :pairs) 0) :key) :value)
|
||||||
|
"a")
|
||||||
|
|
||||||
|
;; ── Binary operators ──────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
(rb-test "addition"
|
||||||
|
(rb-p-first "1 + 2")
|
||||||
|
{:type "binop" :op "+"
|
||||||
|
:left {:type "lit-int" :value 1}
|
||||||
|
:right {:type "lit-int" :value 2}})
|
||||||
|
|
||||||
|
(rb-test "subtraction"
|
||||||
|
(get (rb-p-first "a - b") :op)
|
||||||
|
"-")
|
||||||
|
|
||||||
|
(rb-test "multiplication"
|
||||||
|
(get (rb-p-first "x * y") :op)
|
||||||
|
"*")
|
||||||
|
|
||||||
|
(rb-test "precedence: * before +"
|
||||||
|
(rb-p-first "1 + 2 * 3")
|
||||||
|
{:type "binop" :op "+"
|
||||||
|
:left {:type "lit-int" :value 1}
|
||||||
|
:right {:type "binop" :op "*"
|
||||||
|
:left {:type "lit-int" :value 2}
|
||||||
|
:right {:type "lit-int" :value 3}}})
|
||||||
|
|
||||||
|
(rb-test "power right-assoc"
|
||||||
|
(rb-p-first "2 ** 3 ** 4")
|
||||||
|
{:type "binop" :op "**"
|
||||||
|
:left {:type "lit-int" :value 2}
|
||||||
|
:right {:type "binop" :op "**"
|
||||||
|
:left {:type "lit-int" :value 3}
|
||||||
|
:right {:type "lit-int" :value 4}}})
|
||||||
|
|
||||||
|
(rb-test "equality"
|
||||||
|
(get (rb-p-first "a == b") :op)
|
||||||
|
"==")
|
||||||
|
|
||||||
|
(rb-test "logical and"
|
||||||
|
(get (rb-p-first "a && b") :op)
|
||||||
|
"&&")
|
||||||
|
|
||||||
|
(rb-test "logical or"
|
||||||
|
(get (rb-p-first "a || b") :op)
|
||||||
|
"||")
|
||||||
|
|
||||||
|
(rb-test "range inclusive"
|
||||||
|
(rb-p-first "1..5")
|
||||||
|
{:type "range"
|
||||||
|
:from {:type "lit-int" :value 1}
|
||||||
|
:to {:type "lit-int" :value 5}
|
||||||
|
:exclusive false})
|
||||||
|
|
||||||
|
(rb-test "range exclusive"
|
||||||
|
(get (rb-p-first "1...5") :exclusive)
|
||||||
|
true)
|
||||||
|
|
||||||
|
;; ── Assignment ────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
(rb-test "assign"
|
||||||
|
(rb-p-first "x = 1")
|
||||||
|
{:type "assign"
|
||||||
|
:target {:type "send" :name "x" :args (list) :block nil}
|
||||||
|
:value {:type "lit-int" :value 1}})
|
||||||
|
|
||||||
|
(rb-test "op-assign +="
|
||||||
|
(get (rb-p-first "x += 1") :type)
|
||||||
|
"op-assign")
|
||||||
|
|
||||||
|
(rb-test "op-assign op"
|
||||||
|
(get (rb-p-first "x += 1") :op)
|
||||||
|
"+")
|
||||||
|
|
||||||
|
(rb-test "massign"
|
||||||
|
(get (rb-p-first "a, b = 1, 2") :type)
|
||||||
|
"massign")
|
||||||
|
|
||||||
|
(rb-test "massign targets"
|
||||||
|
(len (get (rb-p-first "a, b = 1, 2") :targets))
|
||||||
|
2)
|
||||||
|
|
||||||
|
(rb-test "massign value array"
|
||||||
|
(get (get (rb-p-first "a, b = 1, 2") :value) :type)
|
||||||
|
"array")
|
||||||
|
|
||||||
|
;; ── Method calls ──────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
(rb-test "call with parens"
|
||||||
|
(rb-p-first "foo(1, 2)")
|
||||||
|
{:type "send" :name "foo"
|
||||||
|
:args (list {:type "lit-int" :value 1}
|
||||||
|
{:type "lit-int" :value 2})
|
||||||
|
:block nil})
|
||||||
|
|
||||||
|
(rb-test "chained call"
|
||||||
|
(get (rb-p-first "obj.foo") :type)
|
||||||
|
"call")
|
||||||
|
|
||||||
|
(rb-test "chained call method"
|
||||||
|
(get (rb-p-first "obj.foo") :method)
|
||||||
|
"foo")
|
||||||
|
|
||||||
|
(rb-test "chained call with args"
|
||||||
|
(len (get (rb-p-first "obj.foo(1, 2)") :args))
|
||||||
|
2)
|
||||||
|
|
||||||
|
(rb-test "no-paren call"
|
||||||
|
(get (rb-p-first "puts \"hello\"") :type)
|
||||||
|
"send")
|
||||||
|
|
||||||
|
(rb-test "no-paren call name"
|
||||||
|
(get (rb-p-first "puts \"hello\"") :name)
|
||||||
|
"puts")
|
||||||
|
|
||||||
|
(rb-test "no-paren call args"
|
||||||
|
(len (get (rb-p-first "puts \"hello\"") :args))
|
||||||
|
1)
|
||||||
|
|
||||||
|
(rb-test "indexing"
|
||||||
|
(get (rb-p-first "a[0]") :type)
|
||||||
|
"index")
|
||||||
|
|
||||||
|
;; ── Unary operators ───────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
(rb-test "unary not"
|
||||||
|
(rb-p-first "!x")
|
||||||
|
{:type "unop" :op "!"
|
||||||
|
:value {:type "send" :name "x" :args (list) :block nil}})
|
||||||
|
|
||||||
|
(rb-test "unary minus"
|
||||||
|
(get (rb-p-first "-x") :op)
|
||||||
|
"-")
|
||||||
|
|
||||||
|
;; ── Method def ────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
(rb-test "empty method def"
|
||||||
|
(get (rb-p-first "def foo; end") :type)
|
||||||
|
"method-def")
|
||||||
|
|
||||||
|
(rb-test "method def name"
|
||||||
|
(get (rb-p-first "def foo; end") :name)
|
||||||
|
"foo")
|
||||||
|
|
||||||
|
(rb-test "method def no params"
|
||||||
|
(len (get (rb-p-first "def foo; end") :params))
|
||||||
|
0)
|
||||||
|
|
||||||
|
(rb-test "method def with params"
|
||||||
|
(len (get (rb-p-first "def foo(a, b); end") :params))
|
||||||
|
2)
|
||||||
|
|
||||||
|
(rb-test "method def param-req"
|
||||||
|
(get (nth (get (rb-p-first "def foo(a); end") :params) 0) :type)
|
||||||
|
"param-req")
|
||||||
|
|
||||||
|
(rb-test "method def param name"
|
||||||
|
(get (nth (get (rb-p-first "def foo(a); end") :params) 0) :name)
|
||||||
|
"a")
|
||||||
|
|
||||||
|
(rb-test "method def optional param"
|
||||||
|
(get (nth (get (rb-p-first "def foo(a, b=1); end") :params) 1) :type)
|
||||||
|
"param-opt")
|
||||||
|
|
||||||
|
(rb-test "method def splat"
|
||||||
|
(get (nth (get (rb-p-first "def foo(*args); end") :params) 0) :type)
|
||||||
|
"param-rest")
|
||||||
|
|
||||||
|
(rb-test "method def double splat"
|
||||||
|
(get (nth (get (rb-p-first "def foo(**opts); end") :params) 0) :type)
|
||||||
|
"param-kwrest")
|
||||||
|
|
||||||
|
(rb-test "method def block param"
|
||||||
|
(get (nth (get (rb-p-first "def foo(&blk); end") :params) 0) :type)
|
||||||
|
"param-block")
|
||||||
|
|
||||||
|
(rb-test "method def all param types"
|
||||||
|
(len (get (rb-p-first "def foo(a, b=1, *c, **d, &e); end") :params))
|
||||||
|
5)
|
||||||
|
|
||||||
|
(rb-test "method def singleton recv"
|
||||||
|
(get (get (rb-p-first "def self.bar; end") :recv) :type)
|
||||||
|
"self")
|
||||||
|
|
||||||
|
(rb-test "method def body"
|
||||||
|
(len (get (rb-p-first "def foo; 1; 2; end") :body))
|
||||||
|
2)
|
||||||
|
|
||||||
|
;; ── Class def ────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
(rb-test "class def type"
|
||||||
|
(get (rb-p-first "class Foo; end") :type)
|
||||||
|
"class-def")
|
||||||
|
|
||||||
|
(rb-test "class def name"
|
||||||
|
(get (get (rb-p-first "class Foo; end") :name) :name)
|
||||||
|
"Foo")
|
||||||
|
|
||||||
|
(rb-test "class def no super"
|
||||||
|
(nil? (get (rb-p-first "class Foo; end") :super))
|
||||||
|
true)
|
||||||
|
|
||||||
|
(rb-test "class def with super"
|
||||||
|
(get (get (rb-p-first "class Foo < Bar; end") :super) :name)
|
||||||
|
"Bar")
|
||||||
|
|
||||||
|
(rb-test "singleton class"
|
||||||
|
(get (rb-p-first "class << self; end") :type)
|
||||||
|
"sclass")
|
||||||
|
|
||||||
|
;; ── Module def ────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
(rb-test "module def type"
|
||||||
|
(get (rb-p-first "module M; end") :type)
|
||||||
|
"module-def")
|
||||||
|
|
||||||
|
(rb-test "module def name"
|
||||||
|
(get (get (rb-p-first "module M; end") :name) :name)
|
||||||
|
"M")
|
||||||
|
|
||||||
|
;; ── Blocks ────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
(rb-test "block do...end"
|
||||||
|
(get (get (rb-p-first "foo do |x| x end") :block) :type)
|
||||||
|
"block")
|
||||||
|
|
||||||
|
(rb-test "block brace"
|
||||||
|
(get (get (rb-p-first "foo { |x| x }") :block) :type)
|
||||||
|
"block")
|
||||||
|
|
||||||
|
(rb-test "block params"
|
||||||
|
(len (get (get (rb-p-first "foo { |a, b| a }") :block) :params))
|
||||||
|
2)
|
||||||
|
|
||||||
|
(rb-test "block no params"
|
||||||
|
(len (get (get (rb-p-first "foo { 42 }") :block) :params))
|
||||||
|
0)
|
||||||
|
|
||||||
|
;; ── Control flow ──────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
(rb-test "return type"
|
||||||
|
(get (rb-p-first "return 1") :type)
|
||||||
|
"return")
|
||||||
|
|
||||||
|
(rb-test "return value"
|
||||||
|
(get (get (rb-p-first "return 1") :value) :value)
|
||||||
|
1)
|
||||||
|
|
||||||
|
(rb-test "return nil"
|
||||||
|
(nil? (get (rb-p-first "return") :value))
|
||||||
|
true)
|
||||||
|
|
||||||
|
(rb-test "yield type"
|
||||||
|
(get (rb-p-first "yield 1") :type)
|
||||||
|
"yield")
|
||||||
|
|
||||||
|
(rb-test "break type"
|
||||||
|
(get (rb-p-first "break") :type)
|
||||||
|
"break")
|
||||||
|
|
||||||
|
(rb-test "next type"
|
||||||
|
(get (rb-p-first "next") :type)
|
||||||
|
"next")
|
||||||
|
|
||||||
|
(rb-test "redo type"
|
||||||
|
(get (rb-p-first "redo") :type)
|
||||||
|
"redo")
|
||||||
|
|
||||||
|
;; ── Multi-statement program ───────────────────────────────────────────────────
|
||||||
|
|
||||||
|
(rb-test "two statements"
|
||||||
|
(len (rb-p-stmts "1\n2"))
|
||||||
|
2)
|
||||||
|
|
||||||
|
(rb-test "semi-separated"
|
||||||
|
(len (rb-p-stmts "1; 2; 3"))
|
||||||
|
3)
|
||||||
|
|
||||||
|
(rb-test "class with method"
|
||||||
|
(let ((cls (rb-p-first "class Foo\n def bar\n 1\n end\nend")))
|
||||||
|
(len (get cls :body)))
|
||||||
|
1)
|
||||||
|
|
||||||
|
(list rb-test-pass rb-test-fail)
|
||||||
210
lib/ruby/tests/tokenizer.sx
Normal file
210
lib/ruby/tests/tokenizer.sx
Normal file
@@ -0,0 +1,210 @@
|
|||||||
|
;; Ruby tokenizer tests.
|
||||||
|
;; Final value: {:pass N :fail N :fails (list)}
|
||||||
|
|
||||||
|
(define rb-deep=?
|
||||||
|
(fn (a b)
|
||||||
|
(cond
|
||||||
|
((= a b) true)
|
||||||
|
((and (dict? a) (dict? b))
|
||||||
|
(let ((ak (keys a)) (bk (keys b)))
|
||||||
|
(if (not (= (len ak) (len bk)))
|
||||||
|
false
|
||||||
|
(every?
|
||||||
|
(fn (k) (and (has-key? b k) (rb-deep=? (get a k) (get b k))))
|
||||||
|
ak))))
|
||||||
|
((and (list? a) (list? b))
|
||||||
|
(if (not (= (len a) (len b)))
|
||||||
|
false
|
||||||
|
(let ((i 0) (ok true))
|
||||||
|
(define rb-de-loop
|
||||||
|
(fn ()
|
||||||
|
(when (and ok (< i (len a)))
|
||||||
|
(do
|
||||||
|
(when (not (rb-deep=? (nth a i) (nth b i)))
|
||||||
|
(set! ok false))
|
||||||
|
(set! i (+ i 1))
|
||||||
|
(rb-de-loop)))))
|
||||||
|
(rb-de-loop)
|
||||||
|
ok)))
|
||||||
|
(:else false))))
|
||||||
|
|
||||||
|
(define rb-test-pass 0)
|
||||||
|
(define rb-test-fail 0)
|
||||||
|
(define rb-test-fails (list))
|
||||||
|
|
||||||
|
(define rb-test
|
||||||
|
(fn (name actual expected)
|
||||||
|
(if (rb-deep=? actual expected)
|
||||||
|
(set! rb-test-pass (+ rb-test-pass 1))
|
||||||
|
(do
|
||||||
|
(set! rb-test-fail (+ rb-test-fail 1))
|
||||||
|
(append! rb-test-fails {:name name :actual actual :expected expected})))))
|
||||||
|
|
||||||
|
;; Helper: tokenize, drop newline+eof, return {:type :value} pairs
|
||||||
|
(define rb-toks
|
||||||
|
(fn (src)
|
||||||
|
(map
|
||||||
|
(fn (tok) {:value (get tok "value") :type (get tok "type")})
|
||||||
|
(filter
|
||||||
|
(fn (tok)
|
||||||
|
(let ((ty (get tok "type")))
|
||||||
|
(not (or (= ty "newline") (= ty "eof")))))
|
||||||
|
(rb-tokenize src)))))
|
||||||
|
|
||||||
|
;; Helper: get just types
|
||||||
|
(define rb-types
|
||||||
|
(fn (src) (map (fn (t) (get t "type")) (rb-toks src))))
|
||||||
|
|
||||||
|
;; Helper: get first token type
|
||||||
|
(define rb-first-type
|
||||||
|
(fn (src) (get (get (rb-tokenize src) 0) "type")))
|
||||||
|
|
||||||
|
(define rb-first-value
|
||||||
|
(fn (src) (get (get (rb-tokenize src) 0) "value")))
|
||||||
|
|
||||||
|
;; ── 1. Keywords ────────────────────────<E29480><E29480><EFBFBD>─────────────────────────
|
||||||
|
(rb-test "keyword def" (rb-toks "def") (list {:value "def" :type "keyword"}))
|
||||||
|
(rb-test "keyword end" (rb-toks "end") (list {:value "end" :type "keyword"}))
|
||||||
|
(rb-test "keyword class" (rb-toks "class") (list {:value "class" :type "keyword"}))
|
||||||
|
(rb-test "keyword if" (rb-toks "if") (list {:value "if" :type "keyword"}))
|
||||||
|
(rb-test "keyword while" (rb-toks "while") (list {:value "while" :type "keyword"}))
|
||||||
|
(rb-test "keyword nil" (rb-toks "nil") (list {:value "nil" :type "keyword"}))
|
||||||
|
(rb-test "keyword true" (rb-toks "true") (list {:value "true" :type "keyword"}))
|
||||||
|
(rb-test "keyword false" (rb-toks "false") (list {:value "false" :type "keyword"}))
|
||||||
|
(rb-test "keyword return" (rb-toks "return") (list {:value "return" :type "keyword"}))
|
||||||
|
(rb-test "keyword yield" (rb-toks "yield") (list {:value "yield" :type "keyword"}))
|
||||||
|
(rb-test "keyword begin" (rb-toks "begin") (list {:value "begin" :type "keyword"}))
|
||||||
|
(rb-test "keyword rescue" (rb-toks "rescue") (list {:value "rescue" :type "keyword"}))
|
||||||
|
(rb-test "keyword self" (rb-toks "self") (list {:value "self" :type "keyword"}))
|
||||||
|
(rb-test "keyword super" (rb-toks "super") (list {:value "super" :type "keyword"}))
|
||||||
|
|
||||||
|
;; ── 2. Identifiers ────────────────────────────────────────────────
|
||||||
|
(rb-test "ident simple" (rb-toks "foo") (list {:value "foo" :type "ident"}))
|
||||||
|
(rb-test "ident underscore" (rb-toks "_foo") (list {:value "_foo" :type "ident"}))
|
||||||
|
(rb-test "ident with digit" (rb-toks "foo2") (list {:value "foo2" :type "ident"}))
|
||||||
|
(rb-test "ident predicate" (rb-toks "empty?") (list {:value "empty?" :type "ident"}))
|
||||||
|
(rb-test "ident bang" (rb-toks "save!") (list {:value "save!" :type "ident"}))
|
||||||
|
(rb-test "defined?" (rb-toks "defined?") (list {:value "defined?" :type "keyword"}))
|
||||||
|
|
||||||
|
;; ── 3. Constants ──────────────────────────────────────────────────
|
||||||
|
(rb-test "const simple" (rb-toks "Foo") (list {:value "Foo" :type "const"}))
|
||||||
|
(rb-test "const upcase" (rb-toks "MY_CONST") (list {:value "MY_CONST" :type "const"}))
|
||||||
|
(rb-test "const class" (rb-toks "String") (list {:value "String" :type "const"}))
|
||||||
|
|
||||||
|
;; ── 4. Sigil variables ───────────────────────────────────────────
|
||||||
|
(rb-test "ivar" (rb-toks "@name") (list {:value "@name" :type "ivar"}))
|
||||||
|
(rb-test "cvar" (rb-toks "@@count") (list {:value "@@count" :type "cvar"}))
|
||||||
|
(rb-test "gvar" (rb-toks "$global") (list {:value "$global" :type "gvar"}))
|
||||||
|
|
||||||
|
;; ── 5. Integers ───────────────────────────────────────────────────
|
||||||
|
(rb-test "int decimal" (rb-first-value "42") 42)
|
||||||
|
(rb-test "int zero" (rb-first-value "0") 0)
|
||||||
|
(rb-test "int underscore" (rb-first-value "1_000") 1000)
|
||||||
|
(rb-test "int hex" (rb-first-value "0xFF") 255)
|
||||||
|
(rb-test "int hex lower" (rb-first-value "0xff") 255)
|
||||||
|
(rb-test "int octal" (rb-first-value "0o17") 15)
|
||||||
|
(rb-test "int binary" (rb-first-value "0b1010") 10)
|
||||||
|
(rb-test "int type" (rb-first-type "42") "int")
|
||||||
|
|
||||||
|
;; ── 6. Floats ─────────────────────────────────────────────────────
|
||||||
|
(rb-test "float simple" (rb-first-type "3.14") "float")
|
||||||
|
(rb-test "float value" (rb-first-value "3.14") "3.14")
|
||||||
|
(rb-test "float exp" (rb-first-type "1.5e10") "float")
|
||||||
|
(rb-test "float exp value" (rb-first-value "1.5e10") "1.5e10")
|
||||||
|
|
||||||
|
;; ── 7. Strings ────────────────────────────────────────────────────
|
||||||
|
(rb-test "dq string" (rb-first-value "\"hello\"") "hello")
|
||||||
|
(rb-test "dq string type" (rb-first-type "\"hello\"") "string")
|
||||||
|
(rb-test "sq string" (rb-first-value "'world'") "world")
|
||||||
|
(rb-test "dq escape nl" (rb-first-value "\"a\\nb\"") "a\nb")
|
||||||
|
(rb-test "dq escape tab" (rb-first-value "\"a\\tb\"") "a\tb")
|
||||||
|
(rb-test "dq escape quote" (rb-first-value "\"a\\\"b\"") "a\"b")
|
||||||
|
(rb-test "sq no escape" (rb-first-value "'a\\nb'") "a\\nb")
|
||||||
|
(rb-test "sq escape backslash" (rb-first-value "'a\\\\'") "a\\")
|
||||||
|
(rb-test "dq interp kept" (rb-first-value "\"#{x}\"") "#{x}")
|
||||||
|
|
||||||
|
;; ── 8. Symbols ────────────────────────────────────────────────────
|
||||||
|
(rb-test "symbol simple" (rb-first-type ":foo") "symbol")
|
||||||
|
(rb-test "symbol value" (rb-first-value ":foo") "foo")
|
||||||
|
(rb-test "symbol predicate" (rb-first-value ":empty?") "empty?")
|
||||||
|
(rb-test "symbol dq" (rb-first-value ":\"hello world\"") "hello world")
|
||||||
|
(rb-test "symbol sq" (rb-first-value ":'hello'") "hello")
|
||||||
|
|
||||||
|
;; ── 9. %w and %i literals ────────────────────────────────────────
|
||||||
|
(rb-test "%w bracket" (rb-first-type "%w[a b c]") "words")
|
||||||
|
(rb-test "%w value" (rb-first-value "%w[a b c]") (list "a" "b" "c"))
|
||||||
|
(rb-test "%w paren" (rb-first-value "%w(x y)") (list "x" "y"))
|
||||||
|
(rb-test "%i bracket" (rb-first-type "%i[a b]") "isymbols")
|
||||||
|
(rb-test "%i value" (rb-first-value "%i[foo bar]") (list "foo" "bar"))
|
||||||
|
|
||||||
|
;; ── 10. Punctuation ───────────────────────────────────────────────
|
||||||
|
(rb-test "dot" (rb-first-type ".") "dot")
|
||||||
|
(rb-test "dotdot" (rb-first-type "..") "dotdot")
|
||||||
|
(rb-test "dotdotdot" (rb-first-type "...") "dotdotdot")
|
||||||
|
(rb-test "dcolon" (rb-first-type "::") "dcolon")
|
||||||
|
(rb-test "comma" (rb-first-type ",") "comma")
|
||||||
|
(rb-test "semi" (rb-first-type ";") "semi")
|
||||||
|
(rb-test "lparen" (rb-first-type "(") "lparen")
|
||||||
|
(rb-test "rparen" (rb-first-type ")") "rparen")
|
||||||
|
(rb-test "lbracket" (rb-first-type "[") "lbracket")
|
||||||
|
(rb-test "rbracket" (rb-first-type "]") "rbracket")
|
||||||
|
(rb-test "lbrace" (rb-first-type "{") "lbrace")
|
||||||
|
(rb-test "rbrace" (rb-first-type "}") "rbrace")
|
||||||
|
(rb-test "pipe" (rb-first-type "|") "pipe")
|
||||||
|
|
||||||
|
;; ── 11. Operators ─────────────────────────────────────────────────
|
||||||
|
(rb-test "op plus" (rb-first-value "+") "+")
|
||||||
|
(rb-test "op minus" (rb-first-value "-") "-")
|
||||||
|
(rb-test "op star" (rb-first-value "*") "*")
|
||||||
|
(rb-test "op slash" (rb-first-value "/") "/")
|
||||||
|
(rb-test "op eq" (rb-first-value "=") "=")
|
||||||
|
(rb-test "op eqeq" (rb-first-value "==") "==")
|
||||||
|
(rb-test "op neq" (rb-first-value "!=") "!=")
|
||||||
|
(rb-test "op lt" (rb-first-value "<") "<")
|
||||||
|
(rb-test "op gt" (rb-first-value ">") ">")
|
||||||
|
(rb-test "op lte" (rb-first-value "<=") "<=")
|
||||||
|
(rb-test "op gte" (rb-first-value ">=") ">=")
|
||||||
|
(rb-test "op spaceship" (rb-first-value "<=>") "<=>")
|
||||||
|
(rb-test "op tripleq" (rb-first-value "===") "===")
|
||||||
|
(rb-test "op match" (rb-first-value "=~") "=~")
|
||||||
|
(rb-test "op nomatch" (rb-first-value "!~") "!~")
|
||||||
|
(rb-test "op lshift" (rb-first-value "<<") "<<")
|
||||||
|
(rb-test "op rshift" (rb-first-value ">>") ">>")
|
||||||
|
(rb-test "op and" (rb-first-value "&&") "&&")
|
||||||
|
(rb-test "op or" (rb-first-value "||") "||")
|
||||||
|
(rb-test "op power" (rb-first-value "**") "**")
|
||||||
|
(rb-test "op plus-eq" (rb-first-value "+=") "+=")
|
||||||
|
(rb-test "op minus-eq" (rb-first-value "-=") "-=")
|
||||||
|
(rb-test "op arrow" (rb-first-value "->") "->")
|
||||||
|
(rb-test "op hash-rocket" (rb-first-value "=>") "=>")
|
||||||
|
|
||||||
|
;; ── 12. Comments ──────────────────────────────────────────────────
|
||||||
|
(rb-test "comment skipped" (len (rb-toks "# this is a comment")) 0)
|
||||||
|
(rb-test "comment mid-line" (rb-types "x = 1 # comment") (list "ident" "op" "int"))
|
||||||
|
|
||||||
|
;; ── 13. Multi-token sequences ─────────────────────────────────────
|
||||||
|
(rb-test "method call" (rb-types "foo.bar")
|
||||||
|
(list "ident" "dot" "ident"))
|
||||||
|
(rb-test "class def" (rb-types "class Foo")
|
||||||
|
(list "keyword" "const"))
|
||||||
|
(rb-test "method def" (rb-types "def greet(name)")
|
||||||
|
(list "keyword" "ident" "lparen" "ident" "rparen"))
|
||||||
|
(rb-test "assignment" (rb-types "x = 42")
|
||||||
|
(list "ident" "op" "int"))
|
||||||
|
(rb-test "block params" (rb-types "|x, y|")
|
||||||
|
(list "pipe" "ident" "comma" "ident" "pipe"))
|
||||||
|
(rb-test "scope resolution" (rb-types "Foo::Bar")
|
||||||
|
(list "const" "dcolon" "const"))
|
||||||
|
(rb-test "range" (rb-types "1..10")
|
||||||
|
(list "int" "dotdot" "int"))
|
||||||
|
(rb-test "exclusive range" (rb-types "1...10")
|
||||||
|
(list "int" "dotdotdot" "int"))
|
||||||
|
|
||||||
|
;; ── 14. Line/col tracking ────────────────────────────────────────
|
||||||
|
(define rb-tok1 (get (rb-tokenize "hello\nworld") 0))
|
||||||
|
(define rb-tok2 (get (rb-tokenize "hello\nworld") 2))
|
||||||
|
(rb-test "line track start" (get rb-tok1 "line") 1)
|
||||||
|
(rb-test "line track second" (get rb-tok2 "line") 2)
|
||||||
|
(rb-test "col track start" (get rb-tok1 "col") 1)
|
||||||
|
|
||||||
|
(list rb-test-pass rb-test-fail)
|
||||||
549
lib/ruby/tokenizer.sx
Normal file
549
lib/ruby/tokenizer.sx
Normal file
@@ -0,0 +1,549 @@
|
|||||||
|
;; Ruby tokenizer for Ruby 2.7 subset.
|
||||||
|
;; Token: {:type T :value V :line L :col C}
|
||||||
|
;;
|
||||||
|
;; Types: keyword ident ivar cvar gvar const
|
||||||
|
;; int float string symbol
|
||||||
|
;; op dot dotdot dotdotdot dcolon colon
|
||||||
|
;; lparen rparen lbracket rbracket lbrace rbrace
|
||||||
|
;; comma semi pipe newline words isymbols eof
|
||||||
|
|
||||||
|
;; ── Character code table ──────────────────────────────────────────
|
||||||
|
(define rb-ord-table
|
||||||
|
(let ((t (dict)) (i 0))
|
||||||
|
(define rb-build-table
|
||||||
|
(fn ()
|
||||||
|
(when (< i 128)
|
||||||
|
(do
|
||||||
|
(dict-set! t (char-from-code i) i)
|
||||||
|
(set! i (+ i 1))
|
||||||
|
(rb-build-table)))))
|
||||||
|
(rb-build-table)
|
||||||
|
t))
|
||||||
|
|
||||||
|
(define rb-ord (fn (c) (or (get rb-ord-table c) 0)))
|
||||||
|
|
||||||
|
;; ── Character predicates ──────────────────────────────────────────
|
||||||
|
(define rb-digit?
|
||||||
|
(fn (c) (and (string? c) (>= (rb-ord c) 48) (<= (rb-ord c) 57))))
|
||||||
|
|
||||||
|
(define rb-hex-digit?
|
||||||
|
(fn (c)
|
||||||
|
(and (string? c)
|
||||||
|
(or (and (>= (rb-ord c) 48) (<= (rb-ord c) 57))
|
||||||
|
(and (>= (rb-ord c) 97) (<= (rb-ord c) 102))
|
||||||
|
(and (>= (rb-ord c) 65) (<= (rb-ord c) 70))))))
|
||||||
|
|
||||||
|
(define rb-octal-digit?
|
||||||
|
(fn (c) (and (string? c) (>= (rb-ord c) 48) (<= (rb-ord c) 55))))
|
||||||
|
|
||||||
|
(define rb-binary-digit? (fn (c) (or (= c "0") (= c "1"))))
|
||||||
|
|
||||||
|
(define rb-lower?
|
||||||
|
(fn (c) (and (string? c) (>= (rb-ord c) 97) (<= (rb-ord c) 122))))
|
||||||
|
|
||||||
|
(define rb-upper?
|
||||||
|
(fn (c) (and (string? c) (>= (rb-ord c) 65) (<= (rb-ord c) 90))))
|
||||||
|
|
||||||
|
(define rb-ident-start?
|
||||||
|
(fn (c) (or (rb-lower? c) (rb-upper? c) (= c "_"))))
|
||||||
|
|
||||||
|
(define rb-ident-cont?
|
||||||
|
(fn (c) (or (rb-lower? c) (rb-upper? c) (rb-digit? c) (= c "_"))))
|
||||||
|
|
||||||
|
(define rb-space? (fn (c) (or (= c " ") (= c "\t") (= c "\r"))))
|
||||||
|
|
||||||
|
;; ── Reserved words ────────────────────────────────────────────────
|
||||||
|
(define rb-keywords
|
||||||
|
(list "__ENCODING__" "__LINE__" "__FILE__"
|
||||||
|
"BEGIN" "END"
|
||||||
|
"alias" "and"
|
||||||
|
"begin" "break"
|
||||||
|
"case" "class"
|
||||||
|
"def" "defined?" "do"
|
||||||
|
"else" "elsif" "end" "ensure"
|
||||||
|
"false" "for"
|
||||||
|
"if" "in"
|
||||||
|
"module"
|
||||||
|
"next" "nil" "not"
|
||||||
|
"or"
|
||||||
|
"redo" "rescue" "retry" "return"
|
||||||
|
"self" "super"
|
||||||
|
"then" "true"
|
||||||
|
"undef" "unless" "until"
|
||||||
|
"when" "while"
|
||||||
|
"yield"))
|
||||||
|
|
||||||
|
(define rb-keyword? (fn (w) (contains? rb-keywords w)))
|
||||||
|
|
||||||
|
;; ── Token constructor ─────────────────────────────────────────────
|
||||||
|
(define rb-make-token
|
||||||
|
(fn (type value line col) {:type type :value value :line line :col col}))
|
||||||
|
|
||||||
|
;; ── Radix number parser ───────────────────────────────────────────
|
||||||
|
(define rb-parse-radix
|
||||||
|
(fn (s radix)
|
||||||
|
(let ((n (len s)) (i 0) (acc 0))
|
||||||
|
(define rb-rad-loop
|
||||||
|
(fn ()
|
||||||
|
(when (< i n)
|
||||||
|
(do
|
||||||
|
(let ((c (substring s i (+ i 1))))
|
||||||
|
(cond
|
||||||
|
((and (>= (rb-ord c) 48) (<= (rb-ord c) 57))
|
||||||
|
(set! acc (+ (* acc radix) (- (rb-ord c) 48))))
|
||||||
|
((and (>= (rb-ord c) 97) (<= (rb-ord c) 102))
|
||||||
|
(set! acc (+ (* acc radix) (+ 10 (- (rb-ord c) 97)))))
|
||||||
|
((and (>= (rb-ord c) 65) (<= (rb-ord c) 70))
|
||||||
|
(set! acc (+ (* acc radix) (+ 10 (- (rb-ord c) 65)))))))
|
||||||
|
(set! i (+ i 1))
|
||||||
|
(rb-rad-loop)))))
|
||||||
|
(rb-rad-loop)
|
||||||
|
acc)))
|
||||||
|
|
||||||
|
;; ── Strip underscores from numeric literals ───────────────────────
|
||||||
|
(define rb-strip-underscores
|
||||||
|
(fn (s)
|
||||||
|
(let ((n (len s)) (i 0) (parts (list)))
|
||||||
|
(define rb-su-loop
|
||||||
|
(fn ()
|
||||||
|
(when (< i n)
|
||||||
|
(do
|
||||||
|
(let ((c (substring s i (+ i 1))))
|
||||||
|
(when (not (= c "_"))
|
||||||
|
(append! parts c)))
|
||||||
|
(set! i (+ i 1))
|
||||||
|
(rb-su-loop)))))
|
||||||
|
(rb-su-loop)
|
||||||
|
(join "" parts))))
|
||||||
|
|
||||||
|
;; ── Main tokenizer ────────────────────────────────────────────────
|
||||||
|
(define rb-tokenize
|
||||||
|
(fn (src)
|
||||||
|
(let ((tokens (list))
|
||||||
|
(pos 0)
|
||||||
|
(line 1)
|
||||||
|
(col 1)
|
||||||
|
(src-len (len src)))
|
||||||
|
(define rb-peek
|
||||||
|
(fn (offset)
|
||||||
|
(if (< (+ pos offset) src-len)
|
||||||
|
(substring src (+ pos offset) (+ pos offset 1))
|
||||||
|
nil)))
|
||||||
|
(define rb-cur (fn () (rb-peek 0)))
|
||||||
|
(define rb-advance!
|
||||||
|
(fn ()
|
||||||
|
(let ((c (rb-cur)))
|
||||||
|
(set! pos (+ pos 1))
|
||||||
|
(if (= c "\n")
|
||||||
|
(do (set! line (+ line 1)) (set! col 1))
|
||||||
|
(set! col (+ col 1))))))
|
||||||
|
(define rb-advance-n!
|
||||||
|
(fn (n)
|
||||||
|
(when (> n 0)
|
||||||
|
(do (rb-advance!) (rb-advance-n! (- n 1))))))
|
||||||
|
(define rb-push!
|
||||||
|
(fn (type value tok-line tok-col)
|
||||||
|
(append! tokens (rb-make-token type value tok-line tok-col))))
|
||||||
|
(define rb-read-while
|
||||||
|
(fn (pred)
|
||||||
|
(let ((start pos))
|
||||||
|
(define rb-rw-loop
|
||||||
|
(fn ()
|
||||||
|
(when (and (< pos src-len) (pred (rb-cur)))
|
||||||
|
(do (rb-advance!) (rb-rw-loop)))))
|
||||||
|
(rb-rw-loop)
|
||||||
|
(substring src start pos))))
|
||||||
|
(define rb-skip-line-comment!
|
||||||
|
(fn ()
|
||||||
|
(define rb-slc-loop
|
||||||
|
(fn ()
|
||||||
|
(when (and (< pos src-len) (not (= (rb-cur) "\n")))
|
||||||
|
(do (rb-advance!) (rb-slc-loop)))))
|
||||||
|
(rb-slc-loop)))
|
||||||
|
(define rb-read-escape
|
||||||
|
(fn ()
|
||||||
|
(rb-advance!)
|
||||||
|
(let ((c (rb-cur)))
|
||||||
|
(cond
|
||||||
|
((= c "n") (do (rb-advance!) "\n"))
|
||||||
|
((= c "t") (do (rb-advance!) "\t"))
|
||||||
|
((= c "r") (do (rb-advance!) "\r"))
|
||||||
|
((= c "\\") (do (rb-advance!) "\\"))
|
||||||
|
((= c "'") (do (rb-advance!) "'"))
|
||||||
|
((= c "\"") (do (rb-advance!) "\""))
|
||||||
|
((= c "a") (do (rb-advance!) (char-from-code 7)))
|
||||||
|
((= c "b") (do (rb-advance!) (char-from-code 8)))
|
||||||
|
((= c "f") (do (rb-advance!) (char-from-code 12)))
|
||||||
|
((= c "v") (do (rb-advance!) (char-from-code 11)))
|
||||||
|
((= c "e") (do (rb-advance!) (char-from-code 27)))
|
||||||
|
((= c "s") (do (rb-advance!) " "))
|
||||||
|
((= c "0") (do (rb-advance!) (char-from-code 0)))
|
||||||
|
(:else (do (rb-advance!) (str "\\" c)))))))
|
||||||
|
(define rb-read-sq-string
|
||||||
|
(fn ()
|
||||||
|
(let ((parts (list)))
|
||||||
|
(rb-advance!)
|
||||||
|
(define rb-sq-loop
|
||||||
|
(fn ()
|
||||||
|
(cond
|
||||||
|
((>= pos src-len) nil)
|
||||||
|
((= (rb-cur) "'") (rb-advance!))
|
||||||
|
((and (= (rb-cur) "\\")
|
||||||
|
(let ((n (rb-peek 1)))
|
||||||
|
(or (= n "\\") (= n "'"))))
|
||||||
|
(do
|
||||||
|
(rb-advance!)
|
||||||
|
(append! parts (rb-cur))
|
||||||
|
(rb-advance!)
|
||||||
|
(rb-sq-loop)))
|
||||||
|
(:else
|
||||||
|
(do
|
||||||
|
(append! parts (rb-cur))
|
||||||
|
(rb-advance!)
|
||||||
|
(rb-sq-loop))))))
|
||||||
|
(rb-sq-loop)
|
||||||
|
(join "" parts))))
|
||||||
|
(define rb-read-dq-string
|
||||||
|
(fn ()
|
||||||
|
(let ((parts (list)))
|
||||||
|
(rb-advance!)
|
||||||
|
(define rb-dq-loop
|
||||||
|
(fn ()
|
||||||
|
(cond
|
||||||
|
((>= pos src-len) nil)
|
||||||
|
((= (rb-cur) "\"") (rb-advance!))
|
||||||
|
((= (rb-cur) "\\")
|
||||||
|
(do
|
||||||
|
(append! parts (rb-read-escape))
|
||||||
|
(rb-dq-loop)))
|
||||||
|
((and (= (rb-cur) "#") (= (rb-peek 1) "{"))
|
||||||
|
(do
|
||||||
|
(append! parts "#{")
|
||||||
|
(rb-advance-n! 2)
|
||||||
|
(let ((depth 1))
|
||||||
|
(define rb-interp-inner
|
||||||
|
(fn ()
|
||||||
|
(when (and (< pos src-len) (> depth 0))
|
||||||
|
(do
|
||||||
|
(let ((c (rb-cur)))
|
||||||
|
(cond
|
||||||
|
((= c "{")
|
||||||
|
(do
|
||||||
|
(set! depth (+ depth 1))
|
||||||
|
(append! parts c)
|
||||||
|
(rb-advance!)))
|
||||||
|
((= c "}")
|
||||||
|
(do
|
||||||
|
(set! depth (- depth 1))
|
||||||
|
(when (> depth 0)
|
||||||
|
(do (append! parts c) (rb-advance!)))))
|
||||||
|
(:else
|
||||||
|
(do (append! parts c) (rb-advance!)))))
|
||||||
|
(rb-interp-inner)))))
|
||||||
|
(rb-interp-inner))
|
||||||
|
(when (= (rb-cur) "}")
|
||||||
|
(do (append! parts "}") (rb-advance!)))
|
||||||
|
(rb-dq-loop)))
|
||||||
|
(:else
|
||||||
|
(do
|
||||||
|
(append! parts (rb-cur))
|
||||||
|
(rb-advance!)
|
||||||
|
(rb-dq-loop))))))
|
||||||
|
(rb-dq-loop)
|
||||||
|
(join "" parts))))
|
||||||
|
(define rb-read-percent-words
|
||||||
|
(fn ()
|
||||||
|
(rb-advance-n! 2)
|
||||||
|
(let ((open-ch (rb-cur)))
|
||||||
|
(let ((close-ch
|
||||||
|
(cond
|
||||||
|
((= open-ch "[") "]")
|
||||||
|
((= open-ch "(") ")")
|
||||||
|
((= open-ch "{") "}")
|
||||||
|
((= open-ch "<") ">")
|
||||||
|
(:else open-ch))))
|
||||||
|
(rb-advance!)
|
||||||
|
(let ((items (list)))
|
||||||
|
(define rb-pw-skip
|
||||||
|
(fn ()
|
||||||
|
(when (and (< pos src-len) (or (rb-space? (rb-cur)) (= (rb-cur) "\n")))
|
||||||
|
(do (rb-advance!) (rb-pw-skip)))))
|
||||||
|
(define rb-pw-word
|
||||||
|
(fn (wparts)
|
||||||
|
(if (or (>= pos src-len)
|
||||||
|
(rb-space? (rb-cur))
|
||||||
|
(= (rb-cur) "\n")
|
||||||
|
(= (rb-cur) close-ch))
|
||||||
|
(append! items (join "" wparts))
|
||||||
|
(do
|
||||||
|
(append! wparts (rb-cur))
|
||||||
|
(rb-advance!)
|
||||||
|
(rb-pw-word wparts)))))
|
||||||
|
(define rb-pw-loop
|
||||||
|
(fn ()
|
||||||
|
(rb-pw-skip)
|
||||||
|
(when (and (< pos src-len) (not (= (rb-cur) close-ch)))
|
||||||
|
(do
|
||||||
|
(rb-pw-word (list))
|
||||||
|
(rb-pw-loop)))))
|
||||||
|
(rb-pw-loop)
|
||||||
|
(when (= (rb-cur) close-ch) (rb-advance!))
|
||||||
|
items)))))
|
||||||
|
(define rb-read-ident-word
|
||||||
|
(fn ()
|
||||||
|
(let ((start pos))
|
||||||
|
(rb-read-while rb-ident-cont?)
|
||||||
|
(when (and (= (rb-cur) "?") (not (= (rb-peek 1) "=")))
|
||||||
|
(rb-advance!))
|
||||||
|
(when (and (= (rb-cur) "!") (not (or (= (rb-peek 1) "=") (= (rb-peek 1) "~"))))
|
||||||
|
(rb-advance!))
|
||||||
|
(substring src start pos))))
|
||||||
|
(define rb-read-number!
|
||||||
|
(fn (tok-line tok-col)
|
||||||
|
(let ((start pos))
|
||||||
|
(cond
|
||||||
|
((and (= (rb-cur) "0") (let ((p (rb-peek 1))) (or (= p "b") (= p "B"))))
|
||||||
|
(do
|
||||||
|
(rb-advance-n! 2)
|
||||||
|
(let ((bin-str (rb-read-while rb-binary-digit?)))
|
||||||
|
(rb-push! "int" (rb-parse-radix bin-str 2) tok-line tok-col))))
|
||||||
|
((and (= (rb-cur) "0") (let ((p (rb-peek 1))) (or (= p "o") (= p "O"))))
|
||||||
|
(do
|
||||||
|
(rb-advance-n! 2)
|
||||||
|
(let ((oct-str (rb-read-while rb-octal-digit?)))
|
||||||
|
(rb-push! "int" (rb-parse-radix oct-str 8) tok-line tok-col))))
|
||||||
|
((and (= (rb-cur) "0") (let ((p (rb-peek 1))) (or (= p "x") (= p "X"))))
|
||||||
|
(do
|
||||||
|
(rb-advance-n! 2)
|
||||||
|
(let ((hex-str (rb-read-while rb-hex-digit?)))
|
||||||
|
(rb-push! "int" (rb-parse-radix hex-str 16) tok-line tok-col))))
|
||||||
|
(:else
|
||||||
|
(do
|
||||||
|
(rb-read-while (fn (c) (or (rb-digit? c) (= c "_"))))
|
||||||
|
(let ((is-float false))
|
||||||
|
(when (and (= (rb-cur) ".") (rb-digit? (rb-peek 1)))
|
||||||
|
(do
|
||||||
|
(set! is-float true)
|
||||||
|
(rb-advance!)
|
||||||
|
(rb-read-while (fn (c) (or (rb-digit? c) (= c "_"))))))
|
||||||
|
(when (or (= (rb-cur) "e") (= (rb-cur) "E"))
|
||||||
|
(do
|
||||||
|
(set! is-float true)
|
||||||
|
(rb-advance!)
|
||||||
|
(when (or (= (rb-cur) "+") (= (rb-cur) "-"))
|
||||||
|
(rb-advance!))
|
||||||
|
(rb-read-while rb-digit?)))
|
||||||
|
(let ((num-str (rb-strip-underscores (substring src start pos))))
|
||||||
|
(if is-float
|
||||||
|
(rb-push! "float" num-str tok-line tok-col)
|
||||||
|
(rb-push! "int" (parse-int num-str) tok-line tok-col))))))))))
|
||||||
|
(define rb-read-op!
|
||||||
|
(fn (tok-line tok-col)
|
||||||
|
(let ((c0 (rb-cur)) (c1 (rb-peek 1)) (c2 (rb-peek 2)))
|
||||||
|
(cond
|
||||||
|
((and (= c0 "<") (= c1 "=") (= c2 ">"))
|
||||||
|
(do (rb-advance-n! 3) (rb-push! "op" "<=>" tok-line tok-col)))
|
||||||
|
((and (= c0 "=") (= c1 "=") (= c2 "="))
|
||||||
|
(do (rb-advance-n! 3) (rb-push! "op" "===" tok-line tok-col)))
|
||||||
|
((and (= c0 "*") (= c1 "*") (= c2 "="))
|
||||||
|
(do (rb-advance-n! 3) (rb-push! "op" "**=" tok-line tok-col)))
|
||||||
|
((and (= c0 "<") (= c1 "<") (= c2 "="))
|
||||||
|
(do (rb-advance-n! 3) (rb-push! "op" "<<=" tok-line tok-col)))
|
||||||
|
((and (= c0 ">") (= c1 ">") (= c2 "="))
|
||||||
|
(do (rb-advance-n! 3) (rb-push! "op" ">>=" tok-line tok-col)))
|
||||||
|
((and (= c0 "&") (= c1 "&") (= c2 "="))
|
||||||
|
(do (rb-advance-n! 3) (rb-push! "op" "&&=" tok-line tok-col)))
|
||||||
|
((and (= c0 "|") (= c1 "|") (= c2 "="))
|
||||||
|
(do (rb-advance-n! 3) (rb-push! "op" "||=" tok-line tok-col)))
|
||||||
|
((and (= c0 "*") (= c1 "*"))
|
||||||
|
(do (rb-advance-n! 2) (rb-push! "op" "**" tok-line tok-col)))
|
||||||
|
((and (= c0 "=") (= c1 "="))
|
||||||
|
(do (rb-advance-n! 2) (rb-push! "op" "==" tok-line tok-col)))
|
||||||
|
((and (= c0 "!") (= c1 "="))
|
||||||
|
(do (rb-advance-n! 2) (rb-push! "op" "!=" tok-line tok-col)))
|
||||||
|
((and (= c0 "<") (= c1 "="))
|
||||||
|
(do (rb-advance-n! 2) (rb-push! "op" "<=" tok-line tok-col)))
|
||||||
|
((and (= c0 ">") (= c1 "="))
|
||||||
|
(do (rb-advance-n! 2) (rb-push! "op" ">=" tok-line tok-col)))
|
||||||
|
((and (= c0 "=") (= c1 "~"))
|
||||||
|
(do (rb-advance-n! 2) (rb-push! "op" "=~" tok-line tok-col)))
|
||||||
|
((and (= c0 "!") (= c1 "~"))
|
||||||
|
(do (rb-advance-n! 2) (rb-push! "op" "!~" tok-line tok-col)))
|
||||||
|
((and (= c0 "<") (= c1 "<"))
|
||||||
|
(do (rb-advance-n! 2) (rb-push! "op" "<<" tok-line tok-col)))
|
||||||
|
((and (= c0 ">") (= c1 ">"))
|
||||||
|
(do (rb-advance-n! 2) (rb-push! "op" ">>" tok-line tok-col)))
|
||||||
|
((and (= c0 "&") (= c1 "&"))
|
||||||
|
(do (rb-advance-n! 2) (rb-push! "op" "&&" tok-line tok-col)))
|
||||||
|
((and (= c0 "|") (= c1 "|"))
|
||||||
|
(do (rb-advance-n! 2) (rb-push! "op" "||" tok-line tok-col)))
|
||||||
|
((and (= c0 "+") (= c1 "="))
|
||||||
|
(do (rb-advance-n! 2) (rb-push! "op" "+=" tok-line tok-col)))
|
||||||
|
((and (= c0 "-") (= c1 "="))
|
||||||
|
(do (rb-advance-n! 2) (rb-push! "op" "-=" tok-line tok-col)))
|
||||||
|
((and (= c0 "*") (= c1 "="))
|
||||||
|
(do (rb-advance-n! 2) (rb-push! "op" "*=" tok-line tok-col)))
|
||||||
|
((and (= c0 "/") (= c1 "="))
|
||||||
|
(do (rb-advance-n! 2) (rb-push! "op" "/=" tok-line tok-col)))
|
||||||
|
((and (= c0 "%") (= c1 "="))
|
||||||
|
(do (rb-advance-n! 2) (rb-push! "op" "%=" tok-line tok-col)))
|
||||||
|
((and (= c0 "&") (= c1 "="))
|
||||||
|
(do (rb-advance-n! 2) (rb-push! "op" "&=" tok-line tok-col)))
|
||||||
|
((and (= c0 "|") (= c1 "="))
|
||||||
|
(do (rb-advance-n! 2) (rb-push! "op" "|=" tok-line tok-col)))
|
||||||
|
((and (= c0 "^") (= c1 "="))
|
||||||
|
(do (rb-advance-n! 2) (rb-push! "op" "^=" tok-line tok-col)))
|
||||||
|
((and (= c0 "-") (= c1 ">"))
|
||||||
|
(do (rb-advance-n! 2) (rb-push! "op" "->" tok-line tok-col)))
|
||||||
|
((and (= c0 "=") (= c1 ">"))
|
||||||
|
(do (rb-advance-n! 2) (rb-push! "op" "=>" tok-line tok-col)))
|
||||||
|
((and (= c0 "|") (nil? c1))
|
||||||
|
(do (rb-advance!) (rb-push! "pipe" "|" tok-line tok-col)))
|
||||||
|
((= c0 "|")
|
||||||
|
(do (rb-advance!) (rb-push! "pipe" "|" tok-line tok-col)))
|
||||||
|
(:else
|
||||||
|
(do (rb-advance!) (rb-push! "op" c0 tok-line tok-col)))))))
|
||||||
|
(define rb-scan!
|
||||||
|
(fn ()
|
||||||
|
(cond
|
||||||
|
((>= pos src-len) nil)
|
||||||
|
((rb-space? (rb-cur)) (do (rb-advance!) (rb-scan!)))
|
||||||
|
((= (rb-cur) "#") (do (rb-skip-line-comment!) (rb-scan!)))
|
||||||
|
((= (rb-cur) "\n")
|
||||||
|
(do
|
||||||
|
(let ((l line) (c col))
|
||||||
|
(rb-advance!)
|
||||||
|
(rb-push! "newline" nil l c))
|
||||||
|
(rb-scan!)))
|
||||||
|
((rb-digit? (rb-cur))
|
||||||
|
(do
|
||||||
|
(let ((l line) (c col))
|
||||||
|
(rb-read-number! l c))
|
||||||
|
(rb-scan!)))
|
||||||
|
((rb-ident-start? (rb-cur))
|
||||||
|
(do
|
||||||
|
(let ((l line) (c col))
|
||||||
|
(let ((w (rb-read-ident-word)))
|
||||||
|
(if (rb-keyword? w)
|
||||||
|
(rb-push! "keyword" w l c)
|
||||||
|
(if (rb-upper? (substring w 0 1))
|
||||||
|
(rb-push! "const" w l c)
|
||||||
|
(rb-push! "ident" w l c)))))
|
||||||
|
(rb-scan!)))
|
||||||
|
((= (rb-cur) "@")
|
||||||
|
(do
|
||||||
|
(let ((l line) (c col))
|
||||||
|
(if (= (rb-peek 1) "@")
|
||||||
|
(do
|
||||||
|
(rb-advance-n! 2)
|
||||||
|
(let ((name (rb-read-while rb-ident-cont?)))
|
||||||
|
(rb-push! "cvar" (str "@@" name) l c)))
|
||||||
|
(do
|
||||||
|
(rb-advance!)
|
||||||
|
(let ((name (rb-read-while rb-ident-cont?)))
|
||||||
|
(rb-push! "ivar" (str "@" name) l c)))))
|
||||||
|
(rb-scan!)))
|
||||||
|
((= (rb-cur) "$")
|
||||||
|
(do
|
||||||
|
(let ((l line) (c col))
|
||||||
|
(rb-advance!)
|
||||||
|
(let ((name (rb-read-while rb-ident-cont?)))
|
||||||
|
(rb-push! "gvar" (str "$" name) l c)))
|
||||||
|
(rb-scan!)))
|
||||||
|
((= (rb-cur) "\"")
|
||||||
|
(do
|
||||||
|
(let ((l line) (c col))
|
||||||
|
(rb-push! "string" (rb-read-dq-string) l c))
|
||||||
|
(rb-scan!)))
|
||||||
|
((= (rb-cur) "'")
|
||||||
|
(do
|
||||||
|
(let ((l line) (c col))
|
||||||
|
(rb-push! "string" (rb-read-sq-string) l c))
|
||||||
|
(rb-scan!)))
|
||||||
|
((and (= (rb-cur) ":") (= (rb-peek 1) ":"))
|
||||||
|
(do
|
||||||
|
(let ((l line) (c col))
|
||||||
|
(rb-advance-n! 2)
|
||||||
|
(rb-push! "dcolon" "::" l c))
|
||||||
|
(rb-scan!)))
|
||||||
|
((= (rb-cur) ":")
|
||||||
|
(do
|
||||||
|
(let ((l line) (c col))
|
||||||
|
(rb-advance!)
|
||||||
|
(cond
|
||||||
|
((= (rb-cur) "\"")
|
||||||
|
(rb-push! "symbol" (rb-read-dq-string) l c))
|
||||||
|
((= (rb-cur) "'")
|
||||||
|
(rb-push! "symbol" (rb-read-sq-string) l c))
|
||||||
|
((rb-ident-start? (rb-cur))
|
||||||
|
(let ((name (rb-read-ident-word)))
|
||||||
|
(rb-push! "symbol" name l c)))
|
||||||
|
(:else
|
||||||
|
(rb-push! "colon" ":" l c))))
|
||||||
|
(rb-scan!)))
|
||||||
|
((and (= (rb-cur) "%")
|
||||||
|
(let ((p (rb-peek 1)))
|
||||||
|
(or (= p "w") (= p "W") (= p "i") (= p "I"))))
|
||||||
|
(do
|
||||||
|
(let ((l line) (c col))
|
||||||
|
(let ((kind (rb-peek 1)))
|
||||||
|
(let ((items (rb-read-percent-words)))
|
||||||
|
(if (or (= kind "i") (= kind "I"))
|
||||||
|
(rb-push! "isymbols" items l c)
|
||||||
|
(rb-push! "words" items l c)))))
|
||||||
|
(rb-scan!)))
|
||||||
|
((= (rb-cur) ".")
|
||||||
|
(do
|
||||||
|
(let ((l line) (c col))
|
||||||
|
(cond
|
||||||
|
((and (= (rb-peek 1) ".") (= (rb-peek 2) "."))
|
||||||
|
(do (rb-advance-n! 3) (rb-push! "dotdotdot" "..." l c)))
|
||||||
|
((= (rb-peek 1) ".")
|
||||||
|
(do (rb-advance-n! 2) (rb-push! "dotdot" ".." l c)))
|
||||||
|
(:else
|
||||||
|
(do (rb-advance!) (rb-push! "dot" "." l c)))))
|
||||||
|
(rb-scan!)))
|
||||||
|
((= (rb-cur) ",")
|
||||||
|
(do
|
||||||
|
(let ((l line) (c col)) (rb-push! "comma" "," l c) (rb-advance!))
|
||||||
|
(rb-scan!)))
|
||||||
|
((= (rb-cur) ";")
|
||||||
|
(do
|
||||||
|
(let ((l line) (c col)) (rb-push! "semi" ";" l c) (rb-advance!))
|
||||||
|
(rb-scan!)))
|
||||||
|
((= (rb-cur) "(")
|
||||||
|
(do
|
||||||
|
(let ((l line) (c col)) (rb-push! "lparen" "(" l c) (rb-advance!))
|
||||||
|
(rb-scan!)))
|
||||||
|
((= (rb-cur) ")")
|
||||||
|
(do
|
||||||
|
(let ((l line) (c col)) (rb-push! "rparen" ")" l c) (rb-advance!))
|
||||||
|
(rb-scan!)))
|
||||||
|
((= (rb-cur) "[")
|
||||||
|
(do
|
||||||
|
(let ((l line) (c col)) (rb-push! "lbracket" "[" l c) (rb-advance!))
|
||||||
|
(rb-scan!)))
|
||||||
|
((= (rb-cur) "]")
|
||||||
|
(do
|
||||||
|
(let ((l line) (c col)) (rb-push! "rbracket" "]" l c) (rb-advance!))
|
||||||
|
(rb-scan!)))
|
||||||
|
((= (rb-cur) "{")
|
||||||
|
(do
|
||||||
|
(let ((l line) (c col)) (rb-push! "lbrace" "{" l c) (rb-advance!))
|
||||||
|
(rb-scan!)))
|
||||||
|
((= (rb-cur) "}")
|
||||||
|
(do
|
||||||
|
(let ((l line) (c col)) (rb-push! "rbrace" "}" l c) (rb-advance!))
|
||||||
|
(rb-scan!)))
|
||||||
|
((or (= (rb-cur) "+") (= (rb-cur) "-") (= (rb-cur) "*")
|
||||||
|
(= (rb-cur) "/") (= (rb-cur) "%") (= (rb-cur) "=")
|
||||||
|
(= (rb-cur) "!") (= (rb-cur) "<") (= (rb-cur) ">")
|
||||||
|
(= (rb-cur) "&") (= (rb-cur) "^") (= (rb-cur) "~")
|
||||||
|
(= (rb-cur) "|"))
|
||||||
|
(do
|
||||||
|
(let ((l line) (c col)) (rb-read-op! l c))
|
||||||
|
(rb-scan!)))
|
||||||
|
(:else (do (rb-advance!) (rb-scan!))))))
|
||||||
|
(rb-scan!)
|
||||||
|
(rb-push! "eof" nil line col)
|
||||||
|
tokens)))
|
||||||
81
plans/agent-briefings/apl-loop.md
Normal file
81
plans/agent-briefings/apl-loop.md
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
# apl-on-sx loop agent (single agent, queue-driven)
|
||||||
|
|
||||||
|
Role: iterates `plans/apl-on-sx.md` forever. Rank-polymorphic primitives + 6 operators on the JIT is the headline showcase — APL is the densest combinator algebra you can put on top of a primitive table. Every program is `array → array` pure pipelines, exactly what the JIT was built for.
|
||||||
|
|
||||||
|
```
|
||||||
|
description: apl-on-sx queue loop
|
||||||
|
subagent_type: general-purpose
|
||||||
|
run_in_background: true
|
||||||
|
isolation: worktree
|
||||||
|
```
|
||||||
|
|
||||||
|
## Prompt
|
||||||
|
|
||||||
|
You are the sole background agent working `/root/rose-ash/plans/apl-on-sx.md`. Isolated worktree, forever, one commit per feature. Never push.
|
||||||
|
|
||||||
|
## Restart baseline — check before iterating
|
||||||
|
|
||||||
|
1. Read `plans/apl-on-sx.md` — roadmap + Progress log.
|
||||||
|
2. `ls lib/apl/` — pick up from the most advanced file.
|
||||||
|
3. If `lib/apl/tests/*.sx` exist, run them. Green before new work.
|
||||||
|
4. If `lib/apl/scoreboard.md` exists, that's your baseline.
|
||||||
|
|
||||||
|
## The queue
|
||||||
|
|
||||||
|
Phase order per `plans/apl-on-sx.md`:
|
||||||
|
|
||||||
|
- **Phase 1** — tokenizer + parser. Unicode glyphs, `¯` for negative, strands (juxtaposition), right-to-left, valence resolution by syntactic position
|
||||||
|
- **Phase 2** — array model + scalar primitives. `make-array {shape, ravel}`, scalar promotion, broadcast for `+ - × ÷ ⌈ ⌊ * ⍟ | ! ○`, comparison, logical, `⍳`, `⎕IO`
|
||||||
|
- **Phase 3** — structural primitives + indexing. `⍴ , ⍉ ↑ ↓ ⌽ ⊖ ⌷ ⍋ ⍒ ⊂ ⊃ ∊`
|
||||||
|
- **Phase 4** — **THE SHOWCASE**: operators. `f/` (reduce), `f¨` (each), `∘.f` (outer), `f.g` (inner), `f⍨` (commute), `f∘g` (compose), `f⍣n` (power), `f⍤k` (rank), `@` (at)
|
||||||
|
- **Phase 5** — dfns + tradfns + control flow. `{⍺+⍵}`, `∇` recurse, `⍺←default`, tradfn header, `:If/:While/:For/:Select`
|
||||||
|
- **Phase 6** — classic programs (life, mandelbrot, primes, n-queens, quicksort) + idiom corpus + drive to 100+
|
||||||
|
|
||||||
|
Within a phase, pick the checkbox that unlocks the most tests per effort.
|
||||||
|
|
||||||
|
Every iteration: implement → test → commit → tick `[ ]` → Progress log → next.
|
||||||
|
|
||||||
|
## Ground rules (hard)
|
||||||
|
|
||||||
|
- **Scope:** only `lib/apl/**` and `plans/apl-on-sx.md`. Do **not** edit `spec/`, `hosts/`, `shared/`, other `lib/<lang>/` dirs, `lib/stdlib.sx`, or `lib/` root. APL primitives go in `lib/apl/runtime.sx`.
|
||||||
|
- **NEVER call `sx_build`.** 600s watchdog. If sx_server binary broken → Blockers entry, stop.
|
||||||
|
- **Shared-file issues** → plan's Blockers with minimal repro.
|
||||||
|
- **SX files:** `sx-tree` MCP tools ONLY. `sx_validate` after edits.
|
||||||
|
- **Unicode in `.sx`:** raw UTF-8 only, never `\uXXXX` escapes. Glyphs land directly in source.
|
||||||
|
- **Worktree:** commit locally. Never push. Never touch `main`.
|
||||||
|
- **Commit granularity:** one feature per commit.
|
||||||
|
- **Plan file:** update Progress log + tick boxes every commit.
|
||||||
|
|
||||||
|
## APL-specific gotchas
|
||||||
|
|
||||||
|
- **Right-to-left, no precedence among functions.** `2 × 3 + 4` is `2 × (3 + 4)` = 14, not 10. Operators bind tighter than functions: `+/ ⍳5` is `+/(⍳5)`, and `2 +.× 3 4` is `2 (+.×) 3 4`.
|
||||||
|
- **Valence by position.** `-3` is monadic negate (`-` with no left arg). `5-3` is dyadic subtract. The parser must look left to decide. Same glyph; different fn.
|
||||||
|
- **`¯` is part of a number literal**, not a prefix function. `¯3` is the literal negative three; `-3` is the function call. Tokenizer eats `¯` into the numeric token.
|
||||||
|
- **Strands.** `1 2 3` is a 3-element vector, not three separate calls. Adjacent literals fuse into a strand at parse time. Adjacent names do *not* fuse — `a b c` is three separate references.
|
||||||
|
- **Scalar promotion.** `1 + 2 3 4` ↦ `3 4 5`. Any scalar broadcasts against any-rank conformable shape.
|
||||||
|
- **Conformability** = exactly matching shapes, OR one side scalar, OR (in some dialects) one side rank-1 cycling against rank-N. Keep strict in v1: matching shape or scalar only.
|
||||||
|
- **`⍳` is overloaded.** Monadic `⍳N` = vector 1..N (or 0..N-1 if `⎕IO=0`). Dyadic `V ⍳ W` = first-index lookup, returns `≢V+1` for not-found.
|
||||||
|
- **Reduce with `+/⍳0`** = `0` (identity for `+`). Each scalar primitive has a defined identity used by reduce-on-empty. Don't crash; return identity.
|
||||||
|
- **Reduce direction.** `f/` reduces the *last* axis. `f⌿` reduces the *first*. Matters for matrices.
|
||||||
|
- **Indexing is 1-based** by default (`⎕IO=1`). Do not silently translate to 0-based; respect `⎕IO`.
|
||||||
|
- **Bracket indexing** `A[I]` is sugar for `I⌷A` (squad-quad). Multi-axis: `A[I;J]` is `I J⌷A` with semicolon-separated axes; `A[;J]` selects all of axis 0.
|
||||||
|
- **Dfn `{...}`** — `⍺` = left arg (may be unbound for monadic call → check with `⍺←default`), `⍵` = right arg, `∇` = recurse. Default left arg syntax: `⍺←0`.
|
||||||
|
- **Tradfn vs dfn** — tradfns use line-numbered `→linenum` for goto; dfns use guards `cond:expr`. Pick the right one for the user's syntax.
|
||||||
|
- **Empty array** = rank-N array where some dim is 0. `0⍴⍳0` is empty rank-1. Scalar prototype matters for empty-array operations; ignore in v1, return 0/space.
|
||||||
|
- **Test corpus:** custom + idioms. Place programs in `lib/apl/tests/programs/` with `.apl` extension.
|
||||||
|
|
||||||
|
## General gotchas (all loops)
|
||||||
|
|
||||||
|
- SX `do` = R7RS iteration. Use `begin` for multi-expr sequences.
|
||||||
|
- `cond`/`when`/`let` clauses evaluate only the last expr.
|
||||||
|
- `type-of` on user fn returns `"lambda"`.
|
||||||
|
- Shell heredoc `||` gets eaten — escape or use `case`.
|
||||||
|
|
||||||
|
## Style
|
||||||
|
|
||||||
|
- No comments in `.sx` unless non-obvious.
|
||||||
|
- No new planning docs — update `plans/apl-on-sx.md` inline.
|
||||||
|
- Short, factual commit messages (`apl: outer product ∘. (+9)`).
|
||||||
|
- One feature per iteration. Commit. Log. Next.
|
||||||
|
|
||||||
|
Go. Read the plan; find first `[ ]`; implement.
|
||||||
80
plans/agent-briefings/common-lisp-loop.md
Normal file
80
plans/agent-briefings/common-lisp-loop.md
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
# common-lisp-on-sx loop agent (single agent, queue-driven)
|
||||||
|
|
||||||
|
Role: iterates `plans/common-lisp-on-sx.md` forever. Conditions + restarts on delimited continuations is the headline showcase — every other Lisp reinvents resumable exceptions on the host stack. On SX `signal`/`invoke-restart` is just a captured continuation. Plus CLOS, the LOOP macro, packages.
|
||||||
|
|
||||||
|
```
|
||||||
|
description: common-lisp-on-sx queue loop
|
||||||
|
subagent_type: general-purpose
|
||||||
|
run_in_background: true
|
||||||
|
isolation: worktree
|
||||||
|
```
|
||||||
|
|
||||||
|
## Prompt
|
||||||
|
|
||||||
|
You are the sole background agent working `/root/rose-ash/plans/common-lisp-on-sx.md`. Isolated worktree, forever, one commit per feature. Never push.
|
||||||
|
|
||||||
|
## Restart baseline — check before iterating
|
||||||
|
|
||||||
|
1. Read `plans/common-lisp-on-sx.md` — roadmap + Progress log.
|
||||||
|
2. `ls lib/common-lisp/` — pick up from the most advanced file.
|
||||||
|
3. If `lib/common-lisp/tests/*.sx` exist, run them. Green before new work.
|
||||||
|
4. If `lib/common-lisp/scoreboard.md` exists, that's your baseline.
|
||||||
|
|
||||||
|
## The queue
|
||||||
|
|
||||||
|
Phase order per `plans/common-lisp-on-sx.md`:
|
||||||
|
|
||||||
|
- **Phase 1** — reader + parser (read macros `#'` `'` `` ` `` `,` `,@` `#( … )` `#:` `#\char` `#xFF` `#b1010`, ratios, dispatch chars, lambda lists with `&optional`/`&rest`/`&key`/`&aux`)
|
||||||
|
- **Phase 2** — sequential eval + special forms (`let`/`let*`/`flet`/`labels`, `block`/`return-from`, `tagbody`/`go`, `unwind-protect`, multiple values, `setf` subset, dynamic variables)
|
||||||
|
- **Phase 3** — **THE SHOWCASE**: condition system + restarts. `define-condition`, `signal`/`error`/`cerror`/`warn`, `handler-bind` (non-unwinding), `handler-case` (unwinding), `restart-case`, `restart-bind`, `find-restart`/`invoke-restart`/`compute-restarts`, `with-condition-restarts`. Classic programs (restart-demo, parse-recover, interactive-debugger) green.
|
||||||
|
- **Phase 4** — CLOS: `defclass`, `defgeneric`, `defmethod` with `:before`/`:after`/`:around`, `call-next-method`, multiple dispatch
|
||||||
|
- **Phase 5** — macros + LOOP macro + reader macros
|
||||||
|
- **Phase 6** — packages + stdlib (sequence functions, FORMAT directives, drive corpus to 200+)
|
||||||
|
|
||||||
|
Within a phase, pick the checkbox that unlocks the most tests per effort.
|
||||||
|
|
||||||
|
Every iteration: implement → test → commit → tick `[ ]` → Progress log → next.
|
||||||
|
|
||||||
|
## Ground rules (hard)
|
||||||
|
|
||||||
|
- **Scope:** only `lib/common-lisp/**` and `plans/common-lisp-on-sx.md`. Do **not** edit `spec/`, `hosts/`, `shared/`, other `lib/<lang>/` dirs, `lib/stdlib.sx`, or `lib/` root. CL primitives go in `lib/common-lisp/runtime.sx`.
|
||||||
|
- **NEVER call `sx_build`.** 600s watchdog. If sx_server binary broken → Blockers entry, stop.
|
||||||
|
- **Shared-file issues** → plan's Blockers with minimal repro.
|
||||||
|
- **Delimited continuations** are in `lib/callcc.sx` + `spec/evaluator.sx` Step 5. `sx_summarise` spec/evaluator.sx first — 2300+ lines.
|
||||||
|
- **SX files:** `sx-tree` MCP tools ONLY. `sx_validate` after edits.
|
||||||
|
- **Worktree:** commit locally. Never push. Never touch `main`.
|
||||||
|
- **Commit granularity:** one feature per commit.
|
||||||
|
- **Plan file:** update Progress log + tick boxes every commit.
|
||||||
|
|
||||||
|
## Common-Lisp-specific gotchas
|
||||||
|
|
||||||
|
- **`handler-bind` is non-unwinding** — handlers can decline by returning normally, in which case `signal` keeps walking the chain. **`handler-case` is unwinding** — picking a handler aborts the protected form via a captured continuation. Don't conflate them.
|
||||||
|
- **Restarts are not handlers.** `restart-case` establishes named *resumption points*; `signal` runs handler code with restarts visible; the handler chooses a restart by calling `invoke-restart`, which abandons handler stack and resumes at the restart point. Two stacks: handlers walk down, restarts wait to be invoked.
|
||||||
|
- **`block` / `return-from`** is lexical. `block name … (return-from name v) …` captures `^k` once at entry; `return-from` invokes it. `return-from` to a name not in scope is an error (don't fall back to outer block).
|
||||||
|
- **`tagbody` / `go`** — each tag in tagbody is a continuation; `go tag` invokes it. Tags are lexical, can only target tagbodies in scope.
|
||||||
|
- **`unwind-protect`** runs cleanup on *any* non-local exit (return-from, throw, condition unwind). Implement as a scope frame fired by the cleanup machinery.
|
||||||
|
- **Multiple values**: primary-value-only contexts (function args, `if` test, etc.) drop extras silently. `values` produces multiple. `multiple-value-bind` / `multiple-value-call` consume them. Don't auto-list.
|
||||||
|
- **CLOS dispatch:** sort applicable methods by argument-list specificity (`subclassp` per arg, left-to-right); standard method combination calls primary methods most-specific-first via `call-next-method` chain. `:before` runs all before primaries; `:after` runs all after, in reverse-specificity. `:around` wraps everything.
|
||||||
|
- **`call-next-method`** is a *continuation* available only inside a method body. Implement as a thunk stored in a dynamic-extent variable.
|
||||||
|
- **Generalised reference (`setf`)**: `(setf (foo x) v)` ↦ `(setf-foo v x)`. Look up the setf-expander, not just a writer fn. `define-setf-expander` is mandatory for non-trivial places. Start with the symbolic / list / aref / slot-value cases.
|
||||||
|
- **Dynamic variables (specials):** `defvar`/`defparameter` mark a symbol as special. `let` over a special name *rebinds* in dynamic extent (use parameterize-style scope), not lexical.
|
||||||
|
- **Symbols are package-qualified.** Reader resolves `cl:car`, `mypkg::internal`, bare `foo` (current package). Internal vs external matters for `:` (one colon) reads.
|
||||||
|
- **`nil` is also `()` is also the empty list.** Same object. `nil` is also false. CL has no distinct unit value.
|
||||||
|
- **LOOP macro is huge.** Build incrementally — start with `for/in`, `for/from`, `collect`, `sum`, `count`, `repeat`. Add conditional clauses (`when`, `if`, `else`) once iteration drivers stable. `named` blocks + `return-from named` last.
|
||||||
|
- **Test corpus:** custom + curated `ansi-test` slice. Place programs in `lib/common-lisp/tests/programs/` with `.lisp` extension.
|
||||||
|
|
||||||
|
## General gotchas (all loops)
|
||||||
|
|
||||||
|
- SX `do` = R7RS iteration. Use `begin` for multi-expr sequences.
|
||||||
|
- `cond`/`when`/`let` clauses evaluate only the last expr.
|
||||||
|
- `type-of` on user fn returns `"lambda"`.
|
||||||
|
- Shell heredoc `||` gets eaten — escape or use `case`.
|
||||||
|
|
||||||
|
## Style
|
||||||
|
|
||||||
|
- No comments in `.sx` unless non-obvious.
|
||||||
|
- No new planning docs — update `plans/common-lisp-on-sx.md` inline.
|
||||||
|
- Short, factual commit messages (`common-lisp: handler-bind + 12 tests`).
|
||||||
|
- One feature per iteration. Commit. Log. Next.
|
||||||
|
|
||||||
|
Go. Read the plan; find first `[ ]`; implement.
|
||||||
@@ -14,7 +14,7 @@ You are the sole background agent working `/root/rose-ash/plans/js-on-sx.md`. A
|
|||||||
|
|
||||||
## Current state (restart baseline — verify before iterating)
|
## Current state (restart baseline — verify before iterating)
|
||||||
|
|
||||||
- Branch: `loops/js`.
|
- Branch: `architecture`. HEAD: `14b6586e` (HS-related, not js-on-sx).
|
||||||
- `lib/js/` is **untracked** — nothing is committed yet. First commit should stage everything current on disk.
|
- `lib/js/` is **untracked** — nothing is committed yet. First commit should stage everything current on disk.
|
||||||
- `lib/js/test262-upstream/` is a clone of tc39/test262 pinned at `d5e73fc8d2c663554fb72e2380a8c2bc1a318a33`. **Gitignore it** (`lib/js/.gitignore` → `test262-upstream/`). Do not commit the 50k test files.
|
- `lib/js/test262-upstream/` is a clone of tc39/test262 pinned at `d5e73fc8d2c663554fb72e2380a8c2bc1a318a33`. **Gitignore it** (`lib/js/.gitignore` → `test262-upstream/`). Do not commit the 50k test files.
|
||||||
- `lib/js/test262-runner.py` exists but is buggy — current scoreboard is `0/8 (7 timeouts, 1 fail)`. The runner needs real work: harness script loading, batching, per-test timeout tuning, strict-mode skipping.
|
- `lib/js/test262-runner.py` exists but is buggy — current scoreboard is `0/8 (7 timeouts, 1 fail)`. The runner needs real work: harness script loading, batching, per-test timeout tuning, strict-mode skipping.
|
||||||
@@ -61,7 +61,7 @@ Tagged dict: `{:__js_string__ true :utf16 <list-of-uint16> :str <lazy-utf8-cache
|
|||||||
- **Scope:** only `lib/js/**` and `plans/js-on-sx.md`. Do NOT touch `spec/`, `shared/`, `lib/hyperscript/`. Shared-file issues go under the plan's "Blockers" section.
|
- **Scope:** only `lib/js/**` and `plans/js-on-sx.md`. Do NOT touch `spec/`, `shared/`, `lib/hyperscript/`. Shared-file issues go under the plan's "Blockers" section.
|
||||||
- **SX files:** `sx-tree` MCP tools ONLY. `sx_summarise` / `sx_read_subtree` / `sx_find_all` / `sx_get_context` before edits. `sx_replace_node` / `sx_insert_child` / `sx_insert_near` / `sx_replace_by_pattern` / `sx_rename_symbol` for edits. `sx_validate` after. `sx_write_file` for new files. Never `Edit`/`Read`/`Write` on `.sx`.
|
- **SX files:** `sx-tree` MCP tools ONLY. `sx_summarise` / `sx_read_subtree` / `sx_find_all` / `sx_get_context` before edits. `sx_replace_node` / `sx_insert_child` / `sx_insert_near` / `sx_replace_by_pattern` / `sx_rename_symbol` for edits. `sx_validate` after. `sx_write_file` for new files. Never `Edit`/`Read`/`Write` on `.sx`.
|
||||||
- **Shell, Python, Markdown, JSON:** edit normally.
|
- **Shell, Python, Markdown, JSON:** edit normally.
|
||||||
- **Branch:** `loops/js`. Commit, then push to `origin/loops/js`. Never touch `main`.
|
- **Branch:** `architecture`. Commit locally. Never push. Never touch `main`.
|
||||||
- **Commit granularity:** one feature per commit. Short, factual commit messages. Commit even if a partial fix — don't hoard changes.
|
- **Commit granularity:** one feature per commit. Short, factual commit messages. Commit even if a partial fix — don't hoard changes.
|
||||||
- **Tests:** `bash lib/js/test.sh` (254/254 baseline) and `bash lib/js/conformance.sh` (148/148 baseline). Never regress. If a feature requires larger refactor, split into multiple commits each green.
|
- **Tests:** `bash lib/js/test.sh` (254/254 baseline) and `bash lib/js/conformance.sh` (148/148 baseline). Never regress. If a feature requires larger refactor, split into multiple commits each green.
|
||||||
- **Plan file:** append one paragraph per iteration to "Progress log". Tick `[x]` boxes. Don't rewrite history.
|
- **Plan file:** append one paragraph per iteration to "Progress log". Tick `[x]` boxes. Don't rewrite history.
|
||||||
|
|||||||
83
plans/agent-briefings/ruby-loop.md
Normal file
83
plans/agent-briefings/ruby-loop.md
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
# ruby-on-sx loop agent (single agent, queue-driven)
|
||||||
|
|
||||||
|
Role: iterates `plans/ruby-on-sx.md` forever. Fibers via delcc is the headline showcase — `Fiber.new`/`Fiber.yield`/`Fiber.resume` are textbook delimited continuations with sugar, where MRI does it via C-stack swapping. Plus blocks/yield (lexical escape continuations, same shape as Smalltalk's non-local return), method_missing, and singleton classes.
|
||||||
|
|
||||||
|
```
|
||||||
|
description: ruby-on-sx queue loop
|
||||||
|
subagent_type: general-purpose
|
||||||
|
run_in_background: true
|
||||||
|
isolation: worktree
|
||||||
|
```
|
||||||
|
|
||||||
|
## Prompt
|
||||||
|
|
||||||
|
You are the sole background agent working `/root/rose-ash/plans/ruby-on-sx.md`. Isolated worktree, forever, one commit per feature. Push to `origin/loops/ruby` after every commit.
|
||||||
|
|
||||||
|
## Restart baseline — check before iterating
|
||||||
|
|
||||||
|
1. Read `plans/ruby-on-sx.md` — roadmap + Progress log.
|
||||||
|
2. `ls lib/ruby/` — pick up from the most advanced file.
|
||||||
|
3. If `lib/ruby/tests/*.sx` exist, run them. Green before new work.
|
||||||
|
4. If `lib/ruby/scoreboard.md` exists, that's your baseline.
|
||||||
|
|
||||||
|
## The queue
|
||||||
|
|
||||||
|
Phase order per `plans/ruby-on-sx.md`:
|
||||||
|
|
||||||
|
- **Phase 1** — tokenizer + parser. Keywords, identifier sigils (`@` ivar, `@@` cvar, `$` global), strings with interpolation, `%w[]`/`%i[]`, symbols, blocks `{|x| …}` and `do |x| … end`, splats, default args, method def
|
||||||
|
- **Phase 2** — object model + sequential eval. Class table, ancestor-chain dispatch, `super`, singleton classes, `method_missing` fallback, dynamic constant lookup
|
||||||
|
- **Phase 3** — blocks + procs + lambdas. Method captures escape continuation `^k`; `yield` / `return` / `break` / `next` / `redo` semantics; lambda strict arity vs proc lax
|
||||||
|
- **Phase 4** — **THE SHOWCASE**: fibers via delcc. `Fiber.new`/`Fiber.resume`/`Fiber.yield`/`Fiber.transfer`. Classic programs (generator, producer-consumer, tree-walk) green
|
||||||
|
- **Phase 5** — modules + mixins + metaprogramming. `include`/`prepend`/`extend`, `define_method`, `class_eval`/`instance_eval`, `respond_to?`/`respond_to_missing?`, hooks
|
||||||
|
- **Phase 6** — stdlib drive. `Enumerable` mixin, `Comparable`, Array/Hash/Range/String/Integer methods, drive corpus to 200+
|
||||||
|
|
||||||
|
Within a phase, pick the checkbox that unlocks the most tests per effort.
|
||||||
|
|
||||||
|
Every iteration: implement → test → commit → tick `[ ]` → Progress log → next.
|
||||||
|
|
||||||
|
## Ground rules (hard)
|
||||||
|
|
||||||
|
- **Scope:** only `lib/ruby/**` and `plans/ruby-on-sx.md`. Do **not** edit `spec/`, `hosts/`, `shared/`, other `lib/<lang>/` dirs, `lib/stdlib.sx`, or `lib/` root. Ruby primitives go in `lib/ruby/runtime.sx`.
|
||||||
|
- **NEVER call `sx_build`.** 600s watchdog. If sx_server binary broken → Blockers entry, stop.
|
||||||
|
- **Shared-file issues** → plan's Blockers with minimal repro.
|
||||||
|
- **Delimited continuations** are in `lib/callcc.sx` + `spec/evaluator.sx` Step 5. `sx_summarise` spec/evaluator.sx first — 2300+ lines.
|
||||||
|
- **SX files:** `sx-tree` MCP tools ONLY. `sx_validate` after edits.
|
||||||
|
- **Worktree:** commit, then push to `origin/loops/ruby`. Never touch `main`.
|
||||||
|
- **Commit granularity:** one feature per commit.
|
||||||
|
- **Plan file:** update Progress log + tick boxes every commit.
|
||||||
|
|
||||||
|
## Ruby-specific gotchas
|
||||||
|
|
||||||
|
- **Block `return` vs lambda `return`.** Inside a block `{ ... return v }`, `return` invokes the *enclosing method's* escape continuation (non-local return). Inside a lambda `->(){ ... return v }`, `return` returns from the *lambda*. Don't conflate. Implement: blocks bind their `^method-k`; lambdas bind their own `^lambda-k`.
|
||||||
|
- **`break` from inside a block** invokes a different escape — the *iteration loop's* escape — and the loop returns the break-value. `next` is escape from current iteration, returns iteration value. `redo` re-enters current iteration without advancing.
|
||||||
|
- **Proc arity is lax.** `proc { |a, b, c| … }.call(1, 2)` ↦ `c = nil`. Lambda is strict — same call raises ArgumentError. Check arity at call site for lambdas only.
|
||||||
|
- **Block argument unpacking.** `[[1,2],[3,4]].each { |a, b| … }` — single Array arg auto-unpacks for blocks (not lambdas). One arg, one Array → unpack. Frequent footgun.
|
||||||
|
- **Method dispatch chain order:** prepended modules → class methods → included modules → superclass → BasicObject → method_missing. `super` walks from the *defining* class's position, not the receiver class's.
|
||||||
|
- **Singleton classes** are lazily allocated. Looking up the chain for an object passes through its singleton class first, then its actual class. `class << obj; …; end` opens the singleton.
|
||||||
|
- **`method_missing`** — fallback when ancestor walk misses. Receives `(name_symbol, *args, &blk)`. Pair with `respond_to_missing?` for `respond_to?` to also report true. Do **not** swallow NoMethodError silently.
|
||||||
|
- **Ivars are per-object dicts.** Reading an unset ivar yields `nil` and a warning (`-W`). Don't error.
|
||||||
|
- **Constant lookup** is first lexical (Module.nesting), then inheritance (Module.ancestors of the innermost class). Different from method lookup.
|
||||||
|
- **`Object#send`** invokes private and public methods alike; `Object#public_send` skips privates.
|
||||||
|
- **Class reopening.** `class Foo; def bar; …; end; end` plus a later `class Foo; def baz; …; end; end` adds methods to the same class. Class table lookups must be by-name, mutable; methods dict is mutable.
|
||||||
|
- **Fiber semantics.** `Fiber.new { |arg| … }` creates a fiber suspended at entry. First `Fiber.resume(v)` enters with `arg = v`. Inside, `Fiber.yield(w)` returns `w` to the resumer; the next `Fiber.resume(v')` returns `v'` to the yield site. End of block returns final value to last resumer; subsequent `Fiber.resume` raises FiberError.
|
||||||
|
- **`Fiber.transfer`** is symmetric — either side can transfer to the other; no resume/yield asymmetry. Implement on top of the same continuation pair, just don't enforce direction.
|
||||||
|
- **Symbols are interned.** `:foo == :foo` is identity. Use SX symbols.
|
||||||
|
- **Strings are mutable.** `s = "abc"; s << "d"; s == "abcd"`. Hash keys can be strings; hash dups string keys at insertion to be safe (or freeze them).
|
||||||
|
- **Truthiness:** only `false` and `nil` are falsy. `0`, `""`, `[]` are truthy.
|
||||||
|
- **Test corpus:** custom + curated RubySpec slice. Place programs in `lib/ruby/tests/programs/` with `.rb` extension.
|
||||||
|
|
||||||
|
## General gotchas (all loops)
|
||||||
|
|
||||||
|
- SX `do` = R7RS iteration. Use `begin` for multi-expr sequences.
|
||||||
|
- `cond`/`when`/`let` clauses evaluate only the last expr.
|
||||||
|
- `type-of` on user fn returns `"lambda"`.
|
||||||
|
- Shell heredoc `||` gets eaten — escape or use `case`.
|
||||||
|
|
||||||
|
## Style
|
||||||
|
|
||||||
|
- No comments in `.sx` unless non-obvious.
|
||||||
|
- No new planning docs — update `plans/ruby-on-sx.md` inline.
|
||||||
|
- Short, factual commit messages (`ruby: Fiber.yield + Fiber.resume (+8)`).
|
||||||
|
- One feature per iteration. Commit. Log. Next.
|
||||||
|
|
||||||
|
Go. Read the plan; find first `[ ]`; implement.
|
||||||
77
plans/agent-briefings/smalltalk-loop.md
Normal file
77
plans/agent-briefings/smalltalk-loop.md
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
# smalltalk-on-sx loop agent (single agent, queue-driven)
|
||||||
|
|
||||||
|
Role: iterates `plans/smalltalk-on-sx.md` forever. Message-passing OO + **blocks with non-local return** on delimited continuations. Non-local return is the headline showcase — every other Smalltalk reinvents it on the host stack; on SX it falls out of the captured method-return continuation.
|
||||||
|
|
||||||
|
```
|
||||||
|
description: smalltalk-on-sx queue loop
|
||||||
|
subagent_type: general-purpose
|
||||||
|
run_in_background: true
|
||||||
|
isolation: worktree
|
||||||
|
```
|
||||||
|
|
||||||
|
## Prompt
|
||||||
|
|
||||||
|
You are the sole background agent working `/root/rose-ash/plans/smalltalk-on-sx.md`. Isolated worktree, forever, one commit per feature. Never push.
|
||||||
|
|
||||||
|
## Restart baseline — check before iterating
|
||||||
|
|
||||||
|
1. Read `plans/smalltalk-on-sx.md` — roadmap + Progress log.
|
||||||
|
2. `ls lib/smalltalk/` — pick up from the most advanced file.
|
||||||
|
3. If `lib/smalltalk/tests/*.sx` exist, run them. Green before new work.
|
||||||
|
4. If `lib/smalltalk/scoreboard.md` exists, that's your baseline.
|
||||||
|
|
||||||
|
## The queue
|
||||||
|
|
||||||
|
Phase order per `plans/smalltalk-on-sx.md`:
|
||||||
|
|
||||||
|
- **Phase 1** — tokenizer + parser (chunk format, identifiers, keywords `foo:`, binary selectors, `#sym`, `#(…)`, `$c`, blocks `[:a | …]`, cascades, message precedence)
|
||||||
|
- **Phase 2** — object model + sequential eval (class table bootstrap, message dispatch, `super`, `doesNotUnderstand:`, instance variables)
|
||||||
|
- **Phase 3** — **THE SHOWCASE**: blocks with non-local return via captured method-return continuation. `whileTrue:` / `ifTrue:ifFalse:` as block sends. 5 classic programs (eight-queens, quicksort, mandelbrot, life, fibonacci) green.
|
||||||
|
- **Phase 4** — reflection + MOP: `perform:`, `respondsTo:`, runtime method addition, `becomeForward:`, `Exception` / `on:do:` / `ensure:` on top of `handler-bind`/`raise`
|
||||||
|
- **Phase 5** — collections + numeric tower + streams
|
||||||
|
- **Phase 6** — port SUnit, vendor Pharo Kernel-Tests slice, drive corpus to 200+
|
||||||
|
- **Phase 7** — speed (optional): inline caching, block intrinsification
|
||||||
|
|
||||||
|
Within a phase, pick the checkbox that unlocks the most tests per effort.
|
||||||
|
|
||||||
|
Every iteration: implement → test → commit → tick `[ ]` → Progress log → next.
|
||||||
|
|
||||||
|
## Ground rules (hard)
|
||||||
|
|
||||||
|
- **Scope:** only `lib/smalltalk/**` and `plans/smalltalk-on-sx.md`. Do **not** edit `spec/`, `hosts/`, `shared/`, other `lib/<lang>/` dirs, `lib/stdlib.sx`, or `lib/` root. Smalltalk primitives go in `lib/smalltalk/runtime.sx`.
|
||||||
|
- **NEVER call `sx_build`.** 600s watchdog. If sx_server binary broken → Blockers entry, stop.
|
||||||
|
- **Shared-file issues** → plan's Blockers with minimal repro.
|
||||||
|
- **Delimited continuations** are in `lib/callcc.sx` + `spec/evaluator.sx` Step 5. `sx_summarise` spec/evaluator.sx first — 2300+ lines.
|
||||||
|
- **SX files:** `sx-tree` MCP tools ONLY. `sx_validate` after edits.
|
||||||
|
- **Worktree:** commit locally. Never push. Never touch `main`.
|
||||||
|
- **Commit granularity:** one feature per commit.
|
||||||
|
- **Plan file:** update Progress log + tick boxes every commit.
|
||||||
|
|
||||||
|
## Smalltalk-specific gotchas
|
||||||
|
|
||||||
|
- **Method invocation captures `^k`** — the return continuation. Bind it as the block's escape token. `^expr` from inside any nested block invokes that captured `^k`. Escape past method return raises `BlockContext>>cannotReturn:`.
|
||||||
|
- **Blocks are lambdas + escape token**, not bare lambdas. `value`/`value:`/… invoke the lambda; `^` invokes the escape.
|
||||||
|
- **`ifTrue:` / `ifFalse:` / `whileTrue:` are ordinary block sends** — no special form. The runtime intrinsifies them in the JIT path (Tier 1 of bytecode expansion already covers this pattern).
|
||||||
|
- **Cascade** `r m1; m2; m3` desugars to `(let ((tmp r)) (st-send tmp 'm1 ()) (st-send tmp 'm2 ()) (st-send tmp 'm3 ()))`. Result is the cascade's last send (or first, depending on parser variant — pick one and document).
|
||||||
|
- **`super` send** looks up starting from the *defining* class's superclass, not the receiver class. Stash the defining class on the method record.
|
||||||
|
- **Selectors are interned symbols.** Use SX symbols.
|
||||||
|
- **Receiver dispatch:** tagged ints / floats / strings / symbols / `nil` / `true` / `false` aren't boxed. Their classes (`SmallInteger`, `Float`, `String`, `Symbol`, `UndefinedObject`, `True`, `False`) are looked up by SX type-of, not by an `:class` field.
|
||||||
|
- **Method precedence:** unary > binary > keyword. `3 + 4 factorial` is `3 + (4 factorial)`. `a foo: b bar` is `a foo: (b bar)` (keyword absorbs trailing unary).
|
||||||
|
- **Image / fileIn / become: between sessions** = out of scope. One-way `becomeForward:` only.
|
||||||
|
- **Test corpus:** ~200 hand-written + a slice of Pharo Kernel-Tests. Place programs in `lib/smalltalk/tests/programs/`.
|
||||||
|
|
||||||
|
## General gotchas (all loops)
|
||||||
|
|
||||||
|
- SX `do` = R7RS iteration. Use `begin` for multi-expr sequences.
|
||||||
|
- `cond`/`when`/`let` clauses evaluate only the last expr.
|
||||||
|
- `type-of` on user fn returns `"lambda"`.
|
||||||
|
- Shell heredoc `||` gets eaten — escape or use `case`.
|
||||||
|
|
||||||
|
## Style
|
||||||
|
|
||||||
|
- No comments in `.sx` unless non-obvious.
|
||||||
|
- No new planning docs — update `plans/smalltalk-on-sx.md` inline.
|
||||||
|
- Short, factual commit messages (`smalltalk: tokenizer + 56 tests`).
|
||||||
|
- One feature per iteration. Commit. Log. Next.
|
||||||
|
|
||||||
|
Go. Read the plan; find first `[ ]`; implement.
|
||||||
83
plans/agent-briefings/tcl-loop.md
Normal file
83
plans/agent-briefings/tcl-loop.md
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
# tcl-on-sx loop agent (single agent, queue-driven)
|
||||||
|
|
||||||
|
Role: iterates `plans/tcl-on-sx.md` forever. `uplevel`/`upvar` is the headline showcase — Tcl's superpower for defining your own control structures, requiring deep VM cooperation in any normal host but falling out of SX's first-class env-chain. Plus the Dodekalogue (12 rules), command-substitution everywhere, and "everything is a string" homoiconicity.
|
||||||
|
|
||||||
|
```
|
||||||
|
description: tcl-on-sx queue loop
|
||||||
|
subagent_type: general-purpose
|
||||||
|
run_in_background: true
|
||||||
|
isolation: worktree
|
||||||
|
```
|
||||||
|
|
||||||
|
## Prompt
|
||||||
|
|
||||||
|
You are the sole background agent working `/root/rose-ash/plans/tcl-on-sx.md`. Isolated worktree, forever, one commit per feature. Never push.
|
||||||
|
|
||||||
|
## Restart baseline — check before iterating
|
||||||
|
|
||||||
|
1. Read `plans/tcl-on-sx.md` — roadmap + Progress log.
|
||||||
|
2. `ls lib/tcl/` — pick up from the most advanced file.
|
||||||
|
3. If `lib/tcl/tests/*.sx` exist, run them. Green before new work.
|
||||||
|
4. If `lib/tcl/scoreboard.md` exists, that's your baseline.
|
||||||
|
|
||||||
|
## The queue
|
||||||
|
|
||||||
|
Phase order per `plans/tcl-on-sx.md`:
|
||||||
|
|
||||||
|
- **Phase 1** — tokenizer + parser. The Dodekalogue (12 rules): word-splitting, command sub `[…]`, var sub `$name`/`${name}`/`$arr(idx)`, double-quote vs brace word, backslash, `;`, `#` comments only at command start, single-pass left-to-right substitution
|
||||||
|
- **Phase 2** — sequential eval + core commands. `set`/`unset`/`incr`/`append`/`lappend`, `puts`/`gets`, `expr` (own mini-language), `if`/`while`/`for`/`foreach`/`switch`, string commands, list commands, dict commands
|
||||||
|
- **Phase 3** — **THE SHOWCASE**: `proc` + `uplevel` + `upvar`. Frame stack with proc-call push/pop; `uplevel #N script` evaluates in caller's frame; `upvar` aliases names across frames. Classic programs (for-each-line, assert macro, with-temp-var) green
|
||||||
|
- **Phase 4** — `return -code N`, `catch`, `try`/`trap`/`finally`, `throw`. Control flow as integer codes
|
||||||
|
- **Phase 5** — namespaces + ensembles. `namespace eval`, qualified names `::ns::cmd`, ensembles, `namespace path`
|
||||||
|
- **Phase 6** — coroutines (built on fibers, same delcc as Ruby fibers) + system commands + drive corpus to 150+
|
||||||
|
|
||||||
|
Within a phase, pick the checkbox that unlocks the most tests per effort.
|
||||||
|
|
||||||
|
Every iteration: implement → test → commit → tick `[ ]` → Progress log → next.
|
||||||
|
|
||||||
|
## Ground rules (hard)
|
||||||
|
|
||||||
|
- **Scope:** only `lib/tcl/**` and `plans/tcl-on-sx.md`. Do **not** edit `spec/`, `hosts/`, `shared/`, other `lib/<lang>/` dirs, `lib/stdlib.sx`, or `lib/` root. Tcl primitives go in `lib/tcl/runtime.sx`.
|
||||||
|
- **NEVER call `sx_build`.** 600s watchdog. If sx_server binary broken → Blockers entry, stop.
|
||||||
|
- **Shared-file issues** → plan's Blockers with minimal repro.
|
||||||
|
- **Delimited continuations** are in `lib/callcc.sx` + `spec/evaluator.sx` Step 5. `sx_summarise` spec/evaluator.sx first — 2300+ lines.
|
||||||
|
- **SX files:** `sx-tree` MCP tools ONLY. `sx_validate` after edits.
|
||||||
|
- **Worktree:** commit locally. Never push. Never touch `main`.
|
||||||
|
- **Commit granularity:** one feature per commit.
|
||||||
|
- **Plan file:** update Progress log + tick boxes every commit.
|
||||||
|
|
||||||
|
## Tcl-specific gotchas
|
||||||
|
|
||||||
|
- **Everything is a string.** Internally cache shimmer reps (list, dict, int, double) for performance, but every value must be re-stringifiable. Mutating one rep dirties the cached string and vice versa.
|
||||||
|
- **The Dodekalogue is strict.** Substitution is **one-pass**, **left-to-right**. The result of a substitution is a value, not a script — it does NOT get re-parsed for further substitutions. This is what makes Tcl safe-by-default. Don't accidentally re-parse.
|
||||||
|
- **Brace word `{…}`** is the only way to defer evaluation. No substitution inside, just balanced braces. Used for `if {expr}` body, `proc body`, `expr` arguments.
|
||||||
|
- **Double-quote word `"…"`** is identical to a bare word for substitution purposes — it just allows whitespace in a single word. `\` escapes still apply.
|
||||||
|
- **Comments are only at command position.** `# this is a comment` after a `;` or newline; *not* inside a command. `set x 1 # not a comment` is a 4-arg `set`.
|
||||||
|
- **`expr` has its own grammar** — operator precedence, function calls — and does its own substitution. Brace `expr {$x + 1}` to avoid double-substitution and to enable bytecode caching.
|
||||||
|
- **`if` and `while` re-parse** the condition only if not braced. Always use `if {…}`/`while {…}` form. The unbraced form re-substitutes per iteration.
|
||||||
|
- **`return` from a `proc`** uses control code 2. `break` is 3, `continue` is 4. `error` is 1. `catch` traps any non-zero code; user can return non-zero with `return -code error -errorcode FOO message`.
|
||||||
|
- **`uplevel #0 script`** is global frame. `uplevel 1 script` (or just `uplevel script`) is caller's frame. `uplevel #N` is absolute level N (0=global, 1=top-level proc, 2=proc-called-from-top, …). Negative levels are errors.
|
||||||
|
- **`upvar #N otherVar localVar`** binds `localVar` in the current frame as an *alias* — both names refer to the same storage. Reads and writes go through the alias.
|
||||||
|
- **`info level`** with no arg returns current level number. `info level N` (positive) returns the command list that invoked level N. `info level -N` returns the command list of the level N relative-up.
|
||||||
|
- **Variable names with `(…)`** are array elements: `set arr(foo) 1`. Arrays are not first-class values — you can't `set x $arr`. `array get arr` gives a flat list `{key1 val1 key2 val2 …}`.
|
||||||
|
- **List vs string.** `set l "a b c"` and `set l [list a b c]` look the same when printed but the second has a cached list rep. `lindex` works on both via shimmering. Most user code can't tell the difference.
|
||||||
|
- **`incr x`** errors if x doesn't exist; pre-set with `set x 0` or use `incr x 0` first if you mean "create-or-increment". Or use `dict incr` for dicts.
|
||||||
|
- **Coroutines are fibers.** `coroutine name body` starts a coroutine; calling `name` resumes it; `yield value` from inside suspends and returns `value` to the resumer. Same primitive as Ruby fibers — share the implementation under the hood.
|
||||||
|
- **`switch`** matches first clause whose pattern matches. Default is `default`. Variant matches: glob (default), `-exact`, `-glob`, `-regexp`. Body `-` means "fall through to next clause's body".
|
||||||
|
- **Test corpus:** custom + slice of Tcl's own tests. Place programs in `lib/tcl/tests/programs/` with `.tcl` extension.
|
||||||
|
|
||||||
|
## General gotchas (all loops)
|
||||||
|
|
||||||
|
- SX `do` = R7RS iteration. Use `begin` for multi-expr sequences.
|
||||||
|
- `cond`/`when`/`let` clauses evaluate only the last expr.
|
||||||
|
- `type-of` on user fn returns `"lambda"`.
|
||||||
|
- Shell heredoc `||` gets eaten — escape or use `case`.
|
||||||
|
|
||||||
|
## Style
|
||||||
|
|
||||||
|
- No comments in `.sx` unless non-obvious.
|
||||||
|
- No new planning docs — update `plans/tcl-on-sx.md` inline.
|
||||||
|
- Short, factual commit messages (`tcl: uplevel + upvar (+11)`).
|
||||||
|
- One feature per iteration. Commit. Log. Next.
|
||||||
|
|
||||||
|
Go. Read the plan; find first `[ ]`; implement.
|
||||||
115
plans/apl-on-sx.md
Normal file
115
plans/apl-on-sx.md
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
# APL-on-SX: rank-polymorphic primitives + glyph parser
|
||||||
|
|
||||||
|
The headline showcase is **rank polymorphism** — a single primitive (`+`, `⌈`, `⊂`, `⍳`) works uniformly on scalars, vectors, matrices, and higher-rank arrays. ~80 glyph primitives + 6 operators bind together with right-to-left evaluation; the entire language is a high-density combinator algebra. The JIT compiler + primitive table pay off massively here because almost every program is `array → array` pure pipelines.
|
||||||
|
|
||||||
|
End-state goal: Dyalog-flavoured APL subset, dfns + tradfns, classic programs (game-of-life, mandelbrot, prime-sieve, n-queens, conway), 100+ green tests.
|
||||||
|
|
||||||
|
## Scope decisions (defaults — override by editing before we spawn)
|
||||||
|
|
||||||
|
- **Syntax:** Dyalog APL surface, Unicode glyphs. `⎕`-quad system functions for I/O. `∇` tradfn header.
|
||||||
|
- **Conformance:** "Reads like APL, runs like APL." Not byte-compat with Dyalog; we care about right-to-left semantics and rank polymorphism.
|
||||||
|
- **Test corpus:** custom — APL idioms (Roger Hui style), classic programs, plus ~50 pattern tests for primitives.
|
||||||
|
- **Out of scope:** ⎕-namespaces beyond a handful, complex numbers, full TAO ordering, `⎕FX` runtime function definition (use static `∇` only), nested-array-of-functions higher orders, the editor.
|
||||||
|
- **Glyphs:** input via plain Unicode in `.apl` source files. Backtick-prefix shortcuts handled by the user's editor — we don't ship one.
|
||||||
|
|
||||||
|
## Ground rules
|
||||||
|
|
||||||
|
- **Scope:** only touch `lib/apl/**` and `plans/apl-on-sx.md`. Don't edit `spec/`, `hosts/`, `shared/`, or any other `lib/<lang>/**`. APL primitives go in `lib/apl/runtime.sx`.
|
||||||
|
- **SX files:** use `sx-tree` MCP tools only.
|
||||||
|
- **Commits:** one feature per commit. Keep `## Progress log` updated and tick roadmap boxes.
|
||||||
|
|
||||||
|
## Architecture sketch
|
||||||
|
|
||||||
|
```
|
||||||
|
APL source (Unicode glyphs)
|
||||||
|
│
|
||||||
|
▼
|
||||||
|
lib/apl/tokenizer.sx — glyphs, identifiers, numbers (¯ for negative), strings, strands
|
||||||
|
│
|
||||||
|
▼
|
||||||
|
lib/apl/parser.sx — right-to-left with valence resolution (mon vs dyadic by position)
|
||||||
|
│
|
||||||
|
▼
|
||||||
|
lib/apl/transpile.sx — AST → SX AST (entry: apl-eval-ast)
|
||||||
|
│
|
||||||
|
▼
|
||||||
|
lib/apl/runtime.sx — array model, ~80 primitives, 6 operators, dfns/tradfns
|
||||||
|
```
|
||||||
|
|
||||||
|
Core mapping:
|
||||||
|
- **Array** = SX dict `{:shape (d1 d2 …) :ravel #(v1 v2 …)}`. Scalar is rank-0 (empty shape), vector is rank-1, matrix rank-2, etc. Type uniformity not required (heterogeneous nested arrays via "boxed" elements `⊂x`).
|
||||||
|
- **Rank polymorphism** — every scalar primitive is broadcast: `1 2 3 + 4 5 6` ↦ `5 7 9`; `(2 3⍴⍳6) + 1` ↦ broadcast scalar to matrix.
|
||||||
|
- **Conformability** = matching shapes, or one-side scalar, or rank-1 cycling (deferred — keep strict in v1).
|
||||||
|
- **Valence** = each glyph has a monadic and a dyadic meaning; resolution is purely positional (left-arg present → dyadic).
|
||||||
|
- **Operator** = takes one or two function operands, returns a derived function (`f¨` = `each f`, `f/` = `reduce f`, `f∘g` = `compose`, `f⍨` = `commute`).
|
||||||
|
- **Tradfn** `∇R←L F R; locals` = named function with explicit header.
|
||||||
|
- **Dfn** `{⍺+⍵}` = anonymous, `⍺` = left arg, `⍵` = right arg, `∇` = recurse.
|
||||||
|
|
||||||
|
## Roadmap
|
||||||
|
|
||||||
|
### Phase 1 — tokenizer + parser
|
||||||
|
- [ ] Tokenizer: Unicode glyphs (the full APL set: `+ - × ÷ * ⍟ ⌈ ⌊ | ! ? ○ ~ < ≤ = ≥ > ≠ ∊ ∧ ∨ ⍱ ⍲ , ⍪ ⍴ ⌽ ⊖ ⍉ ↑ ↓ ⊂ ⊃ ⊆ ∪ ∩ ⍳ ⍸ ⌷ ⍋ ⍒ ⊥ ⊤ ⊣ ⊢ ⍎ ⍕ ⍝`), operators (`/ \ ¨ ⍨ ∘ . ⍣ ⍤ ⍥ @`), numbers (`¯` for negative, `1E2`, `1J2` complex deferred), characters (`'a'`, `''` escape), strands (juxtaposition of literals: `1 2 3`), names, comments `⍝ …`
|
||||||
|
- [ ] Parser: right-to-left; classify each token as function, operator, value, or name; resolve valence positionally; dfn `{…}` body, tradfn `∇` header, guards `:`, control words `:If :While :For …` (Dyalog-style)
|
||||||
|
- [ ] Unit tests in `lib/apl/tests/parse.sx`
|
||||||
|
|
||||||
|
### Phase 2 — array model + scalar primitives
|
||||||
|
- [ ] Array constructor: `make-array shape ravel`, `scalar v`, `vector v…`, `enclose`/`disclose`
|
||||||
|
- [ ] Shape arithmetic: `⍴` (shape), `,` (ravel), `≢` (tally / first-axis-length), `≡` (depth)
|
||||||
|
- [ ] Scalar arithmetic primitives broadcast: `+ - × ÷ ⌈ ⌊ * ⍟ | ! ○`
|
||||||
|
- [ ] Scalar comparison primitives: `< ≤ = ≥ > ≠`
|
||||||
|
- [ ] Scalar logical: `~ ∧ ∨ ⍱ ⍲`
|
||||||
|
- [ ] Index generator: `⍳n` (vector 1..n or 0..n-1 depending on `⎕IO`)
|
||||||
|
- [ ] `⎕IO` = 1 default (Dyalog convention)
|
||||||
|
- [ ] 40+ tests in `lib/apl/tests/scalar.sx`
|
||||||
|
|
||||||
|
### Phase 3 — structural primitives + indexing
|
||||||
|
- [ ] Reshape `⍴`, ravel `,`, transpose `⍉` (full + dyadic axis spec)
|
||||||
|
- [ ] Take `↑`, drop `↓`, rotate `⌽` (last axis), `⊖` (first axis)
|
||||||
|
- [ ] Catenate `,` (last axis) and `⍪` (first axis)
|
||||||
|
- [ ] Index `⌷` (squad), bracket-indexing `A[I]` (sugar for `⌷`)
|
||||||
|
- [ ] Grade-up `⍋`, grade-down `⍒`
|
||||||
|
- [ ] Enclose `⊂`, disclose `⊃`, partition (subset deferred)
|
||||||
|
- [ ] Membership `∊`, find `⍳` (dyadic), without `~` (dyadic), unique `∪` (deferred to phase 6)
|
||||||
|
- [ ] 40+ tests in `lib/apl/tests/structural.sx`
|
||||||
|
|
||||||
|
### Phase 4 — operators (THE SHOWCASE)
|
||||||
|
- [ ] Reduce `f/` (last axis), `f⌿` (first axis) — including `∧/`, `∨/`, `+/`, `×/`, `⌈/`, `⌊/`
|
||||||
|
- [ ] Scan `f\`, `f⍀`
|
||||||
|
- [ ] Each `f¨` — applies `f` to each scalar/element
|
||||||
|
- [ ] Outer product `∘.f` — `1 2 3 ∘.× 1 2 3` ↦ multiplication table
|
||||||
|
- [ ] Inner product `f.g` — `+.×` is matrix multiply
|
||||||
|
- [ ] Commute `f⍨` — `f⍨ x` ↔ `x f x`, `x f⍨ y` ↔ `y f x`
|
||||||
|
- [ ] Compose `f∘g` — applies `g` first then `f`
|
||||||
|
- [ ] Power `f⍣n` — apply f n times; `f⍣≡` until fixed point
|
||||||
|
- [ ] Rank `f⍤k` — apply f at sub-rank k
|
||||||
|
- [ ] At `@` — selective replace
|
||||||
|
- [ ] 40+ tests in `lib/apl/tests/operators.sx`
|
||||||
|
|
||||||
|
### Phase 5 — dfns + tradfns + control flow
|
||||||
|
- [ ] Dfn `{…}` with `⍺` (left arg, may be absent → niladic/monadic), `⍵` (right arg), `∇` (recurse), guards `cond:expr`, default left arg `⍺←default`
|
||||||
|
- [ ] Local assignment via `←` (lexical inside dfn)
|
||||||
|
- [ ] Tradfn `∇` header: `R←L F R;l1;l2`, statement-by-statement, branch via `→linenum`
|
||||||
|
- [ ] Dyalog control words: `:If/:Else/:EndIf`, `:While/:EndWhile`, `:For X :In V :EndFor`, `:Select/:Case/:EndSelect`, `:Trap`/`:EndTrap`
|
||||||
|
- [ ] Niladic / monadic / dyadic dispatch (function valence at definition time)
|
||||||
|
- [ ] `lib/apl/conformance.sh` + runner, `scoreboard.json` + `scoreboard.md`
|
||||||
|
|
||||||
|
### Phase 6 — classic programs + drive corpus
|
||||||
|
- [ ] Classic programs in `lib/apl/tests/programs/`:
|
||||||
|
- [ ] `life.apl` — Conway's Game of Life as a one-liner using `⊂` `⊖` `⌽` `+/`
|
||||||
|
- [ ] `mandelbrot.apl` — complex iteration with rank-polymorphic `+ × ⌊` (or real-axis subset)
|
||||||
|
- [ ] `primes.apl` — `(2=+⌿0=A∘.|A)/A←⍳N` sieve
|
||||||
|
- [ ] `n-queens.apl` — backtracking via reduce
|
||||||
|
- [ ] `quicksort.apl` — the classic Roger Hui one-liner
|
||||||
|
- [ ] System functions: `⎕FMT`, `⎕FR` (float repr), `⎕TS` (timestamp), `⎕IO`, `⎕ML` (migration level — fixed at 1), `⎕←` (print)
|
||||||
|
- [ ] Drive corpus to 100+ green
|
||||||
|
- [ ] Idiom corpus — `lib/apl/tests/idioms.sx` covering classic Roger Hui / Phil Last idioms
|
||||||
|
|
||||||
|
## Progress log
|
||||||
|
|
||||||
|
_Newest first._
|
||||||
|
|
||||||
|
- _(none yet)_
|
||||||
|
|
||||||
|
## Blockers
|
||||||
|
|
||||||
|
- _(none yet)_
|
||||||
121
plans/common-lisp-on-sx.md
Normal file
121
plans/common-lisp-on-sx.md
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
# Common-Lisp-on-SX: conditions + restarts on delimited continuations
|
||||||
|
|
||||||
|
The headline showcase is the **condition system**. Restarts are *resumable* exceptions — every other Lisp implementation reinvents this on host-stack unwind tricks. On SX restarts are textbook delimited continuations: `signal` walks the handler chain; `invoke-restart` resumes the captured continuation at the restart point. Same delcc primitive that powers Erlang actors, expressed as a different surface.
|
||||||
|
|
||||||
|
End-state goal: ANSI Common Lisp subset with a working condition/restart system, CLOS multimethods (with `:before`/`:after`/`:around`), the LOOP macro, packages, and ~150 hand-written + classic programs.
|
||||||
|
|
||||||
|
## Scope decisions (defaults — override by editing before we spawn)
|
||||||
|
|
||||||
|
- **Syntax:** ANSI Common Lisp surface. Read tables, dispatch macros (`#'`, `#(`, `#\`, `#:`, `#x`, `#b`, `#o`, ratios `1/3`).
|
||||||
|
- **Conformance:** ANSI X3.226 *as a target*, not bug-for-bug SBCL/CCL. "Reads like CL, runs like CL."
|
||||||
|
- **Test corpus:** custom + a curated slice of `ansi-test`. Plus classic programs: condition-system demo, restart-driven debugger, multiple-dispatch geometry, LOOP corpus.
|
||||||
|
- **Out of scope:** compilation to native, FFI, sockets, threads, MOP class redefinition, full pathname/logical-pathname machinery, structures with `:include` deep customization.
|
||||||
|
- **Packages:** simple — `defpackage`/`in-package`/`export`/`use-package`/`:cl`/`:cl-user`. No nicknames, no shadowing-import edge cases.
|
||||||
|
|
||||||
|
## Ground rules
|
||||||
|
|
||||||
|
- **Scope:** only touch `lib/common-lisp/**` and `plans/common-lisp-on-sx.md`. Don't edit `spec/`, `hosts/`, `shared/`, or any other `lib/<lang>/**`. CL primitives go in `lib/common-lisp/runtime.sx`.
|
||||||
|
- **SX files:** use `sx-tree` MCP tools only.
|
||||||
|
- **Commits:** one feature per commit. Keep `## Progress log` updated and tick roadmap boxes.
|
||||||
|
|
||||||
|
## Architecture sketch
|
||||||
|
|
||||||
|
```
|
||||||
|
Common Lisp source
|
||||||
|
│
|
||||||
|
▼
|
||||||
|
lib/common-lisp/reader.sx — tokenizer + reader (read macros, dispatch chars)
|
||||||
|
│
|
||||||
|
▼
|
||||||
|
lib/common-lisp/parser.sx — AST: forms, declarations, lambda lists
|
||||||
|
│
|
||||||
|
▼
|
||||||
|
lib/common-lisp/transpile.sx — AST → SX AST (entry: cl-eval-ast)
|
||||||
|
│
|
||||||
|
▼
|
||||||
|
lib/common-lisp/runtime.sx — special forms, condition system, CLOS, packages, BIFs
|
||||||
|
```
|
||||||
|
|
||||||
|
Core mapping:
|
||||||
|
- **Symbol** = SX symbol with package prefix; package table is a flat dict.
|
||||||
|
- **Cons cell** = SX pair via `cons`/`car`/`cdr`; lists native.
|
||||||
|
- **Multiple values** = thread through `values`/`multiple-value-bind`; primary-value default for one-context callers.
|
||||||
|
- **Block / return-from** = captured continuation; `return-from name v` invokes the block-named `^k`.
|
||||||
|
- **Tagbody / go** = each tag is a continuation; `go tag` invokes it.
|
||||||
|
- **Unwind-protect** = scope frame with a cleanup thunk fired on any non-local exit.
|
||||||
|
- **Conditions / restarts** = layered handler chain on top of `handler-bind` + delcc. `signal` walks handlers; `invoke-restart` resumes a captured continuation.
|
||||||
|
- **CLOS** = generic functions are dispatch tables on argument-class lists; method combination computed lazily; `call-next-method` is a continuation.
|
||||||
|
- **Macros** = SX macros (sentinel-body) — defmacro lowers directly.
|
||||||
|
|
||||||
|
## Roadmap
|
||||||
|
|
||||||
|
### Phase 1 — reader + parser
|
||||||
|
- [ ] Tokenizer: symbols (with package qualification `pkg:sym` / `pkg::sym`), numbers (int, float, ratio `1/3`, `#xFF`, `#b1010`, `#o17`), strings `"…"` with `\` escapes, characters `#\Space` `#\Newline` `#\a`, comments `;`, block comments `#| … |#`
|
||||||
|
- [ ] Reader: list, dotted pair, quote `'`, function `#'`, quasiquote `` ` ``, unquote `,`, splice `,@`, vector `#(…)`, uninterned `#:foo`, nil/t literals
|
||||||
|
- [ ] Parser: lambda lists with `&optional` `&rest` `&key` `&aux` `&allow-other-keys`, defaults, supplied-p variables
|
||||||
|
- [ ] Unit tests in `lib/common-lisp/tests/read.sx`
|
||||||
|
|
||||||
|
### Phase 2 — sequential eval + special forms
|
||||||
|
- [ ] `cl-eval-ast`: `quote`, `if`, `progn`, `let`, `let*`, `flet`, `labels`, `setq`, `setf` (subset), `function`, `lambda`, `the`, `locally`, `eval-when`
|
||||||
|
- [ ] `block` + `return-from` via captured continuation
|
||||||
|
- [ ] `tagbody` + `go` via per-tag continuations
|
||||||
|
- [ ] `unwind-protect` cleanup frame
|
||||||
|
- [ ] `multiple-value-bind`, `multiple-value-call`, `multiple-value-prog1`, `values`, `nth-value`
|
||||||
|
- [ ] `defun`, `defparameter`, `defvar`, `defconstant`, `declaim`, `proclaim` (no-op)
|
||||||
|
- [ ] Dynamic variables — `defvar`/`defparameter` produce specials; `let` rebinds via parameterize-style scope
|
||||||
|
- [ ] 60+ tests in `lib/common-lisp/tests/eval.sx`
|
||||||
|
|
||||||
|
### Phase 3 — conditions + restarts (THE SHOWCASE)
|
||||||
|
- [ ] `define-condition` — class hierarchy rooted at `condition`/`error`/`warning`/`simple-error`/`simple-warning`/`type-error`/`arithmetic-error`/`division-by-zero`
|
||||||
|
- [ ] `signal`, `error`, `cerror`, `warn` — all walk the handler chain
|
||||||
|
- [ ] `handler-bind` — non-unwinding handlers, may decline by returning normally
|
||||||
|
- [ ] `handler-case` — unwinding handlers (delcc abort)
|
||||||
|
- [ ] `restart-case`, `with-simple-restart`, `restart-bind`
|
||||||
|
- [ ] `find-restart`, `invoke-restart`, `invoke-restart-interactively`, `compute-restarts`
|
||||||
|
- [ ] `with-condition-restarts` — associate restarts with a specific condition
|
||||||
|
- [ ] `*break-on-signals*`, `*debugger-hook*` (basic)
|
||||||
|
- [ ] Classic programs in `lib/common-lisp/tests/programs/`:
|
||||||
|
- [ ] `restart-demo.lisp` — division with `:use-zero` and `:retry` restarts
|
||||||
|
- [ ] `parse-recover.lisp` — parser with skipped-token restart
|
||||||
|
- [ ] `interactive-debugger.lisp` — ASCII REPL using `:debugger-hook`
|
||||||
|
- [ ] `lib/common-lisp/conformance.sh` + runner, `scoreboard.json` + `scoreboard.md`
|
||||||
|
|
||||||
|
### Phase 4 — CLOS
|
||||||
|
- [ ] `defclass` with `:initarg`/`:initform`/`:accessor`/`:reader`/`:writer`/`:allocation`
|
||||||
|
- [ ] `make-instance`, `slot-value`, `(setf slot-value)`, `with-slots`, `with-accessors`
|
||||||
|
- [ ] `defgeneric` with `:method-combination` (standard, plus `+`, `and`, `or`)
|
||||||
|
- [ ] `defmethod` with `:before` / `:after` / `:around` qualifiers
|
||||||
|
- [ ] `call-next-method` (continuation), `next-method-p`
|
||||||
|
- [ ] `class-of`, `find-class`, `slot-boundp`, `change-class` (basic)
|
||||||
|
- [ ] Multiple dispatch — method specificity by argument-class precedence list
|
||||||
|
- [ ] Built-in classes registered for tagged values (`integer`, `float`, `string`, `symbol`, `cons`, `null`, `t`)
|
||||||
|
- [ ] Classic programs:
|
||||||
|
- [ ] `geometry.lisp` — `intersect` generic dispatching on (point line), (line line), (line plane)…
|
||||||
|
- [ ] `mop-trace.lisp` — `:before` + `:after` printing call trace
|
||||||
|
|
||||||
|
### Phase 5 — macros + LOOP + reader macros
|
||||||
|
- [ ] `defmacro`, `macrolet`, `symbol-macrolet`, `macroexpand-1`, `macroexpand`
|
||||||
|
- [ ] `gensym`, `gentemp`
|
||||||
|
- [ ] `set-macro-character`, `set-dispatch-macro-character`, `get-macro-character`
|
||||||
|
- [ ] **The LOOP macro** — iteration drivers (`for … in/across/from/upto/downto/by`, `while`, `until`, `repeat`), accumulators (`collect`, `append`, `nconc`, `count`, `sum`, `maximize`, `minimize`), conditional clauses (`if`/`when`/`unless`/`else`), termination (`finally`/`thereis`/`always`/`never`), `named` blocks
|
||||||
|
- [ ] LOOP test corpus: 30+ tests covering all clause types
|
||||||
|
|
||||||
|
### Phase 6 — packages + stdlib drive
|
||||||
|
- [ ] `defpackage`, `in-package`, `export`, `use-package`, `import`, `find-package`
|
||||||
|
- [ ] Package qualification at the reader level — `cl:car`, `mypkg::internal`
|
||||||
|
- [ ] `:common-lisp` (`:cl`) and `:common-lisp-user` (`:cl-user`) packages
|
||||||
|
- [ ] Sequence functions — `mapcar`, `mapc`, `mapcan`, `reduce`, `find`, `find-if`, `position`, `count`, `every`, `some`, `notany`, `notevery`, `remove`, `remove-if`, `subst`
|
||||||
|
- [ ] List ops — `assoc`, `getf`, `nth`, `last`, `butlast`, `nthcdr`, `tailp`, `ldiff`
|
||||||
|
- [ ] String ops — `string=`, `string-upcase`, `string-downcase`, `subseq`, `concatenate`
|
||||||
|
- [ ] FORMAT — basic directives `~A`, `~S`, `~D`, `~F`, `~%`, `~&`, `~T`, `~{...~}` (iteration), `~[...~]` (conditional), `~^` (escape), `~P` (plural)
|
||||||
|
- [ ] Drive corpus to 200+ green
|
||||||
|
|
||||||
|
## Progress log
|
||||||
|
|
||||||
|
_Newest first._
|
||||||
|
|
||||||
|
- _(none yet)_
|
||||||
|
|
||||||
|
## Blockers
|
||||||
|
|
||||||
|
- _(none yet)_
|
||||||
96
plans/hs-blockers-drain.md
Normal file
96
plans/hs-blockers-drain.md
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
# HS conformance — blockers drain
|
||||||
|
|
||||||
|
Goal: take hyperscript conformance from **1277/1496 (85.4%)** to **1496/1496 (100%)** by clearing the blocked clusters and the design-done Bucket E subsystems.
|
||||||
|
|
||||||
|
This plan exists because the per-iteration `loops/hs` agent can't fit these into its 30-min budget — they need dedicated multi-commit sit-downs. Track progress here; refer to `plans/hs-conformance-to-100.md` for the canonical cluster ledger.
|
||||||
|
|
||||||
|
## Current state (2026-04-25)
|
||||||
|
|
||||||
|
- Loop running in `/root/rose-ash-loops/hs` (branch `loops/hs`)
|
||||||
|
- sx-tree MCP **fixed** (was a session-stale binary issue — restart of claude in the tmux window picked it up). Loop hinted to retry **#32**, **#29** first.
|
||||||
|
- Recent loop progress: ~1 commit/6h — easy wins drained, what's left needs focused attention.
|
||||||
|
|
||||||
|
## Remaining work
|
||||||
|
|
||||||
|
### Bucket-A/B/C blockers (small, in-place fixes)
|
||||||
|
|
||||||
|
| # | Cluster | Tests | Effort | Blocker | Fix sketch |
|
||||||
|
|---|---------|------:|--------|---------|------------|
|
||||||
|
| **17** | `tell` semantics | +3 | ~1h | Implicit-default-target ambiguity. `bare add .bar` inside `tell X` should target `X` but explicit `to me` must reach the original element. | Add `beingTold` symbol distinct from `me`; bare commands compile to `beingTold-or-me`; explicit `me` always the original. |
|
||||||
|
| **22** | window global fn fallback | +2-4 | ~1h | `foo()` where `foo` isn't SX-defined needs to fall back to `(host-global "foo")`. Three attempts failed: guard (host-level error not catchable), `env-has?` (not in HS kernel), `hs-win-call` (NativeFn not callable from CALL). | Add `symbol-bound?` predicate to HS kernel **OR** a host-call-fn primitive with arity-agnostic dispatch. |
|
||||||
|
| **29** | `hyperscript:before:init` / `:after:init` / `:parse-error` events | +4-6 | ~30m (post sx-tree fix) | Was sx-tree MCP outage. Now unblocked — loop should retry. 4 of 6 tests need stricter parser error-rejection (out of scope; mark partial). | Edit `integration.sx` to fire DOM events at activation boundaries. |
|
||||||
|
|
||||||
|
### Bucket D — medium features
|
||||||
|
|
||||||
|
| # | Cluster | Tests | Effort | Status |
|
||||||
|
|---|---------|------:|--------|--------|
|
||||||
|
| **31** | runtime null-safety error reporting | **+15-18** | **2-4h** | **THIS SESSION'S TARGET.** Plan node fully spec'd: 5 pieces of work. |
|
||||||
|
| **32** | MutationObserver mock + `on mutation` | +10-15 | ~2h | Was sx-tree-blocked. Now unblocked — loop hinted to retry. Multi-file: parser, compiler, runtime, runner mock, generator skip-list. |
|
||||||
|
| **33** | cookie API | +2 (remaining) | ~30m | Partial done (+3). Remaining 2 need `hs-method-call` runtime fallback for unknown methods + `hs-for-each` recognising host-array/proxy collections. |
|
||||||
|
| 34 | event modifier DSL | +6-8 | ~1-2h | `elsewhere`, `every`, count filters (`once`/`twice`/`3 times`/ranges), `from elsewhere`. Pending. |
|
||||||
|
| 35 | namespaced `def` | +3 | ~30m | Pending. |
|
||||||
|
|
||||||
|
### Bucket E — subsystems (design docs landed, multi-commit each)
|
||||||
|
|
||||||
|
Each has a design doc with a step-by-step checklist. These are 1-2 days of focused work each, not loop-fits.
|
||||||
|
|
||||||
|
| # | Subsystem | Tests | Design doc | Branch |
|
||||||
|
|---|-----------|------:|------------|--------|
|
||||||
|
| 36 | WebSocket + `socket` + RPC Proxy | +12-16 | `plans/designs/e36-websocket.md` | `worktree-agent-a9daf73703f520257` |
|
||||||
|
| 37 | Tokenizer-as-API | +16-17 | `plans/designs/e37-tokenizer-api.md` | `worktree-agent-a6bb61d59cc0be8b4` |
|
||||||
|
| 38 | SourceInfo API | +4 | `plans/designs/e38-sourceinfo.md` | `agent-e38-sourceinfo` |
|
||||||
|
| 39 | WebWorker plugin (parser-only stub) | +1 | `plans/designs/e39-webworker.md` | `hs-design-e39-webworker` |
|
||||||
|
| 40 | Real Fetch / non-2xx / before-fetch | +7 | `plans/designs/e40-real-fetch.md` | `worktree-agent-a94612a4283eaa5e0` |
|
||||||
|
|
||||||
|
### Bucket F — generator translation gaps
|
||||||
|
|
||||||
|
~25 tests SKIP'd because `tests/playwright/generate-sx-tests.py` bails with `return None`. Single dedicated generator-repair sit-down once Bucket D is drained. ~half-day.
|
||||||
|
|
||||||
|
## Order of attack
|
||||||
|
|
||||||
|
In approximate cost-per-test order:
|
||||||
|
|
||||||
|
1. **Loop self-heal** (no human work) — wait for #29, #32 to land via the running loop ⏱️ ~next 1-2 hours
|
||||||
|
2. **#31 null-safety** — biggest scoped single win, dedicated worktree agent (this session)
|
||||||
|
3. **#33 cookie API remainder** — quick partial completion
|
||||||
|
4. **#17 / #22 / #34 / #35** — small fiddly fixes, one sit-down each
|
||||||
|
5. **Bucket E** — pick one subsystem at a time. **#39 (WebWorker stub) first** — single commit, smallest. Then **#38 (SourceInfo)** — 4 commits. Then the bigger three (#36, #37, #40).
|
||||||
|
6. **Bucket F** — generator repair sweep at the end.
|
||||||
|
|
||||||
|
Estimated total to 100%: ~10-15 days of focused work, parallelisable across branches.
|
||||||
|
|
||||||
|
## Cluster #31 spec (full detail)
|
||||||
|
|
||||||
|
The plan note from `hs-conformance-to-100.md`:
|
||||||
|
|
||||||
|
> 18 tests in `runtimeErrors`. When accessing `.foo` on nil, emit a structured error with position info. One coordinated fix in the compiler emit paths for property access, function calls, set/put.
|
||||||
|
|
||||||
|
**Required pieces:**
|
||||||
|
|
||||||
|
1. **Generator-side `eval-hs-error` helper + recognizer** for `expect(await error("HS")).toBe("MSG")` blocks. In `tests/playwright/generate-sx-tests.py`.
|
||||||
|
2. **Runtime helpers** in `lib/hyperscript/runtime.sx`:
|
||||||
|
- `hs-null-error!` raising `'<sel>' is null`
|
||||||
|
- `hs-named-target` — wraps a query result with the original selector source
|
||||||
|
- `hs-named-target-list` — same for list results
|
||||||
|
3. **Compiler patches at every target-position `(query SEL)` emit** — wrap in named-target carrying the original selector source. ~17 command emit paths in `lib/hyperscript/compiler.sx`:
|
||||||
|
add, remove, hide, show, measure, settle, trigger, send, set, default, increment, decrement, put, toggle, transition, append, take.
|
||||||
|
4. **Function-call null-check** at bare `(name)`, `hs-method-call`, and `host-get` chains, deriving the leftmost-uncalled-name (`'x'` / `'x.y'`) from the parse tree.
|
||||||
|
5. **Possessive-base null-check** (`set x's y to true` → `'x' is null`).
|
||||||
|
|
||||||
|
**Files in scope:**
|
||||||
|
- `lib/hyperscript/runtime.sx` (new helpers)
|
||||||
|
- `lib/hyperscript/compiler.sx` (~17 emit-path edits)
|
||||||
|
- `tests/playwright/generate-sx-tests.py` (test recognizer)
|
||||||
|
- `tests/hs-run-filtered.js` (if mock helpers needed)
|
||||||
|
- `shared/static/wasm/sx/hs-runtime.sx` + `hs-compiler.sx` (WASM staging copies)
|
||||||
|
|
||||||
|
**Approach:** target-named pieces incrementally — runtime helpers first (no compiler change), then compiler emit paths in batches (group similar commands), then function-call/possessive at the end. Each batch is one commit if it lands +N tests; mark partial if it only unlocks part.
|
||||||
|
|
||||||
|
**Watch for:** smoke-range regressions (tests flipping pass→fail). Each commit: rerun smoke 0-195 and the `runtimeErrors` suite.
|
||||||
|
|
||||||
|
## Notes for future sessions
|
||||||
|
|
||||||
|
- `plans/hs-conformance-to-100.md` is the canonical cluster ledger — update it on every commit.
|
||||||
|
- `plans/hs-conformance-scoreboard.md` is the live tally — bump `Merged:` and the bucket roll-up.
|
||||||
|
- Loop has scope rule "never edit `spec/evaluator.sx` or broader SX kernel" — most fixes here stay in `lib/hyperscript/**`, `tests/`, generator. If a fix needs kernel work, surface to the user; don't merge silently.
|
||||||
|
- Cluster #22's `symbol-bound?` predicate would be a kernel addition — that's a real cross-boundary scope expansion.
|
||||||
@@ -65,7 +65,7 @@ Each item: implement → tests → update progress. Mark `[x]` when tests green.
|
|||||||
- [x] Punctuation: `( ) { } [ ] , ; : . ...`
|
- [x] Punctuation: `( ) { } [ ] , ; : . ...`
|
||||||
- [x] Operators: `+ - * / % ** = == === != !== < > <= >= && || ! ?? ?: & | ^ ~ << >> >>> += -= ...`
|
- [x] Operators: `+ - * / % ** = == === != !== < > <= >= && || ! ?? ?: & | ^ ~ << >> >>> += -= ...`
|
||||||
- [x] Comments (`//`, `/* */`)
|
- [x] Comments (`//`, `/* */`)
|
||||||
- [x] Automatic Semicolon Insertion (defer — initially require semicolons)
|
- [ ] Automatic Semicolon Insertion (defer — initially require semicolons)
|
||||||
|
|
||||||
### Phase 2 — Expression parser (Pratt-style)
|
### Phase 2 — Expression parser (Pratt-style)
|
||||||
- [x] Literals → AST nodes
|
- [x] Literals → AST nodes
|
||||||
@@ -124,7 +124,7 @@ Each item: implement → tests → update progress. Mark `[x]` when tests green.
|
|||||||
- [x] Closures — work via SX `fn` env capture
|
- [x] Closures — work via SX `fn` env capture
|
||||||
- [x] Rest params (`...rest` → `&rest`)
|
- [x] Rest params (`...rest` → `&rest`)
|
||||||
- [x] Default parameters (desugar to `if (param === undefined) param = default`)
|
- [x] Default parameters (desugar to `if (param === undefined) param = default`)
|
||||||
- [x] `var` hoisting (shallow — collects direct `var` decls, emits `(define name :js-undefined)` before funcdecls)
|
- [ ] `var` hoisting (deferred — treated as `let` for now)
|
||||||
- [ ] `let`/`const` TDZ (deferred)
|
- [ ] `let`/`const` TDZ (deferred)
|
||||||
|
|
||||||
### Phase 8 — Objects, prototypes, `this`
|
### Phase 8 — Objects, prototypes, `this`
|
||||||
@@ -158,272 +158,6 @@ Each item: implement → tests → update progress. Mark `[x]` when tests green.
|
|||||||
|
|
||||||
Append-only record of completed iterations. Loop writes one line per iteration: date, what was done, test count delta.
|
Append-only record of completed iterations. Loop writes one line per iteration: date, what was done, test count delta.
|
||||||
|
|
||||||
- 2026-05-10 — **`String.prototype.repeat` no longer arity-collides with itself; raises RangeError on negative or +Infinity counts.** Earlier JSON.stringify iteration introduced a 2-arg `js-string-repeat` that shadowed the existing 3-arg `(s n acc)` accumulator implementation, breaking every `s.repeat(n)` call with "expects 2 args, got 3". Renamed the accumulator helper to `js-string-repeat-loop` and made `js-string-repeat` a 2-arg facade that delegates. Hooked the repeat method to raise RangeError when `count < 0` or `count = Infinity` per spec. Result: built-ins/String/prototype/repeat 7/13 → 11/13 (+4). conformance.sh: 148/148.
|
|
||||||
|
|
||||||
- 2026-05-10 — **test262-runner inlines small upstream harness includes (`nans.js`, `sta.js`, `byteConversionValues.js`, `compareArray.js`) per-test.** The runner parsed `includes:` frontmatter but never used it, so tests like `built-ins/isNaN/return-true-nan.js` (which depends on `var NaNs = [...]`) failed with "ReferenceError: undefined symbol". Added `_load_harness_include` (cached) and `assemble_source` now prepends each allowlisted include's source to the test. Allowlist excludes large helpers like `propertyHelper.js` because per-test js-eval+JIT cost on a 371-line harness pushes tests over the 15s per-test timeout (regressed Math/abs 7/7 → 4/7 in a first-pass attempt before allowlisting). Result: built-ins/isNaN 2/7 → 3/7. conformance.sh: 148/148.
|
|
||||||
|
|
||||||
- 2026-05-10 — **Real `Date.prototype.setFullYear/setMonth/setDate/setHours/setMinutes/setSeconds/setMilliseconds` (+ UTC variants) and a corrected `setTime`.** All Date setters were missing — only `setTime` existed and didn't validate. Added a unified `js-date-setter(d, field, args)` that decomposes the current ms into `(y mo da hh mm ss msv)` via `js-date-decompose`, splices in the `args` per the field's optional-arg contract (e.g. `setHours(h, m?, s?, ms?)`), recomposes via `js-date-civil-to-days`, and TimeClips at ±8.64e15. NaN args anywhere → ms set to NaN. Wired all 14 setters to the helper. Hit a parser gotcha: SX `cond` clause body is single-form only — multi-expression bodies like `(else (dict-set! ...) new-ms)` silently treat the second form as `(<first-result> new-ms)` ("Not callable: false"). Wrapped these in `(begin ...)`. Result: setFullYear 5/18 → 13/18 (+8). setHours 5/21 → 15/21 (+10). setMonth 3/15 → 9/15 (+6). setMinutes 4/16 → 10/16 (+6). setSeconds 3/15 → 9/15 (+6). setDate 2/12 → 6/12 (+4). setMilliseconds 2/12 → 6/12 (+4). setTime 4/9 → 6/9 (+2). conformance.sh: 148/148.
|
|
||||||
|
|
||||||
- 2026-05-10 — **`Object.assign` keys now visible to `Object.keys` / `JSON.stringify`.** `Object.assign({}, {a:1})` was mutating the target via `dict-set!` which bypasses our `__js_order__` insertion-order side table; `Object.keys(t)` (which iterates `__js_order__` when present) returned `[]`, and `JSON.stringify` saw nothing. Switched `js-object-assign` to use `js-set-prop` (which calls `js-obj-order-add!` on new keys) for both dict and string sources. Result: built-ins/Object/assign 13/25 → 14/25. conformance.sh: 148/148.
|
|
||||||
|
|
||||||
- 2026-05-10 — **User functions' `prototype` chain through Object.prototype + auto-set `constructor`.** Per ES spec, every function's `prototype` slot defaults to `{ constructor: F, __proto__: Object.prototype }`. Our `js-get-ctor-proto` lazily created a fresh empty `(dict)` for user functions on first access — so `(new F) instanceof Object` was `false`, `F.prototype.constructor` was undefined, and `x.constructor === F` failed. Now the lazy-init seeds the proto with `__proto__ → Object.prototype` and `constructor → F` before caching in `__js_proto_table__`. Result: language/expressions/instanceof 25/30 → 26/30. conformance.sh: 148/148.
|
|
||||||
|
|
||||||
- 2026-05-10 — **Postfix `++`/`--` reject a preceding LineTerminator (ASI).** Per ES spec, `x\n++;` is a syntax error: no LineTerminator allowed between LHS and postfix `++`/`--`. Our `jp-parse-postfix` was matching `++`/`--` regardless of whether the preceding token had `:nl true`. Added `(not (jp-token-nl? st))` guard so newline-before-`++` makes the postfix arm fall through, the `++` then becomes a prefix-expr starting a new statement, which fails to parse and the runner classifies as SyntaxError. Result: language/expressions/postfix-increment 16/30 → 18/30 (+2). postfix-decrement 16/30 → 18/30 (+2). conformance.sh: 148/148.
|
|
||||||
|
|
||||||
- 2026-05-10 — **Parse-time SyntaxError when `let`/`const`/`function`/`class` appear as a single-statement body of `if`/`while`/`do`/`for`/labeled.** Per ES grammar, those positions accept a Statement, not a Declaration — only block bodies (`{ ... }`) may contain Declarations. Added `jp-disallow-decl-stmt!` helper that, when the next token is a Declaration keyword in single-statement context, raises SyntaxError. The `let` arm checks for `let <ident>`, `let [`, or `let {` to avoid mis-rejecting `let;` (where `let` is just an identifier expression). Hook calls in `jp-parse-if-stmt` (then + else branches), `jp-parse-while-stmt`, `jp-parse-do-while-stmt`, both for-of/in and C-for body sites, and the labeled-statement entry. Result: language/statements/while 16/30 → 20/30. statements/labeled 4/15 → 7/15. statements/if 20/30 → 21/30. conformance.sh: 148/148.
|
|
||||||
|
|
||||||
- 2026-05-10 — **Parse-time SyntaxError for `break`/`continue` outside loops/switches and `return` outside functions; `void <expr>` evaluates `<expr>` for side effects.** Parser tracks `:loop-depth`, `:switch-depth`, and `:fn-depth` on the state dict (initialized to 0). `jp-parse-while-stmt`, `jp-parse-do-while-stmt`, `jp-parse-for-stmt` (both for-of/in and C-for) bump `:loop-depth` around body parsing; `jp-parse-switch-stmt` bumps `:switch-depth`; new `jp-parse-fn-body` and `jp-parse-arrow-body` save+reset loop/switch depth and bump `:fn-depth` (so `break` inside an outer loop's nested function is rejected). Bare `break` requires `loop-depth > 0 OR switch-depth > 0`; bare `continue` requires `loop-depth > 0`; `return` requires `fn-depth > 0`. Separately, `void <expr>` was compiling to just `:js-undefined` (dropping the expression entirely); now `(begin <expr> :js-undefined)` so side effects fire. Result: language/statements/return 4/15 → 14/15 (+10). statements/break 9/20 → 12/20. statements/continue 12/24 → 15/24. expressions/void 7/9 → 8/9. conformance.sh: 148/148.
|
|
||||||
|
|
||||||
- 2026-05-10 — **`Math.hypot` and `Math.cbrt` honour spec edges for NaN, ±Infinity, and ±0.** `Math.hypot(NaN, Infinity)` was returning NaN instead of +Infinity (spec: any ±Infinity arg dominates NaN). Rewrote `js-math-hypot` to scan args once tracking inf/nan flags, return +Infinity if any arg is ±Infinity, else NaN if any was NaN, else `sqrt(sum of squares)`. `Math.cbrt(NaN)` was 0 (because `pow(NaN, 1/3)` produced 0 in our path); also `Math.cbrt(-0)` returned +0 instead of -0. Added explicit short-circuits: NaN→NaN, ±Infinity→arg, ±0→arg, plus changed `(/ 1 3)` (rational) to `(/ 1.0 3.0)` (inexact) to avoid rational fractional-power oddities. Result: built-ins/Math/hypot 9/11 → 10/11. Math/cbrt 3/4 → 4/4. conformance.sh: 148/148.
|
|
||||||
|
|
||||||
- 2026-05-10 — **`globalThis.globalThis === globalThis`; `Number.prototype.toFixed` honours digit-range and ≥1e21 fallback.** (1) `globalThis` was bound to `nil` in the global object literal (originally to dodge an inspect-cycle hang) — added `(dict-set! js-global "globalThis" js-global)` after the literal so `globalThis.globalThis === globalThis` per spec. (2) `Number.prototype.toFixed` rewrites: RangeError when fractionDigits is NaN or outside `[0,100]` (was silently producing garbage), and for `|x| >= 1e21` returns `js-number-to-string` (the value's own ToString) per spec step 9. conformance.sh: 148/148.
|
|
||||||
|
|
||||||
- 2026-05-10 — **`delete <ident>` returns `false` instead of `true` per non-strict spec.** ES non-strict semantics: `delete x` where `x` is a declared binding (variable / function / parameter) returns `false` and does not unbind. Our transpiler was emitting `true` for any `delete <expr>` whose argument wasn't a member or index access. Now `delete <js-ident>` → `false`, and `delete <js-paren expr>` recurses on the inner expression so `delete (1+2)` still works. Result: language/expressions/delete 14/30 → 18/30 (+4). conformance.sh: 148/148.
|
|
||||||
|
|
||||||
- 2026-05-10 — **Parser rejects unary-op directly before `**` (e.g. `-1 ** 2`, `delete o.p ** 2`, `!x ** 2`, `~x ** 2`) per ES spec.** ES disallows `UnaryExpression ** ExponentiationExpression`; only `UpdateExpression ** ExponentiationExpression` and `(<UnaryExpr>) ** ...` are legal. Added a guard in `jp-binary-loop`: when op is `**` and the LHS is a `(js-unop ...)` node, raise SyntaxError. Parens are made transparent for everything except this check via a new `jp-paren-wrap` helper that emits `(js-paren <unop>)` only when wrapping an explicit unary op (so `(-1) ** 2` parses fine), and a new `js-paren` AST tag in `js-transpile` that just unwraps. Result: language/expressions/exponentiation 25/30 → 28/30 (+3). conformance.sh: 148/148.
|
|
||||||
|
|
||||||
- 2026-05-10 — **`Math.round` / `Math.max` / `Math.min` honour spec edge cases for NaN, ±Infinity, and ±0.** `Math.round(NaN)` was returning 0 because `floor(NaN+0.5)` doesn't propagate NaN; ditto `±Infinity` paths. `Math.max({})` silently returned `-Infinity` (initial accumulator) because the first arg wasn't ToNumber'd. `Math.max(0, -0)` returned `-0` because `>` doesn't distinguish them. Rewrites: round NaN/±Infinity/±0 short-circuits; max/min ToNumber the first arg, propagate NaN immediately, and use a `js-is-positive-zero?` (rational-safe) tiebreaker so `Math.max(0, -0) === 0` per spec. Result: built-ins/Math/round 5/10 → 8/10 (+3). Math/max 6/9 → 8/9 (+2). Math/min 6/9 → 8/9 (+2). conformance.sh: 148/148.
|
|
||||||
|
|
||||||
- 2026-05-10 — **`Map.prototype.*` and `Set.prototype.*` raise TypeError when called on non-Map / non-Set `this`.** All five `js-map-do-*` and four `js-set-do-*` helpers were assuming `this` had `__map_keys__` / `__set_items__`, so `Map.prototype.clear.call({})` silently returned undefined (after creating dangling state) instead of throwing. Added `js-map-check!` / `js-set-check!` guards run as the first step of each method; raise spec-correct `TypeError` instances. Result: built-ins/Map 18/30 → 22/30 (+4). built-ins/Set 15/30 → 28/30 (+13). conformance.sh: 148/148.
|
|
||||||
|
|
||||||
- 2026-05-10 — **`Date.UTC` / `new Date(...)` propagate NaN/±Infinity arguments and return NaN.** `Date.UTC()` (no args) returned 0 instead of NaN; `Date.UTC(NaN, ...)` did the math and produced bogus ms; `new Date(year, NaN)` constructed a normal Date instead of an invalid one. Added `js-date-args-have-nan?` (also detects ±Infinity and propagates from rationals) used by both `Date.UTC` and the multi-arg constructor branch; UTC now returns NaN on no-arg / any-NaN-arg / out-of-range result, and `new Date(args)` stores NaN in `__date_value__` when any arg is NaN. Also fixed `js-date-from-one(undefined)` to return NaN. Result: built-ins/Date/UTC 6/16 → 10/16 (+4). Date 17/30 → 26/30 (timeouts dropped from 12 → 4 because invalid Dates now short-circuit). conformance.sh: 148/148.
|
|
||||||
|
|
||||||
- 2026-05-10 — **Real `Date` construction + getters via Howard-Hinnant civil-day arithmetic.** `js-date-from-parts` now computes a true ms-since-epoch from `(year, month, day, hour, min, sec, ms)` via `js-date-civil-to-days` (the inverse of last iteration's `days-to-ymd`), with the legacy 2-digit-year coercion (0..99 → 1900+y). `getFullYear/Month/Date/Day/Hours/Minutes/Seconds/Milliseconds` (UTC + non-UTC) all share a new `js-date-getter`: TypeErrors on non-Date this, returns NaN on invalid time, otherwise decomposes ms into y/m/d/h/m/s/ms/dow. Plus added `Date.prototype.constructor = Date` (was missing). Result: each of the 8 Date getter categories went 2/6 → 5/6 (+3 each, +24 total). Date toISOString 11/16 → 13/16. Some Date construction-loop tests now exceed the 15s per-test timeout — the new civil math is heavier than the old (year-1970)*ms-per-year approximation, but correctness wins. conformance.sh: 148/148.
|
|
||||||
|
|
||||||
- 2026-05-10 — **`Date.prototype.toISOString` produces real `YYYY-MM-DDTHH:mm:ss.sssZ` and validates input.** Old `js-date-iso` only computed the year and hardcoded the rest as `01-01T00:00:00.000Z`. Added: (1) TypeError when this isn't a Date (no `__js_is_date__` slot); (2) RangeError when ms is NaN, undefined, or |ms| > 8.64e15; (3) full date breakdown via Howard-Hinnant `days_to_civil` algorithm (`js-date-days-to-ymd`) → year/month/day, plus modular hours/min/sec/ms; (4) extended-year format `±YYYYYY` for years outside 0..9999. Result: built-ins/Date/prototype/toISOString 7/16 → 11/16 (+4). Date 21/30. conformance.sh: 148/148.
|
|
||||||
|
|
||||||
- 2026-05-10 — **`JSON.stringify` honours `replacer` (function + array forms), `space`, and `toJSON`.** Previous impl ignored the second/third arguments entirely and never called `toJSON`. Rewrote around a `js-json-serialize-property(key, holder, rep-fn, rep-keys, gap, indent)` core: walks `toJSON` first, then replacer-fn (with `holder` as `this`); arrays-as-replacer become a property-name allowlist; numeric `space` clamped to 0..10 spaces, string `space` truncated to 10 chars, non-empty gap activates indented output with `:` → `: ` separator. Number wrapper / String wrapper / Boolean wrapper unwrap before serialization; non-finite numbers serialize as `"null"`; functions serialize as `undefined`. Result: built-ins/JSON/stringify 6/30 → 14/30 (+8). conformance.sh: 148/148.
|
|
||||||
|
|
||||||
- 2026-05-10 — **`JSON.parse` raises spec-correct `SyntaxError` instances and rejects malformed input.** Previously `JSON.parse("12 34")` silently returned `12` (no trailing-content check), `JSON.parse('" | |||||||