vm: add negate opcode

This commit is contained in:
Luna 2019-06-01 15:27:19 -03:00
parent 61e463713c
commit 2822676707
3 changed files with 6 additions and 0 deletions

View file

@ -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;

View file

@ -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);

View file

@ -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;