diff --git a/furc/include/furc/front/ast.hpp b/furc/include/furc/front/ast.hpp index 96b522b..7ec2755 100644 --- a/furc/include/furc/front/ast.hpp +++ b/furc/include/furc/front/ast.hpp @@ -137,6 +137,7 @@ public: Group, BinaryOp, UnaryOp, + If, }; public: 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; }; +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 { public: enum lit_type_e { diff --git a/furc/src/front/parser.cpp b/furc/src/front/parser.cpp index 6422a3d..4980ba3 100644 --- a/furc/src/front/parser.cpp +++ b/furc/src/front/parser.cpp @@ -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(std::string(token.value.string)); @@ -180,6 +180,16 @@ expr_node* parser::parse_expr_primary() { eat_token(token::RParen); return m_arena->allocate(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(std::move(ifExpr)); + } case token::Integer: { return m_arena->allocate(int_lit_node(token.value.integer)); } diff --git a/furc/src/main.cpp b/furc/src/main.cpp index 8f12bf6..8c08984 100644 --- a/furc/src/main.cpp +++ b/furc/src/main.cpp @@ -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; } )";