wcc: match-as-expression with yield
`match (e) { ... }` can now sit in expression position, with each
arm using `yield expr;` to produce the match's value:
let v = match (r) {
case let n: i32 => yield n + 1;
case let s: str => yield s.len: i32 + 100;
};
TK_YIELD keyword + N_YIELD AST node, both appended at the tail of
their enums to keep prior numeric values byte-stable for the
wwdump-diff gates.
Checker: cexpr for N_MATCH walks each arm's body looking for the
first N_YIELD; the match's type is the unified yield type (or
ty_void if no yield, preserving the statement-form semantics).
Mismatched arm yields are flagged.
Cgen: a yield-target stack (separate from the loop break stack)
holds each enclosing match's end label. N_YIELD evaluates its
expression into AX (and BX for str) and JMPs to the topmost entry.
cgmatch pushes its end label on entry and pops on exit.
Selfhost mirror: lib/ww/lex/tok.ww kwtab+name, lib/ww/ast.ww
N_YIELD def+print, lib/ww/parse/stmt.ww yield-stmt; selfhost cgen
adds a yieldbuf to the cgen struct and a cgyield helper. Verified
end-to-end: a yield-using program compiled via the wwstage cgen
matches the C-cgen build's exit code.
This commit is contained in:
@@ -1121,6 +1121,13 @@ parsestmt(Parser *p)
|
||||
expect(p, TK_SEMI);
|
||||
return n;
|
||||
}
|
||||
case TK_YIELD: {
|
||||
advance(p);
|
||||
Node *n = newnode(p->a, N_YIELD, pp);
|
||||
n->lhs = parseexpr(p);
|
||||
expect(p, TK_SEMI);
|
||||
return n;
|
||||
}
|
||||
case TK_BREAK: {
|
||||
advance(p);
|
||||
Node *n = newnode(p->a, N_BREAK, pp);
|
||||
|
||||
Reference in New Issue
Block a user