From a9dca436bd81a660de4451ae4cbcb9086d72027f Mon Sep 17 00:00:00 2001 From: Luna Date: Sat, 1 Jun 2019 16:12:39 -0300 Subject: [PATCH 1/2] remove chunk running code, add draft compiler struct --- src/compiler.zig | 16 +++++++++------- src/main.zig | 9 +++++---- src/vm.zig | 25 +++++++++++++++---------- 3 files changed, 29 insertions(+), 21 deletions(-) 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 { From b80cd52c5002695d35349fa257b35851ef4ccbe9 Mon Sep 17 00:00:00 2001 From: Luna Date: Sat, 1 Jun 2019 16:17:28 -0300 Subject: [PATCH 2/2] main: readd runPrompt and runFile - main: make run() use the VM struct instance --- src/main.zig | 55 +++++++++++++--------------------------------------- 1 file changed, 13 insertions(+), 42 deletions(-) diff --git a/src/main.zig b/src/main.zig index e74c6f8..44463ca 100644 --- a/src/main.zig +++ b/src/main.zig @@ -6,38 +6,20 @@ const Allocator = std.mem.Allocator; const chunk = @import("chunk.zig"); const vm = @import("vm.zig"); +const InterpretResult = vm.InterpretResult; + //const Compiler = @import("compiler.zig").Compiler; pub var hadError = false; -fn run(allocator: *Allocator, data: []u8) !void { +fn run(allocator: *Allocator, data: []u8) !InterpretResult { var stdout_file = try std.io.getStdOut(); const stdout = &stdout_file.outStream().stream; - var scanner = try Scanner.init(allocator, data); - var tokens = try scanner.scanTokens(); - - var it = tokens.iterator(); - - while (it.next()) |token| { - switch (token) { - .Simple => |value| { - try value.printToken(stdout); - }, - .Slice => |value| { - try value.printToken(stdout); - }, - .Number => |value| { - try value.printToken(stdout); - }, - } - - hadError = false; - } + var vmach = try vm.VM.init(allocator, stdout, data, true); + return vmach.interpret(); } -// fn run() !void {} - pub fn doError(line: usize, message: []const u8) !void { try errorReport(line, "", message); } @@ -73,14 +55,15 @@ fn runPrompt(allocator: *Allocator) !void { var line = std.io.readLine(&buffer) catch |err| { if (err == error.EndOfStream) return; + return err; }; - try run(allocator, line); + _ = try run(allocator, line); } } -pub fn mainOld() anyerror!void { +pub fn main() anyerror!void { var da = std.heap.DirectAllocator.init(); var arena = std.heap.ArenaAllocator.init(&da.allocator); defer arena.deinit(); @@ -94,15 +77,14 @@ pub fn mainOld() anyerror!void { }); var lox_path = try (args_it.next(allocator) orelse { - // try runPrompt(allocator); - unreachable; + try runPrompt(allocator); + return; }); - //var vm = VM.init(); - //try runFile(allocator, lox_path); + try runFile(allocator, lox_path); } -pub fn main() !void { +pub fn oldMain() !void { var da = std.heap.DirectAllocator.init(); var arena = std.heap.ArenaAllocator.init(&da.allocator); defer arena.deinit(); @@ -111,19 +93,8 @@ pub fn main() !void { var stdout_file = try std.io.getStdOut(); var stdout = &stdout_file.outStream().stream; - var chk = try chunk.Chunk.init(allocator); - // this crashes zig??? lol + // var chk = try chunk.Chunk.init(allocator); //var opcode_byte: u8 = @enumToInt(chunk.OpCode.Return); //try chk.write(chunk.OpCode.Return); - - try chk.writeConstant(1.2, 123); - try chk.writeConstant(3.4, 123); - try chk.write(chunk.OpCode.Add, 123); - try chk.writeConstant(5.6, 123); - try chk.write(chunk.OpCode.Divide, 123); - try chk.write(chunk.OpCode.Return, 123); - - var vmach = try vm.VM.init(allocator, stdout, "", true); - _ = vmach.interpret(); }