Implements return statement
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2019-06-07 15:23:13 +02:00
parent f143e526ab
commit f4a3918947
12 changed files with 145 additions and 18 deletions

View File

@@ -25,6 +25,7 @@ BoundStatement* Binder::BindStatement(ParsedStatement* statement){
case ParsedStatementKind ::Expression: return this -> BindExpressionStatement(statement);
case ParsedStatementKind::Assignment: return this -> BindAssignmentStatement(statement);
case ParsedStatementKind ::FunctionDeclaration: return this->BindFunctionDeclarationStatement(statement);
case ParsedStatementKind::Return: return this -> BindReturnStatement(statement);
case ParsedStatementKind::Bad: return new BoundBadStatement();
}
@@ -105,6 +106,31 @@ BoundStatement *Binder::BindFunctionDeclarationStatement(ParsedStatement *statem
return new BoundBadStatement();
}
BoundStatement *Binder::BindReturnStatement(ParsedStatement* statement){
auto expression = ((ParsedReturnStatement*)statement)->GetExpression();
shared_ptr<ScriptType> currentReturnType;
if (this->_currentFunction == nullptr){
currentReturnType = this->_scriptData->GetReturnType();
} else{
currentReturnType = this->_currentFunction;
}
if (expression == nullptr && currentReturnType != nullptr){
this -> _scriptData -> Diagnostics -> LogError(DiagnosticCode::InvalidReturnType, statement->GetStartPosition(), statement->GetLength());
return new BoundBadStatement();
}
auto boundExpression = this->BindExpression(expression);
auto expresionType = boundExpression->GetType();
if (currentReturnType == nullptr){
currentReturnType.swap(expresionType);
return new BoundReturnStatement(boundExpression);
}
if (currentReturnType.get()->operator!=(expresionType.get())){
this -> _scriptData -> Diagnostics -> LogError(DiagnosticCode::InvalidReturnType, statement->GetStartPosition(), statement->GetLength());
return new BoundBadStatement();
}
return new BoundReturnStatement(boundExpression);
}
BoundExpression* Binder::BindExpression(ParsedExpression* expression){
switch (expression -> GetKind()){
case ParsedExpressionKind ::LiteralInteger: