remove ident from printExpr

This commit is contained in:
Luna 2019-08-24 11:15:41 -03:00
parent ea5de0914a
commit 9ad0c7df0e
3 changed files with 18 additions and 15 deletions

View file

@ -6,5 +6,7 @@
//)
fn main(a int) int {
1 + 2
1 + 2 + 3 + 4
1 + 1 * 1
3 / (51 + 2)
}

View file

@ -104,7 +104,9 @@ pub fn printNode(node: *Node, ident: usize) void {
}
for (decl.body.toSlice()) |expr| {
printExpr(expr, ident + 1);
printIdent(ident + 1);
printExpr(expr);
std.debug.warn("\n");
}
},
@ -126,7 +128,7 @@ pub fn printNode(node: *Node, ident: usize) void {
}
},
.Expr => |expr| printExpr(expr, 0),
.Expr => |expr| printExpr(expr),
else => {
print(ident, "unknown node: {}\n", node);
@ -134,23 +136,22 @@ pub fn printNode(node: *Node, ident: usize) void {
}
}
fn parenthetize(ident: usize, name: []const u8, exprs: []*Expr) void {
printIdent(ident);
fn parenthetize(name: []const u8, exprs: []*Expr) void {
std.debug.warn("({}", name);
for (exprs) |expr| {
std.debug.warn(" ");
printExpr(expr, ident);
printExpr(expr);
}
std.debug.warn(")");
}
pub fn printExpr(expr: *Expr, ident: usize) void {
pub fn printExpr(expr: *Expr) void {
switch (expr.*) {
.Binary => |binary| parenthetize(ident, binary.op.lexeme, &[_]*Expr{ binary.left, binary.right }),
.Unary => |unary| parenthetize(ident, unary.op.lexeme, &[_]*Expr{unary.right}),
.Grouping => |expr_ptr| parenthetize(ident, "group", &[_]*Expr{expr_ptr}),
.Binary => |binary| parenthetize(binary.op.lexeme, &[_]*Expr{ binary.left, binary.right }),
.Unary => |unary| parenthetize(unary.op.lexeme, &[_]*Expr{unary.right}),
.Grouping => |expr_ptr| parenthetize("group", &[_]*Expr{expr_ptr}),
.Literal => |literal| {
switch (literal) {

View file

@ -428,7 +428,7 @@ pub const Parser = struct {
var expr = try self.parseMultiplication();
std.debug.warn("left expr at addition:");
ast.printExpr(expr, 0);
ast.printExpr(expr);
std.debug.warn("\n");
while (self.compareAnyOf(&[_]TokenType{
@ -442,11 +442,11 @@ pub const Parser = struct {
var right = try self.parseMultiplication();
std.debug.warn("right expr at addition:");
ast.printExpr(right, 0);
ast.printExpr(right);
std.debug.warn("\n");
std.debug.warn("left expr at combination:");
ast.printExpr(expr, 0);
ast.printExpr(expr);
std.debug.warn("\n");
expr = try self.mkBinary(expr, op, right);
@ -460,12 +460,12 @@ pub const Parser = struct {
//};
std.debug.warn("final expr at addition:");
ast.printExpr(expr, 0);
ast.printExpr(expr);
std.debug.warn("\n");
}
std.debug.warn("ret expr at addition:");
ast.printExpr(expr, 0);
ast.printExpr(expr);
std.debug.warn("\n");
return expr;