|
| 1 | +## AST Structure Decisions |
| 2 | + |
| 3 | +### AST vs CST (2026-02-01) |
| 4 | + |
| 5 | +**Decision:** Subscript produces AST (Abstract Syntax Tree), not CST (Concrete Syntax Tree). |
| 6 | + |
| 7 | +| Type | Purpose | Preserves | Use case | |
| 8 | +|------|---------|-----------|----------| |
| 9 | +| CST | Source-faithful | All tokens, whitespace | Formatters, refactoring | |
| 10 | +| AST | Semantic structure | Meaning only | Compilers, evaluators | |
| 11 | + |
| 12 | +**Implications:** |
| 13 | +- Delimiters like `()` `{}` stripped when purely syntactic |
| 14 | +- Keywords preserved when they carry meaning (`catch`, `finally`) |
| 15 | +- Normalization happens in parser, not compiler |
| 16 | +- Lisp tradition: `(if cond then else)` not `(if cond then (else body))` |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | +### Delimiter Handling (2026-02-01) |
| 21 | + |
| 22 | +**Principle:** Strip delimiters when purely syntactic, keep when semantic. |
| 23 | + |
| 24 | +| Construct | Parens meaning | Result | |
| 25 | +|-----------|---------------|--------| |
| 26 | +| `if (cond)` | Required syntax | Strip → `['if', cond, ...]` | |
| 27 | +| `while (cond)` | Required syntax | Strip → `['while', cond, ...]` | |
| 28 | +| `f(a, b)` | Call operator | Keep → `['()', 'f', ...]` | |
| 29 | +| `(a, b) => x` | Grouping | Keep → `['=>', ['()', ...], ...]` | |
| 30 | + |
| 31 | +**Why arrow keeps `['()']`:** Distinguishes `a => x` from `(a) => x`. Also, Python would need this to distinguish tuple `(a, b)` from grouping. |
| 32 | + |
| 33 | +**Core helper:** `parens()` in parse.js strips and returns content. Features use it when parens are syntactic. |
| 34 | + |
| 35 | +--- |
| 36 | + |
| 37 | +### Try/catch structure (2026-02-01) |
| 38 | + |
| 39 | +**Decision:** Keywords as operators: `['try', body, ['catch', param, handler]?, ['finally', cleanup]?]` |
| 40 | + |
| 41 | +**Rationale:** |
| 42 | +- `catch` and `finally` are distinct keywords deserving operator status |
| 43 | +- Clean optional structure - just omit the clause |
| 44 | +- No null padding needed |
| 45 | +- Dialect-friendly: Python `except`, Ruby `rescue` become their own operators |
| 46 | + |
| 47 | +**Examples:** |
| 48 | +``` |
| 49 | +try { a } catch (e) { b } → ['try', 'a', ['catch', 'e', 'b']] |
| 50 | +try { a } finally { c } → ['try', 'a', ['finally', 'c']] |
| 51 | +try { a } catch (e) { b } finally { c } → ['try', 'a', ['catch', 'e', 'b'], ['finally', 'c']] |
| 52 | +``` |
| 53 | + |
| 54 | +--- |
| 55 | + |
| 56 | +### If-else-if chains (2026-02-01) |
| 57 | + |
| 58 | + **Decision:** `['if', cond, then, ['if', cond2, then2, else]]` for C-family |
| 59 | + |
| 60 | + **Rejected alternatives:** |
| 61 | + 1. `['if', c, t, ['else', ['if', ...]]]` — `else` isn't an operator, just positional marker |
| 62 | + 2. `['if', c1, t1, c2, t2, else]` — flat chain loses structure, hard to delimit |
| 63 | + |
| 64 | + **Rationale:** |
| 65 | + - JS/C have no `else if` token — else branch contains if statement |
| 66 | + - Position 4 = else branch, which can be any expression including another if |
| 67 | + - Minimal structure: no invented wrappers |
| 68 | + - Compiles trivially: `c(ctx) ? t(ctx) : e?.(ctx)` |
| 69 | + |
| 70 | + **Dialect handling:** |
| 71 | + - Python `elif` IS a keyword → `['if', c, t, ['elif', c2, t2, ['else', e]]]` |
| 72 | + - Ruby `elsif` IS a keyword → `['if', c, t, ['elsif', c2, t2, ['else', e]]]` |
| 73 | + - The operator name reflects the source token |
| 74 | + |
| 75 | + **Principle:** Use C tokens as universal semantic structure. Dialects that have distinct keywords (elif, elsif) preserve them. Dialects that don't (JS else-if) don't invent them. |
| 76 | + |
| 77 | + ### Try-catch structure (2026-02-01) |
| 78 | + |
| 79 | + **Decision:** Flat `['try', body, param, catch, finally?]` |
| 80 | + |
| 81 | + **Rejected:** Wrapped `['finally', ['catch', ['try', body], ...], ...]` |
| 82 | + |
| 83 | + **Rationale:** |
| 84 | + - Consistent with if: flat positional args |
| 85 | + - One node per statement, not nested operators |
| 86 | + - `null` for missing parts: `['try', body, null, null, finally]` |
| 87 | + |
| 88 | + **Dialect handling:** |
| 89 | + - Python: `['try', body, 'e', except, ['else', e], finally]` (try-except has else!) |
| 90 | + - Ruby: `['begin', body, 'e', rescue, ensure]` |
| 91 | + |
| 92 | +## Design Principles |
| 93 | + |
| 94 | + ### Token Customization |
| 95 | + |
| 96 | + Just as precedence is customizable via `prec()`, tokens should be customizable per dialect. |
| 97 | + |
| 98 | + Current: Features hardcode keywords (`keyword('try')`) |
| 99 | + |
| 100 | + Future possibility: |
| 101 | + ```js |
| 102 | + // Feature exports factory |
| 103 | + export default (tokens = {try:'try', catch:'catch', finally:'finally'}) => { |
| 104 | + keyword(tokens.try, ...) |
| 105 | + } |
| 106 | + ``` |
| 107 | + |
| 108 | + This allows: |
| 109 | + - **subscript.js**: default C/JS tokens |
| 110 | + - **worm.js** (Python): `{try:'try', catch:'except', finally:'finally', throw:'raise'}` |
| 111 | + - **subruby.js**: `{try:'begin', catch:'rescue', finally:'ensure', throw:'raise'}` |
| 112 | + |
| 113 | + ### C as Universal Foundation |
| 114 | + |
| 115 | + C-like tokens serve as **canonical semantic representation**, not because C is superior, but because: |
| 116 | + 1. Widely understood baseline |
| 117 | + 2. Minimal syntax (no significant whitespace) |
| 118 | + 3. Most languages have C-family equivalents |
| 119 | + |
| 120 | + Other dialects parse TO this structure (with their tokens), enabling: |
| 121 | + - Cross-compilation: Python AST → JS output |
| 122 | + - Universal tooling: one AST walker works for all |
| 123 | + - Round-trip within dialect: preserves source tokens |
0 commit comments