2024-03-07 09:15:11 +01:00
|
|
|
#include "elna/source/result.hpp"
|
2024-02-22 21:29:25 +01:00
|
|
|
|
2024-03-07 09:15:11 +01:00
|
|
|
namespace elna::source
|
2024-03-03 13:11:39 +01:00
|
|
|
{
|
2024-03-30 00:21:58 +01:00
|
|
|
error::error(const std::filesystem::path& path, const position position)
|
|
|
|
: m_position(position), m_path(path)
|
2024-02-22 21:29:25 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-03-03 13:11:39 +01:00
|
|
|
std::size_t error::line() const noexcept
|
2024-02-22 21:29:25 +01:00
|
|
|
{
|
2024-03-09 08:36:07 +01:00
|
|
|
return this->m_position.line;
|
2024-02-22 21:29:25 +01:00
|
|
|
}
|
|
|
|
|
2024-03-03 13:11:39 +01:00
|
|
|
std::size_t error::column() const noexcept
|
2024-02-22 21:29:25 +01:00
|
|
|
{
|
2024-03-09 08:36:07 +01:00
|
|
|
return this->m_position.column;
|
2024-02-22 21:29:25 +01:00
|
|
|
}
|
2024-03-14 08:52:45 +01:00
|
|
|
|
2024-03-30 00:21:58 +01:00
|
|
|
const std::filesystem::path& error::path() const noexcept
|
|
|
|
{
|
|
|
|
return this->m_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
name_collision::name_collision(const std::string& name, const std::filesystem::path& path,
|
|
|
|
const position current, const position previous)
|
|
|
|
: error(path, current), name(name), previous(previous)
|
2024-03-14 08:52:45 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string name_collision::what() const
|
|
|
|
{
|
|
|
|
return "Name '" + name + "' was already defined";
|
|
|
|
}
|
|
|
|
|
2024-03-26 23:04:20 +01:00
|
|
|
type::type(const std::size_t byte_size)
|
|
|
|
: byte_size(byte_size)
|
2024-03-19 09:35:50 +01:00
|
|
|
{
|
2024-03-26 23:04:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
primitive_type::primitive_type(const std::string& type_name, const std::size_t byte_size)
|
|
|
|
: type(byte_size), type_name(type_name)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
symbol_table::symbol_table(std::shared_ptr<symbol_table> scope)
|
|
|
|
: outer_scope(scope)
|
|
|
|
{
|
|
|
|
if (scope == nullptr)
|
|
|
|
{
|
2024-03-29 11:01:19 +01:00
|
|
|
auto writei = std::make_shared<intrinsic_info>();
|
|
|
|
writei->parameter_infos.emplace_back(int_type);
|
|
|
|
enter("writei", writei);
|
|
|
|
|
|
|
|
auto writeb = std::make_shared<intrinsic_info>();
|
|
|
|
writeb->parameter_infos.emplace_back(boolean_type);
|
|
|
|
enter("writeb", writeb);
|
|
|
|
|
|
|
|
enter("Boolean", std::make_shared<type_info>(boolean_type));
|
|
|
|
enter("Int", std::make_shared<type_info>(int_type));
|
2024-03-26 23:04:20 +01:00
|
|
|
}
|
2024-03-19 09:35:50 +01:00
|
|
|
}
|
|
|
|
|
2024-03-14 08:52:45 +01:00
|
|
|
std::shared_ptr<info> symbol_table::lookup(const std::string& name)
|
|
|
|
{
|
|
|
|
auto entry = entries.find(name);
|
2024-03-26 23:04:20 +01:00
|
|
|
|
|
|
|
if (entry != entries.cend())
|
2024-03-14 08:52:45 +01:00
|
|
|
{
|
2024-03-26 23:04:20 +01:00
|
|
|
return entry->second;
|
2024-03-14 08:52:45 +01:00
|
|
|
}
|
2024-03-26 23:04:20 +01:00
|
|
|
if (this->outer_scope != nullptr)
|
2024-03-14 08:52:45 +01:00
|
|
|
{
|
2024-03-26 23:04:20 +01:00
|
|
|
return this->outer_scope->lookup(name);
|
2024-03-14 08:52:45 +01:00
|
|
|
}
|
2024-03-26 23:04:20 +01:00
|
|
|
return nullptr;
|
2024-03-14 08:52:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void symbol_table::enter(const std::string& name, std::shared_ptr<info> entry)
|
|
|
|
{
|
|
|
|
entries.insert_or_assign(name, entry);
|
|
|
|
}
|
|
|
|
|
2024-03-26 23:04:20 +01:00
|
|
|
std::shared_ptr<symbol_table> symbol_table::scope()
|
|
|
|
{
|
|
|
|
return this->outer_scope;
|
|
|
|
}
|
|
|
|
|
2024-03-14 08:52:45 +01:00
|
|
|
info::~info()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
info::info()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-03-26 23:04:20 +01:00
|
|
|
type_info::type_info(const class type& type)
|
|
|
|
: info(), m_type(type)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
type_info::~type_info()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
const class type& type_info::type() const noexcept
|
|
|
|
{
|
|
|
|
return m_type;
|
|
|
|
}
|
|
|
|
|
2024-03-14 08:52:45 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-03-29 11:01:19 +01:00
|
|
|
variable_info::variable_info(const class type& type)
|
|
|
|
: m_type(type)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-03-14 08:52:45 +01:00
|
|
|
variable_info::~variable_info()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-03-29 11:01:19 +01:00
|
|
|
const class type& variable_info::type() const noexcept
|
|
|
|
{
|
|
|
|
return m_type;
|
|
|
|
}
|
|
|
|
|
2024-03-26 23:04:20 +01:00
|
|
|
parameter_info::parameter_info(const class type& type)
|
|
|
|
: m_type(type)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
parameter_info::~parameter_info()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
const class type& parameter_info::type() const noexcept
|
|
|
|
{
|
|
|
|
return m_type;
|
|
|
|
}
|
|
|
|
|
2024-03-29 11:01:19 +01:00
|
|
|
intrinsic_info::~intrinsic_info()
|
2024-03-26 23:04:20 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-03-29 11:01:19 +01:00
|
|
|
std::size_t intrinsic_info::parameter_stack_size() const noexcept
|
2024-03-14 08:52:45 +01:00
|
|
|
{
|
2024-03-29 11:01:19 +01:00
|
|
|
return this->parameter_infos.size() * sizeof(std::int32_t);
|
2024-03-14 08:52:45 +01:00
|
|
|
}
|
|
|
|
|
2024-03-29 11:01:19 +01:00
|
|
|
procedure_info::procedure_info(std::shared_ptr<symbol_table> outer_scope)
|
|
|
|
: local_table(std::make_shared<symbol_table>(outer_scope))
|
2024-03-14 08:52:45 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-03-29 11:01:19 +01:00
|
|
|
procedure_info::~procedure_info()
|
2024-03-14 08:52:45 +01:00
|
|
|
{
|
|
|
|
}
|
2024-03-19 09:35:50 +01:00
|
|
|
|
2024-03-26 23:04:20 +01:00
|
|
|
std::shared_ptr<symbol_table> procedure_info::scope()
|
|
|
|
{
|
|
|
|
return local_table;
|
|
|
|
}
|
|
|
|
|
2024-03-29 11:01:19 +01:00
|
|
|
std::size_t procedure_info::stack_size() const noexcept
|
2024-03-19 09:35:50 +01:00
|
|
|
{
|
2024-03-29 11:01:19 +01:00
|
|
|
return local_stack_size + argument_stack_size;
|
2024-03-19 09:35:50 +01:00
|
|
|
}
|
2024-03-03 13:11:39 +01:00
|
|
|
}
|