feat(furc/parser): add if expression
This commit is contained in:
@@ -169,7 +169,7 @@ comp_stmt_node parser::parse_comp() {
|
||||
}
|
||||
|
||||
expr_node* parser::parse_expr_primary() {
|
||||
auto token = eat_token(token::Identifier, token::LParen, token::Integer, token::Char);
|
||||
auto token = eat_token(token::Identifier, token::LParen, token::If, token::Integer, token::Char);
|
||||
switch (token.type) {
|
||||
case token::Identifier: {
|
||||
return m_arena->allocate<var_read_expr_node>(std::string(token.value.string));
|
||||
@@ -180,6 +180,16 @@ expr_node* parser::parse_expr_primary() {
|
||||
eat_token(token::RParen);
|
||||
return m_arena->allocate<group_expr_node>(std::move(group));
|
||||
}
|
||||
case token::If: {
|
||||
if_expr_node ifExpr;
|
||||
eat_token(token::LParen);
|
||||
ifExpr.cond = parse_expr();
|
||||
eat_token(token::RParen);
|
||||
ifExpr.thenExpr = parse_expr();
|
||||
eat_token(token::Else);
|
||||
ifExpr.elseExpr = parse_expr();
|
||||
return m_arena->allocate<if_expr_node>(std::move(ifExpr));
|
||||
}
|
||||
case token::Integer: {
|
||||
return m_arena->allocate<int_lit_node>(int_lit_node(token.value.integer));
|
||||
}
|
||||
|
||||
+1
-2
@@ -8,8 +8,7 @@ int main(void) {
|
||||
std::string_view content = R"(
|
||||
func main(argc: u64) -> s32 pre(arc > 1) {
|
||||
x: s32 = 1 + 2 * 3;
|
||||
if (x == 9) return 1;
|
||||
else return 0;
|
||||
return if (x == 9) 1 else 0;
|
||||
}
|
||||
)";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user