PorygonLang/src/Evaluator/EvalValues/NumericEvalValue.hpp

127 lines
3.5 KiB
C++
Raw Normal View History

#ifndef PORYGONLANG_NUMERICEVALVALUE_HPP
#define PORYGONLANG_NUMERICEVALVALUE_HPP
#include <sstream>
#include "EvalValue.hpp"
#include "../../Utilities/StringUtils.hpp"
namespace Porygon::Evaluation {
class NumericEvalValue : public EvalValue {
2019-07-25 15:23:54 +00:00
[[nodiscard]]
virtual long GetIntegerValue() const = 0;
2019-07-25 15:23:54 +00:00
[[nodiscard]]
virtual double GetFloatValue() const = 0;
public:
2019-07-25 15:23:54 +00:00
[[nodiscard]]
virtual bool IsFloat() const = 0;
2019-07-25 15:23:54 +00:00
[[nodiscard]]
inline TypeClass GetTypeClass() const final {
return TypeClass::Number;
}
2019-07-25 15:23:54 +00:00
shared_ptr<const NumericEvalValue> operator+(const shared_ptr<const NumericEvalValue> &b) const;
2019-07-25 15:23:54 +00:00
shared_ptr<const NumericEvalValue> operator-(const shared_ptr<const NumericEvalValue> &b) const;
2019-07-25 15:23:54 +00:00
shared_ptr<const NumericEvalValue> operator*(const shared_ptr<const NumericEvalValue> &b) const;
2019-07-25 15:23:54 +00:00
shared_ptr<const NumericEvalValue> operator/(const shared_ptr<const NumericEvalValue> &b) const;
2019-07-25 15:23:54 +00:00
shared_ptr<const BooleanEvalValue> operator<(const shared_ptr<const NumericEvalValue> &b) const;
2019-07-25 15:23:54 +00:00
shared_ptr<const BooleanEvalValue> operator<=(const shared_ptr<const NumericEvalValue> &b) const;
2019-07-25 15:23:54 +00:00
shared_ptr<const BooleanEvalValue> operator>(const shared_ptr<const NumericEvalValue> &b) const;
2019-07-25 15:23:54 +00:00
shared_ptr<const BooleanEvalValue> operator>=(const shared_ptr<const NumericEvalValue> &b) const;
2019-07-25 15:23:54 +00:00
bool operator==(const EvalValue *b) const final;
};
class IntegerEvalValue : public NumericEvalValue {
const long _value;
2019-07-25 15:23:54 +00:00
[[nodiscard]]
long GetIntegerValue() const final { return _value; }
2019-07-25 15:23:54 +00:00
[[nodiscard]]
double GetFloatValue() const final {
throw EvaluationException("Attempting to retrieve float from int eval value.");
}
public:
explicit IntegerEvalValue(long value) : _value(value) {
}
2019-07-25 15:23:54 +00:00
[[nodiscard]]
inline bool IsFloat() const final {
return false;
}
2019-07-25 15:23:54 +00:00
[[nodiscard]]
inline long EvaluateInteger() const final {
return _value;
}
2019-07-25 15:23:54 +00:00
[[nodiscard]]
inline std::u16string EvaluateString() const final{
return Utilities::StringUtils::IntToString(_value);
}
2019-07-25 15:23:54 +00:00
[[nodiscard]]
inline shared_ptr<const EvalValue> Clone() const final {
return make_shared<IntegerEvalValue>(_value);
}
2019-07-25 15:23:54 +00:00
[[nodiscard]]
inline std::size_t GetHashCode() const final {
return std::hash<long>{}(_value);
}
};
class FloatEvalValue : public NumericEvalValue {
const double _value;
2019-07-25 15:23:54 +00:00
[[nodiscard]]
inline long GetIntegerValue() const final {
throw EvaluationException("Attempting to retrieve float from int eval value.");
}
2019-07-25 15:23:54 +00:00
[[nodiscard]]
inline double GetFloatValue() const final {
return _value;
}
public:
explicit FloatEvalValue(double value) : _value(value) {
}
2019-07-25 15:23:54 +00:00
[[nodiscard]]
inline bool IsFloat() const final {
return true;
}
2019-07-25 15:23:54 +00:00
[[nodiscard]]
inline double EvaluateFloat() const final {
return _value;
}
2019-07-25 15:23:54 +00:00
[[nodiscard]]
inline shared_ptr<const EvalValue> Clone() const final {
return make_shared<FloatEvalValue>(_value);
}
2019-07-25 15:23:54 +00:00
[[nodiscard]]
inline std::size_t GetHashCode() const final {
return std::hash<double>{}(_value);
}
};
}
#endif //PORYGONLANG_NUMERICEVALVALUE_HPP