Compare commits

...

2 commits

Author SHA1 Message Date
02c2055601 parser: make println consume parens 2019-08-24 16:56:58 -03:00
1651a99faf parser: don't skip tokens on groupings 2019-08-24 16:51:37 -03:00
2 changed files with 19 additions and 8 deletions

View file

@ -1,12 +1,13 @@
//const ( const (
// Cock = 1 Cock = 1
// Ball = 2 Ball = 2 + 3
// Deals = 3 Deals = 3
// Businesses = 4 Businesses = 4
//) )
fn main(a int) int { fn main(a int) int {
1 + 2 + 3 + 4 1 + 2 + 3 + 4
1 + 1 * 1 1 + 1 * 1
3 / (51 + 2) 3 / (51 + 2)
println(2 * 1956)
} }

View file

@ -91,10 +91,12 @@ pub const Parser = struct {
} }
fn consumeSingle(self: *Parser, ttype: TokenType) !Token { fn consumeSingle(self: *Parser, ttype: TokenType) !Token {
std.debug.warn("consume {}..?", ttype);
if (self.check(ttype)) { if (self.check(ttype)) {
var cur = self.peek(); var cur = self.peek();
_ = try self.nextToken(); _ = try self.nextToken();
std.debug.warn("consumed {}, now has {}\n", ttype, self.peek()); std.debug.warn(" now has {}\n", self.peek());
return cur; return cur;
} }
@ -379,6 +381,7 @@ pub const Parser = struct {
_ = try self.consumeSingle(.LeftBrace); _ = try self.consumeSingle(.LeftBrace);
while (self.peek().ttype != .RightBrace) { while (self.peek().ttype != .RightBrace) {
std.debug.warn("get smt with cur {}\n", self.peek().ttype);
var node = try self.parseStmt(); var node = try self.parseStmt();
ast.printNode(node, 0); ast.printNode(node, 0);
try stmts.append(node.Stmt); try stmts.append(node.Stmt);
@ -402,7 +405,11 @@ pub const Parser = struct {
fn parsePrintln(self: *@This()) !*Stmt { fn parsePrintln(self: *@This()) !*Stmt {
_ = try self.consumeSingle(.Println); _ = try self.consumeSingle(.Println);
_ = try self.consumeSingle(.LeftParen);
var expr = try self.parseExpr(); var expr = try self.parseExpr();
_ = try self.consumeSingle(.RightParen);
return try Stmt.mkPrintln(self.allocator, expr.Expr); return try Stmt.mkPrintln(self.allocator, expr.Expr);
} }
@ -509,7 +516,10 @@ pub const Parser = struct {
_ = try self.nextToken(); _ = try self.nextToken();
var expr = (try self.parseExpr()).Expr; var expr = (try self.parseExpr()).Expr;
_ = try self.consume(.RightParen, "Expected ')' after expression"); _ = try self.consume(.RightParen, "Expected ')' after expression");
break :blk try self.mkGrouping(expr);
// for groupings, we don't want to skip tokens as we already
// consumed RightParen.
return try self.mkGrouping(expr);
}, },
else => blk: { else => blk: {