Implements binding and evaluating function declarations

This commit is contained in:
2019-06-01 12:33:52 +02:00
parent c407ba2f50
commit 6936b26cae
11 changed files with 215 additions and 10 deletions

View File

@@ -0,0 +1,57 @@
#ifndef PORYGONLANG_SCRIPTFUNCTIONEVALVALUE_HPP
#define PORYGONLANG_SCRIPTFUNCTIONEVALVALUE_HPP
#include "../../ScriptType.hpp"
#include "EvalValue.hpp"
#include "../../Binder/BoundStatements/BoundStatement.hpp"
#include "../Evaluator.hpp"
class ScriptFunctionEvalValue : public EvalValue{
BoundBlockStatement* _innerBlock;
FunctionScriptType _type;
public:
explicit ScriptFunctionEvalValue(BoundBlockStatement* innerBlock, const FunctionScriptType& type)
: _type(type)
{
_innerBlock = innerBlock;
}
EvalValue* Clone() final{
return new ScriptFunctionEvalValue(_innerBlock, _type);
}
~ScriptFunctionEvalValue() final{
delete _innerBlock;
}
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){
throw EvaluationException("Passed wrong type to function.");
}
auto key = parameterKeys[i];
scope->CreateVariable(_type.GetScopeId(), key, parameter->Clone());
}
evaluator->EvaluateBlockStatement(_innerBlock);
return nullptr;
}
};
#endif //PORYGONLANG_SCRIPTFUNCTIONEVALVALUE_HPP