refactor(parser): change node handles to node results

Refs: #1
This commit is contained in:
2026-06-03 11:57:09 +02:00
parent a1b5726271
commit af982c11ed
11 changed files with 403 additions and 401 deletions
+5 -4
View File
@@ -77,6 +77,7 @@ void unaryop_expression_node::accept(visitor& visitor) const {
}
std::ostream& unaryop_expression_node::print(std::ostream& os) const {
if (!m_node.has_value()) return os;
switch (m_type) {
case unaryop_expression_node_t::Positive:
case unaryop_expression_node_t::Negative:
@@ -184,7 +185,7 @@ void return_statement_node::accept(visitor& visitor) const {
std::ostream& return_statement_node::print(std::ostream& os) const {
os << "return statement";
if (m_value.present()) return os << ' ' << *m_value;
if (m_value.has_value()) return os << ' ' << *m_value.value();
return os;
}
@@ -199,7 +200,7 @@ void if_statement_node::accept(visitor& visitor) const {
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;
if (m_else.has_value()) os << *m_else.value();
return os;
}
@@ -223,9 +224,9 @@ bool compound_statement_node::equal(const node& rhs) const {
void program_node::accept(visitor& visitor) const {
for (const auto& decl : m_declarations) {
if (decl.has_error()) {
visitor.visit_error(decl);
visitor.visit_error(decl.error());
} else {
decl->accept(visitor);
decl.value()->accept(visitor);
}
}
}
+18 -15
View File
@@ -22,7 +22,7 @@ void ir_generator::visit(const ast::function_definition_node& funcDef) {
return;
}
for (const auto& stmt : funcDef.body()->statements) {
stmt->accept(*this);
stmt.value()->accept(*this);
}
m_currentBlock->emplace<ir::return_instruction>();
@@ -30,12 +30,15 @@ void ir_generator::visit(const ast::function_definition_node& funcDef) {
}
void ir_generator::visit(const ast::return_statement_node& returnStmt) {
if (returnStmt.value().has_error()) {
std::cerr << returnStmt.value() << '\n';
if (!returnStmt.value().has_value()) return;
auto value = returnStmt.value().value();
if (value.has_error()) {
std::cerr << value.error() << '\n';
return;
}
if (returnStmt.value().present()) {
returnStmt.value()->accept(*this);
if (value.has_value()) {
value.value()->accept(*this);
push<ir::return_instruction>(ir::operand::new_reg(m_registerCounter - 1));
} else {
push<ir::return_instruction>();
@@ -43,19 +46,19 @@ void ir_generator::visit(const ast::return_statement_node& returnStmt) {
}
void ir_generator::visit(const ast::if_statement_node& node) {
node.cond()->accept(*this);
node.cond().value()->accept(*this);
ir_register cond = m_registerCounter - 1;
push<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()) {
node.then().value()->accept(*this);
if (node.elze().has_value()) {
m_currentBlock->emplace<ir::branch_instruction>(m_currentFunction->blocks().size() + 1);
push_block(); // else block
node.elze()->accept(*this);
node.elze().value().value()->accept(*this);
}
m_currentBlock->emplace<ir::branch_instruction>(m_currentFunction->blocks().size());
@@ -64,7 +67,7 @@ void ir_generator::visit(const ast::if_statement_node& node) {
void ir_generator::visit(const ast::compound_statement_node& node) {
for (const auto& stmt : node.body()->statements) {
stmt->accept(*this);
stmt.value()->accept(*this);
}
}
@@ -110,9 +113,9 @@ static inline furlang::ir::binary_op_instruction_t binary_op_instruction_t(ast::
}
void ir_generator::visit(const ast::binop_expression_node& node) {
node.lhs()->accept(*this);
node.lhs().value()->accept(*this);
ir_register lhs = m_registerCounter - 1;
node.rhs()->accept(*this);
node.rhs().value()->accept(*this);
ir_register rhs = m_registerCounter - 1;
ir_register dst = m_registerCounter++;
push<furlang::ir::binary_op_instruction>(binary_op_instruction_t(node.type()),
@@ -122,10 +125,10 @@ void ir_generator::visit(const ast::binop_expression_node& node) {
}
void ir_generator::visit(const ast::var_assign_expression_node& node) {
node.rhs()->accept(*this);
node.rhs().value()->accept(*this);
ir_register rhs = m_registerCounter - 1;
assert(node.lhs()->expression_type() == ast::expression_node_t::VarRead);
ast::var_read_expression_node_h lhs = node.lhs();
assert(node.lhs().value()->expression_type() == ast::expression_node_t::VarRead);
auto lhs = std::dynamic_pointer_cast<ast::var_read_expression_node>(node.lhs().value());
ir_register reg = 0;
if (auto it = m_variables.find(*lhs->get_name()); it != m_variables.end()) {
+56 -68
View File
@@ -30,17 +30,17 @@ parser::parser(std::string_view filename)
m_lexer = { filename, m_content };
}
ast::program_node_h parser::parse() & {
ast::program_node_r parser::parse() & {
auto program = m_arena.allocate_shared<ast::program_node>();
while (peek_token().has_value()) {
program->push(std::move(parse_declaration()));
}
return { location{ m_filename, 0, 0 }, program };
return program;
}
ast::declaration_node_h parser::parse_declaration() {
ast::declaration_node_r parser::parse_declaration() {
const auto& first = peek_token();
switch (first->type) {
case token_t::Keyword: {
@@ -48,29 +48,29 @@ ast::declaration_node_h parser::parse_declaration() {
switch ((*first)->keyword) {
case keyword_token::Func: {
auto name = eat_token(token_t::Identifier);
if (name.has_error()) return error_handle(name);
if (name.has_error()) return ast::declaration_node_r(ast::error{ name.error().location });
auto tok = eat_token(token_t::LParen);
if (tok.has_error()) return error_handle(tok);
if (tok.has_error()) return ast::declaration_node_r(ast::error{ tok.error().location });
tok = eat_token(token_t::RParen);
if (tok.has_error()) return error_handle(tok);
if (tok.has_error()) return ast::declaration_node_r(ast::error{ tok.error().location });
const auto& peek = peek_token();
if (peek.has_error()) return error_handle(peek);
if (peek.has_error()) return ast::declaration_node_r(ast::error{ peek.error().location });
switch (peek->type) {
case token_t::LBrace: {
ast::body_r body = parse_body();
if (body.has_error()) return ast::node_handle<ast::node>(body.error().location, "Body error");
return ast::function_definition_node_h{ first->location, m_arena, *name, std::move(body) };
if (body.has_error()) return ast::declaration_node_r(ast::error{ body.error().location });
return m_arena.allocate_shared<ast::function_definition_node>(*name, std::move(body));
}
case token_t::Semicolon: {
m_peekBuffer.clear();
return ast::function_declaration_node_h{ first->location, m_arena, *name };
return m_arena.allocate_shared<ast::function_declaration_node>(*name);
}
default: return { tok->location, "unexpected token "s + tok->type };
default: return ast::declaration_node_r(ast::error{ tok->location });
}
}
default: return { first->location, "unexpected keyword "s + (*first)->keyword };
default: return ast::declaration_node_r(ast::error{ first->location });
}
}
case token_t::None:
@@ -85,14 +85,14 @@ ast::declaration_node_h parser::parse_declaration() {
case token_t::Semicolon:
case token_t::Colon:
default: {
return { first->location, "unexpected token "s + first->type };
return ast::declaration_node_r(ast::error{ first->location });
}
}
}
ast::statement_node_h parser::parse_statement() {
ast::statement_node_r parser::parse_statement() {
const auto& tok = peek_token();
if (tok.has_error()) return error_handle(tok);
if (tok.has_error()) return ast::statement_node_r(ast::error{ tok.error().location });
auto location = tok->location;
switch (tok->type) {
case token_t::Keyword: {
@@ -101,104 +101,102 @@ ast::statement_node_h parser::parse_statement() {
auto tok = next_token();
if (peek_token()->type == token_t::Semicolon) {
next_token();
return ast::return_statement_node_h{ tok->location, m_arena };
return m_arena.allocate_shared<ast::return_statement_node>();
}
auto value = parse_expression();
auto err = eat_token(token_t::Semicolon);
if (err.has_error()) return error_handle(err);
return ast::return_statement_node_h{ tok->location, m_arena, std::move(value) };
if (err.has_error()) return ast::statement_node_r(ast::error{ err.error().location });
return m_arena.allocate_shared<ast::return_statement_node>(std::move(value));
}
case keyword_token::If: {
auto tok = next_token();
auto err = eat_token(token_t::LParen);
if (err.has_error()) return error_handle(err);
if (err.has_error()) return ast::statement_node_r(ast::error{ err.error().location });
auto cond = parse_expression();
err = eat_token(token_t::RParen);
if (err.has_error()) return error_handle(err);
if (err.has_error()) return ast::statement_node_r(ast::error{ err.error().location });
auto then = parse_statement();
if (then.has_error()) return then;
if (then.has_error()) return ast::statement_node_r(ast::error{ then.error().location });
if (peek_token().has_value() && peek_token()->type == token_t::Keyword &&
peek_token()->value.keyword == keyword_token::Else) {
next_token();
return ast::if_statement_node_h{ location,
m_arena,
std::move(cond),
return m_arena.allocate_shared<ast::if_statement_node>(std::move(cond),
std::move(then),
std::move(parse_statement()) };
std::move(parse_statement()));
}
return ast::if_statement_node_h{ location, m_arena, std::move(cond), std::move(then) };
return m_arena.allocate_shared<ast::if_statement_node>(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() };
case token_t::LBrace: return m_arena.allocate_shared<ast::compound_statement_node>(parse_body());
default: break;
}
auto declaration = parse_declaration();
if (declaration.present()) return std::move(declaration);
if (declaration.has_value()) return std::move(*declaration);
auto expression = parse_expression();
if (expression.present()) {
if (expression.has_value()) {
auto semi = eat_token(token_t::Semicolon);
if (semi.has_error()) return error_handle(semi);
return std::move(expression);
if (semi.has_error()) return ast::statement_node_r(ast::error{ semi.error().location });
return std::move(*expression);
}
auto token = next_token();
return { token->location, "unexpected token "s + token->type + ", expected statement, declaration or expression" };
return ast::statement_node_r(ast::error{ token->location });
}
ast::expression_node_h parser::parse_expression(std::uint32_t precedence) {
ast::expression_node_r parser::parse_expression(std::uint32_t precedence) {
return parse_expression_rhs(parse_expression_unary(precedence), precedence);
}
ast::literal_node_h parser::parse_literal() {
ast::literal_node_r parser::parse_literal() {
const auto& tok = peek_token();
switch (tok->type) {
case token_t::String: {
auto tok = next_token();
if (tok.has_error()) return error_handle(tok);
return ast::string_literal_node_h{ tok->location, m_arena, (*tok)->string };
if (tok.has_error()) return ast::literal_node_r(ast::error{ tok.error().location });
return m_arena.allocate_shared<ast::string_literal_node>((*tok)->string);
}
case token_t::Integer: {
auto tok = next_token();
if (tok.has_error()) return error_handle(tok);
return ast::integer_literal_node_h{ tok->location, m_arena, (*tok)->integer };
if (tok.has_error()) return ast::literal_node_r(ast::error{ tok.error().location });
return m_arena.allocate_shared<ast::integer_literal_node>((*tok)->integer);
}
default: break;
}
return { tok->location, "unexpected token "s + tok->type + ", expected literal" };
return ast::literal_node_r(ast::error{ tok->location });
}
ast::expression_node_h parser::parse_expression_primary() {
ast::expression_node_r parser::parse_expression_primary() {
const auto& tok = peek_token();
switch (tok->type) {
case token_t::Identifier: {
auto tok = next_token();
if (tok.has_error()) return error_handle(tok);
return ast::var_read_expression_node_h{ tok->location, m_arena, (*tok)->string };
if (tok.has_error()) return ast::expression_node_r(ast::error{ tok.error().location });
return m_arena.allocate_shared<ast::var_read_expression_node>((*tok)->string);
}
case token_t::LParen: {
auto tok = next_token();
auto node = parse_expression();
auto err = eat_token(token_t::RParen);
if (err.has_error()) return error_handle(err);
if (err.has_error()) return ast::expression_node_r(ast::error{ err.error().location });
return node;
}
default: {
auto literal = parse_literal();
if (literal.present()) return std::move(literal);
return { tok->location, "unexpected token"s + tok->type + ", expected expression or literal" };
if (literal.has_value()) return std::move(*literal);
return ast::expression_node_r(ast::error{ tok->location });
}
}
}
@@ -208,7 +206,7 @@ struct unaryop_info {
std::uint32_t precedence;
};
ast::expression_node_h parser::parse_expression_unary(std::uint32_t precedence) {
ast::expression_node_r parser::parse_expression_unary(std::uint32_t precedence) {
static std::unordered_map<token_t, unaryop_info> s_prefixes = {
{ token_t::Plus, unaryop_info{ ast::unaryop_expression_node_t::Positive, 3 } },
{ token_t::Minus, unaryop_info{ ast::unaryop_expression_node_t::Negative, 3 } },
@@ -216,7 +214,7 @@ ast::expression_node_h parser::parse_expression_unary(std::uint32_t precedence)
{ token_t::DMinus, unaryop_info{ ast::unaryop_expression_node_t::PrefixDecrement, 2 } },
};
ast::unaryop_expression_node_h result;
std::shared_ptr<ast::unaryop_expression_node> result;
while (true) {
auto it = s_prefixes.find(peek_token()->type);
if (it == s_prefixes.end()) break;
@@ -224,7 +222,7 @@ ast::expression_node_h parser::parse_expression_unary(std::uint32_t precedence)
if (current.precedence >= precedence) break;
auto token = next_token();
ast::expression_node_h expression;
ast::expression_node_r expression;
auto nextIt = s_prefixes.find(peek_token()->type);
if (nextIt != s_prefixes.end()) {
@@ -232,11 +230,11 @@ ast::expression_node_h parser::parse_expression_unary(std::uint32_t precedence)
expression = parse_expression_unary(current.precedence + 1);
}
result = { token->location, m_arena, current.type, std::move(expression) };
result = m_arena.allocate_shared<ast::unaryop_expression_node>(current.type, std::move(expression));
}
if (!result.present()) return parse_expression_primary();
if (!result->get_node().present()) result->set_node(parse_expression_primary());
if (result == nullptr) return parse_expression_primary();
if (result->get_node().has_value()) result->set_node(parse_expression_primary());
return result;
}
@@ -291,7 +289,7 @@ struct rhsop_info {
}
};
ast::expression_node_h parser::parse_expression_rhs(ast::expression_node_h&& init, std::uint32_t precedence) {
ast::expression_node_r parser::parse_expression_rhs(ast::expression_node_r&& init, std::uint32_t precedence) {
static std::unordered_map<token_t, rhsop_info> s_rhsops = {
{ token_t::Plus, rhsop_info::create(ast::binop_expression_node_t::Add, 5, associativity::Left) },
{ token_t::Minus, rhsop_info::create(ast::binop_expression_node_t::Sub, 5, associativity::Left) },
@@ -315,7 +313,7 @@ ast::expression_node_h parser::parse_expression_rhs(ast::expression_node_h&& ini
{ token_t::GreaterEq, rhsop_info::create(ast::binop_expression_node_t::GreaterEqual, 9, associativity::Left) },
};
ast::expression_node_h lhs = std::move(init);
ast::expression_node_r lhs = std::move(init);
while (peek_token().has_value()) {
auto it = s_rhsops.find(peek_token()->type);
if (it == s_rhsops.end()) return lhs;
@@ -324,7 +322,7 @@ ast::expression_node_h parser::parse_expression_rhs(ast::expression_node_h&& ini
if (current.precedence >= precedence) return lhs;
auto opToken = next_token();
ast::expression_node_h rhs;
ast::expression_node_r rhs;
if (current.type != rhsop_info_t::Unaryop) {
rhs = parse_expression_unary(current.precedence + 1); // unary prefix is always right-associative
}
@@ -343,21 +341,15 @@ ast::expression_node_h parser::parse_expression_rhs(ast::expression_node_h&& ini
switch (current.type) {
case rhsop_info_t::Unaryop:
lhs = ast::unaryop_expression_node_h{ opToken->location, m_arena, current.unary, std::move(lhs) };
lhs = m_arena.allocate_shared<ast::unaryop_expression_node>(current.unary, std::move(lhs));
break;
case rhsop_info_t::Binop:
lhs = ast::binop_expression_node_h{ opToken->location,
m_arena,
current.binary,
std::move(lhs),
std::move(rhs) };
lhs = m_arena.allocate_shared<ast::binop_expression_node>(current.binary, std::move(lhs), std::move(rhs));
break;
case rhsop_info_t::Assignment:
lhs = ast::var_assign_expression_node_h{ opToken->location,
m_arena,
current.assignment,
lhs = m_arena.allocate_shared<ast::var_assign_expression_node>(current.assignment,
std::move(lhs),
std::move(rhs) };
std::move(rhs));
break;
}
}
@@ -382,10 +374,6 @@ ast::body_r parser::parse_body() {
return body;
}
ast::node_handle<ast::node> parser::error_handle(const token_r& result) {
return { result.error().location, std::string(result.error().message) };
}
token_r parser::next_token() {
if (!m_peekBuffer.empty()) {
auto token = std::move(m_peekBuffer.back());
+29 -22
View File
@@ -7,7 +7,8 @@
#include <iostream>
int main(void) {
std::string programStr = R"(
try {
std::string programStr = R"(
func main() {
x = 5;
x -= 3;
@@ -21,30 +22,36 @@ int main(void) {
z = x + y;
}
)";
furc::front::parser parser("<TEMP>", programStr);
furc::front::ir_generator generator;
furc::front::parser parser("<TEMP>", programStr);
furc::front::ir_generator generator;
auto program = parser.parse();
if (program.has_error()) {
std::cerr << program << '\n';
}
program->accept(generator);
auto module = std::move(generator.move_module());
std::cout << "Generated IR:\n";
for (const auto& function : module.functions()) {
std::cout << function->name() << ":\n";
furlang::ir::block_index blockIndex = 0;
for (const auto& block : function->blocks()) {
std::cout << " # block " << blockIndex++ << '\n';
for (const auto& instruction : block->instructions()) {
std::cout << " " << *instruction << '\n';
}
std::cout << " " << *block->exit() << '\n';
auto programResult = parser.parse();
if (programResult.has_error()) {
std::cerr << programResult.error() << '\n';
return 1;
}
}
const auto& program = *programResult;
program->accept(generator);
return 0;
auto module = std::move(generator.move_module());
std::cout << "Generated IR:\n";
for (const auto& function : module.functions()) {
std::cout << function->name() << ":\n";
furlang::ir::block_index blockIndex = 0;
for (const auto& block : function->blocks()) {
std::cout << " # block " << blockIndex++ << '\n';
for (const auto& instruction : block->instructions()) {
std::cout << " " << *instruction << '\n';
}
std::cout << " " << *block->exit() << '\n';
}
}
return 0;
} catch (...) {
std::cerr << "Caught an exception in main!\n";
return 1;
}
}
#endif // LIBFURC