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:

View File

@@ -10,6 +10,7 @@
class Binder {
Script* _scriptData;
BoundScope* _scope;
shared_ptr<FunctionScriptType> _currentFunction;
~Binder();
@@ -18,6 +19,7 @@ class Binder {
BoundStatement *BindExpressionStatement(ParsedStatement *statement);
BoundStatement *BindAssignmentStatement(ParsedStatement *statement);
BoundStatement *BindFunctionDeclarationStatement(ParsedStatement * statement);
BoundStatement *BindReturnStatement(ParsedStatement *statement);
BoundExpression *BindExpression(ParsedExpression *expression);
BoundExpression *BindVariableExpression(VariableExpression *expression);

View File

@@ -1,9 +1,8 @@
#include <utility>
#ifndef PORYGONLANG_BOUNDSTATEMENT_HPP
#define PORYGONLANG_BOUNDSTATEMENT_HPP
#include <utility>
#include <vector>
#include "../BoundExpressions/BoundExpression.hpp"
#include "../BoundVariables/BoundVariableKey.hpp"
@@ -18,6 +17,7 @@ enum class BoundStatementKind{
Expression,
Assignment,
FunctionDeclaration,
Return,
};
class BoundStatement{
@@ -116,6 +116,25 @@ public:
}
};
class BoundReturnStatement : public BoundStatement{
BoundExpression* _expression;
public:
explicit BoundReturnStatement(BoundExpression* expression){
_expression = expression;
}
~BoundReturnStatement() final{
delete _expression;
}
BoundStatementKind GetKind() final{
return BoundStatementKind ::Return;
}
BoundExpression* GetExpression(){
return _expression;
}
};
#include "BoundFunctionDeclarationStatement.hpp"
#endif //PORYGONLANG_BOUNDSTATEMENT_HPP