parser: use mkBinary (allocating them on heap)

This commit is contained in:
Luna 2019-08-24 13:06:46 -03:00
parent 9ad0c7df0e
commit 2ba0efa92c

View file

@ -386,15 +386,10 @@ pub const Parser = struct {
while (self.peek().ttype == .EqualEqual) {
var op = self.peek();
var right = try self.parseComparison();
_ = try self.nextToken();
expr.* = ast.Expr{
.Binary = ast.BinaryExpr{
.left = expr,
.op = op,
.right = right,
},
};
var right = try self.parseComparison();
expr = try self.mkBinary(expr, op, right);
}
return expr;
@ -409,16 +404,11 @@ pub const Parser = struct {
.Less,
.LessEqual,
})) {
var op = self.previous();
var right = try self.parseAddition();
var op = self.peek();
_ = try self.nextToken();
expr.* = ast.Expr{
.Binary = ast.BinaryExpr{
.left = expr,
.op = op,
.right = right,
},
};
var right = try self.parseAddition();
expr = try self.mkBinary(expr, op, right);
}
return expr;
@ -427,47 +417,16 @@ pub const Parser = struct {
fn parseAddition(self: *@This()) !*Expr {
var expr = try self.parseMultiplication();
std.debug.warn("left expr at addition:");
ast.printExpr(expr);
std.debug.warn("\n");
while (self.compareAnyOf(&[_]TokenType{
.Minus, .Plus,
})) {
var op = self.peek();
_ = try self.nextToken();
std.debug.warn("==OP token {}\n", op);
var right = try self.parseMultiplication();
std.debug.warn("right expr at addition:");
ast.printExpr(right);
std.debug.warn("\n");
std.debug.warn("left expr at combination:");
ast.printExpr(expr);
std.debug.warn("\n");
expr = try self.mkBinary(expr, op, right);
//expr.* = ast.Expr{
// .Binary = ast.BinaryExpr{
// .left = expr,
// .op = op,
// .right = right,
// },
//};
std.debug.warn("final expr at addition:");
ast.printExpr(expr);
std.debug.warn("\n");
}
std.debug.warn("ret expr at addition:");
ast.printExpr(expr);
std.debug.warn("\n");
return expr;
}
@ -477,16 +436,11 @@ pub const Parser = struct {
while (self.compareAnyOf(&[_]TokenType{
.Star, .Slash,
})) {
var op = self.previous();
var op = self.peek();
_ = try self.nextToken();
var right = try self.parseUnary();
expr.* = ast.Expr{
.Binary = ast.BinaryExpr{
.left = expr,
.op = op,
.right = right,
},
};
expr = try self.mkBinary(expr, op, right);
}
return expr;