diff --git a/src/compiler.zig b/src/compiler.zig index e029b9c..28e9f15 100644 --- a/src/compiler.zig +++ b/src/compiler.zig @@ -1,15 +1,13 @@ -const std = @import("std"); -const scanner = @import("new_scanner.zig"); - -const Allocator = std.mem.Allocator; +const token = @import("token.zig"); +const scanner = @import("scanner.zig"); +const main = @import("main.zig"); pub const Compiler = struct { - src: []const u8, - allocator: *Allocator, + tokens: *scanner.TokenList, - pub fn init(allocator: *Allocator, source: []const u8) Compiler { - return Compiler{ .src = source, .allocator = allocator }; + fn init(tokens: *scanner.TokenList) Compiler { + return Compiler{ .tokens = tokens }; } - pub fn compile(self: *Compiler) void {} + fn advance(self: *Compiler) void {} }; diff --git a/src/main.zig b/src/main.zig index 44463ca..b43afc1 100644 --- a/src/main.zig +++ b/src/main.zig @@ -6,20 +6,38 @@ 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) !InterpretResult { +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, data, true); - return vmach.interpret(); + 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; + } } +// fn run() !void {} + pub fn doError(line: usize, message: []const u8) !void { try errorReport(line, "", message); } @@ -40,9 +58,8 @@ fn runFile(allocator: *Allocator, path: []const u8) !void { var slice = try allocator.alloc(u8, total_bytes); _ = try lox_file.read(slice); - var res = try run(allocator, slice); - if (res == vm.InterpretResult.CompileError) std.os.exit(65); - if (res == vm.InterpretResult.RuntimeError) std.os.exit(70); + try run(allocator, slice); + if (hadError) std.os.exit(65); } fn runPrompt(allocator: *Allocator) !void { @@ -55,15 +72,14 @@ 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 main() anyerror!void { +pub fn mainOld() anyerror!void { var da = std.heap.DirectAllocator.init(); var arena = std.heap.ArenaAllocator.init(&da.allocator); defer arena.deinit(); @@ -77,14 +93,15 @@ pub fn main() anyerror!void { }); var lox_path = try (args_it.next(allocator) orelse { - try runPrompt(allocator); - return; + // try runPrompt(allocator); + unreachable; }); - try runFile(allocator, lox_path); + //var vm = VM.init(); + //try runFile(allocator, lox_path); } -pub fn oldMain() !void { +pub fn main() !void { var da = std.heap.DirectAllocator.init(); var arena = std.heap.ArenaAllocator.init(&da.allocator); defer arena.deinit(); @@ -93,8 +110,19 @@ pub fn oldMain() !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, &chk, true); + _ = try vmach.interpret(); } diff --git a/src/vm.zig b/src/vm.zig index d09511a..467651a 100644 --- a/src/vm.zig +++ b/src/vm.zig @@ -1,11 +1,9 @@ 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); @@ -16,8 +14,7 @@ pub const InterpretResult = enum { }; pub const VM = struct { - chk: *Chunk = undefined, - src: []const u8, + chk: *Chunk, ip: usize = 0, stack: []Value, @@ -34,11 +31,11 @@ pub const VM = struct { pub fn init( allocator: *std.mem.Allocator, stdout: StdOut, - source: []const u8, + chk: *Chunk, debug_flag: bool, ) !VM { var self = VM{ - .src = source, + .chk = chk, .stack = try allocator.alloc(Value, 256), .stdout = stdout, @@ -159,15 +156,13 @@ 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; - var cmpr = Compiler.init(self.allocator, self.src); - cmpr.compile(); - return InterpretResult.Ok; + 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 push(self: *VM, val: Value) !void {