Phase 6A: .aji layer 2 (structs, enums, exhaustive match)
Owner rules: struct/enum declarations in Rust form; one construction form each (named struct literal, Enum::Variant); struct fields lowered in declaration order, so field order in the source does not change the AIR (golden test with two orders, one AIR); match over enums exhaustive with no '_' (E7004 lists missing variants, E7006 for '_'), integer match needs '_'; payload names scoped to their arm (enum.switch block parameters); match as an expression with a merge block; arm-level source map; no destructuring, guards or nested patterns. Type inference limits written into spec 15 (one function, monomorphic, no search, E7007). aji lower --stats: source/AIR size and expansion ratio. Tests: 4 run (one shared golden), 7 rejects incl. verifier-caught E1003.
This commit is contained in:
1 parent
ba3e9b3719
commit
0fdd04179b
22 files changed
+702
-121
No files matched your search
@@ -39,8 +39,16 @@ Rules:
|
||||
```aji
|
||||
module demo.echo
|
||||
|
||||
type Pair = struct { a: u8, b: u8 }
|
||||
type Shape = enum { Empty, Square(u64), Rect(u64, u64) }
|
||||
struct Pair {
|
||||
a: u8,
|
||||
b: u8,
|
||||
}
|
||||
|
||||
enum Shape {
|
||||
Empty,
|
||||
Square(u64),
|
||||
Rect(u64, u64),
|
||||
}
|
||||
|
||||
fn main(input: cap<io.read>, out: cap<io.write>) -> void
|
||||
effects [io.read, io.write]
|
||||
@@ -93,6 +101,13 @@ Statements end with `;`. Block constructs (`if`, `while`, `for`, `loop`, `match`
|
||||
|
||||
`if` and `match` are expressions when every branch yields a value of the same type through a tail expression (an expression without `;` at the end of a block) or diverges (`return`, `break`, `continue`, `trap`).
|
||||
|
||||
**Type inference is deliberately small.** It works within one function, it is monomorphic, and it does no overload resolution or search among several admissible types. An expression's type comes from:
|
||||
|
||||
- its own form: a literal suffix, a name, a call's declared return type, a cast, a struct or enum constructor;
|
||||
- otherwise its context: an annotation, the other operand, the parameter type of the function called, the function's return type.
|
||||
|
||||
An untyped `let x = <literal>` takes the type its first later use in the same function determines in that way. If nothing determines a type, the result is `E7007`: the frontend never picks a default.
|
||||
|
||||
Lowering: structured control flow becomes blocks and terminators. A variable assigned on several paths becomes a block parameter at the join.
|
||||
|
||||
## 5. Expressions
|
||||
@@ -122,7 +137,14 @@ Lowering: structured control flow becomes blocks and terminators. A variable ass
|
||||
- `true`, `false`;
|
||||
- `b"…"`: a byte string, of type `[u8; N]`, with escapes `\n`, `\t`, `\\`, `\"`, `\xHH`.
|
||||
|
||||
**Aggregates.** `Pair { a: 1, b: 2 }` (every field, in any order); `Shape.Square(4)`; `[e; N]` (`array.splat`). `v.f` on a struct value is `struct.get`.
|
||||
**Aggregates.**
|
||||
|
||||
- **Struct values.** `Pair { a: 1, b: 2 }` is the only way to build one: every field exactly once, in any order. A missing or repeated field is `E7006`, an unknown one `E7002`. The initializers are evaluated, and lowered, in **declaration order**, so the AIR does not depend on the order the fields are written in. There is no positional constructor.
|
||||
- **Enum values.** `Shape::Square(4)`, or `Shape::Empty` for a variant without payload, is the only form.
|
||||
- **Arrays.** `[e; N]` is `array.splat`.
|
||||
- **Field access.** `v.f` on a struct value is `struct.get`.
|
||||
|
||||
6A has no destructuring: no `let Point { x, y } = p`, no nested patterns, guards or or-patterns.
|
||||
|
||||
**Calls.** `f(args)` calls a function of the module. Built-in operations are called by their AIR names (§7, §8). Arguments are evaluated left to right.
|
||||
|
||||
@@ -176,7 +198,11 @@ match io.stdin_read(input, mut buf) {
|
||||
|
||||
- **Rebinding.** A value that comes back on an edge is rebound by the name in its arm: `closed(v)`, `pending(rx)`, `cancelled(t)`.
|
||||
- **`or trap`.** `op(args) or trap` is the expression of the `ok` value, or of `void`. Every other outcome traps (`T0001`). It is the only shorthand.
|
||||
- **Enums.** A `match` over a value of an enum type arms its variants, with `_` as default. A `match` over an integer arms literals, with `_` required.
|
||||
- **Enums.** A `match` over a value of an enum type has exactly one arm per variant, each written `Enum::Variant(names…)` (`Enum::Variant` without payload). It must be exhaustive (`E7004` lists the missing variants), and `_` is not allowed (`E7006`), so a new variant cannot slip through silently. Payload names are bound only inside their arm (they are the arm block's parameters in AIR). It lowers to `enum.switch`.
|
||||
- **Integers.** A `match` over an integer has literal arms and exactly one `_` arm, which is required (`E7004`). It lowers to `switch`.
|
||||
- **Booleans** use `if`.
|
||||
- **As expressions.** `match` and `if` are expressions when their arms yield values (§4). Every arm must have the type of the others, or diverge.
|
||||
- **Source map.** The switch maps to the `match`; each arm's code and its branch to the join map to that arm.
|
||||
|
||||
## 7. Tasks, cancellation, channels, select and mutex
|
||||
|
||||
@@ -290,6 +316,7 @@ A byte string or array used where a `slice<u8>` is expected is materialized in f
|
||||
| `aji check file.aji` | parse, lower, verify |
|
||||
| `aji run file.aji` | the same, then run (same flags and run record as for `.air`) |
|
||||
| `aji lower file.aji` | print the canonical AIR |
|
||||
| `aji lower --stats file.aji` | source and AIR size and the expansion ratio (bytes, non-empty lines): how much mechanical work the frontend took over |
|
||||
| `aji lower --annotate file.aji` | the same with `; line:col-line:col` source spans before each instruction (not canonical; for debugging) |
|
||||
|
||||
Lowering is deterministic: the same source and compiler give byte-identical AIR, and the lowered AIR is canonical (parse → print reproduces it). The conformance runner checks both for every accepted `.aji` test.
|
||||
Reference in new issue
Block a user