feat(furc/parser): add if expression
This commit is contained in:
@@ -137,6 +137,7 @@ public:
|
|||||||
Group,
|
Group,
|
||||||
BinaryOp,
|
BinaryOp,
|
||||||
UnaryOp,
|
UnaryOp,
|
||||||
|
If,
|
||||||
};
|
};
|
||||||
public:
|
public:
|
||||||
category_e category() const override { return ast_node_cat::Expression; }
|
category_e category() const override { return ast_node_cat::Expression; }
|
||||||
@@ -214,6 +215,14 @@ struct unary_op_expr_node final : public expr_node {
|
|||||||
unary_op_type type = Positive;
|
unary_op_type type = Positive;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct if_expr_node final : public expr_node {
|
||||||
|
expr_type_e expr_type() const override { return If; }
|
||||||
|
|
||||||
|
expr_node* cond = nullptr;
|
||||||
|
expr_node* thenExpr = nullptr;
|
||||||
|
expr_node* elseExpr = nullptr;
|
||||||
|
};
|
||||||
|
|
||||||
class lit_node : public expr_node {
|
class lit_node : public expr_node {
|
||||||
public:
|
public:
|
||||||
enum lit_type_e {
|
enum lit_type_e {
|
||||||
|
|||||||
@@ -169,7 +169,7 @@ comp_stmt_node parser::parse_comp() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
expr_node* parser::parse_expr_primary() {
|
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) {
|
switch (token.type) {
|
||||||
case token::Identifier: {
|
case token::Identifier: {
|
||||||
return m_arena->allocate<var_read_expr_node>(std::string(token.value.string));
|
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);
|
eat_token(token::RParen);
|
||||||
return m_arena->allocate<group_expr_node>(std::move(group));
|
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: {
|
case token::Integer: {
|
||||||
return m_arena->allocate<int_lit_node>(int_lit_node(token.value.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"(
|
std::string_view content = R"(
|
||||||
func main(argc: u64) -> s32 pre(arc > 1) {
|
func main(argc: u64) -> s32 pre(arc > 1) {
|
||||||
x: s32 = 1 + 2 * 3;
|
x: s32 = 1 + 2 * 3;
|
||||||
if (x == 9) return 1;
|
return if (x == 9) 1 else 0;
|
||||||
else return 0;
|
|
||||||
}
|
}
|
||||||
)";
|
)";
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user