Implements userdata function support
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2019-06-21 17:03:13 +02:00
parent 6f7d319148
commit 95c322ed2c
9 changed files with 245 additions and 57 deletions

View File

@@ -1,5 +1,7 @@
#include <utility>
#include <utility>
#ifndef PORYGONLANG_SCRIPTFUNCTIONEVALVALUE_HPP
#define PORYGONLANG_SCRIPTFUNCTIONEVALVALUE_HPP
@@ -11,30 +13,19 @@
#include "../Evaluator.hpp"
#include "../EvaluationScope/EvaluationScope.hpp"
using namespace std;
namespace Porygon::Evaluation {
class ScriptFunctionEvalValue : public EvalValue {
const std::shared_ptr<BoundBlockStatement> _innerBlock;
const std::shared_ptr<FunctionScriptType> _type;
const std::shared_ptr<EvaluationScope> _scope;
class GenericFunctionEvalValue : public EvalValue{
protected:
const std::shared_ptr<GenericFunctionScriptType> _type;
const std::size_t _hash;
explicit ScriptFunctionEvalValue(std::shared_ptr<BoundBlockStatement> innerBlock,
std::shared_ptr<EvaluationScope> scope,
std::shared_ptr<FunctionScriptType> type, size_t hash)
: _type(std::move(type)),
_innerBlock(std::move(innerBlock)),
_scope(std::move(scope)),
_hash(hash) {
}
public:
explicit ScriptFunctionEvalValue(std::shared_ptr<BoundBlockStatement> innerBlock,
std::shared_ptr<EvaluationScope> scope,
std::shared_ptr<FunctionScriptType> type)
: _type(std::move(type)),
_innerBlock(std::move(innerBlock)),
_scope(std::move(scope)),
_hash(rand()) {
GenericFunctionEvalValue(shared_ptr<GenericFunctionScriptType> type, size_t hash)
: _type(move(type)),
_hash(hash){
}
const std::shared_ptr<ScriptType> GetType() const {
@@ -45,25 +36,48 @@ namespace Porygon::Evaluation {
return TypeClass::Function;
}
const shared_ptr<EvalValue> Clone() const final {
// We don't run make_shared here as it can't call private constructors
return shared_ptr<ScriptFunctionEvalValue>(new ScriptFunctionEvalValue(_innerBlock, _scope, _type, _hash));
}
const bool operator==(EvalValue *b) const final {
if (b->GetTypeClass() != TypeClass::Function)
return false;
return this->_hash == ((ScriptFunctionEvalValue *) b)->_hash;
return this->_hash == ((GenericFunctionEvalValue *) b)->_hash;
};
const std::shared_ptr<BoundBlockStatement> &GetInnerBlock() const {
return _innerBlock;
}
const std::size_t GetHashCode() const final {
return _hash;
}
};
class ScriptFunctionEvalValue : public GenericFunctionEvalValue {
const std::shared_ptr<BoundBlockStatement> _innerBlock;
const std::shared_ptr<EvaluationScope> _scope;
explicit ScriptFunctionEvalValue(shared_ptr<BoundBlockStatement> innerBlock,
shared_ptr<EvaluationScope> scope,
shared_ptr<FunctionScriptType> type, size_t hash)
: GenericFunctionEvalValue(move(type), hash),
_innerBlock(std::move(innerBlock)),
_scope(std::move(scope)){
}
public:
explicit ScriptFunctionEvalValue(std::shared_ptr<BoundBlockStatement> innerBlock,
std::shared_ptr<EvaluationScope> scope,
std::shared_ptr<FunctionScriptType> type)
: GenericFunctionEvalValue(move(type), rand()),
_innerBlock(std::move(innerBlock)),
_scope(std::move(scope)){
}
const shared_ptr<EvalValue> Clone() const final {
// We don't run make_shared here as it can't call private constructors
return shared_ptr<ScriptFunctionEvalValue>(new ScriptFunctionEvalValue(_innerBlock, _scope,
dynamic_pointer_cast<FunctionScriptType>(_type), _hash));
}
const std::shared_ptr<BoundBlockStatement> &GetInnerBlock() const {
return _innerBlock;
}
const std::shared_ptr<EvaluationScope> &GetScope() const {
return _scope;

View File

@@ -9,6 +9,7 @@
#include "EvalValues/TableEvalValue.hpp"
#include "../Binder/BoundExpressions/BoundTableExpression.hpp"
#include "../TableScriptType.hpp"
#include "../UserData/UserDataFunction.hpp"
using namespace std;
using namespace Porygon::Binder;
@@ -274,7 +275,7 @@ namespace Porygon::Evaluation {
const shared_ptr<EvalValue> Evaluator::EvaluateFunctionCallExpression(const BoundExpression *expression) {
auto functionCall = (BoundFunctionCallExpression *) expression;
auto function = dynamic_pointer_cast<ScriptFunctionEvalValue>(
auto function = dynamic_pointer_cast<GenericFunctionEvalValue>(
this->EvaluateExpression(functionCall->GetFunctionExpression()));
auto boundParameters = functionCall->GetParameters();
@@ -283,24 +284,35 @@ namespace Porygon::Evaluation {
parameters[i] = this->EvaluateExpression(boundParameters->at(i));
}
auto type = std::dynamic_pointer_cast<FunctionScriptType>(function->GetType());
auto type = std::dynamic_pointer_cast<GenericFunctionScriptType>(function->GetType());
auto parameterTypes = type->GetParameterTypes();
auto parameterKeys = type->GetParameterKeys();
auto originalScope = this->_evaluationScope;
this->_evaluationScope = function->GetScope();
if (type -> IsScriptFunction()){
auto scriptFunctionType = std::dynamic_pointer_cast<FunctionScriptType>(type);
auto parameterKeys = scriptFunctionType->GetParameterKeys();
auto originalScope = this->_evaluationScope;
auto scriptFunction = dynamic_pointer_cast<ScriptFunctionEvalValue>(function);
this->_evaluationScope = scriptFunction->GetScope();
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());
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->EvaluateBlockStatement(scriptFunction->GetInnerBlock().get());
this->_evaluationScope = originalScope;
this->_hasReturned = false;
auto r = this->_returnValue;
this->_returnValue = nullptr;
return r;
} else{
auto userDataFunction = dynamic_pointer_cast<UserData::UserDataFunction>(function);
EvalValue* arr[parameters.size()];
for (int i = 0; i < parameters.size(); i++){
arr[i] = parameters[i].get();
}
return shared_ptr<EvalValue>(userDataFunction -> Call(arr, parameters.size()));
}
this->EvaluateBlockStatement(function->GetInnerBlock().get());
this->_evaluationScope = originalScope;
this->_hasReturned = false;
auto r = this->_returnValue;
this->_returnValue = nullptr;
return r;
}
const shared_ptr<EvalValue> Evaluator::EvaluateFunction(const ScriptFunctionEvalValue *function,