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 { 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| { 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 => { else => {
print(ident, "unknown node: {}\n", node); 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 { fn parenthetize(name: []const u8, exprs: []*Expr) void {
printIdent(ident);
std.debug.warn("({}", name); std.debug.warn("({}", name);
for (exprs) |expr| { for (exprs) |expr| {
std.debug.warn(" "); std.debug.warn(" ");
printExpr(expr, ident); printExpr(expr);
} }
std.debug.warn(")"); std.debug.warn(")");
} }
pub fn printExpr(expr: *Expr, ident: usize) void { pub fn printExpr(expr: *Expr) void {
switch (expr.*) { switch (expr.*) {
.Binary => |binary| parenthetize(ident, binary.op.lexeme, &[_]*Expr{ binary.left, binary.right }), .Binary => |binary| parenthetize(binary.op.lexeme, &[_]*Expr{ binary.left, binary.right }),
.Unary => |unary| parenthetize(ident, unary.op.lexeme, &[_]*Expr{unary.right}), .Unary => |unary| parenthetize(unary.op.lexeme, &[_]*Expr{unary.right}),
.Grouping => |expr_ptr| parenthetize(ident, "group", &[_]*Expr{expr_ptr}), .Grouping => |expr_ptr| parenthetize("group", &[_]*Expr{expr_ptr}),
.Literal => |literal| { .Literal => |literal| {
switch (literal) { switch (literal) {

View File

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