feat(furc): introduce while loop statement

Refs: #2
This commit is contained in:
2026-06-12 23:10:26 +02:00
parent 97c01bc96c
commit 767970c69b
11 changed files with 151 additions and 24 deletions
+7 -1
View File
@@ -235,7 +235,13 @@ using compound_statement_node_p =
using compound_statement_node_r = node_r<compound_statement_node>; /**< Alias for compound_statement_node result */
class while_statement_node;
using while_statement_node_p = node_p<while_statement_node>; /**< Alias for a shared pointer to while_statement_node. */
using while_statement_node_r = node_r<while_statement_node>; /**< Alias for while_statement_node result */
} // namespace ast
} // namespace furc
#endif // FURC_AST_FWD_HPP
#endif // FURC_AST_FWD_HPP
+48 -1
View File
@@ -1,6 +1,7 @@
#ifndef FURC_AST_STATEMENT_HPP
#define FURC_AST_STATEMENT_HPP
#include "furc/ast/fwd.hpp"
#include "furc/ast/node.hpp"
#include <optional>
@@ -17,6 +18,7 @@ enum class statement_node_t {
Return, /**< Return statement */
If, /**< If statement */
Compound, /**< Compound statement */
While, /**< While loop statement. */
};
/**
@@ -191,7 +193,52 @@ private:
struct body m_body; /**< The body handle. */
};
/**
* @brief while statement AST node.
*/
class while_statement_node final : public statement_node, public abstract_node {
public:
/**
* @brief Construct a new while statement AST node.
*
* @param location Node location.
* @param body Body handle.
*/
while_statement_node(struct location location, expression_node_p&& cond, statement_node_p&& body)
: abstract_node(location), m_cond(std::move(cond)), m_body(std::move(body)) {}
public:
/**
* @brief Returns this node's condition expression.
*
* @return The condition expression.
*/
const expression_node_p& condition() const { return m_cond; }
/**
* @brief Returns this node's body handle.
*
* @return The body handle.
*/
const statement_node_p& body() const { return m_body; }
public:
/**
* @brief Returns this node's statement type.
*
* @return statement_node_t::while.
*/
statement_node_t statement_type() const override { return statement_node_t::While; }
public:
void accept(visitor& visitor) const override;
std::ostream& print(std::ostream& os) const override;
protected:
bool equal(const node& rhs) const override;
private:
expression_node_p m_cond; /**< The condition expression. */
statement_node_p m_body; /**< The body handle. */
};
} // namespace ast
} // namespace furc
#endif // FURC_AST_STATEMENT_HPP
#endif // FURC_AST_STATEMENT_HPP
+9 -1
View File
@@ -122,6 +122,14 @@ public:
*/
virtual void visit(const compound_statement_node& node) {}
/**
* @brief Visit a while_statement_node.
* @see while_statement_node
*
* @param node Node.
*/
virtual void visit(const while_statement_node& node) {}
/**
* @brief Visit an AST error.
*
@@ -133,4 +141,4 @@ public:
} // namespace ast
} // namespace furc
#endif // FURC_AST_VISITOR_HPP
#endif // FURC_AST_VISITOR_HPP
+3 -2
View File
@@ -27,6 +27,7 @@ public:
void visit(const ast::function_definition_node& funcDef) override;
void visit(const ast::return_statement_node& returnStmt) override;
void visit(const ast::if_statement_node& node) override;
void visit(const ast::while_statement_node& node) override;
void visit(const ast::compound_statement_node& node) override;
void visit(const ast::string_literal_node& node) override;
void visit(const ast::integer_literal_node& node) override;
@@ -42,7 +43,7 @@ private:
}
}
furlang::ir::block_index push_block();
furlang::ir::block_index push_block(bool validate = true);
private:
furlang::ir::module m_module;
std::unique_ptr<furlang::ir::function> m_currentFunction;
@@ -55,4 +56,4 @@ private:
} // namespace front
} // namespace furc
#endif // FURC_FRONT_IR_GENERATOR_HPP
#endif // FURC_FRONT_IR_GENERATOR_HPP
+4 -1
View File
@@ -145,6 +145,7 @@ enum class keyword_token {
Return, /**< `return` */
If, /**< `if` */
Else, /**< `else` */
While, /**< `while` */
};
static inline std::ostream& operator<<(std::ostream& os, keyword_token keyword) {
@@ -154,6 +155,7 @@ static inline std::ostream& operator<<(std::ostream& os, keyword_token keyword)
case keyword_token::Return: return os << "return";
case keyword_token::If: return os << "if";
case keyword_token::Else: return os << "else";
case keyword_token::While: return os << "while";
}
return os;
}
@@ -165,6 +167,7 @@ static inline std::string operator+(const std::string& str, keyword_token keywor
case keyword_token::Return: return str + "return";
case keyword_token::If: return str + "if";
case keyword_token::Else: return str + "else";
case keyword_token::While: return str + "while";
}
return str;
}
@@ -377,4 +380,4 @@ using token_r = furlang::result<token, token_error>; /**< Alias to a token resul
} // namespace front
} // namespace furc
#endif // FURC_FRONT_TOKEN_HPP
#endif // FURC_FRONT_TOKEN_HPP