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) {
|
2019-05-25 10:26:11 +00:00
|
|
|
// Delete previously saved value.
|
|
|
|
delete this->_scriptData->_lastValue;
|
|
|
|
// Save new value
|
2019-05-23 16:50:09 +00:00
|
|
|
this->_scriptData->_lastValue = this -> EvaluateExpression(statement->GetExpression());
|
|
|
|
}
|
|
|
|
|
2019-05-24 17:14:30 +00:00
|
|
|
EvalValue *Evaluator::EvaluateExpression(BoundExpression *expression) {
|
2019-05-23 16:50:09 +00:00
|
|
|
auto type = expression -> GetType();
|
|
|
|
switch (type->GetClass()){
|
2019-05-24 17:14:30 +00:00
|
|
|
case TypeClass ::Number: return this -> EvaluateIntegerExpression(expression);
|
|
|
|
default: throw;
|
2019-05-23 16:50:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-24 17:14:30 +00:00
|
|
|
NumericEvalValue* Evaluator::EvaluateIntegerExpression(BoundExpression *expression) {
|
2019-05-23 16:50:09 +00:00
|
|
|
auto exprType = expression->GetType();
|
|
|
|
if (exprType->GetClass() != TypeClass::Number){
|
|
|
|
throw EvaluationException("Can't evaluate expression as integer, it will not return a number.");
|
|
|
|
}
|
|
|
|
switch (expression->GetKind()){
|
2019-05-24 17:14:30 +00:00
|
|
|
case BoundExpressionKind ::LiteralInteger: return new IntegerEvalValue(((BoundLiteralIntegerExpression*)expression)->GetValue());
|
|
|
|
case BoundExpressionKind ::LiteralFloat: return new FloatEvalValue(((BoundLiteralFloatExpression*)expression)->GetValue());
|
2019-05-23 16:50:09 +00:00
|
|
|
case BoundExpressionKind ::Binary: return this -> EvaluateIntegerBinary((BoundBinaryExpression*)expression);
|
|
|
|
|
2019-05-24 13:31:11 +00:00
|
|
|
case BoundExpressionKind ::LiteralString:
|
|
|
|
case BoundExpressionKind ::LiteralBool:
|
|
|
|
case BoundExpressionKind ::Bad:
|
|
|
|
throw;
|
|
|
|
}
|
2019-05-23 16:50:09 +00:00
|
|
|
}
|
|
|
|
|
2019-05-24 17:14:30 +00:00
|
|
|
EvalValue* Evaluator::EvaluateBoolExpression(BoundExpression *expression) {
|
|
|
|
return nullptr;
|
2019-05-23 16:50:09 +00:00
|
|
|
}
|
|
|
|
|
2019-05-24 17:14:30 +00:00
|
|
|
EvalValue* Evaluator::EvaluateStringExpression(BoundExpression *expression) {
|
|
|
|
return nullptr;
|
2019-05-23 16:50:09 +00:00
|
|
|
}
|
|
|
|
|