Implement division
This commit is contained in:
@ -1,36 +0,0 @@
|
||||
#include "elna/source/ir.hpp"
|
||||
|
||||
#include <cassert>
|
||||
|
||||
namespace elna::source
|
||||
{
|
||||
void TransformVisitor::visit(source::definition *definition)
|
||||
{
|
||||
assert(false);
|
||||
}
|
||||
|
||||
void TransformVisitor::visit(source::bang_statement *statement)
|
||||
{
|
||||
assert(false);
|
||||
}
|
||||
|
||||
void TransformVisitor::visit(source::block *block)
|
||||
{
|
||||
assert(false);
|
||||
}
|
||||
|
||||
void TransformVisitor::visit(source::integer_literal *number)
|
||||
{
|
||||
assert(false);
|
||||
}
|
||||
|
||||
void TransformVisitor::visit(source::variable_expression *variable)
|
||||
{
|
||||
assert(false);
|
||||
}
|
||||
|
||||
void TransformVisitor::visit(source::binary_expression *binaryExpression)
|
||||
{
|
||||
assert(false);
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
#include "elna/source/lexer.hpp"
|
||||
#include <variant>
|
||||
#include <sstream>
|
||||
|
||||
namespace elna::source
|
||||
{
|
||||
@ -194,6 +195,20 @@ namespace elna::source
|
||||
|| of() == type::factor_operator;
|
||||
}
|
||||
|
||||
unexpected_character::unexpected_character(const std::string& character, const source::position position)
|
||||
: error(position), character(character)
|
||||
{
|
||||
}
|
||||
|
||||
std::string unexpected_character::what() const
|
||||
{
|
||||
std::stringstream ss{ "Unexpected character '" };
|
||||
|
||||
ss << character << "'";
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
source_result lex(const std::string& buffer)
|
||||
{
|
||||
std::vector<token> tokens;
|
||||
@ -278,10 +293,10 @@ namespace elna::source
|
||||
}
|
||||
else
|
||||
{
|
||||
return source_result("Unexpected next character", iterator.position());
|
||||
return source_result(unexpected_character{ std::string{ *iterator }, iterator.position() });
|
||||
}
|
||||
++iterator;
|
||||
}
|
||||
return source_result(tokens);
|
||||
return source_result(std::move(tokens));
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ namespace elna::source
|
||||
/**
|
||||
* AST node.
|
||||
*/
|
||||
void node::accept(ParserVisitor *)
|
||||
void node::accept(parser_visitor *)
|
||||
{
|
||||
}
|
||||
|
||||
@ -15,7 +15,7 @@ namespace elna::source
|
||||
{
|
||||
}
|
||||
|
||||
void definition::accept(ParserVisitor *visitor)
|
||||
void definition::accept(parser_visitor *visitor)
|
||||
{
|
||||
visitor->visit(this);
|
||||
}
|
||||
@ -35,7 +35,7 @@ namespace elna::source
|
||||
{
|
||||
}
|
||||
|
||||
void block::accept(ParserVisitor *visitor)
|
||||
void block::accept(parser_visitor *visitor)
|
||||
{
|
||||
visitor->visit(this);
|
||||
}
|
||||
@ -55,7 +55,7 @@ namespace elna::source
|
||||
{
|
||||
}
|
||||
|
||||
void integer_literal::accept(ParserVisitor *visitor)
|
||||
void integer_literal::accept(parser_visitor *visitor)
|
||||
{
|
||||
visitor->visit(this);
|
||||
}
|
||||
@ -70,7 +70,7 @@ namespace elna::source
|
||||
{
|
||||
}
|
||||
|
||||
void variable_expression::accept(ParserVisitor *visitor)
|
||||
void variable_expression::accept(parser_visitor *visitor)
|
||||
{
|
||||
visitor->visit(this);
|
||||
}
|
||||
@ -103,7 +103,7 @@ namespace elna::source
|
||||
}
|
||||
}
|
||||
|
||||
void binary_expression::accept(ParserVisitor *visitor)
|
||||
void binary_expression::accept(parser_visitor *visitor)
|
||||
{
|
||||
visitor->visit(this);
|
||||
}
|
||||
@ -128,7 +128,7 @@ namespace elna::source
|
||||
{
|
||||
}
|
||||
|
||||
void bang_statement::accept(ParserVisitor *visitor)
|
||||
void bang_statement::accept(parser_visitor *visitor)
|
||||
{
|
||||
visitor->visit(this);
|
||||
}
|
||||
@ -182,16 +182,16 @@ namespace elna::source
|
||||
{
|
||||
return lhs;
|
||||
}
|
||||
auto _operator = tokens->identifier()[0];
|
||||
++tokens;
|
||||
|
||||
auto rhs = parse_factor();
|
||||
if (rhs != nullptr)
|
||||
while (tokens->of() == source::token::type::factor_operator)
|
||||
{
|
||||
return std::make_unique<binary_expression>(std::move(lhs),
|
||||
auto _operator = tokens->identifier()[0];
|
||||
++tokens;
|
||||
|
||||
auto rhs = parse_factor();
|
||||
lhs = std::make_unique<binary_expression>(std::move(lhs),
|
||||
std::move(rhs), _operator);
|
||||
}
|
||||
return nullptr;
|
||||
return lhs;
|
||||
}
|
||||
|
||||
std::unique_ptr<expression> parser::parse_expression()
|
||||
@ -201,17 +201,16 @@ namespace elna::source
|
||||
{
|
||||
return term;
|
||||
}
|
||||
auto _operator = tokens->identifier()[0];
|
||||
++tokens;
|
||||
|
||||
auto rhs = parse_expression();
|
||||
|
||||
if (rhs != nullptr)
|
||||
while (tokens->of() == source::token::type::term_operator)
|
||||
{
|
||||
return std::make_unique<binary_expression>(std::move(term),
|
||||
auto _operator = tokens->identifier()[0];
|
||||
++tokens;
|
||||
|
||||
auto rhs = parse_term();
|
||||
term = std::make_unique<binary_expression>(std::move(term),
|
||||
std::move(rhs), _operator);
|
||||
}
|
||||
return nullptr;
|
||||
return term;
|
||||
}
|
||||
|
||||
std::unique_ptr<definition> parser::parse_definition()
|
||||
|
@ -2,24 +2,18 @@
|
||||
|
||||
namespace elna::source
|
||||
{
|
||||
error::error(const char *message, const source::position position) noexcept
|
||||
error::error(const position position)
|
||||
: m_position(position)
|
||||
{
|
||||
this->message = message;
|
||||
this->position = position;
|
||||
}
|
||||
|
||||
char const *error::what() const noexcept
|
||||
{
|
||||
return this->message;
|
||||
}
|
||||
|
||||
std::size_t error::line() const noexcept
|
||||
{
|
||||
return this->position.line;
|
||||
return this->m_position.line;
|
||||
}
|
||||
|
||||
std::size_t error::column() const noexcept
|
||||
{
|
||||
return this->position.column;
|
||||
return this->m_position.column;
|
||||
}
|
||||
}
|
||||
|
72
source/symboltable.cpp
Normal file
72
source/symboltable.cpp
Normal file
@ -0,0 +1,72 @@
|
||||
#include "elna/source/symboltable.hpp"
|
||||
|
||||
namespace elna::source
|
||||
{
|
||||
std::shared_ptr<info> symbol_table::lookup(const std::string& name)
|
||||
{
|
||||
auto entry = entries.find(name);
|
||||
if (entry == entries.cend())
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
else
|
||||
{
|
||||
return entry->second;
|
||||
}
|
||||
}
|
||||
|
||||
void symbol_table::enter(const std::string& name, std::shared_ptr<info> entry)
|
||||
{
|
||||
entries.insert_or_assign(name, entry);
|
||||
}
|
||||
|
||||
info::~info()
|
||||
{
|
||||
}
|
||||
|
||||
info::info()
|
||||
{
|
||||
}
|
||||
|
||||
constant_info::constant_info(const std::int32_t value)
|
||||
: m_value(value)
|
||||
{
|
||||
}
|
||||
|
||||
constant_info::~constant_info()
|
||||
{
|
||||
}
|
||||
|
||||
std::int32_t constant_info::value() const noexcept
|
||||
{
|
||||
return m_value;
|
||||
}
|
||||
|
||||
variable_info::~variable_info()
|
||||
{
|
||||
}
|
||||
|
||||
void name_analysis_visitor::visit(source::definition *definition)
|
||||
{
|
||||
}
|
||||
|
||||
void name_analysis_visitor::visit(source::bang_statement *statement)
|
||||
{
|
||||
}
|
||||
|
||||
void name_analysis_visitor::visit(source::block *block)
|
||||
{
|
||||
}
|
||||
|
||||
void name_analysis_visitor::visit(source::integer_literal *number)
|
||||
{
|
||||
}
|
||||
|
||||
void name_analysis_visitor::visit(source::variable_expression *variable)
|
||||
{
|
||||
}
|
||||
|
||||
void name_analysis_visitor::visit(source::binary_expression *expression)
|
||||
{
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user