diff --git a/tests/goldfish/liii/case-test.scm b/tests/goldfish/liii/case-test.scm deleted file mode 100644 index 52656049..00000000 --- a/tests/goldfish/liii/case-test.scm +++ /dev/null @@ -1,58 +0,0 @@ -(import (liii check) - (liii case) -) ;import - -(check-set-mode! 'report-failed) - -; 0 clause BSD, from S7 repo s7test.scm -(define (scase x) - (case* x - ((a b) 'a-or-b) - ((1 2/3 3.0) => (lambda (a) (* a 2))) - ((pi) 1 123) - (("string1" "string2")) - ((#) 'symbol!) - (((+ x #)) 'got-list) - ((#(1 x 3)) 'got-vector) - (((+ #<>)) 'empty) - (((* # #)) 'got-label) - (((#<> # #)) 'repeated) - (((# #)) 'two) - (((# #)) 'pair) - ((#(# #)) 'vector) - ((#(# #<...> #)) 'vectsn) - ((#(#<...> #)) 'vectstart) - ((#(# # #<...>)) 'vectstr) - (else 'oops) - ) ;case* -) ;define - -(test (scase 3.0) 6.0) -(test (scase 'pi) 123) -(test (scase "string1") "string1") -(test (scase "string3") 'oops) -(test (scase 'a) 'a-or-b) -(test (scase 'abc) 'symbol!) -(test (scase #()) 'oops) -(test (scase '(+ x z)) 'got-list) -(test (scase #(1 x 3)) 'got-vector) -(test (scase '(+ x 3)) 'oops) -(test (scase '(+ x)) 'empty) -(test (scase '(* z z)) 'got-label) -(test (scase '(* z x)) 'oops) -(test (scase '(+ (abs x) (abs x))) 'repeated) -(test (scase '(+ (abs x) (abs y))) 'oops) -(test (scase '(a b)) 'two) -(test (scase '(1 1)) 'pair) -(test (scase '(1 1 2)) 'oops) -(test (scase #(1 1)) 'vector) -(test (scase #(a b c 3)) 'vectsn) -(test (scase #(1 b 2)) 'vectstart) -(test (scase #("asdf" #\space +nan.0 #)) 'vectstr) -(test (scase #(a 3)) 'vectsn) -(test (scase #(1)) 'vectstart) -(test (scase #("asdf" #\space)) 'vectstr) -(test (scase #("asdf")) 'oops) - -(check-report) - diff --git a/tests/liii/case-test.scm b/tests/liii/case-test.scm new file mode 100644 index 00000000..b7d5fb39 --- /dev/null +++ b/tests/liii/case-test.scm @@ -0,0 +1,358 @@ +;; (liii case) 模块测试文件 +;; +;; case* 是一个强大的模式匹配宏,相比标准 case 表达式,它提供了: +;; 1. 数据结构模式匹配(列表、向量) +;; 2. 变量绑定与捕获 +;; 3. 省略号匹配(可变长度) +;; 4. 谓词函数匹配 +;; 5. 递归嵌套模式 +;; +;; 核心用途:在 Scheme 中实现类似 Haskell/OCaml 的模式匹配功能, +;; 特别适用于解析树结构、处理 AST、实现解释器等场景。 + +;; ==== 语法说明 ==== +;; +;; (case* <选择器> +;; (<模式1> <结果1>) +;; (<模式2> <结果2>) +;; ... +;; (else <默认结果>)) +;; +;; <模式> 可以是: +;; - 字面量:数字、字符串、符号等 +;; - 列表模式:(1 2 3) 完全匹配列表 +;; - 向量模式:#(1 2 3) 完全匹配向量 +;; - 标签绑定:# 捕获匹配值,在结果中用 #