| 1 | //! Minimal CLI via `zig build transform -- filepath` |
| 2 | //! Use this only for working on Parse+Transform |
| 3 | |
| 4 | const arg_config: Cli = .{ |
| 5 | .options = &.{ |
| 6 | .{ .name = "--ast", .desc = "Show AST", .short = 'a' }, |
| 7 | .{ .name = "--dump-bin", .kind = .string, .desc = "Dump serialized binary to file" }, |
| 8 | }, |
| 9 | .positionals = .single, |
| 10 | }; |
| 11 | |
| 12 | pub fn main() !u8 { |
| 13 | try core.unicode_props.init(); |
| 14 | |
| 15 | const use_gpa = @import("builtin").mode == .Debug; |
| 16 | var gpa = if (use_gpa) |
| 17 | std.heap.GeneralPurposeAllocator(.{ |
| 18 | .stack_trace_frames = 100, |
| 19 | }){} |
| 20 | else |
| 21 | std.heap.ArenaAllocator.init(std.heap.page_allocator); |
| 22 | defer _ = if (use_gpa) gpa.deinit(); |
| 23 | const alloc = gpa.allocator(); |
| 24 | |
| 25 | const program_name, const raw_args = try Cli.programArgsAlloc(alloc, "transform"); |
| 26 | defer Cli.programArgsFree(alloc, raw_args); |
| 27 | |
| 28 | const args = arg_config.parse(program_name, raw_args); |
| 29 | |
| 30 | const file_path = args.positionals orelse |
| 31 | arg_config.fatalWithHelp(program_name, "missing filepath argument\n", .{}); |
| 32 | |
| 33 | const out = core.OutStream.getStderr(); |
| 34 | |
| 35 | const code = try std.fs.cwd() |
| 36 | .readFileAllocOptions(alloc, file_path, std.math.maxInt(u32), null, @alignOf(u8), 0); |
| 37 | defer alloc.free(code); |
| 38 | |
| 39 | var err_data: core.Parse.AstError.WithTrace = undefined; |
| 40 | var ast = core.Parse.parse( |
| 41 | alloc, |
| 42 | code, |
| 43 | &err_data, |
| 44 | ) catch |err| switch (err) { |
| 45 | error.OutOfMemory => return err, |
| 46 | error.ParseFail => { |
| 47 | var report = try err_data.toReport(alloc); |
| 48 | defer report.deinit(alloc); |
| 49 | |
| 50 | report.dump( |
| 51 | .{ .single_file = .{ |
| 52 | .allocator = alloc, |
| 53 | .url = file_path, |
| 54 | .file = .{ .utf8_source = code }, |
| 55 | } }, |
| 56 | out, |
| 57 | ) catch {}; |
| 58 | |
| 59 | return 1; |
| 60 | }, |
| 61 | }; |
| 62 | defer ast.deinit(alloc); |
| 63 | |
| 64 | if (args.ast == true) { |
| 65 | ast.dumpToWriter(out.writer) catch {}; |
| 66 | } |
| 67 | |
| 68 | var teg_report: core.Report = undefined; |
| 69 | var hir = core.Transform.transform(ast, alloc, &teg_report) catch |err| switch (err) { |
| 70 | error.OutOfMemory => return err, |
| 71 | error.TransformFail => { |
| 72 | defer teg_report.deinit(alloc); |
| 73 | teg_report.dump( |
| 74 | .{ .single_file = .{ |
| 75 | .allocator = alloc, |
| 76 | .url = file_path, |
| 77 | .file = .{ .ast = &ast }, |
| 78 | } }, |
| 79 | out, |
| 80 | ) catch {}; |
| 81 | return 1; |
| 82 | }, |
| 83 | }; |
| 84 | defer hir.deinit(alloc); |
| 85 | |
| 86 | // const file = try std.fs.cwd().createFile("serialized.hir", .{}); |
| 87 | // defer file.close(); |
| 88 | // try hir.serialize(.{ .file = file }); |
| 89 | |
| 90 | try hir.dumpToWriter(out.writer, out.tty); |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | const std = @import("std"); |
| 96 | const core = @import("core"); |
| 97 | const Cli = @import("Cli"); |