feat: code generation for simple choice statements, testing machinery

This commit is contained in:
Brett Broadhurst 2026-03-16 17:33:31 -06:00
parent fac5a968e3
commit ee26be6254
Failed to generate hash of commit
13 changed files with 304 additions and 70 deletions

View file

@ -75,6 +75,8 @@ pub const Inst = struct {
switch_br,
content_push,
content_flush,
choice_br,
implicit_ret,
};
pub const Data = union {
@ -138,6 +140,17 @@ pub const Inst = struct {
body_len: u32,
};
};
pub const ChoiceBr = struct {
cases_len: u32,
pub const Case = struct {
operand_1: Ref,
operand_2: Ref,
operand_3: Ref,
body_len: u32,
};
};
};
pub const Global = struct {
@ -326,6 +339,35 @@ const Render = struct {
try io_w.writeAll(")");
}
fn renderChoiceBr(r: *Render, ir: Ir, inst: Inst) Error!void {
const io_w = r.writer;
const data = inst.data.payload;
const choice_extra = ir.extraData(Inst.ChoiceBr, data.payload_index);
const options_slice = ir.bodySlice(choice_extra.end, choice_extra.data.cases_len);
try io_w.print("{s}(\n", .{@tagName(inst.tag)});
for (options_slice) |option_index| {
const case_extra = ir.extraData(Inst.ChoiceBr.Case, @intFromEnum(option_index));
const body_slice = ir.bodySlice(case_extra.end, case_extra.data.body_len);
const old_len = try r.prefix.pushChildPrefix(r.gpa);
defer r.prefix.restore(old_len);
try r.prefix.writeIndent(io_w);
try renderInstRef(r, case_extra.data.operand_1);
try io_w.writeAll(", ");
try renderInstRef(r, case_extra.data.operand_2);
try io_w.writeAll(", ");
try renderInstRef(r, case_extra.data.operand_3);
try io_w.print(" = ", .{});
try renderBodyInner(r, ir, body_slice);
try io_w.writeAll(",\n");
}
try r.prefix.writeIndent(io_w);
try io_w.writeAll(")");
}
fn renderKnotDecl(r: *Render, ir: Ir, inst: Inst) Error!void {
const io_w = r.writer;
const extra = ir.extraData(Inst.Knot, inst.data.payload.payload_index);
@ -417,6 +459,8 @@ const Render = struct {
},
.content_push => try r.renderUnary(inst),
.content_flush => try r.renderUnary(inst),
.choice_br => try r.renderChoiceBr(ir, inst),
.implicit_ret => try r.renderUnary(inst),
}
try io_w.writeAll("\n");
}