Large overhaul of pointers to shared_ptrs, implemented function evaluation

This commit is contained in:
2019-06-01 19:20:31 +02:00
parent 8b70eed516
commit 4408cf00cd
17 changed files with 261 additions and 129 deletions

View File

@@ -1,3 +1,5 @@
#include <utility>
#ifndef PORYGONLANG_SCRIPTFUNCTIONEVALVALUE_HPP
#define PORYGONLANG_SCRIPTFUNCTIONEVALVALUE_HPP
@@ -11,44 +13,32 @@
class ScriptFunctionEvalValue : public EvalValue{
std::shared_ptr<BoundBlockStatement> _innerBlock;
FunctionScriptType _type;
std::shared_ptr<FunctionScriptType> _type;
public:
explicit ScriptFunctionEvalValue(std::shared_ptr<BoundBlockStatement> innerBlock, FunctionScriptType type)
explicit ScriptFunctionEvalValue(std::shared_ptr<BoundBlockStatement> innerBlock, std::shared_ptr<FunctionScriptType> type)
: _type(std::move(type))
{
_innerBlock = std::move(innerBlock);
}
std::shared_ptr<ScriptType> GetType() final{
return _type;
}
EvalValue* Clone() final{
return new ScriptFunctionEvalValue(_innerBlock, _type);
}
ScriptType* GetType() final{
return &_type;
};
bool operator ==(EvalValue* b) final{
if (b->GetType()->GetClass() != TypeClass::Function)
return false;
return this->_innerBlock == ((ScriptFunctionEvalValue*)b)->_innerBlock;
};
EvalValue* EvaluateFunction(Evaluator* evaluator, const vector<EvalValue*>& parameters){
auto parameterTypes = _type.GetParameterTypes();
auto parameterKeys = _type.GetParameterKeys();
auto scope = evaluator->GetScope();
for (int i = 0; i < parameterTypes.size() && i < parameterKeys.size() && i < parameters.size(); i++){
auto parameter = parameters[i];
auto requiredType = parameterTypes[i];
if (parameter->GetType() != requiredType.get()){
throw EvaluationException("Passed wrong type to function.");
}
auto key = parameterKeys[i];
scope->CreateVariable(key->GetScopeId(), key->GetIdentifier(), parameter->Clone());
}
evaluator->EvaluateBlockStatement(_innerBlock.get());
return nullptr;
std::shared_ptr<BoundBlockStatement> GetInnerBlock(){
return _innerBlock;
}
};