kernel: reader macros + 8 tests (Phase 1 closure) [consumes-lex]
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 44s

Parser now reads 'expr, \`expr, ,expr, ,@expr as the four standard
shorthands. Quote uses existing $quote operative; quasiquote /
unquote / unquote-splicing recognised but not yet expanded at runtime
(left for first consumer to drive). 218 tests total across six suites.
This commit is contained in:
2026-05-11 21:01:01 +00:00
parent d896685555
commit c21eb9d5ad
3 changed files with 40 additions and 2 deletions

View File

@@ -37,7 +37,10 @@
(= c "(")
(= c ")")
(= c "\"")
(= c ";"))))
(= c ";")
(= c "'")
(= c "`")
(= c ","))))
;; Numeric grammar: [+-]? (digit+ ('.' digit+)? | '.' digit+) ([eE][+-]?digit+)?
(define
@@ -199,6 +202,16 @@
((= (at) "(") (do (adv) (read-list (list))))
((= (at) "\"")
(do (adv) (kernel-string-make (read-string-body ""))))
((= (at) "'")
(do (adv) (list "$quote" (read-form))))
((= (at) "`")
(do (adv) (list "$quasiquote" (read-form))))
((= (at) ",")
(do (adv)
(cond
((= (at) "@")
(do (adv) (list "$unquote-splicing" (read-form))))
(:else (list "$unquote" (read-form))))))
(:else (classify-atom (read-atom-body ""))))))
(define
read-list

View File

@@ -131,4 +131,28 @@
(knl-test "identity: wrap" (kernel-parse "wrap") "wrap")
(knl-test "identity: unwrap" (kernel-parse "unwrap") "unwrap")
;; ── reader macros ─────────────────────────────────────────────────
(knl-test "reader: 'foo → ($quote foo)"
(kernel-parse "'foo") (list "$quote" "foo"))
(knl-test "reader: '(a b c)"
(kernel-parse "'(a b c)") (list "$quote" (list "a" "b" "c")))
(knl-test "reader: nested quotes"
(kernel-parse "''x")
(list "$quote" (list "$quote" "x")))
(knl-test "reader: ` quasiquote"
(kernel-parse "`x") (list "$quasiquote" "x"))
(knl-test "reader: , unquote"
(kernel-parse ",x") (list "$unquote" "x"))
(knl-test "reader: ,@ unquote-splicing"
(kernel-parse ",@x") (list "$unquote-splicing" "x"))
(knl-test "reader: quasi-mix"
(kernel-parse "`(a ,b ,@c)")
(list "$quasiquote"
(list "a"
(list "$unquote" "b")
(list "$unquote-splicing" "c"))))
(knl-test "reader: quote separates from neighbouring atom"
(kernel-parse "(a 'b c)")
(list "a" (list "$quote" "b") "c"))
(define knl-tests-run! (fn () {:total (+ knl-test-pass knl-test-fail) :passed knl-test-pass :failed knl-test-fail :fails knl-test-fails}))