aboutsummaryrefslogtreecommitdiff
path: root/src/compile.zig
diff options
context:
space:
mode:
authorMathias Magnusson <mathias@magnusson.space>2025-06-02 21:13:42 +0200
committerMathias Magnusson <mathias@magnusson.space>2025-06-02 21:13:42 +0200
commite18b172d3e4e31b4d50ca978a66187730f744a31 (patch)
tree61fe5c3d71214febc9f2649b530deb3f174597dd /src/compile.zig
parent1b43beffd49d1d3dd5679c54a84ecff304d53d84 (diff)
downloadhuginn-e18b172d3e4e31b4d50ca978a66187730f744a31.tar.gz
add read_int built in procedure
Diffstat (limited to 'src/compile.zig')
-rw-r--r--src/compile.zig21
1 files changed, 13 insertions, 8 deletions
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 => {