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

View File

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

View File

@ -198,7 +198,24 @@ end
var result = script.EvaluateFunction<long>("add", new object[] {400, 128});
Assert.Empty(script.Diagnostics.Messages);
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);
}
}
}