Use match for value dispatch in evaluator and compiler
Convert large cond chains doing string equality dispatch to use the match special form: step-eval-list (42 arms), step-continue (31 arms), compile-list (30 arms), ho-setup-dispatch (7 arms), value-matches-type? (10 arms). Also fix test-canonical.sx to use defsuite/deftest format and load canonical.sx in both test runners. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
1445
spec/evaluator.sx
1445
spec/evaluator.sx
File diff suppressed because it is too large
Load Diff
@@ -1,91 +1,97 @@
|
||||
(test-group
|
||||
(defsuite
|
||||
"canonical-serialize"
|
||||
(test "nil" (assert= (canonical-serialize nil) "nil"))
|
||||
(test
|
||||
(deftest "nil" (assert= (canonical-serialize nil) "nil"))
|
||||
(deftest
|
||||
"booleans"
|
||||
(assert= (canonical-serialize true) "true")
|
||||
(assert= (canonical-serialize false) "false"))
|
||||
(test
|
||||
(deftest
|
||||
"integers"
|
||||
(assert= (canonical-serialize 0) "0")
|
||||
(assert= (canonical-serialize 42) "42")
|
||||
(assert= (canonical-serialize -7) "-7"))
|
||||
(test
|
||||
(deftest
|
||||
"strings"
|
||||
(assert= (canonical-serialize "hello") "\"hello\"")
|
||||
(assert= (canonical-serialize "") "\"\"")
|
||||
(assert= (canonical-serialize "a\"b") "\"a\\\"b\""))
|
||||
(test
|
||||
(deftest
|
||||
"symbols"
|
||||
(assert= (canonical-serialize (quote deref)) "deref")
|
||||
(assert= (canonical-serialize (quote swap!)) "swap!"))
|
||||
(test
|
||||
(deftest
|
||||
"keywords"
|
||||
(assert= (canonical-serialize :class) ":class")
|
||||
(assert= (canonical-serialize :arity) ":arity"))
|
||||
(test "empty list" (assert= (canonical-serialize (list)) "()"))
|
||||
(test "flat list" (assert= (canonical-serialize (list 1 2 3)) "(1 2 3)"))
|
||||
(test
|
||||
(assert= (canonical-serialize (make-keyword "class")) ":class")
|
||||
(assert= (canonical-serialize (make-keyword "arity")) ":arity"))
|
||||
(deftest "empty list" (assert= (canonical-serialize (list)) "()"))
|
||||
(deftest
|
||||
"flat list"
|
||||
(assert= (canonical-serialize (list 1 2 3)) "(1 2 3)"))
|
||||
(deftest
|
||||
"nested list"
|
||||
(assert=
|
||||
(canonical-serialize
|
||||
(list (quote div) :class "flex" (list (quote h2) "title")))
|
||||
(list
|
||||
(quote div)
|
||||
(make-keyword "class")
|
||||
"flex"
|
||||
(list (quote h2) "title")))
|
||||
"(div :class \"flex\" (h2 \"title\"))"))
|
||||
(test
|
||||
(deftest
|
||||
"dict keys sorted"
|
||||
(let
|
||||
((d (dict "zebra" 1 "alpha" 2 "middle" 3)))
|
||||
((d {:zebra 1 :middle 3 :alpha 2}))
|
||||
(assert= (canonical-serialize d) "{:alpha 2 :middle 3 :zebra 1}")))
|
||||
(test
|
||||
(deftest
|
||||
"dict with nested values"
|
||||
(let
|
||||
((d (dict "a" (list 1 2) "b" "hello")))
|
||||
((d {:b "hello" :a (list 1 2)}))
|
||||
(assert= (canonical-serialize d) "{:a (1 2) :b \"hello\"}"))))
|
||||
|
||||
(test-group
|
||||
(defsuite
|
||||
"content-id"
|
||||
(test
|
||||
(deftest
|
||||
"same expression same CID"
|
||||
(assert= (content-id (list 1 2 3)) (content-id (list 1 2 3))))
|
||||
(test
|
||||
(deftest
|
||||
"different expression different CID"
|
||||
(assert
|
||||
(not (= (content-id (list 1 2 3)) (content-id (list 1 2 4))))))
|
||||
(test
|
||||
(deftest
|
||||
"CID is a hex string"
|
||||
(let
|
||||
((cid (content-id 42)))
|
||||
(assert (string? cid))
|
||||
(assert= (len cid) 64)))
|
||||
(test
|
||||
(deftest
|
||||
"short CID is 16 chars"
|
||||
(let ((cid (content-id-short 42))) (assert= (len cid) 16)))
|
||||
(test
|
||||
(deftest
|
||||
"short CID is prefix of full CID"
|
||||
(let
|
||||
((full (content-id 42)) (short (content-id-short 42)))
|
||||
(assert= short (slice full 0 16)))))
|
||||
|
||||
(test-group
|
||||
(defsuite
|
||||
"bytecode-module"
|
||||
(test
|
||||
(deftest
|
||||
"make and query"
|
||||
(let
|
||||
((m (make-bytecode-module 1 "abc123" (list (quote code) :bytecode (list 1 2 3)))))
|
||||
((m (make-bytecode-module 1 "abc123" (list 1 2 3))))
|
||||
(assert (bytecode-module? m))
|
||||
(assert= (bytecode-module-version m) 1)
|
||||
(assert= (bytecode-module-source-hash m) "abc123")))
|
||||
(test
|
||||
(deftest
|
||||
"non-module fails predicate"
|
||||
(assert (not (bytecode-module? (list 1 2 3))))
|
||||
(assert (not (bytecode-module? "hello")))))
|
||||
|
||||
(test-group
|
||||
(defsuite
|
||||
"provenance"
|
||||
(test
|
||||
(deftest
|
||||
"make provenance record"
|
||||
(let
|
||||
((p (make-provenance "src-cid" "bc-cid" "compiler-cid" "2026-03-27T00:00:00Z")))
|
||||
((p (make-provenance "src-cid" "bc-cid" "v1" "js")))
|
||||
(assert= (first p) (quote provenance))
|
||||
(assert= (nth p 2) "src-cid")
|
||||
(assert= (nth p 4) "bc-cid"))))
|
||||
|
||||
Reference in New Issue
Block a user