Don't parse expression on different line from return keyword
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2019-06-19 16:21:21 +02:00
parent b76548da16
commit 6f7d319148
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
4 changed files with 24 additions and 2 deletions

View File

@ -172,10 +172,13 @@ namespace Porygon::Binder {
} else { } else {
currentReturnType = this->_currentFunction->GetReturnType(); 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(), this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::InvalidReturnType, statement->GetStartPosition(),
statement->GetLength()); statement->GetLength());
return new BoundBadStatement(); return new BoundBadStatement();
} else if (expression == nullptr){
currentReturnType = make_shared<ScriptType>(TypeClass::Nil);
return new BoundReturnStatement(nullptr);
} }
auto boundExpression = this->BindExpression(expression); auto boundExpression = this->BindExpression(expression);
auto expresionType = boundExpression->GetType(); auto expresionType = boundExpression->GetType();

View File

@ -95,6 +95,7 @@ namespace Porygon::Evaluation {
auto expression = statement->GetExpression(); auto expression = statement->GetExpression();
if (expression == nullptr) { if (expression == nullptr) {
this->_hasReturned = true; this->_hasReturned = true;
this -> _returnValue = nullptr;
return; return;
} }
auto value = this->EvaluateExpression(expression); auto value = this->EvaluateExpression(expression);

View File

@ -192,8 +192,12 @@ namespace Porygon::Parser {
ParsedStatement *Parser::ParseReturnStatement(const IToken *current) { ParsedStatement *Parser::ParseReturnStatement(const IToken *current) {
//TODO: if next token is on a different line, don't parse it as return expression. //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 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); return new ParsedReturnStatement(expression, start, expression->GetEndPosition() - start);
} }

View File

@ -136,5 +136,19 @@ test()
delete script; 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 #endif