fix AST dumping code in self hosted compiler

This commit is contained in:
Andrew Kelley 2018-05-12 23:57:15 -04:00
parent 7cdc9d98c7
commit 548ddd1f0c
3 changed files with 43 additions and 52 deletions

View File

@ -18083,7 +18083,11 @@ static TypeTableEntry *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira
if (type_is_invalid(end_value->value.type))
return ira->codegen->builtin_types.entry_invalid;
assert(start_value->value.type->id == TypeTableEntryIdEnum);
if (start_value->value.type->id != TypeTableEntryIdEnum) {
ir_add_error(ira, range->start, buf_sprintf("not an enum type"));
return ira->codegen->builtin_types.entry_invalid;
}
BigInt start_index;
bigint_init_bigint(&start_index, &start_value->value.data.x_enum_tag);

View File

@ -67,6 +67,11 @@ pub const Tree = struct {
pub fn tokenLocation(self: &Tree, start_index: usize, token_index: TokenIndex) Location {
return self.tokenLocationPtr(start_index, self.tokens.at(token_index));
}
pub fn dump(self: &Tree) void {
self.root_node.base.dump(0);
}
};
pub const Error = union(enum) {
@ -415,6 +420,20 @@ pub const Node = struct {
}
}
pub fn dump(self: &Node, indent: usize) void {
{
var i: usize = 0;
while (i < indent) : (i += 1) {
std.debug.warn(" ");
}
}
std.debug.warn("{}\n", @tagName(self.id));
var child_i: usize = 0;
while (self.iterate(child_i)) |child| : (child_i += 1) {
child.dump(indent + 2);
}
}
pub const Root = struct {
base: Node,
@ -426,7 +445,7 @@ pub const Node = struct {
pub fn iterate(self: &Root, index: usize) ?&Node {
if (index < self.decls.len) {
return self.decls.items[self.decls.len - index - 1];
return *self.decls.at(index);
}
return null;
}
@ -790,8 +809,16 @@ pub const Node = struct {
pub fn iterate(self: &FnProto, index: usize) ?&Node {
var i = index;
if (self.body_node) |body_node| {
if (i < 1) return body_node;
if (self.lib_name) |lib_name| {
if (i < 1) return lib_name;
i -= 1;
}
if (i < self.params.len) return *self.params.at(self.params.len - i - 1);
i -= self.params.len;
if (self.align_expr) |align_expr| {
if (i < 1) return align_expr;
i -= 1;
}
@ -807,18 +834,11 @@ pub const Node = struct {
},
}
if (self.align_expr) |align_expr| {
if (i < 1) return align_expr;
if (self.body_node) |body_node| {
if (i < 1) return body_node;
i -= 1;
}
if (i < self.params.len) return self.params.items[self.params.len - i - 1];
i -= self.params.len;
if (self.lib_name) |lib_name| {
if (i < 1) return lib_name;
i -= 1;
}
return null;
}
@ -914,7 +934,7 @@ pub const Node = struct {
pub fn iterate(self: &Block, index: usize) ?&Node {
var i = index;
if (i < self.statements.len) return self.statements.items[i];
if (i < self.statements.len) return *self.statements.at(i);
i -= self.statements.len;
return null;
@ -1596,7 +1616,7 @@ pub const Node = struct {
i -= 1;
switch (self.op) {
Op.Call => |call_info| {
@TagType(Op).Call => |*call_info| {
if (i < call_info.params.len) return *call_info.params.at(i);
i -= call_info.params.len;
},
@ -1604,7 +1624,7 @@ pub const Node = struct {
if (i < 1) return index_expr;
i -= 1;
},
Op.Slice => |range| {
@TagType(Op).Slice => |range| {
if (i < 1) return range.start;
i -= 1;
@ -1613,11 +1633,11 @@ pub const Node = struct {
i -= 1;
}
},
Op.ArrayInitializer => |exprs| {
Op.ArrayInitializer => |*exprs| {
if (i < exprs.len) return *exprs.at(i);
i -= exprs.len;
},
Op.StructInitializer => |fields| {
Op.StructInitializer => |*fields| {
if (i < fields.len) return *fields.at(i);
i -= fields.len;
},

View File

@ -7,9 +7,8 @@ const Token = std.zig.Token;
const TokenIndex = ast.TokenIndex;
const Error = ast.Error;
/// Returns an AST tree, allocated with the parser's allocator.
/// Result should be freed with tree.deinit() when there are
/// no more references to any AST nodes of the tree.
/// no more references to any of the tokens or nodes.
pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
var tree_arena = std.heap.ArenaAllocator.init(allocator);
errdefer tree_arena.deinit();
@ -3466,38 +3465,6 @@ fn putBackToken(tok_it: &ast.Tree.TokenList.Iterator, tree: &ast.Tree) void {
}
}
const RenderAstFrame = struct {
node: &ast.Node,
indent: usize,
};
pub fn renderAst(allocator: &mem.Allocator, tree: &const ast.Tree, stream: var) !void {
var stack = std.ArrayList(State).init(allocator);
defer stack.deinit();
try stack.append(RenderAstFrame {
.node = &root_node.base,
.indent = 0,
});
while (stack.popOrNull()) |frame| {
{
var i: usize = 0;
while (i < frame.indent) : (i += 1) {
try stream.print(" ");
}
}
try stream.print("{}\n", @tagName(frame.node.id));
var child_i: usize = 0;
while (frame.node.iterate(child_i)) |child| : (child_i += 1) {
try stack.append(RenderAstFrame {
.node = child,
.indent = frame.indent + 2,
});
}
}
}
test "std.zig.parser" {
_ = @import("parser_test.zig");
}