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
@@ -31,8 +31,8 @@ effects [io.read, io.write]
|
||||
|---|---|
|
||||
| `u8 … u128`, `usize`, `i8 … i128`, `isize`, `bool` | no implicit conversion |
|
||||
| `[T; N]` | array; `[e; N]` repeats `e` |
|
||||
| `type P = struct { a: u8, b: u8 }` | `P { a: 1, b: 2 }`, `p.a` |
|
||||
| `type E = enum { A, B(u64), C(u8, u8) }` | `E.B(4)`; `match` arms `A`, `B(x)`, `C(x, y)`, `_` |
|
||||
| `struct P { a: u8, b: u8 }` | `P { a: 1, b: 2 }` (every field, any order), `p.a` |
|
||||
| `enum E { A, B(u64), C(u8, u8) }` | `E::A`, `E::B(4)`; `match` with one arm per variant: `E::A => …`, `E::B(x) => …`, `E::C(x, y) => …` (no `_`) |
|
||||
| `ref<T>`, `mut<T>`, `slice<T>`, `slice_mut<T>` | borrows (§4) |
|
||||
| `own<T>`, `own_array<T>`, `region` | heap and regions (§6) |
|
||||
| `cap<K>` | authority; kinds below |
|
||||
@@ -54,6 +54,11 @@ for i in 0..n { … } // 0 ≤ i < n
|
||||
loop { … break; … continue; … }
|
||||
return v; trap; assert(c);
|
||||
let y = if c { 1 } else { 2 }; // value-producing if/match: tail expression without `;`
|
||||
let w: u64 = match m { // enum match: exhaustive, one arm per variant
|
||||
Message::Stop => 0,
|
||||
Message::Value(x) => x, // x exists only in this arm
|
||||
};
|
||||
match d { 0 => { … } 1 => { … } _ => { … } } // integer match: `_` required
|
||||
```
|
||||
|
||||
| Operation | Meaning |
|
||||
@@ -197,7 +202,7 @@ scope s { // every task spawned in s must
|
||||
| E7001 | syntax | check `;`, braces, `=>` in `match` |
|
||||
| E7002 | unknown name | spelling; declare the variable or function |
|
||||
| E7003 | linear value consumed without `move` | write `move x` (or borrow it with `ref`/`mut` if it must stay) |
|
||||
| E7004 | `match` not exhaustive | add the missing outcome/variant arm or `_` |
|
||||
| E7004 | `match` not exhaustive | add the arms listed in `missing` (integers: add `_`) |
|
||||
| E7005, E1001 | type mismatch | convert with `as`, `as!`, `as%`; check borrow vs value |
|
||||
| E7007 | ambiguous literal | add a suffix (`0u64`) or a type annotation |
|
||||
| E2101 | conflicting borrows | end the first borrow earlier with an inner `{ }` block |
|
||||
|
||||
@@ -39,7 +39,7 @@ Lowering layers (P6A-3/4), each with accepted (golden AIR), rejected (`E7xxx`) a
|
||||
| Layer | Content | Status |
|
||||
|---|---|---|
|
||||
| 1 | scalars, expressions, functions, control flow, variables carried across joins, short-circuit `and`/`or`, casts | done |
|
||||
| 2 | structs, enums, `match`, outcome matches, `or trap` | todo |
|
||||
| 2 | structs (named construction, declaration-order lowering), enums (`Enum::Variant`), exhaustive `match` over enums (no `_`) and integers (`_` required), `match` as an expression; outcome matches and `or trap` move to layer 5 with the fallible operations | done |
|
||||
| 3 | arrays, slices, byte strings | todo |
|
||||
| 4 | lexical borrows, frame storage, `move`, places | todo |
|
||||
| 5 | `own`, regions, resources, unsafe | todo |
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
-------------------
|
||||
- `//` starts a comment that runs to the end of the line.
|
||||
- Whitespace and line breaks separate tokens and are otherwise insignificant.
|
||||
- Keywords: module type struct enum fn effects trusted from let var zero
|
||||
- Keywords: module struct enum fn effects trusted from let var zero
|
||||
if else while for in loop break continue return trap match select
|
||||
scope shield lock as cancelled ref mut move unchecked or and not
|
||||
true false recv send.
|
||||
@@ -16,10 +16,10 @@ module = "module" path { item } ;
|
||||
item = type_decl | fn_decl ;
|
||||
path = ident { "." ident } ;
|
||||
|
||||
type_decl = "type" ident "=" ( struct_body | enum_body ) ;
|
||||
struct_body = "struct" "{" [ field { "," field } [ "," ] ] "}" ;
|
||||
type_decl = struct_decl | enum_decl ;
|
||||
struct_decl = "struct" ident "{" [ field { "," field } [ "," ] ] "}" ;
|
||||
field = ident ":" type ;
|
||||
enum_body = "enum" "{" [ variant { "," variant } [ "," ] ] "}" ;
|
||||
enum_decl = "enum" ident "{" variant { "," variant } [ "," ] "}" ;
|
||||
variant = ident [ "(" type { "," type } ")" ] ;
|
||||
|
||||
fn_decl = "fn" ident "(" [ param { "," param } ] ")" "->" type [ "from" ident ]
|
||||
@@ -59,8 +59,9 @@ block_stmt = if_expr | match_expr | select_stmt | block
|
||||
if_expr = "if" expr block [ "else" ( if_expr | block ) ] ;
|
||||
match_expr = "match" expr "{" { arm } "}" ;
|
||||
arm = pattern "=>" ( block | expr "," ) ;
|
||||
pattern = "_" | int_lit
|
||||
| ident [ "(" ident { "," ident } ")" ] ; (* variant or outcome: ok(n), closed, Shape.Rect(w, h) is written Rect(w, h) *)
|
||||
pattern = "_" | int_lit (* integer matches only; `_` is required there *)
|
||||
| ident "::" ident [ "(" ident { "," ident } ")" ] (* enum variant: Shape::Rect(w, h); no `_` in enum matches *)
|
||||
| ident [ "(" ident { "," ident } ")" ] ; (* outcome of a fallible operation: ok(n), closed *)
|
||||
|
||||
select_stmt = "select" "{" { select_arm } "cancelled" [ "(" ident { "," ident } ")" ] "=>" ( block | expr "," ) "}" ;
|
||||
select_arm = "recv" "(" ident ")" "{" { arm } "}"
|
||||
@@ -83,6 +84,7 @@ postfix = primary { "." ident | "[" expr [ ".." expr ] "]" | call_args } ;
|
||||
call_args = [ "<" type ">" ] "(" [ expr { "," expr } ] ")" ;
|
||||
primary = int_lit | "true" | "false" | string
|
||||
| ident [ "{" field_init { "," field_init } [ "," ] "}" ] (* struct literal *)
|
||||
| ident "::" ident (* enum variant; applied by a call *)
|
||||
| "[" expr ";" int_lit "]"
|
||||
| "(" expr ")"
|
||||
| if_expr | match_expr | block ;
|
||||
|
||||
@@ -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