parser: make parseExpr return Expr instead of Node
This commit is contained in:
parent
3cd19e6515
commit
b382c136ec
1 changed files with 18 additions and 14 deletions
|
@ -129,6 +129,8 @@ pub const Parser = struct {
|
|||
return false;
|
||||
}
|
||||
|
||||
// TODO maybe move helper functions to ast_helper.zig?
|
||||
|
||||
fn mkFnDecl(
|
||||
self: *Parser,
|
||||
name: Token,
|
||||
|
@ -411,17 +413,20 @@ pub const Parser = struct {
|
|||
errdefer consts.deinit();
|
||||
|
||||
_ = try self.consumeSingle(.Const);
|
||||
|
||||
_ = try self.consumeSingle(.LeftParen);
|
||||
|
||||
while (self.peek().ttype != .RightParen) {
|
||||
const const_name = try self.consumeSingle(.Identifier);
|
||||
_ = try self.consumeSingle(.Equal);
|
||||
|
||||
// const declarations dont have type, a future type system must
|
||||
// check the output type of the expression and assign it to the
|
||||
// const later on.
|
||||
|
||||
var expr = try self.parseExpr();
|
||||
try consts.append(ast.SingleConst{
|
||||
.name = const_name,
|
||||
.expr = expr.Expr,
|
||||
.expr = expr,
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -522,7 +527,7 @@ pub const Parser = struct {
|
|||
|
||||
fn parseIfStmt(self: *@This()) !*Stmt {
|
||||
_ = try self.consumeSingle(.If);
|
||||
var condition = (try self.parseExpr()).Expr;
|
||||
var condition = try self.parseExpr();
|
||||
|
||||
const then_branch = try self.parseStmtBlock();
|
||||
|
||||
|
@ -550,7 +555,7 @@ pub const Parser = struct {
|
|||
if (self.check(.LeftBrace)) {
|
||||
body = try self.parseStmtBlock();
|
||||
} else {
|
||||
expr = (try self.parseExpr()).Expr;
|
||||
expr = try self.parseExpr();
|
||||
body = try self.parseStmtBlock();
|
||||
}
|
||||
|
||||
|
@ -559,7 +564,7 @@ pub const Parser = struct {
|
|||
|
||||
fn parseReturn(self: *@This()) !*Stmt {
|
||||
const tok = try self.consumeSingle(.Return);
|
||||
const expr = (try self.parseExpr()).Expr;
|
||||
const expr = try self.parseExpr();
|
||||
return try Stmt.mkReturn(self.allocator, tok, expr);
|
||||
}
|
||||
|
||||
|
@ -567,20 +572,19 @@ pub const Parser = struct {
|
|||
_ = try self.consumeSingle(.Println);
|
||||
|
||||
_ = try self.consumeSingle(.LeftParen);
|
||||
var expr = (try self.parseExpr()).Expr;
|
||||
var expr = try self.parseExpr();
|
||||
_ = try self.consumeSingle(.RightParen);
|
||||
|
||||
return try Stmt.mkPrintln(self.allocator, expr);
|
||||
}
|
||||
|
||||
fn parseStmtExpr(self: *@This()) !*Stmt {
|
||||
var expr = (try self.parseExpr()).Expr;
|
||||
var expr = try self.parseExpr();
|
||||
return try self.mkStmtExpr(expr);
|
||||
}
|
||||
|
||||
fn parseExpr(self: *@This()) anyerror!*Node {
|
||||
var expr: *Expr = try self.parseAssignment();
|
||||
return self.mkExpr(expr);
|
||||
fn parseExpr(self: *@This()) anyerror!*Expr {
|
||||
return try self.parseAssignment();
|
||||
}
|
||||
|
||||
fn parseAssignment(self: *@This()) anyerror!*Expr {
|
||||
|
@ -784,13 +788,13 @@ pub const Parser = struct {
|
|||
if (!self.check(.RightParen)) {
|
||||
|
||||
// emulating do-while really badly
|
||||
var arg = (try self.parseExpr()).Expr;
|
||||
var arg = try self.parseExpr();
|
||||
try args.append(arg);
|
||||
|
||||
while (self.check(.Comma)) {
|
||||
_ = try self.consumeSingle(.Comma);
|
||||
|
||||
arg = (try self.parseExpr()).Expr;
|
||||
arg = try self.parseExpr();
|
||||
try args.append(arg);
|
||||
}
|
||||
}
|
||||
|
@ -817,7 +821,7 @@ pub const Parser = struct {
|
|||
// TODO check .Comma for the quick initialization {val,val,val}
|
||||
|
||||
_ = try self.consumeSingle(.Colon);
|
||||
const field_value = (try self.parseExpr()).Expr;
|
||||
const field_value = try self.parseExpr();
|
||||
|
||||
try inits.append(ast.StructInit{
|
||||
.field = field_name,
|
||||
|
@ -845,7 +849,7 @@ pub const Parser = struct {
|
|||
|
||||
.LeftParen => blk: {
|
||||
_ = try self.nextToken();
|
||||
var expr = (try self.parseExpr()).Expr;
|
||||
var expr = try self.parseExpr();
|
||||
_ = try self.consume(.RightParen, "Expected ')' after expression");
|
||||
|
||||
// for groupings, we don't want to skip tokens as we already
|
||||
|
|
Loading…
Reference in a new issue