add better ast printing

This commit is contained in:
Luna 2019-08-23 15:42:50 -03:00
parent e3fdf5399b
commit 7ba140dd73
3 changed files with 33 additions and 3 deletions

View file

@ -43,3 +43,28 @@ pub fn mkRoot(allocator: *std.mem.Allocator) !*Node {
node.* = Node{ .Root = NodeList.init(allocator) };
return node;
}
fn print(ident: usize, comptime fmt: []const u8, args: ...) void {
var i: usize = 0;
while (i < ident) : (i += 1) {
std.debug.warn("\t");
}
std.debug.warn(fmt, args);
}
pub fn printNode(node: *Node, ident: usize) void {
switch (node.*) {
.FnDecl => |proto| {
print(ident, "FnDecl name='{}'\n", proto.func_name.lexeme);
},
.Root => {
for (node.Root.toSlice()) |child| {
printNode(child, ident + 1);
}
},
else => {
print(ident, "unknown node: {}\n", node);
},
}
}

View file

@ -233,6 +233,12 @@ pub const Parser = struct {
.Fn => blk: {
_ = try self.consumeSingle(.Fn);
var name = try self.consumeSingle(.Identifier);
_ = try self.consumeSingle(.LeftParen);
// TODO paramlist
_ = try self.consumeSingle(.RightParen);
_ = try self.consumeSingle(.LeftBrace);
// TODO block
_ = try self.consumeSingle(.RightBrace);
std.debug.warn("!fn name: {}\n", name);
break :blk try self.mkFnDecl(
name,

View file

@ -48,9 +48,8 @@ pub const Runner = struct {
var it = root.Root.iterator();
while (it.next()) |node| {
std.debug.warn("{}\n", node.*);
}
std.debug.warn("parse tree\n");
ast.printNode(root, 0);
return Result.Ok;
}