Phase 7: LazySeq + PersistentHashSet completion
- phm.janet: LazySeq (realize-once with caching, ls-first/ls-rest/ls-seq/ls-count) PersistentHashSet (backed by PHM, phs-conj/phs-disj/phs-contains?/phs-count/...) make-lazy-seq creates lazy thunk wrapper with :jolt/lazy-seq type tag make-phs creates hash set with :jolt/set type tag - evaluator.janet: :jolt/set handler in eval-form, disj/set? special forms, special-symbol? entries, phm import for compile-time visibility - core.janet: lazy-seq/iterate macros, set?/disj core fns, make-phs wired into hash-set, core-bindings entries for set?/disj/lazy-seq/make-lazy-seq/iterate Set support in core-conj/core-contains?/core-count/core-empty?/core-seq/core-get LazySeq support in core-first/core-rest/core-count/core-seq - 9 tests (32-33): lazy-seq basic ops, realize-once caching PersistentHashSet construction, conj, disj, count, set? predicate - 315 ok, 2 fail (pre-existing, unchanged)
This commit is contained in:
parent
f410f5c48b
commit
fe22fea3e4
7 changed files with 286 additions and 133 deletions
|
|
@ -1,9 +1,18 @@
|
|||
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.
|
||||
Jolt Compiler Architecture (Phases 1-6, dfa9874→1de109f): 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). Two emitter paths: string (compile-form) and data structures (compile-ast). Core fn values resolved via core-fn-values table. compile-and-eval takes [form ctx]; pass nil for no macro ctx.
|
||||
|
||||
Key naming/facts:
|
||||
- Clojure - → core-sub (NOT core--)
|
||||
- core-nth did not exist — had to add both the function and core-bindings entry
|
||||
- Missing from core-renames early: fn?, list, name, subs
|
||||
- Bare tuples in Janet eval → treated as function calls. Always emit (tuple ...) or ['tuple ...]
|
||||
- make-symbol: / at position 0 means unqualified symbol (was parsing empty ns)
|
||||
- raw-form->janet converter for quote: don't re-analyze quoted forms, pass through verbatim
|
||||
- emit-try-expr: Janet format is (try body ([err] handler)) not (try body (catch sym handler))
|
||||
- Loop compilation: (do (var _loop_N nil) (set _loop_N (fn [params] body)) (_loop_N init-vals...))
|
||||
- Recur compilation: rewrites to (loop-name arg1 arg2...) via :loop-name in AST
|
||||
|
||||
eval-string dispatch: When :compile? true, stateful forms (defmacro, ns, deftype, defmulti, defmethod, require, in-ns) use interpreter. All others (def, macros like defn) go through compile-and-eval. Macros expanded at analyze time via resolve-macro.
|
||||
|
||||
Remaining: syntax-quote, set! compiler support. deftype/defmulti/defmethod routed to interpreter.
|
||||
§
|
||||
Phase 3 (Var system): find-var (ctx-based, resolve-q/nq symbol), alter-meta!, reset-meta!, var-get/var-set/var?/alter-var-root all in types.janet + evaluator dispatch arms + core-bindings wrappers. core-meta fixed: (var? x) branch → var-meta, struct? branch → :meta. 10 tests pass.
|
||||
§
|
||||
Phase 4 (deftype/defrecord): deftype instances are tables with :jolt/deftype key (e.g. "user.Point"). Field access via (. obj field), mutation via (set! (.-field obj) val) — reader parses .-field as (. -field obj) in array form. core-map? recognizes table+deftype. core-count skips :jolt/deftype key. core-defrecord emits (deftype ...) + ->TypeName arrow factory + map->TypeName factory (deferred). 11 tests pass including record equality. 317 total, 0 fail.
|
||||
§
|
||||
Key implementation facts: find-var MUST be placed after ctx-find-ns in types.janet (forward-reference). intern dispatch arm needs eval-form on args. core-meta: check (var? x) before (struct? x) — var-meta returns metadata for vars. core-binding uses array-map (plain struct) not hash-map/PHM — PHM's phm-get incompatible with var-get in push-thread-bindings.
|
||||
§
|
||||
Phase 5 (Multimethods + hierarchy): Not yet started. defmulti/defmethod exist in evaluator.janet (lines 611-656) but are routed to interpreter in compile mode. core-derive, core-isa?, core-ancestors, core-descendants are stubs in core.janet.
|
||||
Janet gotchas: (1) `parse` returns `[form, consumed-count]`, NOT `[form, error?]` — use parser/new→consume→eof→produce pipeline. (2) `:#inst` is invalid keyword literal — use `(keyword "#inst")` dynamically. (3) Janet `case` works for multi-arity simulation when `defn ([] body) ([x] body)` fails. (4) Bare tuples in eval are function calls — always use `['tuple ...]`. (5) Core `-` maps to `core-sub` NOT `core--`.
|
||||
|
|
|
|||
|
|
@ -1,38 +1,16 @@
|
|||
{
|
||||
"jolt-compiler": {
|
||||
"created_by": "agent",
|
||||
"use_count": 6,
|
||||
"view_count": 10,
|
||||
"patch_count": 8,
|
||||
"last_used_at": "2026-06-03T01:15:04.055288+00:00",
|
||||
"last_viewed_at": "2026-06-03T01:15:04.048271+00:00",
|
||||
"last_patched_at": "2026-06-03T01:15:38.021717+00:00",
|
||||
"created_at": "2026-06-02T17:54:38.690279+00:00",
|
||||
"state": "active",
|
||||
"pinned": false
|
||||
},
|
||||
"jolt-dev": {
|
||||
"created_by": "agent",
|
||||
"use_count": 33,
|
||||
"view_count": 48,
|
||||
"use_count": 34,
|
||||
"view_count": 49,
|
||||
"patch_count": 41,
|
||||
"last_used_at": "2026-06-03T01:13:54.913664+00:00",
|
||||
"last_viewed_at": "2026-06-03T01:13:54.902684+00:00",
|
||||
"last_used_at": "2026-06-03T03:03:32.578112+00:00",
|
||||
"last_viewed_at": "2026-06-03T03:03:32.570889+00:00",
|
||||
"last_patched_at": "2026-06-03T01:14:59.915995+00:00",
|
||||
"created_at": "2026-06-01T21:26:06.614465+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": 7,
|
||||
"view_count": 11,
|
||||
"patch_count": 9,
|
||||
"last_used_at": "2026-06-03T03:02:46.825298+00:00",
|
||||
"last_viewed_at": "2026-06-03T03:02:46.818525+00:00",
|
||||
"last_patched_at": "2026-06-03T03:03:27.524946+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
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,6 @@
|
|||
# jolt-compiler
|
||||
# Jolt Compiler
|
||||
|
||||
Source-to-source Clojure→Janet compiler. Two-phase: analyze-form (classify + macro expand) → emit-ast (generate).
|
||||
Jolt Compiler — Clojure→Janet source compiler with data-structure emission path.
|
||||
|
||||
## Architecture
|
||||
|
||||
|
|
@ -29,17 +28,6 @@ emit-core-symbol-expr → (get core-fn-values janet-name)
|
|||
|
||||
Source-to-source (`compile-form` + `emit-ast`) still exists for debugging but is NOT used by `compile-and-eval`.
|
||||
|
||||
## Macro expansion
|
||||
|
||||
`analyze-form` checks whether the head symbol of a list resolves to a macro var before dispatching to special form handling:
|
||||
|
||||
1. Look up symbol in current ns → core ns via `resolve-macro`
|
||||
2. If `var-macro?` is true, call `(var-get macro-var)` to get the fn
|
||||
3. `(apply macro-fn (tuple/slice form 1))` to expand
|
||||
4. `(analyze-form expanded ...)` to re-analyze the result
|
||||
|
||||
Macros expand at analyze time, before emission. `defn` expands to `(def name (fn* ...))`, `when` to `(if test (do ...) nil)`, etc.
|
||||
|
||||
## Symbol classification (in analyze-form)
|
||||
|
||||
Order: qualified ns → local binding → core-symbol → bare symbol
|
||||
|
|
@ -51,18 +39,19 @@ Order: qualified ns → local binding → core-symbol → bare symbol
|
|||
→ :symbol)
|
||||
```
|
||||
|
||||
core-renames MUST match actual fn names: `"-"` → `"core-sub"` (not `"core--"`), `"not"` → `"core-not"`. Verify against `core.janet` bindings.
|
||||
## core-renames vs core-fn-values
|
||||
|
||||
## core-fn-values
|
||||
core-renames maps Clojure name strings → Janet function name strings (used by both emitter paths).
|
||||
core-fn-values maps Janet function name strings → actual function values (used by data-structure emitter only).
|
||||
|
||||
Maps Janet string names to actual function values. Must be kept in sync with core-renames. When adding a new core fn, update BOTH tables.
|
||||
MUST keep both in sync. When adding a new core fn, update BOTH.
|
||||
**Missing entries → symbol treated as unknown global, returns nil.**
|
||||
|
||||
Functions that need special mapping (name differs):
|
||||
Key name mappings:
|
||||
- `"-"` → `"core-sub"` (NOT `"core--"`)
|
||||
- `"apply"` → `apply` (Janet built-in)
|
||||
- `"-"` → `"core-sub"` (not `core--`)
|
||||
- `"some"` → `core-some?` (shared with `core-some?`)
|
||||
- `"pr-str"` → `core-str` (alias)
|
||||
- `"nth"` → `core-nth` (separate function, added in Phase 6)
|
||||
- `"list"` → `core-list`, `"name"` → `core-name`, `"subs"` → `core-subs`
|
||||
|
||||
## Loop/recur compilation
|
||||
|
|
@ -76,62 +65,53 @@ Functions that need special mapping (name differs):
|
|||
|
||||
`recur` saves `:loop-name` in the AST (looked up from bindings `:jolt/current-loop`), then `emit-recur-expr` rewrites to `(loop-name arg1 arg2...)`.
|
||||
|
||||
In the string emitter, recur similarly emits `(loop-name arg ...)`.
|
||||
|
||||
## Throw/try compilation
|
||||
|
||||
- `throw` → `(error val)` in Janet
|
||||
- `try/catch` → `(try body ([err] handler-body))` — NOTE: Janet uses `([sym] handler)` format, NOT `(catch sym handler)`
|
||||
- `try/finally` → appends do-block after catch clause in the Janet tuple
|
||||
- `try/catch` → `(try body ([err] handler-body))` — Janet uses `([sym] handler)` format
|
||||
|
||||
## emi-vector-expr critical fix
|
||||
|
||||
**Bare tuples in Janet eval are function calls**: `[1 2 3]` tries to call `1` as a function. Always emit `['tuple ...]` or `(tuple ...)`.
|
||||
|
||||
## Quote in data-structure emitter
|
||||
|
||||
Don't re-analyze quoted forms. Use `raw-form->janet` to pass Jolt reader forms through verbatim to Janet's `quote`:
|
||||
Use `raw-form->janet` to pass Jolt reader forms through verbatim to Janet's `quote`:
|
||||
```
|
||||
(emit-quote-expr expr) → ['quote (raw-form->janet expr)]
|
||||
```
|
||||
raw-form->janet converts symbols to Janet symbols, arrays/tuples recursively.
|
||||
raw-form->janet converts Jolt symbols to Janet symbols, recursively for arrays/tuples.
|
||||
|
||||
## Remaining ops (interpreter only)
|
||||
## make-symbol fix (reader.janet)
|
||||
|
||||
`syntax-quote`, `set!`, `deftype`, `defmulti`, `defmethod` — these are stateful or complex and always use the interpreter path even in compile mode.
|
||||
`/` at position 0 means unqualified symbol. Use `(if (and slash (> slash 0)) ...)` — only split on `/` when it's not at the start.
|
||||
|
||||
## deftype in interpreter (eval-list arm)
|
||||
## :data-readers key construction
|
||||
|
||||
deftype creates a constructor function returning a Janet table with `:jolt/deftype` key (`"ns.TypeName"`). Evaluator `set!` handles `(.-field obj)` field mutation (reader creates `(. -field obj)` array form). `instance?` checks `:jolt/deftype` tag. defrecord macro in core.janet emits `(do (deftype Name [fields]) (def ->Name ...) (def map->Name ...))`.
|
||||
|
||||
## Stateful forms (must use interpreter, NOT compiler)
|
||||
|
||||
These forms modify context state and cannot be compiled:
|
||||
- `defmacro`, `ns`, `deftype`, `defmulti`, `defmethod`, `require`, `in-ns`
|
||||
- `syntax-quote`, `set!`, `var`, `.`, `new`
|
||||
|
||||
Note: `def` IS handled by the compiler (compiles to Janet `def`, since macros are expanded at analyze time).
|
||||
`:data-readers` uses dynamic table construction because `:#inst` is invalid Janet keyword literal syntax (`:#` triggers reader macro):
|
||||
```janet
|
||||
:data-readers (let [dr @{}]
|
||||
(put dr (keyword "#inst") (fn [s] s))
|
||||
(put dr (keyword "#uuid") (fn [s] s))
|
||||
dr)
|
||||
```
|
||||
|
||||
## eval-string dispatch (compile mode)
|
||||
|
||||
```janet
|
||||
(if (or (= head-name "defmacro") (= head-name "ns")
|
||||
(= head-name "deftype") (= head-name "defmulti") (= head-name "defmethod")
|
||||
(= head-name "require") (= head-name "in-ns")
|
||||
(= head-name "syntax-quote") (= head-name "set!")
|
||||
(= head-name "var") (= head-name ".") (= head-name "new"))
|
||||
(= head-name "require") (= head-name "in-ns"))
|
||||
(eval-form ctx @{} form) ; interpret
|
||||
(compile-and-eval form ctx)) ; compile
|
||||
```
|
||||
|
||||
## Adding a new op
|
||||
|
||||
1. Add `analyze-form` match arm for the special form
|
||||
2. Add `emit-ast` match arm (source string path)
|
||||
3. Add `emit-expr` match arm (data structure path)
|
||||
4. Add `core-renames` entry if it's a core fn (name → Janet string name)
|
||||
5. Add `core-fn-values` entry (Janet string name → actual fn value)
|
||||
6. Add tests in `test/compiler-test.janet`
|
||||
|
||||
## Test files
|
||||
|
||||
- `test/compiler-test.janet` — Phase 2-5 tests (source output + compile-eval + macro tests)
|
||||
- `test/compiler-test.janet` — Phases 0-4 tests (source output + compile-eval + macro tests)
|
||||
- `test/phase6-final.janet` — Phase 6 comprehensive compile-mode tests (47 assertions)
|
||||
- `test/phase5-test.janet` — Phase 5 multimethod + hierarchy tests
|
||||
- `test/phase6-test.janet` — Phase 6 reader extension tests
|
||||
- `test/hash-map-test.janet` — Phase 2 PersistentHashMap tests
|
||||
|
||||
Run: `janet test/compiler-test.janet` or `janet test/phase6-final.janet` or `jpm test`
|
||||
Run: `janet test/<file>.janet` or `jpm test`
|
||||
Loading…
Add table
Add a link
Reference in a new issue