diff --git a/src/chunk.zig b/src/chunk.zig index f4d295f..289ef33 100644 --- a/src/chunk.zig +++ b/src/chunk.zig @@ -27,6 +27,7 @@ const AllOpcodes = struct { pub Less: u8 = 14, pub Print: u8 = 15, + pub Pop: u8 = 16, }; pub const OpCode = AllOpcodes{}; @@ -193,6 +194,8 @@ pub const Chunk = struct { return try simpleInstruction(stdout, "OP_LESS", index); } else if (instruction == OpCode.Print) { return try simpleInstruction(stdout, "OP_PRINT", index); + } else if (instruction == OpCode.Pop) { + return try simpleInstruction(stdout, "OP_POP", index); } else { try stdout.print("Unknown opcode: {}\n", instruction); return index + 1; diff --git a/src/compiler.zig b/src/compiler.zig index eacbdd9..c6bdd5b 100644 --- a/src/compiler.zig +++ b/src/compiler.zig @@ -331,6 +331,12 @@ pub const Compiler = struct { try self.emitByte(OpCode.Print); } + fn exprStmt(self: *Compiler) !void { + try self.expression(); + try self.consume(.SEMICOLON, "Expect ';' after expression."); + try self.emitByte(OpCode.Pop); + } + fn declaration(self: *Compiler) !void { try self.statement(); } @@ -338,6 +344,8 @@ pub const Compiler = struct { fn statement(self: *Compiler) !void { if (try self.match(.PRINT)) { try self.printStmt(); + } else { + try self.exprStmt(); } } diff --git a/src/vm.zig b/src/vm.zig index c3514f5..6ffc733 100644 --- a/src/vm.zig +++ b/src/vm.zig @@ -255,6 +255,8 @@ pub const VM = struct { chunk.OpCode.True => try self.push(values.BoolVal(true)), chunk.OpCode.False => try self.push(values.BoolVal(false)), + chunk.OpCode.Pop => self.pop(), + chunk.OpCode.Equal => blk: { var a = self.pop(); var b = self.pop();