2019-05-23 16:50:09 +00:00
|
|
|
|
|
|
|
#include "Evaluator.hpp"
|
|
|
|
#include "EvaluationException.hpp"
|
|
|
|
#include "../Script.hpp"
|
|
|
|
|
|
|
|
void Evaluator::Evaluate(BoundScriptStatement *statement) {
|
|
|
|
EvaluateBlockStatement(statement);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Evaluator::EvaluateStatement(BoundStatement *statement) {
|
|
|
|
switch (statement->GetKind()){
|
|
|
|
case BoundStatementKind ::Script: throw; // Should never happen
|
|
|
|
case BoundStatementKind ::Block: return this -> EvaluateBlockStatement((BoundBlockStatement*)statement);
|
|
|
|
case BoundStatementKind ::Expression: return this -> EvaluateExpressionStatement((BoundExpressionStatement*)statement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Evaluator::EvaluateBlockStatement(BoundBlockStatement* statement) {
|
|
|
|
for (auto s: statement->GetStatements()){
|
|
|
|
this -> EvaluateStatement(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Evaluator::EvaluateExpressionStatement(BoundExpressionStatement *statement) {
|
|
|
|
this->_scriptData->_lastValue = this -> EvaluateExpression(statement->GetExpression());
|
|
|
|
}
|
|
|
|
|
|
|
|
any *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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
long 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 ::Binary: return this -> EvaluateIntegerBinary((BoundBinaryExpression*)expression);
|
|
|
|
|
|
|
|
case BoundExpressionKind ::LiteralFloat:
|
|
|
|
case BoundExpressionKind ::LiteralString:
|
|
|
|
case BoundExpressionKind ::LiteralBool:
|
|
|
|
case BoundExpressionKind ::Bad:
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
double Evaluator::EvaluateFloatExpression(BoundExpression *expression) {
|
2019-05-24 13:31:11 +00:00
|
|
|
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()){
|
2019-05-24 13:33:13 +00:00
|
|
|
case BoundExpressionKind ::LiteralFloat: return ((BoundLiteralFloatExpression*)expression)->GetValue();
|
2019-05-24 13:31:11 +00:00
|
|
|
case BoundExpressionKind ::Binary: return this -> EvaluateFloatBinary((BoundBinaryExpression*)expression);
|
|
|
|
|
|
|
|
case BoundExpressionKind ::LiteralInteger:
|
|
|
|
case BoundExpressionKind ::LiteralString:
|
|
|
|
case BoundExpressionKind ::LiteralBool:
|
|
|
|
case BoundExpressionKind ::Bad:
|
|
|
|
throw;
|
|
|
|
}
|
2019-05-23 16:50:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Evaluator::EvaluateBoolExpression(BoundExpression *expression) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Evaluator::EvaluateStringExpression(BoundExpression *expression) {
|
|
|
|
return std::__cxx11::string();
|
|
|
|
}
|
|
|
|
|