From 86532befde8205b440ae0d630fa8feb94afe27da Mon Sep 17 00:00:00 2001 From: Mathias Magnusson Date: Tue, 22 Jul 2025 21:25:21 +0200 Subject: move testing source code to own file --- src/main.zig | 47 ++++++++++++++--------------------------------- 1 file changed, 14 insertions(+), 33 deletions(-) (limited to 'src/main.zig') diff --git a/src/main.zig b/src/main.zig index 367edf8..5873c66 100644 --- a/src/main.zig +++ b/src/main.zig @@ -14,45 +14,26 @@ pub fn main() !void { var args = std.process.args(); _ = args.next(); + const in_path = args.next(); + const in_file = if (in_path) |path| + try std.fs.cwd().openFile(path, .{}) + else + std.io.getStdIn(); + const out_path = args.next(); const out_file = if (out_path) |path| try std.fs.cwd().createFile(path, .{ .mode = 0o777 }) else std.io.getStdOut(); + const run = if (args.next()) |arg| std.mem.eql(u8, arg, "run") else false; - // var br = std.io.bufferedReader(std.io.getStdIn().reader()); - // const stdin = br.reader(); - // - // var line: std.ArrayList(u8) = .init(alloc); - // defer line.deinit(); - // while (true) { - // try stdin.streamUntilDelimiter(line.writer(), '\n', null); - // - // const lexer = Lexer{.source = line}; - // - // try stdout.print("{s}\n", .{line.items}); - // } + const source = try in_file.readToEndAlloc( + allocator, + 640 * 1024, // ought to be enough for anyone + ); + defer allocator.free(source); - const source = - \\{ - \\ let x = 10 - \\ let y = 0 - \\ print(x > y) - \\ while y < 3 { - \\ x = x + x - \\ y = y + 1 - \\ } - \\ if x { - \\ # let x = read_int(0) - \\ # print(18446744073709551615) - \\ x = x + x - \\ } else { - \\ x = 69 - \\ } - \\ print(x) - \\} - ; var lexer: Lexer = .{ .source = source }; std.debug.print("Tokens:\n", .{}); while (true) { @@ -61,7 +42,7 @@ pub fn main() !void { if (token.type == .eof) break; } lexer = .{ .source = source }; - const ast = try parse.block(allocator, &lexer); + const ast = try parse.file(allocator, &lexer); std.debug.print("Parse tree:\n{}\n", .{parse.fmt(ast, source, 0)}); if (lexer.peek().type != .eof) { std.debug.print("Unexpected token {}, expected end of file\n", .{lexer.next()}); @@ -81,7 +62,7 @@ pub fn main() !void { else &.{ "qemu-riscv64", out_path.? }, ); - std.debug.print("{}\n{any}\n", .{err, @errorReturnTrace()}); + std.debug.print("{}\n{any}\n", .{ err, @errorReturnTrace() }); } } -- cgit v1.2.3