Fixes and changes for function declarations, using shared_ptr instead of raw pointers

This commit is contained in:
2019-06-01 13:43:25 +02:00
parent 6936b26cae
commit 1231a77761
8 changed files with 91 additions and 68 deletions

View File

@@ -1,5 +1,8 @@
#include <memory>
#include "Binder.hpp"
#include <memory>
BoundScriptStatement *Binder::Bind(Script* script, ParsedScriptStatement *s, BoundScope* scriptScope) {
auto binder = Binder();
@@ -75,22 +78,27 @@ ScriptType* ParseTypeIdentifier(HashedString s){
BoundStatement *Binder::BindFunctionDeclarationStatement(ParsedStatement *statement) {
auto functionStatement = (ParsedFunctionDeclarationStatement*) statement;
auto parameters = functionStatement->GetParameters();
vector<ScriptType*> parameterTypes = vector<ScriptType*>(parameters.size());
vector<int> parameterKeys = vector<int>(parameters.size());
vector<std::shared_ptr<ScriptType>> parameterTypes = vector<std::shared_ptr<ScriptType>>(parameters.size());
vector<std::shared_ptr<BoundVariableKey>> parameterKeys = vector<std::shared_ptr<BoundVariableKey>>(parameters.size());
this->_scope->GoInnerScope();
auto scopeId = this->_scope->GetCurrentScope();
for (int i = 0; i < parameters.size(); i++){
auto var = parameters[i];
auto parsedType = ParseTypeIdentifier(var->GetType());
parameterTypes[i] = parsedType;
parameterKeys[i] = var->GetIdentifier().GetHash();
this->_scope->CreateExplicitLocal(var->GetIdentifier().GetHash(), *parsedType);
parameterTypes[i] = std::shared_ptr<ScriptType>(parsedType);
auto parameterAssignment = this->_scope->CreateExplicitLocal(var->GetIdentifier().GetHash(), *parsedType);
if (parameterAssignment.GetResult() == VariableAssignmentResult::Ok){
parameterKeys[i] = std::shared_ptr<BoundVariableKey>(parameterAssignment.GetKey());
}
else{
//TODO: log error
continue;
}
}
auto boundBlock = this -> BindBlockStatement(functionStatement->GetBlock());
this->_scope->GoOuterScope();
auto identifier = functionStatement->GetIdentifier();
auto returnType = new ScriptType(TypeClass::Nil);
auto type = new FunctionScriptType(returnType, parameterTypes, parameterKeys, scopeId);
auto returnType = std::make_shared<ScriptType>(TypeClass::Nil);
auto type = new FunctionScriptType(returnType, parameterTypes, parameterKeys);
auto assignment = this->_scope->AssignVariable(identifier.GetHash(), *type);
if (assignment.GetResult() == VariableAssignmentResult::Ok){
return new BoundFunctionDeclarationStatement(type, assignment.GetKey(), (BoundBlockStatement*)boundBlock);