diff --git a/src/compiler.zig b/src/compiler.zig index 28e9f15..e029b9c 100644 --- a/src/compiler.zig +++ b/src/compiler.zig @@ -1,13 +1,15 @@ -const token = @import("token.zig"); -const scanner = @import("scanner.zig"); -const main = @import("main.zig"); +const std = @import("std"); +const scanner = @import("new_scanner.zig"); + +const Allocator = std.mem.Allocator; pub const Compiler = struct { - tokens: *scanner.TokenList, + src: []const u8, + allocator: *Allocator, - fn init(tokens: *scanner.TokenList) Compiler { - return Compiler{ .tokens = tokens }; + pub fn init(allocator: *Allocator, source: []const u8) Compiler { + return Compiler{ .src = source, .allocator = allocator }; } - fn advance(self: *Compiler) void {} + pub fn compile(self: *Compiler) void {} }; diff --git a/src/main.zig b/src/main.zig index b43afc1..e74c6f8 100644 --- a/src/main.zig +++ b/src/main.zig @@ -58,8 +58,9 @@ fn runFile(allocator: *Allocator, path: []const u8) !void { var slice = try allocator.alloc(u8, total_bytes); _ = try lox_file.read(slice); - try run(allocator, slice); - if (hadError) std.os.exit(65); + var res = try run(allocator, slice); + if (res == vm.InterpretResult.CompileError) std.os.exit(65); + if (res == vm.InterpretResult.RuntimeError) std.os.exit(70); } fn runPrompt(allocator: *Allocator) !void { @@ -123,6 +124,6 @@ pub fn main() !void { try chk.write(chunk.OpCode.Divide, 123); try chk.write(chunk.OpCode.Return, 123); - var vmach = try vm.VM.init(allocator, stdout, &chk, true); - _ = try vmach.interpret(); + var vmach = try vm.VM.init(allocator, stdout, "", true); + _ = vmach.interpret(); } diff --git a/src/vm.zig b/src/vm.zig index 467651a..d09511a 100644 --- a/src/vm.zig +++ b/src/vm.zig @@ -1,9 +1,11 @@ const std = @import("std"); const chunk = @import("chunk.zig"); const value = @import("value.zig"); +const compiler = @import("compiler.zig"); const Chunk = chunk.Chunk; const Value = value.Value; +const Compiler = compiler.Compiler; const StdOut = *std.io.OutStream(std.fs.File.WriteError); @@ -14,7 +16,8 @@ pub const InterpretResult = enum { }; pub const VM = struct { - chk: *Chunk, + chk: *Chunk = undefined, + src: []const u8, ip: usize = 0, stack: []Value, @@ -31,11 +34,11 @@ pub const VM = struct { pub fn init( allocator: *std.mem.Allocator, stdout: StdOut, - chk: *Chunk, + source: []const u8, debug_flag: bool, ) !VM { var self = VM{ - .chk = chk, + .src = source, .stack = try allocator.alloc(Value, 256), .stdout = stdout, @@ -156,13 +159,15 @@ pub const VM = struct { } } - pub fn interpret(self: *VM) !InterpretResult { - self.ip = 0; - - self.debug("VM start\n"); - var res = try self.run(); - self.debug("VM end\n"); - return res; + pub fn interpret(self: *VM) InterpretResult { + //self.ip = 0; + //self.debug("VM start\n"); + //var res = try self.run(); + //self.debug("VM end\n"); + //return res; + var cmpr = Compiler.init(self.allocator, self.src); + cmpr.compile(); + return InterpretResult.Ok; } pub fn push(self: *VM, val: Value) !void {