forked from KPGPMC/furlang
Add if statement
Signed-off-by: CHatingPython <chatingpython@gmail.com>
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user