diff --git a/src/chunk.zig b/src/chunk.zig index abc54af..76d4bb9 100644 --- a/src/chunk.zig +++ b/src/chunk.zig @@ -31,8 +31,6 @@ const AllOpcodes = struct { pub DefineGlobal: u8 = 17, pub DefineGlobalLong: u8 = 18, - pub GetGlobal: u8 = 19, - pub GetGlobalLong: u8 = 20, }; pub const OpCode = AllOpcodes{}; @@ -223,10 +221,6 @@ pub const Chunk = struct { return try simpleInstruction(stdout, "OP_DEFGLOBAL", index); } else if (instruction == OpCode.DefineGlobalLong) { return try simpleInstruction(stdout, "OP_DEFGLOBAL_LONG", index); - } else if (instruction == OpCode.GetGlobal) { - return try simpleInstruction(stdout, "OP_GETGLOBAL", index); - } else if (instruction == OpCode.GetGlobalLong) { - 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 de0f25a..1b44624 100644 --- a/src/compiler.zig +++ b/src/compiler.zig @@ -89,7 +89,7 @@ var rules = []ParseRule{ ParseRule{ .infix = Compiler.binary, .precedence = .Comparison }, ParseRule{ .infix = Compiler.binary, .precedence = .Comparison }, - ParseRule{ .prefix = Compiler.variable }, + ParseRule{}, ParseRule{ .prefix = Compiler.string }, ParseRule{ .prefix = Compiler.number }, ParseRule{ .precedence = .And }, @@ -252,19 +252,6 @@ pub const Compiler = struct { ))); } - fn namedVariable(self: *Compiler, tok: *Token) !void { - var idx = try self.identifierConstant(tok); - try self.emitConstWithIndex( - chunks.OpCode.GetGlobal, - chunks.OpCode.GetGlobalLong, - idx, - ); - } - - fn variable(self: *Compiler) !void { - try self.namedVariable(&self.parser.previous); - } - /// Emits bytecode for a given unary. fn unary(self: *Compiler) !void { var ttype = self.parser.previous.ttype; @@ -381,16 +368,11 @@ pub const Compiler = struct { return try self.identifierConstant(&self.parser.previous); } - fn emitConstWithIndex( - self: *Compiler, - op_short: u8, - op_long: u8, - idx: chunks.ConstantIndex, - ) !void { - switch (idx) { - .Small => |val| try self.emitBytes(op_short, val), + fn defineVariable(self: *Compiler, global: chunks.ConstantIndex) !void { + switch (global) { + .Small => |val| try self.emitBytes(chunks.OpCode.DefineGlobal, val), .Long => |val| blk: { - try self.emitByte(op_long); + try self.emitByte(chunks.OpCode.DefineGlobalLong); try self.emitByte(val[0]); try self.emitByte(val[1]); try self.emitByte(val[2]); @@ -399,14 +381,6 @@ pub const Compiler = struct { } } - fn defineVariable(self: *Compiler, global: chunks.ConstantIndex) !void { - try self.emitConstWithIndex( - chunks.OpCode.DefineGlobal, - chunks.OpCode.DefineGlobalLong, - global, - ); - } - fn varDecl(self: *Compiler) !void { var global = try self.parseVariable("Expect variable name."); diff --git a/src/main.zig b/src/main.zig index a38e2a3..8451723 100644 --- a/src/main.zig +++ b/src/main.zig @@ -16,17 +16,9 @@ fn run(allocator: *Allocator, data: []u8) !void { var stdout_file = try std.io.getStdOut(); const stdout = &stdout_file.outStream().stream; - var vmach = try vm.VM.init(allocator, stdout, true); + var vmach = try vm.VM.init(allocator, stdout, data, true); defer vmach.deinit(); - try vmach.interpret(data); -} - -fn runWithVM(vmach: *vm.VM, data: []u8) !void { - var stdout_file = try std.io.getStdOut(); - const stdout = &stdout_file.outStream().stream; - - defer vmach.deinit(); - try vmach.interpret(data); + try vmach.interpret(); } pub fn doError(line: usize, message: []const u8) !void { @@ -63,9 +55,6 @@ fn runPrompt(allocator: *Allocator) !void { var stdout_file = try std.io.getStdOut(); const stdout = &stdout_file.outStream().stream; - var vmach = try vm.VM.init(allocator, stdout, true); - defer vmach.deinit(); - while (true) { try stdout.print(">"); var buffer = try std.Buffer.init(allocator, ""[0..]); @@ -76,7 +65,7 @@ fn runPrompt(allocator: *Allocator) !void { return err; }; - runWithVM(&vmach, line) catch |err| { + run(allocator, line) catch |err| { switch (err) { InterpretResult.Ok => {}, InterpretResult.CompileError, InterpretResult.RuntimeError => blk: { diff --git a/src/vm.zig b/src/vm.zig index 24a72f8..0b6e3fb 100644 --- a/src/vm.zig +++ b/src/vm.zig @@ -40,6 +40,7 @@ pub const ValueMap = std.AutoHashMap([]const u8, values.Value); pub const VM = struct { chk: *Chunk = undefined, + src: []const u8, ip: usize = 0, stack: []Value, @@ -59,9 +60,12 @@ pub const VM = struct { pub fn init( allocator: *std.mem.Allocator, stdout: StdOut, + source: []const u8, debug_flag: bool, ) !VM { var self = VM{ + .src = source, + .stack = try allocator.alloc(Value, 256), .stdout = stdout, .debug_flag = debug_flag, @@ -102,7 +106,6 @@ pub const VM = struct { } pub fn deinit(self: *VM) void { - self.globals.deinit(); self.deinitObjects(); } @@ -226,25 +229,6 @@ pub const VM = struct { _ = self.pop(); } - fn readString(self: *VM) []u8 { - return self.readConst().as.Object.value.String; - } - - fn readStringLong(self: *VM) []u8 { - return self.readConstLong().as.Object.value.String; - } - - fn doGetGlobal(self: *VM, name: []u8) !void { - var kv_opt = self.globals.get(name); - - if (kv_opt) |kv| { - try self.push(kv.value); - } else { - self.runtimeError("Undefined variable '{}'.", name); - return InterpretResult.RuntimeError; - } - } - fn run(self: *VM) !void { while (true) { if (self.debug_flag) { @@ -286,24 +270,15 @@ pub const VM = struct { break :blk; }, - chunk.OpCode.GetGlobal => blk: { - try self.doGetGlobal(self.readString()); - break :blk; - }, - chunk.OpCode.GetGlobalLong => blk: { - try self.doGetGlobal(self.readStringLong()); - break :blk; - }, - // extracting the name is different depending of the // op code since one just uses a single byte, the other // uses three bytes since its a u24. chunk.OpCode.DefineGlobal => blk: { - try self.defGlobal(self.readString()); + try self.defGlobal(self.readConst().as.Object.value.String); break :blk; }, chunk.OpCode.DefineGlobalLong => blk: { - try self.defGlobal(self.readStringLong()); + try self.defGlobal(self.readConstLong().as.Object.value.String); break :blk; }, @@ -348,7 +323,7 @@ pub const VM = struct { } } - pub fn interpret(self: *VM, src: []const u8) !void { + pub fn interpret(self: *VM) !void { //self.ip = 0; //self.debug("VM start\n"); //var res = try self.run(); @@ -360,7 +335,7 @@ pub const VM = struct { self.allocator, &chk, self.stdout, - src, + self.src, self.debug_flag, self, );