forked from KPGPMC/furlang
refactor(AST): improve literals
Changed literal node to templated class and literals to aliases.
This commit is contained in:
+1
-31
@@ -1,5 +1,5 @@
|
||||
#include "furc/ast/declaration.hpp"
|
||||
#include "furc/ast/literal.hpp"
|
||||
#include "furc/ast/expression.hpp"
|
||||
#include "furc/ast/node.hpp"
|
||||
#include "furc/ast/program.hpp"
|
||||
#include "furc/ast/statement.hpp"
|
||||
@@ -12,36 +12,6 @@ std::ostream& operator<<(std::ostream& os, const error& error) {
|
||||
return os << error.location << ": ERROR: unknown";
|
||||
}
|
||||
|
||||
bool literal_node::equal(const node& rhs) const {
|
||||
return literal_type() == dynamic_cast<const literal_node&>(rhs).literal_type();
|
||||
}
|
||||
|
||||
void string_literal_node::accept(visitor& visitor) const {
|
||||
visitor.visit(*this);
|
||||
}
|
||||
|
||||
std::ostream& string_literal_node::print(std::ostream& os) const {
|
||||
if (m_value.has_error()) return os << m_value.error();
|
||||
return os << '"' << *m_value << '"';
|
||||
}
|
||||
|
||||
bool string_literal_node::equal(const node& rhs) const {
|
||||
return literal_node::equal(rhs) && m_value == dynamic_cast<const string_literal_node&>(rhs).m_value;
|
||||
}
|
||||
|
||||
void integer_literal_node::accept(visitor& visitor) const {
|
||||
visitor.visit(*this);
|
||||
}
|
||||
|
||||
std::ostream& integer_literal_node::print(std::ostream& os) const {
|
||||
if (m_value.has_error()) return os << m_value.error();
|
||||
return os << *m_value;
|
||||
}
|
||||
|
||||
bool integer_literal_node::equal(const node& rhs) const {
|
||||
return literal_node::equal(rhs) && m_value == dynamic_cast<const integer_literal_node&>(rhs).m_value;
|
||||
}
|
||||
|
||||
bool expression_node::equal(const node& rhs) const {
|
||||
return expression_type() == dynamic_cast<const expression_node&>(rhs).expression_type();
|
||||
}
|
||||
|
||||
@@ -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