From 9ad0c7df0e5102bd694a1d3d9f63c12a74d69063 Mon Sep 17 00:00:00 2001 From: Luna Date: Sat, 24 Aug 2019 11:15:41 -0300 Subject: [PATCH] remove ident from printExpr --- examples/hello.v | 4 +++- src/ast.zig | 19 ++++++++++--------- src/parser.zig | 10 +++++----- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/examples/hello.v b/examples/hello.v index 4b4f28c..9219a29 100644 --- a/examples/hello.v +++ b/examples/hello.v @@ -6,5 +6,7 @@ //) fn main(a int) int { - 1 + 2 + 1 + 2 + 3 + 4 + 1 + 1 * 1 + 3 / (51 + 2) } diff --git a/src/ast.zig b/src/ast.zig index 700f491..b08c4f7 100644 --- a/src/ast.zig +++ b/src/ast.zig @@ -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) { diff --git a/src/parser.zig b/src/parser.zig index b5538b6..4287888 100644 --- a/src/parser.zig +++ b/src/parser.zig @@ -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;