Don't parse expression on different line from return keyword
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
b76548da16
commit
6f7d319148
|
@ -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();
|
||||||
|
|
|
@ -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);
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue