authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-09 21:45:29-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-09 21:45:29-04:00
logbbae6267fe47d6848068938f1a1a83d545f4818f
tree23d52adcac73ac174b8112a1aeefc4dcb2e5360c
parent774b6ffe1e0577a2d1a32b04d71c86525627748a

fix self hosted compiler


2 files changed, 31 insertions(+), 39 deletions(-)

src-self-hosted/main.zig+29-18
...@@ -671,34 +671,45 @@ fn cmdFmt(allocator: &Allocator, args: []const []const u8) !void {...@@ -671,34 +671,45 @@ fn cmdFmt(allocator: &Allocator, args: []const []const u8) !void {
671 };671 };
672 defer allocator.free(source_code);672 defer allocator.free(source_code);
673673
674 var tokenizer = std.zig.Tokenizer.init(source_code);674 var tree = std.zig.parse(allocator, source_code) catch |err| {
675 var parser = std.zig.Parser.init(&tokenizer, allocator, file_path);
676 defer parser.deinit();
677
678 var tree = parser.parse() catch |err| {
679 try stderr.print("error parsing file '{}': {}\n", file_path, err);675 try stderr.print("error parsing file '{}': {}\n", file_path, err);
680 continue;676 continue;
681 };677 };
682 defer tree.deinit();678 defer tree.deinit();
683679
684 var original_file_backup = try Buffer.init(allocator, file_path);
685 defer original_file_backup.deinit();
686 try original_file_backup.append(".backup");
687680
688 try os.rename(allocator, file_path, original_file_backup.toSliceConst());681 var error_it = tree.errors.iterator(0);
682 while (error_it.next()) |parse_error| {
683 const token = tree.tokens.at(parse_error.loc());
684 const loc = tree.tokenLocation(0, parse_error.loc());
685 try stderr.print("{}:{}:{}: error: ", file_path, loc.line + 1, loc.column + 1);
686 try tree.renderError(parse_error, stderr);
687 try stderr.print("\n{}\n", source_code[loc.line_start..loc.line_end]);
688 {
689 var i: usize = 0;
690 while (i < loc.column) : (i += 1) {
691 try stderr.write(" ");
692 }
693 }
694 {
695 const caret_count = token.end - token.start;
696 var i: usize = 0;
697 while (i < caret_count) : (i += 1) {
698 try stderr.write("~");
699 }
700 }
701 try stderr.write("\n");
702 }
703 if (tree.errors.len != 0) {
704 continue;
705 }
689706
690 try stderr.print("{}\n", file_path);707 try stderr.print("{}\n", file_path);
691708
692 // TODO: BufferedAtomicFile has some access problems.709 const baf = try io.BufferedAtomicFile.create(allocator, file_path);
693 var out_file = try os.File.openWrite(allocator, file_path);710 defer baf.destroy();
694 defer out_file.close();
695711
696 var out_file_stream = io.FileOutStream.init(&out_file);712 try std.zig.render(allocator, baf.stream(), &tree);
697 try parser.renderSource(out_file_stream.stream, tree.root_node);
698
699 if (!flags.present("keep-backups")) {
700 try os.deleteFile(allocator, original_file_backup.toSliceConst());
701 }
702 }713 }
703}714}
704715
src-self-hosted/module.zig+2-21
...@@ -8,9 +8,7 @@ const c = @import("c.zig");...@@ -8,9 +8,7 @@ const c = @import("c.zig");
8const builtin = @import("builtin");8const builtin = @import("builtin");
9const Target = @import("target.zig").Target;9const Target = @import("target.zig").Target;
10const warn = std.debug.warn;10const warn = std.debug.warn;
11const Tokenizer = std.zig.Tokenizer;
12const Token = std.zig.Token;11const Token = std.zig.Token;
13const Parser = std.zig.Parser;
14const ArrayList = std.ArrayList;12const ArrayList = std.ArrayList;
1513
16pub const Module = struct {14pub const Module = struct {
...@@ -246,34 +244,17 @@ pub const Module = struct {...@@ -246,34 +244,17 @@ pub const Module = struct {
246244
247 warn("{}", source_code);245 warn("{}", source_code);
248246
249 warn("====tokenization:====\n");
250 {
251 var tokenizer = Tokenizer.init(source_code);
252 while (true) {
253 const token = tokenizer.next();
254 tokenizer.dump(token);
255 if (token.id == Token.Id.Eof) {
256 break;
257 }
258 }
259 }
260
261 warn("====parse:====\n");247 warn("====parse:====\n");
262248
263 var tokenizer = Tokenizer.init(source_code);249 var tree = try std.zig.parse(self.allocator, source_code);
264 var parser = Parser.init(&tokenizer, self.allocator, root_src_real_path);
265 defer parser.deinit();
266
267 var tree = try parser.parse();
268 defer tree.deinit();250 defer tree.deinit();
269251
270 var stderr_file = try std.io.getStdErr();252 var stderr_file = try std.io.getStdErr();
271 var stderr_file_out_stream = std.io.FileOutStream.init(&stderr_file);253 var stderr_file_out_stream = std.io.FileOutStream.init(&stderr_file);
272 const out_stream = &stderr_file_out_stream.stream;254 const out_stream = &stderr_file_out_stream.stream;
273 try parser.renderAst(out_stream, tree.root_node);
274255
275 warn("====fmt:====\n");256 warn("====fmt:====\n");
276 try parser.renderSource(out_stream, tree.root_node);257 try std.zig.render(self.allocator, out_stream, &tree);
277258
278 warn("====ir:====\n");259 warn("====ir:====\n");
279 warn("TODO\n\n");260 warn("TODO\n\n");