From e18b172d3e4e31b4d50ca978a66187730f744a31 Mon Sep 17 00:00:00 2001 From: Mathias Magnusson Date: Mon, 2 Jun 2025 21:13:42 +0200 Subject: add read_int built in procedure --- src/compile.zig | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'src/compile.zig') diff --git a/src/compile.zig b/src/compile.zig index acae7ff..c837495 100644 --- a/src/compile.zig +++ b/src/compile.zig @@ -14,7 +14,7 @@ pub const Instr = struct { pub const Type = union(enum) { constant: Constant, bin_op: BinOp, - print: Print, + proc_call: ProcCall, discard: Discard, }; @@ -43,11 +43,17 @@ pub const Instr = struct { } }; - pub const Print = struct { + pub const ProcCall = struct { dest: VReg, arg: VReg, + proc: Proc, - pub fn sources(self: Print) Sources { + const Proc = enum { + print, + read_int, + }; + + pub fn sources(self: ProcCall) Sources { return Sources.fromSlice(&.{self.arg}) catch unreachable; } }; @@ -68,7 +74,7 @@ pub const Instr = struct { pub fn dest(self: *const Instr) ?VReg { return switch (self.type) { - inline .constant, .bin_op, .print => |s| s.dest, + inline .constant, .bin_op, .proc_call => |s| s.dest, // inline .x => null, }; } @@ -167,14 +173,13 @@ const CompileContext = struct { }); }, .call => |call| { - if (call.proc.type != .identifier or - !std.mem.eql(u8, call.proc.loc.getIdent(self.source), "print")) - return error.CantCallAnythingButPrint; + if (call.proc.type != .identifier) return error.CanOnlyCallIdentifiers; + const proc = std.meta.stringToEnum(Instr.ProcCall.Proc, call.proc.loc.getIdent(self.source)) orelse return error.UnknownProcedure; const arg = try self.compileExpr(call.arg); try self.addInstr(.{ .loc = expr.loc, - .type = .{ .print = .{ .dest = dest, .arg = arg } }, + .type = .{ .proc_call = .{ .dest = dest, .arg = arg, .proc = proc } }, }); }, .identifier => { -- cgit v1.2.3