PorygonLang/src/Evaluator/EvalValues/NumericEvalValue.hpp

102 lines
2.9 KiB
C++
Raw Normal View History

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