feat(furc/parser): add while statement
This commit is contained in:
@@ -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; }
|
||||
|
||||
|
||||
@@ -46,6 +46,15 @@ stmt_node* parser::parse_stmt() {
|
||||
}
|
||||
return m_arena->allocate<if_stmt_node>(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<while_stmt_node>(std::move(node));
|
||||
}
|
||||
default: break;
|
||||
}
|
||||
|
||||
|
||||
+9
-1
@@ -5,7 +5,15 @@
|
||||
int main(void) {
|
||||
furlang::arena arena;
|
||||
|
||||
furc::lexer lexer = { "<AK>", "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 = { "<AK>", content };
|
||||
furc::parser parser = { std::move(lexer), arena };
|
||||
|
||||
auto program = parser.parse();
|
||||
|
||||
Reference in New Issue
Block a user