add better ast printing
This commit is contained in:
parent
e3fdf5399b
commit
7ba140dd73
3 changed files with 33 additions and 3 deletions
25
src/ast.zig
25
src/ast.zig
|
@ -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);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue