IR generation #3
Symlink
+1
@@ -0,0 +1 @@
|
|||||||
|
build/compile_commands.json
|
||||||
@@ -11,7 +11,6 @@ namespace ast {
|
|||||||
enum class declaration_node_t {
|
enum class declaration_node_t {
|
||||||
FunctionDeclaration,
|
FunctionDeclaration,
|
||||||
FunctionDefinition,
|
FunctionDefinition,
|
||||||
Variable,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class declaration_node : public statement_node {
|
class declaration_node : public statement_node {
|
||||||
@@ -64,4 +63,4 @@ private:
|
|||||||
} // namespace ast
|
} // namespace ast
|
||||||
} // namespace furc
|
} // namespace furc
|
||||||
|
|
||||||
#endif // FURC_AST_DECLARATION_HPP
|
#endif // FURC_AST_DECLARATION_HPP
|
||||||
|
|||||||
@@ -63,6 +63,8 @@ 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;
|
class if_statement_node;
|
||||||
using if_statement_node_h = node_handle<if_statement_node>;
|
using if_statement_node_h = node_handle<if_statement_node>;
|
||||||
|
class compound_statement_node;
|
||||||
|
using compound_statement_node_h = node_handle<compound_statement_node>;
|
||||||
|
|
||||||
} // namespace ast
|
} // namespace ast
|
||||||
} // namespace furc
|
} // namespace furc
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ enum class statement_node_t {
|
|||||||
Declaration,
|
Declaration,
|
||||||
Return,
|
Return,
|
||||||
If,
|
If,
|
||||||
|
Compound,
|
||||||
};
|
};
|
||||||
|
|
||||||
class statement_node : public node {
|
class statement_node : public node {
|
||||||
@@ -67,6 +68,24 @@ private:
|
|||||||
statement_node_h m_else;
|
statement_node_h m_else;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class compound_statement_node : public statement_node {
|
||||||
|
public:
|
||||||
|
compound_statement_node(body_h&& body)
|
||||||
|
: m_body(std::move(body)) {}
|
||||||
|
public:
|
||||||
|
const body_h& body() const { return m_body; }
|
||||||
|
public:
|
||||||
|
statement_node_t statement_type() const override { return statement_node_t::Compound; }
|
||||||
|
public:
|
||||||
|
void accept(visitor& visitor) const override;
|
||||||
|
|
||||||
|
std::ostream& print(std::ostream& os) const override;
|
||||||
|
protected:
|
||||||
|
bool equal(const node& rhs) const override;
|
||||||
|
private:
|
||||||
|
body_h m_body;
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace ast
|
} // namespace ast
|
||||||
} // namespace furc
|
} // namespace furc
|
||||||
|
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ public:
|
|||||||
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_if_statement_node(const if_statement_node&) {}
|
||||||
|
virtual void visit_compound_statement_node(const compound_statement_node&) {}
|
||||||
|
|
||||||
virtual void visit_error(const node_handle<node>& handle) {}
|
virtual void visit_error(const node_handle<node>& handle) {}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ 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_if_statement_node(const ast::if_statement_node& node) override;
|
||||||
|
void visit_compound_statement_node(const ast::compound_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;
|
||||||
|
|||||||
@@ -204,6 +204,18 @@ bool if_statement_node::equal(const node& rhsNode) const {
|
|||||||
return statement_node::equal(rhs) && m_cond == rhs.m_cond && m_then == rhs.m_then && m_else == rhs.m_else;
|
return statement_node::equal(rhs) && m_cond == rhs.m_cond && m_then == rhs.m_then && m_else == rhs.m_else;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void compound_statement_node::accept(visitor& visitor) const {
|
||||||
|
visitor.visit_compound_statement_node(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ostream& compound_statement_node::print(std::ostream& os) const {
|
||||||
|
return os << m_body;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool compound_statement_node::equal(const node& rhs) const {
|
||||||
|
return statement_node::equal(rhs) && m_body == reinterpret_cast<const compound_statement_node&>(rhs).m_body;
|
||||||
|
}
|
||||||
|
|
||||||
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()) {
|
||||||
|
|||||||
@@ -60,6 +60,12 @@ void ir_generator::visit_if_statement_node(const ast::if_statement_node& node) {
|
|||||||
push_block();
|
push_block();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ir_generator::visit_compound_statement_node(const ast::compound_statement_node& node) {
|
||||||
|
for (const auto& stmt : node.body()->statements) {
|
||||||
|
stmt->accept(*this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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++));
|
||||||
|
|||||||
@@ -89,6 +89,7 @@ ast::declaration_node_h parser::parse_declaration() {
|
|||||||
ast::statement_node_h parser::parse_statement() {
|
ast::statement_node_h parser::parse_statement() {
|
||||||
const auto& tok = peek_token();
|
const auto& tok = peek_token();
|
||||||
if (tok.has_error()) return tok;
|
if (tok.has_error()) return tok;
|
||||||
|
auto location = tok.location();
|
||||||
switch (tok->type) {
|
switch (tok->type) {
|
||||||
case token_t::Keyword: {
|
case token_t::Keyword: {
|
||||||
switch (tok->value.keyword) {
|
switch (tok->value.keyword) {
|
||||||
@@ -121,20 +122,21 @@ ast::statement_node_h parser::parse_statement() {
|
|||||||
peek_token()->value.keyword == keyword_token::Else) {
|
peek_token()->value.keyword == keyword_token::Else) {
|
||||||
next_token();
|
next_token();
|
||||||
|
|
||||||
return ast::if_statement_node_h{ tok.location(),
|
return ast::if_statement_node_h{ location,
|
||||||
m_arena,
|
m_arena,
|
||||||
std::move(cond),
|
std::move(cond),
|
||||||
std::move(then),
|
std::move(then),
|
||||||
std::move(parse_statement()) };
|
std::move(parse_statement()) };
|
||||||
}
|
}
|
||||||
|
|
||||||
return ast::if_statement_node_h{ tok.location(), m_arena, std::move(cond), std::move(then) };
|
return ast::if_statement_node_h{ location, m_arena, std::move(cond), std::move(then) };
|
||||||
}
|
}
|
||||||
case keyword_token::None:
|
case keyword_token::None:
|
||||||
case keyword_token::Func:
|
case keyword_token::Func:
|
||||||
default: break;
|
default: break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
case token_t::Lbrace: return ast::compound_statement_node_h{ location, m_arena, parse_body() };
|
||||||
default: break;
|
default: break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+16
-2
@@ -6,7 +6,21 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
furc::front::parser parser("<TEMP>", "func main() {\n if (1) return 7 + 6 * 10; else return 0;\n}");
|
std::string programStr = R"(
|
||||||
|
func main() {
|
||||||
|
x = 5;
|
||||||
|
x -= 3;
|
||||||
|
if (x < 3) {
|
||||||
|
y = x * 2;
|
||||||
|
w = y;
|
||||||
|
} else {
|
||||||
|
y = x - 3;
|
||||||
|
}
|
||||||
|
w = x - y;
|
||||||
|
z = x + y;
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
furc::front::parser parser("<TEMP>", programStr);
|
||||||
furc::front::ir_generator generator;
|
furc::front::ir_generator generator;
|
||||||
|
|
||||||
auto program = parser.parse();
|
auto program = parser.parse();
|
||||||
@@ -31,4 +45,4 @@ int main(void) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // LIBFURC
|
#endif // LIBFURC
|
||||||
|
|||||||
Reference in New Issue
Block a user