73 lines
1.3 KiB
C++
73 lines
1.3 KiB
C++
#include "Value.hpp"
|
|
#include "Fun.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 }
|
|
{
|
|
}
|
|
|
|
/*explicit*/ Value::Value(std::shared_ptr<Fun> value, SrcLoc loc)
|
|
: m_type { value->type() }
|
|
, m_fun_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";
|
|
}
|
|
|
|
if (m_type->equals(TY_FUN))
|
|
{
|
|
return "function: " + m_fun_val->type()->string();
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|