PorygonLang/src/Evaluator/EvalValues/EvalValue.hpp

81 lines
2.0 KiB
C++
Raw Normal View History

#ifndef PORYGONLANG_EVALVALUE_HPP
#define PORYGONLANG_EVALVALUE_HPP
#include "../../ScriptType.hpp"
#include "../EvaluationException.hpp"
#include <string>
#include <sstream>
#include <memory>
class EvalValue{
public:
EvalValue() = default;
virtual ~EvalValue() = default;
2019-06-13 14:26:10 +00:00
virtual const TypeClass GetTypeClass() = 0;
virtual bool operator ==(EvalValue* b) = 0;
virtual bool operator !=(EvalValue*b){
return ! (this->operator==(b));
}
virtual shared_ptr<EvalValue> Clone() = 0;
2019-06-14 15:12:27 +00:00
virtual long EvaluateInteger() const{
throw EvaluationException("Can't evaluate this EvalValue as integer.");
}
2019-06-14 15:12:27 +00:00
virtual double EvaluateFloat() const{
throw EvaluationException("Can't evaluate this EvalValue as float.");
}
2019-06-14 15:12:27 +00:00
virtual bool EvaluateBool() const{
throw EvaluationException("Can't evaluate this EvalValue as bool.");
}
2019-06-14 15:12:27 +00:00
virtual const std::string* EvaluateString() const {
throw EvaluationException("Can't evaluate this EvalValue as string.");
}
2019-06-09 18:15:09 +00:00
virtual std::size_t GetHashCode() = 0;
virtual shared_ptr<EvalValue> IndexValue(EvalValue* val){
throw EvaluationException("Can't index this EvalValue");
}
2019-06-14 15:12:27 +00:00
virtual void SetIndexValue(EvalValue *key, shared_ptr<EvalValue> value){
throw EvaluationException("Can't index this EvalValue");
}
};
class BooleanEvalValue : public EvalValue{
2019-06-13 14:26:10 +00:00
const bool _value;
public:
2019-06-13 14:26:10 +00:00
explicit BooleanEvalValue(bool val)
: _value(val)
{
}
shared_ptr<EvalValue> Clone() final{
return make_shared<BooleanEvalValue>(_value);
}
2019-06-13 14:26:10 +00:00
const TypeClass GetTypeClass() final{
return TypeClass ::Bool;
}
2019-06-14 15:12:27 +00:00
bool EvaluateBool() const final{
return _value;
}
bool operator ==(EvalValue* b) final{
2019-06-13 14:26:10 +00:00
if (b->GetTypeClass() != TypeClass::Bool)
return false;
return this->EvaluateBool() == b->EvaluateBool();
};
2019-06-09 18:15:09 +00:00
std::size_t GetHashCode() final{
return _value;
}
};
#endif //PORYGONLANG_EVALVALUE_HPP