Fixes issue where functions inside binary operators wouldnt work

This commit is contained in:
Deukhoofd 2018-11-24 12:55:51 +01:00
parent 62a18e22d4
commit 9be1cfa1fc
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
3 changed files with 29 additions and 5 deletions

View File

@ -268,17 +268,24 @@ namespace Upsilon.Parser
{ {
expression = ParseBinaryExpression(); expression = ParseBinaryExpression();
} }
HandleComplexExpression(expression);
return expression;
}
private ExpressionSyntax HandleComplexExpression(ExpressionSyntax baseExpression)
{
while (Current.Kind == SyntaxKind.OpenBracket || Current.Kind == SyntaxKind.OpenParenthesis || while (Current.Kind == SyntaxKind.OpenBracket || Current.Kind == SyntaxKind.OpenParenthesis ||
Current.Kind == SyntaxKind.FullStop) Current.Kind == SyntaxKind.FullStop)
{ {
if (Current.Kind == SyntaxKind.OpenBracket) if (Current.Kind == SyntaxKind.OpenBracket)
expression = ParseIndexExpression(expression); baseExpression = ParseIndexExpression(baseExpression);
else if (Current.Kind == SyntaxKind.OpenParenthesis) else if (Current.Kind == SyntaxKind.OpenParenthesis)
expression = ParseFunctionCallExpression(expression); baseExpression = ParseFunctionCallExpression(baseExpression);
else if (Current.Kind == SyntaxKind.FullStop) else if (Current.Kind == SyntaxKind.FullStop)
expression = ParseFullStopIndexExpression(expression); baseExpression = ParseFullStopIndexExpression(baseExpression);
} }
return expression; return baseExpression;
} }
private StatementSyntax ParseAssignmentExpression() private StatementSyntax ParseAssignmentExpression()
@ -346,6 +353,7 @@ namespace Upsilon.Parser
var op = NextToken(); var op = NextToken();
var right = ParseBinaryExpression(precedence); var right = ParseBinaryExpression(precedence);
right = HandleComplexExpression(right);
left = new BinaryExpressionSyntax(left, op, right); left = new BinaryExpressionSyntax(left, op, right);
} }
return left; return left;

View File

@ -115,6 +115,5 @@ return value
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(6, result); Assert.Equal(6, result);
} }
} }
} }

View File

@ -198,7 +198,24 @@ end
var result = script.EvaluateFunction<long>("add", new object[] {400, 128}); var result = script.EvaluateFunction<long>("add", new object[] {400, 128});
Assert.Empty(script.Diagnostics.Messages); Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(528, result); Assert.Equal(528, result);
}
[Fact]
public void HandleFunctionsInsideBinaryExpressions()
{
const string input = @"
arr = {100, 56, 28}
value = 0
for key, val in arr do
value = value + tonumber(key)
end
return value
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(6, result);
} }
} }
} }