Rework of memory handling in Evaluation
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2019-07-27 17:59:42 +02:00
parent 268f6b59fb
commit ccc6e297f2
32 changed files with 496 additions and 461 deletions

View File

@@ -15,15 +15,18 @@ using namespace std;
using namespace Porygon::Binder;
namespace Porygon::Evaluation {
shared_ptr<const EvalValue> Evaluator::Evaluate(const BoundScriptStatement *statement) {
const EvalValue* Evaluator::Evaluate(const BoundScriptStatement *statement) {
this->_evaluationScope = make_shared<EvaluationScope>(this->_scriptVariables);
auto statements = statement->GetStatements();
if (statements -> size() == 1 && statements->at(0)->GetKind() == BoundStatementKind::Expression){
auto expStatement = (BoundExpressionStatement*) statements -> at(0);
return this -> EvaluateExpression(expStatement -> GetExpression());
return this -> EvaluateExpression(expStatement -> GetExpression()).Clone();
}
EvaluateBlockStatement(statement);
return this->_returnValue;
if (this->_returnValue.Get() == nullptr){
return nullptr;
}
return this->_returnValue.Clone();
}
void Evaluator::EvaluateStatement(const BoundStatement *statement) {
@@ -78,9 +81,9 @@ namespace Porygon::Evaluation {
auto value = this->EvaluateExpression(statement->GetExpression());
auto key = statement->GetKey();
if (key->IsCreation()) {
this->_evaluationScope->CreateVariable(key, value);
this->_evaluationScope->CreateVariable(key, value.Clone());
} else {
this->_evaluationScope->SetVariable(key, value);
this->_evaluationScope->SetVariable(key, value.Clone());
}
}
@@ -90,7 +93,7 @@ namespace Porygon::Evaluation {
auto index = ((BoundIndexExpression *) indexExpression);
auto table = this->EvaluateExpression(index->GetIndexableExpression());
auto key = this->EvaluateExpression(index->GetIndexExpression());
table->SetIndexValue(key.get(), value);
table->SetIndexValue(key.Get(), value.Take());
}
void Evaluator::EvaluateFunctionDeclarationStatement(const BoundFunctionDeclarationStatement *statement) {
@@ -100,13 +103,14 @@ namespace Porygon::Evaluation {
auto option = new Evaluation::EvaluationScriptFunctionOption(block, this->_evaluationScope);
if (key->IsCreation()) {
auto value = make_shared<GenericFunctionEvalValue>(type, Utilities::Random::Get());
value->RegisterOption(option);
auto p = new GenericFunctionEvalValue(type, Utilities::Random::Get());
p->RegisterOption(option);
auto value = EvalValuePointer(p);
this->_evaluationScope->CreateVariable(key, value);
} else {
auto var = dynamic_pointer_cast<const GenericFunctionEvalValue>(this -> _evaluationScope ->GetVariable(key));
auto var = (GenericFunctionEvalValue*)this -> _evaluationScope ->GetVariable(key).Get();
var->RegisterOption(option);
this->_evaluationScope->SetVariable(key, var);
//this->_evaluationScope->SetVariable(key, var);
}
}
@@ -114,17 +118,17 @@ namespace Porygon::Evaluation {
auto expression = statement->GetExpression();
if (expression == nullptr) {
this->_hasReturned = true;
this -> _returnValue = nullptr;
this -> _returnValue.ClearAssign(nullptr);
return;
}
auto value = this->EvaluateExpression(expression);
auto value = this->EvaluateExpression(expression).Take();
this->_hasReturned = true;
this->_returnValue = value;
this->_returnValue.ClearAssign(value);
}
void Evaluator::EvaluateConditionalStatement(const BoundConditionalStatement *statement) {
auto condition = statement->GetCondition();
if (EvaluateBoolExpression(condition)->EvaluateBool()) {
if (EvaluateExpression(condition)->EvaluateBool()) {
this->EvaluateStatement(statement->GetBlock());
} else {
auto elseStatement = statement->GetElseStatement();
@@ -135,12 +139,12 @@ namespace Porygon::Evaluation {
}
void Evaluator::EvaluateNumericalForStatement(const BoundNumericalForStatement *statement) {
long start = this->EvaluateIntegerExpression(statement -> GetStart()) -> EvaluateInteger();
long end = this->EvaluateIntegerExpression(statement -> GetEnd()) -> EvaluateInteger();
long start = this->EvaluateExpression(statement -> GetStart()) -> EvaluateInteger();
long end = this->EvaluateExpression(statement -> GetEnd()) -> EvaluateInteger();
long step = 1;
auto stepExp = statement -> GetStep();
if (stepExp != nullptr){
step = this -> EvaluateIntegerExpression(stepExp) -> EvaluateInteger();
step = this -> EvaluateExpression(stepExp) -> EvaluateInteger();
}
auto identifier = statement -> GetIdentifier();
this -> _evaluationScope -> CreateVariable(identifier, nullptr);
@@ -148,7 +152,7 @@ namespace Porygon::Evaluation {
auto statements = *block -> GetStatements();
if (step >= 0){
for (long i = start; i <= end; i += step){
this -> _evaluationScope -> SetVariable(identifier, make_shared<IntegerEvalValue>(i));
this -> _evaluationScope -> SetVariable(identifier, new IntegerEvalValue(i));
for (auto s: statements) {
this->EvaluateStatement(s);
if (this->_hasReturned || this -> _hasBroken)
@@ -159,7 +163,7 @@ namespace Porygon::Evaluation {
}
} else{
for (long i = start; i >= end; i += step){
this -> _evaluationScope -> SetVariable(identifier, make_shared<IntegerEvalValue>(i));
this -> _evaluationScope -> SetVariable(identifier, new IntegerEvalValue(i));
for (auto s: statements) {
this->EvaluateStatement(s);
if (this->_hasReturned || this -> _hasBroken)
@@ -184,10 +188,10 @@ namespace Porygon::Evaluation {
auto block = (BoundBlockStatement*)statement -> GetBlock();
auto statements = *block -> GetStatements();
while (iterator->MoveNext()){
auto currentKey = iterator->GetCurrent();
auto currentKey = EvalValuePointer(iterator->GetCurrent());
this -> _evaluationScope -> SetVariable(keyVariable, currentKey);
if (valueVariable != nullptr){
auto currentValue = iteratorVal -> IndexValue(currentKey.get());
auto currentValue = EvalValuePointer(iteratorVal -> IndexValue(currentKey.Get()));
this -> _evaluationScope -> SetVariable(valueVariable, currentValue);
}
for (auto s: statements) {
@@ -206,7 +210,7 @@ namespace Porygon::Evaluation {
auto condition = statement -> GetCondition();
auto block = (BoundBlockStatement*)statement -> GetBlock();
auto statements = *block -> GetStatements();
while (this->EvaluateBoolExpression(condition)->EvaluateBool()){
while (this->EvaluateExpression(condition)->EvaluateBool()){
for (auto s: statements) {
this->EvaluateStatement(s);
if (this->_hasReturned || this -> _hasBroken)
@@ -222,161 +226,75 @@ namespace Porygon::Evaluation {
// Expressions //
/////////////////
shared_ptr<const EvalValue> Evaluator::EvaluateExpression(const BoundExpression *expression) {
auto type = expression->GetType();
switch (type->GetClass()) {
case TypeClass::Number:
return this->EvaluateIntegerExpression(expression);
case TypeClass::Bool:
return this->EvaluateBoolExpression(expression);
case TypeClass::String:
return this->EvaluateStringExpression(expression);
case TypeClass::Function:
return this->EvaluateFunctionExpression(expression);
case TypeClass::Nil:
return this->EvaluateNilExpression(expression);
case TypeClass::Table:
return this->EvaluateTableExpression(expression);
case TypeClass::UserData:
return this->EvaluateUserDataExpression(expression);
default:
throw;
}
}
EvalValuePointer Evaluator::EvaluateExpression(const BoundExpression *expression) {
switch (expression->GetKind()){
shared_ptr<const EvalValue> Evaluator::GetVariable(const BoundVariableExpression *expression) {
auto variable = this->_evaluationScope->GetVariable(expression->GetKey());
if (variable == nullptr) {
throw EvaluationException("Variable not found");
}
return variable->Clone();
}
shared_ptr<const NumericEvalValue> Evaluator::EvaluateIntegerExpression(const BoundExpression *expression) {
switch (expression->GetKind()) {
case BoundExpressionKind::Bad: throw;
case BoundExpressionKind::LiteralInteger:
return make_shared<IntegerEvalValue>(((BoundLiteralIntegerExpression *) expression)->GetValue());
return new IntegerEvalValue(((BoundLiteralIntegerExpression *) expression)->GetValue());
case BoundExpressionKind::LiteralFloat:
return make_shared<FloatEvalValue>(((BoundLiteralFloatExpression *) expression)->GetValue());
case BoundExpressionKind::Unary:
return this->EvaluateIntegerUnary((BoundUnaryExpression *) expression);
case BoundExpressionKind::Binary:
return this->EvaluateIntegerBinary((BoundBinaryExpression *) expression);
case BoundExpressionKind::Variable:
return dynamic_pointer_cast<const NumericEvalValue>(
this->GetVariable((BoundVariableExpression *) expression));
case BoundExpressionKind::FunctionCall:
return dynamic_pointer_cast<const NumericEvalValue>(this->EvaluateFunctionCallExpression(expression));
case BoundExpressionKind::Index:
return dynamic_pointer_cast<const NumericEvalValue>(this->EvaluateIndexExpression(expression));
case BoundExpressionKind::PeriodIndex:
return dynamic_pointer_cast<const NumericEvalValue>(this->EvaluatePeriodIndexExpression(expression));
default:
throw;
}
}
shared_ptr<const 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);
case BoundExpressionKind::Binary:
return this->EvaluateBooleanBinary((BoundBinaryExpression *) expression);
case BoundExpressionKind::Variable:
return dynamic_pointer_cast<const BooleanEvalValue>(
this->GetVariable((BoundVariableExpression *) expression));
case BoundExpressionKind::FunctionCall:
return dynamic_pointer_cast<const BooleanEvalValue>(this->EvaluateFunctionCallExpression(expression));
case BoundExpressionKind::Index:
return dynamic_pointer_cast<const BooleanEvalValue>(this->EvaluateIndexExpression(expression));
case BoundExpressionKind::PeriodIndex:
return dynamic_pointer_cast<const BooleanEvalValue>(this->EvaluatePeriodIndexExpression(expression));
default:
throw;
}
}
shared_ptr<const StringEvalValue> Evaluator::EvaluateStringExpression(const BoundExpression *expression) {
switch (expression->GetKind()) {
return new FloatEvalValue(((BoundLiteralFloatExpression *) expression)->GetValue());
case BoundExpressionKind::LiteralString:
return make_shared<StringEvalValue>(*((BoundLiteralStringExpression *) expression)->GetValue());
return new StringEvalValue(*((BoundLiteralStringExpression *) expression)->GetValue());
case BoundExpressionKind::LiteralBool:
return new BooleanEvalValue(((BoundLiteralBoolExpression *) expression)->GetValue());
case BoundExpressionKind::Variable:
return this -> GetVariable((BoundVariableExpression*)expression);
case BoundExpressionKind::Unary:
return this->EvaluateUnary((BoundUnaryExpression *) expression);
case BoundExpressionKind::Binary:
return this->EvaluateStringBinary((BoundBinaryExpression *) expression);
case BoundExpressionKind::Variable:
return dynamic_pointer_cast<const StringEvalValue>(this->GetVariable((BoundVariableExpression *) expression));
return this->EvaluateBinary((BoundBinaryExpression *) expression);
case BoundExpressionKind::FunctionCall:
return dynamic_pointer_cast<const StringEvalValue>(this->EvaluateFunctionCallExpression(expression));
return this->EvaluateFunctionCallExpression(expression);
case BoundExpressionKind::Index:
return dynamic_pointer_cast<const StringEvalValue>(this->EvaluateIndexExpression(expression));
case BoundExpressionKind::PeriodIndex:
return dynamic_pointer_cast<const StringEvalValue>(this->EvaluatePeriodIndexExpression(expression));
default:
throw;
}
}
shared_ptr<const EvalValue> Evaluator::EvaluateFunctionExpression(const BoundExpression *expression) {
switch (expression->GetKind()) {
case BoundExpressionKind::Variable:
return this->GetVariable((BoundVariableExpression *) expression);
case BoundExpressionKind::Index:
return this->EvaluateIndexExpression(expression);
return this->EvaluateIndexExpression(expression).Take();
case BoundExpressionKind::PeriodIndex:
return this->EvaluatePeriodIndexExpression(expression);
default:
throw;
}
}
shared_ptr<const EvalValue> Evaluator::EvaluateNilExpression(const BoundExpression *expression) {
switch (expression->GetKind()) {
case BoundExpressionKind::FunctionCall:
return this->EvaluateFunctionCallExpression(expression);
default:
return nullptr;
}
}
shared_ptr<const EvalValue> Evaluator::EvaluateTableExpression(const BoundExpression *expression) {
switch (expression->GetKind()) {
case BoundExpressionKind::FunctionCall:
return this->EvaluateFunctionCallExpression(expression);
case BoundExpressionKind::Variable:
return this->GetVariable((BoundVariableExpression *) expression);
case BoundExpressionKind::Index:
return this->EvaluateIndexExpression(expression);
case BoundExpressionKind::NumericalTable:
return this->EvaluateNumericTableExpression(expression);
case BoundExpressionKind::Table:
return this->EvaluateComplexTableExpression(expression);
case BoundExpressionKind::PeriodIndex:
return this->EvaluatePeriodIndexExpression(expression);
default:
throw;
}
}
EvalValuePointer Evaluator::EvaluateBinary(const BoundBinaryExpression *expression){
auto leftValue = this -> EvaluateExpression(expression->GetLeft());
auto rightValue = this -> EvaluateExpression(expression->GetRight());
auto operation = expression->GetOperation();
if (operation == BoundBinaryOperation::Equality){
return new BooleanEvalValue(leftValue == rightValue);
} else if (operation == BoundBinaryOperation::Inequality){
return new BooleanEvalValue(leftValue != rightValue);
}
return leftValue->BinaryOperation(operation, rightValue.Get());
}
shared_ptr<const EvalValue> Evaluator::EvaluateFunctionCallExpression(const BoundExpression *expression) {
EvalValuePointer Evaluator::EvaluateUnary(const BoundUnaryExpression *expression) {
auto exp = expression->GetOperand();
auto val = EvaluateExpression(exp);
return val->UnaryOperation(expression->GetOperation());
}
EvalValuePointer Evaluator::GetVariable(const BoundVariableExpression *expression) {
auto variable = this->_evaluationScope->GetVariable(expression->GetKey());
if (variable.Get() == nullptr) {
throw EvaluationException("Variable not found");
}
return variable;
}
EvalValuePointer Evaluator::EvaluateFunctionCallExpression(const BoundExpression *expression) {
auto functionCall = (BoundFunctionCallExpression *) expression;
auto function = dynamic_pointer_cast<const GenericFunctionEvalValue>(
this->EvaluateExpression(functionCall->GetFunctionExpression()));
auto function = (GenericFunctionEvalValue*)this->EvaluateExpression(functionCall->GetFunctionExpression()).Clone();
auto boundParameters = functionCall->GetParameters();
auto parameters = vector<shared_ptr<const EvalValue>>(boundParameters->size());
auto parameters = vector<EvalValuePointer>(boundParameters->size());
for (size_t i = 0; i < boundParameters->size(); i++) {
parameters[i] = this->EvaluateExpression(boundParameters->at(i));
}
auto type = std::dynamic_pointer_cast<const GenericFunctionScriptType>(function->GetType());
auto func = dynamic_pointer_cast<const GenericFunctionEvalValue>(function);
auto func = function;
auto option = functionCall ->GetFunctionOption();
auto opt = func->GetOption(option->GetOptionId());
if (option -> IsScriptFunction()){
@@ -390,28 +308,30 @@ namespace Porygon::Evaluation {
for (int i = 0; i < parameterTypes.size() && i < parameterKeys.size() && i < parameters.size(); i++) {
auto parameter = parameters[i];
auto key = parameterKeys.at(i);
this->_evaluationScope->CreateVariable(key.get(), parameter->Clone());
this->_evaluationScope->CreateVariable(key.get(), parameter.Clone());
}
this->EvaluateBlockStatement(scriptOption->GetInnerBlock().get());
this->_evaluationScope = originalScope;
this->_hasReturned = false;
auto r = this->_returnValue;
this->_returnValue = nullptr;
this->_returnValue.ClearAssign(nullptr);
delete function;
return r;
} else{
auto scriptOption = dynamic_pointer_cast<const UserData::UserDataFunction>(opt);
const EvalValue* arr[parameters.size()];
for (size_t i = 0; i < parameters.size(); i++){
arr[i] = parameters[i].get();
arr[i] = parameters[i].Get();
}
return shared_ptr<const EvalValue>(scriptOption -> Call(arr, parameters.size()));
delete function;
return scriptOption -> Call(arr, parameters.size());
}
}
shared_ptr<const EvalValue> Evaluator::EvaluateFunction(const GenericFunctionEvalValue *function,
const EvalValue* Evaluator::EvaluateFunction(const GenericFunctionEvalValue *function,
const vector<EvalValue *> &parameters) {
auto type = std::dynamic_pointer_cast<const GenericFunctionScriptType>(function->GetType());
auto type = function->GetType();
auto option = dynamic_cast<const ScriptFunctionOption*>(type->GetFirstOption());
auto parameterTypes = option->GetParameterTypes();
@@ -429,42 +349,43 @@ namespace Porygon::Evaluation {
this->EvaluateBlockStatement(scriptOption->GetInnerBlock().get());
this->_evaluationScope = originalScope;
this->_hasReturned = false;
auto r = this->_returnValue;
this->_returnValue = nullptr;
auto r = this->_returnValue.Take();
delete function;
return r;
}
shared_ptr<const EvalValue> Evaluator::EvaluateIndexExpression(const BoundExpression *expression) {
EvalValuePointer 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());
return indexable->IndexValue(index.Get());
}
shared_ptr<const EvalValue> Evaluator::EvaluatePeriodIndexExpression(const BoundExpression *expression) {
EvalValuePointer Evaluator::EvaluatePeriodIndexExpression(const BoundExpression *expression) {
auto indexExpression = (BoundPeriodIndexExpression *) expression;
auto index = indexExpression->GetIndex()->GetHash();
auto indexable = this->EvaluateExpression(indexExpression->GetIndexableExpression());
return indexable->IndexValue(index)->Clone();
return indexable->IndexValue(index);
}
shared_ptr<const EvalValue> Evaluator::EvaluateNumericTableExpression(const BoundExpression *expression) {
EvalValuePointer Evaluator::EvaluateNumericTableExpression(const BoundExpression *expression) {
auto tableExpression = (BoundNumericalTableExpression *) expression;
auto valueExpressions = tableExpression->GetExpressions();
auto values = new vector<shared_ptr<const EvalValue>>(valueExpressions->size());
auto values = new vector<EvalValuePointer>(valueExpressions->size());
for (size_t i = 0; i < valueExpressions->size(); i++) {
auto val = this->EvaluateExpression(valueExpressions->at(i));
values->at(i) = val;
values->at(i) = val.Take();
}
auto valuesPointer = shared_ptr<vector<shared_ptr<const EvalValue>>>(values);
return make_shared<NumericalTableEvalValue>(valuesPointer);
auto valuesPointer = shared_ptr<vector<EvalValuePointer>>(values);
return new NumericalTableEvalValue(valuesPointer);
}
shared_ptr<const EvalValue> Evaluator::EvaluateComplexTableExpression(const BoundExpression *expression) {
EvalValuePointer Evaluator::EvaluateComplexTableExpression(const BoundExpression *expression) {
auto tableExpression = (BoundTableExpression *) expression;
auto type = dynamic_pointer_cast<const TableScriptType>(tableExpression->GetType());
const auto& baseType = tableExpression -> GetType();
auto type = dynamic_pointer_cast<const TableScriptType>(baseType);
auto declaredVars = type->GetValues();
auto variables = make_shared<map<Utilities::HashedString, shared_ptr<const EvalValue>>>();
auto variables = make_shared<map<Utilities::HashedString, EvalValuePointer>>();
for (const auto& i : *declaredVars) {
variables->insert({i.first, nullptr});
}
@@ -473,17 +394,6 @@ namespace Porygon::Evaluation {
this->_evaluationScope = evaluator;
this->EvaluateBlockStatement(tableExpression->GetBlock());
this->_evaluationScope = currentEvaluator;
return make_shared<TableEvalValue>(variables);
}
shared_ptr<const EvalValue> Evaluator::EvaluateUserDataExpression(const BoundExpression *expression) {
switch (expression->GetKind()) {
case BoundExpressionKind::Variable:
return this->GetVariable((BoundVariableExpression *) expression);
case BoundExpressionKind::Index:
return this->EvaluateIndexExpression(expression);
default:
throw;
}
return new TableEvalValue(variables);
}
}