2019-05-23 16:50:09 +00:00
|
|
|
|
|
|
|
#include "Evaluator.hpp"
|
|
|
|
#include "EvaluationException.hpp"
|
|
|
|
#include "../Script.hpp"
|
2019-05-29 12:55:03 +00:00
|
|
|
#include "EvaluationScope/EvaluationScope.hpp"
|
2019-06-01 10:33:52 +00:00
|
|
|
#include "EvalValues/ScriptFunctionEvalValue.hpp"
|
2019-05-23 16:50:09 +00:00
|
|
|
|
|
|
|
void Evaluator::Evaluate(BoundScriptStatement *statement) {
|
2019-05-29 12:55:03 +00:00
|
|
|
this->_evaluationScope = new EvaluationScope(this->_scriptData->_scriptVariables, statement->GetDeepestScope());
|
2019-05-23 16:50:09 +00:00
|
|
|
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);
|
2019-05-29 12:55:03 +00:00
|
|
|
case BoundStatementKind ::Assignment: return this -> EvaluateAssignmentStatement((BoundAssignmentStatement*)statement);
|
2019-06-01 10:33:52 +00:00
|
|
|
case BoundStatementKind ::FunctionDeclaration: return this->EvaluateFunctionDeclarationStatement((BoundFunctionDeclarationStatement*)statement);
|
2019-05-29 12:55:03 +00:00
|
|
|
|
|
|
|
case BoundStatementKind::Bad:
|
|
|
|
throw;
|
2019-05-23 16:50:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Evaluator::EvaluateBlockStatement(BoundBlockStatement* statement) {
|
2019-05-30 13:23:48 +00:00
|
|
|
this->_evaluationScope->OuterScope();
|
2019-05-23 16:50:09 +00:00
|
|
|
for (auto s: statement->GetStatements()){
|
|
|
|
this -> EvaluateStatement(s);
|
|
|
|
}
|
2019-05-30 13:23:48 +00:00
|
|
|
this->_evaluationScope->InnerScope();
|
2019-05-23 16:50:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-29 12:55:03 +00:00
|
|
|
void Evaluator::EvaluateAssignmentStatement(BoundAssignmentStatement *statement) {
|
|
|
|
auto value = this -> EvaluateExpression(statement->GetExpression());
|
|
|
|
auto key = statement->GetKey();
|
|
|
|
if (key->IsCreation()){
|
|
|
|
this->_evaluationScope->CreateVariable(key->GetScopeId(), key->GetIdentifier(), value);
|
|
|
|
} else{
|
|
|
|
this->_evaluationScope->SetVariable(key->GetScopeId(), key->GetIdentifier(), value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-01 10:33:52 +00:00
|
|
|
void Evaluator::EvaluateFunctionDeclarationStatement(BoundFunctionDeclarationStatement *statement) {
|
|
|
|
auto type = statement->GetType();
|
|
|
|
auto key = statement->GetKey();
|
|
|
|
auto block = statement->GetBlock();
|
2019-06-01 17:20:31 +00:00
|
|
|
auto value = new ScriptFunctionEvalValue(block, type);
|
2019-06-01 10:33:52 +00:00
|
|
|
if (key->IsCreation()){
|
|
|
|
this->_evaluationScope->CreateVariable(key->GetScopeId(), key->GetIdentifier(), value);
|
|
|
|
} else{
|
|
|
|
this->_evaluationScope->SetVariable(key->GetScopeId(), key->GetIdentifier(), value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2019-05-25 11:30:20 +00:00
|
|
|
case TypeClass ::Bool: return this -> EvaluateBoolExpression(expression);
|
2019-05-25 14:15:20 +00:00
|
|
|
case TypeClass ::String: return this -> EvaluateStringExpression(expression);
|
2019-06-01 17:20:31 +00:00
|
|
|
case TypeClass ::Function: return this->EvaluateFunctionExpression(expression);
|
|
|
|
case TypeClass ::Nil: return this->EvaluateNilExpression(expression);
|
2019-05-24 17:14:30 +00:00
|
|
|
default: throw;
|
2019-05-23 16:50:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-30 13:23:48 +00:00
|
|
|
EvalValue* Evaluator::GetVariable(BoundVariableExpression* expression){
|
|
|
|
return this->_evaluationScope->GetVariable(expression->GetScope(), expression->GetId())->Clone();
|
|
|
|
}
|
|
|
|
|
2019-05-24 17:14:30 +00:00
|
|
|
NumericEvalValue* Evaluator::EvaluateIntegerExpression(BoundExpression *expression) {
|
2019-05-23 16:50:09 +00:00
|
|
|
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-25 12:59:12 +00:00
|
|
|
case BoundExpressionKind::Unary: return this -> EvaluateIntegerUnary((BoundUnaryExpression*)expression);
|
2019-05-23 16:50:09 +00:00
|
|
|
case BoundExpressionKind ::Binary: return this -> EvaluateIntegerBinary((BoundBinaryExpression*)expression);
|
2019-05-30 13:23:48 +00:00
|
|
|
case BoundExpressionKind::Variable: return (NumericEvalValue*)this->GetVariable((BoundVariableExpression*)expression);
|
2019-06-01 17:20:31 +00:00
|
|
|
case BoundExpressionKind ::FunctionCall: return (NumericEvalValue*)this->EvaluateFunctionCallExpression(expression);
|
2019-05-23 16:50:09 +00:00
|
|
|
|
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-25 11:30:20 +00:00
|
|
|
BooleanEvalValue* Evaluator::EvaluateBoolExpression(BoundExpression *expression) {
|
|
|
|
switch (expression->GetKind()) {
|
|
|
|
case BoundExpressionKind::LiteralBool: return new BooleanEvalValue(((BoundLiteralBoolExpression*)expression)->GetValue());
|
2019-05-25 12:59:12 +00:00
|
|
|
case BoundExpressionKind::Unary: return this -> EvaluateBooleanUnary((BoundUnaryExpression*)expression);
|
2019-05-25 11:30:20 +00:00
|
|
|
case BoundExpressionKind::Binary: return this -> EvaluateBooleanBinary((BoundBinaryExpression*)expression);
|
2019-05-30 13:23:48 +00:00
|
|
|
case BoundExpressionKind::Variable: return (BooleanEvalValue*)this->GetVariable((BoundVariableExpression*)expression);
|
2019-06-01 17:20:31 +00:00
|
|
|
case BoundExpressionKind ::FunctionCall: return (BooleanEvalValue*)this->EvaluateFunctionCallExpression(expression);
|
2019-05-25 14:15:20 +00:00
|
|
|
|
2019-05-25 11:30:20 +00:00
|
|
|
case BoundExpressionKind::Bad:
|
|
|
|
case BoundExpressionKind::LiteralInteger:
|
|
|
|
case BoundExpressionKind::LiteralFloat:
|
|
|
|
case BoundExpressionKind::LiteralString:
|
|
|
|
throw;
|
|
|
|
|
|
|
|
}
|
2019-05-23 16:50:09 +00:00
|
|
|
}
|
|
|
|
|
2019-05-25 14:15:20 +00:00
|
|
|
StringEvalValue* Evaluator::EvaluateStringExpression(BoundExpression *expression) {
|
|
|
|
switch (expression->GetKind()) {
|
|
|
|
case BoundExpressionKind::LiteralString:
|
|
|
|
return new StringEvalValue(((BoundLiteralStringExpression*)expression)->GetValue());
|
|
|
|
case BoundExpressionKind::Binary:
|
|
|
|
return this -> EvaluateStringBinary((BoundBinaryExpression*)expression);
|
2019-05-30 13:23:48 +00:00
|
|
|
case BoundExpressionKind::Variable: return (StringEvalValue*)this->GetVariable((BoundVariableExpression*)expression);
|
2019-06-01 17:20:31 +00:00
|
|
|
case BoundExpressionKind ::FunctionCall: return (StringEvalValue*)this->EvaluateFunctionCallExpression(expression);
|
2019-05-25 14:15:20 +00:00
|
|
|
|
|
|
|
case BoundExpressionKind::Bad:
|
|
|
|
case BoundExpressionKind::LiteralInteger:
|
|
|
|
case BoundExpressionKind::LiteralFloat:
|
|
|
|
case BoundExpressionKind::LiteralBool:
|
|
|
|
case BoundExpressionKind::Unary:
|
|
|
|
throw;
|
|
|
|
|
2019-06-01 17:20:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EvalValue* Evaluator::EvaluateFunctionExpression(BoundExpression * expression){
|
|
|
|
switch (expression->GetKind()){
|
|
|
|
case BoundExpressionKind ::Variable: return this->GetVariable((BoundVariableExpression*)expression);
|
|
|
|
default: throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EvalValue* Evaluator::EvaluateNilExpression(BoundExpression * expression){
|
|
|
|
switch (expression->GetKind()){
|
|
|
|
case BoundExpressionKind ::FunctionCall:
|
|
|
|
return this->EvaluateFunctionCallExpression(expression);
|
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EvalValue* Evaluator::EvaluateFunctionCallExpression(BoundExpression* expression){
|
|
|
|
auto functionCall = (BoundFunctionCallExpression*)expression;
|
|
|
|
auto function = (ScriptFunctionEvalValue*)this->EvaluateExpression(functionCall->GetFunctionExpression());
|
|
|
|
auto boundParameters = functionCall->GetParameters();
|
|
|
|
auto parameters = vector<EvalValue*>(boundParameters.size());
|
|
|
|
for (int i = 0; i < boundParameters.size(); i++){
|
|
|
|
parameters[i] = this->EvaluateExpression(boundParameters[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto type = std::dynamic_pointer_cast<FunctionScriptType>(function->GetType());
|
|
|
|
auto parameterTypes = type->GetParameterTypes();
|
|
|
|
auto parameterKeys = type->GetParameterKeys();
|
|
|
|
for (int i = 0; i < parameterTypes->size() && i < parameterKeys->size() && i < parameters.size(); i++){
|
|
|
|
auto parameter = parameters[i];
|
|
|
|
auto requiredType = parameterTypes->at(i);
|
|
|
|
if (*parameter->GetType() != requiredType.get()){
|
|
|
|
throw EvaluationException("Passed wrong type to function.");
|
|
|
|
}
|
|
|
|
auto key = parameterKeys->at(i);
|
|
|
|
this->_evaluationScope->CreateVariable(key->GetScopeId(), key->GetIdentifier(), parameter->Clone());
|
|
|
|
}
|
|
|
|
this->EvaluateBlockStatement(function->GetInnerBlock().get());
|
|
|
|
return nullptr;
|
2019-05-23 16:50:09 +00:00
|
|
|
|
2019-06-01 17:20:31 +00:00
|
|
|
}
|