Implements return statement
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:
@@ -1,8 +1,4 @@
|
||||
#include <utility>
|
||||
|
||||
#include <utility>
|
||||
|
||||
|
||||
#include <memory>
|
||||
#include "Evaluator.hpp"
|
||||
#include "EvaluationException.hpp"
|
||||
@@ -18,12 +14,15 @@ void Evaluator::Evaluate(BoundScriptStatement *statement) {
|
||||
}
|
||||
|
||||
void Evaluator::EvaluateStatement(BoundStatement *statement) {
|
||||
if (this->_hasReturned)
|
||||
return;
|
||||
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);
|
||||
case BoundStatementKind ::Assignment: return this -> EvaluateAssignmentStatement((BoundAssignmentStatement*)statement);
|
||||
case BoundStatementKind ::FunctionDeclaration: return this->EvaluateFunctionDeclarationStatement((BoundFunctionDeclarationStatement*)statement);
|
||||
case BoundStatementKind::Return: return this -> EvaluateReturnStatement((BoundReturnStatement*)statement);
|
||||
|
||||
case BoundStatementKind::Bad:
|
||||
throw;
|
||||
@@ -34,6 +33,8 @@ void Evaluator::EvaluateBlockStatement(BoundBlockStatement* statement) {
|
||||
this->_evaluationScope->OuterScope();
|
||||
for (auto s: statement->GetStatements()){
|
||||
this -> EvaluateStatement(s);
|
||||
if (this->_hasReturned)
|
||||
break;
|
||||
}
|
||||
this->_evaluationScope->InnerScope();
|
||||
}
|
||||
@@ -65,6 +66,16 @@ void Evaluator::EvaluateFunctionDeclarationStatement(BoundFunctionDeclarationSta
|
||||
}
|
||||
}
|
||||
|
||||
void Evaluator::EvaluateReturnStatement(BoundReturnStatement* statement){
|
||||
auto expression = statement->GetExpression();
|
||||
this->_hasReturned = true;
|
||||
if (expression == nullptr){
|
||||
return;
|
||||
}
|
||||
auto value = this -> EvaluateExpression(expression);
|
||||
this -> _returnValue = value;
|
||||
}
|
||||
|
||||
shared_ptr<EvalValue> Evaluator::EvaluateExpression(BoundExpression *expression) {
|
||||
auto type = expression -> GetType();
|
||||
switch (type->GetClass()){
|
||||
@@ -174,11 +185,13 @@ shared_ptr<EvalValue> Evaluator::EvaluateFunctionCallExpression(BoundExpression*
|
||||
this->_evaluationScope->CreateVariable(key->GetScopeId(), key->GetIdentifier(), parameter->Clone());
|
||||
}
|
||||
this->EvaluateBlockStatement(function->GetInnerBlock().get());
|
||||
return nullptr;
|
||||
|
||||
this->_hasReturned = false;
|
||||
auto r = this -> _returnValue;
|
||||
this -> _returnValue = nullptr;
|
||||
return r;
|
||||
}
|
||||
|
||||
EvalValue* Evaluator::EvaluateFunction(ScriptFunctionEvalValue *function, vector<EvalValue *> parameters) {
|
||||
shared_ptr<EvalValue> Evaluator::EvaluateFunction(ScriptFunctionEvalValue *function, vector<EvalValue *> parameters) {
|
||||
auto type = std::dynamic_pointer_cast<FunctionScriptType>(function->GetType());
|
||||
auto parameterTypes = type->GetParameterTypes();
|
||||
auto parameterKeys = type->GetParameterKeys();
|
||||
@@ -192,7 +205,10 @@ EvalValue* Evaluator::EvaluateFunction(ScriptFunctionEvalValue *function, vector
|
||||
this->_evaluationScope->CreateVariable(key->GetScopeId(), key->GetIdentifier(), parameter->Clone());
|
||||
}
|
||||
this->EvaluateBlockStatement(function->GetInnerBlock().get());
|
||||
return nullptr;
|
||||
this->_hasReturned = false;
|
||||
auto r = this -> _returnValue;
|
||||
this -> _returnValue = nullptr;
|
||||
return r;
|
||||
}
|
||||
|
||||
shared_ptr<EvalValue> Evaluator::EvaluateIndexExpression(BoundExpression *expression) {
|
||||
|
||||
Reference in New Issue
Block a user