Handle empty return statements

This commit is contained in:
Deukhoofd 2018-11-28 14:13:39 +01:00
parent cc029d0689
commit cefe48cb4b
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
4 changed files with 11 additions and 4 deletions

View File

@ -509,6 +509,9 @@ namespace Upsilon.Binder
private BoundStatement BindReturnStatement(ReturnStatementSyntax e)
{
if (e.Expression == null)
return new BoundReturnStatement(null, e.Span);
var expression = BindExpression(e.Expression);
if (expression.Type != Type.Unknown && expression.Type != Type.Unknown &&
Scope.ReturnType != Type.Unknown && Scope.ReturnType != Type.Nil)

View File

@ -423,7 +423,7 @@ namespace Upsilon.Evaluator
private void EvaluateReturnStatement(BoundReturnStatement b)
{
_returnValue = EvaluateExpression(b.Expression);
_returnValue = b.Expression == null ? null : EvaluateExpression(b.Expression);
_lastValue = _returnValue;
HasReturned = true;
}

View File

@ -283,7 +283,9 @@ namespace Upsilon.Parser
private StatementSyntax ParseReturnStatement()
{
var returnToken = MatchToken(SyntaxKind.ReturnKeyword);
var expression = ParseExpression();
ExpressionSyntax expression = null;
if (Current.Kind != SyntaxKind.EndKeyword)
expression = ParseExpression();
return new ReturnStatementSyntax(returnToken, expression);
}

View File

@ -9,7 +9,9 @@ namespace Upsilon.Parser
{
ReturnToken = returnToken;
Expression = expression;
Span = new TextSpan(returnToken.Span.Start, expression.Span.End - returnToken.Span.Start);
var end = returnToken.Span.End;
if (expression != null) end = expression.Span.End;
Span = new TextSpan(returnToken.Span.Start, end - returnToken.Span.Start);
}
public SyntaxToken ReturnToken { get; }