elna/source/types.cc

84 lines
2.0 KiB
C++
Raw Normal View History

2024-12-27 10:51:46 +01:00
// This Source Code Form is subject to the terms of the Mozilla Public License
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at http://mozilla.org/MPL/2.0/.
2024-12-23 13:54:11 +01:00
#include <elna/source/types.h>
namespace elna
{
namespace source
{
type::type(const std::size_t byte_size)
: byte_size(byte_size)
{
}
std::size_t type::size() const noexcept
{
return this->byte_size;
}
const pointer_type *type::is_pointer_type() const
{
return nullptr;
}
primitive_type::primitive_type(const std::string& type_name, const std::size_t byte_size)
: type(byte_size), m_type_name(type_name)
{
}
std::string primitive_type::type_name() const
{
return m_type_name;
}
pointer_type::pointer_type(std::shared_ptr<const type> base_type, const std::size_t byte_size)
: type(byte_size), base_type(base_type)
{
}
const pointer_type *pointer_type::is_pointer_type() const
{
return this;
}
std::string pointer_type::type_name() const
{
return '^' + base_type->type_name();
}
procedure_type::procedure_type(std::vector<std::shared_ptr<const type>> arguments, const std::size_t byte_size)
: arguments(std::move(arguments)), type(byte_size)
{
}
std::string procedure_type::type_name() const
{
std::string result{ "proc(" };
for (const auto& argument : arguments)
{
result += argument->type_name() + ',';
}
result.at(result.size() - 1) = ')';
return result;
}
bool operator==(const type& lhs, const type& rhs) noexcept
{
auto lhs_type = lhs.type_name();
auto rhs_type = rhs.type_name();
return lhs_type == rhs_type;
}
bool operator!=(const type& lhs, const type& rhs) noexcept
{
return !(lhs == rhs);
}
const primitive_type boolean_type{ "Boolean", 1 };
const primitive_type int_type{ "Int", 4 };
}
}