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:
@@ -438,6 +438,11 @@ fn cgmatch(c: *cgen, n: *node) void = {
|
||||
};
|
||||
};
|
||||
let endl: str = mklabel(c, "match_end");
|
||||
// Push end label as the yield target for this match's arm bodies.
|
||||
if (c.yieldtop < LOOP_MAX) {
|
||||
c.yieldbuf[c.yieldtop] = endl;
|
||||
c.yieldtop += 1;
|
||||
};
|
||||
let cs: *node = n.list;
|
||||
for (cs != nil) {
|
||||
let nxt: str = mklabel(c, "match_next");
|
||||
@@ -553,6 +558,7 @@ fn cgmatch(c: *cgen, n: *node) void = {
|
||||
cs = cs.next;
|
||||
};
|
||||
emitlabel(endl);
|
||||
if (c.yieldtop > 0) { c.yieldtop -= 1; };
|
||||
return;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user