Phase 1+2: Clojure→Janet source compiler with symbol classification

- analyze-form: Clojure forms → annotated AST nodes (:op keys)
- emit-ast: AST dispatch → Janet source via StringBuffer
- compile-form: analyze → emit → Janet source string
- Symbol classification: locals, core refs, qualified refs
- Ops: const, do, if, def, fn, let, invoke, quote, vector, map
- Local binding awareness in fn* and let* (shadowing works)
- All 317 existing tests pass, 24 new compiler tests
This commit is contained in:
Yogthos 2026-06-02 14:59:13 -04:00
parent 679cc1d4ef
commit dfa98746ee
6 changed files with 594 additions and 22 deletions

View file

@ -1,34 +1,43 @@
{
"jolt-dev": {
"created_by": "agent",
"use_count": 24,
"view_count": 35,
"patch_count": 30,
"last_used_at": "2026-06-02T18:45:23.321430+00:00",
"last_viewed_at": "2026-06-02T18:45:23.310874+00:00",
"last_patched_at": "2026-06-02T17:27:24.746485+00:00",
"created_at": "2026-06-01T21:26:06.614465+00:00",
"state": "active",
"pinned": false
},
"jolt-bootstrap": {
"created_by": "agent",
"use_count": 8,
"view_count": 18,
"use_count": 9,
"view_count": 20,
"patch_count": 10,
"last_used_at": "2026-06-02T03:43:48.038435+00:00",
"last_viewed_at": "2026-06-02T03:43:48.028512+00:00",
"last_used_at": "2026-06-02T18:45:23.336653+00:00",
"last_viewed_at": "2026-06-02T18:45:23.328485+00:00",
"last_patched_at": "2026-06-02T03:44:45.935676+00:00",
"created_at": "2026-06-01T21:49:51.101718+00:00",
"state": "active",
"pinned": false
},
"jolt-dev": {
"jolt-compiler": {
"created_by": "agent",
"use_count": 20,
"view_count": 30,
"patch_count": 30,
"last_used_at": "2026-06-02T17:27:04.417433+00:00",
"last_viewed_at": "2026-06-02T17:27:04.404716+00:00",
"last_patched_at": "2026-06-02T17:27:24.746485+00:00",
"created_at": "2026-06-01T21:26:06.614465+00:00",
"use_count": 0,
"view_count": 0,
"patch_count": 0,
"created_at": "2026-06-02T17:54:38.690279+00:00",
"state": "active",
"pinned": false
},
"jpm-build": {
"created_by": "agent",
"use_count": 0,
"view_count": 10,
"view_count": 11,
"patch_count": 0,
"last_viewed_at": "2026-06-02T03:42:37.626504+00:00",
"last_viewed_at": "2026-06-02T17:43:38.327259+00:00",
"created_at": "2026-06-01T20:56:39.144222+00:00",
"state": "active",
"pinned": false

View file

@ -0,0 +1,82 @@
---
description: Jolt compiler architecture and implementation plan
---
# Jolt Compiler
Two-phase source-to-source compiler: Clojure forms → annotated AST → Janet source → Janet bytecode.
## Architecture
```
Clojure source → Reader → raw AST
analyze-form (classify symbols, produce :op AST)
emit* dispatch (generate Janet source string)
Janet compile → bytecode
```
Follows CLJS `cljs.analyzer` / `cljs.compiler` pattern.
## Key decisions
- **Target**: Janet source text (fed to Janet's `compile`), not direct bytecode. Simpler, debuggable, portable across Janet versions.
- **Mode gating**: Opt-in per context via `:compile?` flag on `init`. `eval-string` still interprets unless opted in.
- **Caching**: In-memory bytecode cache in context first. Disk persistence (`.jimage` files) as follow-up.
## AST ops (CLJS subset + Jolt-specific)
Core: `const`, `var`, `local`, `binding`, `if`, `do`, `let`, `loop`, `recur`, `fn`, `fn-method`, `def`, `invoke`, `quote`, `try`, `throw`, `set!`, `new`, `host-field`, `host-call`
Jolt-specific: `deftype`, `defmulti`, `defmethod`, `syntax-quote`
## File layout
| File | Purpose |
|------|---------|
| `src/jolt/compiler.janet` | `analyze-form`, `emit*` dispatch, `compile-form`, symbol classifier |
| `src/jolt/loader.janet` | `load-ns`, `reload-ns`, in-memory bytecode cache |
| `test/compiler-test.janet` | Round-trip: compile-form → Janet eval → assert |
Modified files:
- `evaluator.janet` — add compiler fast-path for `def`/`defn`/`defmacro` when `:compile?` set
- `types.janet` — add `:compiled-cache` table to context
- `api.janet` — expose `compile-string`, `load-ns`, `compile-file`; `init` gets `:compile?` flag
- `reader.janet` — **no change**
- `core.janet` — **no change**
## Emit target advantages
Both input and output are parenthesized prefix syntax, so many forms pass through almost unchanged:
```
Clojure: (defn f [x] (+ x 1))
AST: {:op :def :name "f" :init {:op :fn :methods [...]}}
Janet: (defn f [x] (+ x 1)) ← nearly identical
```
Main work:
- Symbol resolution (Clojure's `clojure.core/+` → Janet's `core-+`)
- Truthiness wrapping (`nil`/`false` are falsey in Clojure, Janet only `nil`)
- Special form mapping (`loop*`/`recur` → Janet `loop` + explicit recur vars)
- Vars → Janet table lookups
## Implementation phases
| Phase | What | Deliverable |
|-------|------|-------------|
| 1 | `compiler.janet``analyze-form` skeleton + `emit*` for `const`, `do`, `if`, `let`, `fn`, `def`, `invoke` | Basic forms compile and run |
| 2 | Symbol classifier — resolve locals/vars/core at analyze time | No runtime `resolve-sym` in compiled code |
| 3 | `loader.janet` + `api.janet` wiring — `:compile?` flag, `load-ns`, caching | File-based namespace loading works |
| 4 | Macro integration — expand at analyze time via interpreter | Macros work in compiled code |
| 5 | Remaining ops: `loop`/`recur`, `try`/`throw`, `quote`, `syntax-quote`, `set!`, `deftype`, `.` | Full language coverage |
| 6 | Tests + benchmarks | Correctness + speedup measurement |
## Pitfalls
- Janet `compile` produces bytecode tied to Janet version — source-to-source avoids this
- CLJS analyzer is ~5000 lines; Jolt's can be simpler because emit target is s-expressions
- Symbol resolution must happen at analyze time, not runtime, for compiled code
- Macro expansion still uses interpreter at analyze time — macros are not AOT-compiled