From 84c6795ff8915c83002ad833ee6b74a9ea6982c8 Mon Sep 17 00:00:00 2001 From: CHatingPython Date: Sat, 8 Aug 2026 00:04:04 +0200 Subject: [PATCH] feat(furc/parser): add if statement --- furc/include/furc/front/ast.hpp | 11 +++++++++++ furc/src/front/parser.cpp | 13 +++++++++++++ 2 files changed, 24 insertions(+) diff --git a/furc/include/furc/front/ast.hpp b/furc/include/furc/front/ast.hpp index 3035d99..b13c39f 100644 --- a/furc/include/furc/front/ast.hpp +++ b/furc/include/furc/front/ast.hpp @@ -52,6 +52,7 @@ public: Expression, Compound, + If, }; public: category_e category() const override { return ast_node_cat::Statement; } @@ -65,6 +66,16 @@ struct comp_stmt_node final : public stmt_node { std::vector stmts; }; +class expr_node; + +struct if_stmt_node final : public stmt_node { + stmt_type_e stmt_type() const override { return If; } + + expr_node* cond = nullptr; + stmt_node* thenBranch = nullptr; + stmt_node* elseBranch = nullptr; +}; + class decl_node : public stmt_node { public: enum decl_type_e { diff --git a/furc/src/front/parser.cpp b/furc/src/front/parser.cpp index db2badc..0756037 100644 --- a/furc/src/front/parser.cpp +++ b/furc/src/front/parser.cpp @@ -24,6 +24,19 @@ ast parser::parse() { stmt_node* parser::parse_stmt() { switch (m_lexer.peek_token().type) { case token::LBrace: return m_arena->allocate(parse_comp()); + case token::If: { + m_lexer.next_token(); + eat_token(token::LParen); + if_stmt_node node; + node.cond = parse_expr(); + eat_token(token::RParen); + node.thenBranch = parse_stmt(); + if (m_lexer.peek_token().type == token::Else) { + m_lexer.next_token(); + node.elseBranch = parse_stmt(); + } + return m_arena->allocate(std::move(node)); + } default: break; }