Phase 3: Var system — var-get, var-set, var?, alter-var-root, find-var, alter-meta!, reset-meta!

- types.janet: find-var (ctx-based var resolution), alter-meta!, reset-meta!
- evaluator.janet: 10 new special-form dispatch arms for var ops
- core.janet: 6 new Clojure-level wrappers in core-bindings
- core-meta: add var-aware branch (was struct-only)
- core-binding macro: use array-map instead of hash-map (PHM incompatibility)
- 8 new tests (17: var system, 18: var metadata) — all pass
- 317 total tests, 0 failures
This commit is contained in:
Yogthos 2026-06-02 20:43:24 -04:00
parent ad895945a0
commit 8e86c7c2ec
8 changed files with 177 additions and 51 deletions

View file

@ -1,15 +1,9 @@
Jolt Compiler Architecture (Phase 0-6, dfa9874→c1dde76): Two-phase — analyze-form (Clojure form → annotated AST) → emit-ast (→ Janet source string) or emit-expr (→ Janet data structures for eval). analyze-form takes [form bindings &opt ctx]; ctx needed for macro expansion. Symbol classification: bindings first (:local), then core-renames (:core-symbol), then plain (:symbol). compile-and-eval takes [form ctx]; pass nil for no macro ctx.
Key naming: Clojure - → core-sub (NOT core--). Missing from core-renames early: fn?, list, name, subs, nth. core-renames MUST match actual defn names in core.janet — add to BOTH core-renames (string table) and core-fn-values (fn value table).
Janet eval gotchas: bare tuples treated as fn calls → emit ['tuple ...]. Janet try = (try body ([err] handler)) NOT (try body (catch sym handler)). make-symbol: / at pos 0 = unqualified symbol. raw-form->janet: pass quoted forms through verbatim, don't re-analyze.
eval-string dispatch: When :compile? true, EVERYTHING goes through compiler EXCEPT stateful forms (defmacro, ns, deftype, defmulti, defmethod, require, in-ns, syntax-quote, set!, var, ., new). Bare symbols now also go through compile path (Phase 0 fix).
Phase 0 (defn fix): compile-and-eval interns def/defn results in Jolt namespace via ns-intern so interpreter can resolve bare symbols.
Phase 1: ns accessors (all-ns, remove-ns, create-ns, the-ns, ns-interns, ns-aliases, ns-imports), ns form extended with :require/:refer, :use, :refer-clojure/:exclude, :import. binding macro via push-thread-bindings/pop-thread-bindings.
Phase 2 (PersistentHashMap): Live in src/jolt/phm.janet — separate module imported via (use ./phm) to avoid forward-reference issues. PHM is a table with :jolt/deftype tag "jolt.lang.persistent-hash-map.PersistentHashMap". Has :cnt, :buckets (array of 8 arrays), :_meta. Bucket-based: each bucket is flat [k v k v ...] array. phm-assoc, phm-dissoc, phm-get, phm-contains?, phm-entries, phm-to-struct (→ Janet struct for compatibility). Core functions updated with PHM branches: core-map?, core-hash-map, core-get, core-count, core-keys, core-vals, core-contains?, core-empty?, core-seq, core-conj, core-assoc, core-dissoc, core-merge, core-merge-with, core-into, core-=.
Macro expansion: resolve-macro at analyze time → expand → re-analyze. Loop: (do (var _loop_N nil) (set _loop_N (fn [params] body)) (_loop_N vals...)). Recur: emits (loop-name args...) via :loop-name in AST.
§
Test files: test/phase6-final.janet (47 tests, 58 assertions — collections, math, predicates, comparison, seq ops, special forms, macros, complex nesting). Phase 1 tests appended to test/compiler-test.janet (ns accessors, ns form extensions). All 317 tests pass.

View file

@ -1,7 +1,7 @@
When you need to mutate a local with `set`, use `(var x nil)` not `(def x nil)`. `def` creates constants — `(set ns-name ...)` on a `def` fails with "cannot set constant". This hit us in loader.janet ns-name extraction loop.
§
core-renames MUST match actual function names in core.janet. `"-"``core-sub` NOT `core--`. `"fn?"` was missing entirely (caused silent nil). Missing entries → symbol treated as unknown global, returns nil. When adding: grep core.janet for actual `(defn core-XXX)` name, add to BOTH core-renames (string table) and core-fn-values (fn value table).
§
Bare tuples in Janet's `eval` are function calls: `(eval [1 2 3])` tries to call `1` as function. Always emit `['tuple 1 2 3]` or `(tuple 1 2 3)` in data-structure emitter. Similarly, `(eval (try body (sym handler)))` fails because `catch` is not a Janet special form — must be `(try body ([sym] handler))`. Discovered during Phase 5/6 compiler work.
§
Duplicate function definitions in the same file cause hard-to-diagnose "unknown symbol" errors. In compiler.janet, emit-quote-str was defined twice (once before emit-ast dispatch, once before emit-expr section). The second definition compiled but the first was used by emit-ast dispatch — causing "unknown symbol raw-form->janet". Always grep for the fn name before adding a new definition.
§
Janet break can't be used inside let blocks — break returns from the innermost loop, and in a let, there's no loop. Pattern: use (var found nil) + (while ... (if condition (do (set found val) (break)))) then check found after loop.

View file

@ -1,38 +1,16 @@
{
"jolt-dev": {
"created_by": "agent",
"use_count": 29,
"view_count": 44,
"patch_count": 34,
"last_used_at": "2026-06-02T20:32:00.407408+00:00",
"last_viewed_at": "2026-06-02T21:03:17.891717+00:00",
"last_patched_at": "2026-06-02T20:33:05.356194+00:00",
"use_count": 30,
"view_count": 45,
"patch_count": 37,
"last_used_at": "2026-06-02T22:24:09.648320+00:00",
"last_viewed_at": "2026-06-02T22:24:09.636430+00:00",
"last_patched_at": "2026-06-02T22:24:40.254179+00:00",
"created_at": "2026-06-01T21:26:06.614465+00:00",
"state": "active",
"pinned": false
},
"jolt-compiler": {
"created_by": "agent",
"use_count": 3,
"view_count": 7,
"patch_count": 6,
"last_used_at": "2026-06-02T21:40:37.747767+00:00",
"last_viewed_at": "2026-06-02T21:40:37.739522+00:00",
"last_patched_at": "2026-06-02T21:40:55.723354+00:00",
"created_at": "2026-06-02T17:54:38.690279+00:00",
"state": "active",
"pinned": false
},
"jpm-build": {
"created_by": "agent",
"use_count": 0,
"view_count": 15,
"patch_count": 0,
"last_viewed_at": "2026-06-02T21:03:17.897587+00:00",
"created_at": "2026-06-01T20:56:39.144222+00:00",
"state": "active",
"pinned": false
},
"jolt-bootstrap": {
"created_by": "agent",
"use_count": 9,
@ -44,5 +22,27 @@
"created_at": "2026-06-01T21:49:51.101718+00:00",
"state": "active",
"pinned": false
},
"jolt-compiler": {
"created_by": "agent",
"use_count": 4,
"view_count": 8,
"patch_count": 6,
"last_used_at": "2026-06-02T22:24:09.679583+00:00",
"last_viewed_at": "2026-06-02T22:24:09.655249+00:00",
"last_patched_at": "2026-06-02T21:40:55.723354+00:00",
"created_at": "2026-06-02T17:54:38.690279+00:00",
"state": "active",
"pinned": false
},
"jpm-build": {
"created_by": "agent",
"use_count": 0,
"view_count": 15,
"patch_count": 0,
"last_viewed_at": "2026-06-02T21:03:17.897587+00:00",
"created_at": "2026-06-01T20:56:39.144222+00:00",
"state": "active",
"pinned": false
}
}

View file

@ -12,8 +12,25 @@ cd /Users/yogthos/src/jolt
jpm build # produces build/jolt
jpm test # runs all tests
janet test/foo.janet # run a single test file from project root
janet -k file.janet # check parse without executing (useful for syntax validation)
```
## Python3 batch edits
When the `edit` tool is blocked by syntax checker false positives on complex multi-line replacements, use `python3` for batch text edits:
```bash
python3 << 'PYEOF'
with open('src/jolt/core.janet') as f:
content = f.read()
content = content.replace('old text', 'new text')
with open('src/jolt/core.janet', 'w') as f:
f.write(content)
PYEOF
```
Always run `janet -k file.janet` after to verify syntax.
## Compiler Development
See `jolt-compiler` skill for the Clojure→Janet source-to-source compiler workflow.
@ -113,15 +130,33 @@ The match arm receives `ctx`, `bindings`, and `form` (the full list). Use `(in f
## Persistent Data Structures
Located in:
- `src/jolt/clojure/lang/persistent_vector.clj`
- `src/jolt/clojure/lang/persistent_hash_map.clj`
### PersistentVector
Located in `src/jolt/clojure/lang/persistent_vector.clj`.
Loaded at init time by `load-persistent-structures` in `api.janet`.
32-way branching trie with tail optimization.
Loaded at init time by `load-persistent-structures` in `api.janet`. Use `{:mutable? true}` to skip and use Janet-native types.
### PersistentHashMap
Located in `src/jolt/phm.janet` — Janet module, NOT a .clj file.
Imported via `(use ./phm)` in `src/jolt/core.janet`.
Bucket-based (8 buckets), each bucket is a flat `[k v k v ...]` array.
PHM is a table with magic type tag: `:jolt/deftype` = `"jolt.lang.persistent-hash-map.PersistentHashMap"`.
Key fields: `:cnt` (entry count), `:buckets` (array of arrays), `:_meta`.
### Implementation detail
Simple array-based implementation (node-assoc/node-find/find-key-index), NOT HAMT bit-trie.
HAMT failed because Janet uses 64-bit doubles and bit operations require 32-bit signed ints.
API: `phm-get`, `phm-assoc`, `phm-dissoc`, `phm-contains?`, `phm-entries`, `phm-to-struct`.
`phm-to-struct` converts PHM back to Janet struct for compatibility with `keys`, `deep=`, etc.
**Design decision**: PHM lives in a separate Janet module rather than embedded in core.janet, to avoid forward-reference issues (core-map? calls phm? which wasn't yet defined when core-map? was being compiled).
**16 core functions updated** with PHM-aware branches: core-map?, core-hash-map, core-get, core-count, core-keys, core-vals, core-contains?, core-empty?, core-seq, core-conj, core-assoc, core-dissoc, core-merge, core-merge-with, core-into, core-=.
### Working with PHM in core functions
- Check `(phm? coll)` FIRST, before generic struct/table checks
- Use `phm-get` not `(m key)` — PHM is a table but keys aren't direct properties
- Use `phm-to-struct` when you need Janet-native operations (deep=, keys, etc.)
- PHM equality: `core-=` converts both sides to structs via `phm-to-struct`, then uses `deep=`
### Loading persistent structures
Use `{:mutable? true}` in `init` to skip persistent types and use Janet-native structs.
## Janet Gotchas
@ -129,6 +164,8 @@ HAMT failed because Janet uses 64-bit doubles and bit operations require 32-bit
- `deftype` creates tables, not structs. `struct?` returns false.
- `(get child :key)` DOES follow table prototype chain — resolved and confirmed working.
- Janet LSP produces many false positives on `.janet` files — safe to ignore.
- `break` can't be used inside `let` blocks — `break` returns from the innermost loop, and a `let` has no loop. Use `(var found nil)` pattern + `(while ... (if cond (do (set found val) (break))))` then check `found` after loop.
- Duplicate function definitions in the same file cause hard-to-diagnose "unknown symbol" errors. Always grep for the fn name before adding.
## Symbol representation