From 7ebc27cab9e2f524c5eaabb7889cde69b8d32a49 Mon Sep 17 00:00:00 2001 From: CHatingPython Date: Sat, 8 Aug 2026 00:07:38 +0200 Subject: [PATCH] feat(furc/parser): add while statement --- furc/include/furc/front/ast.hpp | 8 ++++++++ furc/src/front/parser.cpp | 9 +++++++++ furc/src/main.cpp | 10 +++++++++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/furc/include/furc/front/ast.hpp b/furc/include/furc/front/ast.hpp index dd9c1f9..429f629 100644 --- a/furc/include/furc/front/ast.hpp +++ b/furc/include/furc/front/ast.hpp @@ -53,6 +53,7 @@ public: Compound, If, + While, Return, }; public: @@ -77,6 +78,13 @@ struct if_stmt_node final : public stmt_node { stmt_node* elseBranch = nullptr; }; +struct while_stmt_node final : public stmt_node { + stmt_type_e stmt_type() const override { return While; } + + expr_node* cond = nullptr; + stmt_node* body = nullptr; +}; + struct return_stmt_node final : public stmt_node { stmt_type_e stmt_type() const override { return Return; } diff --git a/furc/src/front/parser.cpp b/furc/src/front/parser.cpp index fb50b87..3bb27c6 100644 --- a/furc/src/front/parser.cpp +++ b/furc/src/front/parser.cpp @@ -46,6 +46,15 @@ stmt_node* parser::parse_stmt() { } return m_arena->allocate(std::move(node)); } + case token::While: { + m_lexer.next_token(); + eat_token(token::LParen); + while_stmt_node node; + node.cond = parse_expr(); + eat_token(token::RParen); + node.body = parse_stmt(); + return m_arena->allocate(std::move(node)); + } default: break; } diff --git a/furc/src/main.cpp b/furc/src/main.cpp index cb0cf8f..de7bc4e 100644 --- a/furc/src/main.cpp +++ b/furc/src/main.cpp @@ -5,7 +5,15 @@ int main(void) { furlang::arena arena; - furc::lexer lexer = { "", "func main(argc: u64) -> s32 { x: s32 = 10 + 67 - 6 * 7; }" }; + std::string_view content = R"( + func main(argc: u64) -> s32 { + x: s32 = 1 + 2 * 3; + if (x == 9) return 1; + else return 0; + } + )"; + + furc::lexer lexer = { "", content }; furc::parser parser = { std::move(lexer), arena }; auto program = parser.parse();