PorygonLang/src/Evaluator/EvalValues/NumericEvalValue.hpp

80 lines
2.6 KiB
C++
Raw Normal View History

#ifndef PORYGONLANG_NUMERICEVALVALUE_HPP
#define PORYGONLANG_NUMERICEVALVALUE_HPP
#include <sstream>
#include "EvalValue.hpp"
class NumericEvalValue : public EvalValue{
2019-06-17 15:43:54 +00:00
virtual const long GetIntegerValue() const = 0;
virtual const double GetFloatValue() const = 0;
public:
2019-06-17 15:43:54 +00:00
virtual const bool IsFloat() const = 0;
2019-06-13 14:26:10 +00:00
2019-06-17 15:43:54 +00:00
const TypeClass GetTypeClass() const final{
2019-06-13 14:26:10 +00:00
return TypeClass ::Number;
}
2019-06-17 15:43:54 +00:00
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{
2019-06-17 15:43:54 +00:00
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:
2019-06-17 15:43:54 +00:00
explicit IntegerEvalValue(long value) :_value(value){
}
2019-06-17 15:43:54 +00:00
const bool IsFloat() const final{
return false;
}
2019-06-17 15:43:54 +00:00
const long EvaluateInteger() const final{
return _value;
}
2019-06-17 15:43:54 +00:00
const shared_ptr<EvalValue> Clone() const final{
return make_shared<IntegerEvalValue>(_value);
}
2019-06-09 18:15:09 +00:00
2019-06-17 15:43:54 +00:00
const std::size_t GetHashCode() const final{
2019-06-09 18:15:09 +00:00
return std::hash<long>{}(_value);
}
};
class FloatEvalValue : public NumericEvalValue{
2019-06-17 15:43:54 +00:00
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:
2019-06-17 15:43:54 +00:00
explicit FloatEvalValue(double value) :_value(value){
}
2019-06-17 15:43:54 +00:00
const bool IsFloat() const final{
return true;
}
2019-06-17 15:43:54 +00:00
const double EvaluateFloat() const final{
return _value;
}
2019-06-17 15:43:54 +00:00
const shared_ptr<EvalValue> Clone() const final{
return make_shared<FloatEvalValue>(_value);
}
2019-06-09 18:15:09 +00:00
2019-06-17 15:43:54 +00:00
const std::size_t GetHashCode() const final{
2019-06-09 18:15:09 +00:00
return std::hash<double >{}(_value);
}
};
#endif //PORYGONLANG_NUMERICEVALVALUE_HPP