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);

View File

@@ -0,0 +1,43 @@
#ifndef PORYGONLANG_BOUNDFUNCTIONDECLARATIONSTATEMENT_HPP
#define PORYGONLANG_BOUNDFUNCTIONDECLARATIONSTATEMENT_HPP
#include <memory>
#include "BoundStatement.hpp"
class BoundFunctionDeclarationStatement : public BoundStatement{
BoundVariableKey* _key;
std::shared_ptr<BoundBlockStatement> _block;
FunctionScriptType* _type;
public:
BoundFunctionDeclarationStatement(FunctionScriptType* type, BoundVariableKey* key, BoundBlockStatement* block){
_key = key;
_block = shared_ptr<BoundBlockStatement>(block);
_type = type;
}
~BoundFunctionDeclarationStatement() final{
delete _key;
delete _type;
}
BoundStatementKind GetKind() final{
return BoundStatementKind ::FunctionDeclaration;
}
BoundVariableKey* GetKey(){
return _key;
}
std::shared_ptr<BoundBlockStatement> GetBlock(){
return _block;
}
FunctionScriptType* GetType(){
return _type;
}
};
#include "BoundStatement.hpp"
#endif //PORYGONLANG_BOUNDFUNCTIONDECLARATIONSTATEMENT_HPP

View File

@@ -8,6 +8,7 @@
#include "../BoundExpressions/BoundExpression.hpp"
#include "../BoundVariables/BoundVariableKey.hpp"
using namespace std;
enum class BoundStatementKind{
@@ -115,32 +116,6 @@ public:
}
};
class BoundFunctionDeclarationStatement : public BoundStatement{
BoundVariableKey* _key;
BoundBlockStatement* _block;
FunctionScriptType* _type;
public:
BoundFunctionDeclarationStatement(FunctionScriptType* type, BoundVariableKey* key, BoundBlockStatement* block){
_key = key;
_block = block;
_type = type;
}
BoundStatementKind GetKind() final{
return BoundStatementKind ::FunctionDeclaration;
}
BoundVariableKey* GetKey(){
return _key;
}
BoundBlockStatement* GetBlock(){
return _block;
}
FunctionScriptType* GetType(){
return _type;
}
};
#include "BoundFunctionDeclarationStatement.hpp"
#endif //PORYGONLANG_BOUNDSTATEMENT_HPP

View File

@@ -30,7 +30,11 @@ void BoundScope::GoInnerScope() {
}
void BoundScope::GoOuterScope() {
_localScope[_currentScope - 1]->clear();
auto scope = _localScope[_currentScope - 1];
for (auto v : *scope){
delete v.second;
}
scope->clear();
_currentScope--;
}