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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO maybe move helper functions to ast_helper.zig?
|
||||||
|
|
||||||
fn mkFnDecl(
|
fn mkFnDecl(
|
||||||
self: *Parser,
|
self: *Parser,
|
||||||
name: Token,
|
name: Token,
|
||||||
|
@ -411,17 +413,20 @@ pub const Parser = struct {
|
||||||
errdefer consts.deinit();
|
errdefer consts.deinit();
|
||||||
|
|
||||||
_ = try self.consumeSingle(.Const);
|
_ = try self.consumeSingle(.Const);
|
||||||
|
|
||||||
_ = try self.consumeSingle(.LeftParen);
|
_ = try self.consumeSingle(.LeftParen);
|
||||||
|
|
||||||
while (self.peek().ttype != .RightParen) {
|
while (self.peek().ttype != .RightParen) {
|
||||||
const const_name = try self.consumeSingle(.Identifier);
|
const const_name = try self.consumeSingle(.Identifier);
|
||||||
_ = try self.consumeSingle(.Equal);
|
_ = 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();
|
var expr = try self.parseExpr();
|
||||||
try consts.append(ast.SingleConst{
|
try consts.append(ast.SingleConst{
|
||||||
.name = const_name,
|
.name = const_name,
|
||||||
.expr = expr.Expr,
|
.expr = expr,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -522,7 +527,7 @@ pub const Parser = struct {
|
||||||
|
|
||||||
fn parseIfStmt(self: *@This()) !*Stmt {
|
fn parseIfStmt(self: *@This()) !*Stmt {
|
||||||
_ = try self.consumeSingle(.If);
|
_ = try self.consumeSingle(.If);
|
||||||
var condition = (try self.parseExpr()).Expr;
|
var condition = try self.parseExpr();
|
||||||
|
|
||||||
const then_branch = try self.parseStmtBlock();
|
const then_branch = try self.parseStmtBlock();
|
||||||
|
|
||||||
|
@ -550,7 +555,7 @@ pub const Parser = struct {
|
||||||
if (self.check(.LeftBrace)) {
|
if (self.check(.LeftBrace)) {
|
||||||
body = try self.parseStmtBlock();
|
body = try self.parseStmtBlock();
|
||||||
} else {
|
} else {
|
||||||
expr = (try self.parseExpr()).Expr;
|
expr = try self.parseExpr();
|
||||||
body = try self.parseStmtBlock();
|
body = try self.parseStmtBlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -559,7 +564,7 @@ pub const Parser = struct {
|
||||||
|
|
||||||
fn parseReturn(self: *@This()) !*Stmt {
|
fn parseReturn(self: *@This()) !*Stmt {
|
||||||
const tok = try self.consumeSingle(.Return);
|
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);
|
return try Stmt.mkReturn(self.allocator, tok, expr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -567,20 +572,19 @@ pub const Parser = struct {
|
||||||
_ = try self.consumeSingle(.Println);
|
_ = try self.consumeSingle(.Println);
|
||||||
|
|
||||||
_ = try self.consumeSingle(.LeftParen);
|
_ = try self.consumeSingle(.LeftParen);
|
||||||
var expr = (try self.parseExpr()).Expr;
|
var expr = try self.parseExpr();
|
||||||
_ = try self.consumeSingle(.RightParen);
|
_ = try self.consumeSingle(.RightParen);
|
||||||
|
|
||||||
return try Stmt.mkPrintln(self.allocator, expr);
|
return try Stmt.mkPrintln(self.allocator, expr);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parseStmtExpr(self: *@This()) !*Stmt {
|
fn parseStmtExpr(self: *@This()) !*Stmt {
|
||||||
var expr = (try self.parseExpr()).Expr;
|
var expr = try self.parseExpr();
|
||||||
return try self.mkStmtExpr(expr);
|
return try self.mkStmtExpr(expr);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parseExpr(self: *@This()) anyerror!*Node {
|
fn parseExpr(self: *@This()) anyerror!*Expr {
|
||||||
var expr: *Expr = try self.parseAssignment();
|
return try self.parseAssignment();
|
||||||
return self.mkExpr(expr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parseAssignment(self: *@This()) anyerror!*Expr {
|
fn parseAssignment(self: *@This()) anyerror!*Expr {
|
||||||
|
@ -784,13 +788,13 @@ pub const Parser = struct {
|
||||||
if (!self.check(.RightParen)) {
|
if (!self.check(.RightParen)) {
|
||||||
|
|
||||||
// emulating do-while really badly
|
// emulating do-while really badly
|
||||||
var arg = (try self.parseExpr()).Expr;
|
var arg = try self.parseExpr();
|
||||||
try args.append(arg);
|
try args.append(arg);
|
||||||
|
|
||||||
while (self.check(.Comma)) {
|
while (self.check(.Comma)) {
|
||||||
_ = try self.consumeSingle(.Comma);
|
_ = try self.consumeSingle(.Comma);
|
||||||
|
|
||||||
arg = (try self.parseExpr()).Expr;
|
arg = try self.parseExpr();
|
||||||
try args.append(arg);
|
try args.append(arg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -817,7 +821,7 @@ pub const Parser = struct {
|
||||||
// TODO check .Comma for the quick initialization {val,val,val}
|
// TODO check .Comma for the quick initialization {val,val,val}
|
||||||
|
|
||||||
_ = try self.consumeSingle(.Colon);
|
_ = try self.consumeSingle(.Colon);
|
||||||
const field_value = (try self.parseExpr()).Expr;
|
const field_value = try self.parseExpr();
|
||||||
|
|
||||||
try inits.append(ast.StructInit{
|
try inits.append(ast.StructInit{
|
||||||
.field = field_name,
|
.field = field_name,
|
||||||
|
@ -845,7 +849,7 @@ pub const Parser = struct {
|
||||||
|
|
||||||
.LeftParen => blk: {
|
.LeftParen => blk: {
|
||||||
_ = try self.nextToken();
|
_ = try self.nextToken();
|
||||||
var expr = (try self.parseExpr()).Expr;
|
var expr = try self.parseExpr();
|
||||||
_ = try self.consume(.RightParen, "Expected ')' after expression");
|
_ = try self.consume(.RightParen, "Expected ')' after expression");
|
||||||
|
|
||||||
// for groupings, we don't want to skip tokens as we already
|
// for groupings, we don't want to skip tokens as we already
|
||||||
|
|
Loading…
Reference in a new issue