Compare commits
26 Commits
4cd8773766
...
loops/fort
| Author | SHA1 | Date | |
|---|---|---|---|
| aad178aa0f | |||
| 32a8ed8ef0 | |||
| 91611f9179 | |||
| 55f3024743 | |||
| 0d6d0bf439 | |||
| f6e333dd19 | |||
| c28333adb3 | |||
| 1b2935828c | |||
| 64af162b5d | |||
| 8ca2fe3564 | |||
| b1a7852045 | |||
| 89a879799a | |||
| 47f66ad1be | |||
| c726a9e0fe | |||
| b6810e90ab | |||
| 3ab01b271d | |||
| 8e1466032a | |||
| 387a6e7f5d | |||
| acf9c273a2 | |||
| 35ce18eb97 | |||
| 1c975f229d | |||
| 0e509af0a2 | |||
| a47b3e5420 | |||
| e066e14267 | |||
| bb16477fd4 | |||
| b2939c1922 |
@@ -1,710 +0,0 @@
|
|||||||
;; Common Lisp evaluator — evaluates CL AST forms.
|
|
||||||
;;
|
|
||||||
;; Depends on: lib/common-lisp/reader.sx, lib/common-lisp/parser.sx
|
|
||||||
;;
|
|
||||||
;; Environment:
|
|
||||||
;; {:vars {"NAME" val ...} :fns {"NAME" cl-fn ...}}
|
|
||||||
;; CL function:
|
|
||||||
;; {:cl-type "function" :params ll :body forms :env env}
|
|
||||||
;;
|
|
||||||
;; Public API:
|
|
||||||
;; (cl-make-env) — create empty environment
|
|
||||||
;; (cl-eval form env) — evaluate one CL AST form
|
|
||||||
;; (cl-eval-str src env) — read+eval a CL source string
|
|
||||||
;; (cl-eval-all-str src env) — read-all+eval-each, return last
|
|
||||||
;; cl-global-env — global mutable environment
|
|
||||||
|
|
||||||
;; ── environment ──────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(define cl-make-env (fn () {:vars {} :fns {}}))
|
|
||||||
|
|
||||||
(define cl-global-env (cl-make-env))
|
|
||||||
|
|
||||||
(define cl-env-get-var (fn (env name) (get (get env "vars") name)))
|
|
||||||
(define cl-env-has-var? (fn (env name) (has-key? (get env "vars") name)))
|
|
||||||
(define cl-env-get-fn (fn (env name) (get (get env "fns") name)))
|
|
||||||
(define cl-env-has-fn? (fn (env name) (has-key? (get env "fns") name)))
|
|
||||||
|
|
||||||
(define cl-env-bind-var
|
|
||||||
(fn (env name value)
|
|
||||||
{:vars (assoc (get env "vars") name value)
|
|
||||||
:fns (get env "fns")}))
|
|
||||||
|
|
||||||
(define cl-env-bind-fn
|
|
||||||
(fn (env name fn-obj)
|
|
||||||
{:vars (get env "vars")
|
|
||||||
:fns (assoc (get env "fns") name fn-obj)}))
|
|
||||||
|
|
||||||
;; ── body evaluation ───────────────────────────────────────────────
|
|
||||||
|
|
||||||
(define cl-block-return?
|
|
||||||
(fn (v) (and (dict? v) (= (get v "cl-type") "block-return"))))
|
|
||||||
|
|
||||||
(define cl-go-tag?
|
|
||||||
(fn (v) (and (dict? v) (= (get v "cl-type") "go-tag"))))
|
|
||||||
|
|
||||||
(define cl-mv?
|
|
||||||
(fn (v) (and (dict? v) (= (get v "cl-type") "mv"))))
|
|
||||||
|
|
||||||
(define cl-mv-primary
|
|
||||||
(fn (v)
|
|
||||||
(if (cl-mv? v)
|
|
||||||
(if (> (len (get v "vals")) 0) (nth (get v "vals") 0) nil)
|
|
||||||
v)))
|
|
||||||
|
|
||||||
(define cl-mv-vals
|
|
||||||
(fn (v) (if (cl-mv? v) (get v "vals") (list v))))
|
|
||||||
|
|
||||||
(define cl-eval-body
|
|
||||||
(fn (forms env)
|
|
||||||
(cond
|
|
||||||
((= (len forms) 0) nil)
|
|
||||||
((= (len forms) 1) (cl-eval (nth forms 0) env))
|
|
||||||
(:else
|
|
||||||
(let ((result (cl-eval (nth forms 0) env)))
|
|
||||||
(if (or (cl-block-return? result) (cl-go-tag? result))
|
|
||||||
result
|
|
||||||
(cl-eval-body (rest forms) env)))))))
|
|
||||||
|
|
||||||
;; ── lambda-list binding helpers ───────────────────────────────────
|
|
||||||
|
|
||||||
(define cl-bind-required
|
|
||||||
(fn (names args env)
|
|
||||||
(if (= (len names) 0)
|
|
||||||
env
|
|
||||||
(cl-bind-required
|
|
||||||
(rest names)
|
|
||||||
(if (> (len args) 0) (rest args) args)
|
|
||||||
(cl-env-bind-var env
|
|
||||||
(nth names 0)
|
|
||||||
(if (> (len args) 0) (nth args 0) nil))))))
|
|
||||||
|
|
||||||
;; returns {:env e :rest remaining-args}
|
|
||||||
(define cl-bind-optional
|
|
||||||
(fn (opts args env)
|
|
||||||
(if (= (len opts) 0)
|
|
||||||
{:env env :rest args}
|
|
||||||
(let ((spec (nth opts 0))
|
|
||||||
(has-val (> (len args) 0)))
|
|
||||||
(let ((val (if has-val (nth args 0) nil))
|
|
||||||
(rem (if has-val (rest args) args)))
|
|
||||||
(let ((e1 (cl-env-bind-var env (get spec "name")
|
|
||||||
(if has-val val
|
|
||||||
(if (get spec "default")
|
|
||||||
(cl-eval (get spec "default") env) nil)))))
|
|
||||||
(let ((e2 (if (get spec "supplied")
|
|
||||||
(cl-env-bind-var e1 (get spec "supplied") has-val)
|
|
||||||
e1)))
|
|
||||||
(cl-bind-optional (rest opts) rem e2))))))))
|
|
||||||
|
|
||||||
;; returns {:found bool :value v}
|
|
||||||
(define cl-find-kw-arg
|
|
||||||
(fn (kw args i)
|
|
||||||
(if (>= i (len args))
|
|
||||||
{:found false :value nil}
|
|
||||||
(let ((a (nth args i)))
|
|
||||||
(if (and (dict? a)
|
|
||||||
(= (get a "cl-type") "keyword")
|
|
||||||
(= (get a "name") kw))
|
|
||||||
{:found true
|
|
||||||
:value (if (< (+ i 1) (len args)) (nth args (+ i 1)) nil)}
|
|
||||||
(cl-find-kw-arg kw args (+ i 2)))))))
|
|
||||||
|
|
||||||
(define cl-bind-key
|
|
||||||
(fn (key-specs all-args env)
|
|
||||||
(if (= (len key-specs) 0)
|
|
||||||
env
|
|
||||||
(let ((spec (nth key-specs 0))
|
|
||||||
(r (cl-find-kw-arg (get (nth key-specs 0) "keyword") all-args 0)))
|
|
||||||
(let ((found (get r "found"))
|
|
||||||
(kval (get r "value")))
|
|
||||||
(let ((e1 (cl-env-bind-var env (get spec "name")
|
|
||||||
(if found kval
|
|
||||||
(if (get spec "default")
|
|
||||||
(cl-eval (get spec "default") env) nil)))))
|
|
||||||
(let ((e2 (if (get spec "supplied")
|
|
||||||
(cl-env-bind-var e1 (get spec "supplied") found)
|
|
||||||
e1)))
|
|
||||||
(cl-bind-key (rest key-specs) all-args e2))))))))
|
|
||||||
|
|
||||||
(define cl-bind-aux
|
|
||||||
(fn (aux-specs env)
|
|
||||||
(if (= (len aux-specs) 0)
|
|
||||||
env
|
|
||||||
(let ((spec (nth aux-specs 0)))
|
|
||||||
(cl-bind-aux
|
|
||||||
(rest aux-specs)
|
|
||||||
(cl-env-bind-var env (get spec "name")
|
|
||||||
(if (get spec "init") (cl-eval (get spec "init") env) nil)))))))
|
|
||||||
|
|
||||||
;; ── function creation ─────────────────────────────────────────────
|
|
||||||
|
|
||||||
;; ll-and-body: (list lambda-list-form body-form ...)
|
|
||||||
(define cl-make-lambda
|
|
||||||
(fn (ll-and-body env)
|
|
||||||
{:cl-type "function"
|
|
||||||
:params (cl-parse-lambda-list (nth ll-and-body 0))
|
|
||||||
:body (rest ll-and-body)
|
|
||||||
:env env}))
|
|
||||||
|
|
||||||
;; ── function application ──────────────────────────────────────────
|
|
||||||
|
|
||||||
(define cl-apply
|
|
||||||
(fn (fn-obj args)
|
|
||||||
(cond
|
|
||||||
((and (dict? fn-obj) (has-key? fn-obj "builtin-fn"))
|
|
||||||
((get fn-obj "builtin-fn") args))
|
|
||||||
((or (not (dict? fn-obj)) (not (= (get fn-obj "cl-type") "function")))
|
|
||||||
{:cl-type "error" :message "Not a function"})
|
|
||||||
(:else
|
|
||||||
(let ((params (get fn-obj "params"))
|
|
||||||
(body (get fn-obj "body"))
|
|
||||||
(cenv (get fn-obj "env")))
|
|
||||||
(let ((req (get params "required"))
|
|
||||||
(opt (get params "optional"))
|
|
||||||
(rest-name (get params "rest"))
|
|
||||||
(key-specs (get params "key"))
|
|
||||||
(aux-specs (get params "aux")))
|
|
||||||
(let ((e1 (cl-bind-required req args cenv)))
|
|
||||||
(let ((opt-r (cl-bind-optional
|
|
||||||
opt (slice args (len req) (len args)) e1)))
|
|
||||||
(let ((e2 (get opt-r "env"))
|
|
||||||
(rem (get opt-r "rest")))
|
|
||||||
(let ((e3 (if rest-name
|
|
||||||
(cl-env-bind-var e2 rest-name rem)
|
|
||||||
e2)))
|
|
||||||
(let ((e4 (cl-bind-key key-specs args e3)))
|
|
||||||
(let ((e5 (cl-bind-aux aux-specs e4)))
|
|
||||||
(cl-eval-body body e5)))))))))))))
|
|
||||||
|
|
||||||
;; ── built-in functions ────────────────────────────────────────────
|
|
||||||
|
|
||||||
(define cl-builtins
|
|
||||||
(dict
|
|
||||||
"+" (fn (args) (reduce (fn (a b) (+ a b)) 0 args))
|
|
||||||
"-" (fn (args)
|
|
||||||
(cond
|
|
||||||
((= (len args) 0) 0)
|
|
||||||
((= (len args) 1) (- 0 (nth args 0)))
|
|
||||||
(:else (reduce (fn (a b) (- a b)) (nth args 0) (rest args)))))
|
|
||||||
"*" (fn (args) (reduce (fn (a b) (* a b)) 1 args))
|
|
||||||
"/" (fn (args)
|
|
||||||
(cond
|
|
||||||
((= (len args) 0) 1)
|
|
||||||
((= (len args) 1) (/ 1 (nth args 0)))
|
|
||||||
(:else (reduce (fn (a b) (/ a b)) (nth args 0) (rest args)))))
|
|
||||||
"1+" (fn (args) (+ (nth args 0) 1))
|
|
||||||
"1-" (fn (args) (- (nth args 0) 1))
|
|
||||||
"=" (fn (args) (if (= (nth args 0) (nth args 1)) true nil))
|
|
||||||
"/=" (fn (args) (if (not (= (nth args 0) (nth args 1))) true nil))
|
|
||||||
"<" (fn (args) (if (< (nth args 0) (nth args 1)) true nil))
|
|
||||||
">" (fn (args) (if (> (nth args 0) (nth args 1)) true nil))
|
|
||||||
"<=" (fn (args) (if (<= (nth args 0) (nth args 1)) true nil))
|
|
||||||
">=" (fn (args) (if (>= (nth args 0) (nth args 1)) true nil))
|
|
||||||
"NOT" (fn (args) (if (nth args 0) nil true))
|
|
||||||
"NULL" (fn (args) (if (= (nth args 0) nil) true nil))
|
|
||||||
"NUMBERP" (fn (args) (if (number? (nth args 0)) true nil))
|
|
||||||
"STRINGP" (fn (args) (if (string? (nth args 0)) true nil))
|
|
||||||
"SYMBOLP" (fn (args) nil)
|
|
||||||
"LISTP" (fn (args)
|
|
||||||
(if (or (list? (nth args 0)) (= (nth args 0) nil)) true nil))
|
|
||||||
"CONSP" (fn (args)
|
|
||||||
(let ((x (nth args 0)))
|
|
||||||
(if (and (dict? x) (= (get x "cl-type") "cons")) true nil)))
|
|
||||||
"ATOM" (fn (args)
|
|
||||||
(let ((x (nth args 0)))
|
|
||||||
(if (and (dict? x) (= (get x "cl-type") "cons")) nil true)))
|
|
||||||
"FUNCTIONP" (fn (args)
|
|
||||||
(let ((x (nth args 0)))
|
|
||||||
(if (and (dict? x) (= (get x "cl-type") "function")) true nil)))
|
|
||||||
"ZEROP" (fn (args) (if (= (nth args 0) 0) true nil))
|
|
||||||
"PLUSP" (fn (args) (if (> (nth args 0) 0) true nil))
|
|
||||||
"MINUSP" (fn (args) (if (< (nth args 0) 0) true nil))
|
|
||||||
"EVENP" (fn (args)
|
|
||||||
(let ((n (nth args 0)))
|
|
||||||
(if (= (mod n 2) 0) true nil)))
|
|
||||||
"ODDP" (fn (args)
|
|
||||||
(let ((n (nth args 0)))
|
|
||||||
(if (not (= (mod n 2) 0)) true nil)))
|
|
||||||
"ABS" (fn (args) (let ((n (nth args 0))) (if (< n 0) (- 0 n) n)))
|
|
||||||
"MAX" (fn (args) (reduce (fn (a b) (if (> a b) a b)) (nth args 0) (rest args)))
|
|
||||||
"MIN" (fn (args) (reduce (fn (a b) (if (< a b) a b)) (nth args 0) (rest args)))
|
|
||||||
"CONS" (fn (args) {:cl-type "cons" :car (nth args 0) :cdr (nth args 1)})
|
|
||||||
"CAR" (fn (args)
|
|
||||||
(let ((x (nth args 0)))
|
|
||||||
(if (and (dict? x) (= (get x "cl-type") "cons"))
|
|
||||||
(get x "car")
|
|
||||||
(if (and (list? x) (> (len x) 0)) (nth x 0) nil))))
|
|
||||||
"CDR" (fn (args)
|
|
||||||
(let ((x (nth args 0)))
|
|
||||||
(if (and (dict? x) (= (get x "cl-type") "cons"))
|
|
||||||
(get x "cdr")
|
|
||||||
(if (list? x) (rest x) nil))))
|
|
||||||
"LIST" (fn (args) args)
|
|
||||||
"APPEND" (fn (args)
|
|
||||||
(if (= (len args) 0) (list)
|
|
||||||
(reduce (fn (a b)
|
|
||||||
(if (= a nil) b (if (= b nil) a (concat a b))))
|
|
||||||
(list) args)))
|
|
||||||
"LENGTH" (fn (args)
|
|
||||||
(let ((x (nth args 0)))
|
|
||||||
(if (= x nil) 0 (len x))))
|
|
||||||
"NTH" (fn (args) (nth (nth args 1) (nth args 0)))
|
|
||||||
"FIRST" (fn (args)
|
|
||||||
(let ((x (nth args 0)))
|
|
||||||
(if (and (list? x) (> (len x) 0)) (nth x 0) nil)))
|
|
||||||
"SECOND" (fn (args)
|
|
||||||
(let ((x (nth args 0)))
|
|
||||||
(if (and (list? x) (> (len x) 1)) (nth x 1) nil)))
|
|
||||||
"THIRD" (fn (args)
|
|
||||||
(let ((x (nth args 0)))
|
|
||||||
(if (and (list? x) (> (len x) 2)) (nth x 2) nil)))
|
|
||||||
"REST" (fn (args) (rest (nth args 0)))
|
|
||||||
"REVERSE" (fn (args)
|
|
||||||
(reduce (fn (acc x) (concat (list x) acc))
|
|
||||||
(list) (nth args 0)))
|
|
||||||
"IDENTITY" (fn (args) (nth args 0))
|
|
||||||
"VALUES" (fn (args) (cond ((= (len args) 0) nil) ((= (len args) 1) (nth args 0)) (:else {:cl-type "mv" :vals args})))
|
|
||||||
"PRINT" (fn (args) (nth args 0))
|
|
||||||
"PRIN1" (fn (args) (nth args 0))
|
|
||||||
"PRINC" (fn (args) (nth args 0))
|
|
||||||
"TERPRI" (fn (args) nil)
|
|
||||||
"WRITE" (fn (args) (nth args 0))
|
|
||||||
"STRING-UPCASE" (fn (args) (upcase (nth args 0)))
|
|
||||||
"STRING-DOWNCASE" (fn (args) (downcase (nth args 0)))
|
|
||||||
"STRING=" (fn (args) (if (= (nth args 0) (nth args 1)) true nil))
|
|
||||||
"CONCATENATE" (fn (args) (reduce (fn (a b) (str a b)) "" (rest args)))
|
|
||||||
"EQ" (fn (args) (if (= (nth args 0) (nth args 1)) true nil))
|
|
||||||
"EQL" (fn (args) (if (= (nth args 0) (nth args 1)) true nil))
|
|
||||||
"EQUAL" (fn (args) (if (= (nth args 0) (nth args 1)) true nil))))
|
|
||||||
|
|
||||||
;; Register builtins in cl-global-env so (function #'name) resolves them
|
|
||||||
(for-each
|
|
||||||
(fn (name)
|
|
||||||
(dict-set! (get cl-global-env "fns") name
|
|
||||||
{:cl-type "function" :builtin-fn (get cl-builtins name)}))
|
|
||||||
(keys cl-builtins))
|
|
||||||
|
|
||||||
;; ── TAGBODY / GO ─────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(define cl-tagbody-tag?
|
|
||||||
(fn (form) (or (string? form) (number? form))))
|
|
||||||
|
|
||||||
(define cl-build-tag-map
|
|
||||||
(fn (forms i acc)
|
|
||||||
(if (>= i (len forms))
|
|
||||||
acc
|
|
||||||
(if (cl-tagbody-tag? (nth forms i))
|
|
||||||
(cl-build-tag-map forms (+ i 1)
|
|
||||||
(assoc acc (str (nth forms i)) i))
|
|
||||||
(cl-build-tag-map forms (+ i 1) acc)))))
|
|
||||||
|
|
||||||
(define cl-eval-tagbody
|
|
||||||
(fn (args env)
|
|
||||||
(let ((tag-map (cl-build-tag-map args 0 {})))
|
|
||||||
(define run
|
|
||||||
(fn (i)
|
|
||||||
(if (>= i (len args))
|
|
||||||
nil
|
|
||||||
(let ((form (nth args i)))
|
|
||||||
(if (cl-tagbody-tag? form)
|
|
||||||
(run (+ i 1))
|
|
||||||
(let ((result (cl-eval form env)))
|
|
||||||
(cond
|
|
||||||
((cl-go-tag? result)
|
|
||||||
(let ((target (get result "tag")))
|
|
||||||
(let ((tkey (str target)))
|
|
||||||
(if (has-key? tag-map tkey)
|
|
||||||
(run (get tag-map tkey))
|
|
||||||
{:cl-type "error" :message (str "No tag: " target)}))))
|
|
||||||
((cl-block-return? result) result)
|
|
||||||
(:else (run (+ i 1))))))))))
|
|
||||||
(run 0))))
|
|
||||||
|
|
||||||
;; ── MULTIPLE VALUES ──────────────────────────────────────────────
|
|
||||||
|
|
||||||
(define cl-eval-multiple-value-bind
|
|
||||||
(fn (args env)
|
|
||||||
(let ((vars (nth args 0))
|
|
||||||
(form (nth args 1))
|
|
||||||
(body (rest (rest args))))
|
|
||||||
(let ((vals (cl-mv-vals (cl-eval form env))))
|
|
||||||
(define bind-vars
|
|
||||||
(fn (names i e)
|
|
||||||
(if (= (len names) 0)
|
|
||||||
e
|
|
||||||
(bind-vars (rest names) (+ i 1)
|
|
||||||
(cl-env-bind-var e (nth names 0)
|
|
||||||
(if (< i (len vals)) (nth vals i) nil))))))
|
|
||||||
(cl-eval-body body (bind-vars vars 0 env))))))
|
|
||||||
|
|
||||||
(define cl-eval-multiple-value-call
|
|
||||||
(fn (args env)
|
|
||||||
(let ((fn-obj (cl-eval (nth args 0) env))
|
|
||||||
(forms (rest args)))
|
|
||||||
(let ((all-vals (reduce
|
|
||||||
(fn (acc f)
|
|
||||||
(concat acc (cl-mv-vals (cl-eval f env))))
|
|
||||||
(list) forms)))
|
|
||||||
(cl-apply fn-obj all-vals)))))
|
|
||||||
|
|
||||||
(define cl-eval-multiple-value-prog1
|
|
||||||
(fn (args env)
|
|
||||||
(let ((first-result (cl-eval (nth args 0) env)))
|
|
||||||
(for-each (fn (f) (cl-eval f env)) (rest args))
|
|
||||||
first-result)))
|
|
||||||
|
|
||||||
;; ── UNWIND-PROTECT ───────────────────────────────────────────────
|
|
||||||
|
|
||||||
(define cl-eval-unwind-protect
|
|
||||||
(fn (args env)
|
|
||||||
(let ((protected (nth args 0))
|
|
||||||
(cleanup (rest args)))
|
|
||||||
(let ((result (cl-eval protected env)))
|
|
||||||
(for-each (fn (f) (cl-eval f env)) cleanup)
|
|
||||||
result))))
|
|
||||||
|
|
||||||
;; ── BLOCK / RETURN-FROM ───────────────────────────────────────────
|
|
||||||
|
|
||||||
(define cl-eval-block
|
|
||||||
(fn (args env)
|
|
||||||
(let ((name (nth args 0))
|
|
||||||
(body (rest args)))
|
|
||||||
(let ((result (cl-eval-body body env)))
|
|
||||||
(if (and (cl-block-return? result)
|
|
||||||
(= (get result "name") name))
|
|
||||||
(get result "value")
|
|
||||||
result)))))
|
|
||||||
|
|
||||||
(define cl-eval-return-from
|
|
||||||
(fn (args env)
|
|
||||||
(let ((name (nth args 0))
|
|
||||||
(val (if (> (len args) 1) (cl-eval (nth args 1) env) nil)))
|
|
||||||
{:cl-type "block-return" :name name :value val})))
|
|
||||||
|
|
||||||
;; ── special form evaluators ───────────────────────────────────────
|
|
||||||
|
|
||||||
(define cl-eval-if
|
|
||||||
(fn (args env)
|
|
||||||
(let ((cond-val (cl-mv-primary (cl-eval (nth args 0) env)))
|
|
||||||
(then-form (nth args 1))
|
|
||||||
(else-form (if (> (len args) 2) (nth args 2) nil)))
|
|
||||||
(if cond-val
|
|
||||||
(cl-eval then-form env)
|
|
||||||
(if else-form (cl-eval else-form env) nil)))))
|
|
||||||
|
|
||||||
(define cl-eval-and
|
|
||||||
(fn (args env)
|
|
||||||
(if (= (len args) 0)
|
|
||||||
true
|
|
||||||
(let ((val (cl-mv-primary (cl-eval (nth args 0) env))))
|
|
||||||
(if (not val)
|
|
||||||
nil
|
|
||||||
(if (= (len args) 1)
|
|
||||||
val
|
|
||||||
(cl-eval-and (rest args) env)))))))
|
|
||||||
|
|
||||||
(define cl-eval-or
|
|
||||||
(fn (args env)
|
|
||||||
(if (= (len args) 0)
|
|
||||||
nil
|
|
||||||
(let ((val (cl-mv-primary (cl-eval (nth args 0) env))))
|
|
||||||
(if val
|
|
||||||
val
|
|
||||||
(cl-eval-or (rest args) env))))))
|
|
||||||
|
|
||||||
(define cl-eval-cond
|
|
||||||
(fn (clauses env)
|
|
||||||
(if (= (len clauses) 0)
|
|
||||||
nil
|
|
||||||
(let ((clause (nth clauses 0)))
|
|
||||||
(let ((test-val (cl-mv-primary (cl-eval (nth clause 0) env))))
|
|
||||||
(if test-val
|
|
||||||
(if (= (len clause) 1)
|
|
||||||
test-val
|
|
||||||
(cl-eval-body (rest clause) env))
|
|
||||||
(cl-eval-cond (rest clauses) env)))))))
|
|
||||||
|
|
||||||
;; Parallel LET and sequential LET*
|
|
||||||
(define cl-eval-let
|
|
||||||
(fn (args env sequential)
|
|
||||||
(let ((bindings (nth args 0))
|
|
||||||
(body (rest args)))
|
|
||||||
(if sequential
|
|
||||||
;; LET*: each binding sees previous ones
|
|
||||||
(let ((new-env env))
|
|
||||||
(define bind-seq
|
|
||||||
(fn (bs e)
|
|
||||||
(if (= (len bs) 0)
|
|
||||||
e
|
|
||||||
(let ((b (nth bs 0)))
|
|
||||||
(let ((name (if (list? b) (nth b 0) b))
|
|
||||||
(init (if (and (list? b) (> (len b) 1)) (nth b 1) nil)))
|
|
||||||
(bind-seq (rest bs)
|
|
||||||
(cl-env-bind-var e name (cl-eval init e))))))))
|
|
||||||
(cl-eval-body body (bind-seq bindings env)))
|
|
||||||
;; LET: evaluate all inits in current env, then bind
|
|
||||||
(let ((pairs (map
|
|
||||||
(fn (b)
|
|
||||||
(let ((name (if (list? b) (nth b 0) b))
|
|
||||||
(init (if (and (list? b) (> (len b) 1)) (nth b 1) nil)))
|
|
||||||
{:name name :value (cl-eval init env)}))
|
|
||||||
bindings)))
|
|
||||||
(let ((new-env (reduce
|
|
||||||
(fn (e pair)
|
|
||||||
(cl-env-bind-var e (get pair "name") (get pair "value")))
|
|
||||||
env pairs)))
|
|
||||||
(cl-eval-body body new-env)))))))
|
|
||||||
|
|
||||||
;; SETQ / SETF (simplified: mutate nearest scope or global)
|
|
||||||
(define cl-eval-setq
|
|
||||||
(fn (args env)
|
|
||||||
(if (< (len args) 2)
|
|
||||||
nil
|
|
||||||
(let ((name (nth args 0))
|
|
||||||
(val (cl-eval (nth args 1) env)))
|
|
||||||
(if (has-key? (get env "vars") name)
|
|
||||||
(dict-set! (get env "vars") name val)
|
|
||||||
(dict-set! (get cl-global-env "vars") name val))
|
|
||||||
(if (> (len args) 2)
|
|
||||||
(cl-eval-setq (rest (rest args)) env)
|
|
||||||
val)))))
|
|
||||||
|
|
||||||
;; FUNCTION: get function value or create lambda
|
|
||||||
(define cl-eval-function
|
|
||||||
(fn (args env)
|
|
||||||
(let ((spec (nth args 0)))
|
|
||||||
(cond
|
|
||||||
((and (list? spec) (> (len spec) 0) (= (nth spec 0) "LAMBDA"))
|
|
||||||
(cl-make-lambda (rest spec) env))
|
|
||||||
((string? spec)
|
|
||||||
(cond
|
|
||||||
((cl-env-has-fn? env spec) (cl-env-get-fn env spec))
|
|
||||||
((cl-env-has-fn? cl-global-env spec)
|
|
||||||
(cl-env-get-fn cl-global-env spec))
|
|
||||||
(:else {:cl-type "error" :message (str "Undefined function: " spec)})))
|
|
||||||
(:else {:cl-type "error" :message "FUNCTION: invalid spec"})))))
|
|
||||||
|
|
||||||
;; FLET: local functions (non-recursive, close over outer env)
|
|
||||||
(define cl-eval-flet
|
|
||||||
(fn (args env)
|
|
||||||
(let ((fn-defs (nth args 0))
|
|
||||||
(body (rest args)))
|
|
||||||
(let ((new-env (reduce
|
|
||||||
(fn (e def)
|
|
||||||
(let ((name (nth def 0))
|
|
||||||
(ll (nth def 1))
|
|
||||||
(fn-body (rest (rest def))))
|
|
||||||
(cl-env-bind-fn e name
|
|
||||||
{:cl-type "function"
|
|
||||||
:params (cl-parse-lambda-list ll)
|
|
||||||
:body fn-body
|
|
||||||
:env env})))
|
|
||||||
env fn-defs)))
|
|
||||||
(cl-eval-body body new-env)))))
|
|
||||||
|
|
||||||
;; LABELS: mutually-recursive local functions
|
|
||||||
(define cl-eval-labels
|
|
||||||
(fn (args env)
|
|
||||||
(let ((fn-defs (nth args 0))
|
|
||||||
(body (rest args)))
|
|
||||||
;; Build env with placeholder nil entries for each name
|
|
||||||
(let ((new-env (reduce
|
|
||||||
(fn (e def) (cl-env-bind-fn e (nth def 0) nil))
|
|
||||||
env fn-defs)))
|
|
||||||
;; Fill in real function objects that capture new-env
|
|
||||||
(for-each
|
|
||||||
(fn (def)
|
|
||||||
(let ((name (nth def 0))
|
|
||||||
(ll (nth def 1))
|
|
||||||
(fn-body (rest (rest def))))
|
|
||||||
(dict-set! (get new-env "fns") name
|
|
||||||
{:cl-type "function"
|
|
||||||
:params (cl-parse-lambda-list ll)
|
|
||||||
:body fn-body
|
|
||||||
:env new-env})))
|
|
||||||
fn-defs)
|
|
||||||
(cl-eval-body body new-env)))))
|
|
||||||
|
|
||||||
;; EVAL-WHEN: evaluate body only if :execute is in situations
|
|
||||||
(define cl-eval-eval-when
|
|
||||||
(fn (args env)
|
|
||||||
(let ((situations (nth args 0))
|
|
||||||
(body (rest args)))
|
|
||||||
(define has-exec
|
|
||||||
(some (fn (s)
|
|
||||||
(or
|
|
||||||
(and (dict? s)
|
|
||||||
(= (get s "cl-type") "keyword")
|
|
||||||
(= (get s "name") "EXECUTE"))
|
|
||||||
(= s "EXECUTE")))
|
|
||||||
situations))
|
|
||||||
(if has-exec (cl-eval-body body env) nil))))
|
|
||||||
|
|
||||||
;; DEFUN: define function in global fns namespace
|
|
||||||
(define cl-eval-defun
|
|
||||||
(fn (args env)
|
|
||||||
(let ((name (nth args 0))
|
|
||||||
(ll (nth args 1))
|
|
||||||
(fn-body (rest (rest args))))
|
|
||||||
(let ((fn-obj {:cl-type "function"
|
|
||||||
:params (cl-parse-lambda-list ll)
|
|
||||||
:body fn-body
|
|
||||||
:env env}))
|
|
||||||
(dict-set! (get cl-global-env "fns") name fn-obj)
|
|
||||||
name))))
|
|
||||||
|
|
||||||
;; DEFVAR / DEFPARAMETER / DEFCONSTANT
|
|
||||||
(define cl-eval-defvar
|
|
||||||
(fn (args env always-assign)
|
|
||||||
(let ((name (nth args 0))
|
|
||||||
(has-init (> (len args) 1)))
|
|
||||||
(let ((val (if has-init (cl-eval (nth args 1) env) nil)))
|
|
||||||
(when (or always-assign
|
|
||||||
(not (cl-env-has-var? cl-global-env name)))
|
|
||||||
(dict-set! (get cl-global-env "vars") name val))
|
|
||||||
name))))
|
|
||||||
|
|
||||||
;; Function call: evaluate name → look up fns, builtins; evaluate args
|
|
||||||
(define cl-call-fn
|
|
||||||
(fn (name args env)
|
|
||||||
(let ((evaled (map (fn (a) (cl-mv-primary (cl-eval a env))) args)))
|
|
||||||
(cond
|
|
||||||
;; FUNCALL: (funcall fn arg...)
|
|
||||||
((= name "FUNCALL")
|
|
||||||
(cl-apply (nth evaled 0) (rest evaled)))
|
|
||||||
;; APPLY: (apply fn arg... list)
|
|
||||||
((= name "APPLY")
|
|
||||||
(let ((fn-obj (nth evaled 0))
|
|
||||||
(all-args (rest evaled)))
|
|
||||||
(let ((leading (slice all-args 0 (- (len all-args) 1)))
|
|
||||||
(last-arg (nth all-args (- (len all-args) 1))))
|
|
||||||
(cl-apply fn-obj (concat leading (if (= last-arg nil) (list) last-arg))))))
|
|
||||||
;; MAPCAR: (mapcar fn list)
|
|
||||||
((= name "MAPCAR")
|
|
||||||
(let ((fn-obj (nth evaled 0))
|
|
||||||
(lst (nth evaled 1)))
|
|
||||||
(if (= lst nil) (list)
|
|
||||||
(map (fn (x) (cl-apply fn-obj (list x))) lst))))
|
|
||||||
;; Look up in local fns namespace
|
|
||||||
((cl-env-has-fn? env name)
|
|
||||||
(cl-apply (cl-env-get-fn env name) evaled))
|
|
||||||
;; Look up in global fns namespace
|
|
||||||
((cl-env-has-fn? cl-global-env name)
|
|
||||||
(cl-apply (cl-env-get-fn cl-global-env name) evaled))
|
|
||||||
;; Look up in builtins
|
|
||||||
((has-key? cl-builtins name)
|
|
||||||
((get cl-builtins name) evaled))
|
|
||||||
(:else
|
|
||||||
{:cl-type "error" :message (str "Undefined function: " name)})))))
|
|
||||||
|
|
||||||
;; ── main evaluator ────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(define cl-eval
|
|
||||||
(fn (form env)
|
|
||||||
(cond
|
|
||||||
;; Nil and booleans are self-evaluating
|
|
||||||
((= form nil) nil)
|
|
||||||
((= form true) true)
|
|
||||||
;; Numbers are self-evaluating
|
|
||||||
((number? form) form)
|
|
||||||
;; Dicts: typed CL values
|
|
||||||
((dict? form)
|
|
||||||
(let ((ct (get form "cl-type")))
|
|
||||||
(cond
|
|
||||||
((= ct "string") (get form "value")) ;; CL string → SX string
|
|
||||||
(:else form)))) ;; keywords, floats, chars, etc.
|
|
||||||
;; Symbol reference (variable lookup)
|
|
||||||
((string? form)
|
|
||||||
(cond
|
|
||||||
((cl-env-has-var? env form) (cl-env-get-var env form))
|
|
||||||
((cl-env-has-var? cl-global-env form)
|
|
||||||
(cl-env-get-var cl-global-env form))
|
|
||||||
(:else {:cl-type "error" :message (str "Undefined variable: " form)})))
|
|
||||||
;; List: special forms or function call
|
|
||||||
((list? form) (cl-eval-list form env))
|
|
||||||
;; Anything else self-evaluates
|
|
||||||
(:else form))))
|
|
||||||
|
|
||||||
(define cl-eval-list
|
|
||||||
(fn (form env)
|
|
||||||
(if (= (len form) 0)
|
|
||||||
nil
|
|
||||||
(let ((head (nth form 0))
|
|
||||||
(args (rest form)))
|
|
||||||
(cond
|
|
||||||
((= head "QUOTE") (nth args 0))
|
|
||||||
((= head "IF") (cl-eval-if args env))
|
|
||||||
((= head "PROGN") (cl-eval-body args env))
|
|
||||||
((= head "LET") (cl-eval-let args env false))
|
|
||||||
((= head "LET*") (cl-eval-let args env true))
|
|
||||||
((= head "AND") (cl-eval-and args env))
|
|
||||||
((= head "OR") (cl-eval-or args env))
|
|
||||||
((= head "COND") (cl-eval-cond args env))
|
|
||||||
((= head "WHEN")
|
|
||||||
(if (cl-eval (nth args 0) env)
|
|
||||||
(cl-eval-body (rest args) env) nil))
|
|
||||||
((= head "UNLESS")
|
|
||||||
(if (not (cl-eval (nth args 0) env))
|
|
||||||
(cl-eval-body (rest args) env) nil))
|
|
||||||
((= head "SETQ") (cl-eval-setq args env))
|
|
||||||
((= head "SETF") (cl-eval-setq args env))
|
|
||||||
((= head "FUNCTION") (cl-eval-function args env))
|
|
||||||
((= head "LAMBDA") (cl-make-lambda args env))
|
|
||||||
((= head "FLET") (cl-eval-flet args env))
|
|
||||||
((= head "LABELS") (cl-eval-labels args env))
|
|
||||||
((= head "THE") (cl-eval (nth args 1) env))
|
|
||||||
((= head "LOCALLY") (cl-eval-body args env))
|
|
||||||
((= head "EVAL-WHEN") (cl-eval-eval-when args env))
|
|
||||||
((= head "DEFUN") (cl-eval-defun args env))
|
|
||||||
((= head "TAGBODY") (cl-eval-tagbody args env))
|
|
||||||
((= head "GO")
|
|
||||||
{:cl-type "go-tag" :tag (nth args 0)})
|
|
||||||
((= head "MULTIPLE-VALUE-BIND") (cl-eval-multiple-value-bind args env))
|
|
||||||
((= head "MULTIPLE-VALUE-CALL") (cl-eval-multiple-value-call args env))
|
|
||||||
((= head "MULTIPLE-VALUE-PROG1") (cl-eval-multiple-value-prog1 args env))
|
|
||||||
((= head "NTH-VALUE")
|
|
||||||
(let ((n (cl-mv-primary (cl-eval (nth args 0) env)))
|
|
||||||
(vals (cl-mv-vals (cl-eval (nth args 1) env))))
|
|
||||||
(if (< n (len vals)) (nth vals n) nil)))
|
|
||||||
((= head "UNWIND-PROTECT") (cl-eval-unwind-protect args env))
|
|
||||||
((= head "BLOCK") (cl-eval-block args env))
|
|
||||||
((= head "RETURN-FROM") (cl-eval-return-from args env))
|
|
||||||
((= head "RETURN")
|
|
||||||
(let ((val (if (> (len args) 0) (cl-eval (nth args 0) env) nil)))
|
|
||||||
{:cl-type "block-return" :name nil :value val}))
|
|
||||||
((= head "DEFVAR") (cl-eval-defvar args env false))
|
|
||||||
((= head "DEFPARAMETER") (cl-eval-defvar args env true))
|
|
||||||
((= head "DEFCONSTANT") (cl-eval-defvar args env true))
|
|
||||||
((= head "DECLAIM") nil)
|
|
||||||
((= head "PROCLAIM") nil)
|
|
||||||
;; Named function call
|
|
||||||
((string? head)
|
|
||||||
(cl-call-fn head args env))
|
|
||||||
;; Anonymous call: ((lambda ...) args)
|
|
||||||
(:else
|
|
||||||
(let ((fn-obj (cl-eval head env)))
|
|
||||||
(if (and (dict? fn-obj) (= (get fn-obj "cl-type") "function"))
|
|
||||||
(cl-apply fn-obj (map (fn (a) (cl-eval a env)) args))
|
|
||||||
{:cl-type "error" :message "Not callable"}))))))))
|
|
||||||
|
|
||||||
;; ── public API ────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(define cl-eval-str
|
|
||||||
(fn (src env)
|
|
||||||
(cl-eval (cl-read src) env)))
|
|
||||||
|
|
||||||
(define cl-eval-all-str
|
|
||||||
(fn (src env)
|
|
||||||
(let ((forms (cl-read-all src)))
|
|
||||||
(if (= (len forms) 0)
|
|
||||||
nil
|
|
||||||
(let ((result nil) (i 0))
|
|
||||||
(define loop (fn ()
|
|
||||||
(when (< i (len forms))
|
|
||||||
(do
|
|
||||||
(set! result (cl-eval (nth forms i) env))
|
|
||||||
(set! i (+ i 1))
|
|
||||||
(loop)))))
|
|
||||||
(loop)
|
|
||||||
result)))))
|
|
||||||
@@ -1,377 +0,0 @@
|
|||||||
;; Common Lisp reader — converts token stream to CL AST forms.
|
|
||||||
;;
|
|
||||||
;; Depends on: lib/common-lisp/reader.sx (cl-tokenize)
|
|
||||||
;;
|
|
||||||
;; AST representation:
|
|
||||||
;; integer/float → SX number (or {:cl-type "float"/:ratio ...})
|
|
||||||
;; string "hello" → {:cl-type "string" :value "hello"}
|
|
||||||
;; symbol FOO → SX string "FOO" (upcase)
|
|
||||||
;; symbol NIL → nil
|
|
||||||
;; symbol T → true
|
|
||||||
;; :keyword → {:cl-type "keyword" :name "FOO"}
|
|
||||||
;; #\char → {:cl-type "char" :value "a"}
|
|
||||||
;; #:uninterned → {:cl-type "uninterned" :name "FOO"}
|
|
||||||
;; ratio 1/3 → {:cl-type "ratio" :value "1/3"}
|
|
||||||
;; float 3.14 → {:cl-type "float" :value "3.14"}
|
|
||||||
;; proper list (a b c) → SX list (a b c)
|
|
||||||
;; dotted pair (a . b) → {:cl-type "cons" :car a :cdr b}
|
|
||||||
;; vector #(a b) → {:cl-type "vector" :elements (list a b)}
|
|
||||||
;; 'x → ("QUOTE" x)
|
|
||||||
;; `x → ("QUASIQUOTE" x)
|
|
||||||
;; ,x → ("UNQUOTE" x)
|
|
||||||
;; ,@x → ("UNQUOTE-SPLICING" x)
|
|
||||||
;; #'x → ("FUNCTION" x)
|
|
||||||
;;
|
|
||||||
;; Public API:
|
|
||||||
;; (cl-read src) — parse first form from string, return form
|
|
||||||
;; (cl-read-all src) — parse all top-level forms, return list
|
|
||||||
|
|
||||||
;; ── number conversion ─────────────────────────────────────────────
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-hex-val
|
|
||||||
(fn
|
|
||||||
(c)
|
|
||||||
(let
|
|
||||||
((o (cl-ord c)))
|
|
||||||
(cond
|
|
||||||
((and (>= o 48) (<= o 57)) (- o 48))
|
|
||||||
((and (>= o 65) (<= o 70)) (+ 10 (- o 65)))
|
|
||||||
((and (>= o 97) (<= o 102)) (+ 10 (- o 97)))
|
|
||||||
(:else 0)))))
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-parse-radix-str
|
|
||||||
(fn
|
|
||||||
(s radix start)
|
|
||||||
(let
|
|
||||||
((n (string-length s)) (i start) (acc 0))
|
|
||||||
(define
|
|
||||||
loop
|
|
||||||
(fn
|
|
||||||
()
|
|
||||||
(when
|
|
||||||
(< i n)
|
|
||||||
(do
|
|
||||||
(set! acc (+ (* acc radix) (cl-hex-val (substring s i (+ i 1)))))
|
|
||||||
(set! i (+ i 1))
|
|
||||||
(loop)))))
|
|
||||||
(loop)
|
|
||||||
acc)))
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-convert-integer
|
|
||||||
(fn
|
|
||||||
(s)
|
|
||||||
(let
|
|
||||||
((n (string-length s)) (neg false))
|
|
||||||
(cond
|
|
||||||
((and (> n 2) (= (substring s 0 1) "#"))
|
|
||||||
(let
|
|
||||||
((letter (downcase (substring s 1 2))))
|
|
||||||
(cond
|
|
||||||
((= letter "x") (cl-parse-radix-str s 16 2))
|
|
||||||
((= letter "b") (cl-parse-radix-str s 2 2))
|
|
||||||
((= letter "o") (cl-parse-radix-str s 8 2))
|
|
||||||
(:else (parse-int s 0)))))
|
|
||||||
(:else (parse-int s 0))))))
|
|
||||||
|
|
||||||
;; ── reader ────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
;; Read one form from token list.
|
|
||||||
;; Returns {:form F :rest remaining-toks} or {:form nil :rest toks :eof true}
|
|
||||||
(define
|
|
||||||
cl-read-form
|
|
||||||
(fn
|
|
||||||
(toks)
|
|
||||||
(if
|
|
||||||
(not toks)
|
|
||||||
{:form nil :rest toks :eof true}
|
|
||||||
(let
|
|
||||||
((tok (nth toks 0)) (nxt (rest toks)))
|
|
||||||
(let
|
|
||||||
((type (get tok "type")) (val (get tok "value")))
|
|
||||||
(cond
|
|
||||||
((= type "eof") {:form nil :rest toks :eof true})
|
|
||||||
((= type "integer") {:form (cl-convert-integer val) :rest nxt})
|
|
||||||
((= type "float") {:form {:cl-type "float" :value val} :rest nxt})
|
|
||||||
((= type "ratio") {:form {:cl-type "ratio" :value val} :rest nxt})
|
|
||||||
((= type "string") {:form {:cl-type "string" :value val} :rest nxt})
|
|
||||||
((= type "char") {:form {:cl-type "char" :value val} :rest nxt})
|
|
||||||
((= type "keyword") {:form {:cl-type "keyword" :name val} :rest nxt})
|
|
||||||
((= type "uninterned") {:form {:cl-type "uninterned" :name val} :rest nxt})
|
|
||||||
((= type "symbol")
|
|
||||||
(cond
|
|
||||||
((= val "NIL") {:form nil :rest nxt})
|
|
||||||
((= val "T") {:form true :rest nxt})
|
|
||||||
(:else {:form val :rest nxt})))
|
|
||||||
;; list forms
|
|
||||||
((= type "lparen") (cl-read-list nxt))
|
|
||||||
((= type "hash-paren") (cl-read-vector nxt))
|
|
||||||
;; reader macros that wrap the next form
|
|
||||||
((= type "quote") (cl-read-wrap "QUOTE" nxt))
|
|
||||||
((= type "backquote") (cl-read-wrap "QUASIQUOTE" nxt))
|
|
||||||
((= type "comma") (cl-read-wrap "UNQUOTE" nxt))
|
|
||||||
((= type "comma-at") (cl-read-wrap "UNQUOTE-SPLICING" nxt))
|
|
||||||
((= type "hash-quote") (cl-read-wrap "FUNCTION" nxt))
|
|
||||||
;; skip unrecognised tokens
|
|
||||||
(:else (cl-read-form nxt))))))))
|
|
||||||
|
|
||||||
;; Wrap next form in a list: (name form)
|
|
||||||
(define
|
|
||||||
cl-read-wrap
|
|
||||||
(fn
|
|
||||||
(name toks)
|
|
||||||
(let
|
|
||||||
((inner (cl-read-form toks)))
|
|
||||||
{:form (list name (get inner "form")) :rest (get inner "rest")})))
|
|
||||||
|
|
||||||
;; Read list forms until ')'; handles dotted pair (a . b)
|
|
||||||
;; Called after consuming '('
|
|
||||||
(define
|
|
||||||
cl-read-list
|
|
||||||
(fn
|
|
||||||
(toks)
|
|
||||||
(let
|
|
||||||
((result (cl-read-list-items toks (list))))
|
|
||||||
{:form (get result "items") :rest (get result "rest")})))
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-read-list-items
|
|
||||||
(fn
|
|
||||||
(toks acc)
|
|
||||||
(if
|
|
||||||
(not toks)
|
|
||||||
{:items acc :rest toks}
|
|
||||||
(let
|
|
||||||
((tok (nth toks 0)))
|
|
||||||
(let
|
|
||||||
((type (get tok "type")))
|
|
||||||
(cond
|
|
||||||
((= type "eof") {:items acc :rest toks})
|
|
||||||
((= type "rparen") {:items acc :rest (rest toks)})
|
|
||||||
;; dotted pair: read one more form then expect ')'
|
|
||||||
((= type "dot")
|
|
||||||
(let
|
|
||||||
((cdr-result (cl-read-form (rest toks))))
|
|
||||||
(let
|
|
||||||
((cdr-form (get cdr-result "form"))
|
|
||||||
(after-cdr (get cdr-result "rest")))
|
|
||||||
;; skip the closing ')'
|
|
||||||
(let
|
|
||||||
((close (if after-cdr (nth after-cdr 0) nil)))
|
|
||||||
(let
|
|
||||||
((remaining
|
|
||||||
(if
|
|
||||||
(and close (= (get close "type") "rparen"))
|
|
||||||
(rest after-cdr)
|
|
||||||
after-cdr)))
|
|
||||||
;; build dotted structure
|
|
||||||
(let
|
|
||||||
((dotted (cl-build-dotted acc cdr-form)))
|
|
||||||
{:items dotted :rest remaining}))))))
|
|
||||||
(:else
|
|
||||||
(let
|
|
||||||
((item (cl-read-form toks)))
|
|
||||||
(cl-read-list-items
|
|
||||||
(get item "rest")
|
|
||||||
(concat acc (list (get item "form"))))))))))))
|
|
||||||
|
|
||||||
;; Build dotted form: (a b . c) → ((DOTTED a b) . c) style
|
|
||||||
;; In CL (a b c . d) means a proper dotted structure.
|
|
||||||
;; We represent it as {:cl-type "cons" :car a :cdr (list->dotted b c d)}
|
|
||||||
(define
|
|
||||||
cl-build-dotted
|
|
||||||
(fn
|
|
||||||
(head-items tail)
|
|
||||||
(if
|
|
||||||
(= (len head-items) 0)
|
|
||||||
tail
|
|
||||||
(if
|
|
||||||
(= (len head-items) 1)
|
|
||||||
{:cl-type "cons" :car (nth head-items 0) :cdr tail}
|
|
||||||
(let
|
|
||||||
((last-item (nth head-items (- (len head-items) 1)))
|
|
||||||
(but-last (slice head-items 0 (- (len head-items) 1))))
|
|
||||||
{:cl-type "cons"
|
|
||||||
:car (cl-build-dotted but-last (list last-item))
|
|
||||||
:cdr tail})))))
|
|
||||||
|
|
||||||
;; Read vector #(…) elements until ')'
|
|
||||||
(define
|
|
||||||
cl-read-vector
|
|
||||||
(fn
|
|
||||||
(toks)
|
|
||||||
(let
|
|
||||||
((result (cl-read-vector-items toks (list))))
|
|
||||||
{:form {:cl-type "vector" :elements (get result "items")} :rest (get result "rest")})))
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-read-vector-items
|
|
||||||
(fn
|
|
||||||
(toks acc)
|
|
||||||
(if
|
|
||||||
(not toks)
|
|
||||||
{:items acc :rest toks}
|
|
||||||
(let
|
|
||||||
((tok (nth toks 0)))
|
|
||||||
(let
|
|
||||||
((type (get tok "type")))
|
|
||||||
(cond
|
|
||||||
((= type "eof") {:items acc :rest toks})
|
|
||||||
((= type "rparen") {:items acc :rest (rest toks)})
|
|
||||||
(:else
|
|
||||||
(let
|
|
||||||
((item (cl-read-form toks)))
|
|
||||||
(cl-read-vector-items
|
|
||||||
(get item "rest")
|
|
||||||
(concat acc (list (get item "form"))))))))))))
|
|
||||||
|
|
||||||
;; ── lambda-list parser ───────────────────────────────────────────
|
|
||||||
;;
|
|
||||||
;; (cl-parse-lambda-list forms) — parse a list of CL forms (already read)
|
|
||||||
;; into a structured dict:
|
|
||||||
;; {:required (list sym ...)
|
|
||||||
;; :optional (list {:name N :default D :supplied S} ...)
|
|
||||||
;; :rest nil | "SYM"
|
|
||||||
;; :key (list {:name N :keyword K :default D :supplied S} ...)
|
|
||||||
;; :allow-other-keys false | true
|
|
||||||
;; :aux (list {:name N :init I} ...)}
|
|
||||||
;;
|
|
||||||
;; Symbols arrive as SX strings (upcase). &-markers are strings like "&OPTIONAL".
|
|
||||||
;; Key params: keyword is the upcase name string; caller uses it as :keyword.
|
|
||||||
;; Supplied-p: nil when absent.
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-parse-opt-spec
|
|
||||||
(fn
|
|
||||||
(spec)
|
|
||||||
(if
|
|
||||||
(list? spec)
|
|
||||||
{:name (nth spec 0)
|
|
||||||
:default (if (> (len spec) 1) (nth spec 1) nil)
|
|
||||||
:supplied (if (> (len spec) 2) (nth spec 2) nil)}
|
|
||||||
{:name spec :default nil :supplied nil})))
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-parse-key-spec
|
|
||||||
(fn
|
|
||||||
(spec)
|
|
||||||
(if
|
|
||||||
(list? spec)
|
|
||||||
(let
|
|
||||||
((first (nth spec 0)))
|
|
||||||
(if
|
|
||||||
(list? first)
|
|
||||||
;; ((:keyword var) default supplied-p)
|
|
||||||
{:name (nth first 1)
|
|
||||||
:keyword (get first "name")
|
|
||||||
:default (if (> (len spec) 1) (nth spec 1) nil)
|
|
||||||
:supplied (if (> (len spec) 2) (nth spec 2) nil)}
|
|
||||||
;; (var default supplied-p)
|
|
||||||
{:name first
|
|
||||||
:keyword first
|
|
||||||
:default (if (> (len spec) 1) (nth spec 1) nil)
|
|
||||||
:supplied (if (> (len spec) 2) (nth spec 2) nil)}))
|
|
||||||
{:name spec :keyword spec :default nil :supplied nil})))
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-parse-aux-spec
|
|
||||||
(fn
|
|
||||||
(spec)
|
|
||||||
(if
|
|
||||||
(list? spec)
|
|
||||||
{:name (nth spec 0) :init (if (> (len spec) 1) (nth spec 1) nil)}
|
|
||||||
{:name spec :init nil})))
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-parse-lambda-list
|
|
||||||
(fn
|
|
||||||
(forms)
|
|
||||||
(let
|
|
||||||
((state "required")
|
|
||||||
(required (list))
|
|
||||||
(optional (list))
|
|
||||||
(rest-name nil)
|
|
||||||
(key (list))
|
|
||||||
(allow-other-keys false)
|
|
||||||
(aux (list)))
|
|
||||||
|
|
||||||
(define
|
|
||||||
scan
|
|
||||||
(fn
|
|
||||||
(items)
|
|
||||||
(when
|
|
||||||
(> (len items) 0)
|
|
||||||
(let
|
|
||||||
((item (nth items 0)) (tail (rest items)))
|
|
||||||
(cond
|
|
||||||
((= item "&OPTIONAL")
|
|
||||||
(do (set! state "optional") (scan tail)))
|
|
||||||
((= item "&REST")
|
|
||||||
(do (set! state "rest") (scan tail)))
|
|
||||||
((= item "&BODY")
|
|
||||||
(do (set! state "rest") (scan tail)))
|
|
||||||
((= item "&KEY")
|
|
||||||
(do (set! state "key") (scan tail)))
|
|
||||||
((= item "&AUX")
|
|
||||||
(do (set! state "aux") (scan tail)))
|
|
||||||
((= item "&ALLOW-OTHER-KEYS")
|
|
||||||
(do (set! allow-other-keys true) (scan tail)))
|
|
||||||
((= state "required")
|
|
||||||
(do (append! required item) (scan tail)))
|
|
||||||
((= state "optional")
|
|
||||||
(do (append! optional (cl-parse-opt-spec item)) (scan tail)))
|
|
||||||
((= state "rest")
|
|
||||||
(do (set! rest-name item) (set! state "done") (scan tail)))
|
|
||||||
((= state "key")
|
|
||||||
(do (append! key (cl-parse-key-spec item)) (scan tail)))
|
|
||||||
((= state "aux")
|
|
||||||
(do (append! aux (cl-parse-aux-spec item)) (scan tail)))
|
|
||||||
(:else (scan tail)))))))
|
|
||||||
|
|
||||||
(scan forms)
|
|
||||||
{:required required
|
|
||||||
:optional optional
|
|
||||||
:rest rest-name
|
|
||||||
:key key
|
|
||||||
:allow-other-keys allow-other-keys
|
|
||||||
:aux aux})))
|
|
||||||
|
|
||||||
;; Convenience: parse lambda list from a CL source string
|
|
||||||
(define
|
|
||||||
cl-parse-lambda-list-str
|
|
||||||
(fn
|
|
||||||
(src)
|
|
||||||
(cl-parse-lambda-list (cl-read src))))
|
|
||||||
|
|
||||||
;; ── public API ────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-read
|
|
||||||
(fn
|
|
||||||
(src)
|
|
||||||
(let
|
|
||||||
((toks (cl-tokenize src)))
|
|
||||||
(get (cl-read-form toks) "form"))))
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-read-all
|
|
||||||
(fn
|
|
||||||
(src)
|
|
||||||
(let
|
|
||||||
((toks (cl-tokenize src)))
|
|
||||||
(define
|
|
||||||
loop
|
|
||||||
(fn
|
|
||||||
(toks acc)
|
|
||||||
(if
|
|
||||||
(or (not toks) (= (get (nth toks 0) "type") "eof"))
|
|
||||||
acc
|
|
||||||
(let
|
|
||||||
((result (cl-read-form toks)))
|
|
||||||
(if
|
|
||||||
(get result "eof")
|
|
||||||
acc
|
|
||||||
(loop (get result "rest") (concat acc (list (get result "form")))))))))
|
|
||||||
(loop toks (list)))))
|
|
||||||
@@ -1,381 +0,0 @@
|
|||||||
;; Common Lisp tokenizer
|
|
||||||
;;
|
|
||||||
;; Tokens: {:type T :value V :pos P}
|
|
||||||
;;
|
|
||||||
;; Types:
|
|
||||||
;; "symbol" — FOO, PKG:SYM, PKG::SYM, T, NIL (upcase)
|
|
||||||
;; "keyword" — :foo (value is upcase name without colon)
|
|
||||||
;; "integer" — 42, -5, #xFF, #b1010, #o17 (string)
|
|
||||||
;; "float" — 3.14, 1.0e10 (string)
|
|
||||||
;; "ratio" — 1/3 (string "N/D")
|
|
||||||
;; "string" — unescaped content
|
|
||||||
;; "char" — single-character string
|
|
||||||
;; "lparen" "rparen" "quote" "backquote" "comma" "comma-at"
|
|
||||||
;; "hash-quote" — #'
|
|
||||||
;; "hash-paren" — #(
|
|
||||||
;; "uninterned" — #:foo (upcase name)
|
|
||||||
;; "dot" — standalone . (dotted pair separator)
|
|
||||||
;; "eof"
|
|
||||||
|
|
||||||
(define cl-make-tok (fn (type value pos) {:type type :value value :pos pos}))
|
|
||||||
|
|
||||||
;; ── char ordinal table ────────────────────────────────────────────
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-ord-table
|
|
||||||
(let
|
|
||||||
((t (dict)) (i 0))
|
|
||||||
(define
|
|
||||||
cl-fill
|
|
||||||
(fn
|
|
||||||
()
|
|
||||||
(when
|
|
||||||
(< i 128)
|
|
||||||
(do
|
|
||||||
(dict-set! t (char-from-code i) i)
|
|
||||||
(set! i (+ i 1))
|
|
||||||
(cl-fill)))))
|
|
||||||
(cl-fill)
|
|
||||||
t))
|
|
||||||
|
|
||||||
(define cl-ord (fn (c) (or (get cl-ord-table c) 0)))
|
|
||||||
|
|
||||||
;; ── character predicates ──────────────────────────────────────────
|
|
||||||
|
|
||||||
(define cl-digit? (fn (c) (and (>= (cl-ord c) 48) (<= (cl-ord c) 57))))
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-hex?
|
|
||||||
(fn
|
|
||||||
(c)
|
|
||||||
(or
|
|
||||||
(cl-digit? c)
|
|
||||||
(and (>= (cl-ord c) 65) (<= (cl-ord c) 70))
|
|
||||||
(and (>= (cl-ord c) 97) (<= (cl-ord c) 102)))))
|
|
||||||
|
|
||||||
(define cl-octal? (fn (c) (and (>= (cl-ord c) 48) (<= (cl-ord c) 55))))
|
|
||||||
|
|
||||||
(define cl-binary? (fn (c) (or (= c "0") (= c "1"))))
|
|
||||||
|
|
||||||
(define cl-ws? (fn (c) (or (= c " ") (= c "\t") (= c "\n") (= c "\r"))))
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-alpha?
|
|
||||||
(fn
|
|
||||||
(c)
|
|
||||||
(or
|
|
||||||
(and (>= (cl-ord c) 65) (<= (cl-ord c) 90))
|
|
||||||
(and (>= (cl-ord c) 97) (<= (cl-ord c) 122)))))
|
|
||||||
|
|
||||||
;; Characters that end a token (whitespace + terminating macro chars)
|
|
||||||
(define
|
|
||||||
cl-terminating?
|
|
||||||
(fn
|
|
||||||
(c)
|
|
||||||
(or
|
|
||||||
(cl-ws? c)
|
|
||||||
(= c "(")
|
|
||||||
(= c ")")
|
|
||||||
(= c "\"")
|
|
||||||
(= c ";")
|
|
||||||
(= c "`")
|
|
||||||
(= c ","))))
|
|
||||||
|
|
||||||
;; Symbol constituent: not terminating, not reader-special
|
|
||||||
(define
|
|
||||||
cl-sym-char?
|
|
||||||
(fn
|
|
||||||
(c)
|
|
||||||
(not
|
|
||||||
(or
|
|
||||||
(cl-terminating? c)
|
|
||||||
(= c "#")
|
|
||||||
(= c "|")
|
|
||||||
(= c "\\")
|
|
||||||
(= c "'")))))
|
|
||||||
|
|
||||||
;; ── named character table ─────────────────────────────────────────
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-named-chars
|
|
||||||
{:space " "
|
|
||||||
:newline "\n"
|
|
||||||
:tab "\t"
|
|
||||||
:return "\r"
|
|
||||||
:backspace (char-from-code 8)
|
|
||||||
:rubout (char-from-code 127)
|
|
||||||
:delete (char-from-code 127)
|
|
||||||
:escape (char-from-code 27)
|
|
||||||
:altmode (char-from-code 27)
|
|
||||||
:null (char-from-code 0)
|
|
||||||
:nul (char-from-code 0)
|
|
||||||
:page (char-from-code 12)
|
|
||||||
:formfeed (char-from-code 12)})
|
|
||||||
|
|
||||||
;; ── main tokenizer ────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-tokenize
|
|
||||||
(fn
|
|
||||||
(src)
|
|
||||||
(let
|
|
||||||
((pos 0) (n (string-length src)) (toks (list)))
|
|
||||||
|
|
||||||
(define at (fn () (if (< pos n) (substring src pos (+ pos 1)) nil)))
|
|
||||||
(define peek1 (fn () (if (< (+ pos 1) n) (substring src (+ pos 1) (+ pos 2)) nil)))
|
|
||||||
(define adv (fn () (set! pos (+ pos 1))))
|
|
||||||
|
|
||||||
;; Advance while predicate holds; return substring from start to end
|
|
||||||
(define
|
|
||||||
read-while
|
|
||||||
(fn
|
|
||||||
(pred)
|
|
||||||
(let
|
|
||||||
((start pos))
|
|
||||||
(define
|
|
||||||
rw-loop
|
|
||||||
(fn
|
|
||||||
()
|
|
||||||
(when
|
|
||||||
(and (at) (pred (at)))
|
|
||||||
(do (adv) (rw-loop)))))
|
|
||||||
(rw-loop)
|
|
||||||
(substring src start pos))))
|
|
||||||
|
|
||||||
(define
|
|
||||||
skip-line
|
|
||||||
(fn
|
|
||||||
()
|
|
||||||
(when
|
|
||||||
(and (at) (not (= (at) "\n")))
|
|
||||||
(do (adv) (skip-line)))))
|
|
||||||
|
|
||||||
(define
|
|
||||||
skip-block
|
|
||||||
(fn
|
|
||||||
(depth)
|
|
||||||
(when
|
|
||||||
(at)
|
|
||||||
(cond
|
|
||||||
((and (= (at) "#") (= (peek1) "|"))
|
|
||||||
(do (adv) (adv) (skip-block (+ depth 1))))
|
|
||||||
((and (= (at) "|") (= (peek1) "#"))
|
|
||||||
(do
|
|
||||||
(adv)
|
|
||||||
(adv)
|
|
||||||
(when (> depth 1) (skip-block (- depth 1)))))
|
|
||||||
(:else (do (adv) (skip-block depth)))))))
|
|
||||||
|
|
||||||
;; Read string literal — called with pos just past opening "
|
|
||||||
(define
|
|
||||||
read-str
|
|
||||||
(fn
|
|
||||||
(acc)
|
|
||||||
(if
|
|
||||||
(not (at))
|
|
||||||
acc
|
|
||||||
(cond
|
|
||||||
((= (at) "\"") (do (adv) acc))
|
|
||||||
((= (at) "\\")
|
|
||||||
(do
|
|
||||||
(adv)
|
|
||||||
(let
|
|
||||||
((e (at)))
|
|
||||||
(adv)
|
|
||||||
(read-str
|
|
||||||
(str
|
|
||||||
acc
|
|
||||||
(cond
|
|
||||||
((= e "n") "\n")
|
|
||||||
((= e "t") "\t")
|
|
||||||
((= e "r") "\r")
|
|
||||||
((= e "\"") "\"")
|
|
||||||
((= e "\\") "\\")
|
|
||||||
(:else e)))))))
|
|
||||||
(:else
|
|
||||||
(let
|
|
||||||
((c (at)))
|
|
||||||
(adv)
|
|
||||||
(read-str (str acc c))))))))
|
|
||||||
|
|
||||||
;; Read #\ char literal — called with pos just past the backslash
|
|
||||||
(define
|
|
||||||
read-char-lit
|
|
||||||
(fn
|
|
||||||
()
|
|
||||||
(let
|
|
||||||
((first (at)))
|
|
||||||
(adv)
|
|
||||||
(let
|
|
||||||
((rest (if (and (at) (cl-alpha? (at))) (read-while cl-alpha?) "")))
|
|
||||||
(if
|
|
||||||
(= rest "")
|
|
||||||
first
|
|
||||||
(let
|
|
||||||
((name (downcase (str first rest))))
|
|
||||||
(or (get cl-named-chars name) first)))))))
|
|
||||||
|
|
||||||
;; Number scanner — called with pos just past first digit(s).
|
|
||||||
;; acc holds what was already consumed (first digit or sign+digit).
|
|
||||||
(define
|
|
||||||
scan-num
|
|
||||||
(fn
|
|
||||||
(p acc)
|
|
||||||
(let
|
|
||||||
((more (read-while cl-digit?)))
|
|
||||||
(set! acc (str acc more))
|
|
||||||
(cond
|
|
||||||
;; ratio N/D
|
|
||||||
((and (at) (= (at) "/") (peek1) (cl-digit? (peek1)))
|
|
||||||
(do
|
|
||||||
(adv)
|
|
||||||
(let
|
|
||||||
((denom (read-while cl-digit?)))
|
|
||||||
{:type "ratio" :value (str acc "/" denom) :pos p})))
|
|
||||||
;; float: decimal point N.M[eE]
|
|
||||||
((and (at) (= (at) ".") (peek1) (cl-digit? (peek1)))
|
|
||||||
(do
|
|
||||||
(adv)
|
|
||||||
(let
|
|
||||||
((frac (read-while cl-digit?)))
|
|
||||||
(set! acc (str acc "." frac))
|
|
||||||
(when
|
|
||||||
(and (at) (or (= (at) "e") (= (at) "E")))
|
|
||||||
(do
|
|
||||||
(set! acc (str acc (at)))
|
|
||||||
(adv)
|
|
||||||
(when
|
|
||||||
(and (at) (or (= (at) "+") (= (at) "-")))
|
|
||||||
(do (set! acc (str acc (at))) (adv)))
|
|
||||||
(set! acc (str acc (read-while cl-digit?)))))
|
|
||||||
{:type "float" :value acc :pos p})))
|
|
||||||
;; float: exponent only NeE
|
|
||||||
((and (at) (or (= (at) "e") (= (at) "E")))
|
|
||||||
(do
|
|
||||||
(set! acc (str acc (at)))
|
|
||||||
(adv)
|
|
||||||
(when
|
|
||||||
(and (at) (or (= (at) "+") (= (at) "-")))
|
|
||||||
(do (set! acc (str acc (at))) (adv)))
|
|
||||||
(set! acc (str acc (read-while cl-digit?)))
|
|
||||||
{:type "float" :value acc :pos p}))
|
|
||||||
(:else {:type "integer" :value acc :pos p})))))
|
|
||||||
|
|
||||||
(define
|
|
||||||
read-radix
|
|
||||||
(fn
|
|
||||||
(letter p)
|
|
||||||
(let
|
|
||||||
((pred
|
|
||||||
(cond
|
|
||||||
((or (= letter "x") (= letter "X")) cl-hex?)
|
|
||||||
((or (= letter "b") (= letter "B")) cl-binary?)
|
|
||||||
((or (= letter "o") (= letter "O")) cl-octal?)
|
|
||||||
(:else cl-digit?))))
|
|
||||||
{:type "integer"
|
|
||||||
:value (str "#" letter (read-while pred))
|
|
||||||
:pos p})))
|
|
||||||
|
|
||||||
(define emit (fn (tok) (append! toks tok)))
|
|
||||||
|
|
||||||
(define
|
|
||||||
scan
|
|
||||||
(fn
|
|
||||||
()
|
|
||||||
(when
|
|
||||||
(< pos n)
|
|
||||||
(let
|
|
||||||
((c (at)) (p pos))
|
|
||||||
(cond
|
|
||||||
((cl-ws? c) (do (adv) (scan)))
|
|
||||||
((= c ";") (do (adv) (skip-line) (scan)))
|
|
||||||
((= c "(") (do (adv) (emit (cl-make-tok "lparen" "(" p)) (scan)))
|
|
||||||
((= c ")") (do (adv) (emit (cl-make-tok "rparen" ")" p)) (scan)))
|
|
||||||
((= c "'") (do (adv) (emit (cl-make-tok "quote" "'" p)) (scan)))
|
|
||||||
((= c "`") (do (adv) (emit (cl-make-tok "backquote" "`" p)) (scan)))
|
|
||||||
((= c ",")
|
|
||||||
(do
|
|
||||||
(adv)
|
|
||||||
(if
|
|
||||||
(= (at) "@")
|
|
||||||
(do (adv) (emit (cl-make-tok "comma-at" ",@" p)))
|
|
||||||
(emit (cl-make-tok "comma" "," p)))
|
|
||||||
(scan)))
|
|
||||||
((= c "\"")
|
|
||||||
(do
|
|
||||||
(adv)
|
|
||||||
(emit (cl-make-tok "string" (read-str "") p))
|
|
||||||
(scan)))
|
|
||||||
;; :keyword
|
|
||||||
((= c ":")
|
|
||||||
(do
|
|
||||||
(adv)
|
|
||||||
(emit (cl-make-tok "keyword" (upcase (read-while cl-sym-char?)) p))
|
|
||||||
(scan)))
|
|
||||||
;; dispatch macro #
|
|
||||||
((= c "#")
|
|
||||||
(do
|
|
||||||
(adv)
|
|
||||||
(let
|
|
||||||
((d (at)))
|
|
||||||
(cond
|
|
||||||
((= d "'") (do (adv) (emit (cl-make-tok "hash-quote" "#'" p)) (scan)))
|
|
||||||
((= d "(") (do (adv) (emit (cl-make-tok "hash-paren" "#(" p)) (scan)))
|
|
||||||
((= d ":")
|
|
||||||
(do
|
|
||||||
(adv)
|
|
||||||
(emit
|
|
||||||
(cl-make-tok "uninterned" (upcase (read-while cl-sym-char?)) p))
|
|
||||||
(scan)))
|
|
||||||
((= d "|") (do (adv) (skip-block 1) (scan)))
|
|
||||||
((= d "\\")
|
|
||||||
(do (adv) (emit (cl-make-tok "char" (read-char-lit) p)) (scan)))
|
|
||||||
((or (= d "x") (= d "X"))
|
|
||||||
(do (adv) (emit (read-radix d p)) (scan)))
|
|
||||||
((or (= d "b") (= d "B"))
|
|
||||||
(do (adv) (emit (read-radix d p)) (scan)))
|
|
||||||
((or (= d "o") (= d "O"))
|
|
||||||
(do (adv) (emit (read-radix d p)) (scan)))
|
|
||||||
(:else (scan))))))
|
|
||||||
;; standalone dot, float .5, or symbol starting with dots
|
|
||||||
((= c ".")
|
|
||||||
(do
|
|
||||||
(adv)
|
|
||||||
(cond
|
|
||||||
((or (not (at)) (cl-terminating? (at)))
|
|
||||||
(do (emit (cl-make-tok "dot" "." p)) (scan)))
|
|
||||||
((cl-digit? (at))
|
|
||||||
(do
|
|
||||||
(emit
|
|
||||||
(cl-make-tok "float" (str "0." (read-while cl-digit?)) p))
|
|
||||||
(scan)))
|
|
||||||
(:else
|
|
||||||
(do
|
|
||||||
(emit
|
|
||||||
(cl-make-tok "symbol" (upcase (str "." (read-while cl-sym-char?))) p))
|
|
||||||
(scan))))))
|
|
||||||
;; sign followed by digit → number
|
|
||||||
((and (or (= c "+") (= c "-")) (peek1) (cl-digit? (peek1)))
|
|
||||||
(do
|
|
||||||
(adv)
|
|
||||||
(let
|
|
||||||
((first-d (at)))
|
|
||||||
(adv)
|
|
||||||
(emit (scan-num p (str c first-d))))
|
|
||||||
(scan)))
|
|
||||||
;; decimal digit → number
|
|
||||||
((cl-digit? c)
|
|
||||||
(do
|
|
||||||
(adv)
|
|
||||||
(emit (scan-num p c))
|
|
||||||
(scan)))
|
|
||||||
;; symbol constituent (includes bare +, -, etc.)
|
|
||||||
((cl-sym-char? c)
|
|
||||||
(do
|
|
||||||
(emit (cl-make-tok "symbol" (upcase (read-while cl-sym-char?)) p))
|
|
||||||
(scan)))
|
|
||||||
(:else (do (adv) (scan))))))))
|
|
||||||
|
|
||||||
(scan)
|
|
||||||
(append! toks (cl-make-tok "eof" nil n))
|
|
||||||
toks)))
|
|
||||||
@@ -1,14 +1,18 @@
|
|||||||
;; lib/common-lisp/runtime.sx — CL built-ins + condition system on SX
|
;; lib/common-lisp/runtime.sx — CL built-ins using SX spec primitives
|
||||||
;;
|
;;
|
||||||
;; Section 1-9: Type predicates, arithmetic, characters, strings, gensym,
|
;; Provides CL-specific wrappers and helpers. Deliberately thin: wherever
|
||||||
;; multiple values, sets, radix formatting, list utilities.
|
;; an SX spec primitive already does the job, we alias it rather than
|
||||||
;; Section 10: Condition system (define-condition, signal/error/warn,
|
;; reinventing it.
|
||||||
;; handler-bind, handler-case, restart-case, invoke-restart).
|
|
||||||
;;
|
;;
|
||||||
;; Primitives used from spec:
|
;; Primitives used from spec:
|
||||||
;; char/char->integer/integer->char/char-upcase/char-downcase
|
;; char/char->integer/integer->char/char-upcase/char-downcase
|
||||||
;; format gensym rational/rational? make-set/set-member?/etc
|
;; format (Phase 21 — must be loaded before this file)
|
||||||
;; modulo/remainder/quotient/gcd/lcm/expt number->string
|
;; gensym (Phase 12)
|
||||||
|
;; rational/rational? (Phase 16)
|
||||||
|
;; make-set/set-member?/set-union/etc (Phase 18)
|
||||||
|
;; open-input-string/read-char/etc (Phase 14)
|
||||||
|
;; modulo/remainder/quotient/gcd/lcm/expt (Phase 2 / Phase 15)
|
||||||
|
;; number->string with radix (Phase 15)
|
||||||
|
|
||||||
;; ---------------------------------------------------------------------------
|
;; ---------------------------------------------------------------------------
|
||||||
;; 1. Type predicates
|
;; 1. Type predicates
|
||||||
@@ -300,425 +304,3 @@
|
|||||||
((or (cl-empty? plist) (cl-empty? (rest plist))) nil)
|
((or (cl-empty? plist) (cl-empty? (rest plist))) nil)
|
||||||
((equal? (first plist) key) (first (rest plist)))
|
((equal? (first plist) key) (first (rest plist)))
|
||||||
(else (cl-getf (rest (rest plist)) key))))
|
(else (cl-getf (rest (rest plist)) key))))
|
||||||
|
|
||||||
;; ---------------------------------------------------------------------------
|
|
||||||
;; 10. Condition system (Phase 3)
|
|
||||||
;;
|
|
||||||
;; Condition objects:
|
|
||||||
;; {:cl-type "cl-condition" :class "NAME" :slots {slot-name val ...}}
|
|
||||||
;;
|
|
||||||
;; The built-in handler-bind / restart-case expect LITERAL handler specs in
|
|
||||||
;; source (they operate on the raw AST), so we implement our own handler and
|
|
||||||
;; restart stacks as mutable SX globals.
|
|
||||||
;; ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
;; ── condition class registry ───────────────────────────────────────────────
|
|
||||||
;;
|
|
||||||
;; Populated at load time with all ANSI standard condition types.
|
|
||||||
;; Also mutated by cl-define-condition.
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-condition-classes
|
|
||||||
(dict
|
|
||||||
"condition"
|
|
||||||
{:parents (list) :slots (list) :name "condition"}
|
|
||||||
"serious-condition"
|
|
||||||
{:parents (list "condition") :slots (list) :name "serious-condition"}
|
|
||||||
"error"
|
|
||||||
{:parents (list "serious-condition") :slots (list) :name "error"}
|
|
||||||
"warning"
|
|
||||||
{:parents (list "condition") :slots (list) :name "warning"}
|
|
||||||
"simple-condition"
|
|
||||||
{:parents (list "condition") :slots (list "format-control" "format-arguments") :name "simple-condition"}
|
|
||||||
"simple-error"
|
|
||||||
{:parents (list "error" "simple-condition") :slots (list "format-control" "format-arguments") :name "simple-error"}
|
|
||||||
"simple-warning"
|
|
||||||
{:parents (list "warning" "simple-condition") :slots (list "format-control" "format-arguments") :name "simple-warning"}
|
|
||||||
"type-error"
|
|
||||||
{:parents (list "error") :slots (list "datum" "expected-type") :name "type-error"}
|
|
||||||
"arithmetic-error"
|
|
||||||
{:parents (list "error") :slots (list "operation" "operands") :name "arithmetic-error"}
|
|
||||||
"division-by-zero"
|
|
||||||
{:parents (list "arithmetic-error") :slots (list) :name "division-by-zero"}
|
|
||||||
"cell-error"
|
|
||||||
{:parents (list "error") :slots (list "name") :name "cell-error"}
|
|
||||||
"unbound-variable"
|
|
||||||
{:parents (list "cell-error") :slots (list) :name "unbound-variable"}
|
|
||||||
"undefined-function"
|
|
||||||
{:parents (list "cell-error") :slots (list) :name "undefined-function"}
|
|
||||||
"program-error"
|
|
||||||
{:parents (list "error") :slots (list) :name "program-error"}
|
|
||||||
"storage-condition"
|
|
||||||
{:parents (list "serious-condition") :slots (list) :name "storage-condition"}))
|
|
||||||
|
|
||||||
;; ── condition predicates ───────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-condition?
|
|
||||||
(fn (x) (and (dict? x) (= (get x "cl-type") "cl-condition"))))
|
|
||||||
|
|
||||||
;; cl-condition-of-type? walks the class hierarchy.
|
|
||||||
;; We capture cl-condition-classes at define time via let to avoid
|
|
||||||
;; free-variable scoping issues at call time.
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-condition-of-type?
|
|
||||||
(let
|
|
||||||
((classes cl-condition-classes))
|
|
||||||
(fn
|
|
||||||
(c type-name)
|
|
||||||
(if
|
|
||||||
(not (cl-condition? c))
|
|
||||||
false
|
|
||||||
(let
|
|
||||||
((class-name (get c "class")))
|
|
||||||
(define
|
|
||||||
check
|
|
||||||
(fn
|
|
||||||
(n)
|
|
||||||
(if
|
|
||||||
(= n type-name)
|
|
||||||
true
|
|
||||||
(let
|
|
||||||
((entry (get classes n)))
|
|
||||||
(if
|
|
||||||
(nil? entry)
|
|
||||||
false
|
|
||||||
(some (fn (p) (check p)) (get entry "parents")))))))
|
|
||||||
(check class-name))))))
|
|
||||||
|
|
||||||
;; ── condition constructors ─────────────────────────────────────────────────
|
|
||||||
|
|
||||||
;; cl-define-condition registers a new condition class.
|
|
||||||
;; name: string (condition class name)
|
|
||||||
;; parents: list of strings (parent class names)
|
|
||||||
;; slot-names: list of strings
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-define-condition
|
|
||||||
(fn
|
|
||||||
(name parents slot-names)
|
|
||||||
(begin (dict-set! cl-condition-classes name {:parents parents :slots slot-names :name name}) name)))
|
|
||||||
|
|
||||||
;; cl-make-condition constructs a condition object.
|
|
||||||
;; Keyword args (alternating slot-name/value pairs) populate the slots dict.
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-make-condition
|
|
||||||
(fn
|
|
||||||
(name &rest kw-args)
|
|
||||||
(let
|
|
||||||
((slots (dict)))
|
|
||||||
(define
|
|
||||||
fill
|
|
||||||
(fn
|
|
||||||
(args)
|
|
||||||
(when
|
|
||||||
(>= (len args) 2)
|
|
||||||
(begin
|
|
||||||
(dict-set! slots (first args) (first (rest args)))
|
|
||||||
(fill (rest (rest args)))))))
|
|
||||||
(fill kw-args)
|
|
||||||
{:cl-type "cl-condition" :slots slots :class name})))
|
|
||||||
|
|
||||||
;; ── condition accessors ────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-condition-slot
|
|
||||||
(fn
|
|
||||||
(c slot-name)
|
|
||||||
(if (cl-condition? c) (get (get c "slots") slot-name) nil)))
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-condition-message
|
|
||||||
(fn
|
|
||||||
(c)
|
|
||||||
(if
|
|
||||||
(not (cl-condition? c))
|
|
||||||
(str c)
|
|
||||||
(let
|
|
||||||
((slots (get c "slots")))
|
|
||||||
(or
|
|
||||||
(get slots "message")
|
|
||||||
(get slots "format-control")
|
|
||||||
(str "Condition: " (get c "class")))))))
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-simple-condition-format-control
|
|
||||||
(fn (c) (cl-condition-slot c "format-control")))
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-simple-condition-format-arguments
|
|
||||||
(fn (c) (cl-condition-slot c "format-arguments")))
|
|
||||||
|
|
||||||
(define cl-type-error-datum (fn (c) (cl-condition-slot c "datum")))
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-type-error-expected-type
|
|
||||||
(fn (c) (cl-condition-slot c "expected-type")))
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-arithmetic-error-operation
|
|
||||||
(fn (c) (cl-condition-slot c "operation")))
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-arithmetic-error-operands
|
|
||||||
(fn (c) (cl-condition-slot c "operands")))
|
|
||||||
|
|
||||||
;; ── mutable handler + restart stacks ──────────────────────────────────────
|
|
||||||
;;
|
|
||||||
;; Handler entry: {:type "type-name" :fn (fn (condition) result)}
|
|
||||||
;; Restart entry: {:name "restart-name" :fn (fn (&optional arg) result) :escape k}
|
|
||||||
;;
|
|
||||||
;; New handlers are prepended (checked first = most recent handler wins).
|
|
||||||
|
|
||||||
(define cl-handler-stack (list))
|
|
||||||
(define cl-restart-stack (list))
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-push-handlers
|
|
||||||
(fn (entries) (set! cl-handler-stack (append entries cl-handler-stack))))
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-pop-handlers
|
|
||||||
(fn
|
|
||||||
(n)
|
|
||||||
(set! cl-handler-stack (slice cl-handler-stack n (len cl-handler-stack)))))
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-push-restarts
|
|
||||||
(fn (entries) (set! cl-restart-stack (append entries cl-restart-stack))))
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-pop-restarts
|
|
||||||
(fn
|
|
||||||
(n)
|
|
||||||
(set! cl-restart-stack (slice cl-restart-stack n (len cl-restart-stack)))))
|
|
||||||
|
|
||||||
;; ── *debugger-hook* + invoke-debugger ────────────────────────────────────
|
|
||||||
;;
|
|
||||||
;; cl-debugger-hook: called when an error propagates with no handler.
|
|
||||||
;; Signature: (fn (condition hook) result). The hook arg is itself
|
|
||||||
;; (so the hook can rebind it to nil to prevent recursion).
|
|
||||||
;; nil = use default (re-raise as host error).
|
|
||||||
|
|
||||||
(define cl-debugger-hook nil)
|
|
||||||
|
|
||||||
(define cl-invoke-debugger
|
|
||||||
(fn (c)
|
|
||||||
(if (nil? cl-debugger-hook)
|
|
||||||
(error (str "Debugger: " (cl-condition-message c)))
|
|
||||||
(let ((hook cl-debugger-hook))
|
|
||||||
(set! cl-debugger-hook nil)
|
|
||||||
(let ((result (hook c hook)))
|
|
||||||
(set! cl-debugger-hook hook)
|
|
||||||
result)))))
|
|
||||||
|
|
||||||
;; ── *break-on-signals* ────────────────────────────────────────────────────
|
|
||||||
;;
|
|
||||||
;; When set to a type name string, cl-signal invokes the debugger hook
|
|
||||||
;; before walking handlers if the condition is of that type.
|
|
||||||
;; nil = disabled (ANSI default).
|
|
||||||
|
|
||||||
(define cl-break-on-signals nil)
|
|
||||||
|
|
||||||
;; ── invoke-restart-interactively ──────────────────────────────────────────
|
|
||||||
;;
|
|
||||||
;; Like invoke-restart but calls the restart's fn with no arguments
|
|
||||||
;; (real CL would prompt the user for each arg via :interactive).
|
|
||||||
|
|
||||||
(define cl-invoke-restart-interactively
|
|
||||||
(fn (name)
|
|
||||||
(let ((entry (cl-find-restart-entry name cl-restart-stack)))
|
|
||||||
(if (nil? entry)
|
|
||||||
(error (str "No active restart: " name))
|
|
||||||
(let ((restart-fn (get entry "fn"))
|
|
||||||
(escape (get entry "escape")))
|
|
||||||
(escape (restart-fn)))))))
|
|
||||||
|
|
||||||
;; ── cl-signal (non-unwinding) ─────────────────────────────────────────────
|
|
||||||
;;
|
|
||||||
;; Walks cl-handler-stack; for each matching entry, calls the handler fn.
|
|
||||||
;; Handlers return normally — signal continues to the next matching handler.
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-signal-obj
|
|
||||||
(fn
|
|
||||||
(obj stack)
|
|
||||||
(if
|
|
||||||
(empty? stack)
|
|
||||||
nil
|
|
||||||
(let
|
|
||||||
((entry (first stack)))
|
|
||||||
(if
|
|
||||||
(cl-condition-of-type? obj (get entry "type"))
|
|
||||||
(begin ((get entry "fn") obj) (cl-signal-obj obj (rest stack)))
|
|
||||||
(cl-signal-obj obj (rest stack)))))))
|
|
||||||
|
|
||||||
(define cl-signal
|
|
||||||
(fn (c)
|
|
||||||
(let ((obj (if (cl-condition? c)
|
|
||||||
c
|
|
||||||
(cl-make-condition "simple-condition"
|
|
||||||
"format-control" (str c)))))
|
|
||||||
;; *break-on-signals*: invoke debugger hook when type matches
|
|
||||||
(when (and (not (nil? cl-break-on-signals))
|
|
||||||
(cl-condition-of-type? obj cl-break-on-signals))
|
|
||||||
(cl-invoke-debugger obj))
|
|
||||||
(cl-signal-obj obj cl-handler-stack))))
|
|
||||||
|
|
||||||
;; ── cl-error ───────────────────────────────────────────────────────────────
|
|
||||||
;;
|
|
||||||
;; Signals an error. If no handler catches it, raises a host-level error.
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-error
|
|
||||||
(fn
|
|
||||||
(c &rest args)
|
|
||||||
(let
|
|
||||||
((obj (cond ((cl-condition? c) c) ((string? c) (cl-make-condition "simple-error" "format-control" c "format-arguments" args)) (:else (cl-make-condition "simple-error" "format-control" (str c))))))
|
|
||||||
(cl-signal-obj obj cl-handler-stack)
|
|
||||||
(cl-invoke-debugger obj))))
|
|
||||||
|
|
||||||
;; ── cl-warn ────────────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-warn
|
|
||||||
(fn
|
|
||||||
(c &rest args)
|
|
||||||
(let
|
|
||||||
((obj (cond ((cl-condition? c) c) ((string? c) (cl-make-condition "simple-warning" "format-control" c "format-arguments" args)) (:else (cl-make-condition "simple-warning" "format-control" (str c))))))
|
|
||||||
(cl-signal-obj obj cl-handler-stack))))
|
|
||||||
|
|
||||||
;; ── cl-handler-bind (non-unwinding) ───────────────────────────────────────
|
|
||||||
;;
|
|
||||||
;; bindings: list of (type-name handler-fn) pairs
|
|
||||||
;; thunk: (fn () body)
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-handler-bind
|
|
||||||
(fn
|
|
||||||
(bindings thunk)
|
|
||||||
(let
|
|
||||||
((entries (map (fn (b) {:fn (first (rest b)) :type (first b)}) bindings)))
|
|
||||||
(begin
|
|
||||||
(cl-push-handlers entries)
|
|
||||||
(let
|
|
||||||
((result (thunk)))
|
|
||||||
(begin (cl-pop-handlers (len entries)) result))))))
|
|
||||||
|
|
||||||
;; ── cl-handler-case (unwinding) ───────────────────────────────────────────
|
|
||||||
;;
|
|
||||||
;; thunk: (fn () body)
|
|
||||||
;; cases: list of (type-name handler-fn) pairs
|
|
||||||
;;
|
|
||||||
;; Uses call/cc for the escape continuation.
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-handler-case
|
|
||||||
(fn
|
|
||||||
(thunk &rest cases)
|
|
||||||
(call/cc
|
|
||||||
(fn
|
|
||||||
(escape)
|
|
||||||
(let
|
|
||||||
((entries (map (fn (c) {:fn (fn (x) (escape ((first (rest c)) x))) :type (first c)}) cases)))
|
|
||||||
(begin
|
|
||||||
(cl-push-handlers entries)
|
|
||||||
(let
|
|
||||||
((result (thunk)))
|
|
||||||
(begin (cl-pop-handlers (len entries)) result))))))))
|
|
||||||
|
|
||||||
;; ── cl-restart-case ────────────────────────────────────────────────────────
|
|
||||||
;;
|
|
||||||
;; thunk: (fn () body)
|
|
||||||
;; restarts: list of (name params body-fn) triples
|
|
||||||
;; body-fn is (fn () val) or (fn (arg) val)
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-restart-case
|
|
||||||
(fn
|
|
||||||
(thunk &rest restarts)
|
|
||||||
(call/cc
|
|
||||||
(fn
|
|
||||||
(escape)
|
|
||||||
(let
|
|
||||||
((entries (map (fn (r) {:fn (first (rest (rest r))) :escape escape :name (first r)}) restarts)))
|
|
||||||
(begin
|
|
||||||
(cl-push-restarts entries)
|
|
||||||
(let
|
|
||||||
((result (thunk)))
|
|
||||||
(begin (cl-pop-restarts (len entries)) result))))))))
|
|
||||||
|
|
||||||
;; ── cl-with-simple-restart ─────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-with-simple-restart
|
|
||||||
(fn
|
|
||||||
(name description thunk)
|
|
||||||
(cl-restart-case thunk (list name (list) (fn () nil)))))
|
|
||||||
|
|
||||||
;; ── find-restart / invoke-restart / compute-restarts ──────────────────────
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-find-restart-entry
|
|
||||||
(fn
|
|
||||||
(name stack)
|
|
||||||
(if
|
|
||||||
(empty? stack)
|
|
||||||
nil
|
|
||||||
(let
|
|
||||||
((entry (first stack)))
|
|
||||||
(if
|
|
||||||
(= (get entry "name") name)
|
|
||||||
entry
|
|
||||||
(cl-find-restart-entry name (rest stack)))))))
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-find-restart
|
|
||||||
(fn (name) (cl-find-restart-entry name cl-restart-stack)))
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-invoke-restart
|
|
||||||
(fn
|
|
||||||
(name &rest args)
|
|
||||||
(let
|
|
||||||
((entry (cl-find-restart-entry name cl-restart-stack)))
|
|
||||||
(if
|
|
||||||
(nil? entry)
|
|
||||||
(error (str "No active restart: " name))
|
|
||||||
(let
|
|
||||||
((restart-fn (get entry "fn")) (escape (get entry "escape")))
|
|
||||||
(escape
|
|
||||||
(if (empty? args) (restart-fn) (restart-fn (first args)))))))))
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-compute-restarts
|
|
||||||
(fn () (map (fn (e) (get e "name")) cl-restart-stack)))
|
|
||||||
|
|
||||||
;; ── with-condition-restarts (stub — association is advisory) ──────────────
|
|
||||||
|
|
||||||
(define cl-with-condition-restarts (fn (c restarts thunk) (thunk)))
|
|
||||||
|
|
||||||
;; ── cl-cerror ──────────────────────────────────────────────────────────────
|
|
||||||
;;
|
|
||||||
;; Signals a continuable error. The "continue" restart is established;
|
|
||||||
;; invoke-restart "continue" to proceed past the error.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
;; ── cl-cerror ──────────────────────────────────────────────────────────────
|
|
||||||
;;
|
|
||||||
;; Signals a continuable error. The "continue" restart is established;
|
|
||||||
;; invoke-restart "continue" to proceed past the error.
|
|
||||||
|
|
||||||
(define cl-cerror
|
|
||||||
(fn (continue-string c &rest args)
|
|
||||||
(let ((obj (if (cl-condition? c)
|
|
||||||
c
|
|
||||||
(cl-make-condition "simple-error"
|
|
||||||
"format-control" (str c)
|
|
||||||
"format-arguments" args))))
|
|
||||||
(cl-restart-case
|
|
||||||
(fn () (cl-signal-obj obj cl-handler-stack))
|
|
||||||
(list "continue" (list) (fn () nil))))))
|
|
||||||
@@ -292,80 +292,6 @@ check 113 "cl-format-decimal 42" '"42"'
|
|||||||
check 114 "n->s base 16" '"1f"'
|
check 114 "n->s base 16" '"1f"'
|
||||||
check 115 "s->n base 16" "31"
|
check 115 "s->n base 16" "31"
|
||||||
|
|
||||||
# ── Phase 2: condition system unit tests ─────────────────────────────────────
|
|
||||||
# Load runtime.sx then conditions.sx; query the passed/failed/failures globals.
|
|
||||||
UNIT_FILE=$(mktemp); trap "rm -f $UNIT_FILE" EXIT
|
|
||||||
cat > "$UNIT_FILE" << 'UNIT'
|
|
||||||
(epoch 1)
|
|
||||||
(load "spec/stdlib.sx")
|
|
||||||
(epoch 2)
|
|
||||||
(load "lib/common-lisp/runtime.sx")
|
|
||||||
(epoch 3)
|
|
||||||
(load "lib/common-lisp/tests/conditions.sx")
|
|
||||||
(epoch 4)
|
|
||||||
(eval "passed")
|
|
||||||
(epoch 5)
|
|
||||||
(eval "failed")
|
|
||||||
(epoch 6)
|
|
||||||
(eval "failures")
|
|
||||||
UNIT
|
|
||||||
|
|
||||||
UNIT_OUT=$(timeout 30 "$SX_SERVER" < "$UNIT_FILE" 2>/dev/null)
|
|
||||||
|
|
||||||
# extract passed/failed counts from ok-len lines
|
|
||||||
UNIT_PASSED=$(echo "$UNIT_OUT" | grep -A1 "^(ok-len 4 " | tail -1 || true)
|
|
||||||
UNIT_FAILED=$(echo "$UNIT_OUT" | grep -A1 "^(ok-len 5 " | tail -1 || true)
|
|
||||||
UNIT_ERRS=$(echo "$UNIT_OUT" | grep -A1 "^(ok-len 6 " | tail -1 || true)
|
|
||||||
# fallback: try plain ok lines
|
|
||||||
[ -z "$UNIT_PASSED" ] && UNIT_PASSED=$(echo "$UNIT_OUT" | grep "^(ok 4 " | awk '{print $3}' | tr -d ')' || true)
|
|
||||||
[ -z "$UNIT_FAILED" ] && UNIT_FAILED=$(echo "$UNIT_OUT" | grep "^(ok 5 " | awk '{print $3}' | tr -d ')' || true)
|
|
||||||
[ -z "$UNIT_PASSED" ] && UNIT_PASSED=0
|
|
||||||
[ -z "$UNIT_FAILED" ] && UNIT_FAILED=0
|
|
||||||
|
|
||||||
if [ "$UNIT_FAILED" = "0" ] && [ "$UNIT_PASSED" -gt 0 ] 2>/dev/null; then
|
|
||||||
PASS=$((PASS + UNIT_PASSED))
|
|
||||||
[ "$VERBOSE" = "-v" ] && echo " ok condition tests ($UNIT_PASSED)"
|
|
||||||
else
|
|
||||||
FAIL=$((FAIL + 1))
|
|
||||||
ERRORS+=" FAIL [condition tests] (${UNIT_PASSED} passed, ${UNIT_FAILED} failed) ${UNIT_ERRS}
|
|
||||||
"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# ── Phase 3: classic program tests ───────────────────────────────────────────
|
|
||||||
run_program_suite() {
|
|
||||||
local prog="$1" pass_var="$2" fail_var="$3" failures_var="$4"
|
|
||||||
local PROG_FILE=$(mktemp)
|
|
||||||
printf '(epoch 1)\n(load "spec/stdlib.sx")\n(epoch 2)\n(load "lib/common-lisp/runtime.sx")\n(epoch 3)\n(load "%s")\n(epoch 4)\n(eval "%s")\n(epoch 5)\n(eval "%s")\n(epoch 6)\n(eval "%s")\n' \
|
|
||||||
"$prog" "$pass_var" "$fail_var" "$failures_var" > "$PROG_FILE"
|
|
||||||
local OUT; OUT=$(timeout 20 "$SX_SERVER" < "$PROG_FILE" 2>/dev/null)
|
|
||||||
rm -f "$PROG_FILE"
|
|
||||||
local P F
|
|
||||||
P=$(echo "$OUT" | grep -A1 "^(ok-len 4 " | tail -1 || true)
|
|
||||||
F=$(echo "$OUT" | grep -A1 "^(ok-len 5 " | tail -1 || true)
|
|
||||||
local ERRS; ERRS=$(echo "$OUT" | grep -A1 "^(ok-len 6 " | tail -1 || true)
|
|
||||||
[ -z "$P" ] && P=0; [ -z "$F" ] && F=0
|
|
||||||
if [ "$F" = "0" ] && [ "$P" -gt 0 ] 2>/dev/null; then
|
|
||||||
PASS=$((PASS + P))
|
|
||||||
[ "$VERBOSE" = "-v" ] && echo " ok $prog ($P)"
|
|
||||||
else
|
|
||||||
FAIL=$((FAIL + 1))
|
|
||||||
ERRORS+=" FAIL [$prog] (${P} passed, ${F} failed) ${ERRS}
|
|
||||||
"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
run_program_suite \
|
|
||||||
"lib/common-lisp/tests/programs/restart-demo.sx" \
|
|
||||||
"demo-passed" "demo-failed" "demo-failures"
|
|
||||||
|
|
||||||
run_program_suite \
|
|
||||||
"lib/common-lisp/tests/programs/parse-recover.sx" \
|
|
||||||
"parse-passed" "parse-failed" "parse-failures"
|
|
||||||
|
|
||||||
run_program_suite \
|
|
||||||
"lib/common-lisp/tests/programs/interactive-debugger.sx" \
|
|
||||||
"debugger-passed" "debugger-failed" "debugger-failures"
|
|
||||||
|
|
||||||
TOTAL=$((PASS+FAIL))
|
TOTAL=$((PASS+FAIL))
|
||||||
if [ $FAIL -eq 0 ]; then
|
if [ $FAIL -eq 0 ]; then
|
||||||
echo "ok $PASS/$TOTAL lib/common-lisp tests passed"
|
echo "ok $PASS/$TOTAL lib/common-lisp tests passed"
|
||||||
|
|||||||
@@ -1,478 +0,0 @@
|
|||||||
;; lib/common-lisp/tests/conditions.sx — Phase 3 condition system tests
|
|
||||||
;;
|
|
||||||
;; Loaded by lib/common-lisp/test.sh after:
|
|
||||||
;; (load "spec/stdlib.sx")
|
|
||||||
;; (load "lib/common-lisp/runtime.sx")
|
|
||||||
;;
|
|
||||||
;; Each test resets the handler/restart stacks to ensure isolation.
|
|
||||||
|
|
||||||
(define
|
|
||||||
reset-stacks!
|
|
||||||
(fn () (set! cl-handler-stack (list)) (set! cl-restart-stack (list))))
|
|
||||||
|
|
||||||
;; ── helpers ────────────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(define passed 0)
|
|
||||||
(define failed 0)
|
|
||||||
(define failures (list))
|
|
||||||
|
|
||||||
(define
|
|
||||||
assert-equal
|
|
||||||
(fn
|
|
||||||
(label got expected)
|
|
||||||
(if
|
|
||||||
(= got expected)
|
|
||||||
(set! passed (+ passed 1))
|
|
||||||
(begin
|
|
||||||
(set! failed (+ failed 1))
|
|
||||||
(set!
|
|
||||||
failures
|
|
||||||
(append
|
|
||||||
failures
|
|
||||||
(list
|
|
||||||
(str
|
|
||||||
"FAIL ["
|
|
||||||
label
|
|
||||||
"]: got="
|
|
||||||
(inspect got)
|
|
||||||
" expected="
|
|
||||||
(inspect expected)))))))))
|
|
||||||
|
|
||||||
(define
|
|
||||||
assert-true
|
|
||||||
(fn
|
|
||||||
(label got)
|
|
||||||
(if
|
|
||||||
got
|
|
||||||
(set! passed (+ passed 1))
|
|
||||||
(begin
|
|
||||||
(set! failed (+ failed 1))
|
|
||||||
(set!
|
|
||||||
failures
|
|
||||||
(append
|
|
||||||
failures
|
|
||||||
(list
|
|
||||||
(str "FAIL [" label "]: expected true, got " (inspect got)))))))))
|
|
||||||
|
|
||||||
(define
|
|
||||||
assert-nil
|
|
||||||
(fn
|
|
||||||
(label got)
|
|
||||||
(if
|
|
||||||
(nil? got)
|
|
||||||
(set! passed (+ passed 1))
|
|
||||||
(begin
|
|
||||||
(set! failed (+ failed 1))
|
|
||||||
(set!
|
|
||||||
failures
|
|
||||||
(append
|
|
||||||
failures
|
|
||||||
(list (str "FAIL [" label "]: expected nil, got " (inspect got)))))))))
|
|
||||||
|
|
||||||
;; ── 1. condition predicates ────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(reset-stacks!)
|
|
||||||
|
|
||||||
(let
|
|
||||||
((c (cl-make-condition "simple-error" "format-control" "oops")))
|
|
||||||
(begin
|
|
||||||
(assert-true "cl-condition? on condition" (cl-condition? c))
|
|
||||||
(assert-equal "cl-condition? on string" (cl-condition? "hello") false)
|
|
||||||
(assert-equal "cl-condition? on number" (cl-condition? 42) false)
|
|
||||||
(assert-equal "cl-condition? on nil" (cl-condition? nil) false)))
|
|
||||||
|
|
||||||
;; ── 2. cl-make-condition + slot access ────────────────────────────────────
|
|
||||||
|
|
||||||
(reset-stacks!)
|
|
||||||
|
|
||||||
(let
|
|
||||||
((c (cl-make-condition "simple-error" "format-control" "msg" "format-arguments" (list 1 2))))
|
|
||||||
(begin
|
|
||||||
(assert-equal "class field" (get c "class") "simple-error")
|
|
||||||
(assert-equal "cl-type field" (get c "cl-type") "cl-condition")
|
|
||||||
(assert-equal
|
|
||||||
"format-control slot"
|
|
||||||
(cl-condition-slot c "format-control")
|
|
||||||
"msg")
|
|
||||||
(assert-equal
|
|
||||||
"format-arguments slot"
|
|
||||||
(cl-condition-slot c "format-arguments")
|
|
||||||
(list 1 2))
|
|
||||||
(assert-nil "missing slot is nil" (cl-condition-slot c "no-such-slot"))
|
|
||||||
(assert-equal "condition-message" (cl-condition-message c) "msg")))
|
|
||||||
|
|
||||||
;; ── 3. cl-condition-of-type? — hierarchy walking ─────────────────────────
|
|
||||||
|
|
||||||
(reset-stacks!)
|
|
||||||
|
|
||||||
(let
|
|
||||||
((se (cl-make-condition "simple-error" "format-control" "x"))
|
|
||||||
(w (cl-make-condition "simple-warning" "format-control" "y"))
|
|
||||||
(te
|
|
||||||
(cl-make-condition
|
|
||||||
"type-error"
|
|
||||||
"datum"
|
|
||||||
5
|
|
||||||
"expected-type"
|
|
||||||
"string"))
|
|
||||||
(dz (cl-make-condition "division-by-zero")))
|
|
||||||
(begin
|
|
||||||
(assert-true
|
|
||||||
"se isa simple-error"
|
|
||||||
(cl-condition-of-type? se "simple-error"))
|
|
||||||
(assert-true "se isa error" (cl-condition-of-type? se "error"))
|
|
||||||
(assert-true
|
|
||||||
"se isa serious-condition"
|
|
||||||
(cl-condition-of-type? se "serious-condition"))
|
|
||||||
(assert-true "se isa condition" (cl-condition-of-type? se "condition"))
|
|
||||||
(assert-equal
|
|
||||||
"se not isa warning"
|
|
||||||
(cl-condition-of-type? se "warning")
|
|
||||||
false)
|
|
||||||
(assert-true
|
|
||||||
"w isa simple-warning"
|
|
||||||
(cl-condition-of-type? w "simple-warning"))
|
|
||||||
(assert-true "w isa warning" (cl-condition-of-type? w "warning"))
|
|
||||||
(assert-true "w isa condition" (cl-condition-of-type? w "condition"))
|
|
||||||
(assert-equal "w not isa error" (cl-condition-of-type? w "error") false)
|
|
||||||
(assert-true "te isa type-error" (cl-condition-of-type? te "type-error"))
|
|
||||||
(assert-true "te isa error" (cl-condition-of-type? te "error"))
|
|
||||||
(assert-true
|
|
||||||
"dz isa division-by-zero"
|
|
||||||
(cl-condition-of-type? dz "division-by-zero"))
|
|
||||||
(assert-true
|
|
||||||
"dz isa arithmetic-error"
|
|
||||||
(cl-condition-of-type? dz "arithmetic-error"))
|
|
||||||
(assert-true "dz isa error" (cl-condition-of-type? dz "error"))
|
|
||||||
(assert-equal
|
|
||||||
"non-condition not isa anything"
|
|
||||||
(cl-condition-of-type? 42 "error")
|
|
||||||
false)))
|
|
||||||
|
|
||||||
;; ── 4. cl-define-condition ────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(reset-stacks!)
|
|
||||||
|
|
||||||
(begin
|
|
||||||
(cl-define-condition "my-app-error" (list "error") (list "code" "detail"))
|
|
||||||
(let
|
|
||||||
((c (cl-make-condition "my-app-error" "code" 404 "detail" "not found")))
|
|
||||||
(begin
|
|
||||||
(assert-true "user condition: cl-condition?" (cl-condition? c))
|
|
||||||
(assert-true
|
|
||||||
"user condition isa my-app-error"
|
|
||||||
(cl-condition-of-type? c "my-app-error"))
|
|
||||||
(assert-true
|
|
||||||
"user condition isa error"
|
|
||||||
(cl-condition-of-type? c "error"))
|
|
||||||
(assert-true
|
|
||||||
"user condition isa condition"
|
|
||||||
(cl-condition-of-type? c "condition"))
|
|
||||||
(assert-equal
|
|
||||||
"user condition slot code"
|
|
||||||
(cl-condition-slot c "code")
|
|
||||||
404)
|
|
||||||
(assert-equal
|
|
||||||
"user condition slot detail"
|
|
||||||
(cl-condition-slot c "detail")
|
|
||||||
"not found"))))
|
|
||||||
|
|
||||||
;; ── 5. cl-handler-bind (non-unwinding) ───────────────────────────────────
|
|
||||||
|
|
||||||
(reset-stacks!)
|
|
||||||
|
|
||||||
(let
|
|
||||||
((log (list)))
|
|
||||||
(begin
|
|
||||||
(cl-handler-bind
|
|
||||||
(list
|
|
||||||
(list
|
|
||||||
"error"
|
|
||||||
(fn (c) (set! log (append log (list (cl-condition-message c)))))))
|
|
||||||
(fn
|
|
||||||
()
|
|
||||||
(cl-signal (cl-make-condition "simple-error" "format-control" "oops"))))
|
|
||||||
(assert-equal "handler-bind: handler fired" log (list "oops"))))
|
|
||||||
|
|
||||||
(reset-stacks!)
|
|
||||||
|
|
||||||
;; Non-unwinding: body continues after signal
|
|
||||||
(let
|
|
||||||
((body-ran false))
|
|
||||||
(begin
|
|
||||||
(cl-handler-bind
|
|
||||||
(list (list "error" (fn (c) nil)))
|
|
||||||
(fn
|
|
||||||
()
|
|
||||||
(cl-signal (cl-make-condition "simple-error" "format-control" "x"))
|
|
||||||
(set! body-ran true)))
|
|
||||||
(assert-true "handler-bind: body continues after signal" body-ran)))
|
|
||||||
|
|
||||||
(reset-stacks!)
|
|
||||||
|
|
||||||
;; Type filtering: warning handler does not fire for error
|
|
||||||
(let
|
|
||||||
((w-fired false))
|
|
||||||
(begin
|
|
||||||
(cl-handler-bind
|
|
||||||
(list (list "warning" (fn (c) (set! w-fired true))))
|
|
||||||
(fn
|
|
||||||
()
|
|
||||||
(cl-signal (cl-make-condition "simple-error" "format-control" "e"))))
|
|
||||||
(assert-equal
|
|
||||||
"handler-bind: type filter (warning ignores error)"
|
|
||||||
w-fired
|
|
||||||
false)))
|
|
||||||
|
|
||||||
(reset-stacks!)
|
|
||||||
|
|
||||||
;; Multiple handlers: both matching handlers fire
|
|
||||||
(let
|
|
||||||
((log (list)))
|
|
||||||
(begin
|
|
||||||
(cl-handler-bind
|
|
||||||
(list
|
|
||||||
(list "error" (fn (c) (set! log (append log (list "e1")))))
|
|
||||||
(list "condition" (fn (c) (set! log (append log (list "e2"))))))
|
|
||||||
(fn
|
|
||||||
()
|
|
||||||
(cl-signal (cl-make-condition "simple-error" "format-control" "x"))))
|
|
||||||
(assert-equal "handler-bind: both handlers fire" log (list "e1" "e2"))))
|
|
||||||
|
|
||||||
(reset-stacks!)
|
|
||||||
|
|
||||||
;; ── 6. cl-handler-case (unwinding) ───────────────────────────────────────
|
|
||||||
|
|
||||||
;; Catches error, returns handler result
|
|
||||||
(let
|
|
||||||
((result (cl-handler-case (fn () (cl-error "boom") 99) (list "error" (fn (c) (str "caught: " (cl-condition-message c)))))))
|
|
||||||
(assert-equal "handler-case: catches error" result "caught: boom"))
|
|
||||||
|
|
||||||
(reset-stacks!)
|
|
||||||
|
|
||||||
;; Returns body result when no signal
|
|
||||||
(let
|
|
||||||
((result (cl-handler-case (fn () 42) (list "error" (fn (c) -1)))))
|
|
||||||
(assert-equal "handler-case: body result" result 42))
|
|
||||||
|
|
||||||
(reset-stacks!)
|
|
||||||
|
|
||||||
;; Only first matching handler runs (unwinding)
|
|
||||||
(let
|
|
||||||
((result (cl-handler-case (fn () (cl-error "x")) (list "simple-error" (fn (c) "simple")) (list "error" (fn (c) "error")))))
|
|
||||||
(assert-equal "handler-case: most specific wins" result "simple"))
|
|
||||||
|
|
||||||
(reset-stacks!)
|
|
||||||
|
|
||||||
;; ── 7. cl-warn ────────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(let
|
|
||||||
((warned false))
|
|
||||||
(begin
|
|
||||||
(cl-handler-bind
|
|
||||||
(list (list "warning" (fn (c) (set! warned true))))
|
|
||||||
(fn () (cl-warn "be careful")))
|
|
||||||
(assert-true "cl-warn: fires warning handler" warned)))
|
|
||||||
|
|
||||||
(reset-stacks!)
|
|
||||||
|
|
||||||
;; Warn with condition object
|
|
||||||
(let
|
|
||||||
((msg ""))
|
|
||||||
(begin
|
|
||||||
(cl-handler-bind
|
|
||||||
(list (list "warning" (fn (c) (set! msg (cl-condition-message c)))))
|
|
||||||
(fn
|
|
||||||
()
|
|
||||||
(cl-warn
|
|
||||||
(cl-make-condition "simple-warning" "format-control" "take care"))))
|
|
||||||
(assert-equal "cl-warn: condition object" msg "take care")))
|
|
||||||
|
|
||||||
(reset-stacks!)
|
|
||||||
|
|
||||||
;; ── 8. cl-restart-case + cl-invoke-restart ───────────────────────────────
|
|
||||||
|
|
||||||
;; Basic restart invocation
|
|
||||||
(let
|
|
||||||
((result (cl-restart-case (fn () (cl-invoke-restart "use-zero")) (list "use-zero" (list) (fn () 0)))))
|
|
||||||
(assert-equal "restart-case: invoke-restart use-zero" result 0))
|
|
||||||
|
|
||||||
(reset-stacks!)
|
|
||||||
|
|
||||||
;; Restart with argument
|
|
||||||
(let
|
|
||||||
((result (cl-restart-case (fn () (cl-invoke-restart "use-value" 77)) (list "use-value" (list "v") (fn (v) v)))))
|
|
||||||
(assert-equal "restart-case: invoke-restart with arg" result 77))
|
|
||||||
|
|
||||||
(reset-stacks!)
|
|
||||||
|
|
||||||
;; Body returns normally when restart not invoked
|
|
||||||
(let
|
|
||||||
((result (cl-restart-case (fn () 42) (list "never-used" (list) (fn () -1)))))
|
|
||||||
(assert-equal "restart-case: body result" result 42))
|
|
||||||
|
|
||||||
(reset-stacks!)
|
|
||||||
|
|
||||||
;; ── 9. cl-with-simple-restart ─────────────────────────────────────────────
|
|
||||||
|
|
||||||
(let
|
|
||||||
((result (cl-with-simple-restart "skip" "Skip this step" (fn () (cl-invoke-restart "skip") 99))))
|
|
||||||
(assert-nil "with-simple-restart: invoke returns nil" result))
|
|
||||||
|
|
||||||
(reset-stacks!)
|
|
||||||
|
|
||||||
;; ── 10. cl-find-restart ───────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(let
|
|
||||||
((found (cl-restart-case (fn () (cl-find-restart "retry")) (list "retry" (list) (fn () nil)))))
|
|
||||||
(assert-true "find-restart: finds active restart" (not (nil? found))))
|
|
||||||
|
|
||||||
(reset-stacks!)
|
|
||||||
|
|
||||||
(let
|
|
||||||
((not-found (cl-restart-case (fn () (cl-find-restart "nonexistent")) (list "retry" (list) (fn () nil)))))
|
|
||||||
(assert-nil "find-restart: nil for inactive restart" not-found))
|
|
||||||
|
|
||||||
(reset-stacks!)
|
|
||||||
|
|
||||||
;; ── 11. cl-compute-restarts ───────────────────────────────────────────────
|
|
||||||
|
|
||||||
(let
|
|
||||||
((names (cl-restart-case (fn () (cl-restart-case (fn () (cl-compute-restarts)) (list "inner" (list) (fn () nil)))) (list "outer" (list) (fn () nil)))))
|
|
||||||
(assert-equal
|
|
||||||
"compute-restarts: both restarts"
|
|
||||||
names
|
|
||||||
(list "inner" "outer")))
|
|
||||||
|
|
||||||
(reset-stacks!)
|
|
||||||
|
|
||||||
;; ── 12. handler-bind + restart-case interop ───────────────────────────────
|
|
||||||
|
|
||||||
;; Classic CL pattern: error handler invokes a restart
|
|
||||||
(let
|
|
||||||
((result (cl-restart-case (fn () (cl-handler-bind (list (list "error" (fn (c) (cl-invoke-restart "use-zero")))) (fn () (cl-error "divide by zero")))) (list "use-zero" (list) (fn () 0)))))
|
|
||||||
(assert-equal "interop: handler invokes restart" result 0))
|
|
||||||
|
|
||||||
(reset-stacks!)
|
|
||||||
|
|
||||||
;; ── 13. cl-cerror ─────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
;; When "continue" restart is invoked, cerror returns nil
|
|
||||||
(let
|
|
||||||
((result (cl-restart-case (fn () (cl-cerror "continue anyway" "something bad") 42) (list "continue" (list) (fn () "resumed")))))
|
|
||||||
(assert-true
|
|
||||||
"cerror: returns"
|
|
||||||
(or (nil? result) (= result 42) (= result "resumed"))))
|
|
||||||
|
|
||||||
(reset-stacks!)
|
|
||||||
|
|
||||||
;; ── 14. slot accessor helpers ─────────────────────────────────────────────
|
|
||||||
|
|
||||||
(let
|
|
||||||
((c (cl-make-condition "simple-error" "format-control" "msg" "format-arguments" (list 1 2))))
|
|
||||||
(begin
|
|
||||||
(assert-equal
|
|
||||||
"simple-condition-format-control"
|
|
||||||
(cl-simple-condition-format-control c)
|
|
||||||
"msg")
|
|
||||||
(assert-equal
|
|
||||||
"simple-condition-format-arguments"
|
|
||||||
(cl-simple-condition-format-arguments c)
|
|
||||||
(list 1 2))))
|
|
||||||
|
|
||||||
(let
|
|
||||||
((c (cl-make-condition "type-error" "datum" 42 "expected-type" "string")))
|
|
||||||
(begin
|
|
||||||
(assert-equal "type-error-datum" (cl-type-error-datum c) 42)
|
|
||||||
(assert-equal
|
|
||||||
"type-error-expected-type"
|
|
||||||
(cl-type-error-expected-type c)
|
|
||||||
"string")))
|
|
||||||
|
|
||||||
(let
|
|
||||||
((c (cl-make-condition "arithmetic-error" "operation" "/" "operands" (list 1 0))))
|
|
||||||
(begin
|
|
||||||
(assert-equal
|
|
||||||
"arithmetic-error-operation"
|
|
||||||
(cl-arithmetic-error-operation c)
|
|
||||||
"/")
|
|
||||||
(assert-equal
|
|
||||||
"arithmetic-error-operands"
|
|
||||||
(cl-arithmetic-error-operands c)
|
|
||||||
(list 1 0))))
|
|
||||||
|
|
||||||
|
|
||||||
;; ── 15. *debugger-hook* ───────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(reset-stacks!)
|
|
||||||
|
|
||||||
(let ((received nil))
|
|
||||||
(begin
|
|
||||||
(set! cl-debugger-hook
|
|
||||||
(fn (c h)
|
|
||||||
(set! received (cl-condition-message c))
|
|
||||||
(cl-invoke-restart "escape")))
|
|
||||||
(cl-restart-case
|
|
||||||
(fn () (cl-error "debugger test"))
|
|
||||||
(list "escape" (list) (fn () nil)))
|
|
||||||
(set! cl-debugger-hook nil)
|
|
||||||
(assert-equal "debugger-hook receives condition" received "debugger test")))
|
|
||||||
|
|
||||||
(reset-stacks!)
|
|
||||||
|
|
||||||
;; ── 16. *break-on-signals* ────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(reset-stacks!)
|
|
||||||
|
|
||||||
(let ((triggered false))
|
|
||||||
(begin
|
|
||||||
(set! cl-break-on-signals "error")
|
|
||||||
(set! cl-debugger-hook
|
|
||||||
(fn (c h)
|
|
||||||
(set! triggered true)
|
|
||||||
(cl-invoke-restart "abort")))
|
|
||||||
(cl-restart-case
|
|
||||||
(fn ()
|
|
||||||
(cl-signal (cl-make-condition "simple-error" "format-control" "x")))
|
|
||||||
(list "abort" (list) (fn () nil)))
|
|
||||||
(set! cl-break-on-signals nil)
|
|
||||||
(set! cl-debugger-hook nil)
|
|
||||||
(assert-true "break-on-signals fires hook" triggered)))
|
|
||||||
|
|
||||||
(reset-stacks!)
|
|
||||||
|
|
||||||
;; break-on-signals: non-matching type does NOT fire hook
|
|
||||||
(let ((triggered false))
|
|
||||||
(begin
|
|
||||||
(set! cl-break-on-signals "error")
|
|
||||||
(set! cl-debugger-hook
|
|
||||||
(fn (c h) (set! triggered true) nil))
|
|
||||||
(cl-handler-bind
|
|
||||||
(list (list "warning" (fn (c) nil)))
|
|
||||||
(fn ()
|
|
||||||
(cl-signal (cl-make-condition "simple-warning" "format-control" "w"))))
|
|
||||||
(set! cl-break-on-signals nil)
|
|
||||||
(set! cl-debugger-hook nil)
|
|
||||||
(assert-equal "break-on-signals: type mismatch not triggered" triggered false)))
|
|
||||||
|
|
||||||
(reset-stacks!)
|
|
||||||
|
|
||||||
;; ── 17. cl-invoke-restart-interactively ──────────────────────────────────
|
|
||||||
|
|
||||||
(let ((result
|
|
||||||
(cl-restart-case
|
|
||||||
(fn () (cl-invoke-restart-interactively "use-default"))
|
|
||||||
(list "use-default" (list) (fn () 99)))))
|
|
||||||
(assert-equal "invoke-restart-interactively: returns restart value" result 99))
|
|
||||||
|
|
||||||
(reset-stacks!)
|
|
||||||
|
|
||||||
;; ── summary ────────────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(if
|
|
||||||
(= failed 0)
|
|
||||||
(print (str "ok " passed "/" (+ passed failed) " condition tests passed"))
|
|
||||||
(begin
|
|
||||||
(for-each (fn (f) (print f)) failures)
|
|
||||||
(print
|
|
||||||
(str "FAIL " passed "/" (+ passed failed) " passed, " failed " failed"))))
|
|
||||||
@@ -1,438 +0,0 @@
|
|||||||
;; CL evaluator tests
|
|
||||||
|
|
||||||
(define cl-test-pass 0)
|
|
||||||
(define cl-test-fail 0)
|
|
||||||
(define cl-test-fails (list))
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-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) (cl-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
|
|
||||||
chk
|
|
||||||
(fn
|
|
||||||
()
|
|
||||||
(when
|
|
||||||
(and ok (< i (len a)))
|
|
||||||
(do
|
|
||||||
(when
|
|
||||||
(not (cl-deep= (nth a i) (nth b i)))
|
|
||||||
(set! ok false))
|
|
||||||
(set! i (+ i 1))
|
|
||||||
(chk)))))
|
|
||||||
(chk)
|
|
||||||
ok)))
|
|
||||||
(:else false))))
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-test
|
|
||||||
(fn
|
|
||||||
(name actual expected)
|
|
||||||
(if
|
|
||||||
(cl-deep= actual expected)
|
|
||||||
(set! cl-test-pass (+ cl-test-pass 1))
|
|
||||||
(do
|
|
||||||
(set! cl-test-fail (+ cl-test-fail 1))
|
|
||||||
(append! cl-test-fails {:name name :expected expected :actual actual})))))
|
|
||||||
|
|
||||||
;; Convenience: evaluate CL string with fresh env each time
|
|
||||||
(define ev (fn (src) (cl-eval-str src (cl-make-env))))
|
|
||||||
(define evall (fn (src) (cl-eval-all-str src (cl-make-env))))
|
|
||||||
|
|
||||||
;; ── self-evaluating literals ──────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "lit: nil" (ev "nil") nil)
|
|
||||||
(cl-test "lit: t" (ev "t") true)
|
|
||||||
(cl-test "lit: integer" (ev "42") 42)
|
|
||||||
(cl-test "lit: negative" (ev "-7") -7)
|
|
||||||
(cl-test "lit: zero" (ev "0") 0)
|
|
||||||
(cl-test "lit: string" (ev "\"hello\"") "hello")
|
|
||||||
(cl-test "lit: empty string" (ev "\"\"") "")
|
|
||||||
(cl-test "lit: keyword type" (get (ev ":foo") "cl-type") "keyword")
|
|
||||||
(cl-test "lit: keyword name" (get (ev ":foo") "name") "FOO")
|
|
||||||
(cl-test "lit: float type" (get (ev "3.14") "cl-type") "float")
|
|
||||||
|
|
||||||
;; ── QUOTE ─────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "quote: symbol" (ev "'x") "X")
|
|
||||||
(cl-test "quote: list" (ev "'(a b c)") (list "A" "B" "C"))
|
|
||||||
(cl-test "quote: nil" (ev "'nil") nil)
|
|
||||||
(cl-test "quote: integer" (ev "'42") 42)
|
|
||||||
(cl-test "quote: nested" (ev "'(a (b c))") (list "A" (list "B" "C")))
|
|
||||||
|
|
||||||
;; ── IF ────────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "if: true branch" (ev "(if t 1 2)") 1)
|
|
||||||
(cl-test "if: false branch" (ev "(if nil 1 2)") 2)
|
|
||||||
(cl-test "if: no else nil" (ev "(if nil 99)") nil)
|
|
||||||
(cl-test "if: number truthy" (ev "(if 0 'yes 'no)") "YES")
|
|
||||||
(cl-test "if: empty string truthy" (ev "(if \"\" 'yes 'no)") "YES")
|
|
||||||
(cl-test "if: nested" (ev "(if t (if nil 1 2) 3)") 2)
|
|
||||||
|
|
||||||
;; ── PROGN ────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "progn: single" (ev "(progn 42)") 42)
|
|
||||||
(cl-test "progn: multiple" (ev "(progn 1 2 3)") 3)
|
|
||||||
(cl-test "progn: nil last" (ev "(progn 1 nil)") nil)
|
|
||||||
|
|
||||||
;; ── AND / OR ─────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "and: empty" (ev "(and)") true)
|
|
||||||
(cl-test "and: all true" (ev "(and 1 2 3)") 3)
|
|
||||||
(cl-test "and: short-circuit" (ev "(and nil 99)") nil)
|
|
||||||
(cl-test "and: returns last" (ev "(and 1 2)") 2)
|
|
||||||
(cl-test "or: empty" (ev "(or)") nil)
|
|
||||||
(cl-test "or: first truthy" (ev "(or 1 2)") 1)
|
|
||||||
(cl-test "or: all nil" (ev "(or nil nil)") nil)
|
|
||||||
(cl-test "or: short-circuit" (ev "(or nil 42)") 42)
|
|
||||||
|
|
||||||
;; ── COND ─────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "cond: first match" (ev "(cond (t 1) (t 2))") 1)
|
|
||||||
(cl-test "cond: second match" (ev "(cond (nil 1) (t 2))") 2)
|
|
||||||
(cl-test "cond: no match" (ev "(cond (nil 1) (nil 2))") nil)
|
|
||||||
(cl-test "cond: returns test value" (ev "(cond (42))") 42)
|
|
||||||
|
|
||||||
;; ── WHEN / UNLESS ─────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "when: true" (ev "(when t 1 2 3)") 3)
|
|
||||||
(cl-test "when: nil" (ev "(when nil 99)") nil)
|
|
||||||
(cl-test "unless: nil runs" (ev "(unless nil 42)") 42)
|
|
||||||
(cl-test "unless: true skips" (ev "(unless t 99)") nil)
|
|
||||||
|
|
||||||
;; ── LET ──────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "let: empty bindings" (ev "(let () 42)") 42)
|
|
||||||
(cl-test "let: single binding" (ev "(let ((x 5)) x)") 5)
|
|
||||||
(cl-test "let: two bindings" (ev "(let ((x 3) (y 4)) (+ x y))") 7)
|
|
||||||
(cl-test "let: parallel" (ev "(let ((x 1)) (let ((x 2) (y x)) y))") 1)
|
|
||||||
(cl-test "let: nested" (ev "(let ((x 1)) (let ((y 2)) (+ x y)))") 3)
|
|
||||||
(cl-test "let: progn body" (ev "(let ((x 5)) (+ x 1) (* x 2))") 10)
|
|
||||||
(cl-test "let: bare name nil" (ev "(let (x) x)") nil)
|
|
||||||
|
|
||||||
;; ── LET* ─────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "let*: sequential" (ev "(let* ((x 1) (y (+ x 1))) y)") 2)
|
|
||||||
(cl-test "let*: chain" (ev "(let* ((a 2) (b (* a 3)) (c (+ b 1))) c)") 7)
|
|
||||||
(cl-test "let*: shadow" (ev "(let ((x 1)) (let* ((x 2) (y x)) y))") 2)
|
|
||||||
|
|
||||||
;; ── SETQ / SETF ──────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "setq: basic" (ev "(let ((x 0)) (setq x 5) x)") 5)
|
|
||||||
(cl-test "setq: returns value" (ev "(let ((x 0)) (setq x 99))") 99)
|
|
||||||
(cl-test "setf: basic" (ev "(let ((x 0)) (setf x 7) x)") 7)
|
|
||||||
|
|
||||||
;; ── LAMBDA ────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "lambda: call" (ev "((lambda (x) x) 42)") 42)
|
|
||||||
(cl-test "lambda: multi-arg" (ev "((lambda (x y) (+ x y)) 3 4)") 7)
|
|
||||||
(cl-test "lambda: closure" (ev "(let ((n 10)) ((lambda (x) (+ x n)) 5))") 15)
|
|
||||||
(cl-test "lambda: rest arg"
|
|
||||||
(ev "((lambda (x &rest xs) (cons x xs)) 1 2 3)")
|
|
||||||
{:cl-type "cons" :car 1 :cdr (list 2 3)})
|
|
||||||
(cl-test "lambda: optional no default"
|
|
||||||
(ev "((lambda (&optional x) x))")
|
|
||||||
nil)
|
|
||||||
(cl-test "lambda: optional with arg"
|
|
||||||
(ev "((lambda (&optional (x 99)) x) 42)")
|
|
||||||
42)
|
|
||||||
(cl-test "lambda: optional default used"
|
|
||||||
(ev "((lambda (&optional (x 7)) x))")
|
|
||||||
7)
|
|
||||||
|
|
||||||
;; ── FUNCTION ─────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "function: lambda" (get (ev "(function (lambda (x) x))") "cl-type") "function")
|
|
||||||
|
|
||||||
;; ── DEFUN ────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "defun: returns name" (evall "(defun sq (x) (* x x))") "SQ")
|
|
||||||
(cl-test "defun: call" (evall "(defun sq (x) (* x x)) (sq 5)") 25)
|
|
||||||
(cl-test "defun: multi-arg" (evall "(defun add (x y) (+ x y)) (add 3 4)") 7)
|
|
||||||
(cl-test "defun: recursive factorial"
|
|
||||||
(evall "(defun fact (n) (if (<= n 1) 1 (* n (fact (- n 1))))) (fact 5)")
|
|
||||||
120)
|
|
||||||
(cl-test "defun: multiple calls"
|
|
||||||
(evall "(defun double (x) (* x 2)) (+ (double 3) (double 5))")
|
|
||||||
16)
|
|
||||||
|
|
||||||
;; ── FLET ─────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "flet: basic"
|
|
||||||
(ev "(flet ((double (x) (* x 2))) (double 5))")
|
|
||||||
10)
|
|
||||||
(cl-test "flet: sees outer vars"
|
|
||||||
(ev "(let ((n 3)) (flet ((add-n (x) (+ x n))) (add-n 7)))")
|
|
||||||
10)
|
|
||||||
(cl-test "flet: non-recursive"
|
|
||||||
(ev "(flet ((f (x) (+ x 1))) (flet ((f (x) (f (f x)))) (f 5)))")
|
|
||||||
7)
|
|
||||||
|
|
||||||
;; ── LABELS ────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "labels: basic"
|
|
||||||
(ev "(labels ((greet (x) x)) (greet 42))")
|
|
||||||
42)
|
|
||||||
(cl-test "labels: recursive"
|
|
||||||
(ev "(labels ((count (n) (if (<= n 0) 0 (+ 1 (count (- n 1)))))) (count 5))")
|
|
||||||
5)
|
|
||||||
(cl-test "labels: mutual recursion"
|
|
||||||
(ev "(labels
|
|
||||||
((even? (n) (if (= n 0) t (odd? (- n 1))))
|
|
||||||
(odd? (n) (if (= n 0) nil (even? (- n 1)))))
|
|
||||||
(list (even? 4) (odd? 3)))")
|
|
||||||
(list true true))
|
|
||||||
|
|
||||||
;; ── THE / LOCALLY / EVAL-WHEN ────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "the: passthrough" (ev "(the integer 42)") 42)
|
|
||||||
(cl-test "the: string" (ev "(the string \"hi\")") "hi")
|
|
||||||
(cl-test "locally: body" (ev "(locally 1 2 3)") 3)
|
|
||||||
(cl-test "eval-when: execute" (ev "(eval-when (:execute) 99)") 99)
|
|
||||||
(cl-test "eval-when: no execute" (ev "(eval-when (:compile-toplevel) 99)") nil)
|
|
||||||
|
|
||||||
;; ── DEFVAR / DEFPARAMETER ────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "defvar: returns name" (evall "(defvar *x* 10)") "*X*")
|
|
||||||
(cl-test "defparameter: sets value" (evall "(defparameter *y* 42) *y*") 42)
|
|
||||||
(cl-test "defvar: no reinit" (evall "(defvar *z* 1) (defvar *z* 99) *z*") 1)
|
|
||||||
|
|
||||||
;; ── built-in arithmetic ───────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "arith: +" (ev "(+ 1 2 3)") 6)
|
|
||||||
(cl-test "arith: + zero" (ev "(+)") 0)
|
|
||||||
(cl-test "arith: -" (ev "(- 10 3 2)") 5)
|
|
||||||
(cl-test "arith: - negate" (ev "(- 5)") -5)
|
|
||||||
(cl-test "arith: *" (ev "(* 2 3 4)") 24)
|
|
||||||
(cl-test "arith: * one" (ev "(*)") 1)
|
|
||||||
(cl-test "arith: /" (ev "(/ 12 3)") 4)
|
|
||||||
(cl-test "arith: max" (ev "(max 3 1 4 1 5)") 5)
|
|
||||||
(cl-test "arith: min" (ev "(min 3 1 4 1 5)") 1)
|
|
||||||
(cl-test "arith: abs neg" (ev "(abs -7)") 7)
|
|
||||||
(cl-test "arith: abs pos" (ev "(abs 7)") 7)
|
|
||||||
|
|
||||||
;; ── built-in comparisons ──────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "cmp: = true" (ev "(= 3 3)") true)
|
|
||||||
(cl-test "cmp: = false" (ev "(= 3 4)") nil)
|
|
||||||
(cl-test "cmp: /=" (ev "(/= 3 4)") true)
|
|
||||||
(cl-test "cmp: <" (ev "(< 1 2)") true)
|
|
||||||
(cl-test "cmp: > false" (ev "(> 1 2)") nil)
|
|
||||||
(cl-test "cmp: <=" (ev "(<= 2 2)") true)
|
|
||||||
|
|
||||||
;; ── built-in predicates ───────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "pred: null nil" (ev "(null nil)") true)
|
|
||||||
(cl-test "pred: null non-nil" (ev "(null 5)") nil)
|
|
||||||
(cl-test "pred: not nil" (ev "(not nil)") true)
|
|
||||||
(cl-test "pred: not truthy" (ev "(not 5)") nil)
|
|
||||||
(cl-test "pred: numberp" (ev "(numberp 5)") true)
|
|
||||||
(cl-test "pred: numberp str" (ev "(numberp \"x\")") nil)
|
|
||||||
(cl-test "pred: stringp" (ev "(stringp \"hello\")") true)
|
|
||||||
(cl-test "pred: listp list" (ev "(listp '(1))") true)
|
|
||||||
(cl-test "pred: listp nil" (ev "(listp nil)") true)
|
|
||||||
(cl-test "pred: zerop" (ev "(zerop 0)") true)
|
|
||||||
(cl-test "pred: plusp" (ev "(plusp 3)") true)
|
|
||||||
(cl-test "pred: evenp" (ev "(evenp 4)") true)
|
|
||||||
(cl-test "pred: oddp" (ev "(oddp 3)") true)
|
|
||||||
|
|
||||||
;; ── built-in list ops ─────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "list: car" (ev "(car '(1 2 3))") 1)
|
|
||||||
(cl-test "list: cdr" (ev "(cdr '(1 2 3))") (list 2 3))
|
|
||||||
(cl-test "list: cons" (get (ev "(cons 1 2)") "car") 1)
|
|
||||||
(cl-test "list: list fn" (ev "(list 1 2 3)") (list 1 2 3))
|
|
||||||
(cl-test "list: length" (ev "(length '(a b c))") 3)
|
|
||||||
(cl-test "list: length nil" (ev "(length nil)") 0)
|
|
||||||
(cl-test "list: append" (ev "(append '(1 2) '(3 4))") (list 1 2 3 4))
|
|
||||||
(cl-test "list: first" (ev "(first '(10 20 30))") 10)
|
|
||||||
(cl-test "list: second" (ev "(second '(10 20 30))") 20)
|
|
||||||
(cl-test "list: third" (ev "(third '(10 20 30))") 30)
|
|
||||||
(cl-test "list: rest" (ev "(rest '(1 2 3))") (list 2 3))
|
|
||||||
(cl-test "list: nth" (ev "(nth 1 '(a b c))") "B")
|
|
||||||
(cl-test "list: reverse" (ev "(reverse '(1 2 3))") (list 3 2 1))
|
|
||||||
|
|
||||||
;; ── FUNCALL / APPLY / MAPCAR ─────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "funcall: lambda"
|
|
||||||
(ev "(funcall (lambda (x) (* x x)) 5)")
|
|
||||||
25)
|
|
||||||
(cl-test "apply: basic"
|
|
||||||
(ev "(apply #'+ '(1 2 3))")
|
|
||||||
6)
|
|
||||||
(cl-test "apply: leading args"
|
|
||||||
(ev "(apply #'+ 1 2 '(3 4))")
|
|
||||||
10)
|
|
||||||
(cl-test "mapcar: basic"
|
|
||||||
(ev "(mapcar (lambda (x) (* x 2)) '(1 2 3))")
|
|
||||||
(list 2 4 6))
|
|
||||||
|
|
||||||
;; ── BLOCK / RETURN-FROM / RETURN ─────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "block: last form value"
|
|
||||||
(ev "(block done 1 2 3)")
|
|
||||||
3)
|
|
||||||
(cl-test "block: empty body"
|
|
||||||
(ev "(block done)")
|
|
||||||
nil)
|
|
||||||
(cl-test "block: single form"
|
|
||||||
(ev "(block foo 42)")
|
|
||||||
42)
|
|
||||||
(cl-test "block: return-from"
|
|
||||||
(ev "(block done 1 (return-from done 99) 2)")
|
|
||||||
99)
|
|
||||||
(cl-test "block: return-from nil block"
|
|
||||||
(ev "(block nil 1 (return-from nil 42) 3)")
|
|
||||||
42)
|
|
||||||
(cl-test "block: return-from no value"
|
|
||||||
(ev "(block done (return-from done))")
|
|
||||||
nil)
|
|
||||||
(cl-test "block: nested inner return stays inner"
|
|
||||||
(ev "(block outer (block inner (return-from inner 1) 2) 3)")
|
|
||||||
3)
|
|
||||||
(cl-test "block: nested outer return"
|
|
||||||
(ev "(block outer (block inner 1 2) (return-from outer 99) 3)")
|
|
||||||
99)
|
|
||||||
(cl-test "return: shorthand for nil block"
|
|
||||||
(ev "(block nil (return 77))")
|
|
||||||
77)
|
|
||||||
(cl-test "return: no value"
|
|
||||||
(ev "(block nil 1 (return) 2)")
|
|
||||||
nil)
|
|
||||||
(cl-test "block: return-from inside let"
|
|
||||||
(ev "(block done (let ((x 5)) (when (> x 3) (return-from done x))) 0)")
|
|
||||||
5)
|
|
||||||
(cl-test "block: return-from inside progn"
|
|
||||||
(ev "(block done (progn (return-from done 7) 99))")
|
|
||||||
7)
|
|
||||||
(cl-test "block: return-from through function"
|
|
||||||
(ev "(block done (flet ((f () (return-from done 42))) (f)) nil)")
|
|
||||||
42)
|
|
||||||
|
|
||||||
;; ── TAGBODY / GO ─────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "tagbody: empty returns nil"
|
|
||||||
(ev "(tagbody)")
|
|
||||||
nil)
|
|
||||||
(cl-test "tagbody: forms only, returns nil"
|
|
||||||
(ev "(let ((x 0)) (tagbody (setq x 1) (setq x 2)) x)")
|
|
||||||
2)
|
|
||||||
(cl-test "tagbody: tag only, returns nil"
|
|
||||||
(ev "(tagbody done)")
|
|
||||||
nil)
|
|
||||||
(cl-test "tagbody: go skips forms"
|
|
||||||
(ev "(let ((x 0)) (tagbody (go done) (setq x 99) done) x)")
|
|
||||||
0)
|
|
||||||
(cl-test "tagbody: go to later tag"
|
|
||||||
(ev "(let ((x 0)) (tagbody start (setq x (+ x 1)) (go done) (setq x 99) done) x)")
|
|
||||||
1)
|
|
||||||
(cl-test "tagbody: loop with counter"
|
|
||||||
(ev "(let ((n 0)) (tagbody loop (when (>= n 3) (go done)) (setq n (+ n 1)) (go loop) done) n)")
|
|
||||||
3)
|
|
||||||
(cl-test "tagbody: go inside when"
|
|
||||||
(ev "(let ((x 0)) (tagbody (setq x 1) (when t (go done)) (setq x 99) done) x)")
|
|
||||||
1)
|
|
||||||
(cl-test "tagbody: go inside progn"
|
|
||||||
(ev "(let ((x 0)) (tagbody (progn (setq x 1) (go done)) (setq x 99) done) x)")
|
|
||||||
1)
|
|
||||||
(cl-test "tagbody: go inside let"
|
|
||||||
(ev "(let ((acc 0)) (tagbody (let ((y 5)) (when (> y 3) (go done))) (setq acc 99) done) acc)")
|
|
||||||
0)
|
|
||||||
(cl-test "tagbody: integer tags"
|
|
||||||
(ev "(let ((x 0)) (tagbody (go 2) 1 (setq x 1) (go 3) 2 (setq x 2) (go 3) 3) x)")
|
|
||||||
2)
|
|
||||||
(cl-test "tagbody: block-return propagates out"
|
|
||||||
(ev "(block done (tagbody (return-from done 42)) nil)")
|
|
||||||
42)
|
|
||||||
|
|
||||||
;; ── UNWIND-PROTECT ───────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "unwind-protect: normal returns protected"
|
|
||||||
(ev "(unwind-protect 42 nil)")
|
|
||||||
42)
|
|
||||||
(cl-test "unwind-protect: cleanup runs"
|
|
||||||
(ev "(let ((x 0)) (unwind-protect 1 (setq x 99)) x)")
|
|
||||||
99)
|
|
||||||
(cl-test "unwind-protect: cleanup result ignored"
|
|
||||||
(ev "(unwind-protect 42 777)")
|
|
||||||
42)
|
|
||||||
(cl-test "unwind-protect: multiple cleanup forms"
|
|
||||||
(ev "(let ((x 0)) (unwind-protect 1 (setq x (+ x 1)) (setq x (+ x 1))) x)")
|
|
||||||
2)
|
|
||||||
(cl-test "unwind-protect: cleanup on return-from"
|
|
||||||
(ev "(let ((x 0)) (block done (unwind-protect (return-from done 7) (setq x 99))) x)")
|
|
||||||
99)
|
|
||||||
(cl-test "unwind-protect: return-from still propagates"
|
|
||||||
(ev "(block done (unwind-protect (return-from done 42) nil))")
|
|
||||||
42)
|
|
||||||
(cl-test "unwind-protect: cleanup on go"
|
|
||||||
(ev "(let ((x 0)) (tagbody (unwind-protect (go done) (setq x 1)) done) x)")
|
|
||||||
1)
|
|
||||||
(cl-test "unwind-protect: nested, inner cleanup first"
|
|
||||||
(ev "(let ((n 0)) (unwind-protect (unwind-protect 1 (setq n (+ n 10))) (setq n (+ n 1))) n)")
|
|
||||||
11)
|
|
||||||
|
|
||||||
;; ── VALUES / MULTIPLE-VALUE-BIND / NTH-VALUE ────────────────────
|
|
||||||
|
|
||||||
(cl-test "values: single returns plain"
|
|
||||||
(ev "(values 42)")
|
|
||||||
42)
|
|
||||||
(cl-test "values: zero returns nil"
|
|
||||||
(ev "(values)")
|
|
||||||
nil)
|
|
||||||
(cl-test "values: multi — primary via funcall"
|
|
||||||
(ev "(car (list (values 1 2)))")
|
|
||||||
1)
|
|
||||||
(cl-test "multiple-value-bind: basic"
|
|
||||||
(ev "(multiple-value-bind (a b) (values 1 2) (+ a b))")
|
|
||||||
3)
|
|
||||||
(cl-test "multiple-value-bind: extra vars get nil"
|
|
||||||
(ev "(multiple-value-bind (a b c) (values 10 20) (list a b c))")
|
|
||||||
(list 10 20 nil))
|
|
||||||
(cl-test "multiple-value-bind: extra values ignored"
|
|
||||||
(ev "(multiple-value-bind (a) (values 1 2 3) a)")
|
|
||||||
1)
|
|
||||||
(cl-test "multiple-value-bind: single value source"
|
|
||||||
(ev "(multiple-value-bind (a b) 42 (list a b))")
|
|
||||||
(list 42 nil))
|
|
||||||
(cl-test "nth-value: 0"
|
|
||||||
(ev "(nth-value 0 (values 10 20 30))")
|
|
||||||
10)
|
|
||||||
(cl-test "nth-value: 1"
|
|
||||||
(ev "(nth-value 1 (values 10 20 30))")
|
|
||||||
20)
|
|
||||||
(cl-test "nth-value: out of range"
|
|
||||||
(ev "(nth-value 5 (values 10 20))")
|
|
||||||
nil)
|
|
||||||
(cl-test "multiple-value-call: basic"
|
|
||||||
(ev "(multiple-value-call #'+ (values 1 2) (values 3 4))")
|
|
||||||
10)
|
|
||||||
(cl-test "multiple-value-prog1: returns first"
|
|
||||||
(ev "(multiple-value-prog1 1 2 3)")
|
|
||||||
1)
|
|
||||||
(cl-test "multiple-value-prog1: side effects run"
|
|
||||||
(ev "(let ((x 0)) (multiple-value-prog1 99 (setq x 7)) x)")
|
|
||||||
7)
|
|
||||||
(cl-test "values: nil primary in if"
|
|
||||||
(ev "(if (values nil t) 'yes 'no)")
|
|
||||||
"NO")
|
|
||||||
(cl-test "values: truthy primary in if"
|
|
||||||
(ev "(if (values 42 nil) 'yes 'no)")
|
|
||||||
"YES")
|
|
||||||
@@ -1,204 +0,0 @@
|
|||||||
;; Lambda list parser tests
|
|
||||||
|
|
||||||
(define cl-test-pass 0)
|
|
||||||
(define cl-test-fail 0)
|
|
||||||
(define cl-test-fails (list))
|
|
||||||
|
|
||||||
;; Deep structural equality for dicts and lists
|
|
||||||
(define
|
|
||||||
cl-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) (cl-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
|
|
||||||
chk
|
|
||||||
(fn
|
|
||||||
()
|
|
||||||
(when
|
|
||||||
(and ok (< i (len a)))
|
|
||||||
(do
|
|
||||||
(when
|
|
||||||
(not (cl-deep= (nth a i) (nth b i)))
|
|
||||||
(set! ok false))
|
|
||||||
(set! i (+ i 1))
|
|
||||||
(chk)))))
|
|
||||||
(chk)
|
|
||||||
ok)))
|
|
||||||
(:else false))))
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-test
|
|
||||||
(fn
|
|
||||||
(name actual expected)
|
|
||||||
(if
|
|
||||||
(cl-deep= actual expected)
|
|
||||||
(set! cl-test-pass (+ cl-test-pass 1))
|
|
||||||
(do
|
|
||||||
(set! cl-test-fail (+ cl-test-fail 1))
|
|
||||||
(append! cl-test-fails {:name name :expected expected :actual actual})))))
|
|
||||||
|
|
||||||
;; Helper: parse lambda list from string "(x y ...)"
|
|
||||||
(define ll (fn (src) (cl-parse-lambda-list-str src)))
|
|
||||||
(define ll-req (fn (src) (get (ll src) "required")))
|
|
||||||
(define ll-opt (fn (src) (get (ll src) "optional")))
|
|
||||||
(define ll-rest (fn (src) (get (ll src) "rest")))
|
|
||||||
(define ll-key (fn (src) (get (ll src) "key")))
|
|
||||||
(define ll-aok (fn (src) (get (ll src) "allow-other-keys")))
|
|
||||||
(define ll-aux (fn (src) (get (ll src) "aux")))
|
|
||||||
|
|
||||||
;; ── required parameters ───────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "required: empty" (ll-req "()") (list))
|
|
||||||
(cl-test "required: one" (ll-req "(x)") (list "X"))
|
|
||||||
(cl-test "required: two" (ll-req "(x y)") (list "X" "Y"))
|
|
||||||
(cl-test "required: three" (ll-req "(a b c)") (list "A" "B" "C"))
|
|
||||||
(cl-test "required: upcased" (ll-req "(foo bar)") (list "FOO" "BAR"))
|
|
||||||
|
|
||||||
;; ── &optional ─────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "optional: none" (ll-opt "(x)") (list))
|
|
||||||
|
|
||||||
(cl-test
|
|
||||||
"optional: bare symbol"
|
|
||||||
(ll-opt "(x &optional z)")
|
|
||||||
(list {:name "Z" :default nil :supplied nil}))
|
|
||||||
|
|
||||||
(cl-test
|
|
||||||
"optional: with default"
|
|
||||||
(ll-opt "(x &optional (z 0))")
|
|
||||||
(list {:name "Z" :default 0 :supplied nil}))
|
|
||||||
|
|
||||||
(cl-test
|
|
||||||
"optional: with supplied-p"
|
|
||||||
(ll-opt "(x &optional (z 0 z-p))")
|
|
||||||
(list {:name "Z" :default 0 :supplied "Z-P"}))
|
|
||||||
|
|
||||||
(cl-test
|
|
||||||
"optional: two params"
|
|
||||||
(ll-opt "(&optional a (b 1))")
|
|
||||||
(list {:name "A" :default nil :supplied nil} {:name "B" :default 1 :supplied nil}))
|
|
||||||
|
|
||||||
(cl-test
|
|
||||||
"optional: string default"
|
|
||||||
(ll-opt "(&optional (name \"world\"))")
|
|
||||||
(list {:name "NAME" :default {:cl-type "string" :value "world"} :supplied nil}))
|
|
||||||
|
|
||||||
;; ── &rest ─────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "rest: none" (ll-rest "(x)") nil)
|
|
||||||
(cl-test "rest: present" (ll-rest "(x &rest args)") "ARGS")
|
|
||||||
(cl-test "rest: with required" (ll-rest "(a b &rest tail)") "TAIL")
|
|
||||||
|
|
||||||
;; &body is an alias for &rest
|
|
||||||
(cl-test "body: alias for rest" (ll-rest "(&body forms)") "FORMS")
|
|
||||||
|
|
||||||
;; rest doesn't consume required params
|
|
||||||
(cl-test "rest: required still there" (ll-req "(a b &rest rest)") (list "A" "B"))
|
|
||||||
|
|
||||||
;; ── &key ──────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "key: none" (ll-key "(x)") (list))
|
|
||||||
|
|
||||||
(cl-test
|
|
||||||
"key: bare symbol"
|
|
||||||
(ll-key "(&key x)")
|
|
||||||
(list {:name "X" :keyword "X" :default nil :supplied nil}))
|
|
||||||
|
|
||||||
(cl-test
|
|
||||||
"key: with default"
|
|
||||||
(ll-key "(&key (x 42))")
|
|
||||||
(list {:name "X" :keyword "X" :default 42 :supplied nil}))
|
|
||||||
|
|
||||||
(cl-test
|
|
||||||
"key: with supplied-p"
|
|
||||||
(ll-key "(&key (x 42 x-p))")
|
|
||||||
(list {:name "X" :keyword "X" :default 42 :supplied "X-P"}))
|
|
||||||
|
|
||||||
(cl-test
|
|
||||||
"key: two params"
|
|
||||||
(ll-key "(&key a b)")
|
|
||||||
(list
|
|
||||||
{:name "A" :keyword "A" :default nil :supplied nil}
|
|
||||||
{:name "B" :keyword "B" :default nil :supplied nil}))
|
|
||||||
|
|
||||||
;; ── &allow-other-keys ─────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "aok: absent" (ll-aok "(x)") false)
|
|
||||||
(cl-test "aok: present" (ll-aok "(&key x &allow-other-keys)") true)
|
|
||||||
|
|
||||||
;; ── &aux ──────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "aux: none" (ll-aux "(x)") (list))
|
|
||||||
|
|
||||||
(cl-test
|
|
||||||
"aux: bare symbol"
|
|
||||||
(ll-aux "(&aux temp)")
|
|
||||||
(list {:name "TEMP" :init nil}))
|
|
||||||
|
|
||||||
(cl-test
|
|
||||||
"aux: with init"
|
|
||||||
(ll-aux "(&aux (count 0))")
|
|
||||||
(list {:name "COUNT" :init 0}))
|
|
||||||
|
|
||||||
(cl-test
|
|
||||||
"aux: two vars"
|
|
||||||
(ll-aux "(&aux a (b 1))")
|
|
||||||
(list {:name "A" :init nil} {:name "B" :init 1}))
|
|
||||||
|
|
||||||
;; ── combined ──────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test
|
|
||||||
"combined: full lambda list"
|
|
||||||
(let
|
|
||||||
((parsed (ll "(x y &optional (z 0 z-p) &rest args &key a (b nil b-p) &aux temp)")))
|
|
||||||
(list
|
|
||||||
(get parsed "required")
|
|
||||||
(get (nth (get parsed "optional") 0) "name")
|
|
||||||
(get (nth (get parsed "optional") 0) "default")
|
|
||||||
(get (nth (get parsed "optional") 0) "supplied")
|
|
||||||
(get parsed "rest")
|
|
||||||
(get (nth (get parsed "key") 0) "name")
|
|
||||||
(get (nth (get parsed "key") 1) "supplied")
|
|
||||||
(get (nth (get parsed "aux") 0) "name")))
|
|
||||||
(list
|
|
||||||
(list "X" "Y")
|
|
||||||
"Z"
|
|
||||||
0
|
|
||||||
"Z-P"
|
|
||||||
"ARGS"
|
|
||||||
"A"
|
|
||||||
"B-P"
|
|
||||||
"TEMP"))
|
|
||||||
|
|
||||||
(cl-test
|
|
||||||
"combined: required only stops before &"
|
|
||||||
(ll-req "(a b &optional c)")
|
|
||||||
(list "A" "B"))
|
|
||||||
|
|
||||||
(cl-test
|
|
||||||
"combined: required only with &key"
|
|
||||||
(ll-req "(x &key y)")
|
|
||||||
(list "X"))
|
|
||||||
|
|
||||||
(cl-test
|
|
||||||
"combined: &rest and &key together"
|
|
||||||
(let
|
|
||||||
((parsed (ll "(&rest args &key verbose)")))
|
|
||||||
(list (get parsed "rest") (get (nth (get parsed "key") 0) "name")))
|
|
||||||
(list "ARGS" "VERBOSE"))
|
|
||||||
@@ -1,160 +0,0 @@
|
|||||||
;; Common Lisp reader/parser tests
|
|
||||||
|
|
||||||
(define cl-test-pass 0)
|
|
||||||
(define cl-test-fail 0)
|
|
||||||
(define cl-test-fails (list))
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-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) (cl-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
|
|
||||||
chk
|
|
||||||
(fn
|
|
||||||
()
|
|
||||||
(when
|
|
||||||
(and ok (< i (len a)))
|
|
||||||
(do
|
|
||||||
(when
|
|
||||||
(not (cl-deep= (nth a i) (nth b i)))
|
|
||||||
(set! ok false))
|
|
||||||
(set! i (+ i 1))
|
|
||||||
(chk)))))
|
|
||||||
(chk)
|
|
||||||
ok)))
|
|
||||||
(:else false))))
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-test
|
|
||||||
(fn
|
|
||||||
(name actual expected)
|
|
||||||
(if
|
|
||||||
(cl-deep= actual expected)
|
|
||||||
(set! cl-test-pass (+ cl-test-pass 1))
|
|
||||||
(do
|
|
||||||
(set! cl-test-fail (+ cl-test-fail 1))
|
|
||||||
(append! cl-test-fails {:name name :expected expected :actual actual})))))
|
|
||||||
|
|
||||||
;; ── atoms ─────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "integer: 42" (cl-read "42") 42)
|
|
||||||
(cl-test "integer: 0" (cl-read "0") 0)
|
|
||||||
(cl-test "integer: negative" (cl-read "-5") -5)
|
|
||||||
(cl-test "integer: positive sign" (cl-read "+3") 3)
|
|
||||||
(cl-test "integer: hex #xFF" (cl-read "#xFF") 255)
|
|
||||||
(cl-test "integer: hex #xAB" (cl-read "#xAB") 171)
|
|
||||||
(cl-test "integer: binary #b1010" (cl-read "#b1010") 10)
|
|
||||||
(cl-test "integer: octal #o17" (cl-read "#o17") 15)
|
|
||||||
|
|
||||||
(cl-test "float: type" (get (cl-read "3.14") "cl-type") "float")
|
|
||||||
(cl-test "float: value" (get (cl-read "3.14") "value") "3.14")
|
|
||||||
(cl-test "float: neg" (get (cl-read "-2.5") "value") "-2.5")
|
|
||||||
(cl-test "float: exp" (get (cl-read "1.0e10") "value") "1.0e10")
|
|
||||||
|
|
||||||
(cl-test "ratio: type" (get (cl-read "1/3") "cl-type") "ratio")
|
|
||||||
(cl-test "ratio: value" (get (cl-read "1/3") "value") "1/3")
|
|
||||||
(cl-test "ratio: 22/7" (get (cl-read "22/7") "value") "22/7")
|
|
||||||
|
|
||||||
(cl-test "string: basic" (cl-read "\"hello\"") {:cl-type "string" :value "hello"})
|
|
||||||
(cl-test "string: empty" (cl-read "\"\"") {:cl-type "string" :value ""})
|
|
||||||
(cl-test "string: with escape" (cl-read "\"a\\nb\"") {:cl-type "string" :value "a\nb"})
|
|
||||||
|
|
||||||
(cl-test "symbol: foo" (cl-read "foo") "FOO")
|
|
||||||
(cl-test "symbol: BAR" (cl-read "BAR") "BAR")
|
|
||||||
(cl-test "symbol: pkg:sym" (cl-read "cl:car") "CL:CAR")
|
|
||||||
(cl-test "symbol: pkg::sym" (cl-read "pkg::foo") "PKG::FOO")
|
|
||||||
|
|
||||||
(cl-test "nil: symbol" (cl-read "nil") nil)
|
|
||||||
(cl-test "nil: uppercase" (cl-read "NIL") nil)
|
|
||||||
(cl-test "t: symbol" (cl-read "t") true)
|
|
||||||
(cl-test "t: uppercase" (cl-read "T") true)
|
|
||||||
|
|
||||||
(cl-test "keyword: type" (get (cl-read ":foo") "cl-type") "keyword")
|
|
||||||
(cl-test "keyword: name" (get (cl-read ":foo") "name") "FOO")
|
|
||||||
(cl-test "keyword: :test" (get (cl-read ":test") "name") "TEST")
|
|
||||||
|
|
||||||
(cl-test "char: type" (get (cl-read "#\\a") "cl-type") "char")
|
|
||||||
(cl-test "char: value" (get (cl-read "#\\a") "value") "a")
|
|
||||||
(cl-test "char: Space" (get (cl-read "#\\Space") "value") " ")
|
|
||||||
(cl-test "char: Newline" (get (cl-read "#\\Newline") "value") "\n")
|
|
||||||
|
|
||||||
(cl-test "uninterned: type" (get (cl-read "#:foo") "cl-type") "uninterned")
|
|
||||||
(cl-test "uninterned: name" (get (cl-read "#:foo") "name") "FOO")
|
|
||||||
|
|
||||||
;; ── lists ─────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "list: empty" (cl-read "()") (list))
|
|
||||||
(cl-test "list: one element" (cl-read "(foo)") (list "FOO"))
|
|
||||||
(cl-test "list: two elements" (cl-read "(foo bar)") (list "FOO" "BAR"))
|
|
||||||
(cl-test "list: nested" (cl-read "((a b) c)") (list (list "A" "B") "C"))
|
|
||||||
(cl-test "list: with integer" (cl-read "(+ 1 2)") (list "+" 1 2))
|
|
||||||
(cl-test "list: with string" (cl-read "(print \"hi\")") (list "PRINT" {:cl-type "string" :value "hi"}))
|
|
||||||
(cl-test "list: nil element" (cl-read "(a nil b)") (list "A" nil "B"))
|
|
||||||
(cl-test "list: t element" (cl-read "(a t b)") (list "A" true "B"))
|
|
||||||
|
|
||||||
;; ── dotted pairs ──────────────────────────────────────────────<E29480><E29480>──
|
|
||||||
|
|
||||||
(cl-test "dotted: type" (get (cl-read "(a . b)") "cl-type") "cons")
|
|
||||||
(cl-test "dotted: car" (get (cl-read "(a . b)") "car") "A")
|
|
||||||
(cl-test "dotted: cdr" (get (cl-read "(a . b)") "cdr") "B")
|
|
||||||
(cl-test "dotted: number cdr" (get (cl-read "(x . 42)") "cdr") 42)
|
|
||||||
|
|
||||||
;; ── reader macros ────────────────────────────────────────────────<E29480><E29480>
|
|
||||||
|
|
||||||
(cl-test "quote: form" (cl-read "'x") (list "QUOTE" "X"))
|
|
||||||
(cl-test "quote: list" (cl-read "'(a b)") (list "QUOTE" (list "A" "B")))
|
|
||||||
(cl-test "backquote: form" (cl-read "`x") (list "QUASIQUOTE" "X"))
|
|
||||||
(cl-test "unquote: form" (cl-read ",x") (list "UNQUOTE" "X"))
|
|
||||||
(cl-test "comma-at: form" (cl-read ",@x") (list "UNQUOTE-SPLICING" "X"))
|
|
||||||
(cl-test "function: form" (cl-read "#'foo") (list "FUNCTION" "FOO"))
|
|
||||||
|
|
||||||
;; ── vector ────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "vector: type" (get (cl-read "#(1 2 3)") "cl-type") "vector")
|
|
||||||
(cl-test "vector: elements" (get (cl-read "#(1 2 3)") "elements") (list 1 2 3))
|
|
||||||
(cl-test "vector: empty" (get (cl-read "#()") "elements") (list))
|
|
||||||
(cl-test "vector: mixed" (get (cl-read "#(a 1 \"s\")") "elements") (list "A" 1 {:cl-type "string" :value "s"}))
|
|
||||||
|
|
||||||
;; ── cl-read-all ───────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test
|
|
||||||
"read-all: empty"
|
|
||||||
(cl-read-all "")
|
|
||||||
(list))
|
|
||||||
|
|
||||||
(cl-test
|
|
||||||
"read-all: two forms"
|
|
||||||
(cl-read-all "42 foo")
|
|
||||||
(list 42 "FOO"))
|
|
||||||
|
|
||||||
(cl-test
|
|
||||||
"read-all: three forms"
|
|
||||||
(cl-read-all "(+ 1 2) (+ 3 4) hello")
|
|
||||||
(list (list "+" 1 2) (list "+" 3 4) "HELLO"))
|
|
||||||
|
|
||||||
(cl-test
|
|
||||||
"read-all: with comments"
|
|
||||||
(cl-read-all "; this is a comment\n42 ; inline\nfoo")
|
|
||||||
(list 42 "FOO"))
|
|
||||||
|
|
||||||
(cl-test
|
|
||||||
"read-all: defun form"
|
|
||||||
(nth (cl-read-all "(defun square (x) (* x x))") 0)
|
|
||||||
(list "DEFUN" "SQUARE" (list "X") (list "*" "X" "X")))
|
|
||||||
@@ -1,196 +0,0 @@
|
|||||||
;; interactive-debugger.sx — Condition debugger using *debugger-hook*
|
|
||||||
;;
|
|
||||||
;; Demonstrates the classic CL debugger pattern:
|
|
||||||
;; - *debugger-hook* is invoked when an unhandled error reaches the top level
|
|
||||||
;; - The hook receives the condition and a reference to itself
|
|
||||||
;; - It can offer restarts interactively (here simulated with a policy fn)
|
|
||||||
;;
|
|
||||||
;; In real CL the debugger reads from the terminal. Here we simulate
|
|
||||||
;; the "user input" via a policy function passed in at call time.
|
|
||||||
;;
|
|
||||||
;; Depends on: lib/common-lisp/runtime.sx already loaded.
|
|
||||||
|
|
||||||
;; ── *debugger-hook* global ────────────────────────────────────────────────
|
|
||||||
;;
|
|
||||||
;; CL: when error is unhandled, invoke *debugger-hook* with (condition hook).
|
|
||||||
;; A nil hook means use the system default (which we simulate as re-raise).
|
|
||||||
|
|
||||||
(define cl-debugger-hook nil)
|
|
||||||
|
|
||||||
;; ── invoke-debugger ────────────────────────────────────────────────────────
|
|
||||||
;;
|
|
||||||
;; Called when cl-error finds no handler. Tries cl-debugger-hook first;
|
|
||||||
;; falls back to a simple error report.
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-invoke-debugger
|
|
||||||
(fn
|
|
||||||
(c)
|
|
||||||
(if
|
|
||||||
(nil? cl-debugger-hook)
|
|
||||||
(error (str "Debugger: " (cl-condition-message c)))
|
|
||||||
(begin
|
|
||||||
(let
|
|
||||||
((hook cl-debugger-hook))
|
|
||||||
(set! cl-debugger-hook nil)
|
|
||||||
(let
|
|
||||||
((result (hook c hook)))
|
|
||||||
(set! cl-debugger-hook hook)
|
|
||||||
result))))))
|
|
||||||
|
|
||||||
;; ── cl-error/debugger — error that routes through invoke-debugger ─────────
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-error-with-debugger
|
|
||||||
(fn
|
|
||||||
(c &rest args)
|
|
||||||
(let
|
|
||||||
((obj (cond ((cl-condition? c) c) ((string? c) (cl-make-condition "simple-error" "format-control" c "format-arguments" args)) (:else (cl-make-condition "simple-error" "format-control" (str c))))))
|
|
||||||
(cl-signal-obj obj cl-handler-stack)
|
|
||||||
(cl-invoke-debugger obj))))
|
|
||||||
|
|
||||||
;; ── simulated debugger session ────────────────────────────────────────────
|
|
||||||
;;
|
|
||||||
;; A debugger hook takes (condition hook) and "reads" user commands.
|
|
||||||
;; We simulate this with a policy function: (fn (c restarts) restart-name)
|
|
||||||
;; that picks a restart given the condition and available restarts.
|
|
||||||
|
|
||||||
(define
|
|
||||||
make-policy-debugger
|
|
||||||
(fn
|
|
||||||
(policy)
|
|
||||||
(fn
|
|
||||||
(c hook)
|
|
||||||
(let
|
|
||||||
((available (cl-compute-restarts)))
|
|
||||||
(let
|
|
||||||
((choice (policy c available)))
|
|
||||||
(if
|
|
||||||
(and choice (not (nil? (cl-find-restart choice))))
|
|
||||||
(cl-invoke-restart choice)
|
|
||||||
(error
|
|
||||||
(str
|
|
||||||
"Debugger: no restart chosen for: "
|
|
||||||
(cl-condition-message c)))))))))
|
|
||||||
|
|
||||||
;; ── tests ─────────────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(define passed 0)
|
|
||||||
(define failed 0)
|
|
||||||
(define failures (list))
|
|
||||||
|
|
||||||
(define
|
|
||||||
check
|
|
||||||
(fn
|
|
||||||
(label got expected)
|
|
||||||
(if
|
|
||||||
(= got expected)
|
|
||||||
(set! passed (+ passed 1))
|
|
||||||
(begin
|
|
||||||
(set! failed (+ failed 1))
|
|
||||||
(set!
|
|
||||||
failures
|
|
||||||
(append
|
|
||||||
failures
|
|
||||||
(list
|
|
||||||
(str
|
|
||||||
"FAIL ["
|
|
||||||
label
|
|
||||||
"]: got="
|
|
||||||
(inspect got)
|
|
||||||
" expected="
|
|
||||||
(inspect expected)))))))))
|
|
||||||
|
|
||||||
(define
|
|
||||||
reset-stacks!
|
|
||||||
(fn
|
|
||||||
()
|
|
||||||
(set! cl-handler-stack (list))
|
|
||||||
(set! cl-restart-stack (list))
|
|
||||||
(set! cl-debugger-hook nil)))
|
|
||||||
|
|
||||||
;; Test 1: debugger hook receives condition
|
|
||||||
(reset-stacks!)
|
|
||||||
(let
|
|
||||||
((received-msg ""))
|
|
||||||
(begin
|
|
||||||
(set!
|
|
||||||
cl-debugger-hook
|
|
||||||
(fn (c hook) (set! received-msg (cl-condition-message c)) nil))
|
|
||||||
(cl-restart-case
|
|
||||||
(fn () (cl-error-with-debugger "something broke"))
|
|
||||||
(list "abort" (list) (fn () nil)))
|
|
||||||
(check "debugger hook receives condition" received-msg "something broke")))
|
|
||||||
|
|
||||||
;; Test 2: policy-driven restart selection (use-zero)
|
|
||||||
(reset-stacks!)
|
|
||||||
(let
|
|
||||||
((result (begin (set! cl-debugger-hook (make-policy-debugger (fn (c restarts) "use-zero"))) (cl-restart-case (fn () (cl-error-with-debugger (cl-make-condition "division-by-zero")) 999) (list "use-zero" (list) (fn () 0))))))
|
|
||||||
(check "policy debugger: use-zero restart" result 0))
|
|
||||||
|
|
||||||
;; Test 3: policy selects abort
|
|
||||||
(reset-stacks!)
|
|
||||||
(let
|
|
||||||
((result (begin (set! cl-debugger-hook (make-policy-debugger (fn (c restarts) "abort"))) (cl-restart-case (fn () (cl-error-with-debugger "aborting error") 999) (list "abort" (list) (fn () "aborted"))))))
|
|
||||||
(check "policy debugger: abort restart" result "aborted"))
|
|
||||||
|
|
||||||
;; Test 4: compute-restarts inside debugger hook
|
|
||||||
(reset-stacks!)
|
|
||||||
(let
|
|
||||||
((seen-restarts (list)))
|
|
||||||
(begin
|
|
||||||
(set!
|
|
||||||
cl-debugger-hook
|
|
||||||
(fn
|
|
||||||
(c hook)
|
|
||||||
(set! seen-restarts (cl-compute-restarts))
|
|
||||||
(cl-invoke-restart "continue")))
|
|
||||||
(cl-restart-case
|
|
||||||
(fn () (cl-error-with-debugger "test") 42)
|
|
||||||
(list "continue" (list) (fn () "ok"))
|
|
||||||
(list "abort" (list) (fn () "no")))
|
|
||||||
(check
|
|
||||||
"debugger: compute-restarts visible"
|
|
||||||
(= (len seen-restarts) 2)
|
|
||||||
true)))
|
|
||||||
|
|
||||||
;; Test 5: hook not invoked when handler catches first
|
|
||||||
(reset-stacks!)
|
|
||||||
(let
|
|
||||||
((hook-called false)
|
|
||||||
(result
|
|
||||||
(begin
|
|
||||||
(set! cl-debugger-hook (fn (c hook) (set! hook-called true) nil))
|
|
||||||
(cl-handler-case
|
|
||||||
(fn () (cl-error-with-debugger "handled"))
|
|
||||||
(list "error" (fn (c) "handler-won"))))))
|
|
||||||
(check "handler wins; hook not called" hook-called false)
|
|
||||||
(check "handler result returned" result "handler-won"))
|
|
||||||
|
|
||||||
;; Test 6: debugger-hook nil after re-raise guard
|
|
||||||
(reset-stacks!)
|
|
||||||
(let
|
|
||||||
((hook-calls 0))
|
|
||||||
(begin
|
|
||||||
(set!
|
|
||||||
cl-debugger-hook
|
|
||||||
(fn
|
|
||||||
(c hook)
|
|
||||||
(set! hook-calls (+ hook-calls 1))
|
|
||||||
(if
|
|
||||||
(> hook-calls 1)
|
|
||||||
(error "infinite loop guard")
|
|
||||||
(cl-invoke-restart "escape"))))
|
|
||||||
(cl-restart-case
|
|
||||||
(fn () (cl-error-with-debugger "once"))
|
|
||||||
(list "escape" (list) (fn () nil)))
|
|
||||||
(check
|
|
||||||
"hook called exactly once (no infinite recursion)"
|
|
||||||
hook-calls
|
|
||||||
1)))
|
|
||||||
|
|
||||||
;; ── summary ────────────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(define debugger-passed passed)
|
|
||||||
(define debugger-failed failed)
|
|
||||||
(define debugger-failures failures)
|
|
||||||
@@ -1,163 +0,0 @@
|
|||||||
;; parse-recover.sx — Parser with skipped-token restart
|
|
||||||
;;
|
|
||||||
;; Classic CL pattern: a simple token parser that signals a condition
|
|
||||||
;; when it encounters an unexpected token. The :skip-token restart
|
|
||||||
;; allows the parser to continue past the offending token.
|
|
||||||
;;
|
|
||||||
;; Depends on: lib/common-lisp/runtime.sx already loaded.
|
|
||||||
|
|
||||||
;; ── condition type ─────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-define-condition "parse-error" (list "error") (list "token" "position"))
|
|
||||||
|
|
||||||
;; ── simple token parser ────────────────────────────────────────────────────
|
|
||||||
;;
|
|
||||||
;; parse-numbers: given a list of tokens (strings), parse integers.
|
|
||||||
;; Non-integer tokens signal parse-error with two restarts:
|
|
||||||
;; skip-token — skip the bad token and continue
|
|
||||||
;; use-zero — use 0 in place of the bad token
|
|
||||||
|
|
||||||
(define
|
|
||||||
parse-numbers
|
|
||||||
(fn
|
|
||||||
(tokens)
|
|
||||||
(define result (list))
|
|
||||||
(define
|
|
||||||
process
|
|
||||||
(fn
|
|
||||||
(toks)
|
|
||||||
(if
|
|
||||||
(empty? toks)
|
|
||||||
result
|
|
||||||
(let
|
|
||||||
((tok (first toks)) (rest-toks (rest toks)))
|
|
||||||
(let
|
|
||||||
((n (string->number tok 10)))
|
|
||||||
(if
|
|
||||||
n
|
|
||||||
(begin
|
|
||||||
(set! result (append result (list n)))
|
|
||||||
(process rest-toks))
|
|
||||||
(cl-restart-case
|
|
||||||
(fn
|
|
||||||
()
|
|
||||||
(cl-signal
|
|
||||||
(cl-make-condition
|
|
||||||
"parse-error"
|
|
||||||
"token"
|
|
||||||
tok
|
|
||||||
"position"
|
|
||||||
(len result)))
|
|
||||||
(set! result (append result (list 0)))
|
|
||||||
(process rest-toks))
|
|
||||||
(list "skip-token" (list) (fn () (process rest-toks)))
|
|
||||||
(list
|
|
||||||
"use-zero"
|
|
||||||
(list)
|
|
||||||
(fn
|
|
||||||
()
|
|
||||||
(begin
|
|
||||||
(set! result (append result (list 0)))
|
|
||||||
(process rest-toks)))))))))))
|
|
||||||
(process tokens)
|
|
||||||
result))
|
|
||||||
|
|
||||||
;; ── tests ─────────────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(define passed 0)
|
|
||||||
(define failed 0)
|
|
||||||
(define failures (list))
|
|
||||||
|
|
||||||
(define
|
|
||||||
check
|
|
||||||
(fn
|
|
||||||
(label got expected)
|
|
||||||
(if
|
|
||||||
(= got expected)
|
|
||||||
(set! passed (+ passed 1))
|
|
||||||
(begin
|
|
||||||
(set! failed (+ failed 1))
|
|
||||||
(set!
|
|
||||||
failures
|
|
||||||
(append
|
|
||||||
failures
|
|
||||||
(list
|
|
||||||
(str
|
|
||||||
"FAIL ["
|
|
||||||
label
|
|
||||||
"]: got="
|
|
||||||
(inspect got)
|
|
||||||
" expected="
|
|
||||||
(inspect expected)))))))))
|
|
||||||
|
|
||||||
(define
|
|
||||||
reset-stacks!
|
|
||||||
(fn () (set! cl-handler-stack (list)) (set! cl-restart-stack (list))))
|
|
||||||
|
|
||||||
;; All valid tokens
|
|
||||||
(reset-stacks!)
|
|
||||||
(check
|
|
||||||
"all valid: 1 2 3"
|
|
||||||
(cl-handler-bind
|
|
||||||
(list (list "parse-error" (fn (c) (cl-invoke-restart "skip-token"))))
|
|
||||||
(fn () (parse-numbers (list "1" "2" "3"))))
|
|
||||||
(list 1 2 3))
|
|
||||||
|
|
||||||
;; Skip bad token
|
|
||||||
(reset-stacks!)
|
|
||||||
(check
|
|
||||||
"skip bad token: 1 x 3 -> (1 3)"
|
|
||||||
(cl-handler-bind
|
|
||||||
(list (list "parse-error" (fn (c) (cl-invoke-restart "skip-token"))))
|
|
||||||
(fn () (parse-numbers (list "1" "x" "3"))))
|
|
||||||
(list 1 3))
|
|
||||||
|
|
||||||
;; Use zero for bad token
|
|
||||||
(reset-stacks!)
|
|
||||||
(check
|
|
||||||
"use-zero for bad: 1 x 3 -> (1 0 3)"
|
|
||||||
(cl-handler-bind
|
|
||||||
(list (list "parse-error" (fn (c) (cl-invoke-restart "use-zero"))))
|
|
||||||
(fn () (parse-numbers (list "1" "x" "3"))))
|
|
||||||
(list 1 0 3))
|
|
||||||
|
|
||||||
;; Multiple bad tokens, all skipped
|
|
||||||
(reset-stacks!)
|
|
||||||
(check
|
|
||||||
"skip multiple bad: a 2 b 4 -> (2 4)"
|
|
||||||
(cl-handler-bind
|
|
||||||
(list (list "parse-error" (fn (c) (cl-invoke-restart "skip-token"))))
|
|
||||||
(fn () (parse-numbers (list "a" "2" "b" "4"))))
|
|
||||||
(list 2 4))
|
|
||||||
|
|
||||||
;; handler-case: abort on first bad token
|
|
||||||
(reset-stacks!)
|
|
||||||
(check
|
|
||||||
"handler-case: abort on first bad"
|
|
||||||
(cl-handler-case
|
|
||||||
(fn () (parse-numbers (list "1" "bad" "3")))
|
|
||||||
(list
|
|
||||||
"parse-error"
|
|
||||||
(fn
|
|
||||||
(c)
|
|
||||||
(str
|
|
||||||
"parse error at position "
|
|
||||||
(cl-condition-slot c "position")
|
|
||||||
": "
|
|
||||||
(cl-condition-slot c "token")))))
|
|
||||||
"parse error at position 1: bad")
|
|
||||||
|
|
||||||
;; Verify condition type hierarchy
|
|
||||||
(reset-stacks!)
|
|
||||||
(check
|
|
||||||
"parse-error isa error"
|
|
||||||
(cl-condition-of-type?
|
|
||||||
(cl-make-condition "parse-error" "token" "x" "position" 0)
|
|
||||||
"error")
|
|
||||||
true)
|
|
||||||
|
|
||||||
;; ── summary ────────────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(define parse-passed passed)
|
|
||||||
(define parse-failed failed)
|
|
||||||
(define parse-failures failures)
|
|
||||||
@@ -1,141 +0,0 @@
|
|||||||
;; restart-demo.sx — Classic CL condition system demo
|
|
||||||
;;
|
|
||||||
;; Demonstrates resumable exceptions via restarts.
|
|
||||||
;; The `safe-divide` function signals a division-by-zero condition
|
|
||||||
;; and offers two restarts:
|
|
||||||
;; :use-zero — return 0 as the result
|
|
||||||
;; :retry — call safe-divide again with a corrected divisor
|
|
||||||
;;
|
|
||||||
;; Depends on: lib/common-lisp/runtime.sx already loaded.
|
|
||||||
|
|
||||||
;; ── safe-divide ────────────────────────────────────────────────────────────
|
|
||||||
;;
|
|
||||||
;; Divides numerator by denominator.
|
|
||||||
;; When denominator is 0, signals division-by-zero with two restarts.
|
|
||||||
|
|
||||||
(define
|
|
||||||
safe-divide
|
|
||||||
(fn
|
|
||||||
(n d)
|
|
||||||
(if
|
|
||||||
(= d 0)
|
|
||||||
(cl-restart-case
|
|
||||||
(fn
|
|
||||||
()
|
|
||||||
(cl-signal
|
|
||||||
(cl-make-condition
|
|
||||||
"division-by-zero"
|
|
||||||
"operation"
|
|
||||||
"/"
|
|
||||||
"operands"
|
|
||||||
(list n d)))
|
|
||||||
(error "division by zero — no restart invoked"))
|
|
||||||
(list "use-zero" (list) (fn () 0))
|
|
||||||
(list "retry" (list "d") (fn (d2) (safe-divide n d2))))
|
|
||||||
(/ n d))))
|
|
||||||
|
|
||||||
;; ── tests ─────────────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(define passed 0)
|
|
||||||
(define failed 0)
|
|
||||||
(define failures (list))
|
|
||||||
|
|
||||||
(define
|
|
||||||
check
|
|
||||||
(fn
|
|
||||||
(label got expected)
|
|
||||||
(if
|
|
||||||
(= got expected)
|
|
||||||
(set! passed (+ passed 1))
|
|
||||||
(begin
|
|
||||||
(set! failed (+ failed 1))
|
|
||||||
(set!
|
|
||||||
failures
|
|
||||||
(append
|
|
||||||
failures
|
|
||||||
(list
|
|
||||||
(str
|
|
||||||
"FAIL ["
|
|
||||||
label
|
|
||||||
"]: got="
|
|
||||||
(inspect got)
|
|
||||||
" expected="
|
|
||||||
(inspect expected)))))))))
|
|
||||||
|
|
||||||
(define
|
|
||||||
reset-stacks!
|
|
||||||
(fn () (set! cl-handler-stack (list)) (set! cl-restart-stack (list))))
|
|
||||||
|
|
||||||
;; Normal division
|
|
||||||
(reset-stacks!)
|
|
||||||
(check "10 / 2 = 5" (safe-divide 10 2) 5)
|
|
||||||
|
|
||||||
;; Invoke use-zero restart
|
|
||||||
(reset-stacks!)
|
|
||||||
(check
|
|
||||||
"10 / 0 -> use-zero"
|
|
||||||
(cl-handler-bind
|
|
||||||
(list
|
|
||||||
(list "division-by-zero" (fn (c) (cl-invoke-restart "use-zero"))))
|
|
||||||
(fn () (safe-divide 10 0)))
|
|
||||||
0)
|
|
||||||
|
|
||||||
;; Invoke retry restart with a corrected denominator
|
|
||||||
(reset-stacks!)
|
|
||||||
(check
|
|
||||||
"10 / 0 -> retry with 2"
|
|
||||||
(cl-handler-bind
|
|
||||||
(list
|
|
||||||
(list
|
|
||||||
"division-by-zero"
|
|
||||||
(fn (c) (cl-invoke-restart "retry" 2))))
|
|
||||||
(fn () (safe-divide 10 0)))
|
|
||||||
5)
|
|
||||||
|
|
||||||
;; Nested calls: outer handles the inner divide-by-zero
|
|
||||||
(reset-stacks!)
|
|
||||||
(check
|
|
||||||
"nested: 20 / (0->4) = 5"
|
|
||||||
(cl-handler-bind
|
|
||||||
(list
|
|
||||||
(list
|
|
||||||
"division-by-zero"
|
|
||||||
(fn (c) (cl-invoke-restart "retry" 4))))
|
|
||||||
(fn () (let ((r1 (safe-divide 20 0))) r1)))
|
|
||||||
5)
|
|
||||||
|
|
||||||
;; handler-case — unwinding version
|
|
||||||
(reset-stacks!)
|
|
||||||
(check
|
|
||||||
"handler-case: catches division-by-zero"
|
|
||||||
(cl-handler-case
|
|
||||||
(fn () (safe-divide 9 0))
|
|
||||||
(list "division-by-zero" (fn (c) "caught!")))
|
|
||||||
"caught!")
|
|
||||||
|
|
||||||
;; Verify use-zero is idempotent (two uses)
|
|
||||||
(reset-stacks!)
|
|
||||||
(check
|
|
||||||
"two use-zero invocations"
|
|
||||||
(cl-handler-bind
|
|
||||||
(list
|
|
||||||
(list "division-by-zero" (fn (c) (cl-invoke-restart "use-zero"))))
|
|
||||||
(fn
|
|
||||||
()
|
|
||||||
(+
|
|
||||||
(safe-divide 10 0)
|
|
||||||
(safe-divide 3 0))))
|
|
||||||
0)
|
|
||||||
|
|
||||||
;; No restart needed for normal division
|
|
||||||
(reset-stacks!)
|
|
||||||
(check
|
|
||||||
"no restart needed for 8/4"
|
|
||||||
(safe-divide 8 4)
|
|
||||||
2)
|
|
||||||
|
|
||||||
;; ── summary ────────────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(define demo-passed passed)
|
|
||||||
(define demo-failed failed)
|
|
||||||
(define demo-failures failures)
|
|
||||||
@@ -1,180 +0,0 @@
|
|||||||
;; Common Lisp tokenizer tests
|
|
||||||
|
|
||||||
(define cl-test-pass 0)
|
|
||||||
(define cl-test-fail 0)
|
|
||||||
(define cl-test-fails (list))
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-test
|
|
||||||
(fn
|
|
||||||
(name actual expected)
|
|
||||||
(if
|
|
||||||
(= actual expected)
|
|
||||||
(set! cl-test-pass (+ cl-test-pass 1))
|
|
||||||
(do
|
|
||||||
(set! cl-test-fail (+ cl-test-fail 1))
|
|
||||||
(append! cl-test-fails {:name name :expected expected :actual actual})))))
|
|
||||||
|
|
||||||
;; Helpers: extract types and values from token stream (drops eof)
|
|
||||||
(define
|
|
||||||
cl-tok-types
|
|
||||||
(fn
|
|
||||||
(src)
|
|
||||||
(map
|
|
||||||
(fn (t) (get t "type"))
|
|
||||||
(filter (fn (t) (not (= (get t "type") "eof"))) (cl-tokenize src)))))
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-tok-values
|
|
||||||
(fn
|
|
||||||
(src)
|
|
||||||
(map
|
|
||||||
(fn (t) (get t "value"))
|
|
||||||
(filter (fn (t) (not (= (get t "type") "eof"))) (cl-tokenize src)))))
|
|
||||||
|
|
||||||
(define
|
|
||||||
cl-tok-first
|
|
||||||
(fn (src) (nth (cl-tokenize src) 0)))
|
|
||||||
|
|
||||||
;; ── symbols ───────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "symbol: bare lowercase" (cl-tok-values "foo") (list "FOO"))
|
|
||||||
(cl-test "symbol: uppercase" (cl-tok-values "BAR") (list "BAR"))
|
|
||||||
(cl-test "symbol: mixed case folded" (cl-tok-values "FooBar") (list "FOOBAR"))
|
|
||||||
(cl-test "symbol: with hyphen" (cl-tok-values "foo-bar") (list "FOO-BAR"))
|
|
||||||
(cl-test "symbol: with star" (cl-tok-values "*special*") (list "*SPECIAL*"))
|
|
||||||
(cl-test "symbol: with question" (cl-tok-values "null?") (list "NULL?"))
|
|
||||||
(cl-test "symbol: with exclamation" (cl-tok-values "set!") (list "SET!"))
|
|
||||||
(cl-test "symbol: plus sign alone" (cl-tok-values "+") (list "+"))
|
|
||||||
(cl-test "symbol: minus sign alone" (cl-tok-values "-") (list "-"))
|
|
||||||
(cl-test "symbol: type is symbol" (cl-tok-types "foo") (list "symbol"))
|
|
||||||
|
|
||||||
;; ── package-qualified symbols ─────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "symbol: pkg:sym external" (cl-tok-values "cl:car") (list "CL:CAR"))
|
|
||||||
(cl-test "symbol: pkg::sym internal" (cl-tok-values "pkg::foo") (list "PKG::FOO"))
|
|
||||||
(cl-test "symbol: cl:car type" (cl-tok-types "cl:car") (list "symbol"))
|
|
||||||
|
|
||||||
;; ── keywords ──────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "keyword: basic" (cl-tok-values ":foo") (list "FOO"))
|
|
||||||
(cl-test "keyword: type" (cl-tok-types ":foo") (list "keyword"))
|
|
||||||
(cl-test "keyword: upcase" (cl-tok-values ":hello-world") (list "HELLO-WORLD"))
|
|
||||||
(cl-test "keyword: multiple" (cl-tok-types ":a :b :c") (list "keyword" "keyword" "keyword"))
|
|
||||||
|
|
||||||
;; ── integers ──────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "integer: zero" (cl-tok-values "0") (list "0"))
|
|
||||||
(cl-test "integer: positive" (cl-tok-values "42") (list "42"))
|
|
||||||
(cl-test "integer: negative" (cl-tok-values "-5") (list "-5"))
|
|
||||||
(cl-test "integer: positive-sign" (cl-tok-values "+3") (list "+3"))
|
|
||||||
(cl-test "integer: type" (cl-tok-types "42") (list "integer"))
|
|
||||||
(cl-test "integer: multi-digit" (cl-tok-values "12345678") (list "12345678"))
|
|
||||||
|
|
||||||
;; ── hex, binary, octal ───────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "hex: lowercase x" (cl-tok-values "#xFF") (list "#xFF"))
|
|
||||||
(cl-test "hex: uppercase X" (cl-tok-values "#XFF") (list "#XFF"))
|
|
||||||
(cl-test "hex: type" (cl-tok-types "#xFF") (list "integer"))
|
|
||||||
(cl-test "hex: zero" (cl-tok-values "#x0") (list "#x0"))
|
|
||||||
(cl-test "binary: #b" (cl-tok-values "#b1010") (list "#b1010"))
|
|
||||||
(cl-test "binary: type" (cl-tok-types "#b1010") (list "integer"))
|
|
||||||
(cl-test "octal: #o" (cl-tok-values "#o17") (list "#o17"))
|
|
||||||
(cl-test "octal: type" (cl-tok-types "#o17") (list "integer"))
|
|
||||||
|
|
||||||
;; ── floats ────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "float: basic" (cl-tok-values "3.14") (list "3.14"))
|
|
||||||
(cl-test "float: type" (cl-tok-types "3.14") (list "float"))
|
|
||||||
(cl-test "float: negative" (cl-tok-values "-2.5") (list "-2.5"))
|
|
||||||
(cl-test "float: exponent" (cl-tok-values "1.0e10") (list "1.0e10"))
|
|
||||||
(cl-test "float: neg exponent" (cl-tok-values "1.5e-3") (list "1.5e-3"))
|
|
||||||
(cl-test "float: leading dot" (cl-tok-values ".5") (list "0.5"))
|
|
||||||
(cl-test "float: exp only" (cl-tok-values "1e5") (list "1e5"))
|
|
||||||
|
|
||||||
;; ── ratios ────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "ratio: 1/3" (cl-tok-values "1/3") (list "1/3"))
|
|
||||||
(cl-test "ratio: type" (cl-tok-types "1/3") (list "ratio"))
|
|
||||||
(cl-test "ratio: 22/7" (cl-tok-values "22/7") (list "22/7"))
|
|
||||||
(cl-test "ratio: negative" (cl-tok-values "-1/2") (list "-1/2"))
|
|
||||||
|
|
||||||
;; ── strings ───────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "string: empty" (cl-tok-values "\"\"") (list ""))
|
|
||||||
(cl-test "string: basic" (cl-tok-values "\"hello\"") (list "hello"))
|
|
||||||
(cl-test "string: type" (cl-tok-types "\"hello\"") (list "string"))
|
|
||||||
(cl-test "string: with space" (cl-tok-values "\"hello world\"") (list "hello world"))
|
|
||||||
(cl-test "string: escaped quote" (cl-tok-values "\"say \\\"hi\\\"\"") (list "say \"hi\""))
|
|
||||||
(cl-test "string: escaped backslash" (cl-tok-values "\"a\\\\b\"") (list "a\\b"))
|
|
||||||
(cl-test "string: newline escape" (cl-tok-values "\"a\\nb\"") (list "a\nb"))
|
|
||||||
(cl-test "string: tab escape" (cl-tok-values "\"a\\tb\"") (list "a\tb"))
|
|
||||||
|
|
||||||
;; ── characters ────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "char: lowercase a" (cl-tok-values "#\\a") (list "a"))
|
|
||||||
(cl-test "char: uppercase A" (cl-tok-values "#\\A") (list "A"))
|
|
||||||
(cl-test "char: digit" (cl-tok-values "#\\1") (list "1"))
|
|
||||||
(cl-test "char: type" (cl-tok-types "#\\a") (list "char"))
|
|
||||||
(cl-test "char: Space" (cl-tok-values "#\\Space") (list " "))
|
|
||||||
(cl-test "char: Newline" (cl-tok-values "#\\Newline") (list "\n"))
|
|
||||||
(cl-test "char: Tab" (cl-tok-values "#\\Tab") (list "\t"))
|
|
||||||
(cl-test "char: Return" (cl-tok-values "#\\Return") (list "\r"))
|
|
||||||
|
|
||||||
;; ── reader macros ─────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "quote: type" (cl-tok-types "'x") (list "quote" "symbol"))
|
|
||||||
(cl-test "backquote: type" (cl-tok-types "`x") (list "backquote" "symbol"))
|
|
||||||
(cl-test "comma: type" (cl-tok-types ",x") (list "comma" "symbol"))
|
|
||||||
(cl-test "comma-at: type" (cl-tok-types ",@x") (list "comma-at" "symbol"))
|
|
||||||
(cl-test "hash-quote: type" (cl-tok-types "#'foo") (list "hash-quote" "symbol"))
|
|
||||||
(cl-test "hash-paren: type" (cl-tok-types "#(1 2)") (list "hash-paren" "integer" "integer" "rparen"))
|
|
||||||
|
|
||||||
;; ── uninterned ────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "uninterned: type" (cl-tok-types "#:foo") (list "uninterned"))
|
|
||||||
(cl-test "uninterned: value upcase" (cl-tok-values "#:foo") (list "FOO"))
|
|
||||||
(cl-test "uninterned: compound" (cl-tok-values "#:my-sym") (list "MY-SYM"))
|
|
||||||
|
|
||||||
;; ── parens and structure ──────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "paren: empty list" (cl-tok-types "()") (list "lparen" "rparen"))
|
|
||||||
(cl-test "paren: nested" (cl-tok-types "((a))") (list "lparen" "lparen" "symbol" "rparen" "rparen"))
|
|
||||||
(cl-test "dot: standalone" (cl-tok-types "(a . b)") (list "lparen" "symbol" "dot" "symbol" "rparen"))
|
|
||||||
|
|
||||||
;; ── comments ──────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test "comment: line" (cl-tok-types "; comment\nfoo") (list "symbol"))
|
|
||||||
(cl-test "comment: inline" (cl-tok-values "foo ; bar\nbaz") (list "FOO" "BAZ"))
|
|
||||||
(cl-test "block-comment: basic" (cl-tok-types "#| hello |# foo") (list "symbol"))
|
|
||||||
(cl-test "block-comment: nested" (cl-tok-types "#| a #| b |# c |# x") (list "symbol"))
|
|
||||||
|
|
||||||
;; ── combined ──────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
(cl-test
|
|
||||||
"combined: defun skeleton"
|
|
||||||
(cl-tok-types "(defun foo (x) x)")
|
|
||||||
(list "lparen" "symbol" "symbol" "lparen" "symbol" "rparen" "symbol" "rparen"))
|
|
||||||
|
|
||||||
(cl-test
|
|
||||||
"combined: let form"
|
|
||||||
(cl-tok-types "(let ((x 1)) x)")
|
|
||||||
(list
|
|
||||||
"lparen"
|
|
||||||
"symbol"
|
|
||||||
"lparen"
|
|
||||||
"lparen"
|
|
||||||
"symbol"
|
|
||||||
"integer"
|
|
||||||
"rparen"
|
|
||||||
"rparen"
|
|
||||||
"symbol"
|
|
||||||
"rparen"))
|
|
||||||
|
|
||||||
(cl-test
|
|
||||||
"combined: whitespace skip"
|
|
||||||
(cl-tok-values " foo bar baz ")
|
|
||||||
(list "FOO" "BAR" "BAZ"))
|
|
||||||
|
|
||||||
(cl-test "eof: present" (get (nth (cl-tokenize "") 0) "type") "eof")
|
|
||||||
(cl-test "eof: at end of tokens" (get (nth (cl-tokenize "x") 1) "type") "eof")
|
|
||||||
14
lib/forth/ans-tests/README.md
Normal file
14
lib/forth/ans-tests/README.md
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
ANS Forth conformance tests — vendored from
|
||||||
|
https://github.com/gerryjackson/forth2012-test-suite (master, commit-locked
|
||||||
|
on first fetch: 2026-04-24).
|
||||||
|
|
||||||
|
Files in this directory are pristine copies of upstream — do not edit them.
|
||||||
|
They are consumed by the conformance runner in `lib/forth/conformance.sh`.
|
||||||
|
|
||||||
|
- `tester.fr` — John Hayes' test harness (`T{ ... -> ... }T`). (C) 1995
|
||||||
|
Johns Hopkins APL, distributable under its notice.
|
||||||
|
- `core.fr` — Core word set tests (Hayes, ~1000 lines).
|
||||||
|
- `coreexttest.fth` — Core Extension tests (Gerry Jackson).
|
||||||
|
|
||||||
|
Only `core.fr` is expected to run green end-to-end for Phase 3; the others
|
||||||
|
stay parked until later phases.
|
||||||
1009
lib/forth/ans-tests/core.fr
Normal file
1009
lib/forth/ans-tests/core.fr
Normal file
File diff suppressed because it is too large
Load Diff
775
lib/forth/ans-tests/coreexttest.fth
Normal file
775
lib/forth/ans-tests/coreexttest.fth
Normal file
@@ -0,0 +1,775 @@
|
|||||||
|
\ To test the ANS Forth Core Extension word set
|
||||||
|
|
||||||
|
\ This program was written by Gerry Jackson in 2006, with contributions from
|
||||||
|
\ others where indicated, and is in the public domain - it can be distributed
|
||||||
|
\ and/or modified in any way but please retain this notice.
|
||||||
|
|
||||||
|
\ This program is distributed in the hope that it will be useful,
|
||||||
|
\ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
\ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
|
||||||
|
\ The tests are not claimed to be comprehensive or correct
|
||||||
|
|
||||||
|
\ ------------------------------------------------------------------------------
|
||||||
|
\ Version 0.15 1 August 2025 Added two tests to VALUE
|
||||||
|
\ 0.14 21 July 2022 Updated first line of BUFFER: test as recommended
|
||||||
|
\ in issue 32
|
||||||
|
\ 0.13 28 October 2015
|
||||||
|
\ Replace <FALSE> and <TRUE> with FALSE and TRUE to avoid
|
||||||
|
\ dependence on Core tests
|
||||||
|
\ Moved SAVE-INPUT and RESTORE-INPUT tests in a file to filetest.fth
|
||||||
|
\ Use of 2VARIABLE (from optional wordset) replaced with CREATE.
|
||||||
|
\ Minor lower to upper case conversions.
|
||||||
|
\ Calls to COMPARE replaced by S= (in utilities.fth) to avoid use
|
||||||
|
\ of a word from an optional word set.
|
||||||
|
\ UNUSED tests revised as UNUSED UNUSED = may return FALSE when an
|
||||||
|
\ implementation has the data stack sharing unused dataspace.
|
||||||
|
\ Double number input dependency removed from the HOLDS tests.
|
||||||
|
\ Minor case sensitivities removed in definition names.
|
||||||
|
\ 0.11 25 April 2015
|
||||||
|
\ Added tests for PARSE-NAME HOLDS BUFFER:
|
||||||
|
\ S\" tests added
|
||||||
|
\ DEFER IS ACTION-OF DEFER! DEFER@ tests added
|
||||||
|
\ Empty CASE statement test added
|
||||||
|
\ [COMPILE] tests removed because it is obsolescent in Forth 2012
|
||||||
|
\ 0.10 1 August 2014
|
||||||
|
\ Added tests contributed by James Bowman for:
|
||||||
|
\ <> U> 0<> 0> NIP TUCK ROLL PICK 2>R 2R@ 2R>
|
||||||
|
\ HEX WITHIN UNUSED AGAIN MARKER
|
||||||
|
\ Added tests for:
|
||||||
|
\ .R U.R ERASE PAD REFILL SOURCE-ID
|
||||||
|
\ Removed ABORT from NeverExecuted to enable Win32
|
||||||
|
\ to continue after failure of RESTORE-INPUT.
|
||||||
|
\ Removed max-intx which is no longer used.
|
||||||
|
\ 0.7 6 June 2012 Extra CASE test added
|
||||||
|
\ 0.6 1 April 2012 Tests placed in the public domain.
|
||||||
|
\ SAVE-INPUT & RESTORE-INPUT tests, position
|
||||||
|
\ of T{ moved so that tests work with ttester.fs
|
||||||
|
\ CONVERT test deleted - obsolete word removed from Forth 200X
|
||||||
|
\ IMMEDIATE VALUEs tested
|
||||||
|
\ RECURSE with :NONAME tested
|
||||||
|
\ PARSE and .( tested
|
||||||
|
\ Parsing behaviour of C" added
|
||||||
|
\ 0.5 14 September 2011 Removed the double [ELSE] from the
|
||||||
|
\ initial SAVE-INPUT & RESTORE-INPUT test
|
||||||
|
\ 0.4 30 November 2009 max-int replaced with max-intx to
|
||||||
|
\ avoid redefinition warnings.
|
||||||
|
\ 0.3 6 March 2009 { and } replaced with T{ and }T
|
||||||
|
\ CONVERT test now independent of cell size
|
||||||
|
\ 0.2 20 April 2007 ANS Forth words changed to upper case
|
||||||
|
\ Tests qd3 to qd6 by Reinhold Straub
|
||||||
|
\ 0.1 Oct 2006 First version released
|
||||||
|
\ -----------------------------------------------------------------------------
|
||||||
|
\ The tests are based on John Hayes test program for the core word set
|
||||||
|
|
||||||
|
\ Words tested in this file are:
|
||||||
|
\ .( .R 0<> 0> 2>R 2R> 2R@ :NONAME <> ?DO AGAIN C" CASE COMPILE, ENDCASE
|
||||||
|
\ ENDOF ERASE FALSE HEX MARKER NIP OF PAD PARSE PICK REFILL
|
||||||
|
\ RESTORE-INPUT ROLL SAVE-INPUT SOURCE-ID TO TRUE TUCK U.R U> UNUSED
|
||||||
|
\ VALUE WITHIN [COMPILE]
|
||||||
|
|
||||||
|
\ Words not tested or partially tested:
|
||||||
|
\ \ because it has been extensively used already and is, hence, unnecessary
|
||||||
|
\ REFILL and SOURCE-ID from the user input device which are not possible
|
||||||
|
\ when testing from a file such as this one
|
||||||
|
\ UNUSED (partially tested) as the value returned is system dependent
|
||||||
|
\ Obsolescent words #TIB CONVERT EXPECT QUERY SPAN TIB as they have been
|
||||||
|
\ removed from the Forth 2012 standard
|
||||||
|
|
||||||
|
\ Results from words that output to the user output device have to visually
|
||||||
|
\ checked for correctness. These are .R U.R .(
|
||||||
|
|
||||||
|
\ -----------------------------------------------------------------------------
|
||||||
|
\ Assumptions & dependencies:
|
||||||
|
\ - tester.fr (or ttester.fs), errorreport.fth and utilities.fth have been
|
||||||
|
\ included prior to this file
|
||||||
|
\ - the Core word set available
|
||||||
|
\ -----------------------------------------------------------------------------
|
||||||
|
TESTING Core Extension words
|
||||||
|
|
||||||
|
DECIMAL
|
||||||
|
|
||||||
|
TESTING TRUE FALSE
|
||||||
|
|
||||||
|
T{ TRUE -> 0 INVERT }T
|
||||||
|
T{ FALSE -> 0 }T
|
||||||
|
|
||||||
|
\ -----------------------------------------------------------------------------
|
||||||
|
TESTING <> U> (contributed by James Bowman)
|
||||||
|
|
||||||
|
T{ 0 0 <> -> FALSE }T
|
||||||
|
T{ 1 1 <> -> FALSE }T
|
||||||
|
T{ -1 -1 <> -> FALSE }T
|
||||||
|
T{ 1 0 <> -> TRUE }T
|
||||||
|
T{ -1 0 <> -> TRUE }T
|
||||||
|
T{ 0 1 <> -> TRUE }T
|
||||||
|
T{ 0 -1 <> -> TRUE }T
|
||||||
|
|
||||||
|
T{ 0 1 U> -> FALSE }T
|
||||||
|
T{ 1 2 U> -> FALSE }T
|
||||||
|
T{ 0 MID-UINT U> -> FALSE }T
|
||||||
|
T{ 0 MAX-UINT U> -> FALSE }T
|
||||||
|
T{ MID-UINT MAX-UINT U> -> FALSE }T
|
||||||
|
T{ 0 0 U> -> FALSE }T
|
||||||
|
T{ 1 1 U> -> FALSE }T
|
||||||
|
T{ 1 0 U> -> TRUE }T
|
||||||
|
T{ 2 1 U> -> TRUE }T
|
||||||
|
T{ MID-UINT 0 U> -> TRUE }T
|
||||||
|
T{ MAX-UINT 0 U> -> TRUE }T
|
||||||
|
T{ MAX-UINT MID-UINT U> -> TRUE }T
|
||||||
|
|
||||||
|
\ -----------------------------------------------------------------------------
|
||||||
|
TESTING 0<> 0> (contributed by James Bowman)
|
||||||
|
|
||||||
|
T{ 0 0<> -> FALSE }T
|
||||||
|
T{ 1 0<> -> TRUE }T
|
||||||
|
T{ 2 0<> -> TRUE }T
|
||||||
|
T{ -1 0<> -> TRUE }T
|
||||||
|
T{ MAX-UINT 0<> -> TRUE }T
|
||||||
|
T{ MIN-INT 0<> -> TRUE }T
|
||||||
|
T{ MAX-INT 0<> -> TRUE }T
|
||||||
|
|
||||||
|
T{ 0 0> -> FALSE }T
|
||||||
|
T{ -1 0> -> FALSE }T
|
||||||
|
T{ MIN-INT 0> -> FALSE }T
|
||||||
|
T{ 1 0> -> TRUE }T
|
||||||
|
T{ MAX-INT 0> -> TRUE }T
|
||||||
|
|
||||||
|
\ -----------------------------------------------------------------------------
|
||||||
|
TESTING NIP TUCK ROLL PICK (contributed by James Bowman)
|
||||||
|
|
||||||
|
T{ 1 2 NIP -> 2 }T
|
||||||
|
T{ 1 2 3 NIP -> 1 3 }T
|
||||||
|
|
||||||
|
T{ 1 2 TUCK -> 2 1 2 }T
|
||||||
|
T{ 1 2 3 TUCK -> 1 3 2 3 }T
|
||||||
|
|
||||||
|
T{ : RO5 100 200 300 400 500 ; -> }T
|
||||||
|
T{ RO5 3 ROLL -> 100 300 400 500 200 }T
|
||||||
|
T{ RO5 2 ROLL -> RO5 ROT }T
|
||||||
|
T{ RO5 1 ROLL -> RO5 SWAP }T
|
||||||
|
T{ RO5 0 ROLL -> RO5 }T
|
||||||
|
|
||||||
|
T{ RO5 2 PICK -> 100 200 300 400 500 300 }T
|
||||||
|
T{ RO5 1 PICK -> RO5 OVER }T
|
||||||
|
T{ RO5 0 PICK -> RO5 DUP }T
|
||||||
|
|
||||||
|
\ -----------------------------------------------------------------------------
|
||||||
|
TESTING 2>R 2R@ 2R> (contributed by James Bowman)
|
||||||
|
|
||||||
|
T{ : RR0 2>R 100 R> R> ; -> }T
|
||||||
|
T{ 300 400 RR0 -> 100 400 300 }T
|
||||||
|
T{ 200 300 400 RR0 -> 200 100 400 300 }T
|
||||||
|
|
||||||
|
T{ : RR1 2>R 100 2R@ R> R> ; -> }T
|
||||||
|
T{ 300 400 RR1 -> 100 300 400 400 300 }T
|
||||||
|
T{ 200 300 400 RR1 -> 200 100 300 400 400 300 }T
|
||||||
|
|
||||||
|
T{ : RR2 2>R 100 2R> ; -> }T
|
||||||
|
T{ 300 400 RR2 -> 100 300 400 }T
|
||||||
|
T{ 200 300 400 RR2 -> 200 100 300 400 }T
|
||||||
|
|
||||||
|
\ -----------------------------------------------------------------------------
|
||||||
|
TESTING HEX (contributed by James Bowman)
|
||||||
|
|
||||||
|
T{ BASE @ HEX BASE @ DECIMAL BASE @ - SWAP BASE ! -> 6 }T
|
||||||
|
|
||||||
|
\ -----------------------------------------------------------------------------
|
||||||
|
TESTING WITHIN (contributed by James Bowman)
|
||||||
|
|
||||||
|
T{ 0 0 0 WITHIN -> FALSE }T
|
||||||
|
T{ 0 0 MID-UINT WITHIN -> TRUE }T
|
||||||
|
T{ 0 0 MID-UINT+1 WITHIN -> TRUE }T
|
||||||
|
T{ 0 0 MAX-UINT WITHIN -> TRUE }T
|
||||||
|
T{ 0 MID-UINT 0 WITHIN -> FALSE }T
|
||||||
|
T{ 0 MID-UINT MID-UINT WITHIN -> FALSE }T
|
||||||
|
T{ 0 MID-UINT MID-UINT+1 WITHIN -> FALSE }T
|
||||||
|
T{ 0 MID-UINT MAX-UINT WITHIN -> FALSE }T
|
||||||
|
T{ 0 MID-UINT+1 0 WITHIN -> FALSE }T
|
||||||
|
T{ 0 MID-UINT+1 MID-UINT WITHIN -> TRUE }T
|
||||||
|
T{ 0 MID-UINT+1 MID-UINT+1 WITHIN -> FALSE }T
|
||||||
|
T{ 0 MID-UINT+1 MAX-UINT WITHIN -> FALSE }T
|
||||||
|
T{ 0 MAX-UINT 0 WITHIN -> FALSE }T
|
||||||
|
T{ 0 MAX-UINT MID-UINT WITHIN -> TRUE }T
|
||||||
|
T{ 0 MAX-UINT MID-UINT+1 WITHIN -> TRUE }T
|
||||||
|
T{ 0 MAX-UINT MAX-UINT WITHIN -> FALSE }T
|
||||||
|
T{ MID-UINT 0 0 WITHIN -> FALSE }T
|
||||||
|
T{ MID-UINT 0 MID-UINT WITHIN -> FALSE }T
|
||||||
|
T{ MID-UINT 0 MID-UINT+1 WITHIN -> TRUE }T
|
||||||
|
T{ MID-UINT 0 MAX-UINT WITHIN -> TRUE }T
|
||||||
|
T{ MID-UINT MID-UINT 0 WITHIN -> TRUE }T
|
||||||
|
T{ MID-UINT MID-UINT MID-UINT WITHIN -> FALSE }T
|
||||||
|
T{ MID-UINT MID-UINT MID-UINT+1 WITHIN -> TRUE }T
|
||||||
|
T{ MID-UINT MID-UINT MAX-UINT WITHIN -> TRUE }T
|
||||||
|
T{ MID-UINT MID-UINT+1 0 WITHIN -> FALSE }T
|
||||||
|
T{ MID-UINT MID-UINT+1 MID-UINT WITHIN -> FALSE }T
|
||||||
|
T{ MID-UINT MID-UINT+1 MID-UINT+1 WITHIN -> FALSE }T
|
||||||
|
T{ MID-UINT MID-UINT+1 MAX-UINT WITHIN -> FALSE }T
|
||||||
|
T{ MID-UINT MAX-UINT 0 WITHIN -> FALSE }T
|
||||||
|
T{ MID-UINT MAX-UINT MID-UINT WITHIN -> FALSE }T
|
||||||
|
T{ MID-UINT MAX-UINT MID-UINT+1 WITHIN -> TRUE }T
|
||||||
|
T{ MID-UINT MAX-UINT MAX-UINT WITHIN -> FALSE }T
|
||||||
|
T{ MID-UINT+1 0 0 WITHIN -> FALSE }T
|
||||||
|
T{ MID-UINT+1 0 MID-UINT WITHIN -> FALSE }T
|
||||||
|
T{ MID-UINT+1 0 MID-UINT+1 WITHIN -> FALSE }T
|
||||||
|
T{ MID-UINT+1 0 MAX-UINT WITHIN -> TRUE }T
|
||||||
|
T{ MID-UINT+1 MID-UINT 0 WITHIN -> TRUE }T
|
||||||
|
T{ MID-UINT+1 MID-UINT MID-UINT WITHIN -> FALSE }T
|
||||||
|
T{ MID-UINT+1 MID-UINT MID-UINT+1 WITHIN -> FALSE }T
|
||||||
|
T{ MID-UINT+1 MID-UINT MAX-UINT WITHIN -> TRUE }T
|
||||||
|
T{ MID-UINT+1 MID-UINT+1 0 WITHIN -> TRUE }T
|
||||||
|
T{ MID-UINT+1 MID-UINT+1 MID-UINT WITHIN -> TRUE }T
|
||||||
|
T{ MID-UINT+1 MID-UINT+1 MID-UINT+1 WITHIN -> FALSE }T
|
||||||
|
T{ MID-UINT+1 MID-UINT+1 MAX-UINT WITHIN -> TRUE }T
|
||||||
|
T{ MID-UINT+1 MAX-UINT 0 WITHIN -> FALSE }T
|
||||||
|
T{ MID-UINT+1 MAX-UINT MID-UINT WITHIN -> FALSE }T
|
||||||
|
T{ MID-UINT+1 MAX-UINT MID-UINT+1 WITHIN -> FALSE }T
|
||||||
|
T{ MID-UINT+1 MAX-UINT MAX-UINT WITHIN -> FALSE }T
|
||||||
|
T{ MAX-UINT 0 0 WITHIN -> FALSE }T
|
||||||
|
T{ MAX-UINT 0 MID-UINT WITHIN -> FALSE }T
|
||||||
|
T{ MAX-UINT 0 MID-UINT+1 WITHIN -> FALSE }T
|
||||||
|
T{ MAX-UINT 0 MAX-UINT WITHIN -> FALSE }T
|
||||||
|
T{ MAX-UINT MID-UINT 0 WITHIN -> TRUE }T
|
||||||
|
T{ MAX-UINT MID-UINT MID-UINT WITHIN -> FALSE }T
|
||||||
|
T{ MAX-UINT MID-UINT MID-UINT+1 WITHIN -> FALSE }T
|
||||||
|
T{ MAX-UINT MID-UINT MAX-UINT WITHIN -> FALSE }T
|
||||||
|
T{ MAX-UINT MID-UINT+1 0 WITHIN -> TRUE }T
|
||||||
|
T{ MAX-UINT MID-UINT+1 MID-UINT WITHIN -> TRUE }T
|
||||||
|
T{ MAX-UINT MID-UINT+1 MID-UINT+1 WITHIN -> FALSE }T
|
||||||
|
T{ MAX-UINT MID-UINT+1 MAX-UINT WITHIN -> FALSE }T
|
||||||
|
T{ MAX-UINT MAX-UINT 0 WITHIN -> TRUE }T
|
||||||
|
T{ MAX-UINT MAX-UINT MID-UINT WITHIN -> TRUE }T
|
||||||
|
T{ MAX-UINT MAX-UINT MID-UINT+1 WITHIN -> TRUE }T
|
||||||
|
T{ MAX-UINT MAX-UINT MAX-UINT WITHIN -> FALSE }T
|
||||||
|
|
||||||
|
T{ MIN-INT MIN-INT MIN-INT WITHIN -> FALSE }T
|
||||||
|
T{ MIN-INT MIN-INT 0 WITHIN -> TRUE }T
|
||||||
|
T{ MIN-INT MIN-INT 1 WITHIN -> TRUE }T
|
||||||
|
T{ MIN-INT MIN-INT MAX-INT WITHIN -> TRUE }T
|
||||||
|
T{ MIN-INT 0 MIN-INT WITHIN -> FALSE }T
|
||||||
|
T{ MIN-INT 0 0 WITHIN -> FALSE }T
|
||||||
|
T{ MIN-INT 0 1 WITHIN -> FALSE }T
|
||||||
|
T{ MIN-INT 0 MAX-INT WITHIN -> FALSE }T
|
||||||
|
T{ MIN-INT 1 MIN-INT WITHIN -> FALSE }T
|
||||||
|
T{ MIN-INT 1 0 WITHIN -> TRUE }T
|
||||||
|
T{ MIN-INT 1 1 WITHIN -> FALSE }T
|
||||||
|
T{ MIN-INT 1 MAX-INT WITHIN -> FALSE }T
|
||||||
|
T{ MIN-INT MAX-INT MIN-INT WITHIN -> FALSE }T
|
||||||
|
T{ MIN-INT MAX-INT 0 WITHIN -> TRUE }T
|
||||||
|
T{ MIN-INT MAX-INT 1 WITHIN -> TRUE }T
|
||||||
|
T{ MIN-INT MAX-INT MAX-INT WITHIN -> FALSE }T
|
||||||
|
T{ 0 MIN-INT MIN-INT WITHIN -> FALSE }T
|
||||||
|
T{ 0 MIN-INT 0 WITHIN -> FALSE }T
|
||||||
|
T{ 0 MIN-INT 1 WITHIN -> TRUE }T
|
||||||
|
T{ 0 MIN-INT MAX-INT WITHIN -> TRUE }T
|
||||||
|
T{ 0 0 MIN-INT WITHIN -> TRUE }T
|
||||||
|
T{ 0 0 0 WITHIN -> FALSE }T
|
||||||
|
T{ 0 0 1 WITHIN -> TRUE }T
|
||||||
|
T{ 0 0 MAX-INT WITHIN -> TRUE }T
|
||||||
|
T{ 0 1 MIN-INT WITHIN -> FALSE }T
|
||||||
|
T{ 0 1 0 WITHIN -> FALSE }T
|
||||||
|
T{ 0 1 1 WITHIN -> FALSE }T
|
||||||
|
T{ 0 1 MAX-INT WITHIN -> FALSE }T
|
||||||
|
T{ 0 MAX-INT MIN-INT WITHIN -> FALSE }T
|
||||||
|
T{ 0 MAX-INT 0 WITHIN -> FALSE }T
|
||||||
|
T{ 0 MAX-INT 1 WITHIN -> TRUE }T
|
||||||
|
T{ 0 MAX-INT MAX-INT WITHIN -> FALSE }T
|
||||||
|
T{ 1 MIN-INT MIN-INT WITHIN -> FALSE }T
|
||||||
|
T{ 1 MIN-INT 0 WITHIN -> FALSE }T
|
||||||
|
T{ 1 MIN-INT 1 WITHIN -> FALSE }T
|
||||||
|
T{ 1 MIN-INT MAX-INT WITHIN -> TRUE }T
|
||||||
|
T{ 1 0 MIN-INT WITHIN -> TRUE }T
|
||||||
|
T{ 1 0 0 WITHIN -> FALSE }T
|
||||||
|
T{ 1 0 1 WITHIN -> FALSE }T
|
||||||
|
T{ 1 0 MAX-INT WITHIN -> TRUE }T
|
||||||
|
T{ 1 1 MIN-INT WITHIN -> TRUE }T
|
||||||
|
T{ 1 1 0 WITHIN -> TRUE }T
|
||||||
|
T{ 1 1 1 WITHIN -> FALSE }T
|
||||||
|
T{ 1 1 MAX-INT WITHIN -> TRUE }T
|
||||||
|
T{ 1 MAX-INT MIN-INT WITHIN -> FALSE }T
|
||||||
|
T{ 1 MAX-INT 0 WITHIN -> FALSE }T
|
||||||
|
T{ 1 MAX-INT 1 WITHIN -> FALSE }T
|
||||||
|
T{ 1 MAX-INT MAX-INT WITHIN -> FALSE }T
|
||||||
|
T{ MAX-INT MIN-INT MIN-INT WITHIN -> FALSE }T
|
||||||
|
T{ MAX-INT MIN-INT 0 WITHIN -> FALSE }T
|
||||||
|
T{ MAX-INT MIN-INT 1 WITHIN -> FALSE }T
|
||||||
|
T{ MAX-INT MIN-INT MAX-INT WITHIN -> FALSE }T
|
||||||
|
T{ MAX-INT 0 MIN-INT WITHIN -> TRUE }T
|
||||||
|
T{ MAX-INT 0 0 WITHIN -> FALSE }T
|
||||||
|
T{ MAX-INT 0 1 WITHIN -> FALSE }T
|
||||||
|
T{ MAX-INT 0 MAX-INT WITHIN -> FALSE }T
|
||||||
|
T{ MAX-INT 1 MIN-INT WITHIN -> TRUE }T
|
||||||
|
T{ MAX-INT 1 0 WITHIN -> TRUE }T
|
||||||
|
T{ MAX-INT 1 1 WITHIN -> FALSE }T
|
||||||
|
T{ MAX-INT 1 MAX-INT WITHIN -> FALSE }T
|
||||||
|
T{ MAX-INT MAX-INT MIN-INT WITHIN -> TRUE }T
|
||||||
|
T{ MAX-INT MAX-INT 0 WITHIN -> TRUE }T
|
||||||
|
T{ MAX-INT MAX-INT 1 WITHIN -> TRUE }T
|
||||||
|
T{ MAX-INT MAX-INT MAX-INT WITHIN -> FALSE }T
|
||||||
|
|
||||||
|
\ -----------------------------------------------------------------------------
|
||||||
|
TESTING UNUSED (contributed by James Bowman & Peter Knaggs)
|
||||||
|
|
||||||
|
VARIABLE UNUSED0
|
||||||
|
T{ UNUSED DROP -> }T
|
||||||
|
T{ ALIGN UNUSED UNUSED0 ! 0 , UNUSED CELL+ UNUSED0 @ = -> TRUE }T
|
||||||
|
T{ UNUSED UNUSED0 ! 0 C, UNUSED CHAR+ UNUSED0 @ =
|
||||||
|
-> TRUE }T \ aligned -> unaligned
|
||||||
|
T{ UNUSED UNUSED0 ! 0 C, UNUSED CHAR+ UNUSED0 @ = -> TRUE }T \ unaligned -> ?
|
||||||
|
|
||||||
|
\ -----------------------------------------------------------------------------
|
||||||
|
TESTING AGAIN (contributed by James Bowman)
|
||||||
|
|
||||||
|
T{ : AG0 701 BEGIN DUP 7 MOD 0= IF EXIT THEN 1+ AGAIN ; -> }T
|
||||||
|
T{ AG0 -> 707 }T
|
||||||
|
|
||||||
|
\ -----------------------------------------------------------------------------
|
||||||
|
TESTING MARKER (contributed by James Bowman)
|
||||||
|
|
||||||
|
T{ : MA? BL WORD FIND NIP 0<> ; -> }T
|
||||||
|
T{ MARKER MA0 -> }T
|
||||||
|
T{ : MA1 111 ; -> }T
|
||||||
|
T{ MARKER MA2 -> }T
|
||||||
|
T{ : MA1 222 ; -> }T
|
||||||
|
T{ MA? MA0 MA? MA1 MA? MA2 -> TRUE TRUE TRUE }T
|
||||||
|
T{ MA1 MA2 MA1 -> 222 111 }T
|
||||||
|
T{ MA? MA0 MA? MA1 MA? MA2 -> TRUE TRUE FALSE }T
|
||||||
|
T{ MA0 -> }T
|
||||||
|
T{ MA? MA0 MA? MA1 MA? MA2 -> FALSE FALSE FALSE }T
|
||||||
|
|
||||||
|
\ -----------------------------------------------------------------------------
|
||||||
|
TESTING ?DO
|
||||||
|
|
||||||
|
: QD ?DO I LOOP ;
|
||||||
|
T{ 789 789 QD -> }T
|
||||||
|
T{ -9876 -9876 QD -> }T
|
||||||
|
T{ 5 0 QD -> 0 1 2 3 4 }T
|
||||||
|
|
||||||
|
: QD1 ?DO I 10 +LOOP ;
|
||||||
|
T{ 50 1 QD1 -> 1 11 21 31 41 }T
|
||||||
|
T{ 50 0 QD1 -> 0 10 20 30 40 }T
|
||||||
|
|
||||||
|
: QD2 ?DO I 3 > IF LEAVE ELSE I THEN LOOP ;
|
||||||
|
T{ 5 -1 QD2 -> -1 0 1 2 3 }T
|
||||||
|
|
||||||
|
: QD3 ?DO I 1 +LOOP ;
|
||||||
|
T{ 4 4 QD3 -> }T
|
||||||
|
T{ 4 1 QD3 -> 1 2 3 }T
|
||||||
|
T{ 2 -1 QD3 -> -1 0 1 }T
|
||||||
|
|
||||||
|
: QD4 ?DO I -1 +LOOP ;
|
||||||
|
T{ 4 4 QD4 -> }T
|
||||||
|
T{ 1 4 QD4 -> 4 3 2 1 }T
|
||||||
|
T{ -1 2 QD4 -> 2 1 0 -1 }T
|
||||||
|
|
||||||
|
: QD5 ?DO I -10 +LOOP ;
|
||||||
|
T{ 1 50 QD5 -> 50 40 30 20 10 }T
|
||||||
|
T{ 0 50 QD5 -> 50 40 30 20 10 0 }T
|
||||||
|
T{ -25 10 QD5 -> 10 0 -10 -20 }T
|
||||||
|
|
||||||
|
VARIABLE ITERS
|
||||||
|
VARIABLE INCRMNT
|
||||||
|
|
||||||
|
: QD6 ( limit start increment -- )
|
||||||
|
INCRMNT !
|
||||||
|
0 ITERS !
|
||||||
|
?DO
|
||||||
|
1 ITERS +!
|
||||||
|
I
|
||||||
|
ITERS @ 6 = IF LEAVE THEN
|
||||||
|
INCRMNT @
|
||||||
|
+LOOP ITERS @
|
||||||
|
;
|
||||||
|
|
||||||
|
T{ 4 4 -1 QD6 -> 0 }T
|
||||||
|
T{ 1 4 -1 QD6 -> 4 3 2 1 4 }T
|
||||||
|
T{ 4 1 -1 QD6 -> 1 0 -1 -2 -3 -4 6 }T
|
||||||
|
T{ 4 1 0 QD6 -> 1 1 1 1 1 1 6 }T
|
||||||
|
T{ 0 0 0 QD6 -> 0 }T
|
||||||
|
T{ 1 4 0 QD6 -> 4 4 4 4 4 4 6 }T
|
||||||
|
T{ 1 4 1 QD6 -> 4 5 6 7 8 9 6 }T
|
||||||
|
T{ 4 1 1 QD6 -> 1 2 3 3 }T
|
||||||
|
T{ 4 4 1 QD6 -> 0 }T
|
||||||
|
T{ 2 -1 -1 QD6 -> -1 -2 -3 -4 -5 -6 6 }T
|
||||||
|
T{ -1 2 -1 QD6 -> 2 1 0 -1 4 }T
|
||||||
|
T{ 2 -1 0 QD6 -> -1 -1 -1 -1 -1 -1 6 }T
|
||||||
|
T{ -1 2 0 QD6 -> 2 2 2 2 2 2 6 }T
|
||||||
|
T{ -1 2 1 QD6 -> 2 3 4 5 6 7 6 }T
|
||||||
|
T{ 2 -1 1 QD6 -> -1 0 1 3 }T
|
||||||
|
|
||||||
|
\ -----------------------------------------------------------------------------
|
||||||
|
TESTING BUFFER:
|
||||||
|
|
||||||
|
T{ 2 CELLS BUFFER: BUF:TEST -> }T
|
||||||
|
T{ BUF:TEST DUP ALIGNED = -> TRUE }T
|
||||||
|
T{ 111 BUF:TEST ! 222 BUF:TEST CELL+ ! -> }T
|
||||||
|
T{ BUF:TEST @ BUF:TEST CELL+ @ -> 111 222 }T
|
||||||
|
|
||||||
|
\ -----------------------------------------------------------------------------
|
||||||
|
TESTING VALUE TO
|
||||||
|
|
||||||
|
T{ 111 VALUE VAL1 -999 VALUE VAL2 -> }T
|
||||||
|
T{ VAL1 -> 111 }T
|
||||||
|
T{ VAL2 -> -999 }T
|
||||||
|
T{ 222 TO VAL1 -> }T
|
||||||
|
T{ VAL1 -> 222 }T
|
||||||
|
T{ : VD1 VAL1 ; -> }T
|
||||||
|
T{ VD1 -> 222 }T
|
||||||
|
T{ : VD2 TO VAL2 ; -> }T
|
||||||
|
T{ VAL2 -> -999 }T
|
||||||
|
T{ -333 VD2 -> }T
|
||||||
|
T{ VAL2 -> -333 }T
|
||||||
|
T{ VAL1 -> 222 }T
|
||||||
|
T{ 444 TO VAL1 -> }T
|
||||||
|
T{ VD1 -> 444 }T
|
||||||
|
T{ 123 VALUE VAL3 IMMEDIATE VAL3 -> 123 }T
|
||||||
|
T{ : VD3 VAL3 LITERAL ; VD3 -> 123 }T
|
||||||
|
|
||||||
|
\ -----------------------------------------------------------------------------
|
||||||
|
TESTING CASE OF ENDOF ENDCASE
|
||||||
|
|
||||||
|
: CS1 CASE 1 OF 111 ENDOF
|
||||||
|
2 OF 222 ENDOF
|
||||||
|
3 OF 333 ENDOF
|
||||||
|
>R 999 R>
|
||||||
|
ENDCASE
|
||||||
|
;
|
||||||
|
|
||||||
|
T{ 1 CS1 -> 111 }T
|
||||||
|
T{ 2 CS1 -> 222 }T
|
||||||
|
T{ 3 CS1 -> 333 }T
|
||||||
|
T{ 4 CS1 -> 999 }T
|
||||||
|
|
||||||
|
\ Nested CASE's
|
||||||
|
|
||||||
|
: CS2 >R CASE -1 OF CASE R@ 1 OF 100 ENDOF
|
||||||
|
2 OF 200 ENDOF
|
||||||
|
>R -300 R>
|
||||||
|
ENDCASE
|
||||||
|
ENDOF
|
||||||
|
-2 OF CASE R@ 1 OF -99 ENDOF
|
||||||
|
>R -199 R>
|
||||||
|
ENDCASE
|
||||||
|
ENDOF
|
||||||
|
>R 299 R>
|
||||||
|
ENDCASE R> DROP
|
||||||
|
;
|
||||||
|
|
||||||
|
T{ -1 1 CS2 -> 100 }T
|
||||||
|
T{ -1 2 CS2 -> 200 }T
|
||||||
|
T{ -1 3 CS2 -> -300 }T
|
||||||
|
T{ -2 1 CS2 -> -99 }T
|
||||||
|
T{ -2 2 CS2 -> -199 }T
|
||||||
|
T{ 0 2 CS2 -> 299 }T
|
||||||
|
|
||||||
|
\ Boolean short circuiting using CASE
|
||||||
|
|
||||||
|
: CS3 ( N1 -- N2 )
|
||||||
|
CASE 1- FALSE OF 11 ENDOF
|
||||||
|
1- FALSE OF 22 ENDOF
|
||||||
|
1- FALSE OF 33 ENDOF
|
||||||
|
44 SWAP
|
||||||
|
ENDCASE
|
||||||
|
;
|
||||||
|
|
||||||
|
T{ 1 CS3 -> 11 }T
|
||||||
|
T{ 2 CS3 -> 22 }T
|
||||||
|
T{ 3 CS3 -> 33 }T
|
||||||
|
T{ 9 CS3 -> 44 }T
|
||||||
|
|
||||||
|
\ Empty CASE statements with/without default
|
||||||
|
|
||||||
|
T{ : CS4 CASE ENDCASE ; 1 CS4 -> }T
|
||||||
|
T{ : CS5 CASE 2 SWAP ENDCASE ; 1 CS5 -> 2 }T
|
||||||
|
T{ : CS6 CASE 1 OF ENDOF 2 ENDCASE ; 1 CS6 -> }T
|
||||||
|
T{ : CS7 CASE 3 OF ENDOF 2 ENDCASE ; 1 CS7 -> 1 }T
|
||||||
|
|
||||||
|
\ -----------------------------------------------------------------------------
|
||||||
|
TESTING :NONAME RECURSE
|
||||||
|
|
||||||
|
VARIABLE NN1
|
||||||
|
VARIABLE NN2
|
||||||
|
:NONAME 1234 ; NN1 !
|
||||||
|
:NONAME 9876 ; NN2 !
|
||||||
|
T{ NN1 @ EXECUTE -> 1234 }T
|
||||||
|
T{ NN2 @ EXECUTE -> 9876 }T
|
||||||
|
|
||||||
|
T{ :NONAME ( n -- 0,1,..n ) DUP IF DUP >R 1- RECURSE R> THEN ;
|
||||||
|
CONSTANT RN1 -> }T
|
||||||
|
T{ 0 RN1 EXECUTE -> 0 }T
|
||||||
|
T{ 4 RN1 EXECUTE -> 0 1 2 3 4 }T
|
||||||
|
|
||||||
|
:NONAME ( n -- n1 ) \ Multiple RECURSEs in one definition
|
||||||
|
1- DUP
|
||||||
|
CASE 0 OF EXIT ENDOF
|
||||||
|
1 OF 11 SWAP RECURSE ENDOF
|
||||||
|
2 OF 22 SWAP RECURSE ENDOF
|
||||||
|
3 OF 33 SWAP RECURSE ENDOF
|
||||||
|
DROP ABS RECURSE EXIT
|
||||||
|
ENDCASE
|
||||||
|
; CONSTANT RN2
|
||||||
|
|
||||||
|
T{ 1 RN2 EXECUTE -> 0 }T
|
||||||
|
T{ 2 RN2 EXECUTE -> 11 0 }T
|
||||||
|
T{ 4 RN2 EXECUTE -> 33 22 11 0 }T
|
||||||
|
T{ 25 RN2 EXECUTE -> 33 22 11 0 }T
|
||||||
|
|
||||||
|
\ -----------------------------------------------------------------------------
|
||||||
|
TESTING C"
|
||||||
|
|
||||||
|
T{ : CQ1 C" 123" ; -> }T
|
||||||
|
T{ CQ1 COUNT EVALUATE -> 123 }T
|
||||||
|
T{ : CQ2 C" " ; -> }T
|
||||||
|
T{ CQ2 COUNT EVALUATE -> }T
|
||||||
|
T{ : CQ3 C" 2345"COUNT EVALUATE ; CQ3 -> 2345 }T
|
||||||
|
|
||||||
|
\ -----------------------------------------------------------------------------
|
||||||
|
TESTING COMPILE,
|
||||||
|
|
||||||
|
:NONAME DUP + ; CONSTANT DUP+
|
||||||
|
T{ : Q DUP+ COMPILE, ; -> }T
|
||||||
|
T{ : AS1 [ Q ] ; -> }T
|
||||||
|
T{ 123 AS1 -> 246 }T
|
||||||
|
|
||||||
|
\ -----------------------------------------------------------------------------
|
||||||
|
\ Cannot automatically test SAVE-INPUT and RESTORE-INPUT from a console source
|
||||||
|
|
||||||
|
TESTING SAVE-INPUT and RESTORE-INPUT with a string source
|
||||||
|
|
||||||
|
VARIABLE SI_INC 0 SI_INC !
|
||||||
|
|
||||||
|
: SI1
|
||||||
|
SI_INC @ >IN +!
|
||||||
|
15 SI_INC !
|
||||||
|
;
|
||||||
|
|
||||||
|
: S$ S" SAVE-INPUT SI1 RESTORE-INPUT 12345" ;
|
||||||
|
|
||||||
|
T{ S$ EVALUATE SI_INC @ -> 0 2345 15 }T
|
||||||
|
|
||||||
|
\ -----------------------------------------------------------------------------
|
||||||
|
TESTING .(
|
||||||
|
|
||||||
|
CR CR .( Output from .()
|
||||||
|
T{ CR .( You should see -9876: ) -9876 . -> }T
|
||||||
|
T{ CR .( and again: ).( -9876)CR -> }T
|
||||||
|
|
||||||
|
CR CR .( On the next 2 lines you should see First then Second messages:)
|
||||||
|
T{ : DOTP CR ." Second message via ." [CHAR] " EMIT \ Check .( is immediate
|
||||||
|
[ CR ] .( First message via .( ) ; DOTP -> }T
|
||||||
|
CR CR
|
||||||
|
T{ : IMM? BL WORD FIND NIP ; IMM? .( -> 1 }T
|
||||||
|
|
||||||
|
\ -----------------------------------------------------------------------------
|
||||||
|
TESTING .R and U.R - has to handle different cell sizes
|
||||||
|
|
||||||
|
\ Create some large integers just below/above MAX and Min INTs
|
||||||
|
MAX-INT 73 79 */ CONSTANT LI1
|
||||||
|
MIN-INT 71 73 */ CONSTANT LI2
|
||||||
|
|
||||||
|
LI1 0 <# #S #> NIP CONSTANT LENLI1
|
||||||
|
|
||||||
|
: (.R&U.R) ( u1 u2 -- ) \ u1 <= string length, u2 is required indentation
|
||||||
|
TUCK + >R
|
||||||
|
LI1 OVER SPACES . CR R@ LI1 SWAP .R CR
|
||||||
|
LI2 OVER SPACES . CR R@ 1+ LI2 SWAP .R CR
|
||||||
|
LI1 OVER SPACES U. CR R@ LI1 SWAP U.R CR
|
||||||
|
LI2 SWAP SPACES U. CR R> LI2 SWAP U.R CR
|
||||||
|
;
|
||||||
|
|
||||||
|
: .R&U.R ( -- )
|
||||||
|
CR ." You should see lines duplicated:" CR
|
||||||
|
." indented by 0 spaces" CR 0 0 (.R&U.R) CR
|
||||||
|
." indented by 0 spaces" CR LENLI1 0 (.R&U.R) CR \ Just fits required width
|
||||||
|
." indented by 5 spaces" CR LENLI1 5 (.R&U.R) CR
|
||||||
|
;
|
||||||
|
|
||||||
|
CR CR .( Output from .R and U.R)
|
||||||
|
T{ .R&U.R -> }T
|
||||||
|
|
||||||
|
\ -----------------------------------------------------------------------------
|
||||||
|
TESTING PAD ERASE
|
||||||
|
\ Must handle different size characters i.e. 1 CHARS >= 1
|
||||||
|
|
||||||
|
84 CONSTANT CHARS/PAD \ Minimum size of PAD in chars
|
||||||
|
CHARS/PAD CHARS CONSTANT AUS/PAD
|
||||||
|
: CHECKPAD ( caddr u ch -- f ) \ f = TRUE if u chars = ch
|
||||||
|
SWAP 0
|
||||||
|
?DO
|
||||||
|
OVER I CHARS + C@ OVER <>
|
||||||
|
IF 2DROP UNLOOP FALSE EXIT THEN
|
||||||
|
LOOP
|
||||||
|
2DROP TRUE
|
||||||
|
;
|
||||||
|
|
||||||
|
T{ PAD DROP -> }T
|
||||||
|
T{ 0 INVERT PAD C! -> }T
|
||||||
|
T{ PAD C@ CONSTANT MAXCHAR -> }T
|
||||||
|
T{ PAD CHARS/PAD 2DUP MAXCHAR FILL MAXCHAR CHECKPAD -> TRUE }T
|
||||||
|
T{ PAD CHARS/PAD 2DUP CHARS ERASE 0 CHECKPAD -> TRUE }T
|
||||||
|
T{ PAD CHARS/PAD 2DUP MAXCHAR FILL PAD 0 ERASE MAXCHAR CHECKPAD -> TRUE }T
|
||||||
|
T{ PAD 43 CHARS + 9 CHARS ERASE -> }T
|
||||||
|
T{ PAD 43 MAXCHAR CHECKPAD -> TRUE }T
|
||||||
|
T{ PAD 43 CHARS + 9 0 CHECKPAD -> TRUE }T
|
||||||
|
T{ PAD 52 CHARS + CHARS/PAD 52 - MAXCHAR CHECKPAD -> TRUE }T
|
||||||
|
|
||||||
|
\ Check that use of WORD and pictured numeric output do not corrupt PAD
|
||||||
|
\ Minimum size of buffers for these are 33 chars and (2*n)+2 chars respectively
|
||||||
|
\ where n is number of bits per cell
|
||||||
|
|
||||||
|
PAD CHARS/PAD ERASE
|
||||||
|
2 BASE !
|
||||||
|
MAX-UINT MAX-UINT <# #S CHAR 1 DUP HOLD HOLD #> 2DROP
|
||||||
|
DECIMAL
|
||||||
|
BL WORD 12345678123456781234567812345678 DROP
|
||||||
|
T{ PAD CHARS/PAD 0 CHECKPAD -> TRUE }T
|
||||||
|
|
||||||
|
\ -----------------------------------------------------------------------------
|
||||||
|
TESTING PARSE
|
||||||
|
|
||||||
|
T{ CHAR | PARSE 1234| DUP ROT ROT EVALUATE -> 4 1234 }T
|
||||||
|
T{ CHAR ^ PARSE 23 45 ^ DUP ROT ROT EVALUATE -> 7 23 45 }T
|
||||||
|
: PA1 [CHAR] $ PARSE DUP >R PAD SWAP CHARS MOVE PAD R> ;
|
||||||
|
T{ PA1 3456
|
||||||
|
DUP ROT ROT EVALUATE -> 4 3456 }T
|
||||||
|
T{ CHAR A PARSE A SWAP DROP -> 0 }T
|
||||||
|
T{ CHAR Z PARSE
|
||||||
|
SWAP DROP -> 0 }T
|
||||||
|
T{ CHAR " PARSE 4567 "DUP ROT ROT EVALUATE -> 5 4567 }T
|
||||||
|
|
||||||
|
\ -----------------------------------------------------------------------------
|
||||||
|
TESTING PARSE-NAME (Forth 2012)
|
||||||
|
\ Adapted from the PARSE-NAME RfD tests
|
||||||
|
|
||||||
|
T{ PARSE-NAME abcd STR1 S= -> TRUE }T \ No leading spaces
|
||||||
|
T{ PARSE-NAME abcde STR2 S= -> TRUE }T \ Leading spaces
|
||||||
|
|
||||||
|
\ Test empty parse area, new lines are necessary
|
||||||
|
T{ PARSE-NAME
|
||||||
|
NIP -> 0 }T
|
||||||
|
\ Empty parse area with spaces after PARSE-NAME
|
||||||
|
T{ PARSE-NAME
|
||||||
|
NIP -> 0 }T
|
||||||
|
|
||||||
|
T{ : PARSE-NAME-TEST ( "name1" "name2" -- n )
|
||||||
|
PARSE-NAME PARSE-NAME S= ; -> }T
|
||||||
|
T{ PARSE-NAME-TEST abcd abcd -> TRUE }T
|
||||||
|
T{ PARSE-NAME-TEST abcd abcd -> TRUE }T \ Leading spaces
|
||||||
|
T{ PARSE-NAME-TEST abcde abcdf -> FALSE }T
|
||||||
|
T{ PARSE-NAME-TEST abcdf abcde -> FALSE }T
|
||||||
|
T{ PARSE-NAME-TEST abcde abcde
|
||||||
|
-> TRUE }T \ Parse to end of line
|
||||||
|
T{ PARSE-NAME-TEST abcde abcde
|
||||||
|
-> TRUE }T \ Leading and trailing spaces
|
||||||
|
|
||||||
|
\ -----------------------------------------------------------------------------
|
||||||
|
TESTING DEFER DEFER@ DEFER! IS ACTION-OF (Forth 2012)
|
||||||
|
\ Adapted from the Forth 200X RfD tests
|
||||||
|
|
||||||
|
T{ DEFER DEFER1 -> }T
|
||||||
|
T{ : MY-DEFER DEFER ; -> }T
|
||||||
|
T{ : IS-DEFER1 IS DEFER1 ; -> }T
|
||||||
|
T{ : ACTION-DEFER1 ACTION-OF DEFER1 ; -> }T
|
||||||
|
T{ : DEF! DEFER! ; -> }T
|
||||||
|
T{ : DEF@ DEFER@ ; -> }T
|
||||||
|
|
||||||
|
T{ ' * ' DEFER1 DEFER! -> }T
|
||||||
|
T{ 2 3 DEFER1 -> 6 }T
|
||||||
|
T{ ' DEFER1 DEFER@ -> ' * }T
|
||||||
|
T{ ' DEFER1 DEF@ -> ' * }T
|
||||||
|
T{ ACTION-OF DEFER1 -> ' * }T
|
||||||
|
T{ ACTION-DEFER1 -> ' * }T
|
||||||
|
T{ ' + IS DEFER1 -> }T
|
||||||
|
T{ 1 2 DEFER1 -> 3 }T
|
||||||
|
T{ ' DEFER1 DEFER@ -> ' + }T
|
||||||
|
T{ ' DEFER1 DEF@ -> ' + }T
|
||||||
|
T{ ACTION-OF DEFER1 -> ' + }T
|
||||||
|
T{ ACTION-DEFER1 -> ' + }T
|
||||||
|
T{ ' - IS-DEFER1 -> }T
|
||||||
|
T{ 1 2 DEFER1 -> -1 }T
|
||||||
|
T{ ' DEFER1 DEFER@ -> ' - }T
|
||||||
|
T{ ' DEFER1 DEF@ -> ' - }T
|
||||||
|
T{ ACTION-OF DEFER1 -> ' - }T
|
||||||
|
T{ ACTION-DEFER1 -> ' - }T
|
||||||
|
|
||||||
|
T{ MY-DEFER DEFER2 -> }T
|
||||||
|
T{ ' DUP IS DEFER2 -> }T
|
||||||
|
T{ 1 DEFER2 -> 1 1 }T
|
||||||
|
|
||||||
|
\ -----------------------------------------------------------------------------
|
||||||
|
TESTING HOLDS (Forth 2012)
|
||||||
|
|
||||||
|
: HTEST S" Testing HOLDS" ;
|
||||||
|
: HTEST2 S" works" ;
|
||||||
|
: HTEST3 S" Testing HOLDS works 123" ;
|
||||||
|
T{ 0 0 <# HTEST HOLDS #> HTEST S= -> TRUE }T
|
||||||
|
T{ 123 0 <# #S BL HOLD HTEST2 HOLDS BL HOLD HTEST HOLDS #>
|
||||||
|
HTEST3 S= -> TRUE }T
|
||||||
|
T{ : HLD HOLDS ; -> }T
|
||||||
|
T{ 0 0 <# HTEST HLD #> HTEST S= -> TRUE }T
|
||||||
|
|
||||||
|
\ -----------------------------------------------------------------------------
|
||||||
|
TESTING REFILL SOURCE-ID
|
||||||
|
\ REFILL and SOURCE-ID from the user input device can't be tested from a file,
|
||||||
|
\ can only be tested from a string via EVALUATE
|
||||||
|
|
||||||
|
T{ : RF1 S" REFILL" EVALUATE ; RF1 -> FALSE }T
|
||||||
|
T{ : SID1 S" SOURCE-ID" EVALUATE ; SID1 -> -1 }T
|
||||||
|
|
||||||
|
\ ------------------------------------------------------------------------------
|
||||||
|
TESTING S\" (Forth 2012 compilation mode)
|
||||||
|
\ Extended the Forth 200X RfD tests
|
||||||
|
\ Note this tests the Core Ext definition of S\" which has unedfined
|
||||||
|
\ interpretation semantics. S\" in interpretation mode is tested in the tests on
|
||||||
|
\ the File-Access word set
|
||||||
|
|
||||||
|
T{ : SSQ1 S\" abc" S" abc" S= ; -> }T \ No escapes
|
||||||
|
T{ SSQ1 -> TRUE }T
|
||||||
|
T{ : SSQ2 S\" " ; SSQ2 SWAP DROP -> 0 }T \ Empty string
|
||||||
|
|
||||||
|
T{ : SSQ3 S\" \a\b\e\f\l\m\q\r\t\v\x0F0\x1Fa\xaBx\z\"\\" ; -> }T
|
||||||
|
T{ SSQ3 SWAP DROP -> 20 }T \ String length
|
||||||
|
T{ SSQ3 DROP C@ -> 7 }T \ \a BEL Bell
|
||||||
|
T{ SSQ3 DROP 1 CHARS + C@ -> 8 }T \ \b BS Backspace
|
||||||
|
T{ SSQ3 DROP 2 CHARS + C@ -> 27 }T \ \e ESC Escape
|
||||||
|
T{ SSQ3 DROP 3 CHARS + C@ -> 12 }T \ \f FF Form feed
|
||||||
|
T{ SSQ3 DROP 4 CHARS + C@ -> 10 }T \ \l LF Line feed
|
||||||
|
T{ SSQ3 DROP 5 CHARS + C@ -> 13 }T \ \m CR of CR/LF pair
|
||||||
|
T{ SSQ3 DROP 6 CHARS + C@ -> 10 }T \ LF of CR/LF pair
|
||||||
|
T{ SSQ3 DROP 7 CHARS + C@ -> 34 }T \ \q " Double Quote
|
||||||
|
T{ SSQ3 DROP 8 CHARS + C@ -> 13 }T \ \r CR Carriage Return
|
||||||
|
T{ SSQ3 DROP 9 CHARS + C@ -> 9 }T \ \t TAB Horizontal Tab
|
||||||
|
T{ SSQ3 DROP 10 CHARS + C@ -> 11 }T \ \v VT Vertical Tab
|
||||||
|
T{ SSQ3 DROP 11 CHARS + C@ -> 15 }T \ \x0F Given Char
|
||||||
|
T{ SSQ3 DROP 12 CHARS + C@ -> 48 }T \ 0 0 Digit follow on
|
||||||
|
T{ SSQ3 DROP 13 CHARS + C@ -> 31 }T \ \x1F Given Char
|
||||||
|
T{ SSQ3 DROP 14 CHARS + C@ -> 97 }T \ a a Hex follow on
|
||||||
|
T{ SSQ3 DROP 15 CHARS + C@ -> 171 }T \ \xaB Insensitive Given Char
|
||||||
|
T{ SSQ3 DROP 16 CHARS + C@ -> 120 }T \ x x Non hex follow on
|
||||||
|
T{ SSQ3 DROP 17 CHARS + C@ -> 0 }T \ \z NUL No Character
|
||||||
|
T{ SSQ3 DROP 18 CHARS + C@ -> 34 }T \ \" " Double Quote
|
||||||
|
T{ SSQ3 DROP 19 CHARS + C@ -> 92 }T \ \\ \ Back Slash
|
||||||
|
|
||||||
|
\ The above does not test \n as this is a system dependent value.
|
||||||
|
\ Check it displays a new line
|
||||||
|
CR .( The next test should display:)
|
||||||
|
CR .( One line...)
|
||||||
|
CR .( another line)
|
||||||
|
T{ : SSQ4 S\" \nOne line...\nanotherLine\n" TYPE ; SSQ4 -> }T
|
||||||
|
|
||||||
|
\ Test bare escapable characters appear as themselves
|
||||||
|
T{ : SSQ5 S\" abeflmnqrtvxz" S" abeflmnqrtvxz" S= ; SSQ5 -> TRUE }T
|
||||||
|
|
||||||
|
T{ : SSQ6 S\" a\""2DROP 1111 ; SSQ6 -> 1111 }T \ Parsing behaviour
|
||||||
|
|
||||||
|
T{ : SSQ7 S\" 111 : SSQ8 S\\\" 222\" EVALUATE ; SSQ8 333" EVALUATE ; -> }T
|
||||||
|
T{ SSQ7 -> 111 222 333 }T
|
||||||
|
T{ : SSQ9 S\" 11 : SSQ10 S\\\" \\x32\\x32\" EVALUATE ; SSQ10 33" EVALUATE ; -> }T
|
||||||
|
T{ SSQ9 -> 11 22 33 }T
|
||||||
|
|
||||||
|
\ -----------------------------------------------------------------------------
|
||||||
|
CORE-EXT-ERRORS SET-ERROR-COUNT
|
||||||
|
|
||||||
|
CR .( End of Core Extension word tests) CR
|
||||||
|
|
||||||
|
|
||||||
66
lib/forth/ans-tests/tester.fr
Normal file
66
lib/forth/ans-tests/tester.fr
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
\ From: John Hayes S1I
|
||||||
|
\ Subject: tester.fr
|
||||||
|
\ Date: Mon, 27 Nov 95 13:10:09 PST
|
||||||
|
|
||||||
|
\ (C) 1995 JOHNS HOPKINS UNIVERSITY / APPLIED PHYSICS LABORATORY
|
||||||
|
\ MAY BE DISTRIBUTED FREELY AS LONG AS THIS COPYRIGHT NOTICE REMAINS.
|
||||||
|
\ VERSION 1.2
|
||||||
|
|
||||||
|
\ 24/11/2015 Replaced Core Ext word <> with = 0=
|
||||||
|
\ 31/3/2015 Variable #ERRORS added and incremented for each error reported.
|
||||||
|
\ 22/1/09 The words { and } have been changed to T{ and }T respectively to
|
||||||
|
\ agree with the Forth 200X file ttester.fs. This avoids clashes with
|
||||||
|
\ locals using { ... } and the FSL use of }
|
||||||
|
|
||||||
|
HEX
|
||||||
|
|
||||||
|
\ SET THE FOLLOWING FLAG TO TRUE FOR MORE VERBOSE OUTPUT; THIS MAY
|
||||||
|
\ ALLOW YOU TO TELL WHICH TEST CAUSED YOUR SYSTEM TO HANG.
|
||||||
|
VARIABLE VERBOSE
|
||||||
|
FALSE VERBOSE !
|
||||||
|
\ TRUE VERBOSE !
|
||||||
|
|
||||||
|
: EMPTY-STACK \ ( ... -- ) EMPTY STACK: HANDLES UNDERFLOWED STACK TOO.
|
||||||
|
DEPTH ?DUP IF DUP 0< IF NEGATE 0 DO 0 LOOP ELSE 0 DO DROP LOOP THEN THEN ;
|
||||||
|
|
||||||
|
VARIABLE #ERRORS 0 #ERRORS !
|
||||||
|
|
||||||
|
: ERROR \ ( C-ADDR U -- ) DISPLAY AN ERROR MESSAGE FOLLOWED BY
|
||||||
|
\ THE LINE THAT HAD THE ERROR.
|
||||||
|
CR TYPE SOURCE TYPE \ DISPLAY LINE CORRESPONDING TO ERROR
|
||||||
|
EMPTY-STACK \ THROW AWAY EVERY THING ELSE
|
||||||
|
#ERRORS @ 1 + #ERRORS !
|
||||||
|
\ QUIT \ *** Uncomment this line to QUIT on an error
|
||||||
|
;
|
||||||
|
|
||||||
|
VARIABLE ACTUAL-DEPTH \ STACK RECORD
|
||||||
|
CREATE ACTUAL-RESULTS 20 CELLS ALLOT
|
||||||
|
|
||||||
|
: T{ \ ( -- ) SYNTACTIC SUGAR.
|
||||||
|
;
|
||||||
|
|
||||||
|
: -> \ ( ... -- ) RECORD DEPTH AND CONTENT OF STACK.
|
||||||
|
DEPTH DUP ACTUAL-DEPTH ! \ RECORD DEPTH
|
||||||
|
?DUP IF \ IF THERE IS SOMETHING ON STACK
|
||||||
|
0 DO ACTUAL-RESULTS I CELLS + ! LOOP \ SAVE THEM
|
||||||
|
THEN ;
|
||||||
|
|
||||||
|
: }T \ ( ... -- ) COMPARE STACK (EXPECTED) CONTENTS WITH SAVED
|
||||||
|
\ (ACTUAL) CONTENTS.
|
||||||
|
DEPTH ACTUAL-DEPTH @ = IF \ IF DEPTHS MATCH
|
||||||
|
DEPTH ?DUP IF \ IF THERE IS SOMETHING ON THE STACK
|
||||||
|
0 DO \ FOR EACH STACK ITEM
|
||||||
|
ACTUAL-RESULTS I CELLS + @ \ COMPARE ACTUAL WITH EXPECTED
|
||||||
|
= 0= IF S" INCORRECT RESULT: " ERROR LEAVE THEN
|
||||||
|
LOOP
|
||||||
|
THEN
|
||||||
|
ELSE \ DEPTH MISMATCH
|
||||||
|
S" WRONG NUMBER OF RESULTS: " ERROR
|
||||||
|
THEN ;
|
||||||
|
|
||||||
|
: TESTING \ ( -- ) TALKING COMMENT.
|
||||||
|
SOURCE VERBOSE @
|
||||||
|
IF DUP >R TYPE CR R> >IN !
|
||||||
|
ELSE >IN ! DROP [CHAR] * EMIT
|
||||||
|
THEN ;
|
||||||
|
|
||||||
File diff suppressed because it is too large
Load Diff
170
lib/forth/conformance.sh
Executable file
170
lib/forth/conformance.sh
Executable file
@@ -0,0 +1,170 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# Run the Hayes/Gerry-Jackson Core conformance suite against our Forth
|
||||||
|
# interpreter and emit scoreboard.json + scoreboard.md.
|
||||||
|
#
|
||||||
|
# Method:
|
||||||
|
# 1. Preprocess lib/forth/ans-tests/core.fr — strip \ comments, ( ... )
|
||||||
|
# comments, and TESTING … metadata lines.
|
||||||
|
# 2. Split into chunks ending at each `}T` so an error in one test
|
||||||
|
# chunk doesn't abort the run.
|
||||||
|
# 3. Emit an SX file that exposes those chunks as a list.
|
||||||
|
# 4. Run our Forth + hayes-runner under sx_server; record pass/fail/error.
|
||||||
|
|
||||||
|
set -e
|
||||||
|
FORTH_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||||
|
ROOT="$(cd "$FORTH_DIR/../.." && pwd)"
|
||||||
|
SX_SERVER="${SX_SERVER:-/root/rose-ash/hosts/ocaml/_build/default/bin/sx_server.exe}"
|
||||||
|
SOURCE="$FORTH_DIR/ans-tests/core.fr"
|
||||||
|
OUT_JSON="$FORTH_DIR/scoreboard.json"
|
||||||
|
OUT_MD="$FORTH_DIR/scoreboard.md"
|
||||||
|
TMP="$(mktemp -d)"
|
||||||
|
PREPROC="$TMP/preproc.forth"
|
||||||
|
CHUNKS_SX="$TMP/chunks.sx"
|
||||||
|
|
||||||
|
cd "$ROOT"
|
||||||
|
|
||||||
|
# 1. preprocess
|
||||||
|
awk '
|
||||||
|
{
|
||||||
|
line = $0
|
||||||
|
# protect POSTPONE \ so the comment-strip below leaves the literal \ alone
|
||||||
|
gsub(/POSTPONE[ \t]+\\/, "POSTPONE @@BS@@", line)
|
||||||
|
# strip leading/embedded \ line comments (must be \ followed by space or EOL)
|
||||||
|
gsub(/(^|[ \t])\\([ \t].*|$)/, " ", line)
|
||||||
|
# strip ( ... ) block comments that sit on one line
|
||||||
|
gsub(/\([^)]*\)/, " ", line)
|
||||||
|
# strip TESTING … metadata lines (rest of line, incl. bare TESTING)
|
||||||
|
sub(/TESTING([ \t].*)?$/, " ", line)
|
||||||
|
# restore the protected backslash
|
||||||
|
gsub(/@@BS@@/, "\\", line)
|
||||||
|
print line
|
||||||
|
}' "$SOURCE" > "$PREPROC"
|
||||||
|
|
||||||
|
# 2 + 3: split into chunks at each `}T` and emit as a SX file
|
||||||
|
#
|
||||||
|
# Cap chunks via MAX_CHUNKS env (default 638 = full Hayes Core). Lower
|
||||||
|
# it temporarily if later tests regress into an infinite loop while you
|
||||||
|
# are iterating on primitives.
|
||||||
|
MAX_CHUNKS="${MAX_CHUNKS:-638}"
|
||||||
|
|
||||||
|
MAX_CHUNKS="$MAX_CHUNKS" python3 - "$PREPROC" "$CHUNKS_SX" <<'PY'
|
||||||
|
import os, re, sys
|
||||||
|
preproc_path, out_path = sys.argv[1], sys.argv[2]
|
||||||
|
max_chunks = int(os.environ.get("MAX_CHUNKS", "590"))
|
||||||
|
text = open(preproc_path).read()
|
||||||
|
# keep the `}T` attached to the preceding chunk
|
||||||
|
parts = re.split(r'(\}T)', text)
|
||||||
|
chunks = []
|
||||||
|
buf = ""
|
||||||
|
for p in parts:
|
||||||
|
buf += p
|
||||||
|
if p == "}T":
|
||||||
|
s = buf.strip()
|
||||||
|
if s:
|
||||||
|
chunks.append(s)
|
||||||
|
buf = ""
|
||||||
|
if buf.strip():
|
||||||
|
chunks.append(buf.strip())
|
||||||
|
chunks = chunks[:max_chunks]
|
||||||
|
|
||||||
|
def esc(s):
|
||||||
|
s = s.replace('\\', '\\\\').replace('"', '\\"')
|
||||||
|
s = s.replace('\r', ' ').replace('\n', ' ')
|
||||||
|
s = re.sub(r'\s+', ' ', s).strip()
|
||||||
|
return s
|
||||||
|
|
||||||
|
with open(out_path, "w") as f:
|
||||||
|
f.write("(define hayes-chunks (list\n")
|
||||||
|
for c in chunks:
|
||||||
|
f.write(' "' + esc(c) + '"\n')
|
||||||
|
f.write("))\n\n")
|
||||||
|
f.write("(define\n")
|
||||||
|
f.write(" hayes-run-all\n")
|
||||||
|
f.write(" (fn\n")
|
||||||
|
f.write(" ()\n")
|
||||||
|
f.write(" (hayes-reset!)\n")
|
||||||
|
f.write(" (let ((s (hayes-boot)))\n")
|
||||||
|
f.write(" (for-each (fn (c) (hayes-run-chunk s c)) hayes-chunks))\n")
|
||||||
|
f.write(" (hayes-summary)))\n")
|
||||||
|
PY
|
||||||
|
|
||||||
|
# 4. run it
|
||||||
|
OUT=$(printf '(epoch 1)\n(load "lib/forth/runtime.sx")\n(epoch 2)\n(load "lib/forth/reader.sx")\n(epoch 3)\n(load "lib/forth/interpreter.sx")\n(epoch 4)\n(load "lib/forth/compiler.sx")\n(epoch 5)\n(load "lib/forth/hayes-runner.sx")\n(epoch 6)\n(load "%s")\n(epoch 7)\n(eval "(hayes-run-all)")\n' "$CHUNKS_SX" \
|
||||||
|
| timeout 180 "$SX_SERVER" 2>&1)
|
||||||
|
STATUS=$?
|
||||||
|
|
||||||
|
SUMMARY=$(printf '%s\n' "$OUT" | awk '/^\{:pass / {print; exit}')
|
||||||
|
PASS=$(printf '%s' "$SUMMARY" | sed -n 's/.*:pass \([0-9-]*\).*/\1/p')
|
||||||
|
FAIL=$(printf '%s' "$SUMMARY" | sed -n 's/.*:fail \([0-9-]*\).*/\1/p')
|
||||||
|
ERR=$(printf '%s' "$SUMMARY" | sed -n 's/.*:error \([0-9-]*\).*/\1/p')
|
||||||
|
TOTAL=$(printf '%s' "$SUMMARY" | sed -n 's/.*:total \([0-9-]*\).*/\1/p')
|
||||||
|
CHUNK_COUNT=$(grep -c '^ "' "$CHUNKS_SX" || echo 0)
|
||||||
|
TOTAL_AVAILABLE=$(grep -c '}T' "$PREPROC" || echo 0)
|
||||||
|
|
||||||
|
NOW="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
|
||||||
|
|
||||||
|
if [ -z "$PASS" ]; then
|
||||||
|
PASS=0; FAIL=0; ERR=0; TOTAL=0
|
||||||
|
NOTE="runner halted before completing (timeout or SX error)"
|
||||||
|
else
|
||||||
|
NOTE="completed"
|
||||||
|
fi
|
||||||
|
|
||||||
|
PCT=0
|
||||||
|
if [ "$TOTAL" -gt 0 ]; then
|
||||||
|
PCT=$((PASS * 100 / TOTAL))
|
||||||
|
fi
|
||||||
|
|
||||||
|
cat > "$OUT_JSON" <<JSON
|
||||||
|
{
|
||||||
|
"source": "gerryjackson/forth2012-test-suite src/core.fr",
|
||||||
|
"generated_at": "$NOW",
|
||||||
|
"chunks_available": $TOTAL_AVAILABLE,
|
||||||
|
"chunks_fed": $CHUNK_COUNT,
|
||||||
|
"total": $TOTAL,
|
||||||
|
"pass": $PASS,
|
||||||
|
"fail": $FAIL,
|
||||||
|
"error": $ERR,
|
||||||
|
"percent": $PCT,
|
||||||
|
"note": "$NOTE"
|
||||||
|
}
|
||||||
|
JSON
|
||||||
|
|
||||||
|
cat > "$OUT_MD" <<MD
|
||||||
|
# Forth Hayes Core scoreboard
|
||||||
|
|
||||||
|
| metric | value |
|
||||||
|
| ----------------- | ----: |
|
||||||
|
| chunks available | $TOTAL_AVAILABLE |
|
||||||
|
| chunks fed | $CHUNK_COUNT |
|
||||||
|
| total | $TOTAL |
|
||||||
|
| pass | $PASS |
|
||||||
|
| fail | $FAIL |
|
||||||
|
| error | $ERR |
|
||||||
|
| percent | ${PCT}% |
|
||||||
|
|
||||||
|
- **Source**: \`gerryjackson/forth2012-test-suite\` \`src/core.fr\`
|
||||||
|
- **Generated**: $NOW
|
||||||
|
- **Note**: $NOTE
|
||||||
|
|
||||||
|
A "chunk" is any preprocessed segment ending at a \`}T\` (every Hayes test
|
||||||
|
is one chunk, plus the small declaration blocks between tests).
|
||||||
|
The runner catches raised errors at chunk boundaries so one bad chunk
|
||||||
|
does not abort the rest. \`error\` covers chunks that raised; \`fail\`
|
||||||
|
covers tests whose \`->\` / \`}T\` comparison mismatched.
|
||||||
|
|
||||||
|
### Chunk cap
|
||||||
|
|
||||||
|
\`conformance.sh\` processes the first \`\$MAX_CHUNKS\` chunks (default
|
||||||
|
**638**, i.e. the whole Hayes Core file). Lower the cap temporarily
|
||||||
|
while iterating on primitives if a regression re-opens an infinite
|
||||||
|
loop in later tests.
|
||||||
|
MD
|
||||||
|
|
||||||
|
echo "$SUMMARY"
|
||||||
|
echo "Scoreboard: $OUT_JSON"
|
||||||
|
echo " $OUT_MD"
|
||||||
|
|
||||||
|
if [ "$STATUS" -ne 0 ] && [ "$TOTAL" -eq 0 ]; then
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
158
lib/forth/hayes-runner.sx
Normal file
158
lib/forth/hayes-runner.sx
Normal file
@@ -0,0 +1,158 @@
|
|||||||
|
;; Hayes conformance test runner.
|
||||||
|
;; Installs T{ -> }T as Forth primitives that snapshot and compare dstack,
|
||||||
|
;; plus stub TESTING / HEX / DECIMAL so the Hayes Core file can stream
|
||||||
|
;; through the interpreter without halting on unsupported metadata words.
|
||||||
|
|
||||||
|
(define hayes-pass 0)
|
||||||
|
(define hayes-fail 0)
|
||||||
|
(define hayes-error 0)
|
||||||
|
(define hayes-start-depth 0)
|
||||||
|
(define hayes-actual (list))
|
||||||
|
(define hayes-actual-set false)
|
||||||
|
(define hayes-failures (list))
|
||||||
|
(define hayes-first-error "")
|
||||||
|
(define hayes-error-hist (dict))
|
||||||
|
|
||||||
|
(define
|
||||||
|
hayes-reset!
|
||||||
|
(fn
|
||||||
|
()
|
||||||
|
(set! hayes-pass 0)
|
||||||
|
(set! hayes-fail 0)
|
||||||
|
(set! hayes-error 0)
|
||||||
|
(set! hayes-start-depth 0)
|
||||||
|
(set! hayes-actual (list))
|
||||||
|
(set! hayes-actual-set false)
|
||||||
|
(set! hayes-failures (list))
|
||||||
|
(set! hayes-first-error "")
|
||||||
|
(set! hayes-error-hist (dict))))
|
||||||
|
|
||||||
|
(define
|
||||||
|
hayes-slice
|
||||||
|
(fn
|
||||||
|
(state base)
|
||||||
|
(let
|
||||||
|
((n (- (forth-depth state) base)))
|
||||||
|
(if (<= n 0) (list) (take (get state "dstack") n)))))
|
||||||
|
|
||||||
|
(define
|
||||||
|
hayes-truncate!
|
||||||
|
(fn
|
||||||
|
(state base)
|
||||||
|
(let
|
||||||
|
((n (- (forth-depth state) base)))
|
||||||
|
(when (> n 0) (dict-set! state "dstack" (drop (get state "dstack") n))))))
|
||||||
|
|
||||||
|
(define
|
||||||
|
hayes-install!
|
||||||
|
(fn
|
||||||
|
(state)
|
||||||
|
(forth-def-prim!
|
||||||
|
state
|
||||||
|
"T{"
|
||||||
|
(fn
|
||||||
|
(s)
|
||||||
|
(set! hayes-start-depth (forth-depth s))
|
||||||
|
(set! hayes-actual-set false)
|
||||||
|
(set! hayes-actual (list))))
|
||||||
|
(forth-def-prim!
|
||||||
|
state
|
||||||
|
"->"
|
||||||
|
(fn
|
||||||
|
(s)
|
||||||
|
(set! hayes-actual (hayes-slice s hayes-start-depth))
|
||||||
|
(set! hayes-actual-set true)
|
||||||
|
(hayes-truncate! s hayes-start-depth)))
|
||||||
|
(forth-def-prim!
|
||||||
|
state
|
||||||
|
"}T"
|
||||||
|
(fn
|
||||||
|
(s)
|
||||||
|
(let
|
||||||
|
((expected (hayes-slice s hayes-start-depth)))
|
||||||
|
(hayes-truncate! s hayes-start-depth)
|
||||||
|
(if
|
||||||
|
(and hayes-actual-set (= expected hayes-actual))
|
||||||
|
(set! hayes-pass (+ hayes-pass 1))
|
||||||
|
(begin
|
||||||
|
(set! hayes-fail (+ hayes-fail 1))
|
||||||
|
(set!
|
||||||
|
hayes-failures
|
||||||
|
(concat
|
||||||
|
hayes-failures
|
||||||
|
(list
|
||||||
|
(dict
|
||||||
|
"kind"
|
||||||
|
"fail"
|
||||||
|
"expected"
|
||||||
|
(str expected)
|
||||||
|
"actual"
|
||||||
|
(str hayes-actual))))))))))
|
||||||
|
(forth-def-prim! state "TESTING" (fn (s) nil))
|
||||||
|
;; HEX/DECIMAL are real primitives now (runtime.sx) — no stub needed.
|
||||||
|
state))
|
||||||
|
|
||||||
|
(define
|
||||||
|
hayes-boot
|
||||||
|
(fn () (let ((s (forth-boot))) (hayes-install! s) (hayes-reset!) s)))
|
||||||
|
|
||||||
|
;; Run a single preprocessed chunk (string of Forth source) on the shared
|
||||||
|
;; state. Catch any raised error and move on — the chunk boundary is a
|
||||||
|
;; safe resume point.
|
||||||
|
(define
|
||||||
|
hayes-bump-error-key!
|
||||||
|
(fn
|
||||||
|
(err)
|
||||||
|
(let
|
||||||
|
((msg (str err)))
|
||||||
|
(let
|
||||||
|
((space-idx (index-of msg " ")))
|
||||||
|
(let
|
||||||
|
((key
|
||||||
|
(if
|
||||||
|
(> space-idx 0)
|
||||||
|
(substr msg 0 space-idx)
|
||||||
|
msg)))
|
||||||
|
(dict-set!
|
||||||
|
hayes-error-hist
|
||||||
|
key
|
||||||
|
(+ 1 (or (get hayes-error-hist key) 0))))))))
|
||||||
|
|
||||||
|
(define
|
||||||
|
hayes-run-chunk
|
||||||
|
(fn
|
||||||
|
(state src)
|
||||||
|
(guard
|
||||||
|
(err
|
||||||
|
((= 1 1)
|
||||||
|
(begin
|
||||||
|
(set! hayes-error (+ hayes-error 1))
|
||||||
|
(when
|
||||||
|
(= (len hayes-first-error) 0)
|
||||||
|
(set! hayes-first-error (str err)))
|
||||||
|
(hayes-bump-error-key! err)
|
||||||
|
(dict-set! state "dstack" (list))
|
||||||
|
(dict-set! state "rstack" (list))
|
||||||
|
(dict-set! state "compiling" false)
|
||||||
|
(dict-set! state "current-def" nil)
|
||||||
|
(dict-set! state "cstack" (list))
|
||||||
|
(dict-set! state "input" (list)))))
|
||||||
|
(forth-interpret state src))))
|
||||||
|
|
||||||
|
(define
|
||||||
|
hayes-summary
|
||||||
|
(fn
|
||||||
|
()
|
||||||
|
(dict
|
||||||
|
"pass"
|
||||||
|
hayes-pass
|
||||||
|
"fail"
|
||||||
|
hayes-fail
|
||||||
|
"error"
|
||||||
|
hayes-error
|
||||||
|
"total"
|
||||||
|
(+ (+ hayes-pass hayes-fail) hayes-error)
|
||||||
|
"first-error"
|
||||||
|
hayes-first-error
|
||||||
|
"error-hist"
|
||||||
|
hayes-error-hist)))
|
||||||
@@ -5,7 +5,39 @@
|
|||||||
|
|
||||||
(define
|
(define
|
||||||
forth-execute-word
|
forth-execute-word
|
||||||
(fn (state word) (let ((body (get word "body"))) (body state))))
|
(fn
|
||||||
|
(state word)
|
||||||
|
(dict-set! word "call-count" (+ 1 (or (get word "call-count") 0)))
|
||||||
|
(let ((body (get word "body"))) (body state))))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-hot-words
|
||||||
|
(fn
|
||||||
|
(state threshold)
|
||||||
|
(forth-hot-walk
|
||||||
|
(keys (get state "dict"))
|
||||||
|
(get state "dict")
|
||||||
|
threshold
|
||||||
|
(list))))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-hot-walk
|
||||||
|
(fn
|
||||||
|
(names dict threshold acc)
|
||||||
|
(if
|
||||||
|
(= (len names) 0)
|
||||||
|
acc
|
||||||
|
(let
|
||||||
|
((n (first names)))
|
||||||
|
(let
|
||||||
|
((w (get dict n)))
|
||||||
|
(let
|
||||||
|
((c (or (get w "call-count") 0)))
|
||||||
|
(forth-hot-walk
|
||||||
|
(rest names)
|
||||||
|
dict
|
||||||
|
threshold
|
||||||
|
(if (>= c threshold) (cons (list n c) acc) acc))))))))
|
||||||
|
|
||||||
(define
|
(define
|
||||||
forth-interpret-token
|
forth-interpret-token
|
||||||
@@ -17,7 +49,7 @@
|
|||||||
(not (nil? w))
|
(not (nil? w))
|
||||||
(forth-execute-word state w)
|
(forth-execute-word state w)
|
||||||
(let
|
(let
|
||||||
((n (forth-parse-number tok (get state "base"))))
|
((n (forth-parse-number tok (get (get state "vars") "base"))))
|
||||||
(if
|
(if
|
||||||
(not (nil? n))
|
(not (nil? n))
|
||||||
(forth-push state n)
|
(forth-push state n)
|
||||||
|
|||||||
1547
lib/forth/runtime.sx
1547
lib/forth/runtime.sx
File diff suppressed because it is too large
Load Diff
12
lib/forth/scoreboard.json
Normal file
12
lib/forth/scoreboard.json
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
{
|
||||||
|
"source": "gerryjackson/forth2012-test-suite src/core.fr",
|
||||||
|
"generated_at": "2026-05-05T21:30:21Z",
|
||||||
|
"chunks_available": 638,
|
||||||
|
"chunks_fed": 638,
|
||||||
|
"total": 638,
|
||||||
|
"pass": 632,
|
||||||
|
"fail": 6,
|
||||||
|
"error": 0,
|
||||||
|
"percent": 99,
|
||||||
|
"note": "completed"
|
||||||
|
}
|
||||||
28
lib/forth/scoreboard.md
Normal file
28
lib/forth/scoreboard.md
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
# Forth Hayes Core scoreboard
|
||||||
|
|
||||||
|
| metric | value |
|
||||||
|
| ----------------- | ----: |
|
||||||
|
| chunks available | 638 |
|
||||||
|
| chunks fed | 638 |
|
||||||
|
| total | 638 |
|
||||||
|
| pass | 632 |
|
||||||
|
| fail | 6 |
|
||||||
|
| error | 0 |
|
||||||
|
| percent | 99% |
|
||||||
|
|
||||||
|
- **Source**: `gerryjackson/forth2012-test-suite` `src/core.fr`
|
||||||
|
- **Generated**: 2026-05-05T21:30:21Z
|
||||||
|
- **Note**: completed
|
||||||
|
|
||||||
|
A "chunk" is any preprocessed segment ending at a `}T` (every Hayes test
|
||||||
|
is one chunk, plus the small declaration blocks between tests).
|
||||||
|
The runner catches raised errors at chunk boundaries so one bad chunk
|
||||||
|
does not abort the rest. `error` covers chunks that raised; `fail`
|
||||||
|
covers tests whose `->` / `}T` comparison mismatched.
|
||||||
|
|
||||||
|
### Chunk cap
|
||||||
|
|
||||||
|
`conformance.sh` processes the first `$MAX_CHUNKS` chunks (default
|
||||||
|
**638**, i.e. the whole Hayes Core file). Lower the cap temporarily
|
||||||
|
while iterating on primitives if a regression re-opens an infinite
|
||||||
|
loop in later tests.
|
||||||
239
lib/forth/tests/test-phase3.sx
Normal file
239
lib/forth/tests/test-phase3.sx
Normal file
@@ -0,0 +1,239 @@
|
|||||||
|
;; Phase 3 — control flow (IF/ELSE/THEN, BEGIN/UNTIL/WHILE/REPEAT/AGAIN,
|
||||||
|
;; DO/LOOP, return stack). Grows as each control construct lands.
|
||||||
|
|
||||||
|
(define forth-p3-passed 0)
|
||||||
|
(define forth-p3-failed 0)
|
||||||
|
(define forth-p3-failures (list))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p3-assert
|
||||||
|
(fn
|
||||||
|
(label expected actual)
|
||||||
|
(if
|
||||||
|
(= expected actual)
|
||||||
|
(set! forth-p3-passed (+ forth-p3-passed 1))
|
||||||
|
(begin
|
||||||
|
(set! forth-p3-failed (+ forth-p3-failed 1))
|
||||||
|
(set!
|
||||||
|
forth-p3-failures
|
||||||
|
(concat
|
||||||
|
forth-p3-failures
|
||||||
|
(list
|
||||||
|
(str label ": expected " (str expected) " got " (str actual)))))))))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p3-check-stack
|
||||||
|
(fn
|
||||||
|
(label src expected)
|
||||||
|
(let ((r (forth-run src))) (forth-p3-assert label expected (nth r 2)))))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p3-if-tests
|
||||||
|
(fn
|
||||||
|
()
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"IF taken (-1)"
|
||||||
|
": Q -1 IF 10 THEN ; Q"
|
||||||
|
(list 10))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"IF not taken (0)"
|
||||||
|
": Q 0 IF 10 THEN ; Q"
|
||||||
|
(list))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"IF with non-zero truthy"
|
||||||
|
": Q 42 IF 10 THEN ; Q"
|
||||||
|
(list 10))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"IF ELSE — true branch"
|
||||||
|
": Q -1 IF 10 ELSE 20 THEN ; Q"
|
||||||
|
(list 10))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"IF ELSE — false branch"
|
||||||
|
": Q 0 IF 10 ELSE 20 THEN ; Q"
|
||||||
|
(list 20))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"IF consumes flag"
|
||||||
|
": Q IF 1 ELSE 2 THEN ; 0 Q"
|
||||||
|
(list 2))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"absolute value via IF"
|
||||||
|
": ABS2 DUP 0 < IF NEGATE THEN ; -7 ABS2"
|
||||||
|
(list 7))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"abs leaves positive alone"
|
||||||
|
": ABS2 DUP 0 < IF NEGATE THEN ; 7 ABS2"
|
||||||
|
(list 7))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"sign: negative"
|
||||||
|
": SIGN DUP 0 < IF DROP -1 ELSE DROP 1 THEN ; -3 SIGN"
|
||||||
|
(list -1))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"sign: positive"
|
||||||
|
": SIGN DUP 0 < IF DROP -1 ELSE DROP 1 THEN ; 3 SIGN"
|
||||||
|
(list 1))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"nested IF (both true)"
|
||||||
|
": Q 1 IF 1 IF 10 ELSE 20 THEN ELSE 30 THEN ; Q"
|
||||||
|
(list 10))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"nested IF (inner false)"
|
||||||
|
": Q 1 IF 0 IF 10 ELSE 20 THEN ELSE 30 THEN ; Q"
|
||||||
|
(list 20))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"nested IF (outer false)"
|
||||||
|
": Q 0 IF 0 IF 10 ELSE 20 THEN ELSE 30 THEN ; Q"
|
||||||
|
(list 30))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"IF before other ops"
|
||||||
|
": Q 1 IF 5 ELSE 6 THEN 2 * ; Q"
|
||||||
|
(list 10))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"IF in chained def"
|
||||||
|
": POS? 0 > ;
|
||||||
|
: DOUBLE-IF-POS DUP POS? IF 2 * THEN ;
|
||||||
|
3 DOUBLE-IF-POS"
|
||||||
|
(list 6))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"empty then branch"
|
||||||
|
": Q 1 IF THEN 99 ; Q"
|
||||||
|
(list 99))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"empty else branch"
|
||||||
|
": Q 0 IF 99 ELSE THEN ; Q"
|
||||||
|
(list))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"sequential IF blocks"
|
||||||
|
": Q -1 IF 1 THEN -1 IF 2 THEN ; Q"
|
||||||
|
(list 1 2))))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p3-loop-tests
|
||||||
|
(fn
|
||||||
|
()
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"BEGIN UNTIL (countdown to zero)"
|
||||||
|
": CD BEGIN 1- DUP 0 = UNTIL ; 3 CD"
|
||||||
|
(list 0))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"BEGIN UNTIL — single pass (UNTIL true immediately)"
|
||||||
|
": Q BEGIN -1 UNTIL 42 ; Q"
|
||||||
|
(list 42))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"BEGIN UNTIL — accumulate sum 1+2+3"
|
||||||
|
": SUM3 0 3 BEGIN TUCK + SWAP 1- DUP 0 = UNTIL DROP ; SUM3"
|
||||||
|
(list 6))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"BEGIN WHILE REPEAT — triangular sum 5"
|
||||||
|
": TRI 0 5 BEGIN DUP 0 > WHILE TUCK + SWAP 1- REPEAT DROP ; TRI"
|
||||||
|
(list 15))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"BEGIN WHILE REPEAT — zero iterations"
|
||||||
|
": TRI 0 0 BEGIN DUP 0 > WHILE TUCK + SWAP 1- REPEAT DROP ; TRI"
|
||||||
|
(list 0))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"BEGIN WHILE REPEAT — one iteration"
|
||||||
|
": TRI 0 1 BEGIN DUP 0 > WHILE TUCK + SWAP 1- REPEAT DROP ; TRI"
|
||||||
|
(list 1))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"nested BEGIN UNTIL"
|
||||||
|
": INNER BEGIN 1- DUP 0 = UNTIL DROP ;
|
||||||
|
: OUTER BEGIN 3 INNER 1- DUP 0 = UNTIL ;
|
||||||
|
2 OUTER"
|
||||||
|
(list 0))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"BEGIN UNTIL after colon prefix"
|
||||||
|
": TEN 10 ;
|
||||||
|
: CD TEN BEGIN 1- DUP 0 = UNTIL ;
|
||||||
|
CD"
|
||||||
|
(list 0))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"WHILE inside IF branch"
|
||||||
|
": Q 1 IF 0 3 BEGIN DUP 0 > WHILE TUCK + SWAP 1- REPEAT DROP ELSE 99 THEN ; Q"
|
||||||
|
(list 6))))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p3-do-tests
|
||||||
|
(fn
|
||||||
|
()
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"DO LOOP — simple sum 0..4"
|
||||||
|
": SUM 0 5 0 DO I + LOOP ; SUM"
|
||||||
|
(list 10))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"DO LOOP — 10..14 sum using I"
|
||||||
|
": SUM 0 15 10 DO I + LOOP ; SUM"
|
||||||
|
(list 60))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"DO LOOP — limit = start runs one pass"
|
||||||
|
": SUM 0 5 5 DO I + LOOP ; SUM"
|
||||||
|
(list 5))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"DO LOOP — count iterations"
|
||||||
|
": COUNT 0 4 0 DO 1+ LOOP ; COUNT"
|
||||||
|
(list 4))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"DO LOOP — nested, I inner / J outer"
|
||||||
|
": MATRIX 0 3 0 DO 3 0 DO I J + + LOOP LOOP ; MATRIX"
|
||||||
|
(list 18))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"DO LOOP — I used in arithmetic"
|
||||||
|
": DBL 0 5 1 DO I 2 * + LOOP ; DBL"
|
||||||
|
(list 20))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"+LOOP — count by 2"
|
||||||
|
": Q 0 10 0 DO I + 2 +LOOP ; Q"
|
||||||
|
(list 20))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"+LOOP — count by 3"
|
||||||
|
": Q 0 10 0 DO I + 3 +LOOP ; Q"
|
||||||
|
(list 18))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"+LOOP — negative step"
|
||||||
|
": Q 0 0 10 DO I + -1 +LOOP ; Q"
|
||||||
|
(list 55))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"LEAVE — early exit at I=3"
|
||||||
|
": Q 0 10 0 DO I 3 = IF LEAVE THEN I + LOOP ; Q"
|
||||||
|
(list 3))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"LEAVE — in nested loop exits only inner"
|
||||||
|
": Q 0 3 0 DO 5 0 DO I 2 = IF LEAVE THEN I + LOOP LOOP ; Q"
|
||||||
|
(list 3))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"DO LOOP preserves outer stack"
|
||||||
|
": Q 99 5 0 DO I + LOOP ; Q"
|
||||||
|
(list 109))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
">R R>"
|
||||||
|
": Q 7 >R 11 R> ; Q"
|
||||||
|
(list 11 7))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
">R R@ R>"
|
||||||
|
": Q 7 >R R@ R> ; Q"
|
||||||
|
(list 7 7))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"2>R 2R>"
|
||||||
|
": Q 1 2 2>R 99 2R> ; Q"
|
||||||
|
(list 99 1 2))
|
||||||
|
(forth-p3-check-stack
|
||||||
|
"2>R 2R@ 2R>"
|
||||||
|
": Q 3 4 2>R 2R@ 2R> ; Q"
|
||||||
|
(list 3 4 3 4))))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p3-run-all
|
||||||
|
(fn
|
||||||
|
()
|
||||||
|
(set! forth-p3-passed 0)
|
||||||
|
(set! forth-p3-failed 0)
|
||||||
|
(set! forth-p3-failures (list))
|
||||||
|
(forth-p3-if-tests)
|
||||||
|
(forth-p3-loop-tests)
|
||||||
|
(forth-p3-do-tests)
|
||||||
|
(dict
|
||||||
|
"passed"
|
||||||
|
forth-p3-passed
|
||||||
|
"failed"
|
||||||
|
forth-p3-failed
|
||||||
|
"failures"
|
||||||
|
forth-p3-failures)))
|
||||||
268
lib/forth/tests/test-phase4.sx
Normal file
268
lib/forth/tests/test-phase4.sx
Normal file
@@ -0,0 +1,268 @@
|
|||||||
|
;; Phase 4 — strings + more Core.
|
||||||
|
;; Uses the byte-memory model on state ("mem" dict + "here" cursor).
|
||||||
|
|
||||||
|
(define forth-p4-passed 0)
|
||||||
|
(define forth-p4-failed 0)
|
||||||
|
(define forth-p4-failures (list))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p4-assert
|
||||||
|
(fn
|
||||||
|
(label expected actual)
|
||||||
|
(if
|
||||||
|
(= expected actual)
|
||||||
|
(set! forth-p4-passed (+ forth-p4-passed 1))
|
||||||
|
(begin
|
||||||
|
(set! forth-p4-failed (+ forth-p4-failed 1))
|
||||||
|
(set!
|
||||||
|
forth-p4-failures
|
||||||
|
(concat
|
||||||
|
forth-p4-failures
|
||||||
|
(list
|
||||||
|
(str label ": expected " (str expected) " got " (str actual)))))))))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p4-check-output
|
||||||
|
(fn
|
||||||
|
(label src expected)
|
||||||
|
(let ((r (forth-run src))) (forth-p4-assert label expected (nth r 1)))))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p4-check-stack-size
|
||||||
|
(fn
|
||||||
|
(label src expected-n)
|
||||||
|
(let
|
||||||
|
((r (forth-run src)))
|
||||||
|
(forth-p4-assert label expected-n (len (nth r 2))))))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p4-check-top
|
||||||
|
(fn
|
||||||
|
(label src expected)
|
||||||
|
(let
|
||||||
|
((r (forth-run src)))
|
||||||
|
(let
|
||||||
|
((stk (nth r 2)))
|
||||||
|
(forth-p4-assert label expected (nth stk (- (len stk) 1)))))))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p4-check-typed
|
||||||
|
(fn
|
||||||
|
(label src expected)
|
||||||
|
(forth-p4-check-output label (str src " TYPE") expected)))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p4-string-tests
|
||||||
|
(fn
|
||||||
|
()
|
||||||
|
(forth-p4-check-typed
|
||||||
|
"S\" + TYPE — hello"
|
||||||
|
"S\" HELLO\""
|
||||||
|
"HELLO")
|
||||||
|
(forth-p4-check-typed
|
||||||
|
"S\" + TYPE — two words"
|
||||||
|
"S\" HELLO WORLD\""
|
||||||
|
"HELLO WORLD")
|
||||||
|
(forth-p4-check-typed
|
||||||
|
"S\" + TYPE — empty"
|
||||||
|
"S\" \""
|
||||||
|
"")
|
||||||
|
(forth-p4-check-typed
|
||||||
|
"S\" + TYPE — single char"
|
||||||
|
"S\" X\""
|
||||||
|
"X")
|
||||||
|
(forth-p4-check-stack-size
|
||||||
|
"S\" pushes (addr len)"
|
||||||
|
"S\" HI\""
|
||||||
|
2)
|
||||||
|
(forth-p4-check-top
|
||||||
|
"S\" length is correct"
|
||||||
|
"S\" HELLO\""
|
||||||
|
5)
|
||||||
|
(forth-p4-check-output
|
||||||
|
".\" prints at interpret time"
|
||||||
|
".\" HELLO\""
|
||||||
|
"HELLO")
|
||||||
|
(forth-p4-check-output
|
||||||
|
".\" in colon def"
|
||||||
|
": GREET .\" HI \" ; GREET GREET"
|
||||||
|
"HI HI ")))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p4-count-tests
|
||||||
|
(fn
|
||||||
|
()
|
||||||
|
(forth-p4-check-typed
|
||||||
|
"C\" + COUNT + TYPE"
|
||||||
|
"C\" ABC\" COUNT"
|
||||||
|
"ABC")
|
||||||
|
(forth-p4-check-typed
|
||||||
|
"C\" then COUNT leaves right len"
|
||||||
|
"C\" HI THERE\" COUNT"
|
||||||
|
"HI THERE")))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p4-fill-tests
|
||||||
|
(fn
|
||||||
|
()
|
||||||
|
(forth-p4-check-typed
|
||||||
|
"FILL overwrites prefix bytes"
|
||||||
|
"S\" ABCDE\" 2DUP DROP 3 65 FILL"
|
||||||
|
"AAADE")
|
||||||
|
(forth-p4-check-typed
|
||||||
|
"BLANK sets spaces"
|
||||||
|
"S\" XYZAB\" 2DUP DROP 3 BLANK"
|
||||||
|
" AB")))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p4-cmove-tests
|
||||||
|
(fn
|
||||||
|
()
|
||||||
|
(forth-p4-check-output
|
||||||
|
"CMOVE copies HELLO forward"
|
||||||
|
": MKH 72 0 C! 69 1 C! 76 2 C! 76 3 C! 79 4 C! ;
|
||||||
|
: T MKH 0 10 5 CMOVE 10 5 TYPE ; T"
|
||||||
|
"HELLO")
|
||||||
|
(forth-p4-check-output
|
||||||
|
"CMOVE> copies overlapping backward"
|
||||||
|
": MKA 65 0 C! 66 1 C! 67 2 C! ;
|
||||||
|
: T MKA 0 1 2 CMOVE> 0 3 TYPE ; T"
|
||||||
|
"AAB")
|
||||||
|
(forth-p4-check-output
|
||||||
|
"MOVE picks direction for overlap"
|
||||||
|
": MKA 65 0 C! 66 1 C! 67 2 C! ;
|
||||||
|
: T MKA 0 1 2 MOVE 0 3 TYPE ; T"
|
||||||
|
"AAB")))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p4-charplus-tests
|
||||||
|
(fn
|
||||||
|
()
|
||||||
|
(forth-p4-check-top
|
||||||
|
"CHAR+ increments"
|
||||||
|
"5 CHAR+"
|
||||||
|
6)))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p4-char-tests
|
||||||
|
(fn
|
||||||
|
()
|
||||||
|
(forth-p4-check-top "CHAR A -> 65" "CHAR A" 65)
|
||||||
|
(forth-p4-check-top "CHAR x -> 120" "CHAR x" 120)
|
||||||
|
(forth-p4-check-top "CHAR takes only first char" "CHAR HELLO" 72)
|
||||||
|
(forth-p4-check-top
|
||||||
|
"[CHAR] compiles literal"
|
||||||
|
": AA [CHAR] A ; AA"
|
||||||
|
65)
|
||||||
|
(forth-p4-check-top
|
||||||
|
"[CHAR] reads past IMMEDIATE"
|
||||||
|
": ZZ [CHAR] Z ; ZZ"
|
||||||
|
90)
|
||||||
|
(forth-p4-check-stack-size
|
||||||
|
"[CHAR] doesn't leak at compile time"
|
||||||
|
": FOO [CHAR] A ; "
|
||||||
|
0)))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p4-key-accept-tests
|
||||||
|
(fn
|
||||||
|
()
|
||||||
|
(let
|
||||||
|
((r (forth-run "1000 2 ACCEPT")))
|
||||||
|
(let ((stk (nth r 2))) (forth-p4-assert "ACCEPT empty buf -> 0" (list 0) stk)))))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p4-shift-tests
|
||||||
|
(fn
|
||||||
|
()
|
||||||
|
(forth-p4-check-top "1 0 LSHIFT" "1 0 LSHIFT" 1)
|
||||||
|
(forth-p4-check-top "1 1 LSHIFT" "1 1 LSHIFT" 2)
|
||||||
|
(forth-p4-check-top "1 2 LSHIFT" "1 2 LSHIFT" 4)
|
||||||
|
(forth-p4-check-top "1 15 LSHIFT" "1 15 LSHIFT" 32768)
|
||||||
|
(forth-p4-check-top "1 31 LSHIFT" "1 31 LSHIFT" -2147483648)
|
||||||
|
(forth-p4-check-top "1 0 RSHIFT" "1 0 RSHIFT" 1)
|
||||||
|
(forth-p4-check-top "1 1 RSHIFT" "1 1 RSHIFT" 0)
|
||||||
|
(forth-p4-check-top "2 1 RSHIFT" "2 1 RSHIFT" 1)
|
||||||
|
(forth-p4-check-top "4 2 RSHIFT" "4 2 RSHIFT" 1)
|
||||||
|
(forth-p4-check-top "-1 1 RSHIFT (logical, not arithmetic)" "-1 1 RSHIFT" 2147483647)
|
||||||
|
(forth-p4-check-top "MSB via 1S 1 RSHIFT INVERT" "0 INVERT 1 RSHIFT INVERT" -2147483648)))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p4-sp-tests
|
||||||
|
(fn
|
||||||
|
()
|
||||||
|
(forth-p4-check-top "SP@ returns depth (0)" "SP@" 0)
|
||||||
|
(forth-p4-check-top
|
||||||
|
"SP@ after pushes"
|
||||||
|
"1 2 3 SP@ SWAP DROP SWAP DROP SWAP DROP"
|
||||||
|
3)
|
||||||
|
(forth-p4-check-stack-size
|
||||||
|
"SP! truncates"
|
||||||
|
"1 2 3 4 5 2 SP!"
|
||||||
|
2)
|
||||||
|
(forth-p4-check-top
|
||||||
|
"SP! leaves base items intact"
|
||||||
|
"1 2 3 4 5 2 SP!"
|
||||||
|
2)))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p4-base-tests
|
||||||
|
(fn
|
||||||
|
()
|
||||||
|
(forth-p4-check-top
|
||||||
|
"BASE default is 10"
|
||||||
|
"BASE @"
|
||||||
|
10)
|
||||||
|
(forth-p4-check-top
|
||||||
|
"HEX switches base to 16"
|
||||||
|
"HEX BASE @"
|
||||||
|
16)
|
||||||
|
(forth-p4-check-top
|
||||||
|
"DECIMAL resets to 10"
|
||||||
|
"HEX DECIMAL BASE @"
|
||||||
|
10)
|
||||||
|
(forth-p4-check-top
|
||||||
|
"HEX parses 10 as 16"
|
||||||
|
"HEX 10"
|
||||||
|
16)
|
||||||
|
(forth-p4-check-top
|
||||||
|
"HEX parses FF as 255"
|
||||||
|
"HEX FF"
|
||||||
|
255)
|
||||||
|
(forth-p4-check-top
|
||||||
|
"DECIMAL parses 10 as 10"
|
||||||
|
"HEX DECIMAL 10"
|
||||||
|
10)
|
||||||
|
(forth-p4-check-top
|
||||||
|
"OCTAL parses 17 as 15"
|
||||||
|
"OCTAL 17"
|
||||||
|
15)
|
||||||
|
(forth-p4-check-top
|
||||||
|
"BASE @ ; 16 BASE ! ; BASE @"
|
||||||
|
"BASE @ 16 BASE ! BASE @ SWAP DROP"
|
||||||
|
16)))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p4-run-all
|
||||||
|
(fn
|
||||||
|
()
|
||||||
|
(set! forth-p4-passed 0)
|
||||||
|
(set! forth-p4-failed 0)
|
||||||
|
(set! forth-p4-failures (list))
|
||||||
|
(forth-p4-string-tests)
|
||||||
|
(forth-p4-count-tests)
|
||||||
|
(forth-p4-fill-tests)
|
||||||
|
(forth-p4-cmove-tests)
|
||||||
|
(forth-p4-charplus-tests)
|
||||||
|
(forth-p4-char-tests)
|
||||||
|
(forth-p4-key-accept-tests)
|
||||||
|
(forth-p4-base-tests)
|
||||||
|
(forth-p4-shift-tests)
|
||||||
|
(forth-p4-sp-tests)
|
||||||
|
(dict
|
||||||
|
"passed"
|
||||||
|
forth-p4-passed
|
||||||
|
"failed"
|
||||||
|
forth-p4-failed
|
||||||
|
"failures"
|
||||||
|
forth-p4-failures)))
|
||||||
333
lib/forth/tests/test-phase5.sx
Normal file
333
lib/forth/tests/test-phase5.sx
Normal file
@@ -0,0 +1,333 @@
|
|||||||
|
;; Phase 5 — Core Extension + memory primitives.
|
||||||
|
|
||||||
|
(define forth-p5-passed 0)
|
||||||
|
(define forth-p5-failed 0)
|
||||||
|
(define forth-p5-failures (list))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p5-assert
|
||||||
|
(fn
|
||||||
|
(label expected actual)
|
||||||
|
(if
|
||||||
|
(= expected actual)
|
||||||
|
(set! forth-p5-passed (+ forth-p5-passed 1))
|
||||||
|
(begin
|
||||||
|
(set! forth-p5-failed (+ forth-p5-failed 1))
|
||||||
|
(set!
|
||||||
|
forth-p5-failures
|
||||||
|
(concat
|
||||||
|
forth-p5-failures
|
||||||
|
(list
|
||||||
|
(str label ": expected " (str expected) " got " (str actual)))))))))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p5-check-stack
|
||||||
|
(fn
|
||||||
|
(label src expected)
|
||||||
|
(let ((r (forth-run src))) (forth-p5-assert label expected (nth r 2)))))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p5-check-top
|
||||||
|
(fn
|
||||||
|
(label src expected)
|
||||||
|
(let
|
||||||
|
((r (forth-run src)))
|
||||||
|
(let
|
||||||
|
((stk (nth r 2)))
|
||||||
|
(forth-p5-assert label expected (nth stk (- (len stk) 1)))))))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p5-create-tests
|
||||||
|
(fn
|
||||||
|
()
|
||||||
|
(forth-p5-check-top
|
||||||
|
"CREATE pushes HERE-at-creation"
|
||||||
|
"HERE CREATE FOO FOO ="
|
||||||
|
-1)
|
||||||
|
(forth-p5-check-top
|
||||||
|
"CREATE + ALLOT advances HERE"
|
||||||
|
"HERE 5 ALLOT HERE SWAP -"
|
||||||
|
5)
|
||||||
|
(forth-p5-check-top
|
||||||
|
"CREATE + , stores cell"
|
||||||
|
"CREATE FOO 42 , FOO @"
|
||||||
|
42)
|
||||||
|
(forth-p5-check-stack
|
||||||
|
"CREATE multiple ,"
|
||||||
|
"CREATE TBL 1 , 2 , 3 , TBL @ TBL CELL+ @ TBL CELL+ CELL+ @"
|
||||||
|
(list 1 2 3))
|
||||||
|
(forth-p5-check-top
|
||||||
|
"C, stores byte"
|
||||||
|
"CREATE B 65 C, 66 C, B C@"
|
||||||
|
65)))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p5-unsigned-tests
|
||||||
|
(fn
|
||||||
|
()
|
||||||
|
(forth-p5-check-top "1 2 U<" "1 2 U<" -1)
|
||||||
|
(forth-p5-check-top "2 1 U<" "2 1 U<" 0)
|
||||||
|
(forth-p5-check-top "0 1 U<" "0 1 U<" -1)
|
||||||
|
(forth-p5-check-top "-1 1 U< (since -1 unsigned is huge)" "-1 1 U<" 0)
|
||||||
|
(forth-p5-check-top "1 -1 U<" "1 -1 U<" -1)
|
||||||
|
(forth-p5-check-top "1 2 U>" "1 2 U>" 0)
|
||||||
|
(forth-p5-check-top "-1 1 U>" "-1 1 U>" -1)))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p5-2bang-tests
|
||||||
|
(fn
|
||||||
|
()
|
||||||
|
(forth-p5-check-stack
|
||||||
|
"2! / 2@"
|
||||||
|
"CREATE X 0 , 0 , 11 22 X 2! X 2@"
|
||||||
|
(list 11 22))))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p5-mixed-tests
|
||||||
|
(fn
|
||||||
|
()
|
||||||
|
(forth-p5-check-stack "S>D positive" "5 S>D" (list 5 0))
|
||||||
|
(forth-p5-check-stack "S>D negative" "-5 S>D" (list -5 -1))
|
||||||
|
(forth-p5-check-stack "S>D zero" "0 S>D" (list 0 0))
|
||||||
|
(forth-p5-check-top "D>S keeps low" "5 0 D>S" 5)
|
||||||
|
(forth-p5-check-stack "M* small positive" "3 4 M*" (list 12 0))
|
||||||
|
(forth-p5-check-stack "M* negative" "-3 4 M*" (list -12 -1))
|
||||||
|
(forth-p5-check-stack
|
||||||
|
"M* negative * negative"
|
||||||
|
"-3 -4 M*"
|
||||||
|
(list 12 0))
|
||||||
|
(forth-p5-check-stack "UM* small" "3 4 UM*" (list 12 0))
|
||||||
|
(forth-p5-check-stack
|
||||||
|
"UM/MOD: 100 0 / 5"
|
||||||
|
"100 0 5 UM/MOD"
|
||||||
|
(list 0 20))
|
||||||
|
(forth-p5-check-stack
|
||||||
|
"FM/MOD: -7 / 2 floored"
|
||||||
|
"-7 -1 2 FM/MOD"
|
||||||
|
(list 1 -4))
|
||||||
|
(forth-p5-check-stack
|
||||||
|
"SM/REM: -7 / 2 truncated"
|
||||||
|
"-7 -1 2 SM/REM"
|
||||||
|
(list -1 -3))
|
||||||
|
(forth-p5-check-top "*/ truncated" "7 11 13 */" 5)
|
||||||
|
(forth-p5-check-stack "*/MOD" "7 11 13 */MOD" (list 12 5))))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p5-double-tests
|
||||||
|
(fn
|
||||||
|
()
|
||||||
|
(forth-p5-check-stack "D+ small" "5 0 7 0 D+" (list 12 0))
|
||||||
|
(forth-p5-check-stack "D+ negative" "-5 -1 -3 -1 D+" (list -8 -1))
|
||||||
|
(forth-p5-check-stack "D- small" "10 0 3 0 D-" (list 7 0))
|
||||||
|
(forth-p5-check-stack "DNEGATE positive" "5 0 DNEGATE" (list -5 -1))
|
||||||
|
(forth-p5-check-stack "DNEGATE negative" "-5 -1 DNEGATE" (list 5 0))
|
||||||
|
(forth-p5-check-stack "DABS negative" "-7 -1 DABS" (list 7 0))
|
||||||
|
(forth-p5-check-stack "DABS positive" "7 0 DABS" (list 7 0))
|
||||||
|
(forth-p5-check-top "D= equal" "5 0 5 0 D=" -1)
|
||||||
|
(forth-p5-check-top "D= unequal lo" "5 0 7 0 D=" 0)
|
||||||
|
(forth-p5-check-top "D= unequal hi" "5 0 5 1 D=" 0)
|
||||||
|
(forth-p5-check-top "D< lt" "5 0 7 0 D<" -1)
|
||||||
|
(forth-p5-check-top "D< gt" "7 0 5 0 D<" 0)
|
||||||
|
(forth-p5-check-top "D0= zero" "0 0 D0=" -1)
|
||||||
|
(forth-p5-check-top "D0= nonzero" "5 0 D0=" 0)
|
||||||
|
(forth-p5-check-top "D0< neg" "-5 -1 D0<" -1)
|
||||||
|
(forth-p5-check-top "D0< pos" "5 0 D0<" 0)
|
||||||
|
(forth-p5-check-stack "DMAX" "5 0 7 0 DMAX" (list 7 0))
|
||||||
|
(forth-p5-check-stack "DMIN" "5 0 7 0 DMIN" (list 5 0))))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p5-format-tests
|
||||||
|
(fn
|
||||||
|
()
|
||||||
|
(forth-p4-check-output-passthrough
|
||||||
|
"U. prints with trailing space"
|
||||||
|
"123 U."
|
||||||
|
"123 ")
|
||||||
|
(forth-p4-check-output-passthrough
|
||||||
|
"<# #S #> TYPE — decimal"
|
||||||
|
"123 0 <# #S #> TYPE"
|
||||||
|
"123")
|
||||||
|
(forth-p4-check-output-passthrough
|
||||||
|
"<# #S #> TYPE — hex"
|
||||||
|
"255 HEX 0 <# #S #> TYPE"
|
||||||
|
"FF")
|
||||||
|
(forth-p4-check-output-passthrough
|
||||||
|
"<# # # #> partial"
|
||||||
|
"1234 0 <# # # #> TYPE"
|
||||||
|
"34")
|
||||||
|
(forth-p4-check-output-passthrough
|
||||||
|
"SIGN holds minus"
|
||||||
|
"<# -1 SIGN -1 SIGN 0 0 #> TYPE"
|
||||||
|
"--")
|
||||||
|
(forth-p4-check-output-passthrough
|
||||||
|
".R right-justifies"
|
||||||
|
"42 5 .R"
|
||||||
|
" 42")
|
||||||
|
(forth-p4-check-output-passthrough
|
||||||
|
".R negative"
|
||||||
|
"-42 5 .R"
|
||||||
|
" -42")
|
||||||
|
(forth-p4-check-output-passthrough
|
||||||
|
"U.R"
|
||||||
|
"42 5 U.R"
|
||||||
|
" 42")
|
||||||
|
(forth-p4-check-output-passthrough
|
||||||
|
"HOLD char"
|
||||||
|
"<# 0 0 65 HOLD #> TYPE"
|
||||||
|
"A")))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p5-dict-tests
|
||||||
|
(fn
|
||||||
|
()
|
||||||
|
(forth-p5-check-top
|
||||||
|
"EXECUTE via tick"
|
||||||
|
": INC 1+ ; 9 ' INC EXECUTE"
|
||||||
|
10)
|
||||||
|
(forth-p5-check-top
|
||||||
|
"['] inside def"
|
||||||
|
": DUB 2* ; : APPLY ['] DUB EXECUTE ; 5 APPLY"
|
||||||
|
10)
|
||||||
|
(forth-p5-check-top
|
||||||
|
">BODY of CREATE word"
|
||||||
|
"CREATE C 99 , ' C >BODY @"
|
||||||
|
99)
|
||||||
|
(forth-p5-check-stack
|
||||||
|
"WORD parses next token to counted-string"
|
||||||
|
": A 5 ; BL WORD A COUNT TYPE"
|
||||||
|
(list))
|
||||||
|
(forth-p5-check-top
|
||||||
|
"FIND on known word -> non-zero"
|
||||||
|
": A 5 ; BL WORD A FIND SWAP DROP"
|
||||||
|
-1)))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p5-state-tests
|
||||||
|
(fn
|
||||||
|
()
|
||||||
|
(forth-p5-check-top
|
||||||
|
"STATE @ in interpret mode"
|
||||||
|
"STATE @"
|
||||||
|
0)
|
||||||
|
(forth-p5-check-top
|
||||||
|
"STATE @ via IMMEDIATE inside compile"
|
||||||
|
": GT8 STATE @ ; IMMEDIATE : T GT8 LITERAL ; T"
|
||||||
|
-1)
|
||||||
|
(forth-p5-check-top
|
||||||
|
"[ ] LITERAL captures"
|
||||||
|
": SEVEN [ 7 ] LITERAL ; SEVEN"
|
||||||
|
7)
|
||||||
|
(forth-p5-check-top
|
||||||
|
"EVALUATE in interpret mode"
|
||||||
|
"S\" 5 7 +\" EVALUATE"
|
||||||
|
12)
|
||||||
|
(forth-p5-check-top
|
||||||
|
"EVALUATE inside def"
|
||||||
|
": A 100 ; : B S\" A\" EVALUATE ; B"
|
||||||
|
100)))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p5-misc-tests
|
||||||
|
(fn
|
||||||
|
()
|
||||||
|
(forth-p5-check-top "WITHIN inclusive lower" "3 2 10 WITHIN" -1)
|
||||||
|
(forth-p5-check-top "WITHIN exclusive upper" "10 2 10 WITHIN" 0)
|
||||||
|
(forth-p5-check-top "WITHIN below range" "1 2 10 WITHIN" 0)
|
||||||
|
(forth-p5-check-top "WITHIN at lower" "2 2 10 WITHIN" -1)
|
||||||
|
(forth-p5-check-top
|
||||||
|
"EXIT leaves colon-def early"
|
||||||
|
": F 5 EXIT 99 ; F"
|
||||||
|
5)
|
||||||
|
(forth-p5-check-stack
|
||||||
|
"EXIT in IF branch"
|
||||||
|
": F 5 0 IF DROP 99 EXIT THEN ; F"
|
||||||
|
(list 5))
|
||||||
|
(forth-p5-check-top
|
||||||
|
"UNLOOP + EXIT in DO"
|
||||||
|
": SUM 0 10 0 DO I 5 = IF I UNLOOP EXIT THEN LOOP ; SUM"
|
||||||
|
5)))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p5-fa-tests
|
||||||
|
(fn
|
||||||
|
()
|
||||||
|
(forth-p5-check-top
|
||||||
|
"R/O R/W W/O constants"
|
||||||
|
"R/O R/W W/O + +"
|
||||||
|
3)
|
||||||
|
(forth-p5-check-top
|
||||||
|
"CREATE-FILE returns ior=0"
|
||||||
|
"CREATE PAD 50 ALLOT PAD S\" /tmp/test.fxf\" ROT SWAP CMOVE S\" /tmp/test.fxf\" R/W CREATE-FILE SWAP DROP"
|
||||||
|
0)
|
||||||
|
(forth-p5-check-top
|
||||||
|
"WRITE-FILE then CLOSE"
|
||||||
|
"S\" /tmp/t2.fxf\" R/W CREATE-FILE DROP >R S\" HI\" R@ WRITE-FILE R> CLOSE-FILE +"
|
||||||
|
0)
|
||||||
|
(forth-p5-check-top
|
||||||
|
"OPEN-FILE on unknown path returns ior!=0"
|
||||||
|
"S\" /tmp/nope.fxf\" R/O OPEN-FILE SWAP DROP 0 ="
|
||||||
|
0)))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p5-string-tests
|
||||||
|
(fn
|
||||||
|
()
|
||||||
|
(forth-p5-check-top "COMPARE equal" "S\" ABC\" S\" ABC\" COMPARE" 0)
|
||||||
|
(forth-p5-check-top "COMPARE less" "S\" ABC\" S\" ABD\" COMPARE" -1)
|
||||||
|
(forth-p5-check-top "COMPARE greater" "S\" ABD\" S\" ABC\" COMPARE" 1)
|
||||||
|
(forth-p5-check-top
|
||||||
|
"COMPARE prefix less"
|
||||||
|
"S\" AB\" S\" ABC\" COMPARE"
|
||||||
|
-1)
|
||||||
|
(forth-p5-check-top
|
||||||
|
"COMPARE prefix greater"
|
||||||
|
"S\" ABC\" S\" AB\" COMPARE"
|
||||||
|
1)
|
||||||
|
(forth-p5-check-top
|
||||||
|
"SEARCH found flag"
|
||||||
|
"S\" HELLO WORLD\" S\" WORLD\" SEARCH"
|
||||||
|
-1)
|
||||||
|
(forth-p5-check-top
|
||||||
|
"SEARCH not found flag"
|
||||||
|
"S\" HELLO\" S\" XYZ\" SEARCH"
|
||||||
|
0)
|
||||||
|
(forth-p5-check-top
|
||||||
|
"SEARCH empty needle flag"
|
||||||
|
"S\" HELLO\" S\" \" SEARCH"
|
||||||
|
-1)
|
||||||
|
(forth-p5-check-top
|
||||||
|
"SLITERAL via [ S\" ... \" ]"
|
||||||
|
": A [ S\" HI\" ] SLITERAL ; A SWAP DROP"
|
||||||
|
2)))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p4-check-output-passthrough
|
||||||
|
(fn
|
||||||
|
(label src expected)
|
||||||
|
(let ((r (forth-run src))) (forth-p5-assert label expected (nth r 1)))))
|
||||||
|
|
||||||
|
(define
|
||||||
|
forth-p5-run-all
|
||||||
|
(fn
|
||||||
|
()
|
||||||
|
(set! forth-p5-passed 0)
|
||||||
|
(set! forth-p5-failed 0)
|
||||||
|
(set! forth-p5-failures (list))
|
||||||
|
(forth-p5-create-tests)
|
||||||
|
(forth-p5-unsigned-tests)
|
||||||
|
(forth-p5-2bang-tests)
|
||||||
|
(forth-p5-mixed-tests)
|
||||||
|
(forth-p5-double-tests)
|
||||||
|
(forth-p5-format-tests)
|
||||||
|
(forth-p5-dict-tests)
|
||||||
|
(forth-p5-state-tests)
|
||||||
|
(forth-p5-misc-tests)
|
||||||
|
(forth-p5-fa-tests)
|
||||||
|
(forth-p5-string-tests)
|
||||||
|
(dict
|
||||||
|
"passed"
|
||||||
|
forth-p5-passed
|
||||||
|
"failed"
|
||||||
|
forth-p5-failed
|
||||||
|
"failures"
|
||||||
|
forth-p5-failures)))
|
||||||
@@ -11,7 +11,7 @@ isolation: worktree
|
|||||||
|
|
||||||
## Prompt
|
## Prompt
|
||||||
|
|
||||||
You are the sole background agent working `/root/rose-ash/plans/forth-on-sx.md`. Isolated worktree, forever, one commit per feature. Never push.
|
You are the sole background agent working `/root/rose-ash/plans/forth-on-sx.md`. Isolated worktree, forever, one commit per feature. Push to `origin/loops/forth` after every commit.
|
||||||
|
|
||||||
## Restart baseline — check before iterating
|
## Restart baseline — check before iterating
|
||||||
|
|
||||||
@@ -41,7 +41,7 @@ Every iteration: implement → test → commit → tick `[ ]` → append Progres
|
|||||||
- **NEVER call `sx_build`.** 600s watchdog. If sx_server binary broken → Blockers entry, stop.
|
- **NEVER call `sx_build`.** 600s watchdog. If sx_server binary broken → Blockers entry, stop.
|
||||||
- **Shared-file issues** → plan's Blockers with minimal repro.
|
- **Shared-file issues** → plan's Blockers with minimal repro.
|
||||||
- **SX files:** `sx-tree` MCP tools ONLY. `sx_validate` after edits.
|
- **SX files:** `sx-tree` MCP tools ONLY. `sx_validate` after edits.
|
||||||
- **Worktree:** commit locally. Never push. Never touch `main`.
|
- **Worktree:** commit, then push to `origin/loops/forth`. Never touch `main`.
|
||||||
- **Commit granularity:** one feature per commit.
|
- **Commit granularity:** one feature per commit.
|
||||||
- **Plan file:** update Progress log + tick boxes every commit.
|
- **Plan file:** update Progress log + tick boxes every commit.
|
||||||
|
|
||||||
|
|||||||
@@ -50,34 +50,34 @@ Core mapping:
|
|||||||
## Roadmap
|
## Roadmap
|
||||||
|
|
||||||
### Phase 1 — reader + parser
|
### Phase 1 — reader + parser
|
||||||
- [x] 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 `#| … |#`
|
- [ ] 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 `#| … |#`
|
||||||
- [x] Reader: list, dotted pair, quote `'`, function `#'`, quasiquote `` ` ``, unquote `,`, splice `,@`, vector `#(…)`, uninterned `#:foo`, nil/t literals
|
- [ ] Reader: list, dotted pair, quote `'`, function `#'`, quasiquote `` ` ``, unquote `,`, splice `,@`, vector `#(…)`, uninterned `#:foo`, nil/t literals
|
||||||
- [x] Parser: lambda lists with `&optional` `&rest` `&key` `&aux` `&allow-other-keys`, defaults, supplied-p variables
|
- [ ] Parser: lambda lists with `&optional` `&rest` `&key` `&aux` `&allow-other-keys`, defaults, supplied-p variables
|
||||||
- [x] Unit tests in `lib/common-lisp/tests/read.sx`
|
- [ ] Unit tests in `lib/common-lisp/tests/read.sx`
|
||||||
|
|
||||||
### Phase 2 — sequential eval + special forms
|
### Phase 2 — sequential eval + special forms
|
||||||
- [x] `cl-eval-ast`: `quote`, `if`, `progn`, `let`, `let*`, `flet`, `labels`, `setq`, `setf` (subset), `function`, `lambda`, `the`, `locally`, `eval-when`
|
- [ ] `cl-eval-ast`: `quote`, `if`, `progn`, `let`, `let*`, `flet`, `labels`, `setq`, `setf` (subset), `function`, `lambda`, `the`, `locally`, `eval-when`
|
||||||
- [x] `block` + `return-from` via captured continuation
|
- [ ] `block` + `return-from` via captured continuation
|
||||||
- [x] `tagbody` + `go` via per-tag continuations
|
- [ ] `tagbody` + `go` via per-tag continuations
|
||||||
- [x] `unwind-protect` cleanup frame
|
- [ ] `unwind-protect` cleanup frame
|
||||||
- [x] `multiple-value-bind`, `multiple-value-call`, `multiple-value-prog1`, `values`, `nth-value`
|
- [ ] `multiple-value-bind`, `multiple-value-call`, `multiple-value-prog1`, `values`, `nth-value`
|
||||||
- [x] `defun`, `defparameter`, `defvar`, `defconstant`, `declaim`, `proclaim` (no-op)
|
- [ ] `defun`, `defparameter`, `defvar`, `defconstant`, `declaim`, `proclaim` (no-op)
|
||||||
- [ ] Dynamic variables — `defvar`/`defparameter` produce specials; `let` rebinds via parameterize-style scope
|
- [ ] Dynamic variables — `defvar`/`defparameter` produce specials; `let` rebinds via parameterize-style scope
|
||||||
- [x] 127 tests in `lib/common-lisp/tests/eval.sx`
|
- [ ] 60+ tests in `lib/common-lisp/tests/eval.sx`
|
||||||
|
|
||||||
### Phase 3 — conditions + restarts (THE SHOWCASE)
|
### Phase 3 — conditions + restarts (THE SHOWCASE)
|
||||||
- [x] `define-condition` — class hierarchy rooted at `condition`/`error`/`warning`/`simple-error`/`simple-warning`/`type-error`/`arithmetic-error`/`division-by-zero`
|
- [ ] `define-condition` — class hierarchy rooted at `condition`/`error`/`warning`/`simple-error`/`simple-warning`/`type-error`/`arithmetic-error`/`division-by-zero`
|
||||||
- [x] `signal`, `error`, `cerror`, `warn` — all walk the handler chain
|
- [ ] `signal`, `error`, `cerror`, `warn` — all walk the handler chain
|
||||||
- [x] `handler-bind` — non-unwinding handlers, may decline by returning normally
|
- [ ] `handler-bind` — non-unwinding handlers, may decline by returning normally
|
||||||
- [x] `handler-case` — unwinding handlers (call/cc escape)
|
- [ ] `handler-case` — unwinding handlers (delcc abort)
|
||||||
- [x] `restart-case`, `with-simple-restart`, `restart-bind`
|
- [ ] `restart-case`, `with-simple-restart`, `restart-bind`
|
||||||
- [x] `find-restart`, `invoke-restart`, `compute-restarts`
|
- [ ] `find-restart`, `invoke-restart`, `invoke-restart-interactively`, `compute-restarts`
|
||||||
- [x] `with-condition-restarts` — associate restarts with a specific condition
|
- [ ] `with-condition-restarts` — associate restarts with a specific condition
|
||||||
- [x] `invoke-restart-interactively`, `*break-on-signals*`, `*debugger-hook*` (basic)
|
- [ ] `*break-on-signals*`, `*debugger-hook*` (basic)
|
||||||
- [x] Classic programs in `lib/common-lisp/tests/programs/`:
|
- [ ] Classic programs in `lib/common-lisp/tests/programs/`:
|
||||||
- [x] `restart-demo.sx` — division with `use-zero` and `retry` restarts (7 tests)
|
- [ ] `restart-demo.lisp` — division with `:use-zero` and `:retry` restarts
|
||||||
- [x] `parse-recover.sx` — parser with skipped-token restart (6 tests)
|
- [ ] `parse-recover.lisp` — parser with skipped-token restart
|
||||||
- [x] `interactive-debugger.sx` — policy-driven debugger hook, *debugger-hook* global (7 tests)
|
- [ ] `interactive-debugger.lisp` — ASCII REPL using `:debugger-hook`
|
||||||
- [ ] `lib/common-lisp/conformance.sh` + runner, `scoreboard.json` + `scoreboard.md`
|
- [ ] `lib/common-lisp/conformance.sh` + runner, `scoreboard.json` + `scoreboard.md`
|
||||||
|
|
||||||
### Phase 4 — CLOS
|
### Phase 4 — CLOS
|
||||||
@@ -124,18 +124,7 @@ data; format for string templating.
|
|||||||
|
|
||||||
_Newest first._
|
_Newest first._
|
||||||
|
|
||||||
- 2026-05-05: Phase 3 complete — cl-debugger-hook/cl-invoke-debugger in runtime.sx (cl-error routes through hook), cl-break-on-signals (fires hook before handlers on type match), cl-invoke-restart-interactively (calls fn with no args); 4 new tests (147 total). Phase 3 all boxes ticked.
|
- _(none yet)_
|
||||||
- 2026-05-05: Phase 3 interactive-debugger.sx — cl-debugger-hook global, cl-invoke-debugger, cl-error-with-debugger, make-policy-debugger; 7 tests (143 total). Tests wired into test.sh program suite runner. Phase 3 condition core complete.
|
|
||||||
- 2026-05-05: Phase 3 classic programs — `tests/programs/restart-demo.sx` (7 tests: safe-divide with use-zero + retry restarts) and `tests/programs/parse-recover.sx` (6 tests: token parser with skip-token + use-zero restarts, handler-case abort). Key gotcha: use `=` not `equal?` for list comparison in sx_server.
|
|
||||||
- 2026-05-05: Phase 3 conditions + restarts — `cl-condition-classes` hierarchy (15 types), `cl-condition?`/`cl-condition-of-type?`, `cl-make-condition`, `cl-define-condition`, `cl-signal`/`cl-error`/`cl-warn`/`cl-cerror`, `cl-handler-bind` (non-unwinding), `cl-handler-case` (call/cc escape), `cl-restart-case`/`cl-with-simple-restart`, `cl-find-restart`/`cl-invoke-restart`/`cl-compute-restarts`, `cl-with-condition-restarts`; 55 new tests in `tests/conditions.sx` (123 total runtime tests). Key gotcha: `cl-condition-classes` must be captured at define-time via `let` in `cl-condition-of-type?` — free-variable lookup at call-time fails through env_merge parent chain.
|
|
||||||
- 2026-05-05: multiple values — VALUES returns {:cl-type "mv"} wrapper for 2+ values; cl-mv-primary/cl-mv-vals helpers; MULTIPLE-VALUE-BIND binds vars to value list; MULTIPLE-VALUE-CALL/PROG1/NTH-VALUE; cl-mv-primary applied in IF/AND/OR/COND/cl-call-fn for single-value contexts; 15 new tests (174 eval, 346 total green).
|
|
||||||
- 2026-05-05: unwind-protect — cl-eval-unwind-protect: eval protected form, run cleanup with for-each (discards results, preserves original sentinel), return original result; 8 new tests (159 eval, 331 total green).
|
|
||||||
- 2026-05-05: tagbody + go — cl-go-tag? sentinel; cl-eval-tagbody runs body with tag-index map (keys str-normalised for integer tags); go-tag propagation in cl-eval-body alongside block-return; 11 new tests (151 eval, 323 total green).
|
|
||||||
- 2026-05-05: block + return-from — sentinel propagation in cl-eval-body; cl-eval-block catches matching sentinels; BLOCK/RETURN-FROM/RETURN dispatch in cl-eval-list; 13 new tests (140 eval, 312 total green). Parser: CL strings → {:cl-type "string"} dicts.
|
|
||||||
- 2026-04-25: Phase 2 eval — 127 tests, 299 total green. `lib/common-lisp/eval.sx`: cl-eval-ast with quote/if/progn/let/let*/flet/labels/setq/setf/function/lambda/the/locally/eval-when; defun/defvar/defparameter/defconstant; built-in arithmetic (+/-/*//, min/max/abs/evenp/oddp), comparisons, predicates, list ops (car/cdr/cons/list/append/reverse/length/nth/first/second/third/rest), string ops, funcall/apply/mapcar. Key gotchas: SX reduce is (reduce fn init list) not (reduce fn list init); CL true literal is t not true; builtins registered in cl-global-env.fns via wrapper dicts for #' syntax.
|
|
||||||
- 2026-04-25: Phase 1 lambda-list parser — 31 new tests, 172 total green. `cl-parse-lambda-list` in `parser.sx` + `tests/lambda.sx`. Handles &optional/&rest/&body/&key/&aux/&allow-other-keys, defaults, supplied-p. Key gotchas: `(when (> (len items) 0) ...)` not `(when items ...)` (empty list is truthy); custom `cl-deep=` needed for dict/list structural equality in tests.
|
|
||||||
- 2026-04-25: Phase 1 reader/parser — 62 new tests, 141 total green. `lib/common-lisp/parser.sx`: cl-read/cl-read-all, lists, dotted pairs, quote/backquote/unquote/splice/#', vectors, #:uninterned, NIL→nil, T→true, reader macro wrappers.
|
|
||||||
- 2026-04-25: Phase 1 tokenizer — 79 tests green. `lib/common-lisp/reader.sx` + `tests/read.sx` + `test.sh`. Handles symbols (pkg:sym, pkg::sym), integers, floats, ratios, hex/binary/octal, strings, #\ chars, reader macros (#' #( #: ,@), line/block comments. Key gotcha: SX `str` for string concat (not `concat`), substring-based read-while.
|
|
||||||
|
|
||||||
## Blockers
|
## Blockers
|
||||||
|
|
||||||
|
|||||||
@@ -69,36 +69,347 @@ Representation:
|
|||||||
- [x] Tests in `lib/forth/tests/test-phase2.sx` — 26/26 pass
|
- [x] Tests in `lib/forth/tests/test-phase2.sx` — 26/26 pass
|
||||||
|
|
||||||
### Phase 3 — control flow + first Hayes tests green
|
### Phase 3 — control flow + first Hayes tests green
|
||||||
- [ ] `IF`, `ELSE`, `THEN` — compile to SX `if`
|
- [x] `IF`, `ELSE`, `THEN` — compile to SX `if`
|
||||||
- [ ] `BEGIN`, `UNTIL`, `WHILE`, `REPEAT`, `AGAIN` — compile to loops
|
- [x] `BEGIN`, `UNTIL`, `WHILE`, `REPEAT`, `AGAIN` — compile to loops
|
||||||
- [ ] `DO`, `LOOP`, `+LOOP`, `I`, `J`, `LEAVE` — counted loops (needs a return stack)
|
- [x] `DO`, `LOOP`, `+LOOP`, `I`, `J`, `LEAVE` — counted loops (needs a return stack)
|
||||||
- [ ] Return stack: `>R`, `R>`, `R@`, `2>R`, `2R>`, `2R@`
|
- [x] Return stack: `>R`, `R>`, `R@`, `2>R`, `2R>`, `2R@`
|
||||||
- [ ] Vendor John Hayes' test suite to `lib/forth/ans-tests/`
|
- [x] Vendor John Hayes' test suite to `lib/forth/ans-tests/`
|
||||||
- [ ] `lib/forth/conformance.sh` + runner; `scoreboard.json` + `scoreboard.md`
|
- [x] `lib/forth/conformance.sh` + runner; `scoreboard.json` + `scoreboard.md`
|
||||||
- [ ] Baseline: probably 30-50% Core passing after phase 3
|
- [x] Baseline: probably 30-50% Core passing after phase 3
|
||||||
|
|
||||||
### Phase 4 — strings + more Core
|
### Phase 4 — strings + more Core
|
||||||
- [ ] `S"`, `C"`, `."`, `TYPE`, `COUNT`, `CMOVE`, `FILL`, `BLANK`
|
- [x] `S"`, `C"`, `."`, `TYPE`, `COUNT`, `CMOVE`, `FILL`, `BLANK`
|
||||||
- [ ] `CHAR`, `[CHAR]`, `KEY`, `ACCEPT`
|
- [x] `CHAR`, `[CHAR]`, `KEY`, `ACCEPT`
|
||||||
- [ ] `BASE` manipulation: `DECIMAL`, `HEX`
|
- [x] `BASE` manipulation: `DECIMAL`, `HEX`
|
||||||
- [ ] `DEPTH`, `SP@`, `SP!`
|
- [x] `DEPTH`, `SP@`, `SP!`
|
||||||
- [ ] Drive Hayes Core pass-rate up
|
- [x] Drive Hayes Core pass-rate up
|
||||||
|
|
||||||
### Phase 5 — Core Extension + optional word sets
|
### Phase 5 — Core Extension + optional word sets
|
||||||
- [ ] Full Core + Core Extension
|
- [x] Memory: `CREATE`, `HERE`, `ALLOT`, `,`, `C,`, `CELL+`, `CELLS`, `ALIGN`, `ALIGNED`, `2!`, `2@`
|
||||||
- [ ] File Access word set (via SX IO)
|
- [x] Unsigned compare: `U<`, `U>`
|
||||||
- [ ] String word set (`SLITERAL`, `COMPARE`, `SEARCH`)
|
- [x] Mixed/double-cell math: `S>D`, `M*`, `UM*`, `UM/MOD`, `FM/MOD`, `SM/REM`, `*/`, `*/MOD`
|
||||||
- [ ] Target: 100% Hayes Core
|
- [x] Double-cell ops: `D+`, `D-`, `D=`, `D<`, `D0=`, `2DUP`, `2DROP`, `2OVER`, `2SWAP` (already), plus `D>S`, `DABS`, `DNEGATE`
|
||||||
|
- [x] Number formatting: `<#`, `#`, `#S`, `#>`, `HOLD`, `SIGN`, `.R`, `U.`, `U.R`
|
||||||
|
- [x] Parsing/dictionary: `WORD`, `FIND`, `EXECUTE`, `'`, `[']`, `LITERAL`, `POSTPONE`, `>BODY` (DOES> deferred — needs runtime-rebind of last CREATE)
|
||||||
|
- [x] Source/state: `EVALUATE`, `STATE`, `[`, `]` (`SOURCE`/`>IN` stubbed; tokenized input means the exact byte/offset semantics aren't useful here)
|
||||||
|
- [x] Misc Core: `WITHIN`, `MAX`/`MIN` (already), `ABORT`, `ABORT"`, `EXIT`, `UNLOOP`
|
||||||
|
- [x] File Access word set (in-memory — `read-file` is not reachable from the epoch eval env)
|
||||||
|
- [x] String word set (`SLITERAL`, `COMPARE`, `SEARCH`)
|
||||||
|
- [x] Target: 100% Hayes Core (97% achieved — remaining 5 errors all in `GI5`'s multi-`WHILE`-per-`BEGIN` non-standard pattern, plus one stuck `dict-set!` chunk and 14 numeric-edge fails)
|
||||||
|
|
||||||
### Phase 6 — speed
|
### Phase 6 — speed
|
||||||
- [ ] Inline primitive calls during compile (skip dict lookup)
|
- [x] Inline primitive calls during compile (skip dict lookup)
|
||||||
- [ ] Tail-call optimise colon-def endings
|
- [x] Tail-call optimise colon-def endings
|
||||||
- [ ] JIT cooperation: mark compiled colon-defs as VM-eligible
|
- [x] JIT cooperation: mark compiled colon-defs as VM-eligible
|
||||||
|
|
||||||
## Progress log
|
## Progress log
|
||||||
|
|
||||||
_Newest first._
|
_Newest first._
|
||||||
|
|
||||||
|
- **Post-phase-6 conformance fixes — Hayes 628→632/638 (99%).** Round 2:
|
||||||
|
fixed `forth-pic-step` (used by `#S`) to use the same precise two-step
|
||||||
|
16-bit division as `#`, and rewrote `UM/MOD` using two-phase 16-bit long
|
||||||
|
division to avoid `mod_float` vs `floor-division` inconsistency at integer
|
||||||
|
boundaries. Fixes GP6 / GN1 (pictured output), and the UM/MOD remainder bug.
|
||||||
|
|
||||||
|
- **Post-phase-6 conformance fixes — Hayes 618→628/638 (98%).** Round 1:
|
||||||
|
fixed multi-WHILE compiler bug (REPEAT was consuming back-pc instead of
|
||||||
|
WHILE-target dicts — added `forth-drain-cstack-dicts`); fixed `+LOOP` exit
|
||||||
|
test by clipping increment to 32-bit signed; rewrote `M*`/`UM*` using
|
||||||
|
16-bit half-multiply (`forth-umul32`) to avoid float64 precision loss near
|
||||||
|
2^62; rewrote `#` with two-step division. Eliminated all 6 errors; 10 fails
|
||||||
|
remain (SOURCE/>IN tracking and CHAR " require deeper plumbing changes).
|
||||||
|
|
||||||
|
- **Phase 6 close — JIT cooperation hooks (Hayes unchanged at 618/638).**
|
||||||
|
Every word record now carries `:vm-eligible? true` and a
|
||||||
|
`:call-count` counter that `forth-execute-word` bumps on every
|
||||||
|
invocation. The flag is a hint for downstream JIT consumers — our
|
||||||
|
bodies are plain SX lambdas already, so the existing SX VM's
|
||||||
|
on-first-call JIT lifts them into bytecode automatically; the
|
||||||
|
metadata just makes that fact discoverable. Added
|
||||||
|
`forth-hot-words state threshold` returning `(name count)`
|
||||||
|
pairs above a threshold so a future tracing JIT can pick out
|
||||||
|
hot definitions to specialise. Phase 6 boxes all ticked.
|
||||||
|
All 306 internal tests green; Hayes Core stays at 618/638.
|
||||||
|
|
||||||
|
- **Phase 6 — TCO at colon-def endings (Hayes unchanged at 618/638).**
|
||||||
|
`forth-run-body` now special-cases the final op when it's a plain
|
||||||
|
function (not a branch dict): we call it in tail position with no
|
||||||
|
pc-increment and no recursive `forth-run-body` call. This means
|
||||||
|
the SX CEK can collapse the continuation frame, so chains like
|
||||||
|
`: A ... B ; : B ... C ; …` and `RECURSE` deep-recursion test
|
||||||
|
cases run without piling up frames at each colon-def boundary.
|
||||||
|
All 306 internal tests still green; verified 5000-deep
|
||||||
|
`COUNTDOWN RECURSE` still terminates fine.
|
||||||
|
|
||||||
|
- **Phase 6 — inline primitive calls (Hayes unchanged at 618/638).**
|
||||||
|
`forth-compile-call` now appends the looked-up word's body fn
|
||||||
|
directly to the colon-def body instead of wrapping it in
|
||||||
|
`(fn (s) (forth-execute-word s w))`. `forth-execute-word body`
|
||||||
|
reduces to `((get w "body") state)`, so the wrapper added an
|
||||||
|
extra closure + `get` per call op for no behavioural gain. Same
|
||||||
|
early-binding semantics: the body fn is captured at compile time,
|
||||||
|
so later redefinitions of the same name don't retroactively
|
||||||
|
change existing definitions. All 306 internal tests still green;
|
||||||
|
Hayes Core stays at 618/638. Pure optimisation.
|
||||||
|
|
||||||
|
- **Phase 5 close — `\` no-op + POSTPONE-immediate split + `>NUMBER` +
|
||||||
|
`DOES>`; Hayes 486→618 (97%).** Big closing-out iteration.
|
||||||
|
Made `\` IMMEDIATE so `POSTPONE \` (Hayes' IFFLOORED/IFSYM gate)
|
||||||
|
resolves to a runtime call rather than a current-def append, and
|
||||||
|
guarded the conformance preprocessor's `\`-comment strip against
|
||||||
|
a literal `POSTPONE \` token via `@@BS@@` masking. Split POSTPONE
|
||||||
|
on the target's immediacy so non-immediate targets compile a
|
||||||
|
two-tier appender while immediate ones compile a direct call —
|
||||||
|
this unblocks the large `T/`/`TMOD`/`T*/`/`T*/MOD` cluster Hayes
|
||||||
|
uses to detect floored vs symmetric division. `>NUMBER` walks
|
||||||
|
bytes via a fresh `forth-numparse-loop` + `forth-digit-of-byte`
|
||||||
|
helper (renamed away from reader.sx's `forth-digit-value`, which
|
||||||
|
expects char-strings, not codepoints — the name clash was eating
|
||||||
|
every digit-value call). Implemented `DOES>` by:
|
||||||
|
1) tracking the last CREATE on `state.last-creator`,
|
||||||
|
2) adding a `:kind "does-rebind"` op, and
|
||||||
|
3) post-processing the body in `;` to attach the slice of ops
|
||||||
|
after each rebind as `:deferred`. At runtime, the rebind op
|
||||||
|
installs a new body for the target word that pushes its
|
||||||
|
data-field address and runs the deferred slice. Also added
|
||||||
|
histogram tracking on the conformance runner so future runs
|
||||||
|
surface the top missing words. Hayes: 618/638 pass (97%),
|
||||||
|
14 fail, 6 error (5× GI5 multi-WHILE, 1× dict-set! chunk).
|
||||||
|
|
||||||
|
- **Phase 5 — String word set `COMPARE`/`SEARCH`/`SLITERAL` (+9).**
|
||||||
|
`COMPARE` walks bytes via the new `forth-compare-bytes-loop`,
|
||||||
|
returning -1/0/1 with standard prefix semantics (shorter string
|
||||||
|
compares less than its extension). `SEARCH` scans the haystack
|
||||||
|
with a helper `forth-search-bytes` and `forth-match-at`, returning
|
||||||
|
the tail after the first match or the original string with flag=0.
|
||||||
|
Empty needle returns at offset 0 with flag=-1 per ANS. `SLITERAL`
|
||||||
|
is IMMEDIATE: pops `(c-addr u)` at compile time, copies the bytes
|
||||||
|
into a fresh allocation, and emits the two pushes so the compiled
|
||||||
|
word yields the interned string at runtime.
|
||||||
|
|
||||||
|
- **Phase 5 — File Access word set (in-memory backing; +4).**
|
||||||
|
`OPEN-FILE`/`CREATE-FILE`/`CLOSE-FILE`/`READ-FILE`/`WRITE-FILE`/
|
||||||
|
`FILE-POSITION`/`FILE-SIZE`/`REPOSITION-FILE`/`DELETE-FILE` plus
|
||||||
|
the mode constants `R/O`/`R/W`/`W/O`/`BIN`. File handles live on
|
||||||
|
`state.files` (fileid → {content, pos, path}) with a
|
||||||
|
`state.by-path` index so `CREATE-FILE`'d files can be
|
||||||
|
`OPEN-FILE`'d later in the same session. Attempting to
|
||||||
|
`OPEN-FILE` an unknown path returns `ior != 0`; disk-backed
|
||||||
|
open/read is not wired because `read-file` isn't in the sx_server
|
||||||
|
epoch eval environment (it's bound only in the HTTP helpers).
|
||||||
|
Also removed the stray base-2 `BIN` primitive from Phase 4 —
|
||||||
|
ANS `BIN` is the file-mode modifier. Hayes Core unchanged at
|
||||||
|
486/638 since core.fr doesn't exercise file words.
|
||||||
|
|
||||||
|
- **Phase 5 — `WITHIN`/`ABORT`/`ABORT"`/`EXIT`/`UNLOOP` (+7;
|
||||||
|
Hayes 477→486, 76%).** `WITHIN` uses the ANS two's-complement
|
||||||
|
trick: `(n1-n2) U< (n3-n2)`. `ABORT` wipes the data/return/control
|
||||||
|
stacks and raises — the conformance runner catches it at the
|
||||||
|
chunk boundary. `ABORT"` parses its message like `S"`, then at
|
||||||
|
runtime pops a flag and raises only if truthy. `EXIT` adds a new
|
||||||
|
`:kind "exit"` op that the PC-driven body runner treats as a
|
||||||
|
jump-to-end; added a matching cond clause in `forth-step-op`.
|
||||||
|
`UNLOOP` pops two from the return stack — usable paired with
|
||||||
|
`EXIT` to bail from inside `DO`/`LOOP`.
|
||||||
|
|
||||||
|
- **Phase 5 — `[`, `]`, `STATE`, `EVALUATE` (+5; Hayes 463→477, 74%).**
|
||||||
|
`[` (IMMEDIATE) clears `state.compiling`, `]` sets it. `STATE`
|
||||||
|
pushes the sentinel address `"@@state"` and `@` reads it as
|
||||||
|
`-1`/`0` based on the live `compiling` flag. `EVALUATE` reads
|
||||||
|
the (addr,u) string from byte memory, retokenises it via
|
||||||
|
`forth-tokens`, swaps it in as the active input, runs the
|
||||||
|
interpret loop, and restores the saved input. `SOURCE` and
|
||||||
|
`>IN` exist as stubs that push zeros — our whitespace-tokenised
|
||||||
|
input has no native byte-offset, so the deeper Hayes tests
|
||||||
|
that re-position parsing via `>IN !` stay marked as errors
|
||||||
|
rather than silently misbehaving.
|
||||||
|
|
||||||
|
- **Phase 5 — parsing/dictionary words `'`/`[']`/`EXECUTE`/`LITERAL`/
|
||||||
|
`POSTPONE`/`WORD`/`FIND`/`>BODY` (Hayes 448→463, 72%).** xt is
|
||||||
|
represented as the SX dict reference of the word record, so
|
||||||
|
`'`/`[']` push the looked-up record and `EXECUTE` calls
|
||||||
|
`forth-execute-word` on the popped value. `LITERAL` (IMMEDIATE)
|
||||||
|
pops a value at compile time and emits a push-op. `POSTPONE`
|
||||||
|
(IMMEDIATE) compiles into the *outer* def an op that, when run
|
||||||
|
during a *later* compile, appends a call-w op to whatever def is
|
||||||
|
current — the standard two-tier compile semantic. Added
|
||||||
|
`state.last-defined` tracked by every primitive/colon definition
|
||||||
|
so `IMMEDIATE` can target the most-recent word even after `;`
|
||||||
|
closes the def. CREATE now stashes its data-field address on the
|
||||||
|
word record so `>BODY` can recover it. `WORD`/`FIND` use the byte
|
||||||
|
memory and counted-string layout already in place.
|
||||||
|
`DOES>` is deferred — needs a runtime mechanism to rebind the
|
||||||
|
last-CREATE'd word's action.
|
||||||
|
|
||||||
|
- **Phase 5 — pictured numeric output: `<#`/`#`/`#S`/`#>`/`HOLD`/`SIGN` +
|
||||||
|
`U.`/`U.R`/`.R` (+9; Hayes 446→448, 70%).** Added a `state.hold`
|
||||||
|
list of single-character strings — `<#` resets it, `HOLD` and
|
||||||
|
`SIGN` prepend, `#` divides ud by BASE and prepends one digit,
|
||||||
|
`#S` loops `#` until ud is zero (running once even on zero),
|
||||||
|
`#>` drops ud and copies the joined hold buffer into mem,
|
||||||
|
pushing `(addr, len)`. `U.` / `.R` / `U.R` use a separate
|
||||||
|
`forth-num-to-string` for one-shot decimal/hex output and
|
||||||
|
`forth-spaces-str` for right-justify padding.
|
||||||
|
|
||||||
|
- **Phase 5 — double-cell ops `D+`/`D-`/`DNEGATE`/`DABS`/`D=`/`D<`/`D0=`/
|
||||||
|
`D0<`/`DMAX`/`DMIN` (+18; Hayes unchanged).** Doubles get rebuilt
|
||||||
|
from `(lo, hi)` cells via `forth-double-from-cells-s`, the op runs
|
||||||
|
in bignum, and we push back via `forth-double-push-s`. Hayes Core
|
||||||
|
doesn't exercise D-words (those live in Gerry Jackson's separate
|
||||||
|
`doublest.fth` Double word-set tests we have not vendored), so the
|
||||||
|
scoreboard stays at 446/638 — but the words now exist for any
|
||||||
|
consumer that needs them.
|
||||||
|
|
||||||
|
- **Phase 5 — mixed/double-cell math; Hayes 342→446 (69%).** Added
|
||||||
|
`S>D`, `D>S`, `M*`, `UM*`, `UM/MOD`, `FM/MOD`, `SM/REM`, `*/`, `*/MOD`.
|
||||||
|
Doubles ride on the stack as `(lo, hi)` with `hi` on top.
|
||||||
|
Helpers `forth-double-push-{u,s}` / `forth-double-from-cells-{u,s}`
|
||||||
|
split & rebuild via 32-bit unsigned mod/div, picking the negative
|
||||||
|
path explicitly so we don't form `2^64 + small` (float precision
|
||||||
|
drops at ULP=2^12 once you cross 2^64). `M*`/`UM*` use bignum
|
||||||
|
multiply then split; `*/`/`*/MOD` use bignum intermediate and
|
||||||
|
truncated division. Hayes: 446 pass / 185 error / 7 fail.
|
||||||
|
|
||||||
|
- **Phase 5 — memory primitives + unsigned compare; Hayes 268→342 (53%).**
|
||||||
|
Added `CREATE`/`HERE`/`ALLOT`/`,`/`C,`/`CELL+`/`CELLS`/`ALIGN`/`ALIGNED`/
|
||||||
|
`2!`/`2@`/`U<`/`U>`. Generalised `@`/`!`/`+!` to dispatch on address
|
||||||
|
type: string addresses still go through `state.vars` (VARIABLE/VALUE
|
||||||
|
cells) while integer addresses now fall through to `state.mem` —
|
||||||
|
letting CREATE-allocated cells coexist with existing variables.
|
||||||
|
Decomposed the original "Full Core + Core Extension" box into
|
||||||
|
smaller unticked sub-bullets so iterations land per cluster.
|
||||||
|
Hayes: 342 pass / 292 error / 4 fail (53%). 237/237 internal.
|
||||||
|
|
||||||
|
- **Phase 4 close — LSHIFT/RSHIFT, 32-bit arith truncation, early
|
||||||
|
binding; Hayes 174→268 (42%).** Added `LSHIFT` / `RSHIFT` as logical
|
||||||
|
shifts on 32-bit unsigned values, converted through
|
||||||
|
`forth-to-unsigned`/`forth-from-unsigned`. All arithmetic
|
||||||
|
primitives (`+` `-` `*` `/` `MOD` `NEGATE` `ABS` `1+` `1-` `2+`
|
||||||
|
`2-` `2*` `2/`) now clip results to 32-bit signed via a new
|
||||||
|
`forth-clip` helper, so loop idioms that rely on `2*` shifting the
|
||||||
|
MSB out (e.g. Hayes' `BITS` counter) actually terminate.
|
||||||
|
Changed colon-def call compilation from late-binding to early
|
||||||
|
binding: `forth-compile-call` now resolves the target word at
|
||||||
|
compile time, which makes `: GDX 123 ; : GDX GDX 234 ;` behave
|
||||||
|
per ANS (inner `GDX` → old def, not infinite recursion). `RECURSE`
|
||||||
|
keeps its late-binding thunk via the new `forth-compile-recurse`
|
||||||
|
helper. Raised `MAX_CHUNKS` default to 638 (full `core.fr`) now
|
||||||
|
that the BITS and COUNT-BITS loops terminate. Hayes: 268 pass /
|
||||||
|
368 error / 2 fail.
|
||||||
|
|
||||||
|
- **Phase 4 — `SP@`/`SP!` (+4; Hayes unchanged; `DEPTH` was already present).**
|
||||||
|
`SP@` pushes the current data-stack depth (our closest analogue to a
|
||||||
|
stack pointer — SX lists have no addressable backing). `SP!` pops a
|
||||||
|
target depth and truncates the stack via `drop` on the dstack list.
|
||||||
|
This preserves the save/restore idiom `SP@ … SP!` even though the
|
||||||
|
returned "pointer" is really a count.
|
||||||
|
|
||||||
|
- **Phase 4 — `BASE`/`DECIMAL`/`HEX`/`BIN`/`OCTAL` (+9; Hayes unchanged).**
|
||||||
|
Moved `base` from its top-level state slot into `state.vars["base"]`
|
||||||
|
so the regular `@`/`!`/VARIABLE machinery works on it.
|
||||||
|
`BASE` pushes the sentinel address `"base"`; `DECIMAL`/`HEX`/`BIN`/
|
||||||
|
`OCTAL` are thin primitives that write into that slot. Parser
|
||||||
|
reads through `vars` now. Hayes unchanged because the runner had
|
||||||
|
already been stubbing `HEX`/`DECIMAL` — now real words, stubs
|
||||||
|
removed from `hayes-runner.sx`.
|
||||||
|
|
||||||
|
- **Phase 4 — `CHAR`/`[CHAR]`/`KEY`/`ACCEPT` (+7 / Hayes 168→174).**
|
||||||
|
`CHAR` parses the next token and pushes the first-char code. `[CHAR]`
|
||||||
|
is IMMEDIATE: in compile mode it embeds the code as a compiled push
|
||||||
|
op, in interpret mode it pushes inline. `KEY`/`ACCEPT` read from an
|
||||||
|
optional `state.keybuf` string — empty buffer makes `KEY` raise
|
||||||
|
`"no input available"` (matches ANS when stdin is closed) and
|
||||||
|
`ACCEPT` returns `0`. Enough for Hayes to get past CHAR-gated
|
||||||
|
clusters; real interactive IO lands later.
|
||||||
|
|
||||||
|
- **Phase 4 — strings: `S"`/`C"`/`."`/`TYPE`/`COUNT`/`CMOVE`/`CMOVE>`/`MOVE`/`FILL`/`BLANK`/`C@`/`C!`/`CHAR+`/`CHARS` (+16 / Hayes 165→168).**
|
||||||
|
Added a byte-addressable memory model to state: `mem` (dict keyed by
|
||||||
|
stringified address → integer byte) and `here` (next-free integer
|
||||||
|
addr). Helpers `forth-alloc-bytes!` / `forth-mem-write-string!` /
|
||||||
|
`forth-mem-read-string`. `S"`/`C"`/`."` are IMMEDIATE parsing words
|
||||||
|
that consume tokens until one ends with `"`, then either copy content
|
||||||
|
into memory at compile time (and emit a push of `addr`/`addr len` for
|
||||||
|
the colon-def body) or do it inline in interpret mode. `TYPE` emits
|
||||||
|
`u` bytes from `addr` via `char-from-code`. `COUNT` reads the length
|
||||||
|
byte at a counted-string address and pushes (`addr+1`, `u`). `FILL`,
|
||||||
|
`BLANK` (FILL with space), `CMOVE` (forward), `CMOVE>` (backward),
|
||||||
|
and `MOVE` (auto-directional) mutate the byte dict. 193/193 internal
|
||||||
|
tests, Hayes 168/590 (+3).
|
||||||
|
|
||||||
|
- **Phase 3 — Hayes conformance runner + baseline scoreboard (165/590, 28%).**
|
||||||
|
`lib/forth/conformance.sh` preprocesses `ans-tests/core.fr` (strips `\`
|
||||||
|
and `( ... )` comments + `TESTING` lines), splits the source on every
|
||||||
|
`}T` so each Hayes test plus the small declaration blocks between
|
||||||
|
them are one safe-resume chunk, and emits an SX driver that feeds
|
||||||
|
the chunks through `lib/forth/hayes-runner.sx`. The runner registers
|
||||||
|
`T{`/`->`/`}T` as Forth primitives that snapshot the dstack depth on
|
||||||
|
`T{`, record actual on `->`, compare on `}T`, and install stub
|
||||||
|
`HEX`/`DECIMAL`/`TESTING` so metadata doesn't halt the stream. Errors
|
||||||
|
raised inside a chunk are caught by `guard` and the state is reset,
|
||||||
|
so one bad test does not break the rest. Outputs
|
||||||
|
`scoreboard.json` + `scoreboard.md`.
|
||||||
|
|
||||||
|
First-run baseline: 165 pass / 425 error / 0 fail on the first 590
|
||||||
|
chunks. The default cap sits at 590 because `core.fr` chunks beyond
|
||||||
|
that rely on unsigned-integer wrap-around (e.g. `COUNT-BITS` with
|
||||||
|
`BEGIN DUP WHILE … 2* REPEAT`) which never terminates on our
|
||||||
|
bignum-based Forth; raise `MAX_CHUNKS` once those tests unblock.
|
||||||
|
Majority of errors are missing Phase-4 words (`RSHIFT`, `LSHIFT`,
|
||||||
|
`CELLS`, `S"`, `CHAR`, `SOURCE`, etc.) — each one implemented should
|
||||||
|
convert a cluster of errors to passes.
|
||||||
|
|
||||||
|
- **Phase 3 — vendor Gerry Jackson's forth2012-test-suite.** Added
|
||||||
|
`lib/forth/ans-tests/{tester.fr, core.fr, coreexttest.fth}` from
|
||||||
|
https://github.com/gerryjackson/forth2012-test-suite (master, fetched
|
||||||
|
2026-04-24). `tester.fr` is Hayes' `T{ ... -> ... }T` harness; `core.fr`
|
||||||
|
is the ~1000-line Core word tests; `coreexttest.fth` is Core Ext
|
||||||
|
(parked for later phases). Files are pristine — the conformance runner
|
||||||
|
(next iteration) will consume them.
|
||||||
|
|
||||||
|
- **Phase 3 — `DO`/`LOOP`/`+LOOP`/`I`/`J`/`LEAVE` + return stack words (+16).**
|
||||||
|
Counted loops compile onto the same PC-driven body runner. DO emits an
|
||||||
|
enter-op (pops limit+start from data stack, pushes them to rstack) and
|
||||||
|
pushes a `{:kind "do" :back PC :leaves ()}` marker onto cstack. LOOP/+LOOP
|
||||||
|
emit a dict op (`:kind "loop"`/`"+loop"` with target=back-cell). The step
|
||||||
|
handler pops index & reads limit, increments, and either restores the
|
||||||
|
updated index + jumps back, or drops the frame and advances. LEAVE walks
|
||||||
|
cstack for the innermost DO marker, emits a `:kind "leave"` dict op with
|
||||||
|
a fresh target cell, and registers it on the marker's leaves list. LOOP
|
||||||
|
patches all registered leave-targets to the exit PC and drops the marker.
|
||||||
|
The leave op pops two from rstack (unloop) and branches. `I` peeks rtop;
|
||||||
|
`J` reads rstack index 2 (below inner frame). Added non-immediate
|
||||||
|
return-stack words `>R`, `R>`, `R@`, `2>R`, `2R>`, `2R@`. Nested
|
||||||
|
DO/LOOP with J tested; LEAVE in nested loops exits only the inner.
|
||||||
|
177/177 green.
|
||||||
|
|
||||||
|
- **Phase 3 — `BEGIN`/`UNTIL`/`WHILE`/`REPEAT`/`AGAIN` (+9).** Indefinite-loop
|
||||||
|
constructs built on the same PC-driven body runner introduced for `IF`.
|
||||||
|
BEGIN records the current body length on `state.cstack` (a plain numeric
|
||||||
|
back-target). UNTIL/AGAIN pop that back-target and emit a `bif`/`branch`
|
||||||
|
op whose target cell is set to the recorded PC. WHILE emits a forward
|
||||||
|
`bif` with a fresh target cell and pushes it on the cstack *above* the
|
||||||
|
BEGIN marker; REPEAT pops both (while-target first, then back-pc), emits
|
||||||
|
an unconditional branch back to BEGIN, then patches the while-target to
|
||||||
|
the current body length — so WHILE's false flag jumps past the REPEAT.
|
||||||
|
Mixed compile-time layout (numeric back-targets + dict forward targets
|
||||||
|
on the same cstack) is OK because the immediate words pop them in the
|
||||||
|
order they expect. AGAIN works structurally but lacks a test without a
|
||||||
|
usable mid-loop exit; revisit once `EXIT` lands. 161/161 green.
|
||||||
|
|
||||||
|
- **Phase 3 start — `IF`/`ELSE`/`THEN` (+18).** `lib/forth/compiler.sx`
|
||||||
|
+ `tests/test-phase3.sx`. Colon-def body switched from `for-each` to
|
||||||
|
a PC-driven runner so branch ops can jump: ops now include dict tags
|
||||||
|
`{"kind" "bif"|"branch" "target" cell}` alongside the existing
|
||||||
|
`(fn (s) ...)` shape. IF compiles a `bif` with a fresh target cell
|
||||||
|
pushed to `state.cstack`; ELSE emits an unconditional `branch`,
|
||||||
|
patches the IF's target to the instruction after this branch, and
|
||||||
|
pushes the new target; THEN patches the most recent target to the
|
||||||
|
current body length. Nested IF/ELSE/THEN works via the cstack.
|
||||||
|
Also fixed `EMIT`: `code-char` → `char-from-code` (spec-correct
|
||||||
|
primitive name) so Phase 1/2 tests run green on sx_server.
|
||||||
|
152/152 (Phase 1 + 2 + 3) green.
|
||||||
|
|
||||||
- **Phase 2 complete — colon defs, compile mode, VARIABLE/CONSTANT/VALUE/TO, @/!/+! (+26).**
|
- **Phase 2 complete — colon defs, compile mode, VARIABLE/CONSTANT/VALUE/TO, @/!/+! (+26).**
|
||||||
`lib/forth/compiler.sx` plus `tests/test-phase2.sx`.
|
`lib/forth/compiler.sx` plus `tests/test-phase2.sx`.
|
||||||
Colon-def body is a list of ops (one per source token) wrapped in a single
|
Colon-def body is a list of ops (one per source token) wrapped in a single
|
||||||
|
|||||||
Reference in New Issue
Block a user