Implements support for functions with the same name, but different parameters
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:
112
src/FunctionScriptType.hpp
Normal file
112
src/FunctionScriptType.hpp
Normal file
@@ -0,0 +1,112 @@
|
||||
|
||||
#ifndef PORYGONLANG_FUNCTIONSCRIPTTYPE_HPP
|
||||
#define PORYGONLANG_FUNCTIONSCRIPTTYPE_HPP
|
||||
|
||||
#include "ScriptType.hpp"
|
||||
|
||||
namespace Porygon {
|
||||
class GenericFunctionOption{
|
||||
shared_ptr<ScriptType> _returnType;
|
||||
vector<shared_ptr<ScriptType>> _parameterTypes;
|
||||
size_t _option = 0;
|
||||
public:
|
||||
GenericFunctionOption(shared_ptr<ScriptType> returnType, vector<shared_ptr<ScriptType>> parameterTypes){
|
||||
_returnType = move(returnType);
|
||||
_parameterTypes = move(parameterTypes);
|
||||
}
|
||||
|
||||
virtual ~GenericFunctionOption() = default;
|
||||
|
||||
const shared_ptr<ScriptType> GetReturnType() const {
|
||||
return _returnType;
|
||||
}
|
||||
|
||||
void SetOption(size_t v){
|
||||
_option = v;
|
||||
}
|
||||
|
||||
void SetReturnType(shared_ptr<ScriptType> t) {
|
||||
_returnType = move(t);
|
||||
}
|
||||
|
||||
const vector<shared_ptr<ScriptType>> GetParameterTypes() const {
|
||||
return _parameterTypes;
|
||||
}
|
||||
|
||||
const bool IsValid(const vector<shared_ptr<ScriptType>>& parameters){
|
||||
if (parameters.size() != _parameterTypes.size()){
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < parameters.size(); i++){
|
||||
if (parameters[i]->operator!=(_parameterTypes[i].get())){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
const size_t GetOptionId() const{
|
||||
return _option;
|
||||
}
|
||||
|
||||
virtual const bool IsScriptFunction() const = 0;
|
||||
};
|
||||
|
||||
class GenericFunctionScriptType : public Porygon::ScriptType {
|
||||
vector<GenericFunctionOption *> _options;
|
||||
public:
|
||||
GenericFunctionScriptType()
|
||||
: ScriptType(Porygon::TypeClass::Function){};
|
||||
|
||||
explicit GenericFunctionScriptType(GenericFunctionOption * option)
|
||||
: ScriptType(Porygon::TypeClass::Function){
|
||||
this -> RegisterFunctionOption(option);
|
||||
};
|
||||
|
||||
virtual ~GenericFunctionScriptType() final{
|
||||
for (auto o: _options){
|
||||
delete o;
|
||||
}
|
||||
}
|
||||
|
||||
void RegisterFunctionOption (GenericFunctionOption * opt){
|
||||
opt->SetOption(_options.size());
|
||||
_options.push_back(opt);
|
||||
}
|
||||
|
||||
GenericFunctionOption* GetFunctionOption(const vector<shared_ptr<ScriptType>>& parameters){
|
||||
for (auto o: _options){
|
||||
if (o->IsValid(parameters)){
|
||||
return o;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
GenericFunctionOption* GetFirstOption(){
|
||||
return _options[0];
|
||||
}
|
||||
};
|
||||
|
||||
class ScriptFunctionOption : public GenericFunctionOption {
|
||||
vector<shared_ptr<Porygon::Binder::BoundVariableKey>> _parameterKeys;
|
||||
public:
|
||||
ScriptFunctionOption(shared_ptr<ScriptType> returnType, vector<shared_ptr<ScriptType>> parameterTypes,
|
||||
vector<shared_ptr<Porygon::Binder::BoundVariableKey>> parameterKeys)
|
||||
: GenericFunctionOption(move(returnType), std::move(parameterTypes)) {
|
||||
_parameterKeys = move(parameterKeys);
|
||||
}
|
||||
|
||||
const vector<shared_ptr<Porygon::Binder::BoundVariableKey>> GetParameterKeys() const {
|
||||
return _parameterKeys;
|
||||
}
|
||||
|
||||
const bool IsScriptFunction() const final {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#include "ScriptType.hpp"
|
||||
|
||||
#endif //PORYGONLANG_FUNCTIONSCRIPTTYPE_HPP
|
||||
Reference in New Issue
Block a user