diff --git a/src/chunk.zig b/src/chunk.zig index 491d52b..db17a34 100644 --- a/src/chunk.zig +++ b/src/chunk.zig @@ -8,6 +8,7 @@ const AllOpcodes = struct { pub Constant: u8 = 0, pub ConstantLong: u8 = 1, pub Return: u8 = 2, + pub Negate: u8 = 3, }; pub const OpCode = AllOpcodes{}; @@ -148,6 +149,8 @@ pub const Chunk = struct { self, index, ); + } else if (instruction == OpCode.Negate) { + return try simpleInstruction(stdout, "OP_NEGATE", index); } else { try stdout.print("Unknown opcode: {}\n", instruction); return index + 1; diff --git a/src/main.zig b/src/main.zig index 1d4bdc7..d493e1a 100644 --- a/src/main.zig +++ b/src/main.zig @@ -117,6 +117,7 @@ pub fn main() !void { //try chk.write(chunk.OpCode.Return); var constant = try chk.writeConstant(1.2, 123); + try chk.write(chunk.OpCode.Negate, 123); try chk.write(chunk.OpCode.Return, 123); var vmach = vm.VM.init(stdout, &chk, true); diff --git a/src/vm.zig b/src/vm.zig index b4243b8..5d74853 100644 --- a/src/vm.zig +++ b/src/vm.zig @@ -107,6 +107,8 @@ pub const VM = struct { try self.stdout.print("\n"); return InterpretResult.Ok; }, + + chunk.OpCode.Negate => self.push(-self.pop()), else => blk: { std.debug.warn("Unknown instruction: {x}\n", instruction); return InterpretResult.RuntimeError;