Phase 10: Standard Library — clojure.string, clojure.set, clojure.walk
- src/jolt/clojure/string.clj (123 lines, 20 functions): blank?, capitalize, lower-case, upper-case, includes?, join, replace, replace-first, str-reverse, split, starts-with?, ends-with?, trim, triml, trimr, trim-newline, escape, index-of, last-index-of - src/jolt/clojure/set.clj (124 lines, 10 operations): union, intersection, difference, select, project, rename, rename-keys, map-invert, join, index, subset?, superset? - src/jolt/clojure/walk.clj (77 lines, 9 functions): walk, postwalk, prewalk, postwalk-demo, prewalk-demo, postwalk-replace, prewalk-replace, keywordize-keys, stringify-keys, macroexpand-all - src/jolt/core.janet: 11 Janet string interop bindings (str-trim, str-upper, str-lower, str-find, str-replace, str-replace-all, str-reverse-b, str-join, str-split, str-triml, str-trimr) - test/phase10-test.janet: 2 test sections (40-41) 15+ assertions covering string and set functions - All .clj files use eval-form for multi-form loading - 315 ok, 2 fail (pre-existing, unchanged)
This commit is contained in:
parent
e63c2ce8d5
commit
fdb0f4ab83
10 changed files with 518 additions and 77 deletions
|
|
@ -1,4 +1,8 @@
|
|||
# jolt-dev
|
||||
---
|
||||
name: jolt-dev
|
||||
description: Jolt development workflow — build, test, special form patterns, Janet gotchas
|
||||
---
|
||||
|
||||
# jolt-dev
|
||||
|
||||
Jolt development workflow — build, test, special form patterns, Janet gotchas
|
||||
|
|
@ -20,14 +24,14 @@ To add a new special form to the evaluator:
|
|||
|
||||
1. Add the name to `special-symbol?` in `src/jolt/evaluator.janet`
|
||||
2. Add a match arm in `eval-list` (the match on `name`)
|
||||
3. Add tests in `test/evaluator-test.janet`
|
||||
3. Add tests
|
||||
|
||||
The match arm receives `ctx`, `bindings`, and `form` (the full list). Use `(in form 1)` for first arg, etc.
|
||||
|
||||
**Non-symbol heads** (keywords, etc.): `eval-list` first checks `(and (struct? first-form) (= :symbol (...)))` before extracting `name`. If not a symbol, falls through to default function application.
|
||||
|
||||
### Current special forms (29):
|
||||
`quote`, `syntax-quote`, `unquote`, `unquote-splicing`, `do`, `if`, `def`, `defmacro`, `fn*`, `let*`, `loop*`, `recur`, `throw`, `try`, `set!`, `var`, `locking`, `instance?`, `defmulti`, `defmethod`, `deftype`, `new`, `.`, `var-get`, `var-set`, `var?`, `alter-var-root`, `find-var`, `intern`, `alter-meta!`, `reset-meta!`, `disj`, `set?`
|
||||
### Current special forms (37):
|
||||
`quote`, `syntax-quote`, `unquote`, `unquote-splicing`, `do`, `if`, `def`, `defmacro`, `fn*`, `let*`, `loop*`, `recur`, `throw`, `try`, `set!`, `var`, `locking`, `instance?`, `defmulti`, `defmethod`, `deftype`, `new`, `.`, `var-get`, `var-set`, `var?`, `alter-var-root`, `find-var`, `intern`, `alter-meta!`, `reset-meta!`, `disj`, `set?`, `protocol-dispatch`, `register-method`, `make-reified`, `satisfies?`
|
||||
|
||||
## Compiler Architecture
|
||||
|
||||
|
|
@ -37,6 +41,40 @@ Two-phase: `analyze-form [form bindings ctx]` → `emit-ast` (string) or `emit-e
|
|||
|
||||
**eval-string dispatch** (compile mode): stateful forms → interpreter; everything else → `compile-and-eval`. Macros expand at analyze time.
|
||||
|
||||
## Protocol System
|
||||
|
||||
Protocols are maps with `:jolt/type :jolt/protocol` and `:methods` map.
|
||||
Type registry in context env (`:type-registry`) maps `type-tag → proto-name → method-name → fn`.
|
||||
|
||||
**Special forms:**
|
||||
- `protocol-dispatch [proto-sym method-sym obj rest-args]` — resolves method via type registry or reified methods
|
||||
- `register-method [type-sym proto-sym method-sym fn-form]` — stores impl in type registry
|
||||
- `make-reified [proto-sym methods-map]` — creates anonymous object with `:jolt/protocol-methods`
|
||||
|
||||
**Critical rule:** fn* form inside extend-type/extend-protocol MUST be `@[...]` (array) to trigger eval-list's special form dispatch. Tuples `[...]` hit `(tuple? form)` branch instead. Same for register-method, protocol-dispatch calls.
|
||||
|
||||
## REPL Collection Rendering (Buffer-Based)
|
||||
|
||||
Use `write-value` + `write-collection` with a StringBuffer (`@""`) rather than `prin`/`print` directly. Build the entire output string in a buffer, then atomically `(print (string buf))`. Prevents Janet's C runtime (in `jpm build` executables) from interleaving its native `<tuple 0x...>` printer between incremental `prin` calls.
|
||||
|
||||
```janet
|
||||
(var write-value nil) ; forward declaration
|
||||
|
||||
(defn- write-collection [v buf]
|
||||
(cond (tuple? v) (do (buffer/push-string buf "[") ...)
|
||||
(array? v) (do (buffer/push-string buf "(") ...) ...))
|
||||
|
||||
(set write-value (fn [v buf]
|
||||
(cond (nil? v) (buffer/push-string buf "nil")
|
||||
(number? v) (buffer/push-string buf (string v))
|
||||
(tuple? v) (write-collection v buf)
|
||||
true (buffer/push-string buf (string v))))) ; true REQUIRED
|
||||
|
||||
(defn print-value [v] (def buf @"") (write-value v buf) (print (string buf)))
|
||||
```
|
||||
|
||||
**Critical:** Janet's `cond` treats a bare expression in the last position as a **test** clause, not a catch-all body. Use `true` as the guard.
|
||||
|
||||
## PersistentHashMap Gotchas
|
||||
|
||||
- `core-map?`: `(if (and (table? x) (get x :jolt/deftype)) true false)` — `and` returns last truthy, not boolean
|
||||
|
|
@ -45,9 +83,9 @@ Two-phase: `analyze-form [form bindings ctx]` → `emit-ast` (string) or `emit-e
|
|||
|
||||
## defrecord / deftype Patterns
|
||||
|
||||
- defrecord emits `(deftype TypeName [fields])` + arrow factory `(fn fields-vec (TypeName. field1 field2...))`
|
||||
- defrecord emits `(deftype TypeName [fields])` + arrow factory
|
||||
- Records are tables with `:jolt/deftype` = type name string
|
||||
- `set!` field mutation: `(set! (.-x obj) val)` parses as array with `.-x` symbol head — check symbol name before dispatch
|
||||
- `set!` field mutation: `(set! (.-x obj) val)` parses as array with `.-x` symbol head
|
||||
|
||||
## Binding Macro
|
||||
|
||||
|
|
@ -55,22 +93,20 @@ Uses `array-map` (plain Janet struct) not `hash-map` (PHM) to avoid PHM get() in
|
|||
|
||||
## Tagged Literals (#inst, #uuid)
|
||||
|
||||
`:#inst` is invalid Janet keyword syntax (contains `#`). Use dynamic table construction:
|
||||
```janet
|
||||
(let [dr @{}] (put dr (keyword "#inst") (fn [s] s)) dr)
|
||||
```
|
||||
Use dynamic table construction: `(let [dr @{}] (put dr (keyword "#inst") fn) dr)`
|
||||
|
||||
## LazySeq Patterns
|
||||
|
||||
- Use `indexed?` not `tuple?` for realized sequences (may be arrays from `cons`/`concat`)
|
||||
- Avoid `val'` (apostrophe in symbol names) — causes Janet parse errors; use `vf` instead
|
||||
- `ls-first`/`ls-rest`/`ls-seq` all call `realize-ls` first (caches result, realizes once)
|
||||
- Avoid `val'` (apostrophe in symbol names) — use `vf` instead
|
||||
|
||||
## Janet Gotchas
|
||||
|
||||
- `def` creates constants; use `(var x nil)` for mutable locals
|
||||
- Bare tuples in `eval` are function calls: `[1 2 3]` tries to call `1`. Use `['tuple 1 2 3]`
|
||||
- Bare tuples in `eval` are function calls: `[1 2 3]` tries to call `1`
|
||||
- `try` format: `(try body ([err] handler))` NOT `(try body (catch sym handler))`
|
||||
- core-renames MUST match actual fn names: `"-"` → `"core-sub"` (not `"core--"`)
|
||||
- Janet `parse` vs `parser/new`: use `parser/new` + `parser/consume` + `parser/eof` + `parser/produce` for full source parsing
|
||||
- `(break val)` breaks from a while loop returning val — useful in bucket search patterns
|
||||
- `(break val)` breaks from loop returning val — useful in bucket search patterns
|
||||
- `boolean` doesn't exist — use `(if x true false)`
|
||||
- Janet doesn't support Clojure-style multi-arity defn — use `[& args]` with `case (length args)`
|
||||
- Janet's `cond` treats bare expression in last position as test, not catch-all — use `true` guard
|
||||
Loading…
Add table
Add a link
Reference in a new issue