authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-12 23:57:15-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-12 23:57:15-04:00
log548ddd1f0c35033cd7e0d1940975bc7185bf7346
tree0d60d99858bb8123a35ac68495c04347dee82fc0
parent7cdc9d98c7134be5edd18eb6f94dd8cfc55bb764

fix AST dumping code in self hosted compiler


3 files changed, 43 insertions(+), 52 deletions(-)

src/ir.cpp+5-1
...@@ -18083,7 +18083,11 @@ static TypeTableEntry *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira...@@ -18083,7 +18083,11 @@ static TypeTableEntry *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira
18083 if (type_is_invalid(end_value->value.type))18083 if (type_is_invalid(end_value->value.type))
18084 return ira->codegen->builtin_types.entry_invalid;18084 return ira->codegen->builtin_types.entry_invalid;
1808518085
18086 assert(start_value->value.type->id == TypeTableEntryIdEnum);18086 if (start_value->value.type->id != TypeTableEntryIdEnum) {
18087 ir_add_error(ira, range->start, buf_sprintf("not an enum type"));
18088 return ira->codegen->builtin_types.entry_invalid;
18089 }
18090
18087 BigInt start_index;18091 BigInt start_index;
18088 bigint_init_bigint(&start_index, &start_value->value.data.x_enum_tag);18092 bigint_init_bigint(&start_index, &start_value->value.data.x_enum_tag);
1808918093
std/zig/ast.zig+37-17
...@@ -67,6 +67,11 @@ pub const Tree = struct {...@@ -67,6 +67,11 @@ pub const Tree = struct {
67 pub fn tokenLocation(self: &Tree, start_index: usize, token_index: TokenIndex) Location {67 pub fn tokenLocation(self: &Tree, start_index: usize, token_index: TokenIndex) Location {
68 return self.tokenLocationPtr(start_index, self.tokens.at(token_index));68 return self.tokenLocationPtr(start_index, self.tokens.at(token_index));
69 }69 }
70
71 pub fn dump(self: &Tree) void {
72 self.root_node.base.dump(0);
73 }
74
70};75};
7176
72pub const Error = union(enum) {77pub const Error = union(enum) {
...@@ -415,6 +420,20 @@ pub const Node = struct {...@@ -415,6 +420,20 @@ pub const Node = struct {
415 }420 }
416 }421 }
417422
423 pub fn dump(self: &Node, indent: usize) void {
424 {
425 var i: usize = 0;
426 while (i < indent) : (i += 1) {
427 std.debug.warn(" ");
428 }
429 }
430 std.debug.warn("{}\n", @tagName(self.id));
431
432 var child_i: usize = 0;
433 while (self.iterate(child_i)) |child| : (child_i += 1) {
434 child.dump(indent + 2);
435 }
436 }
418437
419 pub const Root = struct {438 pub const Root = struct {
420 base: Node,439 base: Node,
...@@ -426,7 +445,7 @@ pub const Node = struct {...@@ -426,7 +445,7 @@ pub const Node = struct {
426445
427 pub fn iterate(self: &Root, index: usize) ?&Node {446 pub fn iterate(self: &Root, index: usize) ?&Node {
428 if (index < self.decls.len) {447 if (index < self.decls.len) {
429 return self.decls.items[self.decls.len - index - 1];448 return *self.decls.at(index);
430 }449 }
431 return null;450 return null;
432 }451 }
...@@ -790,8 +809,16 @@ pub const Node = struct {...@@ -790,8 +809,16 @@ pub const Node = struct {
790 pub fn iterate(self: &FnProto, index: usize) ?&Node {809 pub fn iterate(self: &FnProto, index: usize) ?&Node {
791 var i = index;810 var i = index;
792811
793 if (self.body_node) |body_node| {812 if (self.lib_name) |lib_name| {
794 if (i < 1) return body_node;813 if (i < 1) return lib_name;
814 i -= 1;
815 }
816
817 if (i < self.params.len) return *self.params.at(self.params.len - i - 1);
818 i -= self.params.len;
819
820 if (self.align_expr) |align_expr| {
821 if (i < 1) return align_expr;
795 i -= 1;822 i -= 1;
796 }823 }
797824
...@@ -807,18 +834,11 @@ pub const Node = struct {...@@ -807,18 +834,11 @@ pub const Node = struct {
807 },834 },
808 }835 }
809836
810 if (self.align_expr) |align_expr| {837 if (self.body_node) |body_node| {
811 if (i < 1) return align_expr;838 if (i < 1) return body_node;
812 i -= 1;839 i -= 1;
813 }840 }
814841
815 if (i < self.params.len) return self.params.items[self.params.len - i - 1];
816 i -= self.params.len;
817
818 if (self.lib_name) |lib_name| {
819 if (i < 1) return lib_name;
820 i -= 1;
821 }
822842
823 return null;843 return null;
824 }844 }
...@@ -914,7 +934,7 @@ pub const Node = struct {...@@ -914,7 +934,7 @@ pub const Node = struct {
914 pub fn iterate(self: &Block, index: usize) ?&Node {934 pub fn iterate(self: &Block, index: usize) ?&Node {
915 var i = index;935 var i = index;
916936
917 if (i < self.statements.len) return self.statements.items[i];937 if (i < self.statements.len) return *self.statements.at(i);
918 i -= self.statements.len;938 i -= self.statements.len;
919939
920 return null;940 return null;
...@@ -1596,7 +1616,7 @@ pub const Node = struct {...@@ -1596,7 +1616,7 @@ pub const Node = struct {
1596 i -= 1;1616 i -= 1;
15971617
1598 switch (self.op) {1618 switch (self.op) {
1599 Op.Call => |call_info| {1619 @TagType(Op).Call => |*call_info| {
1600 if (i < call_info.params.len) return *call_info.params.at(i);1620 if (i < call_info.params.len) return *call_info.params.at(i);
1601 i -= call_info.params.len;1621 i -= call_info.params.len;
1602 },1622 },
...@@ -1604,7 +1624,7 @@ pub const Node = struct {...@@ -1604,7 +1624,7 @@ pub const Node = struct {
1604 if (i < 1) return index_expr;1624 if (i < 1) return index_expr;
1605 i -= 1;1625 i -= 1;
1606 },1626 },
1607 Op.Slice => |range| {1627 @TagType(Op).Slice => |range| {
1608 if (i < 1) return range.start;1628 if (i < 1) return range.start;
1609 i -= 1;1629 i -= 1;
16101630
...@@ -1613,11 +1633,11 @@ pub const Node = struct {...@@ -1613,11 +1633,11 @@ pub const Node = struct {
1613 i -= 1;1633 i -= 1;
1614 }1634 }
1615 },1635 },
1616 Op.ArrayInitializer => |exprs| {1636 Op.ArrayInitializer => |*exprs| {
1617 if (i < exprs.len) return *exprs.at(i);1637 if (i < exprs.len) return *exprs.at(i);
1618 i -= exprs.len;1638 i -= exprs.len;
1619 },1639 },
1620 Op.StructInitializer => |fields| {1640 Op.StructInitializer => |*fields| {
1621 if (i < fields.len) return *fields.at(i);1641 if (i < fields.len) return *fields.at(i);
1622 i -= fields.len;1642 i -= fields.len;
1623 },1643 },
std/zig/parse.zig+1-34
...@@ -7,9 +7,8 @@ const Token = std.zig.Token;...@@ -7,9 +7,8 @@ const Token = std.zig.Token;
7const TokenIndex = ast.TokenIndex;7const TokenIndex = ast.TokenIndex;
8const Error = ast.Error;8const Error = ast.Error;
99
10/// Returns an AST tree, allocated with the parser's allocator.
11/// Result should be freed with tree.deinit() when there are10/// Result should be freed with tree.deinit() when there are
12/// no more references to any AST nodes of the tree.11/// no more references to any of the tokens or nodes.
13pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {12pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
14 var tree_arena = std.heap.ArenaAllocator.init(allocator);13 var tree_arena = std.heap.ArenaAllocator.init(allocator);
15 errdefer tree_arena.deinit();14 errdefer tree_arena.deinit();
...@@ -3466,38 +3465,6 @@ fn putBackToken(tok_it: &ast.Tree.TokenList.Iterator, tree: &ast.Tree) void {...@@ -3466,38 +3465,6 @@ fn putBackToken(tok_it: &ast.Tree.TokenList.Iterator, tree: &ast.Tree) void {
3466 }3465 }
3467}3466}
34683467
3469const RenderAstFrame = struct {
3470 node: &ast.Node,
3471 indent: usize,
3472};
3473
3474pub fn renderAst(allocator: &mem.Allocator, tree: &const ast.Tree, stream: var) !void {
3475 var stack = std.ArrayList(State).init(allocator);
3476 defer stack.deinit();
3477
3478 try stack.append(RenderAstFrame {
3479 .node = &root_node.base,
3480 .indent = 0,
3481 });
3482
3483 while (stack.popOrNull()) |frame| {
3484 {
3485 var i: usize = 0;
3486 while (i < frame.indent) : (i += 1) {
3487 try stream.print(" ");
3488 }
3489 }
3490 try stream.print("{}\n", @tagName(frame.node.id));
3491 var child_i: usize = 0;
3492 while (frame.node.iterate(child_i)) |child| : (child_i += 1) {
3493 try stack.append(RenderAstFrame {
3494 .node = child,
3495 .indent = frame.indent + 2,
3496 });
3497 }
3498 }
3499}
3500
3501test "std.zig.parser" {3468test "std.zig.parser" {
3502 _ = @import("parser_test.zig");3469 _ = @import("parser_test.zig");
3503}3470}