forked from KPGPMC/furlang
Add if statement
Signed-off-by: CHatingPython <chatingpython@gmail.com>
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
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 {
|
||||
for (const auto& decl : m_declarations) {
|
||||
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;
|
||||
}
|
||||
|
||||
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
|
||||
@@ -14,9 +14,9 @@ namespace ir = furlang::ir;
|
||||
}
|
||||
|
||||
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()) {
|
||||
std::cerr << funcDef.body().error() << '\n';
|
||||
return;
|
||||
@@ -25,7 +25,7 @@ void ir_generator::visit_function_definition_node(const ast::function_definition
|
||||
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) {
|
||||
@@ -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) {
|
||||
m_currentBlock->emplace<furlang::ir::assign_instruction>(ir::operand::new_string(std::string(*node.value())),
|
||||
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");
|
||||
}
|
||||
|
||||
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
|
||||
@@ -90,6 +90,8 @@ token_handle<> lexer::next_token() {
|
||||
static std::unordered_map<std::string_view, keyword_token> s_keywords = {
|
||||
{ "func", keyword_token::Func },
|
||||
{ "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 };
|
||||
|
||||
+46
-13
@@ -1,6 +1,6 @@
|
||||
#include "furc/front/parser.hpp"
|
||||
|
||||
#include "furc/ast/declaration.hpp"
|
||||
// #include "furc/ast/declaration.hpp" // IWYU pragma: keep
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
@@ -55,7 +55,7 @@ ast::declaration_node_h parser::parse_declaration() {
|
||||
if (peek.has_error()) return peek;
|
||||
switch (peek->type) {
|
||||
case token_t::Lbrace: {
|
||||
ast::function_body_h body = parse_body();
|
||||
ast::body_h body = parse_body();
|
||||
if (body.has_error()) return 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;
|
||||
switch (tok->type) {
|
||||
case token_t::Keyword: {
|
||||
auto tok = next_token();
|
||||
if (peek_token()->type == token_t::Semicolon) {
|
||||
next_token();
|
||||
return ast::return_statement_node_h{ tok.location(), m_arena };
|
||||
}
|
||||
switch (tok->value.keyword) {
|
||||
case keyword_token::Return: {
|
||||
auto tok = next_token();
|
||||
if (peek_token()->type == token_t::Semicolon) {
|
||||
next_token();
|
||||
return ast::return_statement_node_h{ tok.location(), m_arena };
|
||||
}
|
||||
|
||||
auto value = parse_expression();
|
||||
auto err = eat_token(token_t::Semicolon);
|
||||
if (err.has_error()) return err;
|
||||
return ast::return_statement_node_h{ tok.location(), m_arena, std::move(value) };
|
||||
auto value = parse_expression();
|
||||
auto err = eat_token(token_t::Semicolon);
|
||||
if (err.has_error()) return err;
|
||||
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;
|
||||
}
|
||||
@@ -328,8 +361,8 @@ ast::expression_node_h parser::parse_expression_rhs(ast::expression_node_h&& ini
|
||||
return lhs;
|
||||
}
|
||||
|
||||
ast::function_body_h parser::parse_body() {
|
||||
ast::function_body body;
|
||||
ast::body_h parser::parse_body() {
|
||||
ast::body body;
|
||||
|
||||
token_handle<> begin = eat_token(token_t::Lbrace);
|
||||
if (begin.has_error()) return begin;
|
||||
|
||||
+1
-1
@@ -6,7 +6,7 @@
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
|
||||
auto program = parser.parse();
|
||||
|
||||
Reference in New Issue
Block a user