const std = @import("std"); const Allocator = std.mem.Allocator; // const Scanner = @import("scanner.zig").Scanner; const chunk = @import("chunk.zig"); //const Compiler = @import("compiler.zig").Compiler; pub var hadError = false; fn run(allocator: *Allocator, data: []u8) !void { 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; } } // fn run() !void {} pub fn doError(line: usize, message: []const u8) !void { try errorReport(line, "", message); } pub fn errorReport(line: usize, where: []const u8, message: []const u8) !void { var stdout_file = try std.io.getStdOut(); const stdout = &stdout_file.outStream().stream; try stdout.print("[line {}] Error {}: {}\n", line, where, message); hadError = true; } fn runFile(allocator: *Allocator, path: []const u8) !void { var lox_file = try std.fs.File.openRead(path); defer lox_file.close(); const total_bytes = try lox_file.getEndPos(); var slice = try allocator.alloc(u8, total_bytes); _ = try lox_file.read(slice); try run(allocator, slice); if (hadError) std.os.exit(65); } fn runPrompt(allocator: *Allocator) !void { var stdout_file = try std.io.getStdOut(); const stdout = &stdout_file.outStream().stream; while (true) { try stdout.print(">"); var buffer = try std.Buffer.init(allocator, ""[0..]); var line = std.io.readLine(&buffer) catch |err| { if (err == error.EndOfStream) return; return err; }; try run(allocator, line); } } 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(); var jorts_arg0 = try (args_it.next(allocator) orelse { // if you ever reach this, tell me what is your os lmao unreachable; }); var lox_path = try (args_it.next(allocator) orelse { // try runPrompt(allocator); unreachable; }); //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); var constant = try chk.addConstant(1.2); try chk.write(chunk.OpCode.Constant, 123); try chk.write(constant, 123); try chk.write(chunk.OpCode.Return, 123); try chk.disassemble(stdout, "test chunk"); }