diff options
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; |