2019-05-24 17:14:30 +00:00
|
|
|
|
|
|
|
#ifndef PORYGONLANG_EVALVALUE_HPP
|
|
|
|
#define PORYGONLANG_EVALVALUE_HPP
|
|
|
|
|
|
|
|
#include <string>
|
2019-05-25 14:15:20 +00:00
|
|
|
#include <sstream>
|
2019-06-01 17:20:31 +00:00
|
|
|
#include <memory>
|
2019-07-28 10:58:38 +00:00
|
|
|
#include "../../ScriptTypes/ScriptType.hpp"
|
2019-06-26 14:19:34 +00:00
|
|
|
#include "../EvaluationException.hpp"
|
|
|
|
namespace Porygon::Evaluation{
|
|
|
|
class EvalValue;
|
|
|
|
class Iterator;
|
|
|
|
}
|
|
|
|
#include "../Iterator/Iterator.hpp"
|
2019-07-27 15:59:42 +00:00
|
|
|
#include "../../Binder/BoundOperators.hpp"
|
2019-06-26 14:19:34 +00:00
|
|
|
|
2019-05-24 17:14:30 +00:00
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
namespace Porygon::Evaluation {
|
|
|
|
class EvalValue {
|
|
|
|
public:
|
|
|
|
EvalValue() = default;
|
|
|
|
|
|
|
|
virtual ~EvalValue() = default;
|
|
|
|
|
2019-07-25 15:23:54 +00:00
|
|
|
[[nodiscard]]
|
|
|
|
virtual TypeClass GetTypeClass() const = 0;
|
2019-06-17 16:35:12 +00:00
|
|
|
|
2019-07-25 15:23:54 +00:00
|
|
|
[[nodiscard]]
|
|
|
|
virtual bool operator==(const EvalValue *b) const = 0;
|
2019-06-17 16:35:12 +00:00
|
|
|
|
2019-07-25 15:23:54 +00:00
|
|
|
[[nodiscard]]
|
|
|
|
virtual bool operator!=(const EvalValue *b) const {
|
2019-06-17 16:35:12 +00:00
|
|
|
return !(this->operator==(b));
|
|
|
|
}
|
|
|
|
|
2019-07-25 15:23:54 +00:00
|
|
|
[[nodiscard]]
|
2019-07-27 15:59:42 +00:00
|
|
|
virtual EvalValue* Clone() const = 0;
|
2019-06-17 16:35:12 +00:00
|
|
|
|
2019-07-25 15:23:54 +00:00
|
|
|
[[nodiscard]]
|
2019-08-18 08:30:58 +00:00
|
|
|
virtual int64_t EvaluateInteger() const {
|
2019-06-17 16:35:12 +00:00
|
|
|
throw EvaluationException("Can't evaluate this EvalValue as integer.");
|
|
|
|
}
|
|
|
|
|
2019-07-25 15:23:54 +00:00
|
|
|
[[nodiscard]]
|
|
|
|
virtual double EvaluateFloat() const {
|
2019-06-17 16:35:12 +00:00
|
|
|
throw EvaluationException("Can't evaluate this EvalValue as float.");
|
|
|
|
}
|
|
|
|
|
2019-07-25 15:23:54 +00:00
|
|
|
[[nodiscard]]
|
|
|
|
virtual bool EvaluateBool() const {
|
2019-06-17 16:35:12 +00:00
|
|
|
throw EvaluationException("Can't evaluate this EvalValue as bool.");
|
|
|
|
}
|
|
|
|
|
2019-07-25 15:23:54 +00:00
|
|
|
[[nodiscard]]
|
|
|
|
virtual std::u16string EvaluateString() const {
|
2019-06-17 16:35:12 +00:00
|
|
|
throw EvaluationException("Can't evaluate this EvalValue as string.");
|
|
|
|
}
|
|
|
|
|
2019-07-25 15:23:54 +00:00
|
|
|
[[nodiscard]]
|
|
|
|
virtual std::size_t GetHashCode() const = 0;
|
2019-06-17 16:35:12 +00:00
|
|
|
|
2019-07-25 15:23:54 +00:00
|
|
|
[[nodiscard]]
|
2019-07-27 15:59:42 +00:00
|
|
|
virtual const EvalValue* IndexValue(const EvalValue *val) const {
|
2019-09-07 11:04:42 +00:00
|
|
|
std::stringstream err;
|
|
|
|
err << "Can't index this EvalValue: " << ToString() << " with key: " << val->ToString();
|
|
|
|
throw EvaluationException(err.str());
|
2019-06-17 16:35:12 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 15:23:54 +00:00
|
|
|
[[nodiscard]]
|
2019-07-27 15:59:42 +00:00
|
|
|
virtual const EvalValue* IndexValue(uint32_t hash) const {
|
2019-09-07 11:04:42 +00:00
|
|
|
std::stringstream err;
|
|
|
|
err << "Can't index this EvalValue: " << ToString() << " with key hash: " << hash;
|
|
|
|
throw EvaluationException(err.str());
|
2019-06-17 16:35:12 +00:00
|
|
|
}
|
|
|
|
|
2019-07-27 15:59:42 +00:00
|
|
|
virtual void SetIndexValue(const EvalValue *key, const EvalValue* value) const {
|
2019-09-07 11:04:42 +00:00
|
|
|
std::stringstream err;
|
|
|
|
err << "Can't index this EvalValue: " << ToString() << " with key: " << value->ToString();
|
|
|
|
throw EvaluationException(err.str());
|
2019-06-17 16:35:12 +00:00
|
|
|
}
|
2019-06-26 14:19:34 +00:00
|
|
|
|
2019-07-25 15:23:54 +00:00
|
|
|
[[nodiscard]]
|
2019-06-26 14:19:34 +00:00
|
|
|
virtual Iterator * GetKeyIterator() const{
|
|
|
|
throw EvaluationException("Can't iterate over this EvalValue");
|
|
|
|
}
|
2019-07-27 15:59:42 +00:00
|
|
|
|
|
|
|
[[nodiscard]]
|
|
|
|
virtual EvalValue* BinaryOperation(Binder::BoundBinaryOperation operation, const EvalValue* b) const{
|
|
|
|
throw EvaluationException("Binary operations are not implemented for this type.");
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]]
|
|
|
|
virtual EvalValue* UnaryOperation(Binder::BoundUnaryOperation operation) const{
|
|
|
|
throw EvaluationException("Unary operations are not implemented for this type.");
|
|
|
|
}
|
2019-08-18 11:17:53 +00:00
|
|
|
|
|
|
|
[[nodiscard]]
|
|
|
|
virtual EvalValue* Cast(shared_ptr<const ScriptType> castType) const{
|
|
|
|
throw new EvaluationException("Casting to invalid type.");
|
|
|
|
}
|
2019-09-07 11:04:42 +00:00
|
|
|
|
|
|
|
virtual std::string ToString() const{
|
|
|
|
std::stringstream s;
|
|
|
|
s << "Type: " << ScriptType::ToString(this->GetTypeClass());
|
|
|
|
return s.str();
|
|
|
|
}
|
2019-05-25 11:57:43 +00:00
|
|
|
};
|
2019-06-09 18:15:09 +00:00
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
class BooleanEvalValue : public EvalValue {
|
|
|
|
const bool _value;
|
|
|
|
public:
|
|
|
|
explicit BooleanEvalValue(bool val)
|
|
|
|
: _value(val) {
|
|
|
|
}
|
|
|
|
|
2019-07-25 15:23:54 +00:00
|
|
|
[[nodiscard]]
|
2019-07-27 15:59:42 +00:00
|
|
|
inline EvalValue* Clone() const final {
|
|
|
|
return new BooleanEvalValue(_value);
|
2019-06-17 16:35:12 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 15:23:54 +00:00
|
|
|
[[nodiscard]]
|
|
|
|
inline TypeClass GetTypeClass() const final {
|
2019-06-17 16:35:12 +00:00
|
|
|
return TypeClass::Bool;
|
|
|
|
}
|
|
|
|
|
2019-07-25 15:23:54 +00:00
|
|
|
[[nodiscard]]
|
|
|
|
inline bool EvaluateBool() const final {
|
2019-06-17 16:35:12 +00:00
|
|
|
return _value;
|
|
|
|
}
|
|
|
|
|
2019-07-25 15:23:54 +00:00
|
|
|
[[nodiscard]]
|
|
|
|
bool operator==(const EvalValue *b) const final {
|
2019-06-17 16:35:12 +00:00
|
|
|
if (b->GetTypeClass() != TypeClass::Bool)
|
|
|
|
return false;
|
|
|
|
return this->EvaluateBool() == b->EvaluateBool();
|
|
|
|
};
|
|
|
|
|
2019-07-25 15:23:54 +00:00
|
|
|
[[nodiscard]]
|
|
|
|
inline std::size_t GetHashCode() const final {
|
2019-06-17 16:35:12 +00:00
|
|
|
return _value;
|
|
|
|
}
|
2019-07-27 15:59:42 +00:00
|
|
|
|
|
|
|
[[nodiscard]]
|
|
|
|
inline EvalValue* BinaryOperation(Binder::BoundBinaryOperation operation, const EvalValue* b) const final{
|
|
|
|
auto bVal = b -> EvaluateBool();
|
|
|
|
switch (operation){
|
|
|
|
case Binder::BoundBinaryOperation::LogicalAnd: return new BooleanEvalValue(_value && bVal);
|
|
|
|
case Binder::BoundBinaryOperation::LogicalOr: return new BooleanEvalValue(_value || bVal);
|
|
|
|
default:
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]]
|
|
|
|
EvalValue* UnaryOperation(Binder::BoundUnaryOperation operation) const final{
|
|
|
|
switch (operation){
|
|
|
|
case Binder::BoundUnaryOperation::LogicalNegation: return new BooleanEvalValue(!_value);
|
|
|
|
default: throw;
|
|
|
|
}
|
|
|
|
}
|
2019-06-17 16:35:12 +00:00
|
|
|
};
|
|
|
|
}
|
2019-05-25 11:30:20 +00:00
|
|
|
|
2019-05-24 17:14:30 +00:00
|
|
|
#endif //PORYGONLANG_EVALVALUE_HPP
|