Skip to content

Commit

Permalink
#706 - CallExpression uses incorrect scope for parameters evaluation.
Browse files Browse the repository at this point in the history
  • Loading branch information
sys27 committed Sep 4, 2023
1 parent b31cf5d commit ba25ecd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/articles/change-log.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* [#667](https://github.com/sys27/xFunc/issues/667) - Migrate to NUnit ([#700](https://github.com/sys27/xFunc/pull/700)).
* [#701](https://github.com/sys27/xFunc/issues/701) - Align units `ToString` implementation with parser.
* [#704](https://github.com/sys27/xFunc/issues/704) - Lambda can't resolve a parameter ([#705](https://github.com/sys27/xFunc/pull/705)).
* [#706](https://github.com/sys27/xFunc/issues/706) - `CallExpression` uses incorrect scope for parameters evaluation ([#707](https://github.com/sys27/xFunc/issues/707)).

## xFunc v4.3.0

Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/CallExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public object Execute(ExpressionParameters? parameters)
var nestedScope = ExpressionParameters.CreateScoped(function.CapturedScope ?? parameters);
var zip = function.Parameters.Zip(Parameters, (parameter, expression) => (parameter, expression));
foreach (var (parameter, expression) in zip)
nestedScope[parameter] = new ParameterValue(expression.Execute(nestedScope));
nestedScope[parameter] = new ParameterValue(expression.Execute(parameters));

var result = function.Call(nestedScope);
if (result is Lambda lambdaResult)
Expand Down
22 changes: 22 additions & 0 deletions xFunc.Tests/ProcessorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -387,4 +387,26 @@ public void LambdaExecuteAnotherLambdaTest()

Assert.That(result.Result, Is.EqualTo(193536720.0));
}

[Test]
public void CallExpressionUsesCorrectContext1()
{
var processor = new Processor();
processor.Solve("f := (x) => (y) => x + y");
processor.Solve("add1 := f(1)");

Assert.Throws<KeyNotFoundException>(() => processor.Solve<NumberResult>("add1(x + 2)"));
}

[Test]
public void CallExpressionUsesCorrectContext2()
{
var processor = new Processor();
processor.Solve("f := (x) => (y) => x + y");
processor.Solve("add1 := f(1)");
processor.Solve("x := 3");
var result = processor.Solve<NumberResult>("add1(x + 2)");

Assert.That(result.Result, Is.EqualTo(6));
}
}

0 comments on commit ba25ecd

Please sign in to comment.