feat: ir for declarations and semantic analyzer start

This commit is contained in:
Brett Broadhurst 2026-03-09 05:57:25 -06:00
parent f16162b5bb
commit 197a37ebe7
Failed to generate hash of commit
4 changed files with 453 additions and 145 deletions

View file

@ -18,7 +18,10 @@ pub const Inst = struct {
pub const Tag = enum {
file,
declaration,
decl_knot,
decl_var,
decl_ref,
block,
add,
sub,
@ -57,8 +60,16 @@ pub const Inst = struct {
},
};
pub const Declaration = struct {
name: NullTerminatedString,
value: Inst.Index,
};
pub const Knot = struct {
name_ref: NullTerminatedString,
body_len: u32,
};
pub const Var = struct {
body_len: u32,
};
@ -151,12 +162,22 @@ const Render = struct {
try io_w.writeAll(")");
}
fn renderKnotInst(r: *Render, ir: Ir, inst: Inst) Error!void {
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);
const body_slice = ir.bodySlice(extra.end, extra.data.body_len);
const knot_ident_str = ir.nullTerminatedString(extra.data.name_ref);
try io_w.print("{s}(name=\"{s}\",body=", .{ @tagName(inst.tag), knot_ident_str });
try io_w.print("{s}(body=", .{@tagName(inst.tag)});
try renderBodyInner(r, ir, body_slice);
try io_w.writeAll(")");
}
fn renderVarDecl(r: *Render, ir: Ir, inst: Inst) Error!void {
const io_w = r.writer;
const extra = ir.extraData(Inst.Knot, inst.data.payload.payload_index);
const body_slice = ir.bodySlice(extra.end, extra.data.body_len);
try io_w.print("{s}(body=", .{@tagName(inst.tag)});
try renderBodyInner(r, ir, body_slice);
try io_w.writeAll(")");
}
@ -171,6 +192,21 @@ const Render = struct {
return io_w.writeAll(")");
}
fn renderDeclaration(r: *Render, ir: Ir, inst: Inst) Error!void {
const io_w = r.writer;
const extra = ir.extraData(Inst.Declaration, inst.data.payload.payload_index);
const ident_str = ir.nullTerminatedString(extra.data.name);
try io_w.print("{s}(name=\"{s}\", value={{\n", .{ @tagName(inst.tag), ident_str });
{
const old_len = try r.prefix.pushChildPrefix(r.gpa);
defer r.prefix.restore(old_len);
try renderInst(r, ir, extra.data.value);
}
try r.prefix.writeIndent(io_w);
return io_w.writeAll(")");
}
fn renderInst(r: *Render, ir: Ir, index: Inst.Index) Error!void {
const io_w = r.writer;
const inst_index: u32 = @intFromEnum(index);
@ -180,7 +216,13 @@ const Render = struct {
try io_w.print("%{d} = ", .{inst_index});
switch (inst.tag) {
.file => try r.renderFileInst(ir, inst),
.decl_knot => try r.renderKnotInst(ir, inst),
.declaration => try r.renderDeclaration(ir, inst),
.decl_knot => try r.renderKnotDecl(ir, inst),
.decl_var => try r.renderVarDecl(ir, inst),
.decl_ref => {
const str_bytes = inst.data.string.get(ir);
try io_w.print("{s}(\"{s}\")", .{ @tagName(inst.tag), str_bytes });
},
.block => try r.renderBlockInst(ir, inst),
.add => try r.renderBinaryInst(inst),
.sub => try r.renderBinaryInst(inst),