Phase 4: deftype/defrecord completion
- evaluator.janet: fix set! to handle (.-field obj) shorthand Previously only recognized (. obj -field) format - core.janet: rewrite core-defrecord to emit (deftype ...) instead of plain def with array-map constructor - core.janet: fix core-map? to recognize deftype table instances (and returns last truthy, not boolean true — wrap in if) - core.janet: fix core-count to skip :jolt/deftype key for records - core.janet: add ->TypeName arrow factory that calls TypeName. constructor via Clojure form emission - 11 new tests: deftype fields/mutation/instance?, defrecord construction/map-behavior/fields/factories, record equality - map->TypeName factory deferred (needs get/symbol resolution fix) - 317 tests pass, 0 failures
This commit is contained in:
parent
fc7f49487b
commit
fb66851e06
8 changed files with 204 additions and 284 deletions
|
|
@ -1,13 +1,4 @@
|
|||
---
|
||||
triggers:
|
||||
- "compile jolt"
|
||||
- "jolt compiler"
|
||||
- "Clojure to Janet compilation"
|
||||
- "add new op to compiler"
|
||||
- "fix compiler"
|
||||
---
|
||||
|
||||
# Jolt Compiler
|
||||
# jolt-compiler
|
||||
|
||||
Source-to-source Clojure→Janet compiler. Two-phase: analyze-form (classify + macro expand) → emit-ast (generate).
|
||||
|
||||
|
|
@ -24,142 +15,81 @@ Clojure form → analyze-form [form bindings ctx] → AST {:op ...}
|
|||
Three public entry points:
|
||||
- `(compile-form form &opt ctx)` → Janet source string (debug/display)
|
||||
- `(compile-ast form &opt ctx)` → Janet data structure (for eval)
|
||||
- `(compile-and-eval form ctx)` → compile-ast + eval
|
||||
- `(compile-and-eval form ctx)` → compile-ast + eval + ns interning
|
||||
|
||||
## Why data structures, not source strings
|
||||
|
||||
Janet's `eval` does NOT have access to `use`-imported symbols from the calling file. `(eval "(core-inc 1)")` fails with "unknown symbol core-inc". The fix: emit Janet tuples where function VALUES are embedded: `[core-inc 1]`.
|
||||
Janet's `eval` does NOT have access to `use`-imported symbols. Emit Janet tuples with embedded function VALUES via `core-fn-values` table.
|
||||
|
||||
```
|
||||
core-fn-values table: "core-inc" → core-inc (the actual function)
|
||||
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`.
|
||||
Source-to-source (`compile-form` + `emit-ast`) exists for debugging, 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:
|
||||
`analyze-form` checks `resolve-macro` before special form dispatch:
|
||||
1. Look up symbol in current ns → core ns
|
||||
2. If `var-macro?`, apply fn, re-analyze expanded form
|
||||
|
||||
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)
|
||||
## Symbol classification
|
||||
|
||||
Order: qualified ns → local binding → core-symbol → bare symbol
|
||||
|
||||
```
|
||||
(if (form :ns) → :qualified-symbol
|
||||
(get bindings name) → :local
|
||||
(get core-renames name) → :core-symbol
|
||||
→ :symbol)
|
||||
```
|
||||
|
||||
core-renames MUST match actual fn names: `"-"` → `"core-sub"` (not `"core--"`), `"not"` → `"core-not"`. Verify against `core.janet` bindings.
|
||||
core-renames MUST match actual fn names: `"-"` → `"core-sub"` (not `"core--"`).
|
||||
|
||||
## core-fn-values
|
||||
|
||||
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.
|
||||
Maps Janet string names to actual function values. Must be kept in sync with core-renames.
|
||||
Special mappings: `"apply"` → `apply` (built-in), `"some"` → `core-some?`, `"pr-str"` → `core-str`, `"nth"` → `core-nth`.
|
||||
|
||||
Functions that need special mapping (name differs):
|
||||
- `"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`
|
||||
## compile-and-eval interning
|
||||
|
||||
`def`/`defn` results are interned in the Jolt namespace via `ns-intern` so the interpreter can resolve bare symbols. This is critical — without it, `(defn foo [x] x)` followed by `(foo 1)` would fail with "Unable to resolve symbol: foo".
|
||||
|
||||
## Loop/recur compilation
|
||||
|
||||
`loop*` emits a self-referential closure:
|
||||
```janet
|
||||
(do (var _loop_N nil)
|
||||
(set _loop_N (fn [params] body))
|
||||
(_loop_N init-vals...))
|
||||
```
|
||||
|
||||
`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 ...)`.
|
||||
`loop*` → `(do (var name nil) (set name (fn [params] body)) (name init-vals...))`
|
||||
`recur` → `(loop-name arg...)` via `:jolt/current-loop` binding
|
||||
|
||||
## 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` → `(try body ([err] handler))` — Janet uses `([sym] handler)`, NOT `(catch sym handler)`
|
||||
|
||||
## Quote in data-structure emitter
|
||||
## Quote — use raw-form->janet
|
||||
|
||||
Don't re-analyze quoted forms. 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.
|
||||
Don't re-analyze quoted forms. Use `raw-form->janet` to pass Jolt reader forms verbatim to Janet's `quote`.
|
||||
|
||||
## Remaining ops (interpreter only)
|
||||
|
||||
`syntax-quote`, `set!`, `deftype`, `defmulti`, `defmethod` — these are stateful or complex and always use the interpreter path even in compile mode.
|
||||
`syntax-quote`, `set!`, `deftype`, `defmulti`, `defmethod` — stateful/complex, always use interpreter.
|
||||
|
||||
## Stateful forms (must use interpreter, NOT compiler)
|
||||
## Stateful forms (interpreter only even in compile mode)
|
||||
|
||||
These forms modify context state and cannot be compiled:
|
||||
- `defmacro`, `ns`, `deftype`, `defmulti`, `defmethod`, `require`, `in-ns`
|
||||
`defmacro`, `ns`, `deftype`, `defmulti`, `defmethod`, `require`, `in-ns`
|
||||
|
||||
Note: `def` IS handled by the compiler (compiles to Janet `def`, since macros are expanded at analyze time).
|
||||
Note: `def` IS handled by compiler (macros expanded at analyze time).
|
||||
|
||||
## eval-string dispatch (compile mode)
|
||||
|
||||
When `:compile?` is true, everything goes through the compiler EXCEPT stateful forms. Phase 0 fix: bare symbols and tuples also go through compile path (not just arrays).
|
||||
|
||||
Stateful forms (always use interpreter):
|
||||
`defmacro`, `ns`, `deftype`, `defmulti`, `defmethod`, `require`, `in-ns`, `syntax-quote`, `set!`, `var`, `.`, `new`
|
||||
|
||||
Note: `def` is handled by the compiler — macros like `defn` expand to `(def name (fn* ...))` and `def` emits Janet `def`.
|
||||
|
||||
## compile-and-eval def/defn interning
|
||||
|
||||
`compile-and-eval` interns `def`/`defn`/`defn-` results in the Jolt namespace via `ns-intern` so the interpreter can resolve bare symbols later. Without this, `(defn foo [x] x)` creates the Janet global but `foo` as a bare symbol can't be found by the interpreter.
|
||||
|
||||
## 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`. raw-form->janet now handles namespace-qualified symbols: `{:ns "foo" :name "bar"}` → `foo/bar` Janet symbol.
|
||||
|
||||
## Remaining ops (interpreter only)
|
||||
|
||||
`syntax-quote`, `set!`, `deftype`, `defmulti`, `defmethod`, `var`, `.`, `new` — these are stateful or complex and always use the interpreter path even in compile mode.
|
||||
|
||||
## Binding macro (Phase 1)
|
||||
|
||||
`core-binding` macro generates `(let [frame {var1 var1-obj var2 var2-obj ...}] (push-thread-bindings frame) (try (do body...) (finally (pop-thread-bindings))))`. Call forms inside the macro expansion MUST use `@[...]` arrays, not tuples — the evaluator treats tuples as literal vectors.
|
||||
Stateful check: `defmacro`, `ns`, `deftype`, `defmulti`, `defmethod`, `require`, `in-ns`, `syntax-quote`, `set!`, `var`, `.`, `new` → interpreter. Everything else → `compile-and-eval`.
|
||||
|
||||
## 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`
|
||||
1. Add `analyze-form` match arm
|
||||
2. Add `emit-ast` + `emit-expr` match arms
|
||||
3. Update `core-renames` + `core-fn-values` if core fn
|
||||
4. Add tests in `test/compiler-test.janet`
|
||||
|
||||
## Phase 1: Var/Namespace system
|
||||
## Var system (Phase 3)
|
||||
|
||||
- `types.janet`: `all-ns`, `remove-ns`, `create-ns`, `the-ns`, `ns-interns`, `ns-aliases`, `ns-imports-fn`
|
||||
- `core.janet`: `core-binding` macro + `core-push-thread-bindings`/`core-pop-thread-bindings`
|
||||
- `evaluator.janet`: ns accessor special forms, ns form extended with `:require`/`:refer`, `:use`, `:refer-clojure`/`:exclude`, `:import` clauses
|
||||
`var-get`, `var-set`, `var?`, `alter-var-root`, `find-var`, `alter-meta!`, `reset-meta!`, `intern` — all dispatched in evaluator, with Clojure wrappers in `core-bindings`. `find-var` takes a ctx + symbol, must eval args first (NOT pass raw form). `core-meta` handles vars via `var-meta` branch.
|
||||
|
||||
## PersistentHashMap (Phase 2)
|
||||
|
||||
`src/jolt/phm.janet` — separate module, bucket-based with `:jolt/deftype` tag. `core-binding` macro uses `array-map` (not `hash-map`) to avoid PHM get() incompatibility with `push-thread-bindings`.
|
||||
|
||||
## Test files
|
||||
|
||||
- `test/compiler-test.janet` — Phase 2-5 + Phase 0-1 tests (source output + compile-eval + macro + ns + defn tests)
|
||||
- `test/phase6-final.janet` — Phase 6 comprehensive compile-mode tests (47 assertions)
|
||||
|
||||
Run: `janet test/compiler-test.janet` or `janet test/phase6-final.janet` or `jpm test`
|
||||
|
||||
- Source output tests: `(assert (= "(expected)" (compile-str "(input)")) "label")`
|
||||
- Round-trip tests: `(assert (= val (compile-eval-str "(input)")) "label")`
|
||||
- Compile flag tests: `(eval-string ctx "(input)")` with `{:compile? true}`
|
||||
- `test/compiler-test.janet` — all compiler tests (Phases 0-3)
|
||||
- `test/phase6-final.janet` — 47 comprehensive compile-mode tests
|
||||
|
||||
Run: `janet test/compiler-test.janet` or `jpm test`
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue