const std = @import("std"); const parse = @import("./parse.zig"); const peek = @import("./peek.zig"); const codegen = @import("./codegen.zig"); pub const peekable = peek.peekable; pub const Peekable = peek.Peekable; pub const Expr = parse.Expr; pub const Lexer = @import("./Lexer.zig"); pub const compile = @import("./compile.zig"); pub fn main() !void { var arena: std.heap.ArenaAllocator = .init(std.heap.smp_allocator); defer arena.deinit(); const allocator = arena.allocator(); var args = std.process.args(); _ = args.next(); const out_file = if (args.next()) |path| try std.fs.cwd().createFile(path, .{ .mode = 0o777 }) else std.io.getStdOut(); const output = out_file.writer(); // 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 = "17216961135462248174 + 4095 - 4294967295 + 2147483647"; // var lexer = Lexer{ .source = source }; // while (true) { // const token = lexer.next().?; // std.debug.print("{}\n", .{token}); // if (token.type == .Eof) break; // } // std.debug.print("\n", .{}); var lexer = peekable(Lexer{ .source = source }); const expr = try parse.expression(allocator, &lexer); std.debug.print("{}\n", .{expr}); if (lexer.next()) |token| if (token.type != .eof) { std.debug.print("Unexpected token {}, expected end of file\n", .{token}); }; const block = try compile.compile(allocator, source, expr); for (block.instrs) |instr| { std.debug.print("{}\n", .{instr}); } const elf = try codegen.create_elf(allocator, block); try output.writeAll(elf); // try stdout.print("{x}\n", .{elf}); } fn HashMapFormatter(HashMap: type) type { return std.fmt.Formatter(struct { fn formatHashMap( hash_map: HashMap, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype, ) !void { _ = fmt; _ = options; try writer.writeAll("{"); var it = hash_map.iterator(); var first = true; while (it.next()) |kv| { try writer.print("{s} {any}: {any}", .{ if (first) "" else ",", kv.key_ptr.*, kv.value_ptr.* }); first = false; } try writer.writeAll(if (first) "}" else " }"); } }.formatHashMap); } pub fn fmtHashMap(hash_map: anytype) HashMapFormatter(@TypeOf(hash_map)) { return .{ .data = hash_map }; } test { _ = peek; }