diff --git a/src/chunk.zig b/src/chunk.zig index 7a89c3e..abc54af 100644 --- a/src/chunk.zig +++ b/src/chunk.zig @@ -134,7 +134,7 @@ pub const Chunk = struct { return self.constants.count - 1; } - pub fn writeConstantRaw( + pub fn writeConstant( self: *Chunk, val: value.Value, line: usize, @@ -143,7 +143,10 @@ pub const Chunk = struct { var constant_idx = self.constants.count - 1; if (constant_idx < 256) { + try self.write(OpCode.Constant, line); + var idx_small = @intCast(u8, constant_idx); + try self.write(idx_small, line); return ConstantIndex{ .Small = idx_small }; } else { var idx_u24: u24 = @intCast(u24, constant_idx); @@ -154,35 +157,14 @@ pub const Chunk = struct { const v2: u8 = @intCast(u8, (idx_u24 >> 8) & mask); const v3: u8 = @intCast(u8, (idx_u24 >> 16) & mask); + try self.write(OpCode.ConstantLong, line); + try self.write(v3, line); + try self.write(v2, line); + try self.write(v1, line); return ConstantIndex{ .Long = []u8{ v3, v2, v1 } }; } } - pub fn writeConstant( - self: *Chunk, - val: value.Value, - line: usize, - ) !ConstantIndex { - var idx = try self.writeConstantRaw(val, line); - - switch (idx) { - .Small => |idx_small| blk: { - try self.write(OpCode.Constant, line); - try self.write(idx_small, line); - break :blk; - }, - .Long => |long_u8| blk: { - try self.write(OpCode.ConstantLong, line); - try self.write(long_u8[0], line); - try self.write(long_u8[1], line); - try self.write(long_u8[2], line); - }, - else => unreachable, - } - - return idx; - } - pub fn disassembleInstruction( self: *Chunk, stdout: var, @@ -238,13 +220,13 @@ pub const Chunk = struct { } else if (instruction == OpCode.Pop) { return try simpleInstruction(stdout, "OP_POP", index); } else if (instruction == OpCode.DefineGlobal) { - return try constantInstruction(stdout, "OP_DEFGLOBAL", self, index); + return try simpleInstruction(stdout, "OP_DEFGLOBAL", index); } else if (instruction == OpCode.DefineGlobalLong) { - return try constantLongInstruction(stdout, "OP_DEFGLOBAL_LONG", self, index); + return try simpleInstruction(stdout, "OP_DEFGLOBAL_LONG", index); } else if (instruction == OpCode.GetGlobal) { - return try constantInstruction(stdout, "OP_GETGLOBAL", self, index); + return try simpleInstruction(stdout, "OP_GETGLOBAL", index); } else if (instruction == OpCode.GetGlobalLong) { - return try constantLongInstruction(stdout, "OP_GETGLOBAL_LONG", self, index); + return try simpleInstruction(stdout, "OP_GETGLOBAL_LONG", index); } else { try stdout.print("Unknown opcode: {}\n", instruction); return index + 1; diff --git a/src/compiler.zig b/src/compiler.zig index fb3ee39..de0f25a 100644 --- a/src/compiler.zig +++ b/src/compiler.zig @@ -253,13 +253,7 @@ pub const Compiler = struct { } fn namedVariable(self: *Compiler, tok: *Token) !void { - // writeConstant always writes OP_CODE which may be not - // what we want, so. - var idx = try self.currentChunk().writeConstantRaw(values.ObjVal(try objects.copyString( - self.vmach, - tok.lexeme, - )), tok.line); - + var idx = try self.identifierConstant(tok); try self.emitConstWithIndex( chunks.OpCode.GetGlobal, chunks.OpCode.GetGlobalLong, diff --git a/src/main.zig b/src/main.zig index 3983140..a38e2a3 100644 --- a/src/main.zig +++ b/src/main.zig @@ -85,8 +85,6 @@ fn runPrompt(allocator: *Allocator) !void { else => return err, } }; - - vmach.resetStack(); } }