forked from KPGPMC/furlang
feat(furc): add compound statement
This one: { [statement]... }
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
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 {
|
||||
for (const auto& decl : m_declarations) {
|
||||
if (decl.has_error()) {
|
||||
|
||||
@@ -60,6 +60,12 @@ void ir_generator::visit_if_statement_node(const ast::if_statement_node& node) {
|
||||
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) {
|
||||
m_currentBlock->emplace<furlang::ir::assign_instruction>(ir::operand::new_string(std::string(*node.value())),
|
||||
ir::operand::new_reg(m_registerCounter++));
|
||||
|
||||
@@ -89,6 +89,7 @@ ast::declaration_node_h parser::parse_declaration() {
|
||||
ast::statement_node_h parser::parse_statement() {
|
||||
const auto& tok = peek_token();
|
||||
if (tok.has_error()) return tok;
|
||||
auto location = tok.location();
|
||||
switch (tok->type) {
|
||||
case token_t::Keyword: {
|
||||
switch (tok->value.keyword) {
|
||||
@@ -121,20 +122,21 @@ ast::statement_node_h parser::parse_statement() {
|
||||
peek_token()->value.keyword == keyword_token::Else) {
|
||||
next_token();
|
||||
|
||||
return ast::if_statement_node_h{ tok.location(),
|
||||
return ast::if_statement_node_h{ 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) };
|
||||
return ast::if_statement_node_h{ location, m_arena, std::move(cond), std::move(then) };
|
||||
}
|
||||
case keyword_token::None:
|
||||
case keyword_token::Func:
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
case token_t::Lbrace: return ast::compound_statement_node_h{ location, m_arena, parse_body() };
|
||||
default: break;
|
||||
}
|
||||
|
||||
|
||||
+16
-2
@@ -6,7 +6,21 @@
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
|
||||
auto program = parser.parse();
|
||||
@@ -31,4 +45,4 @@ int main(void) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif // LIBFURC
|
||||
#endif // LIBFURC
|
||||
|
||||
Reference in New Issue
Block a user