Reworked evaluation to use internal type instead of boost::any

This commit is contained in:
2019-05-24 19:14:30 +02:00
parent fed4c65bef
commit 4a034bc051
10 changed files with 197 additions and 132 deletions

View File

@@ -25,40 +25,24 @@ void Evaluator::EvaluateExpressionStatement(BoundExpressionStatement *statement)
this->_scriptData->_lastValue = this -> EvaluateExpression(statement->GetExpression());
}
any *Evaluator::EvaluateExpression(BoundExpression *expression) {
EvalValue *Evaluator::EvaluateExpression(BoundExpression *expression) {
auto type = expression -> GetType();
switch (type->GetClass()){
case TypeClass ::Number:
{
auto numType = (NumericScriptType*)type;
if (numType->IsAwareOfFloat()){
if (numType->IsFloat()){
double d = this -> EvaluateFloatExpression(expression);
return new boost::any(d);
} else{
long l = this -> EvaluateIntegerExpression(expression);
return new boost::any(l);
}
}
break;
}
case TypeClass ::Number: return this -> EvaluateIntegerExpression(expression);
default: throw;
}
}
long Evaluator::EvaluateIntegerExpression(BoundExpression *expression) {
NumericEvalValue* Evaluator::EvaluateIntegerExpression(BoundExpression *expression) {
auto exprType = expression->GetType();
if (exprType->GetClass() != TypeClass::Number){
throw EvaluationException("Can't evaluate expression as integer, it will not return a number.");
}
auto numType = (NumericScriptType*)exprType;
if (numType->IsAwareOfFloat() && numType->IsFloat()){
throw EvaluationException("Can't evaluate expression as integer, it will return a float, not an integer.");
}
switch (expression->GetKind()){
case BoundExpressionKind ::LiteralInteger: return ((BoundLiteralIntegerExpression*)expression)->GetValue();
case BoundExpressionKind ::LiteralInteger: return new IntegerEvalValue(((BoundLiteralIntegerExpression*)expression)->GetValue());
case BoundExpressionKind ::LiteralFloat: return new FloatEvalValue(((BoundLiteralFloatExpression*)expression)->GetValue());
case BoundExpressionKind ::Binary: return this -> EvaluateIntegerBinary((BoundBinaryExpression*)expression);
case BoundExpressionKind ::LiteralFloat:
case BoundExpressionKind ::LiteralString:
case BoundExpressionKind ::LiteralBool:
case BoundExpressionKind ::Bad:
@@ -66,32 +50,11 @@ long Evaluator::EvaluateIntegerExpression(BoundExpression *expression) {
}
}
double Evaluator::EvaluateFloatExpression(BoundExpression *expression) {
auto exprType = expression->GetType();
if (exprType->GetClass() != TypeClass::Number){
throw EvaluationException("Can't evaluate expression as float, it will not return a number.");
}
auto numType = (NumericScriptType*)exprType;
if (numType->IsAwareOfFloat() && !numType->IsFloat()){
throw EvaluationException("Can't evaluate expression as integer, it will return an integer, not a float.");
}
switch (expression->GetKind()){
case BoundExpressionKind ::LiteralFloat: return ((BoundLiteralFloatExpression*)expression)->GetValue();
case BoundExpressionKind ::Binary: return this -> EvaluateFloatBinary((BoundBinaryExpression*)expression);
case BoundExpressionKind ::LiteralInteger:
case BoundExpressionKind ::LiteralString:
case BoundExpressionKind ::LiteralBool:
case BoundExpressionKind ::Bad:
throw;
}
EvalValue* Evaluator::EvaluateBoolExpression(BoundExpression *expression) {
return nullptr;
}
bool Evaluator::EvaluateBoolExpression(BoundExpression *expression) {
return false;
}
std::string Evaluator::EvaluateStringExpression(BoundExpression *expression) {
return std::__cxx11::string();
EvalValue* Evaluator::EvaluateStringExpression(BoundExpression *expression) {
return nullptr;
}