Make functions be able to call themselves
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2019-06-08 16:02:21 +02:00
parent 7d75131822
commit 7ed53193de
6 changed files with 49 additions and 29 deletions

View File

@@ -77,34 +77,34 @@ std::shared_ptr<ScriptType> ParseTypeIdentifier(HashedString s){
BoundStatement *Binder::BindFunctionDeclarationStatement(ParsedStatement *statement) {
auto functionStatement = (ParsedFunctionDeclarationStatement*) statement;
auto parameters = functionStatement->GetParameters();
auto parameterTypes = new vector<std::shared_ptr<ScriptType>>(parameters.size());
auto parameterKeys = new vector<std::shared_ptr<BoundVariableKey>>(parameters.size());
auto parameterTypes = vector<shared_ptr<ScriptType>>(parameters.size());
auto parameterKeys = vector<shared_ptr<BoundVariableKey>>(parameters.size());
this->_scope->GoInnerScope();
for (int i = 0; i < parameters.size(); i++){
auto var = parameters[i];
auto parsedType = ParseTypeIdentifier(var->GetType());
parameterTypes->at(i) = parsedType;
parameterTypes.at(i) = parsedType;
auto parameterAssignment = this->_scope->CreateExplicitLocal(var->GetIdentifier().GetHash(), parsedType);
if (parameterAssignment.GetResult() == VariableAssignmentResult::Ok){
parameterKeys -> at(i) = std::shared_ptr<BoundVariableKey>(parameterAssignment.GetKey());
parameterKeys.at(i) = std::shared_ptr<BoundVariableKey>(parameterAssignment.GetKey());
}
else{
//TODO: log error
continue;
}
}
auto identifier = functionStatement->GetIdentifier();
auto returnType = make_shared<ScriptType>(TypeClass::Nil);
auto type = make_shared<FunctionScriptType>(returnType, parameterTypes, parameterKeys);
auto assignment = this->_scope->AssignVariable(identifier.GetHash(), type);
if (assignment.GetResult() != VariableAssignmentResult::Ok){
return new BoundBadStatement();
}
auto boundBlock = this -> BindBlockStatement(functionStatement->GetBlock());
this->_scope->GoOuterScope();
auto identifier = functionStatement->GetIdentifier();
auto returnType = std::make_shared<ScriptType>(TypeClass::Nil);
auto parameterTypesPtr = std::shared_ptr<std::vector<std::shared_ptr<ScriptType>>>(parameterTypes);
auto parameterKeysPtr = std::shared_ptr<std::vector<std::shared_ptr<BoundVariableKey>>>(parameterKeys);
auto type = make_shared<FunctionScriptType>(returnType, parameterTypesPtr, parameterKeysPtr);
auto assignment = this->_scope->AssignVariable(identifier.GetHash(), type);
if (assignment.GetResult() == VariableAssignmentResult::Ok){
return new BoundFunctionDeclarationStatement(type, assignment.GetKey(), (BoundBlockStatement*)boundBlock);
}
return new BoundBadStatement();
return new BoundFunctionDeclarationStatement(type, assignment.GetKey(), (BoundBlockStatement*)boundBlock);
}
BoundStatement *Binder::BindReturnStatement(ParsedStatement* statement){
@@ -354,7 +354,7 @@ BoundExpression* Binder::BindFunctionCall(FunctionCallExpression* expression){
auto functionType = std::dynamic_pointer_cast<FunctionScriptType>(type);
auto parameterTypes = functionType->GetParameterTypes();
auto givenParameters = expression->GetParameters();
if (parameterTypes->size() != givenParameters.size()){
if (parameterTypes.size() != givenParameters.size()){
this->_scriptData->Diagnostics->LogError(DiagnosticCode::ParameterCountMismatch, expression->GetStartPosition(),
expression->GetLength());
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
@@ -363,7 +363,7 @@ BoundExpression* Binder::BindFunctionCall(FunctionCallExpression* expression){
for (int i = 0; i < givenParameters.size(); i++){
auto parameter = givenParameters[i];
auto boundParameter = this -> BindExpression(parameter);
if (boundParameter->GetType().get()->operator!=(parameterTypes.get()-> at(i).get())){
if (boundParameter->GetType().get()->operator!=(parameterTypes.at(i).get())){
this->_scriptData->Diagnostics->LogError(DiagnosticCode::ParameterTypeMismatch, parameter->GetStartPosition(),
parameter->GetLength());
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());

View File

@@ -5,7 +5,7 @@
#include "BoundStatements/BoundStatement.hpp"
#include "../Script.hpp"
#include "BoundVariables/BoundScope.hpp"
using namespace std;
class Binder {
Script* _scriptData;