feat: complete atom implementation, new macros, predicates, and test coverage

## Runtime (src/jolt/core.janet, +637/-99)

### Atom system (complete)
- core-atom: :validator and :meta options, watches table, atom-validate/atom-notify-watches helpers
- New functions: swap-vals!, reset-vals!, compare-and-set!, set-validator!, get-validator, add-watch, remove-watch
- Validator: blocks invalid reset!/swap!; watches: notification with old/new values; metadata support
- All 7 functions registered in core-bindings

### New predicates
- integer?, boolean?, list? — all registered in core-bindings

### New math
- abs, rand, rand-int — registered in core-bindings

### Control flow macros (7 new)
- cond, case (with one-of-many list support), condp, dotimes, while, if-not, when-first
- Registered as macros: cond, case, for, dotimes, while, condp, if-not, when-first

### Threading macros (7)
- -> (thread-first), ->> (thread-last), some->, some->>, cond->, cond->>, as->
- Renamed to core-thread-first/core-thread-last to avoid collision with comparison operators

### for comprehension
- Full Clojure for macro: :when, :let modifiers, nested bindings
- Uses map/mapcat expansion, correct last-group optimization

### Sequence operations (15 new)
- last, butlast, second, ffirst, fnext, nfirst, nnext, nthnext, nthrest
- drop-last, take-nth, map-indexed, keep, keep-indexed, reductions

### Nil-safety fixes
- core-reverse, core-sort, core-sort-by, core-distinct now handle nil input

## Tests (7 new files, 361 assertions across all tests)

### Ported from Clojure test suite (4 files)
- clojure-logic-test.janet: 82 assertions (if/and/or/not/some?/nil-punning)
- clojure-control-test.janet: 56 assertions (do/when/if-let/cond/dotimes/while/loop/case)
- clojure-macros-test.janet: 21 assertions (->/->>/some->/some->>/cond->/cond->>/as->)
- clojure-for-test.janet: 4 assertions (:when/:let/nested for)

### Systematic coverage
- clojure-atom-test.janet: 31 assertions (creation/reset!/swap!/swap-vals!/reset-vals!/CAS/validator/watches/meta)
- systematic-coverage.janet: 120+ assertions (predicates/math/comparison/logic/collections/seq-ops/destructuring)
- logic-test.janet: earlier ported logic tests

### Existing tests updated
- lazy-test.janet, phase7-test.janet: updated for nil-safe core functions

## Result
47 test files, all passing. 19 new functions/macros implemented.
Clojure atom semantics complete: validators, watches, metadata, CAS, swap-vals!/reset-vals!.
This commit is contained in:
Yogthos 2026-06-04 12:01:58 -04:00
parent 82bce033a5
commit e2e7fa1447
12 changed files with 1462 additions and 98 deletions

View file

