roza/lib/Value.cpp

60 lines
1.1 KiB
C++

#include "Value.hpp"
namespace roza
{
/*explicit*/ Value::Value(int value, SrcLoc loc)
: m_type { std::make_shared<Type>(BaseType::TY_INT) }
, m_int_val { value }
, m_loc { loc }
{
}
/*explicit*/ Value::Value(bool value, SrcLoc loc)
: m_type { std::make_shared<Type>(BaseType::TY_BOOL) }
, m_bool_val { value }
, m_loc { loc }
{
}
/*virtual*/ Value::~Value()
{
}
std::string Value::string() const
{
if (m_type->equals(TY_INT))
{
return std::to_string(m_int_val);
}
if (m_type->equals(TY_BOOL))
{
return m_bool_val ? "true" : "false";
}
assert("cannot stringify unknown value " && 0);
}
bool Value::equals(Value const& rhs) const
{
if (!m_type->equals(*rhs.m_type))
{
return false;
}
switch (m_type->base())
{
case TY_BOOL: {
return as_bool() == rhs.as_bool();
} break;
case TY_INT: {
return as_int() == rhs.as_int();
} break;
default:
assert("value equals: base type not found" && 0);
}
}
}