refactor(AST): improve literals
Changed literal node to templated class and literals to aliases.
This commit is contained in:
@@ -72,12 +72,12 @@ void ir_generator::visit(const ast::compound_statement_node& node) {
|
||||
}
|
||||
|
||||
void ir_generator::visit(const ast::string_literal_node& node) {
|
||||
push<furlang::ir::assign_instruction>(ir::operand::new_string(std::string(*node.value())),
|
||||
push<furlang::ir::assign_instruction>(ir::operand::new_string(node.value()),
|
||||
ir::operand::new_reg(m_registerCounter++));
|
||||
}
|
||||
|
||||
void ir_generator::visit(const ast::integer_literal_node& node) {
|
||||
push<furlang::ir::assign_instruction>(ir::operand::new_integer(*node.value()),
|
||||
push<furlang::ir::assign_instruction>(ir::operand::new_integer(node.value()),
|
||||
ir::operand::new_reg(m_registerCounter++));
|
||||
}
|
||||
|
||||
|
||||
+10
-21
@@ -160,25 +160,6 @@ ast::expression_node_r parser::parse_expression(std::uint32_t precedence) {
|
||||
return parse_expression_rhs(parse_expression_unary(precedence), precedence);
|
||||
}
|
||||
|
||||
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 ast::literal_node_r(ast::error{ tok.error().location });
|
||||
return m_arena.allocate_shared<ast::string_literal_node>(tok->location, (*tok)->string);
|
||||
}
|
||||
case token_t::Integer: {
|
||||
auto tok = next_token();
|
||||
if (tok.has_error()) return ast::literal_node_r(ast::error{ tok.error().location });
|
||||
return m_arena.allocate_shared<ast::integer_literal_node>(tok->location, (*tok)->integer);
|
||||
}
|
||||
default: break;
|
||||
}
|
||||
|
||||
return ast::literal_node_r(ast::error{ tok->location });
|
||||
}
|
||||
|
||||
ast::expression_node_r parser::parse_expression_primary() {
|
||||
const auto& tok = peek_token();
|
||||
switch (tok->type) {
|
||||
@@ -194,9 +175,17 @@ ast::expression_node_r parser::parse_expression_primary() {
|
||||
if (err.has_error()) return ast::expression_node_r(ast::error{ err.error().location });
|
||||
return node;
|
||||
}
|
||||
case token_t::String: {
|
||||
auto tok = next_token();
|
||||
if (tok.has_error()) return ast::expression_node_r(ast::error{ tok.error().location });
|
||||
return m_arena.allocate_shared<ast::string_literal_node>(tok->location, (*tok)->string);
|
||||
}
|
||||
case token_t::Integer: {
|
||||
auto tok = next_token();
|
||||
if (tok.has_error()) return ast::expression_node_r(ast::error{ tok.error().location });
|
||||
return m_arena.allocate_shared<ast::integer_literal_node>(tok->location, (*tok)->integer);
|
||||
}
|
||||
default: {
|
||||
auto literal = parse_literal();
|
||||
if (literal.has_value()) return std::move(*literal);
|
||||
return ast::expression_node_r(ast::error{ tok->location });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user