Handle bound classes as constants during evaluation
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -13,18 +13,18 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
EvalValue* Evaluator::Evaluate(BoundScriptStatement *statement) {
|
||||
EvalValue* Evaluator::Evaluate(const BoundScriptStatement *statement) {
|
||||
this->_evaluationScope = make_shared<EvaluationScope>(this->_scriptData->_scriptVariables, statement->GetLocalVariableCount());
|
||||
EvaluateBlockStatement(statement, false);
|
||||
EvaluateBlockStatement(statement);
|
||||
return this -> _returnValue.get();
|
||||
}
|
||||
|
||||
void Evaluator::EvaluateStatement(BoundStatement *statement) {
|
||||
void Evaluator::EvaluateStatement(const BoundStatement *statement) {
|
||||
if (this->_hasReturned)
|
||||
return;
|
||||
switch (statement->GetKind()){
|
||||
case BoundStatementKind ::Script: throw; // Should never happen
|
||||
case BoundStatementKind ::Block: return this -> EvaluateBlockStatement((BoundBlockStatement*)statement, true);
|
||||
case BoundStatementKind ::Block: return this->EvaluateBlockStatement((BoundBlockStatement *) statement);
|
||||
case BoundStatementKind ::Expression: return this -> EvaluateExpressionStatement((BoundExpressionStatement*)statement);
|
||||
case BoundStatementKind ::Assignment: return this -> EvaluateAssignmentStatement((BoundAssignmentStatement*)statement);
|
||||
case BoundStatementKind ::FunctionDeclaration: return this->EvaluateFunctionDeclarationStatement((BoundFunctionDeclarationStatement*)statement);
|
||||
@@ -36,20 +36,20 @@ void Evaluator::EvaluateStatement(BoundStatement *statement) {
|
||||
}
|
||||
}
|
||||
|
||||
void Evaluator::EvaluateBlockStatement(BoundBlockStatement* statement, bool clearScope) {
|
||||
for (auto s: statement->GetStatements()){
|
||||
void Evaluator::EvaluateBlockStatement(const BoundBlockStatement *statement) {
|
||||
for (auto s: *statement->GetStatements()){
|
||||
this -> EvaluateStatement(s);
|
||||
if (this->_hasReturned)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Evaluator::EvaluateExpressionStatement(BoundExpressionStatement *statement) {
|
||||
void Evaluator::EvaluateExpressionStatement(const BoundExpressionStatement *statement) {
|
||||
// Save new value
|
||||
this->_lastValue = this -> EvaluateExpression(statement->GetExpression());
|
||||
}
|
||||
|
||||
void Evaluator::EvaluateAssignmentStatement(BoundAssignmentStatement *statement) {
|
||||
void Evaluator::EvaluateAssignmentStatement(const BoundAssignmentStatement *statement) {
|
||||
auto value = this -> EvaluateExpression(statement->GetExpression());
|
||||
auto key = statement->GetKey();
|
||||
if (key->IsCreation()){
|
||||
@@ -59,7 +59,7 @@ void Evaluator::EvaluateAssignmentStatement(BoundAssignmentStatement *statement)
|
||||
}
|
||||
}
|
||||
|
||||
void Evaluator::EvaluateFunctionDeclarationStatement(BoundFunctionDeclarationStatement *statement) {
|
||||
void Evaluator::EvaluateFunctionDeclarationStatement(const BoundFunctionDeclarationStatement *statement) {
|
||||
auto type = statement->GetType();
|
||||
auto key = statement->GetKey();
|
||||
auto block = statement->GetBlock();
|
||||
@@ -71,7 +71,7 @@ void Evaluator::EvaluateFunctionDeclarationStatement(BoundFunctionDeclarationSta
|
||||
}
|
||||
}
|
||||
|
||||
void Evaluator::EvaluateReturnStatement(BoundReturnStatement* statement){
|
||||
void Evaluator::EvaluateReturnStatement(const BoundReturnStatement* statement){
|
||||
auto expression = statement->GetExpression();
|
||||
if (expression == nullptr){
|
||||
this->_hasReturned = true;
|
||||
@@ -82,7 +82,7 @@ void Evaluator::EvaluateReturnStatement(BoundReturnStatement* statement){
|
||||
this -> _returnValue = value;
|
||||
}
|
||||
|
||||
void Evaluator::EvaluateConditionalStatement(BoundConditionalStatement *statement) {
|
||||
void Evaluator::EvaluateConditionalStatement(const BoundConditionalStatement *statement) {
|
||||
auto condition = statement->GetCondition();
|
||||
if (EvaluateBoolExpression(condition) -> EvaluateBool()){
|
||||
this -> EvaluateStatement(statement->GetBlock());
|
||||
@@ -94,7 +94,7 @@ void Evaluator::EvaluateConditionalStatement(BoundConditionalStatement *statemen
|
||||
}
|
||||
}
|
||||
|
||||
shared_ptr<EvalValue> Evaluator::EvaluateExpression(BoundExpression *expression) {
|
||||
shared_ptr<EvalValue> Evaluator::EvaluateExpression(const BoundExpression *expression) {
|
||||
auto type = expression -> GetType();
|
||||
switch (type->GetClass()){
|
||||
case TypeClass ::Number: return this -> EvaluateIntegerExpression(expression);
|
||||
@@ -107,7 +107,7 @@ shared_ptr<EvalValue> Evaluator::EvaluateExpression(BoundExpression *expression)
|
||||
}
|
||||
}
|
||||
|
||||
shared_ptr<EvalValue> Evaluator::GetVariable(BoundVariableExpression* expression){
|
||||
shared_ptr<EvalValue> Evaluator::GetVariable(const BoundVariableExpression* expression){
|
||||
auto variable = this->_evaluationScope->GetVariable(expression->GetKey());
|
||||
if (variable == nullptr){
|
||||
throw EvaluationException("Variable not found");
|
||||
@@ -115,7 +115,7 @@ shared_ptr<EvalValue> Evaluator::GetVariable(BoundVariableExpression* expression
|
||||
return variable->Clone();
|
||||
}
|
||||
|
||||
shared_ptr<NumericEvalValue> Evaluator::EvaluateIntegerExpression(BoundExpression *expression) {
|
||||
shared_ptr<NumericEvalValue> Evaluator::EvaluateIntegerExpression(const BoundExpression *expression) {
|
||||
switch (expression->GetKind()){
|
||||
case BoundExpressionKind ::LiteralInteger: return make_shared<IntegerEvalValue>(((BoundLiteralIntegerExpression*)expression)->GetValue());
|
||||
case BoundExpressionKind ::LiteralFloat: return make_shared<FloatEvalValue>(((BoundLiteralFloatExpression*)expression)->GetValue());
|
||||
@@ -134,7 +134,7 @@ shared_ptr<NumericEvalValue> Evaluator::EvaluateIntegerExpression(BoundExpressio
|
||||
}
|
||||
}
|
||||
|
||||
shared_ptr<BooleanEvalValue> Evaluator::EvaluateBoolExpression(BoundExpression *expression) {
|
||||
shared_ptr<BooleanEvalValue> Evaluator::EvaluateBoolExpression(const BoundExpression *expression) {
|
||||
switch (expression->GetKind()) {
|
||||
case BoundExpressionKind ::LiteralBool: return make_shared<BooleanEvalValue>(((BoundLiteralBoolExpression*)expression)->GetValue());
|
||||
case BoundExpressionKind::Unary: return this -> EvaluateBooleanUnary((BoundUnaryExpression*)expression);
|
||||
@@ -154,7 +154,7 @@ shared_ptr<BooleanEvalValue> Evaluator::EvaluateBoolExpression(BoundExpression *
|
||||
}
|
||||
}
|
||||
|
||||
shared_ptr<StringEvalValue> Evaluator::EvaluateStringExpression(BoundExpression *expression) {
|
||||
shared_ptr<StringEvalValue> Evaluator::EvaluateStringExpression(const BoundExpression *expression) {
|
||||
switch (expression->GetKind()) {
|
||||
case BoundExpressionKind ::LiteralString:
|
||||
return make_shared<StringEvalValue>(((BoundLiteralStringExpression*)expression)->GetValue());
|
||||
@@ -176,14 +176,14 @@ shared_ptr<StringEvalValue> Evaluator::EvaluateStringExpression(BoundExpression
|
||||
}
|
||||
}
|
||||
|
||||
shared_ptr<EvalValue> Evaluator::EvaluateFunctionExpression(BoundExpression * expression){
|
||||
shared_ptr<EvalValue> Evaluator::EvaluateFunctionExpression(const BoundExpression * expression){
|
||||
switch (expression->GetKind()){
|
||||
case BoundExpressionKind ::Variable: return this->GetVariable((BoundVariableExpression*)expression);
|
||||
case BoundExpressionKind ::Index: return this->EvaluateIndexExpression(expression);
|
||||
default: throw;
|
||||
}
|
||||
}
|
||||
shared_ptr<EvalValue> Evaluator::EvaluateNilExpression(BoundExpression * expression){
|
||||
shared_ptr<EvalValue> Evaluator::EvaluateNilExpression(const BoundExpression * expression){
|
||||
switch (expression->GetKind()){
|
||||
case BoundExpressionKind ::FunctionCall:
|
||||
return this->EvaluateFunctionCallExpression(expression);
|
||||
@@ -191,7 +191,7 @@ shared_ptr<EvalValue> Evaluator::EvaluateNilExpression(BoundExpression * express
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
shared_ptr<EvalValue> Evaluator::EvaluateTableExpression(BoundExpression * expression){
|
||||
shared_ptr<EvalValue> Evaluator::EvaluateTableExpression(const BoundExpression * expression){
|
||||
switch (expression->GetKind()){
|
||||
case BoundExpressionKind ::FunctionCall:
|
||||
return this->EvaluateFunctionCallExpression(expression);
|
||||
@@ -207,14 +207,14 @@ shared_ptr<EvalValue> Evaluator::EvaluateTableExpression(BoundExpression * expre
|
||||
|
||||
|
||||
|
||||
shared_ptr<EvalValue> Evaluator::EvaluateFunctionCallExpression(BoundExpression* expression){
|
||||
shared_ptr<EvalValue> Evaluator::EvaluateFunctionCallExpression(const BoundExpression* expression){
|
||||
auto functionCall = (BoundFunctionCallExpression*)expression;
|
||||
auto function = dynamic_pointer_cast<ScriptFunctionEvalValue>(this->EvaluateExpression(functionCall->GetFunctionExpression()));
|
||||
|
||||
auto boundParameters = functionCall->GetParameters();
|
||||
auto parameters = vector<shared_ptr<EvalValue>>(boundParameters.size());
|
||||
for (int i = 0; i < boundParameters.size(); i++){
|
||||
parameters[i] = this->EvaluateExpression(boundParameters[i]);
|
||||
auto parameters = vector<shared_ptr<EvalValue>>(boundParameters->size());
|
||||
for (int i = 0; i < boundParameters->size(); i++){
|
||||
parameters[i] = this->EvaluateExpression(boundParameters->at(i));
|
||||
}
|
||||
|
||||
auto type = std::dynamic_pointer_cast<FunctionScriptType>(function->GetType());
|
||||
@@ -228,7 +228,7 @@ shared_ptr<EvalValue> Evaluator::EvaluateFunctionCallExpression(BoundExpression*
|
||||
auto key = parameterKeys.at(i);
|
||||
this->_evaluationScope->CreateVariable(key.get(), parameter->Clone());
|
||||
}
|
||||
this->EvaluateBlockStatement(function->GetInnerBlock().get(), true);
|
||||
this->EvaluateBlockStatement(function->GetInnerBlock().get());
|
||||
this->_evaluationScope = originalScope;
|
||||
|
||||
this->_hasReturned = false;
|
||||
@@ -237,7 +237,7 @@ shared_ptr<EvalValue> Evaluator::EvaluateFunctionCallExpression(BoundExpression*
|
||||
return r;
|
||||
}
|
||||
|
||||
shared_ptr<EvalValue> Evaluator::EvaluateFunction(ScriptFunctionEvalValue *function, vector<EvalValue *> parameters) {
|
||||
shared_ptr<EvalValue> Evaluator::EvaluateFunction(const ScriptFunctionEvalValue *function, const vector<EvalValue *>& parameters) {
|
||||
auto type = std::dynamic_pointer_cast<FunctionScriptType>(function->GetType());
|
||||
auto parameterTypes = type->GetParameterTypes();
|
||||
auto parameterKeys = type->GetParameterKeys();
|
||||
@@ -250,7 +250,7 @@ shared_ptr<EvalValue> Evaluator::EvaluateFunction(ScriptFunctionEvalValue *funct
|
||||
auto key = parameterKeys.at(i);
|
||||
this->_evaluationScope->CreateVariable(key.get(), parameter->Clone());
|
||||
}
|
||||
this->EvaluateBlockStatement(function->GetInnerBlock().get(), true);
|
||||
this->EvaluateBlockStatement(function->GetInnerBlock().get());
|
||||
this->_evaluationScope = originalScope;
|
||||
this->_hasReturned = false;
|
||||
auto r = this -> _returnValue;
|
||||
@@ -258,26 +258,26 @@ shared_ptr<EvalValue> Evaluator::EvaluateFunction(ScriptFunctionEvalValue *funct
|
||||
return r;
|
||||
}
|
||||
|
||||
shared_ptr<EvalValue> Evaluator::EvaluateIndexExpression(BoundExpression *expression) {
|
||||
shared_ptr<EvalValue> Evaluator::EvaluateIndexExpression(const BoundExpression *expression) {
|
||||
auto indexExpression = (BoundIndexExpression*)expression;
|
||||
auto index = this -> EvaluateExpression(indexExpression->GetIndexExpression());
|
||||
auto indexable = this -> EvaluateExpression(indexExpression->GetIndexableExpression());
|
||||
return indexable -> IndexValue(index.get()) -> Clone();
|
||||
}
|
||||
|
||||
shared_ptr<EvalValue> Evaluator::EvaluateNumericTableExpression(BoundExpression *expression) {
|
||||
shared_ptr<EvalValue> Evaluator::EvaluateNumericTableExpression(const BoundExpression *expression) {
|
||||
auto tableExpression = (BoundNumericalTableExpression*)expression;
|
||||
auto valueExpressions = tableExpression->GetExpressions();
|
||||
auto values = new unordered_map<size_t, shared_ptr<EvalValue>>(valueExpressions.size());
|
||||
for (int i = 0; i < valueExpressions.size(); i++){
|
||||
auto val = this -> EvaluateExpression(valueExpressions[i]);
|
||||
auto values = new unordered_map<size_t, shared_ptr<EvalValue>>(valueExpressions->size());
|
||||
for (int i = 0; i < valueExpressions->size(); i++){
|
||||
auto val = this -> EvaluateExpression(valueExpressions -> at(i));
|
||||
values -> insert({i + 1, val});
|
||||
}
|
||||
auto valuesPointer = shared_ptr<unordered_map<size_t, shared_ptr<EvalValue>>>(values);
|
||||
return make_shared<TableEvalValue>(valuesPointer);
|
||||
}
|
||||
|
||||
shared_ptr<EvalValue> Evaluator::EvaluateComplexTableExpression(BoundExpression *expression) {
|
||||
shared_ptr<EvalValue> Evaluator::EvaluateComplexTableExpression(const BoundExpression *expression) {
|
||||
auto tableExpression = (BoundTableExpression*)expression;
|
||||
auto type = dynamic_pointer_cast<TableScriptType>(tableExpression->GetType());
|
||||
auto declaredVars = type -> GetValues();
|
||||
@@ -288,7 +288,7 @@ shared_ptr<EvalValue> Evaluator::EvaluateComplexTableExpression(BoundExpression
|
||||
auto evaluator = make_shared<EvaluationScope>(variables.get(), type -> GetLocalVariableCount());
|
||||
auto currentEvaluator = this -> _evaluationScope;
|
||||
this -> _evaluationScope = evaluator;
|
||||
this -> EvaluateBlockStatement(tableExpression->GetBlock(), false);
|
||||
this->EvaluateBlockStatement(tableExpression->GetBlock());
|
||||
this -> _evaluationScope = currentEvaluator;
|
||||
return make_shared<TableEvalValue>(variables);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user