From 803f0a295b168de058ac915d9ac45add44a41f40 Mon Sep 17 00:00:00 2001 From: Marc Tiehuis Date: Thu, 12 Apr 2018 22:23:58 +1200 Subject: Revise self-hosted command line interface Commands are now separated more precisely from one another. Arguments are parsed mostly using a custom argument parser instead of manually. This should be on parity feature-wise with the previous main.zig but adds a few extra code-paths as well that were not yet implemented. Subcommands are much more prominent and consistent. The first argument is always a sub-command and then all following arguments refer to that command. Different commands display there own usage messages and options based on what they can do instead of a one-for-all usage message that was only applicable for the build commands previously. The `cc` command is added and is intended for driving a c compiler. See #490. This is currently a wrapper over the system cc and assumes that it exists, but it should suffice as a starting point. --- src-self-hosted/module.zig | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src-self-hosted/module.zig') diff --git a/src-self-hosted/module.zig b/src-self-hosted/module.zig index 464737bbbb..eec30749e2 100644 --- a/src-self-hosted/module.zig +++ b/src-self-hosted/module.zig @@ -109,6 +109,29 @@ pub const Module = struct { LlvmIr, }; + pub const CliPkg = struct { + name: []const u8, + path: []const u8, + children: ArrayList(&CliPkg), + parent: ?&CliPkg, + + pub fn init(allocator: &mem.Allocator, name: []const u8, path: []const u8, parent: ?&CliPkg) !&CliPkg { + var pkg = try allocator.create(CliPkg); + pkg.name = name; + pkg.path = path; + pkg.children = ArrayList(&CliPkg).init(allocator); + pkg.parent = parent; + return pkg; + } + + pub fn deinit(self: &CliPkg) void { + for (self.children.toSliceConst()) |child| { + child.deinit(); + } + self.children.deinit(); + } + }; + pub fn create(allocator: &mem.Allocator, name: []const u8, root_src_path: ?[]const u8, target: &const Target, kind: Kind, build_mode: builtin.Mode, zig_lib_dir: []const u8, cache_dir: []const u8) !&Module { -- cgit v1.2.3 From bbae6267fe47d6848068938f1a1a83d545f4818f Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 9 May 2018 21:45:29 -0400 Subject: fix self hosted compiler --- src-self-hosted/main.zig | 47 ++++++++++++++++++++++++++++------------------ src-self-hosted/module.zig | 23 ++--------------------- 2 files changed, 31 insertions(+), 39 deletions(-) (limited to 'src-self-hosted/module.zig') diff --git a/src-self-hosted/main.zig b/src-self-hosted/main.zig index c1a6bbe99a..51cc0014a1 100644 --- a/src-self-hosted/main.zig +++ b/src-self-hosted/main.zig @@ -671,34 +671,45 @@ fn cmdFmt(allocator: &Allocator, args: []const []const u8) !void { }; defer allocator.free(source_code); - var tokenizer = std.zig.Tokenizer.init(source_code); - var parser = std.zig.Parser.init(&tokenizer, allocator, file_path); - defer parser.deinit(); - - var tree = parser.parse() catch |err| { + var tree = std.zig.parse(allocator, source_code) catch |err| { try stderr.print("error parsing file '{}': {}\n", file_path, err); continue; }; defer tree.deinit(); - var original_file_backup = try Buffer.init(allocator, file_path); - defer original_file_backup.deinit(); - try original_file_backup.append(".backup"); - try os.rename(allocator, file_path, original_file_backup.toSliceConst()); + var error_it = tree.errors.iterator(0); + while (error_it.next()) |parse_error| { + const token = tree.tokens.at(parse_error.loc()); + const loc = tree.tokenLocation(0, parse_error.loc()); + try stderr.print("{}:{}:{}: error: ", file_path, loc.line + 1, loc.column + 1); + try tree.renderError(parse_error, stderr); + try stderr.print("\n{}\n", source_code[loc.line_start..loc.line_end]); + { + var i: usize = 0; + while (i < loc.column) : (i += 1) { + try stderr.write(" "); + } + } + { + const caret_count = token.end - token.start; + var i: usize = 0; + while (i < caret_count) : (i += 1) { + try stderr.write("~"); + } + } + try stderr.write("\n"); + } + if (tree.errors.len != 0) { + continue; + } try stderr.print("{}\n", file_path); - // TODO: BufferedAtomicFile has some access problems. - var out_file = try os.File.openWrite(allocator, file_path); - defer out_file.close(); + const baf = try io.BufferedAtomicFile.create(allocator, file_path); + defer baf.destroy(); - var out_file_stream = io.FileOutStream.init(&out_file); - try parser.renderSource(out_file_stream.stream, tree.root_node); - - if (!flags.present("keep-backups")) { - try os.deleteFile(allocator, original_file_backup.toSliceConst()); - } + try std.zig.render(allocator, baf.stream(), &tree); } } diff --git a/src-self-hosted/module.zig b/src-self-hosted/module.zig index eec30749e2..ccbd683bdc 100644 --- a/src-self-hosted/module.zig +++ b/src-self-hosted/module.zig @@ -8,9 +8,7 @@ const c = @import("c.zig"); const builtin = @import("builtin"); const Target = @import("target.zig").Target; const warn = std.debug.warn; -const Tokenizer = std.zig.Tokenizer; const Token = std.zig.Token; -const Parser = std.zig.Parser; const ArrayList = std.ArrayList; pub const Module = struct { @@ -246,34 +244,17 @@ pub const Module = struct { warn("{}", source_code); - warn("====tokenization:====\n"); - { - var tokenizer = Tokenizer.init(source_code); - while (true) { - const token = tokenizer.next(); - tokenizer.dump(token); - if (token.id == Token.Id.Eof) { - break; - } - } - } - warn("====parse:====\n"); - var tokenizer = Tokenizer.init(source_code); - var parser = Parser.init(&tokenizer, self.allocator, root_src_real_path); - defer parser.deinit(); - - var tree = try parser.parse(); + var tree = try std.zig.parse(self.allocator, source_code); defer tree.deinit(); var stderr_file = try std.io.getStdErr(); var stderr_file_out_stream = std.io.FileOutStream.init(&stderr_file); const out_stream = &stderr_file_out_stream.stream; - try parser.renderAst(out_stream, tree.root_node); warn("====fmt:====\n"); - try parser.renderSource(out_stream, tree.root_node); + try std.zig.render(self.allocator, out_stream, &tree); warn("====ir:====\n"); warn("TODO\n\n"); -- cgit v1.2.3