Reworked evaluation to use internal type instead of boost::any
This commit is contained in:
28
src/Evaluator/EvalValues/EvalValue.hpp
Normal file
28
src/Evaluator/EvalValues/EvalValue.hpp
Normal file
@@ -0,0 +1,28 @@
|
||||
|
||||
#ifndef PORYGONLANG_EVALVALUE_HPP
|
||||
#define PORYGONLANG_EVALVALUE_HPP
|
||||
|
||||
#include "../../ScriptType.hpp"
|
||||
#include "../EvaluationException.hpp"
|
||||
#include <string>
|
||||
|
||||
class EvalValue{
|
||||
public:
|
||||
virtual ~EvalValue() = default;
|
||||
virtual ScriptType* GetType() = 0;
|
||||
|
||||
virtual long EvaluateInteger(){
|
||||
throw EvaluationException("Can't evaluate this EvalValue as integer.");
|
||||
}
|
||||
virtual double EvaluateFloat(){
|
||||
throw EvaluationException("Can't evaluate this EvalValue as float.");
|
||||
}
|
||||
virtual bool EvaluateBool(){
|
||||
throw EvaluationException("Can't evaluate this EvalValue as bool.");
|
||||
}
|
||||
virtual std::string EvaluateString(){
|
||||
throw EvaluationException("Can't evaluate this EvalValue as string.");
|
||||
}
|
||||
};
|
||||
|
||||
#endif //PORYGONLANG_EVALVALUE_HPP
|
||||
50
src/Evaluator/EvalValues/NumericEvalValue.cpp
Normal file
50
src/Evaluator/EvalValues/NumericEvalValue.cpp
Normal file
@@ -0,0 +1,50 @@
|
||||
|
||||
#include "NumericEvalValue.hpp"
|
||||
|
||||
NumericEvalValue *NumericEvalValue::operator+(NumericEvalValue *b) {
|
||||
if (this->IsFloat() && b->IsFloat()){
|
||||
return new FloatEvalValue(this->GetFloatValue() + b->GetFloatValue());
|
||||
} else if (this->IsFloat()){
|
||||
return new FloatEvalValue(this->GetFloatValue() + b->GetIntegerValue());
|
||||
} else if (b->IsFloat()){
|
||||
return new FloatEvalValue(this->GetIntegerValue() + b->GetFloatValue());
|
||||
} else{
|
||||
return new IntegerEvalValue(this->GetIntegerValue() + b->GetIntegerValue());
|
||||
}
|
||||
}
|
||||
|
||||
NumericEvalValue *NumericEvalValue::operator-(NumericEvalValue *b) {
|
||||
if (this->IsFloat() && b->IsFloat()){
|
||||
return new FloatEvalValue(this->GetFloatValue() - b->GetFloatValue());
|
||||
} else if (this->IsFloat()){
|
||||
return new FloatEvalValue(this->GetFloatValue() - b->GetIntegerValue());
|
||||
} else if (b->IsFloat()){
|
||||
return new FloatEvalValue(this->GetIntegerValue() - b->GetFloatValue());
|
||||
} else{
|
||||
return new IntegerEvalValue(this->GetIntegerValue() - b->GetIntegerValue());
|
||||
}
|
||||
}
|
||||
|
||||
NumericEvalValue *NumericEvalValue::operator*(NumericEvalValue *b) {
|
||||
if (this->IsFloat() && b->IsFloat()){
|
||||
return new FloatEvalValue(this->GetFloatValue() * b->GetFloatValue());
|
||||
} else if (this->IsFloat()){
|
||||
return new FloatEvalValue(this->GetFloatValue() * b->GetIntegerValue());
|
||||
} else if (b->IsFloat()){
|
||||
return new FloatEvalValue(this->GetIntegerValue() * b->GetFloatValue());
|
||||
} else{
|
||||
return new IntegerEvalValue(this->GetIntegerValue() * b->GetIntegerValue());
|
||||
}
|
||||
}
|
||||
|
||||
NumericEvalValue *NumericEvalValue::operator/(NumericEvalValue *b) {
|
||||
if (this->IsFloat() && b->IsFloat()){
|
||||
return new FloatEvalValue(this->GetFloatValue() / b->GetFloatValue());
|
||||
} else if (this->IsFloat()){
|
||||
return new FloatEvalValue(this->GetFloatValue() / b->GetIntegerValue());
|
||||
} else if (b->IsFloat()){
|
||||
return new FloatEvalValue(this->GetIntegerValue() / b->GetFloatValue());
|
||||
} else{
|
||||
return new IntegerEvalValue(this->GetIntegerValue() / b->GetIntegerValue());
|
||||
}
|
||||
}
|
||||
65
src/Evaluator/EvalValues/NumericEvalValue.hpp
Normal file
65
src/Evaluator/EvalValues/NumericEvalValue.hpp
Normal file
@@ -0,0 +1,65 @@
|
||||
|
||||
#ifndef PORYGONLANG_NUMERICEVALVALUE_HPP
|
||||
#define PORYGONLANG_NUMERICEVALVALUE_HPP
|
||||
|
||||
#include "EvalValue.hpp"
|
||||
|
||||
class NumericEvalValue : public EvalValue{
|
||||
|
||||
virtual long GetIntegerValue() = 0;
|
||||
virtual double GetFloatValue() = 0;
|
||||
|
||||
protected:
|
||||
ScriptType* _type;
|
||||
public:
|
||||
~NumericEvalValue() override{
|
||||
delete _type;
|
||||
};
|
||||
virtual const bool IsFloat() = 0;
|
||||
ScriptType* GetType() override {
|
||||
return _type;
|
||||
}
|
||||
|
||||
NumericEvalValue* operator +(NumericEvalValue* b);
|
||||
NumericEvalValue* operator -(NumericEvalValue* b);
|
||||
NumericEvalValue* operator *(NumericEvalValue* b);
|
||||
NumericEvalValue* operator /(NumericEvalValue* b);
|
||||
};
|
||||
|
||||
class IntegerEvalValue : public NumericEvalValue{
|
||||
long _value;
|
||||
long GetIntegerValue() final{return _value;}
|
||||
double GetFloatValue() final{ throw EvaluationException("Attempting to retrieve float from int eval value."); }
|
||||
public:
|
||||
explicit IntegerEvalValue(long value){
|
||||
_type = new NumericScriptType(true, false);
|
||||
_value = value;
|
||||
}
|
||||
const bool IsFloat() final{
|
||||
return false;
|
||||
}
|
||||
|
||||
long EvaluateInteger() final{
|
||||
return _value;
|
||||
}
|
||||
};
|
||||
|
||||
class FloatEvalValue : public NumericEvalValue{
|
||||
double _value;
|
||||
long GetIntegerValue() final{ throw EvaluationException("Attempting to retrieve float from int eval value."); }
|
||||
double GetFloatValue() final{return _value;}
|
||||
public:
|
||||
explicit FloatEvalValue(double value){
|
||||
_type = new NumericScriptType(true, true);
|
||||
_value = value;
|
||||
}
|
||||
const bool IsFloat() final{
|
||||
return true;
|
||||
}
|
||||
|
||||
double EvaluateFloat() final{
|
||||
return _value;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //PORYGONLANG_NUMERICEVALVALUE_HPP
|
||||
Reference in New Issue
Block a user