diff options
author | Mathias Magnusson <mathias@magnusson.space> | 2025-06-01 02:32:13 +0200 |
---|---|---|
committer | Mathias Magnusson <mathias@magnusson.space> | 2025-06-01 02:32:13 +0200 |
commit | 546bb5cb935832791a280d1561620cf7a7a64842 (patch) | |
tree | 8c9439fdc4728a93380f93914605b890b8e4806a /src/compile.zig | |
parent | a49805b7a8a9fc05df34f14cb8b6892c723e2a61 (diff) | |
download | huginn-546bb5cb935832791a280d1561620cf7a7a64842.tar.gz |
add identifiers, procedure calls and a built in print procedure
Diffstat (limited to 'src/compile.zig')
-rw-r--r-- | src/compile.zig | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/src/compile.zig b/src/compile.zig index 2bf404d..9d70a58 100644 --- a/src/compile.zig +++ b/src/compile.zig @@ -14,6 +14,7 @@ pub const Instr = struct { pub const Type = union(enum) { constant: Constant, bin_op: BinOp, + print: Print, }; pub const Constant = struct { @@ -41,15 +42,24 @@ pub const Instr = struct { } }; + pub const Print = struct { + arg: VReg, + + pub fn sources(self: Print) Sources { + return Sources.fromSlice(&.{self.arg}) catch unreachable; + } + }; + pub fn sources(self: Instr) Sources { return switch (self.type) { inline else => |instr| instr.sources(), }; } - pub fn dest(self: *const Instr) VReg { + pub fn dest(self: *const Instr) ?VReg { return switch (self.type) { inline .constant, .bin_op => |s| s.dest, + inline .print => null, }; } @@ -123,6 +133,21 @@ const CompileContext = struct { }, }); }, + .call => |call| { + if (call.proc.type == .identifier and + std.mem.eql(u8, call.proc.loc.getIdent(self.source), "print")) + { + const arg = try self.compileExpr(call.arg); + try self.addInstr(.{ + .loc = expr.loc, + .type = .{ .print = .{ .arg = arg } }, + }); + // BUG: we're returning a bogus virtual register that we didn't write to + } else { + return error.CantCallAnythingButPrint; + } + }, + .identifier => return error.CantCompileIdentifierExpr, .invalid => return error.CantCompileInvalidExpr, } return dest; |