@ -0,0 +1,127 @@
# Ported from clojure/test_clojure/logic.clj
(use ../src/jolt/api)
(defn ct-eval [ctx s] (eval-string ctx s))
(print "Ported Logic Tests (from clojure/test-clojure/logic.clj)")
# --- test-if ---
(print "test-if: true/false/nil...")
(let [ctx (init)]
(assert (= :t (ct-eval ctx "(if true :t)")) "if true")
(assert (= :t (ct-eval ctx "(if true :t :f)")) "if true with else")
(assert (= nil (ct-eval ctx "(if false :t)")) "if false no else")
(assert (= :f (ct-eval ctx "(if false :t :f)")) "if false with else")
(assert (= nil (ct-eval ctx "(if nil :t)")) "if nil no else")
(assert (= :f (ct-eval ctx "(if nil :t :f)")) "if nil with else"))
(print " ok")
(print "test-if: zero/empty is true...")
(let [ctx (init)]
(assert (= :t (ct-eval ctx "(if 0 :t :f)")) "0 is true")
(assert (= :t (ct-eval ctx "(if 0.0 :t :f)")) "0.0 is true")
(assert (= :t (ct-eval ctx "(if \"\" :t :f)")) "empty string is true")
(assert (= :t (ct-eval ctx "(if () :t :f)")) "empty list is true")
(assert (= :t (ct-eval ctx "(if [] :t :f)")) "empty vector is true")
(assert (= :t (ct-eval ctx "(if {} :t :f)")) "empty map is true")
(assert (= :t (ct-eval ctx "(if #{} :t :f)")) "empty set is true"))
(print " ok")
(print "test-if: anything except nil/false is true...")
(let [ctx (init)]
(assert (= :t (ct-eval ctx "(if 42 :t :f)")) "42 is true")
(assert (= :t (ct-eval ctx "(if 1.2 :t :f)")) "1.2 is true")
(assert (= :t (ct-eval ctx "(if \"abc\" :t :f)")) "string is true")
(assert (= :t (ct-eval ctx "(if 'abc :t :f)")) "symbol is true")
(assert (= :t (ct-eval ctx "(if :kw :t :f)")) "keyword is true")
(assert (= :t (ct-eval ctx "(if '(1 2) :t :f)")) "list is true")
(assert (= :t (ct-eval ctx "(if [1 2] :t :f)")) "vector is true")
(assert (= :t (ct-eval ctx "(if {:a 1 :b 2} :t :f)")) "map is true")
(assert (= :t (ct-eval ctx "(if #{1 2} :t :f)")) "set is true"))
(print " ok")
# --- test-nil-punning ---
(print "test-nil-punning...")
(let [ctx (init)]
(assert (= :yes (ct-eval ctx "(if (first []) :no :yes)")) "first [] nil")
(assert (= :yes (ct-eval ctx "(if (next [1]) :no :yes)")) "next [1] nil")
(assert (= :no (ct-eval ctx "(if (rest [1]) :no :yes)")) "rest [1] non-nil")
(assert (= :yes (ct-eval ctx "(if (seq nil) :no :yes)")) "seq nil")
(assert (= :yes (ct-eval ctx "(if (seq []) :no :yes)")) "seq [] nil")
(assert (= :no (ct-eval ctx "(if (lazy-seq nil) :no :yes)")) "lazy-seq nil non-nil")
(assert (= :no (ct-eval ctx "(if (lazy-seq []) :no :yes)")) "lazy-seq [] non-nil")
(assert (= :no (ct-eval ctx "(if (filter (fn [x] (> x 10)) [1 2 3]) :no :yes)")) "filter non-match non-nil")
(assert (= :no (ct-eval ctx "(if (map identity []) :no :yes)")) "map empty non-nil")
(assert (= :no (ct-eval ctx "(if (apply concat []) :no :yes)")) "apply concat [] non-nil")
(assert (= :no (ct-eval ctx "(if (concat) :no :yes)")) "concat empty non-nil")
(assert (= :no (ct-eval ctx "(if (concat []) :no :yes)")) "concat [] non-nil")
(assert (= :no (ct-eval ctx "(if (reverse nil) :no :yes)")) "reverse nil non-nil")
(assert (= :no (ct-eval ctx "(if (reverse []) :no :yes)")) "reverse [] non-nil")
(assert (= :no (ct-eval ctx "(if (sort nil) :no :yes)")) "sort nil non-nil")
(assert (= :no (ct-eval ctx "(if (sort []) :no :yes)")) "sort [] non-nil"))
(print " ok")
# --- test-and ---
(print "test-and...")
(let [ctx (init)]
(assert (= true (ct-eval ctx "(and)")) "and empty")
(assert (= true (ct-eval ctx "(and true)")) "and true")
(assert (= nil (ct-eval ctx "(and nil)")) "and nil")
(assert (= false (ct-eval ctx "(and false)")) "and false")
(assert (= nil (ct-eval ctx "(and true nil)")) "and true nil")
(assert (= false (ct-eval ctx "(and true false)")) "and true false")
(assert (= "abc" (ct-eval ctx "(and 1 true :kw 'abc \"abc\")")) "and chain last")
(assert (= nil (ct-eval ctx "(and 1 true :kw nil 'abc \"abc\")")) "and chain nil")
(assert (= false (ct-eval ctx "(and 1 true :kw 'abc \"abc\" false)")) "and chain false"))
(print " ok")
# --- test-or ---
(print "test-or...")
(let [ctx (init)]
(assert (= nil (ct-eval ctx "(or)")) "or empty")
(assert (= true (ct-eval ctx "(or true)")) "or true")
(assert (= nil (ct-eval ctx "(or nil)")) "or nil")
(assert (= false (ct-eval ctx "(or false)")) "or false")
(assert (= true (ct-eval ctx "(or nil false true)")) "or nil false true")
(assert (= 1 (ct-eval ctx "(or nil false 1 2)")) "or picks first truthy")
(assert (= "abc" (ct-eval ctx "(or nil false \"abc\" :kw)")) "or picks string")
(assert (= nil (ct-eval ctx "(or false nil)")) "or false nil -> nil")
(assert (= false (ct-eval ctx "(or nil false)")) "or nil false -> false")
(assert (= false (ct-eval ctx "(or nil nil nil false)")) "or chain to false")
(assert (= true (ct-eval ctx "(or nil true false)")) "or nil true false"))
(print " ok")
# --- test-not ---
(print "test-not...")
(let [ctx (init)]
(assert (= true (ct-eval ctx "(not nil)")) "not nil")
(assert (= true (ct-eval ctx "(not false)")) "not false")
(assert (= false (ct-eval ctx "(not true)")) "not true")
(assert (= false (ct-eval ctx "(not 0)")) "not 0")
(assert (= false (ct-eval ctx "(not 0.0)")) "not 0.0")
(assert (= false (ct-eval ctx "(not 42)")) "not 42")
(assert (= false (ct-eval ctx "(not 1.2)")) "not 1.2")
(assert (= false (ct-eval ctx "(not \"\")")) "not empty string")
(assert (= false (ct-eval ctx "(not \"abc\")")) "not string")
(assert (= false (ct-eval ctx "(not 'abc)")) "not symbol")
(assert (= false (ct-eval ctx "(not :kw)")) "not keyword")
(assert (= false (ct-eval ctx "(not ())")) "not empty list")
(assert (= false (ct-eval ctx "(not '(1 2))")) "not list")
(assert (= false (ct-eval ctx "(not []))")) "not empty vector")
(assert (= false (ct-eval ctx "(not [1 2])")) "not vector")
(assert (= false (ct-eval ctx "(not {})")) "not empty map")
(assert (= false (ct-eval ctx "(not {:a 1 :b 2})")) "not map")
(assert (= false (ct-eval ctx "(not #{})")) "not empty set")
(assert (= false (ct-eval ctx "(not #{1 2})")) "not set"))
(print " ok")
# --- test-some? ---
(print "test-some?...")
(let [ctx (init)]
(assert (= false (ct-eval ctx "(some? nil)")) "some? nil")
(assert (= true (ct-eval ctx "(some? false)")) "some? false")
(assert (= true (ct-eval ctx "(some? 0)")) "some? 0")
(assert (= true (ct-eval ctx "(some? \"abc\")")) "some? string")
(assert (= true (ct-eval ctx "(some? [])")) "some? empty vec"))
(print " ok")
(print "\nAll Ported Logic tests passed!")