diff --git a/src/chunk.zig b/src/chunk.zig new file mode 100644 index 0000000..5553975 --- /dev/null +++ b/src/chunk.zig @@ -0,0 +1,71 @@ +const std = @import("std"); + +const Allocator = std.mem.Allocator; + +// hack. ugly hack. zig has compiler crash. +const AllOpcodes = struct { + pub Return: u8 = 0, +}; + +pub const OpCode = AllOpcodes{}; + +fn simpleInstruction( + stdout: var, + comptime name: []const u8, + offset: usize, +) !usize { + try stdout.print("{}\n", name); + return offset + 1; +} + +pub const Chunk = struct { + count: usize, + code: []u8, + allocator: *Allocator, + + pub fn init(allocator: *Allocator) !Chunk { + return Chunk{ + .count = 0, + .allocator = allocator, + .code = try allocator.alloc(u8, 0), + }; + } + + pub fn write(self: *Chunk, byte: u8) !void { + if (self.code.len < self.count + 1) { + self.code = try self.allocator.realloc( + self.code, + self.count + 1, + ); + } + + self.code[self.count] = byte; + self.count += 1; + } + + pub fn disassembleInstruction( + self: *Chunk, + stdout: var, + index: usize, + ) !usize { + try stdout.print("{} ", index); + + var instruction = self.code[index]; + + if (instruction == 0) { + return try simpleInstruction(stdout, "OP_RETURN", index); + } else { + try stdout.print("Unknown opcode: {}\n", instruction); + return index + 1; + } + } + + pub fn disassemble(self: *Chunk, stdout: var, name: []const u8) !void { + try stdout.print("== {} ==\n", name); + + var i: usize = 0; + while (i < self.count) : (i += 1) { + i = try self.disassembleInstruction(stdout, i); + } + } +}; diff --git a/src/compiler.zig b/src/compiler.zig new file mode 100644 index 0000000..28e9f15 --- /dev/null +++ b/src/compiler.zig @@ -0,0 +1,13 @@ +const token = @import("token.zig"); +const scanner = @import("scanner.zig"); +const main = @import("main.zig"); + +pub const Compiler = struct { + tokens: *scanner.TokenList, + + fn init(tokens: *scanner.TokenList) Compiler { + return Compiler{ .tokens = tokens }; + } + + fn advance(self: *Compiler) void {} +}; diff --git a/src/expr.zig b/src/expr.zig deleted file mode 100644 index e00e76f..0000000 --- a/src/expr.zig +++ /dev/null @@ -1,28 +0,0 @@ -const Token = @import("token.zig").Token; - -pub const Binary = struct { - left: Expr, - operator: Token, - right: Expr, -}; - -pub const Grouping = struct { - expression: Expr, -}; - -pub const Unary = struct { - operator: Token, - right: Expr, -}; - -pub const ExprType = enum { - Binary, - Grouping, - Unary, -}; - -pub const Expr = union(ExprType) { - Binary: Binary, - Grouping: Grouping, - Unary: Unary, -}; diff --git a/src/main.zig b/src/main.zig index 3dd56e1..c895b02 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,7 +1,11 @@ const std = @import("std"); const Allocator = std.mem.Allocator; -const Scanner = @import("scanner.zig").Scanner; + +// const Scanner = @import("scanner.zig").Scanner; +const chunk = @import("chunk.zig"); + +//const Compiler = @import("compiler.zig").Compiler; pub var hadError = false; @@ -11,6 +15,7 @@ fn run(allocator: *Allocator, data: []u8) !void { var scanner = try Scanner.init(allocator, data); var tokens = try scanner.scanTokens(); + var it = tokens.iterator(); while (it.next()) |token| { @@ -30,6 +35,8 @@ fn run(allocator: *Allocator, data: []u8) !void { } } +// fn run() !void {} + pub fn doError(line: usize, message: []const u8) !void { try errorReport(line, "", message); } @@ -71,22 +78,42 @@ fn runPrompt(allocator: *Allocator) !void { } } -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(); var allocator = &arena.allocator; var args_it = std.process.args(); - const jorts_arg0 = try (args_it.next(allocator) orelse { + var jorts_arg0 = try (args_it.next(allocator) orelse { // if you ever reach this, tell me what is your os lmao unreachable; }); - const lox_path = try (args_it.next(allocator) orelse { - try runPrompt(allocator); - return; + var lox_path = try (args_it.next(allocator) orelse { + // try runPrompt(allocator); + unreachable; }); - try runFile(allocator, lox_path); + //var vm = VM.init(); + //try runFile(allocator, lox_path); +} + +pub fn main() !void { + var da = std.heap.DirectAllocator.init(); + var arena = std.heap.ArenaAllocator.init(&da.allocator); + defer arena.deinit(); + var allocator = &arena.allocator; + + var stdout_file = try std.io.getStdOut(); + const stdout = &stdout_file.outStream().stream; + + var chk = try chunk.Chunk.init(allocator); + + // this crashes zig??? lol + //var opcode_byte: u8 = @enumToInt(chunk.OpCode.Return); + try chk.write(chunk.OpCode.Return); + + try chk.disassemble(stdout, "test chunk"); } diff --git a/src/vm.zig b/src/vm.zig new file mode 100644 index 0000000..b70fb93 --- /dev/null +++ b/src/vm.zig @@ -0,0 +1,7 @@ +const Chunk = @import("chunk.zig"); + +pub const VM = struct { + chunk: *Chunk, + + pub fn init() VM {} +};