diff --git a/src/Binder/Binder.cpp b/src/Binder/Binder.cpp index 89114c7..d676a20 100644 --- a/src/Binder/Binder.cpp +++ b/src/Binder/Binder.cpp @@ -172,10 +172,13 @@ namespace Porygon::Binder { } else { currentReturnType = this->_currentFunction->GetReturnType(); } - if (expression == nullptr && currentReturnType != nullptr) { + if (expression == nullptr && (currentReturnType != nullptr && currentReturnType -> GetClass() != TypeClass::Nil)) { this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::InvalidReturnType, statement->GetStartPosition(), statement->GetLength()); return new BoundBadStatement(); + } else if (expression == nullptr){ + currentReturnType = make_shared(TypeClass::Nil); + return new BoundReturnStatement(nullptr); } auto boundExpression = this->BindExpression(expression); auto expresionType = boundExpression->GetType(); diff --git a/src/Evaluator/Evaluator.cpp b/src/Evaluator/Evaluator.cpp index af53223..ac307e9 100644 --- a/src/Evaluator/Evaluator.cpp +++ b/src/Evaluator/Evaluator.cpp @@ -95,6 +95,7 @@ namespace Porygon::Evaluation { auto expression = statement->GetExpression(); if (expression == nullptr) { this->_hasReturned = true; + this -> _returnValue = nullptr; return; } auto value = this->EvaluateExpression(expression); diff --git a/src/Parser/Parser.cpp b/src/Parser/Parser.cpp index e1be27c..8a6daf2 100644 --- a/src/Parser/Parser.cpp +++ b/src/Parser/Parser.cpp @@ -192,8 +192,12 @@ namespace Porygon::Parser { ParsedStatement *Parser::ParseReturnStatement(const IToken *current) { //TODO: if next token is on a different line, don't parse it as return expression. - auto expression = this->ParseExpression(this->Next()); auto start = current->GetStartPosition(); + auto startLine = this -> ScriptData -> Diagnostics ->GetLineFromPosition(start); + if (startLine != this -> ScriptData -> Diagnostics -> GetLineFromPosition(this -> Peek() -> GetStartPosition())){ + return new ParsedReturnStatement(nullptr, start, current->GetLength()); + } + auto expression = this->ParseExpression(this->Next()); return new ParsedReturnStatement(expression, start, expression->GetEndPosition() - start); } diff --git a/tests/integration/Functions.cpp b/tests/integration/Functions.cpp index ed4ee7c..3261c79 100644 --- a/tests/integration/Functions.cpp +++ b/tests/integration/Functions.cpp @@ -136,5 +136,19 @@ test() delete script; } +TEST_CASE( "Return doesn't parse next line expression", "[integration]" ) { + Script* script = Script::Create( + R"( +return +1 + 1 +)" + ); + REQUIRE(!script->Diagnostics -> HasErrors()); + auto result = script->Evaluate(); + REQUIRE(result == nullptr); + + delete script; +} + #endif