Add if statement

Signed-off-by: CHatingPython <chatingpython@gmail.com>
This commit is contained in:
CHatingPython
2026-05-31 17:29:17 +02:00
committed by CHatingPython
parent 646d185d40
commit 91ef7f01a4
12 changed files with 170 additions and 44 deletions
+3 -18
View File
@@ -5,8 +5,6 @@
#include "furc/ast/statement.hpp" #include "furc/ast/statement.hpp"
#include "furc/front/token.hpp" #include "furc/front/token.hpp"
#include <vector>
namespace furc { namespace furc {
namespace ast { namespace ast {
@@ -45,27 +43,14 @@ protected:
front::token m_name; front::token m_name;
}; };
struct function_body {
location begin, end;
std::vector<node_handle<statement_node>> statements;
bool operator==(const function_body& rhs) const {
return begin == rhs.begin && end == rhs.end && statements == rhs.statements;
}
bool operator!=(const function_body& rhs) const { return !this->operator==(rhs); }
};
using function_body_h = handle<ast::function_body>;
class function_definition_node : public function_declaration_node { class function_definition_node : public function_declaration_node {
public: public:
function_definition_node(front::token name, function_body_h&& body) function_definition_node(front::token name, body_h&& body)
: function_declaration_node(name), m_body(std::move(body)) {} : function_declaration_node(name), m_body(std::move(body)) {}
public: public:
declaration_node_t declaration_type() const override { return declaration_node_t::FunctionDefinition; } declaration_node_t declaration_type() const override { return declaration_node_t::FunctionDefinition; }
const function_body_h& body() const { return m_body; } const body_h& body() const { return m_body; }
public: public:
void accept(visitor& visitor) const override; void accept(visitor& visitor) const override;
@@ -73,7 +58,7 @@ public:
protected: protected:
bool equal(const node& rhs) const override; bool equal(const node& rhs) const override;
private: private:
function_body_h m_body; body_h m_body;
}; };
} // namespace ast } // namespace ast
+18
View File
@@ -4,6 +4,7 @@
#include "furc/handle.hpp" #include "furc/handle.hpp"
#include <string> #include <string>
#include <vector>
namespace furc { namespace furc {
namespace ast { namespace ast {
@@ -38,6 +39,21 @@ using binop_expression_node_h = node_handle<binop_expression_node>;
class var_assign_expression_node; class var_assign_expression_node;
using var_assign_expression_node_h = node_handle<var_assign_expression_node>; using var_assign_expression_node_h = node_handle<var_assign_expression_node>;
struct body {
location begin, end;
std::vector<statement_node_h> statements;
bool operator==(const body& rhs) const {
return begin == rhs.begin && end == rhs.end && statements == rhs.statements;
}
bool operator!=(const body& rhs) const { return !this->operator==(rhs); }
friend std::ostream& operator<<(std::ostream&, const body&);
};
using body_h = handle<body>;
class function_declaration_node; class function_declaration_node;
using function_declaration_node_h = node_handle<function_declaration_node>; using function_declaration_node_h = node_handle<function_declaration_node>;
class function_definition_node; class function_definition_node;
@@ -45,6 +61,8 @@ using function_definition_node_h = node_handle<function_definition_node>;
class return_statement_node; class return_statement_node;
using return_statement_node_h = node_handle<return_statement_node>; using return_statement_node_h = node_handle<return_statement_node>;
class if_statement_node;
using if_statement_node_h = node_handle<if_statement_node>;
} // namespace ast } // namespace ast
} // namespace furc } // namespace furc
+29 -3
View File
@@ -10,6 +10,7 @@ enum class statement_node_t {
Expression, Expression,
Declaration, Declaration,
Return, Return,
If,
}; };
class statement_node : public node { class statement_node : public node {
@@ -25,10 +26,10 @@ class return_statement_node : public statement_node {
public: public:
return_statement_node() = default; return_statement_node() = default;
return_statement_node(node_handle<expression_node>&& value) return_statement_node(expression_node_h&& value)
: m_value(std::move(value)) {} : m_value(std::move(value)) {}
public: public:
node_handle<expression_node> value() const { return m_value; } expression_node_h value() const { return m_value; }
public: public:
statement_node_t statement_type() const override { return statement_node_t::Return; } statement_node_t statement_type() const override { return statement_node_t::Return; }
public: public:
@@ -38,7 +39,32 @@ public:
protected: protected:
bool equal(const node& rhs) const override; bool equal(const node& rhs) const override;
private: private:
node_handle<expression_node> m_value; expression_node_h m_value;
};
class if_statement_node : public statement_node {
public:
if_statement_node(expression_node_h&& cond, statement_node_h&& then)
: m_cond(std::move(cond)), m_then(std::move(then)) {}
if_statement_node(expression_node_h&& cond, statement_node_h&& then, statement_node_h&& elze)
: m_cond(std::move(cond)), m_then(std::move(then)), m_else(std::move(elze)) {}
public:
expression_node_h cond() const { return m_cond; }
const statement_node_h& then() const { return m_then; }
const statement_node_h& elze() const { return m_else; }
public:
statement_node_t statement_type() const override { return statement_node_t::If; }
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_h m_cond;
statement_node_h m_then;
statement_node_h m_else;
}; };
} // namespace ast } // namespace ast
+1
View File
@@ -25,6 +25,7 @@ public:
virtual void visit_function_declaration_node(const function_declaration_node&) {} virtual void visit_function_declaration_node(const function_declaration_node&) {}
virtual void visit_function_definition_node(const function_definition_node&) {} virtual void visit_function_definition_node(const function_definition_node&) {}
virtual void visit_return_statement_node(const return_statement_node&) {} virtual void visit_return_statement_node(const return_statement_node&) {}
virtual void visit_if_statement_node(const if_statement_node&) {}
virtual void visit_error(const node_handle<node>& handle) {} virtual void visit_error(const node_handle<node>& handle) {}
}; };
+9 -5
View File
@@ -17,13 +17,14 @@ public:
ir_generator(ir_generator&&) = default; ir_generator(ir_generator&&) = default;
ir_generator& operator=(ir_generator&&) = default; ir_generator& operator=(ir_generator&&) = default;
ir_generator(const ir_generator&) = default; ir_generator(const ir_generator&) = delete;
ir_generator& operator=(const ir_generator&) = default; ir_generator& operator=(const ir_generator&) = delete;
public: public:
furlang::ir::module&& move_module() { return std::move(m_module); } furlang::ir::module&& move_module() { return std::move(m_module); }
public: public:
void visit_function_definition_node(const ast::function_definition_node& funcDef) override; void visit_function_definition_node(const ast::function_definition_node& funcDef) override;
void visit_return_statement_node(const ast::return_statement_node& returnStmt) override; void visit_return_statement_node(const ast::return_statement_node& returnStmt) override;
void visit_if_statement_node(const ast::if_statement_node& node) override;
void visit_string_literal_node(const ast::string_literal_node& node) override; void visit_string_literal_node(const ast::string_literal_node& node) override;
void visit_integer_literal_node(const ast::integer_literal_node& node) override; void visit_integer_literal_node(const ast::integer_literal_node& node) override;
void visit_var_read_expression_node(const ast::var_read_expression_node& node) override; void visit_var_read_expression_node(const ast::var_read_expression_node& node) override;
@@ -31,9 +32,12 @@ public:
void visit_binop_expression_node(const ast::binop_expression_node& node) override; void visit_binop_expression_node(const ast::binop_expression_node& node) override;
void visit_var_assign_expression_node(const ast::var_assign_expression_node& node) override; void visit_var_assign_expression_node(const ast::var_assign_expression_node& node) override;
private: private:
furlang::ir::module m_module; furlang::ir::block_index push_block();
std::shared_ptr<furlang::ir::block> m_currentBlock; private:
std::uint32_t m_registerCounter = 0; furlang::ir::module m_module;
std::unique_ptr<furlang::ir::function> m_currentFunction;
std::shared_ptr<furlang::ir::block> m_currentBlock;
std::uint32_t m_registerCounter = 0;
std::unordered_map<std::string_view, std::uint32_t> m_variableMap; std::unordered_map<std::string_view, std::uint32_t> m_variableMap;
}; };
+1 -1
View File
@@ -36,7 +36,7 @@ private:
ast::expression_node_h parse_expression_unary(std::uint32_t precedence); ast::expression_node_h parse_expression_unary(std::uint32_t precedence);
ast::expression_node_h parse_expression_rhs(ast::expression_node_h&& init, std::uint32_t precedence); ast::expression_node_h parse_expression_rhs(ast::expression_node_h&& init, std::uint32_t precedence);
ast::function_body_h parse_body(); ast::body_h parse_body();
private: private:
token_handle<> next_token(); token_handle<> next_token();
const token_handle<>& peek_token(); const token_handle<>& peek_token();
+8
View File
@@ -134,6 +134,8 @@ enum class keyword_token {
None, None,
Func, Func,
Return, Return,
If,
Else,
}; };
static inline std::ostream& operator<<(std::ostream& os, keyword_token keyword) { static inline std::ostream& operator<<(std::ostream& os, keyword_token keyword) {
@@ -141,7 +143,10 @@ static inline std::ostream& operator<<(std::ostream& os, keyword_token keyword)
case keyword_token::None: return os << "none"; case keyword_token::None: return os << "none";
case keyword_token::Func: return os << "func"; case keyword_token::Func: return os << "func";
case keyword_token::Return: return os << "return"; case keyword_token::Return: return os << "return";
case keyword_token::If: return os << "if";
case keyword_token::Else: return os << "else";
} }
return os;
} }
static inline std::string operator+(const std::string& str, keyword_token keyword) { static inline std::string operator+(const std::string& str, keyword_token keyword) {
@@ -149,7 +154,10 @@ static inline std::string operator+(const std::string& str, keyword_token keywor
case keyword_token::None: return str + "none"; case keyword_token::None: return str + "none";
case keyword_token::Func: return str + "func"; case keyword_token::Func: return str + "func";
case keyword_token::Return: return str + "return"; case keyword_token::Return: return str + "return";
case keyword_token::If: return str + "if";
case keyword_token::Else: return str + "else";
} }
return str;
} }
using integer_token = std::uint64_t; using integer_token = std::uint64_t;
+24
View File
@@ -188,6 +188,22 @@ bool return_statement_node::equal(const node& rhs) const {
return statement_node::equal(rhs) && m_value == reinterpret_cast<const return_statement_node&>(rhs).m_value; return statement_node::equal(rhs) && m_value == reinterpret_cast<const return_statement_node&>(rhs).m_value;
} }
void if_statement_node::accept(visitor& visitor) const {
visitor.visit_if_statement_node(*this);
}
std::ostream& if_statement_node::print(std::ostream& os) const {
os << "if " << *m_cond << ", then:\n";
os << m_then;
if (m_else.present()) os << m_else;
return os;
}
bool if_statement_node::equal(const node& rhsNode) const {
const auto& rhs = reinterpret_cast<const if_statement_node&>(rhsNode);
return statement_node::equal(rhs) && m_cond == rhs.m_cond && m_then == rhs.m_then && m_else == rhs.m_else;
}
void program_node::accept(visitor& visitor) const { void program_node::accept(visitor& visitor) const {
for (const auto& decl : m_declarations) { for (const auto& decl : m_declarations) {
if (decl.has_error()) { if (decl.has_error()) {
@@ -210,4 +226,12 @@ bool program_node::equal(const node& rhs) const {
return m_declarations == reinterpret_cast<const program_node&>(rhs).m_declarations; return m_declarations == reinterpret_cast<const program_node&>(rhs).m_declarations;
} }
std::ostream& operator<<(std::ostream& os, const body& body) {
os << "body:";
for (const auto& stmt : body.statements) {
os << '\n' << stmt;
}
return os;
}
} // namespace furc::ast } // namespace furc::ast
+28 -3
View File
@@ -14,9 +14,9 @@ namespace ir = furlang::ir;
} }
void ir_generator::visit_function_definition_node(const ast::function_definition_node& funcDef) { void ir_generator::visit_function_definition_node(const ast::function_definition_node& funcDef) {
auto func = std::make_unique<furlang::ir::function>(std::string(funcDef.name()->string)); m_currentFunction = std::make_unique<furlang::ir::function>(std::string(funcDef.name()->string));
m_currentBlock = func->push(); push_block();
if (funcDef.body().has_error()) { if (funcDef.body().has_error()) {
std::cerr << funcDef.body().error() << '\n'; std::cerr << funcDef.body().error() << '\n';
return; return;
@@ -25,7 +25,7 @@ void ir_generator::visit_function_definition_node(const ast::function_definition
stmt->accept(*this); stmt->accept(*this);
} }
m_module.push(std::move(func)); m_module.push(std::move(m_currentFunction));
} }
void ir_generator::visit_return_statement_node(const ast::return_statement_node& returnStmt) { void ir_generator::visit_return_statement_node(const ast::return_statement_node& returnStmt) {
@@ -41,6 +41,25 @@ void ir_generator::visit_return_statement_node(const ast::return_statement_node&
} }
} }
void ir_generator::visit_if_statement_node(const ast::if_statement_node& node) {
node.cond()->accept(*this);
std::uint32_t cond = m_registerCounter - 1;
m_currentBlock->emplace<ir::branch_cond_instruction>(ir::operand::new_reg(cond),
m_currentFunction->blocks().size(),
m_currentFunction->blocks().size() + 1);
push_block(); // then block
node.then()->accept(*this);
if (node.elze().present()) {
m_currentBlock->emplace<ir::branch_instruction>(m_currentFunction->blocks().size() + 1);
push_block(); // else block
node.elze()->accept(*this);
}
push_block();
}
void ir_generator::visit_string_literal_node(const ast::string_literal_node& node) { void ir_generator::visit_string_literal_node(const ast::string_literal_node& node) {
m_currentBlock->emplace<furlang::ir::assign_instruction>(ir::operand::new_string(std::string(*node.value())), m_currentBlock->emplace<furlang::ir::assign_instruction>(ir::operand::new_string(std::string(*node.value())),
ir::operand::new_reg(m_registerCounter++)); ir::operand::new_reg(m_registerCounter++));
@@ -93,4 +112,10 @@ void ir_generator::visit_var_assign_expression_node(const ast::var_assign_expres
throw std::runtime_error("unimplemented"); throw std::runtime_error("unimplemented");
} }
furlang::ir::block_index ir_generator::push_block() {
ir::block_index index = m_currentFunction->blocks().size();
m_currentBlock = m_currentFunction->push();
return index;
}
} // namespace furc::front } // namespace furc::front
+2
View File
@@ -90,6 +90,8 @@ token_handle<> lexer::next_token() {
static std::unordered_map<std::string_view, keyword_token> s_keywords = { static std::unordered_map<std::string_view, keyword_token> s_keywords = {
{ "func", keyword_token::Func }, { "func", keyword_token::Func },
{ "return", keyword_token::Return }, { "return", keyword_token::Return },
{ "if", keyword_token::If },
{ "else", keyword_token::Else },
}; };
if (auto it = s_keywords.find(value); it != s_keywords.end()) return { location, it->second, value }; if (auto it = s_keywords.find(value); it != s_keywords.end()) return { location, it->second, value };
+46 -13
View File
@@ -1,6 +1,6 @@
#include "furc/front/parser.hpp" #include "furc/front/parser.hpp"
#include "furc/ast/declaration.hpp" // #include "furc/ast/declaration.hpp" // IWYU pragma: keep
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
@@ -55,7 +55,7 @@ ast::declaration_node_h parser::parse_declaration() {
if (peek.has_error()) return peek; if (peek.has_error()) return peek;
switch (peek->type) { switch (peek->type) {
case token_t::Lbrace: { case token_t::Lbrace: {
ast::function_body_h body = parse_body(); ast::body_h body = parse_body();
if (body.has_error()) return body; if (body.has_error()) return body;
return ast::function_definition_node_h{ first.location(), m_arena, *name, std::move(body) }; return ast::function_definition_node_h{ first.location(), m_arena, *name, std::move(body) };
} }
@@ -91,16 +91,49 @@ ast::statement_node_h parser::parse_statement() {
if (tok.has_error()) return tok; if (tok.has_error()) return tok;
switch (tok->type) { switch (tok->type) {
case token_t::Keyword: { case token_t::Keyword: {
auto tok = next_token(); switch (tok->value.keyword) {
if (peek_token()->type == token_t::Semicolon) { case keyword_token::Return: {
next_token(); auto tok = next_token();
return ast::return_statement_node_h{ tok.location(), m_arena }; if (peek_token()->type == token_t::Semicolon) {
} next_token();
return ast::return_statement_node_h{ tok.location(), m_arena };
}
auto value = parse_expression(); auto value = parse_expression();
auto err = eat_token(token_t::Semicolon); auto err = eat_token(token_t::Semicolon);
if (err.has_error()) return err; if (err.has_error()) return err;
return ast::return_statement_node_h{ tok.location(), m_arena, std::move(value) }; return ast::return_statement_node_h{ tok.location(), m_arena, std::move(value) };
}
case keyword_token::If: {
auto tok = next_token();
auto err = eat_token(token_t::Lparen);
if (err.has_error()) return err;
auto cond = parse_expression();
err = eat_token(token_t::Rparen);
if (err.has_error()) return err;
auto then = parse_statement();
if (then.has_error()) return then;
if (peek_token().present() && peek_token()->type == token_t::Keyword &&
peek_token()->value.keyword == keyword_token::Else) {
next_token();
return ast::if_statement_node_h{ tok.location(),
m_arena,
std::move(cond),
std::move(then),
std::move(parse_statement()) };
}
return ast::if_statement_node_h{ tok.location(), m_arena, std::move(cond), std::move(then) };
}
case keyword_token::None:
case keyword_token::Func:
default: break;
}
} }
default: break; default: break;
} }
@@ -328,8 +361,8 @@ ast::expression_node_h parser::parse_expression_rhs(ast::expression_node_h&& ini
return lhs; return lhs;
} }
ast::function_body_h parser::parse_body() { ast::body_h parser::parse_body() {
ast::function_body body; ast::body body;
token_handle<> begin = eat_token(token_t::Lbrace); token_handle<> begin = eat_token(token_t::Lbrace);
if (begin.has_error()) return begin; if (begin.has_error()) return begin;
+1 -1
View File
@@ -6,7 +6,7 @@
#include <iostream> #include <iostream>
int main(void) { int main(void) {
furc::front::parser parser("<TEMP>", "func main() {\n return 7 + 6 * 10;\n}"); furc::front::parser parser("<TEMP>", "func main() {\n if (1) return 7 + 6 * 10; else return 0;\n}");
furc::front::ir_generator generator; furc::front::ir_generator generator;
auto program = parser.parse(); auto program = parser.parse();