ast: add some more love to pretty printer
This commit is contained in:
parent
71cef32fb0
commit
c32b802765
3 changed files with 78 additions and 7 deletions
61
src/ast.zig
61
src/ast.zig
|
@ -1,19 +1,80 @@
|
|||
pub const AstNodeType = enum {
|
||||
Program,
|
||||
BinOp,
|
||||
Number,
|
||||
};
|
||||
|
||||
pub const AstNode = union(AstNodeType) {
|
||||
Program: []AstNode,
|
||||
BinOp: AstBinOp,
|
||||
Number: AstNumber,
|
||||
};
|
||||
|
||||
pub const BinOpType = enum {
|
||||
Equality,
|
||||
};
|
||||
|
||||
pub const AstBinOp = struct {
|
||||
optype: BinOpType,
|
||||
|
||||
// TODO expression
|
||||
left: AstNumber,
|
||||
right: AstNumber,
|
||||
};
|
||||
|
||||
pub const NumberType = enum {
|
||||
Integer32,
|
||||
Integer64,
|
||||
Unsigned32,
|
||||
Unsigned64,
|
||||
Float32,
|
||||
Float64,
|
||||
};
|
||||
|
||||
pub const AstNumber = union(NumberType) {
|
||||
Integer32: i32,
|
||||
Integer64: i64,
|
||||
Unsigned32: u32,
|
||||
Unsigned64: u64,
|
||||
Float32: f32,
|
||||
Float64: f64,
|
||||
};
|
||||
|
||||
pub fn printNode(stdout: var, node: AstNode) anyerror!void {
|
||||
switch (node) {
|
||||
.Program => |children| try printNodes(stdout, children),
|
||||
.BinOp => |bin_op| blk: {
|
||||
try stdout.print("(");
|
||||
switch (bin_op.optype) {
|
||||
.Equality => try stdout.write("=="),
|
||||
}
|
||||
|
||||
try stdout.write(" ");
|
||||
try printNode(stdout, AstNode{ .Number = bin_op.left });
|
||||
try stdout.write(" ");
|
||||
try printNode(stdout, AstNode{ .Number = bin_op.right });
|
||||
try stdout.print(")");
|
||||
},
|
||||
.Number => |ast_num| {
|
||||
switch (ast_num) {
|
||||
.Integer32 => |num| try stdout.print("{}", num),
|
||||
.Integer64 => |num| try stdout.print("{}", num),
|
||||
.Unsigned32 => |num| try stdout.print("{}", num),
|
||||
.Unsigned64 => |num| try stdout.print("{}", num),
|
||||
.Float32 => |num| try stdout.print("{}", num),
|
||||
.Float64 => |num| try stdout.print("{}", num),
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn printNodes(stdout: var, nodes: []AstNode) anyerror!void {
|
||||
try stdout.print("(");
|
||||
|
||||
for (nodes) |node| {
|
||||
try stdout.print(" ");
|
||||
try printNode(stdout, node);
|
||||
}
|
||||
|
||||
try stdout.print(")");
|
||||
}
|
||||
|
|
|
@ -51,12 +51,13 @@ pub const Parser = struct {
|
|||
}
|
||||
}
|
||||
|
||||
fn statement(self: *Parser) !void {}
|
||||
|
||||
fn block(self: *Parser) !void {}
|
||||
|
||||
fn program(self: *Parser) !void {
|
||||
try self.advance();
|
||||
try self.advance();
|
||||
try self.advance();
|
||||
try self.advance();
|
||||
|
||||
try self.block();
|
||||
try self.expect(.EOF);
|
||||
}
|
||||
|
||||
|
|
|
@ -40,9 +40,18 @@ pub const Runner = struct {
|
|||
try self.testScanner(&scanner);
|
||||
|
||||
scanner = scanners.Scanner.init(self.allocator, code);
|
||||
var parser = Parser.init(self.allocator, &scanner);
|
||||
var tree = try parser.parse();
|
||||
try ast.printNode(self.stdout, tree);
|
||||
//var parser = Parser.init(self.allocator, &scanner);
|
||||
//var tree = try parser.parse();
|
||||
try ast.printNode(self.stdout, ast.AstNode{
|
||||
.Program = &[]ast.AstNode{ast.AstNode{
|
||||
.BinOp = ast.AstBinOp{
|
||||
.optype = ast.BinOpType.Equality,
|
||||
.left = ast.AstNumber{ .Integer32 = 30 },
|
||||
.right = ast.AstNumber{ .Integer32 = 30 },
|
||||
},
|
||||
}},
|
||||
});
|
||||
try self.stdout.print("\n");
|
||||
return Result.Ok;
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue