From 392a46b2316fa331fada72a4677900d148d39bf2 Mon Sep 17 00:00:00 2001 From: Dmytro Kyshchenko Date: Tue, 1 Aug 2023 18:56:18 +0300 Subject: [PATCH] #653 - Add support of function currying. --- .../Analyzers/DifferentiatorContext.cs | 6 +- xFunc.Maths/Expressions/Abs.cs | 2 +- xFunc.Maths/Expressions/Add.cs | 2 +- xFunc.Maths/Expressions/Assign.cs | 2 +- xFunc.Maths/Expressions/BinaryExpression.cs | 2 +- xFunc.Maths/Expressions/CallExpression.cs | 4 +- xFunc.Maths/Expressions/Ceil.cs | 2 +- .../ComplexNumbers/ComplexNumber.cs | 2 +- .../Expressions/ComplexNumbers/Conjugate.cs | 2 +- xFunc.Maths/Expressions/ComplexNumbers/Im.cs | 2 +- .../Expressions/ComplexNumbers/Phase.cs | 2 +- xFunc.Maths/Expressions/ComplexNumbers/Re.cs | 2 +- .../Expressions/ComplexNumbers/Reciprocal.cs | 2 +- .../Expressions/ComplexNumbers/ToComplex.cs | 2 +- xFunc.Maths/Expressions/Del.cs | 2 +- xFunc.Maths/Expressions/DelegateExpression.cs | 6 +- xFunc.Maths/Expressions/Derivative.cs | 2 +- .../DifferentParametersExpression.cs | 2 +- xFunc.Maths/Expressions/Div.cs | 2 +- xFunc.Maths/Expressions/Exp.cs | 2 +- xFunc.Maths/Expressions/Fact.cs | 2 +- xFunc.Maths/Expressions/Floor.cs | 2 +- xFunc.Maths/Expressions/Frac.cs | 2 +- xFunc.Maths/Expressions/GCD.cs | 2 +- .../Hyperbolic/HyperbolicExpression.cs | 2 +- .../Hyperbolic/InverseHyperbolicExpression.cs | 2 +- xFunc.Maths/Expressions/IExpression.cs | 2 +- xFunc.Maths/Expressions/LCM.cs | 2 +- xFunc.Maths/Expressions/Lambda.cs | 17 ++- xFunc.Maths/Expressions/LambdaExpression.cs | 2 +- xFunc.Maths/Expressions/Lb.cs | 2 +- xFunc.Maths/Expressions/Lg.cs | 2 +- xFunc.Maths/Expressions/Ln.cs | 2 +- xFunc.Maths/Expressions/Log.cs | 2 +- .../Expressions/LogicalAndBitwise/And.cs | 2 +- .../Expressions/LogicalAndBitwise/Bool.cs | 2 +- .../Expressions/LogicalAndBitwise/Equality.cs | 2 +- .../LogicalAndBitwise/Implication.cs | 2 +- .../Expressions/LogicalAndBitwise/NAnd.cs | 2 +- .../Expressions/LogicalAndBitwise/NOr.cs | 2 +- .../Expressions/LogicalAndBitwise/Not.cs | 2 +- .../Expressions/LogicalAndBitwise/Or.cs | 2 +- .../Expressions/LogicalAndBitwise/XOr.cs | 2 +- .../Expressions/Matrices/CrossProduct.cs | 2 +- .../Expressions/Matrices/Determinant.cs | 2 +- .../Expressions/Matrices/DotProduct.cs | 2 +- xFunc.Maths/Expressions/Matrices/Inverse.cs | 2 +- xFunc.Maths/Expressions/Matrices/Matrix.cs | 2 +- xFunc.Maths/Expressions/Matrices/Transpose.cs | 2 +- xFunc.Maths/Expressions/Matrices/Vector.cs | 2 +- xFunc.Maths/Expressions/Mod.cs | 2 +- xFunc.Maths/Expressions/Mul.cs | 2 +- xFunc.Maths/Expressions/Number.cs | 2 +- ...onParameters.ScopedExpressionParameters.cs | 14 +- ...nParameters.WrapperExpressionParameters.cs | 120 ------------------ .../Parameters/ExpressionParameters.cs | 29 ++--- .../Parameters/IExpressionParameters.cs | 77 ----------- xFunc.Maths/Expressions/Pow.cs | 2 +- .../Expressions/Programming/ConditionalAnd.cs | 2 +- .../Expressions/Programming/ConditionalOr.cs | 2 +- xFunc.Maths/Expressions/Programming/Equal.cs | 2 +- xFunc.Maths/Expressions/Programming/For.cs | 2 +- .../Expressions/Programming/GreaterOrEqual.cs | 2 +- .../Expressions/Programming/GreaterThan.cs | 2 +- xFunc.Maths/Expressions/Programming/If.cs | 2 +- .../Expressions/Programming/LeftShift.cs | 2 +- .../Expressions/Programming/LessOrEqual.cs | 2 +- .../Expressions/Programming/LessThan.cs | 2 +- .../Expressions/Programming/NotEqual.cs | 2 +- .../Expressions/Programming/RightShift.cs | 2 +- .../Programming/VariableBinaryExpression.cs | 2 +- .../Programming/VariableUnaryExpression.cs | 2 +- xFunc.Maths/Expressions/Programming/While.cs | 2 +- xFunc.Maths/Expressions/Root.cs | 2 +- xFunc.Maths/Expressions/Round.cs | 2 +- xFunc.Maths/Expressions/Sign.cs | 2 +- xFunc.Maths/Expressions/Simplify.cs | 2 +- xFunc.Maths/Expressions/Sqrt.cs | 2 +- .../Statistical/StatisticalExpression.cs | 2 +- xFunc.Maths/Expressions/StringExpression.cs | 2 +- xFunc.Maths/Expressions/Sub.cs | 2 +- xFunc.Maths/Expressions/ToBin.cs | 2 +- xFunc.Maths/Expressions/ToHex.cs | 2 +- xFunc.Maths/Expressions/ToOct.cs | 2 +- .../InverseTrigonometricExpression.cs | 2 +- .../Trigonometric/TrigonometricExpression.cs | 2 +- xFunc.Maths/Expressions/Trunc.cs | 2 +- xFunc.Maths/Expressions/UnaryExpression.cs | 2 +- xFunc.Maths/Expressions/UnaryMinus.cs | 2 +- xFunc.Maths/Expressions/Unassign.cs | 2 +- .../Expressions/Units/AngleUnits/ToDegree.cs | 2 +- .../Expressions/Units/AngleUnits/ToGradian.cs | 2 +- .../Expressions/Units/AngleUnits/ToRadian.cs | 2 +- xFunc.Maths/Expressions/Units/Convert.cs | 2 +- xFunc.Maths/Expressions/Units/ToNumber.cs | 2 +- xFunc.Maths/Expressions/Units/Unit.cs | 2 +- xFunc.Maths/Expressions/Variable.cs | 2 +- .../Expressions/DelegateExpressionTest.cs | 2 +- .../Parameters/ExpressionParameterTest.cs | 40 ++++++ xFunc.Tests/ProcessorTest.cs | 12 ++ 100 files changed, 180 insertions(+), 325 deletions(-) delete mode 100644 xFunc.Maths/Expressions/Parameters/ExpressionParameters.WrapperExpressionParameters.cs delete mode 100644 xFunc.Maths/Expressions/Parameters/IExpressionParameters.cs diff --git a/xFunc.Maths/Analyzers/DifferentiatorContext.cs b/xFunc.Maths/Analyzers/DifferentiatorContext.cs index 202d0bddc..2c4543322 100644 --- a/xFunc.Maths/Analyzers/DifferentiatorContext.cs +++ b/xFunc.Maths/Analyzers/DifferentiatorContext.cs @@ -14,7 +14,7 @@ public class DifferentiatorContext /// Initializes a new instance of the class. /// /// The parameters. - public DifferentiatorContext(IExpressionParameters? parameters) + public DifferentiatorContext(ExpressionParameters? parameters) : this(parameters, Variable.X) { } @@ -24,7 +24,7 @@ public DifferentiatorContext(IExpressionParameters? parameters) /// /// The parameters. /// The variable. - public DifferentiatorContext(IExpressionParameters? parameters, Variable variable) + public DifferentiatorContext(ExpressionParameters? parameters, Variable variable) { Parameters = parameters; Variable = variable; @@ -43,7 +43,7 @@ public static DifferentiatorContext Default() /// /// The parameters. /// - public IExpressionParameters? Parameters { get; } + public ExpressionParameters? Parameters { get; } /// /// Gets or sets the variable. diff --git a/xFunc.Maths/Expressions/Abs.cs b/xFunc.Maths/Expressions/Abs.cs index a54858742..d7db71b82 100644 --- a/xFunc.Maths/Expressions/Abs.cs +++ b/xFunc.Maths/Expressions/Abs.cs @@ -37,7 +37,7 @@ internal Abs(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Argument.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Add.cs b/xFunc.Maths/Expressions/Add.cs index 57dd7d70a..d57f2f6eb 100644 --- a/xFunc.Maths/Expressions/Add.cs +++ b/xFunc.Maths/Expressions/Add.cs @@ -40,7 +40,7 @@ internal Add(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var leftResult = Left.Execute(parameters); var rightResult = Right.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Assign.cs b/xFunc.Maths/Expressions/Assign.cs index 8242403dd..7a3e1ebec 100644 --- a/xFunc.Maths/Expressions/Assign.cs +++ b/xFunc.Maths/Expressions/Assign.cs @@ -42,7 +42,7 @@ public override bool Equals(object? obj) public object Execute() => throw new NotSupportedException(); /// - public object Execute(IExpressionParameters? parameters) + public object Execute(ExpressionParameters? parameters) { if (parameters is null) throw new ArgumentNullException(nameof(parameters)); diff --git a/xFunc.Maths/Expressions/BinaryExpression.cs b/xFunc.Maths/Expressions/BinaryExpression.cs index d3e19c6c8..ee32f6b84 100644 --- a/xFunc.Maths/Expressions/BinaryExpression.cs +++ b/xFunc.Maths/Expressions/BinaryExpression.cs @@ -75,7 +75,7 @@ public override bool Equals(object? obj) public object Execute() => Execute(null); /// - public abstract object Execute(IExpressionParameters? parameters); + public abstract object Execute(ExpressionParameters? parameters); /// public TResult Analyze(IAnalyzer analyzer) diff --git a/xFunc.Maths/Expressions/CallExpression.cs b/xFunc.Maths/Expressions/CallExpression.cs index e12f75a6d..8c078f918 100644 --- a/xFunc.Maths/Expressions/CallExpression.cs +++ b/xFunc.Maths/Expressions/CallExpression.cs @@ -76,7 +76,7 @@ public object Execute() => throw new NotSupportedException(); /// - public object Execute(IExpressionParameters? parameters) + public object Execute(ExpressionParameters? parameters) { if (parameters is null) throw new ArgumentNullException(nameof(parameters)); @@ -84,7 +84,7 @@ public object Execute(IExpressionParameters? parameters) if (Function.Execute(parameters) is not Lambda function) throw new ResultIsNotSupportedException(this, Function); - var nestedScope = ExpressionParameters.CreateScoped(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)); diff --git a/xFunc.Maths/Expressions/Ceil.cs b/xFunc.Maths/Expressions/Ceil.cs index 9bf0c511b..5b49c107c 100644 --- a/xFunc.Maths/Expressions/Ceil.cs +++ b/xFunc.Maths/Expressions/Ceil.cs @@ -36,7 +36,7 @@ internal Ceil(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Argument.Execute(parameters); diff --git a/xFunc.Maths/Expressions/ComplexNumbers/ComplexNumber.cs b/xFunc.Maths/Expressions/ComplexNumbers/ComplexNumber.cs index 64d234174..1538eaa0a 100644 --- a/xFunc.Maths/Expressions/ComplexNumbers/ComplexNumber.cs +++ b/xFunc.Maths/Expressions/ComplexNumbers/ComplexNumber.cs @@ -72,7 +72,7 @@ public override bool Equals(object? obj) public object Execute() => Value; /// - public object Execute(IExpressionParameters? parameters) => Value; + public object Execute(ExpressionParameters? parameters) => Value; /// public TResult Analyze(IAnalyzer analyzer) diff --git a/xFunc.Maths/Expressions/ComplexNumbers/Conjugate.cs b/xFunc.Maths/Expressions/ComplexNumbers/Conjugate.cs index a35ac5a6d..490fb34f3 100644 --- a/xFunc.Maths/Expressions/ComplexNumbers/Conjugate.cs +++ b/xFunc.Maths/Expressions/ComplexNumbers/Conjugate.cs @@ -38,7 +38,7 @@ internal Conjugate(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Argument.Execute(parameters); diff --git a/xFunc.Maths/Expressions/ComplexNumbers/Im.cs b/xFunc.Maths/Expressions/ComplexNumbers/Im.cs index 090156c02..13f3181aa 100644 --- a/xFunc.Maths/Expressions/ComplexNumbers/Im.cs +++ b/xFunc.Maths/Expressions/ComplexNumbers/Im.cs @@ -38,7 +38,7 @@ internal Im(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Argument.Execute(parameters); diff --git a/xFunc.Maths/Expressions/ComplexNumbers/Phase.cs b/xFunc.Maths/Expressions/ComplexNumbers/Phase.cs index 6d7fbbf1c..7042fcaaf 100644 --- a/xFunc.Maths/Expressions/ComplexNumbers/Phase.cs +++ b/xFunc.Maths/Expressions/ComplexNumbers/Phase.cs @@ -38,7 +38,7 @@ internal Phase(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Argument.Execute(parameters); diff --git a/xFunc.Maths/Expressions/ComplexNumbers/Re.cs b/xFunc.Maths/Expressions/ComplexNumbers/Re.cs index 0ca1a9cf6..a5f87d3b3 100644 --- a/xFunc.Maths/Expressions/ComplexNumbers/Re.cs +++ b/xFunc.Maths/Expressions/ComplexNumbers/Re.cs @@ -38,7 +38,7 @@ internal Re(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Argument.Execute(parameters); diff --git a/xFunc.Maths/Expressions/ComplexNumbers/Reciprocal.cs b/xFunc.Maths/Expressions/ComplexNumbers/Reciprocal.cs index 80b4ee4d1..a017b3649 100644 --- a/xFunc.Maths/Expressions/ComplexNumbers/Reciprocal.cs +++ b/xFunc.Maths/Expressions/ComplexNumbers/Reciprocal.cs @@ -38,7 +38,7 @@ internal Reciprocal(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Argument.Execute(parameters); diff --git a/xFunc.Maths/Expressions/ComplexNumbers/ToComplex.cs b/xFunc.Maths/Expressions/ComplexNumbers/ToComplex.cs index ed7eb9de2..d2e596704 100644 --- a/xFunc.Maths/Expressions/ComplexNumbers/ToComplex.cs +++ b/xFunc.Maths/Expressions/ComplexNumbers/ToComplex.cs @@ -38,7 +38,7 @@ internal ToComplex(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Argument.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Del.cs b/xFunc.Maths/Expressions/Del.cs index 00cfaf82b..2009bec12 100644 --- a/xFunc.Maths/Expressions/Del.cs +++ b/xFunc.Maths/Expressions/Del.cs @@ -53,7 +53,7 @@ internal Del( } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Argument.Execute(parameters); if (result is not Lambda lambda) diff --git a/xFunc.Maths/Expressions/DelegateExpression.cs b/xFunc.Maths/Expressions/DelegateExpression.cs index 815b750cd..3ce82c48a 100644 --- a/xFunc.Maths/Expressions/DelegateExpression.cs +++ b/xFunc.Maths/Expressions/DelegateExpression.cs @@ -8,13 +8,13 @@ namespace xFunc.Maths.Expressions; /// public class DelegateExpression : IExpression { - private readonly Func func; + private readonly Func func; /// /// Initializes a new instance of the class. /// /// The delegate of function. - public DelegateExpression(Func func) + public DelegateExpression(Func func) => this.func = func; /// @@ -39,7 +39,7 @@ public override bool Equals(object? obj) public object Execute() => func(null); /// - public object Execute(IExpressionParameters? parameters) + public object Execute(ExpressionParameters? parameters) => func(parameters); /// diff --git a/xFunc.Maths/Expressions/Derivative.cs b/xFunc.Maths/Expressions/Derivative.cs index 3f197133d..ae277be9c 100644 --- a/xFunc.Maths/Expressions/Derivative.cs +++ b/xFunc.Maths/Expressions/Derivative.cs @@ -82,7 +82,7 @@ internal Derivative( } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Expression.Execute(parameters); if (result is not Lambda lambda) diff --git a/xFunc.Maths/Expressions/DifferentParametersExpression.cs b/xFunc.Maths/Expressions/DifferentParametersExpression.cs index 811904304..b73f751c0 100644 --- a/xFunc.Maths/Expressions/DifferentParametersExpression.cs +++ b/xFunc.Maths/Expressions/DifferentParametersExpression.cs @@ -77,7 +77,7 @@ public override bool Equals(object? obj) public virtual object Execute() => Execute(null); /// - public abstract object Execute(IExpressionParameters? parameters); + public abstract object Execute(ExpressionParameters? parameters); /// public TResult Analyze(IAnalyzer analyzer) diff --git a/xFunc.Maths/Expressions/Div.cs b/xFunc.Maths/Expressions/Div.cs index 010bbbd58..dd5b2340a 100644 --- a/xFunc.Maths/Expressions/Div.cs +++ b/xFunc.Maths/Expressions/Div.cs @@ -39,7 +39,7 @@ internal Div(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var leftResult = Left.Execute(parameters); var rightResult = Right.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Exp.cs b/xFunc.Maths/Expressions/Exp.cs index e23de4901..3d7b8ba30 100644 --- a/xFunc.Maths/Expressions/Exp.cs +++ b/xFunc.Maths/Expressions/Exp.cs @@ -36,7 +36,7 @@ internal Exp(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Argument.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Fact.cs b/xFunc.Maths/Expressions/Fact.cs index 2b6fe383d..8696176b7 100644 --- a/xFunc.Maths/Expressions/Fact.cs +++ b/xFunc.Maths/Expressions/Fact.cs @@ -35,7 +35,7 @@ internal Fact(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Argument.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Floor.cs b/xFunc.Maths/Expressions/Floor.cs index 819b565d2..4285f4586 100644 --- a/xFunc.Maths/Expressions/Floor.cs +++ b/xFunc.Maths/Expressions/Floor.cs @@ -36,7 +36,7 @@ internal Floor(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Argument.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Frac.cs b/xFunc.Maths/Expressions/Frac.cs index 47f1999f0..a89507811 100644 --- a/xFunc.Maths/Expressions/Frac.cs +++ b/xFunc.Maths/Expressions/Frac.cs @@ -36,7 +36,7 @@ internal Frac(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Argument.Execute(parameters); diff --git a/xFunc.Maths/Expressions/GCD.cs b/xFunc.Maths/Expressions/GCD.cs index c91971af1..17aa52ddb 100644 --- a/xFunc.Maths/Expressions/GCD.cs +++ b/xFunc.Maths/Expressions/GCD.cs @@ -42,7 +42,7 @@ public GCD(IExpression first, IExpression second) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var gcd = new NumberValue(0.0); foreach (var argument in Arguments) diff --git a/xFunc.Maths/Expressions/Hyperbolic/HyperbolicExpression.cs b/xFunc.Maths/Expressions/Hyperbolic/HyperbolicExpression.cs index 2632f1bed..4ce435903 100644 --- a/xFunc.Maths/Expressions/Hyperbolic/HyperbolicExpression.cs +++ b/xFunc.Maths/Expressions/Hyperbolic/HyperbolicExpression.cs @@ -52,7 +52,7 @@ internal HyperbolicExpression(ImmutableArray arguments) protected abstract Complex ExecuteComplex(Complex complex); /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Argument.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Hyperbolic/InverseHyperbolicExpression.cs b/xFunc.Maths/Expressions/Hyperbolic/InverseHyperbolicExpression.cs index 9147672cd..1340220d5 100644 --- a/xFunc.Maths/Expressions/Hyperbolic/InverseHyperbolicExpression.cs +++ b/xFunc.Maths/Expressions/Hyperbolic/InverseHyperbolicExpression.cs @@ -52,7 +52,7 @@ internal InverseHyperbolicExpression(ImmutableArray arguments) protected abstract Complex ExecuteComplex(Complex complex); /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Argument.Execute(parameters); diff --git a/xFunc.Maths/Expressions/IExpression.cs b/xFunc.Maths/Expressions/IExpression.cs index 67d6a222d..c35ff00f3 100644 --- a/xFunc.Maths/Expressions/IExpression.cs +++ b/xFunc.Maths/Expressions/IExpression.cs @@ -20,7 +20,7 @@ public interface IExpression /// An object that contains all parameters and functions for expressions. /// A result of the execution. /// - object Execute(IExpressionParameters? parameters); + object Execute(ExpressionParameters? parameters); /// /// Returns a that represents this instance. diff --git a/xFunc.Maths/Expressions/LCM.cs b/xFunc.Maths/Expressions/LCM.cs index 796dd9ab1..f062f32d1 100644 --- a/xFunc.Maths/Expressions/LCM.cs +++ b/xFunc.Maths/Expressions/LCM.cs @@ -42,7 +42,7 @@ public LCM(IExpression first, IExpression second) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var lcm = new NumberValue(1.0); foreach (var argument in Arguments) diff --git a/xFunc.Maths/Expressions/Lambda.cs b/xFunc.Maths/Expressions/Lambda.cs index d38d4aae3..065679357 100644 --- a/xFunc.Maths/Expressions/Lambda.cs +++ b/xFunc.Maths/Expressions/Lambda.cs @@ -11,8 +11,6 @@ namespace xFunc.Maths.Expressions; /// public readonly struct Lambda : IEquatable { - private readonly IExpressionParameters? capturedParameters; - /// /// Initializes a new instance of the struct. /// @@ -42,11 +40,11 @@ public Lambda(ImmutableArray parameters, IExpression body) { } - private Lambda(ImmutableArray parameters, IExpression body, IExpressionParameters? capturedParameters) + private Lambda(ImmutableArray parameters, IExpression body, ExpressionParameters? capturedScope) { Parameters = parameters; Body = body; - this.capturedParameters = capturedParameters; + CapturedScope = capturedScope; } /// @@ -90,15 +88,15 @@ public override string ToString() /// /// An object that contains all parameters and functions for expressions. /// A result of the execution. - public object Call(IExpressionParameters parameters) - => Body.Execute(ExpressionParameters.CreateCombined(parameters, capturedParameters)); + public object Call(ExpressionParameters parameters) + => Body.Execute(parameters); /// /// Returns a new lambda instance with captured parameters. /// /// An object that contains all parameters and functions for expressions. /// The lambda with captured parameters. - public Lambda Capture(IExpressionParameters? parameters) + public Lambda Capture(ExpressionParameters? parameters) => new Lambda(Parameters, Body, parameters); /// @@ -117,4 +115,9 @@ public LambdaExpression AsExpression() /// Gets an expression of the function body. /// public IExpression Body { get; } + + /// + /// Gets the captured scope. + /// + internal ExpressionParameters? CapturedScope { get; } } \ No newline at end of file diff --git a/xFunc.Maths/Expressions/LambdaExpression.cs b/xFunc.Maths/Expressions/LambdaExpression.cs index d471a23ed..11a36debb 100644 --- a/xFunc.Maths/Expressions/LambdaExpression.cs +++ b/xFunc.Maths/Expressions/LambdaExpression.cs @@ -47,7 +47,7 @@ public object Execute() => Execute(null); /// - public object Execute(IExpressionParameters? parameters) + public object Execute(ExpressionParameters? parameters) => Lambda.Capture(parameters); /// diff --git a/xFunc.Maths/Expressions/Lb.cs b/xFunc.Maths/Expressions/Lb.cs index 898b5441a..e68cb1c6b 100644 --- a/xFunc.Maths/Expressions/Lb.cs +++ b/xFunc.Maths/Expressions/Lb.cs @@ -36,7 +36,7 @@ internal Lb(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Argument.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Lg.cs b/xFunc.Maths/Expressions/Lg.cs index c7f6cb652..d8f0cb36c 100644 --- a/xFunc.Maths/Expressions/Lg.cs +++ b/xFunc.Maths/Expressions/Lg.cs @@ -37,7 +37,7 @@ internal Lg(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Argument.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Ln.cs b/xFunc.Maths/Expressions/Ln.cs index 804dbae46..556854094 100644 --- a/xFunc.Maths/Expressions/Ln.cs +++ b/xFunc.Maths/Expressions/Ln.cs @@ -37,7 +37,7 @@ internal Ln(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Argument.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Log.cs b/xFunc.Maths/Expressions/Log.cs index 66a745349..6b2a2b681 100644 --- a/xFunc.Maths/Expressions/Log.cs +++ b/xFunc.Maths/Expressions/Log.cs @@ -40,7 +40,7 @@ public Log(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var leftResult = Left.Execute(parameters); var rightResult = Right.Execute(parameters); diff --git a/xFunc.Maths/Expressions/LogicalAndBitwise/And.cs b/xFunc.Maths/Expressions/LogicalAndBitwise/And.cs index 060f37984..89247face 100644 --- a/xFunc.Maths/Expressions/LogicalAndBitwise/And.cs +++ b/xFunc.Maths/Expressions/LogicalAndBitwise/And.cs @@ -21,7 +21,7 @@ public And(IExpression left, IExpression right) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var leftResult = Left.Execute(parameters); var rightResult = Right.Execute(parameters); diff --git a/xFunc.Maths/Expressions/LogicalAndBitwise/Bool.cs b/xFunc.Maths/Expressions/LogicalAndBitwise/Bool.cs index 146de696e..935455955 100644 --- a/xFunc.Maths/Expressions/LogicalAndBitwise/Bool.cs +++ b/xFunc.Maths/Expressions/LogicalAndBitwise/Bool.cs @@ -18,7 +18,7 @@ public class Bool : IExpression, IEquatable public object Execute() => Value; /// - public object Execute(IExpressionParameters? parameters) => Value; + public object Execute(ExpressionParameters? parameters) => Value; /// public TResult Analyze(IAnalyzer analyzer) diff --git a/xFunc.Maths/Expressions/LogicalAndBitwise/Equality.cs b/xFunc.Maths/Expressions/LogicalAndBitwise/Equality.cs index 12035c79f..a8a86e454 100644 --- a/xFunc.Maths/Expressions/LogicalAndBitwise/Equality.cs +++ b/xFunc.Maths/Expressions/LogicalAndBitwise/Equality.cs @@ -21,7 +21,7 @@ public Equality(IExpression left, IExpression right) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var left = Left.Execute(parameters); var right = Right.Execute(parameters); diff --git a/xFunc.Maths/Expressions/LogicalAndBitwise/Implication.cs b/xFunc.Maths/Expressions/LogicalAndBitwise/Implication.cs index 7372fb2d5..9bd6439cf 100644 --- a/xFunc.Maths/Expressions/LogicalAndBitwise/Implication.cs +++ b/xFunc.Maths/Expressions/LogicalAndBitwise/Implication.cs @@ -21,7 +21,7 @@ public Implication(IExpression left, IExpression right) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var left = Left.Execute(parameters); var right = Right.Execute(parameters); diff --git a/xFunc.Maths/Expressions/LogicalAndBitwise/NAnd.cs b/xFunc.Maths/Expressions/LogicalAndBitwise/NAnd.cs index b9a36808f..500f5cf7a 100644 --- a/xFunc.Maths/Expressions/LogicalAndBitwise/NAnd.cs +++ b/xFunc.Maths/Expressions/LogicalAndBitwise/NAnd.cs @@ -22,7 +22,7 @@ public NAnd(IExpression left, IExpression right) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var left = Left.Execute(parameters); var right = Right.Execute(parameters); diff --git a/xFunc.Maths/Expressions/LogicalAndBitwise/NOr.cs b/xFunc.Maths/Expressions/LogicalAndBitwise/NOr.cs index 9fc1733cc..8d45784ea 100644 --- a/xFunc.Maths/Expressions/LogicalAndBitwise/NOr.cs +++ b/xFunc.Maths/Expressions/LogicalAndBitwise/NOr.cs @@ -21,7 +21,7 @@ public NOr(IExpression left, IExpression right) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var left = Left.Execute(parameters); var right = Right.Execute(parameters); diff --git a/xFunc.Maths/Expressions/LogicalAndBitwise/Not.cs b/xFunc.Maths/Expressions/LogicalAndBitwise/Not.cs index 75391e77d..6ede3ab95 100644 --- a/xFunc.Maths/Expressions/LogicalAndBitwise/Not.cs +++ b/xFunc.Maths/Expressions/LogicalAndBitwise/Not.cs @@ -21,7 +21,7 @@ public Not(IExpression expression) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var arg = Argument.Execute(parameters); diff --git a/xFunc.Maths/Expressions/LogicalAndBitwise/Or.cs b/xFunc.Maths/Expressions/LogicalAndBitwise/Or.cs index 4ca10ab75..1a3067d6d 100644 --- a/xFunc.Maths/Expressions/LogicalAndBitwise/Or.cs +++ b/xFunc.Maths/Expressions/LogicalAndBitwise/Or.cs @@ -21,7 +21,7 @@ public Or(IExpression left, IExpression right) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var leftResult = Left.Execute(parameters); var rightResult = Right.Execute(parameters); diff --git a/xFunc.Maths/Expressions/LogicalAndBitwise/XOr.cs b/xFunc.Maths/Expressions/LogicalAndBitwise/XOr.cs index 15462a4a6..b08f16846 100644 --- a/xFunc.Maths/Expressions/LogicalAndBitwise/XOr.cs +++ b/xFunc.Maths/Expressions/LogicalAndBitwise/XOr.cs @@ -22,7 +22,7 @@ public XOr(IExpression left, IExpression right) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var leftResult = Left.Execute(parameters); var rightResult = Right.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Matrices/CrossProduct.cs b/xFunc.Maths/Expressions/Matrices/CrossProduct.cs index 569a27e3d..3df7d515c 100644 --- a/xFunc.Maths/Expressions/Matrices/CrossProduct.cs +++ b/xFunc.Maths/Expressions/Matrices/CrossProduct.cs @@ -39,7 +39,7 @@ public CrossProduct(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var left = Left.Execute(parameters); var right = Right.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Matrices/Determinant.cs b/xFunc.Maths/Expressions/Matrices/Determinant.cs index 717544805..f602cea91 100644 --- a/xFunc.Maths/Expressions/Matrices/Determinant.cs +++ b/xFunc.Maths/Expressions/Matrices/Determinant.cs @@ -36,7 +36,7 @@ internal Determinant(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Argument.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Matrices/DotProduct.cs b/xFunc.Maths/Expressions/Matrices/DotProduct.cs index 75bbf91c2..00c9feec6 100644 --- a/xFunc.Maths/Expressions/Matrices/DotProduct.cs +++ b/xFunc.Maths/Expressions/Matrices/DotProduct.cs @@ -39,7 +39,7 @@ public DotProduct(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var left = Left.Execute(parameters); var right = Right.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Matrices/Inverse.cs b/xFunc.Maths/Expressions/Matrices/Inverse.cs index 8b66a30e0..b96e9b6ab 100644 --- a/xFunc.Maths/Expressions/Matrices/Inverse.cs +++ b/xFunc.Maths/Expressions/Matrices/Inverse.cs @@ -36,7 +36,7 @@ internal Inverse(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Argument.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Matrices/Matrix.cs b/xFunc.Maths/Expressions/Matrices/Matrix.cs index c45377669..cb9a4abff 100644 --- a/xFunc.Maths/Expressions/Matrices/Matrix.cs +++ b/xFunc.Maths/Expressions/Matrices/Matrix.cs @@ -81,7 +81,7 @@ public override bool Equals(object? obj) public object Execute() => Execute(null); /// - public object Execute(IExpressionParameters? parameters) + public object Execute(ExpressionParameters? parameters) { var vectors = new VectorValue[Vectors.Length]; diff --git a/xFunc.Maths/Expressions/Matrices/Transpose.cs b/xFunc.Maths/Expressions/Matrices/Transpose.cs index 2b7ef3866..82af9fb74 100644 --- a/xFunc.Maths/Expressions/Matrices/Transpose.cs +++ b/xFunc.Maths/Expressions/Matrices/Transpose.cs @@ -36,7 +36,7 @@ internal Transpose(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Argument.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Matrices/Vector.cs b/xFunc.Maths/Expressions/Matrices/Vector.cs index 5e4908a3e..273a38e6c 100644 --- a/xFunc.Maths/Expressions/Matrices/Vector.cs +++ b/xFunc.Maths/Expressions/Matrices/Vector.cs @@ -33,7 +33,7 @@ public Vector(ImmutableArray args) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var values = new NumberValue[ParametersCount]; diff --git a/xFunc.Maths/Expressions/Mod.cs b/xFunc.Maths/Expressions/Mod.cs index a04dd71bf..d1b922da3 100644 --- a/xFunc.Maths/Expressions/Mod.cs +++ b/xFunc.Maths/Expressions/Mod.cs @@ -22,7 +22,7 @@ public Mod(IExpression left, IExpression right) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var leftResult = Left.Execute(parameters); var rightResult = Right.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Mul.cs b/xFunc.Maths/Expressions/Mul.cs index 7fd530e53..180528aa8 100644 --- a/xFunc.Maths/Expressions/Mul.cs +++ b/xFunc.Maths/Expressions/Mul.cs @@ -39,7 +39,7 @@ internal Mul(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var leftResult = Left.Execute(parameters); var rightResult = Right.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Number.cs b/xFunc.Maths/Expressions/Number.cs index 18c0e7958..db2d8849b 100644 --- a/xFunc.Maths/Expressions/Number.cs +++ b/xFunc.Maths/Expressions/Number.cs @@ -83,7 +83,7 @@ public override string ToString() public object Execute() => Value; /// - public object Execute(IExpressionParameters? parameters) => Value; + public object Execute(ExpressionParameters? parameters) => Value; /// public TResult Analyze(IAnalyzer analyzer) diff --git a/xFunc.Maths/Expressions/Parameters/ExpressionParameters.ScopedExpressionParameters.cs b/xFunc.Maths/Expressions/Parameters/ExpressionParameters.ScopedExpressionParameters.cs index 4ad561596..86852ea1e 100644 --- a/xFunc.Maths/Expressions/Parameters/ExpressionParameters.ScopedExpressionParameters.cs +++ b/xFunc.Maths/Expressions/Parameters/ExpressionParameters.ScopedExpressionParameters.cs @@ -10,9 +10,9 @@ public partial class ExpressionParameters { private sealed class ScopedExpressionParameters : ExpressionParameters { - private readonly IExpressionParameters parent; + private readonly ExpressionParameters parent; - public ScopedExpressionParameters(IExpressionParameters parent) + public ScopedExpressionParameters(ExpressionParameters parent) : base(false) => this.parent = parent; @@ -30,7 +30,15 @@ public override ParameterValue this[string key] get => collection.TryGetValue(key, out var parameter) ? parameter.Value : parent[key]; - set => base[key] = value; + set + { + if (collection.TryGetValue(key, out _)) + base[key] = value; + else if (parent.TryGetParameter(key, out _)) + parent[key] = value; + else + base[key] = value; + } } public override bool Contains(Parameter param) diff --git a/xFunc.Maths/Expressions/Parameters/ExpressionParameters.WrapperExpressionParameters.cs b/xFunc.Maths/Expressions/Parameters/ExpressionParameters.WrapperExpressionParameters.cs deleted file mode 100644 index aa71db80d..000000000 --- a/xFunc.Maths/Expressions/Parameters/ExpressionParameters.WrapperExpressionParameters.cs +++ /dev/null @@ -1,120 +0,0 @@ -// Copyright (c) Dmytro Kyshchenko. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System.Collections; -using System.Diagnostics.CodeAnalysis; -using System.Globalization; - -namespace xFunc.Maths.Expressions.Parameters; - -/// -/// Strongly typed collection that contains parameters. -/// -public partial class ExpressionParameters -{ - private class WrapperExpressionParameters : IExpressionParameters - { - private readonly IExpressionParameters expressionParameters; - - public WrapperExpressionParameters(IExpressionParameters expressionParameters) - => this.expressionParameters = expressionParameters; - - public virtual ParameterValue this[string key] - { - get => expressionParameters[key]; - set => expressionParameters[key] = value; - } - - public virtual IEnumerator GetEnumerator() - => expressionParameters.GetEnumerator(); - - IEnumerator IEnumerable.GetEnumerator() - => expressionParameters.GetEnumerator(); - - public virtual void Add(Parameter item) - => expressionParameters.Add(item); - - public virtual void Clear() - => expressionParameters.Clear(); - - public virtual bool Contains(Parameter item) - => expressionParameters.Contains(item); - - public virtual bool Remove(Parameter item) - => expressionParameters.Remove(item); - - public virtual bool TryGetParameter(string key, [NotNullWhen(true)] out Parameter? parameter) - => expressionParameters.TryGetParameter(key, out parameter); - - public virtual void Add(string key, ParameterValue value) - => expressionParameters.Add(key, value); - - public virtual void Remove(string key) - => expressionParameters.Remove(key); - - public virtual bool ContainsKey(string key) - => expressionParameters.ContainsKey(key); - } - - private sealed class ScopedWrapperExpressionParameters : WrapperExpressionParameters - { - private readonly WrapperExpressionParameters parent; - - public ScopedWrapperExpressionParameters( - IExpressionParameters expressionParameters, - WrapperExpressionParameters parent) - : base(expressionParameters) - => this.parent = parent; - - public override ParameterValue this[string key] - { - get - { - if (!TryGetParameter(key, out var parameter)) - throw new KeyNotFoundException(string.Format(CultureInfo.InvariantCulture, Resource.VariableNotFoundExceptionError, key)); - - return parameter.Value; - } - set => base[key] = value; - } - - public override IEnumerator GetEnumerator() - { - foreach (var parameter in parent) - yield return parameter; - - using var enumerator = base.GetEnumerator(); - while (enumerator.MoveNext()) - yield return enumerator.Current; - } - - public override void Clear() - { - base.Clear(); - parent.Clear(); - } - - public override bool Contains(Parameter item) - => base.Contains(item) | parent.Contains(item); - - public override bool ContainsKey(string key) - => base.ContainsKey(key) | parent.ContainsKey(key); - - public override bool TryGetParameter(string key, [NotNullWhen(true)] out Parameter? parameter) - { - if (base.TryGetParameter(key, out parameter)) - return true; - - return parent.TryGetParameter(key, out parameter); - } - - public override bool Remove(Parameter item) - => base.Remove(item) | parent.Remove(item); - - public override void Remove(string key) - { - base.Remove(key); - parent.Remove(key); - } - } -} \ No newline at end of file diff --git a/xFunc.Maths/Expressions/Parameters/ExpressionParameters.cs b/xFunc.Maths/Expressions/Parameters/ExpressionParameters.cs index d822bcb25..08af91591 100644 --- a/xFunc.Maths/Expressions/Parameters/ExpressionParameters.cs +++ b/xFunc.Maths/Expressions/Parameters/ExpressionParameters.cs @@ -12,7 +12,7 @@ namespace xFunc.Maths.Expressions.Parameters; /// /// Strongly typed collection that contains parameters. /// -public partial class ExpressionParameters : IExpressionParameters, INotifyCollectionChanged +public partial class ExpressionParameters : IEnumerable, INotifyCollectionChanged { private static readonly Dictionary Constants; private readonly Dictionary collection; @@ -334,7 +334,13 @@ public void Add(Parameter param) public void Add(string key, ParameterValue value) => Add(new Parameter(key, value)); - /// + /// + /// Removes the specified element from this object. + /// + /// The element. + /// is null. + /// The variable is read only. + /// true if item was successfully removed from the collection; otherwise, false. public bool Remove(Parameter param) { if (param is null) @@ -397,23 +403,6 @@ public virtual bool ContainsKey(string key) /// /// The instance of the base parameters collection. /// The expression parameters. - public static IExpressionParameters CreateScoped(IExpressionParameters parameters) + public static ExpressionParameters CreateScoped(ExpressionParameters parameters) => new ScopedExpressionParameters(parameters); - - /// - /// Creates a new instance based on two specified . - /// - /// The instance with actual parameters. - /// The instance with parent parameters. - /// The expression parameters. - public static IExpressionParameters? CreateCombined(IExpressionParameters? actual, IExpressionParameters? parent) - { - if (parent is null) - return actual; - - if (actual is null) - return parent; - - return new ScopedWrapperExpressionParameters(actual, new WrapperExpressionParameters(parent)); - } } \ No newline at end of file diff --git a/xFunc.Maths/Expressions/Parameters/IExpressionParameters.cs b/xFunc.Maths/Expressions/Parameters/IExpressionParameters.cs deleted file mode 100644 index f4dfa74cf..000000000 --- a/xFunc.Maths/Expressions/Parameters/IExpressionParameters.cs +++ /dev/null @@ -1,77 +0,0 @@ -// Copyright (c) Dmytro Kyshchenko. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System.Diagnostics.CodeAnalysis; - -namespace xFunc.Maths.Expressions.Parameters; - -/// -/// Represents a strongly typed collection that contains parameters. -/// -public interface IExpressionParameters : IEnumerable -{ - /// - /// Gets or sets the value of parameter. - /// - /// The name of parameter. - ParameterValue this[string key] { get; set; } - - /// - /// Gets the value of parameter. - /// - /// The name of parameter. - /// The parameter. - /// true if the current collection contains specified parameter, otherwise false. - bool TryGetParameter(string key, [NotNullWhen(true)] out Parameter? parameter); - - /// - /// Adds the specified element to a set. - /// - /// The element. - /// is null. - /// The variable is read only. - void Add(Parameter param); - - /// - /// Adds the specified element to a set. - /// - /// The name of variable. - /// The value of variable. - void Add(string key, ParameterValue value); - - /// - /// Removes the specified element from this object. - /// - /// The element. - /// is null. - /// The variable is read only. - /// true if item was successfully removed from the collection; otherwise, false. - bool Remove(Parameter param); - - /// - /// Removes the specified element from this object. - /// - /// The name of variable. - /// is null. - /// The variable is read only. - void Remove(string key); - - /// - /// Determines whether an object contains the specified key. - /// - /// The name of variable. - /// true if the object contains the specified key; otherwise, false. - bool ContainsKey(string key); - - /// - /// Determines whether an object contains the specified element. - /// - /// The element. - /// true if the object contains the specified element; otherwise, false. - bool Contains(Parameter param); - - /// - /// Clears this collection. - /// - void Clear(); -} \ No newline at end of file diff --git a/xFunc.Maths/Expressions/Pow.cs b/xFunc.Maths/Expressions/Pow.cs index 7b34f4c92..29389e879 100644 --- a/xFunc.Maths/Expressions/Pow.cs +++ b/xFunc.Maths/Expressions/Pow.cs @@ -39,7 +39,7 @@ internal Pow(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var leftResult = Left.Execute(parameters); var rightResult = Right.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Programming/ConditionalAnd.cs b/xFunc.Maths/Expressions/Programming/ConditionalAnd.cs index 0c7bd791f..b607adf1b 100644 --- a/xFunc.Maths/Expressions/Programming/ConditionalAnd.cs +++ b/xFunc.Maths/Expressions/Programming/ConditionalAnd.cs @@ -21,7 +21,7 @@ public ConditionalAnd(IExpression left, IExpression right) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var left = Left.Execute(parameters); var right = Right.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Programming/ConditionalOr.cs b/xFunc.Maths/Expressions/Programming/ConditionalOr.cs index b72f708a0..bfed2fde8 100644 --- a/xFunc.Maths/Expressions/Programming/ConditionalOr.cs +++ b/xFunc.Maths/Expressions/Programming/ConditionalOr.cs @@ -21,7 +21,7 @@ public ConditionalOr(IExpression left, IExpression right) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var left = Left.Execute(parameters); var right = Right.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Programming/Equal.cs b/xFunc.Maths/Expressions/Programming/Equal.cs index 12ddec844..c5aa6ce48 100644 --- a/xFunc.Maths/Expressions/Programming/Equal.cs +++ b/xFunc.Maths/Expressions/Programming/Equal.cs @@ -21,7 +21,7 @@ public Equal(IExpression left, IExpression right) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var leftResult = Left.Execute(parameters); var rightResult = Right.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Programming/For.cs b/xFunc.Maths/Expressions/Programming/For.cs index 661fa687c..6e12a44fd 100644 --- a/xFunc.Maths/Expressions/Programming/For.cs +++ b/xFunc.Maths/Expressions/Programming/For.cs @@ -34,7 +34,7 @@ public For(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { for (Initialization.Execute(parameters); (bool)Condition.Execute(parameters); Iteration.Execute(parameters)) Body.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Programming/GreaterOrEqual.cs b/xFunc.Maths/Expressions/Programming/GreaterOrEqual.cs index 959acf9ca..9a64b58c4 100644 --- a/xFunc.Maths/Expressions/Programming/GreaterOrEqual.cs +++ b/xFunc.Maths/Expressions/Programming/GreaterOrEqual.cs @@ -21,7 +21,7 @@ public GreaterOrEqual(IExpression left, IExpression right) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var leftResult = Left.Execute(parameters); var rightResult = Right.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Programming/GreaterThan.cs b/xFunc.Maths/Expressions/Programming/GreaterThan.cs index d50ef08f6..e9e83bf6f 100644 --- a/xFunc.Maths/Expressions/Programming/GreaterThan.cs +++ b/xFunc.Maths/Expressions/Programming/GreaterThan.cs @@ -21,7 +21,7 @@ public GreaterThan(IExpression left, IExpression right) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var leftResult = Left.Execute(parameters); var rightResult = Right.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Programming/If.cs b/xFunc.Maths/Expressions/Programming/If.cs index 698572151..93968a5af 100644 --- a/xFunc.Maths/Expressions/Programming/If.cs +++ b/xFunc.Maths/Expressions/Programming/If.cs @@ -42,7 +42,7 @@ public If(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Condition.Execute(parameters); if (result is bool condition) diff --git a/xFunc.Maths/Expressions/Programming/LeftShift.cs b/xFunc.Maths/Expressions/Programming/LeftShift.cs index 5bed2effd..1c6f70930 100644 --- a/xFunc.Maths/Expressions/Programming/LeftShift.cs +++ b/xFunc.Maths/Expressions/Programming/LeftShift.cs @@ -21,7 +21,7 @@ public LeftShift(IExpression left, IExpression right) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var leftResult = Left.Execute(parameters); var rightResult = Right.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Programming/LessOrEqual.cs b/xFunc.Maths/Expressions/Programming/LessOrEqual.cs index 908fc6809..b7f7c4505 100644 --- a/xFunc.Maths/Expressions/Programming/LessOrEqual.cs +++ b/xFunc.Maths/Expressions/Programming/LessOrEqual.cs @@ -21,7 +21,7 @@ public LessOrEqual(IExpression left, IExpression right) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var leftResult = Left.Execute(parameters); var rightResult = Right.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Programming/LessThan.cs b/xFunc.Maths/Expressions/Programming/LessThan.cs index 59589467f..a2ae10639 100644 --- a/xFunc.Maths/Expressions/Programming/LessThan.cs +++ b/xFunc.Maths/Expressions/Programming/LessThan.cs @@ -21,7 +21,7 @@ public LessThan(IExpression left, IExpression right) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var leftResult = Left.Execute(parameters); var rightResult = Right.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Programming/NotEqual.cs b/xFunc.Maths/Expressions/Programming/NotEqual.cs index daed841f4..f91e75433 100644 --- a/xFunc.Maths/Expressions/Programming/NotEqual.cs +++ b/xFunc.Maths/Expressions/Programming/NotEqual.cs @@ -21,7 +21,7 @@ public NotEqual(IExpression left, IExpression right) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var leftResult = Left.Execute(parameters); var rightResult = Right.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Programming/RightShift.cs b/xFunc.Maths/Expressions/Programming/RightShift.cs index a68a97139..3c3b4a066 100644 --- a/xFunc.Maths/Expressions/Programming/RightShift.cs +++ b/xFunc.Maths/Expressions/Programming/RightShift.cs @@ -21,7 +21,7 @@ public RightShift(IExpression left, IExpression right) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var leftResult = Left.Execute(parameters); var rightResult = Right.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Programming/VariableBinaryExpression.cs b/xFunc.Maths/Expressions/Programming/VariableBinaryExpression.cs index 2161e63cd..570492132 100644 --- a/xFunc.Maths/Expressions/Programming/VariableBinaryExpression.cs +++ b/xFunc.Maths/Expressions/Programming/VariableBinaryExpression.cs @@ -47,7 +47,7 @@ public override bool Equals(object? obj) public object Execute() => Execute(null); /// - public object Execute(IExpressionParameters? parameters) + public object Execute(ExpressionParameters? parameters) { if (parameters is null) throw new ArgumentNullException(nameof(parameters)); diff --git a/xFunc.Maths/Expressions/Programming/VariableUnaryExpression.cs b/xFunc.Maths/Expressions/Programming/VariableUnaryExpression.cs index bcb0ef059..ec96fb673 100644 --- a/xFunc.Maths/Expressions/Programming/VariableUnaryExpression.cs +++ b/xFunc.Maths/Expressions/Programming/VariableUnaryExpression.cs @@ -41,7 +41,7 @@ public override bool Equals(object? obj) public object Execute() => Execute(null); /// - public object Execute(IExpressionParameters? parameters) + public object Execute(ExpressionParameters? parameters) { if (parameters is null) throw new ArgumentNullException(nameof(parameters)); diff --git a/xFunc.Maths/Expressions/Programming/While.cs b/xFunc.Maths/Expressions/Programming/While.cs index ee0af4f31..106c86a27 100644 --- a/xFunc.Maths/Expressions/Programming/While.cs +++ b/xFunc.Maths/Expressions/Programming/While.cs @@ -21,7 +21,7 @@ public While(IExpression body, IExpression condition) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { while ((bool)Right.Execute(parameters)) Left.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Root.cs b/xFunc.Maths/Expressions/Root.cs index 84e871110..796502a73 100644 --- a/xFunc.Maths/Expressions/Root.cs +++ b/xFunc.Maths/Expressions/Root.cs @@ -38,7 +38,7 @@ internal Root(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var leftResult = Left.Execute(parameters); var rightResult = Right.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Round.cs b/xFunc.Maths/Expressions/Round.cs index f6af243a7..c1aca6e5d 100644 --- a/xFunc.Maths/Expressions/Round.cs +++ b/xFunc.Maths/Expressions/Round.cs @@ -48,7 +48,7 @@ internal Round(ImmutableArray args) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Argument.Execute(parameters); var digits = Digits?.Execute(parameters) ?? new NumberValue(0.0); diff --git a/xFunc.Maths/Expressions/Sign.cs b/xFunc.Maths/Expressions/Sign.cs index a9832ff88..b7ececf6d 100644 --- a/xFunc.Maths/Expressions/Sign.cs +++ b/xFunc.Maths/Expressions/Sign.cs @@ -37,7 +37,7 @@ internal Sign(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Argument.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Simplify.cs b/xFunc.Maths/Expressions/Simplify.cs index 754f1d5f5..914e4c336 100644 --- a/xFunc.Maths/Expressions/Simplify.cs +++ b/xFunc.Maths/Expressions/Simplify.cs @@ -48,7 +48,7 @@ internal Simplify(ISimplifier simplifier, ImmutableArray arguments) /// /// Simplifier is null. /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Argument.Execute(parameters); if (result is not Lambda lambda) diff --git a/xFunc.Maths/Expressions/Sqrt.cs b/xFunc.Maths/Expressions/Sqrt.cs index b0bc844f2..df0fe4c62 100644 --- a/xFunc.Maths/Expressions/Sqrt.cs +++ b/xFunc.Maths/Expressions/Sqrt.cs @@ -37,7 +37,7 @@ internal Sqrt(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Argument.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Statistical/StatisticalExpression.cs b/xFunc.Maths/Expressions/Statistical/StatisticalExpression.cs index b9c438102..071ee73e4 100644 --- a/xFunc.Maths/Expressions/Statistical/StatisticalExpression.cs +++ b/xFunc.Maths/Expressions/Statistical/StatisticalExpression.cs @@ -38,7 +38,7 @@ protected StatisticalExpression(ImmutableArray arguments) protected abstract object ExecuteInternal(VectorValue vector); /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var vector = default(VectorValue); diff --git a/xFunc.Maths/Expressions/StringExpression.cs b/xFunc.Maths/Expressions/StringExpression.cs index 7effbeab8..0a284941d 100644 --- a/xFunc.Maths/Expressions/StringExpression.cs +++ b/xFunc.Maths/Expressions/StringExpression.cs @@ -60,7 +60,7 @@ public override string ToString() public object Execute() => Value; /// - public object Execute(IExpressionParameters? parameters) => Value; + public object Execute(ExpressionParameters? parameters) => Value; /// public TResult Analyze(IAnalyzer analyzer) diff --git a/xFunc.Maths/Expressions/Sub.cs b/xFunc.Maths/Expressions/Sub.cs index b507781cf..0102150f1 100644 --- a/xFunc.Maths/Expressions/Sub.cs +++ b/xFunc.Maths/Expressions/Sub.cs @@ -38,7 +38,7 @@ internal Sub(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var leftResult = Left.Execute(parameters); var rightResult = Right.Execute(parameters); diff --git a/xFunc.Maths/Expressions/ToBin.cs b/xFunc.Maths/Expressions/ToBin.cs index 90f325bea..381bc9ca1 100644 --- a/xFunc.Maths/Expressions/ToBin.cs +++ b/xFunc.Maths/Expressions/ToBin.cs @@ -36,7 +36,7 @@ internal ToBin(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Argument.Execute(parameters); diff --git a/xFunc.Maths/Expressions/ToHex.cs b/xFunc.Maths/Expressions/ToHex.cs index f1920914f..940453d6d 100644 --- a/xFunc.Maths/Expressions/ToHex.cs +++ b/xFunc.Maths/Expressions/ToHex.cs @@ -36,7 +36,7 @@ internal ToHex(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Argument.Execute(parameters); diff --git a/xFunc.Maths/Expressions/ToOct.cs b/xFunc.Maths/Expressions/ToOct.cs index 66652182c..4b982bde8 100644 --- a/xFunc.Maths/Expressions/ToOct.cs +++ b/xFunc.Maths/Expressions/ToOct.cs @@ -36,7 +36,7 @@ internal ToOct(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Argument.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Trigonometric/InverseTrigonometricExpression.cs b/xFunc.Maths/Expressions/Trigonometric/InverseTrigonometricExpression.cs index 5c40e5a1f..f21a987bb 100644 --- a/xFunc.Maths/Expressions/Trigonometric/InverseTrigonometricExpression.cs +++ b/xFunc.Maths/Expressions/Trigonometric/InverseTrigonometricExpression.cs @@ -58,7 +58,7 @@ protected InverseTrigonometricExpression(ImmutableArray arguments) /// A result of the calculation. /// /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Argument.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Trigonometric/TrigonometricExpression.cs b/xFunc.Maths/Expressions/Trigonometric/TrigonometricExpression.cs index dccbc74c8..fb89e2308 100644 --- a/xFunc.Maths/Expressions/Trigonometric/TrigonometricExpression.cs +++ b/xFunc.Maths/Expressions/Trigonometric/TrigonometricExpression.cs @@ -57,7 +57,7 @@ protected TrigonometricExpression(ImmutableArray arguments) /// A result of the calculation. /// /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Argument.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Trunc.cs b/xFunc.Maths/Expressions/Trunc.cs index 7b195cbd4..982ef39d8 100644 --- a/xFunc.Maths/Expressions/Trunc.cs +++ b/xFunc.Maths/Expressions/Trunc.cs @@ -36,7 +36,7 @@ internal Trunc(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Argument.Execute(parameters); diff --git a/xFunc.Maths/Expressions/UnaryExpression.cs b/xFunc.Maths/Expressions/UnaryExpression.cs index 0fcad7f0c..e8cbfc33a 100644 --- a/xFunc.Maths/Expressions/UnaryExpression.cs +++ b/xFunc.Maths/Expressions/UnaryExpression.cs @@ -63,7 +63,7 @@ public override bool Equals(object? obj) public object Execute() => Execute(null); /// - public abstract object Execute(IExpressionParameters? parameters); + public abstract object Execute(ExpressionParameters? parameters); /// public TResult Analyze(IAnalyzer analyzer) diff --git a/xFunc.Maths/Expressions/UnaryMinus.cs b/xFunc.Maths/Expressions/UnaryMinus.cs index b1a4de08b..5b5b691de 100644 --- a/xFunc.Maths/Expressions/UnaryMinus.cs +++ b/xFunc.Maths/Expressions/UnaryMinus.cs @@ -20,7 +20,7 @@ public UnaryMinus(IExpression expression) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Argument.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Unassign.cs b/xFunc.Maths/Expressions/Unassign.cs index f06479b1a..515d87fd4 100644 --- a/xFunc.Maths/Expressions/Unassign.cs +++ b/xFunc.Maths/Expressions/Unassign.cs @@ -39,7 +39,7 @@ public object Execute() => throw new NotSupportedException(); /// - public object Execute(IExpressionParameters? parameters) + public object Execute(ExpressionParameters? parameters) { if (parameters is null) throw new ArgumentNullException(nameof(parameters)); diff --git a/xFunc.Maths/Expressions/Units/AngleUnits/ToDegree.cs b/xFunc.Maths/Expressions/Units/AngleUnits/ToDegree.cs index 9804996b7..cde6e19a6 100644 --- a/xFunc.Maths/Expressions/Units/AngleUnits/ToDegree.cs +++ b/xFunc.Maths/Expressions/Units/AngleUnits/ToDegree.cs @@ -37,7 +37,7 @@ internal ToDegree(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Argument.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Units/AngleUnits/ToGradian.cs b/xFunc.Maths/Expressions/Units/AngleUnits/ToGradian.cs index bc961d9f5..ae4251dfc 100644 --- a/xFunc.Maths/Expressions/Units/AngleUnits/ToGradian.cs +++ b/xFunc.Maths/Expressions/Units/AngleUnits/ToGradian.cs @@ -37,7 +37,7 @@ internal ToGradian(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Argument.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Units/AngleUnits/ToRadian.cs b/xFunc.Maths/Expressions/Units/AngleUnits/ToRadian.cs index 38e94f2f5..1b3bd46c7 100644 --- a/xFunc.Maths/Expressions/Units/AngleUnits/ToRadian.cs +++ b/xFunc.Maths/Expressions/Units/AngleUnits/ToRadian.cs @@ -37,7 +37,7 @@ internal ToRadian(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Argument.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Units/Convert.cs b/xFunc.Maths/Expressions/Units/Convert.cs index 9ea21ee6b..5e6e288ed 100644 --- a/xFunc.Maths/Expressions/Units/Convert.cs +++ b/xFunc.Maths/Expressions/Units/Convert.cs @@ -75,7 +75,7 @@ public override bool Equals(object? obj) public object Execute() => Execute(null); /// - public object Execute(IExpressionParameters? parameters) + public object Execute(ExpressionParameters? parameters) { var valueResult = Value.Execute(parameters); var unitResult = Unit.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Units/ToNumber.cs b/xFunc.Maths/Expressions/Units/ToNumber.cs index ab3682226..64a129b7b 100644 --- a/xFunc.Maths/Expressions/Units/ToNumber.cs +++ b/xFunc.Maths/Expressions/Units/ToNumber.cs @@ -32,7 +32,7 @@ internal ToNumber(ImmutableArray arguments) } /// - public override object Execute(IExpressionParameters? parameters) + public override object Execute(ExpressionParameters? parameters) { var result = Argument.Execute(parameters); diff --git a/xFunc.Maths/Expressions/Units/Unit.cs b/xFunc.Maths/Expressions/Units/Unit.cs index 50a87ac77..05225da8c 100644 --- a/xFunc.Maths/Expressions/Units/Unit.cs +++ b/xFunc.Maths/Expressions/Units/Unit.cs @@ -61,7 +61,7 @@ public override string ToString() public object Execute() => Value; /// - public object Execute(IExpressionParameters? parameters) => Value; + public object Execute(ExpressionParameters? parameters) => Value; /// public TResult Analyze(IAnalyzer analyzer) diff --git a/xFunc.Maths/Expressions/Variable.cs b/xFunc.Maths/Expressions/Variable.cs index 64316057d..b907694d1 100644 --- a/xFunc.Maths/Expressions/Variable.cs +++ b/xFunc.Maths/Expressions/Variable.cs @@ -92,7 +92,7 @@ public override string ToString() public object Execute() => throw new NotSupportedException(); /// - public object Execute(IExpressionParameters? parameters) + public object Execute(ExpressionParameters? parameters) { if (parameters is null) throw new ArgumentNullException(nameof(parameters)); diff --git a/xFunc.Tests/Expressions/DelegateExpressionTest.cs b/xFunc.Tests/Expressions/DelegateExpressionTest.cs index e12c24899..3cb26c3a2 100644 --- a/xFunc.Tests/Expressions/DelegateExpressionTest.cs +++ b/xFunc.Tests/Expressions/DelegateExpressionTest.cs @@ -66,7 +66,7 @@ public void EqualDiffTypeTest() [Fact] public void EqualSameTest() { - Func d = _ => 1.0; + Func d = _ => 1.0; var exp1 = new DelegateExpression(d); var exp2 = new DelegateExpression(d); diff --git a/xFunc.Tests/Expressions/Parameters/ExpressionParameterTest.cs b/xFunc.Tests/Expressions/Parameters/ExpressionParameterTest.cs index 86cf9d9fb..256428f8c 100644 --- a/xFunc.Tests/Expressions/Parameters/ExpressionParameterTest.cs +++ b/xFunc.Tests/Expressions/Parameters/ExpressionParameterTest.cs @@ -267,6 +267,46 @@ public void ScopedGetParentValue() Assert.Equal(parameter.Value, result); } + [Fact] + public void ScopedSetActualValue() + { + const string key = "x"; + var parent = new ExpressionParameters(false) + { + [key] = new ParameterValue(NumberValue.One) + }; + + var scoped = ExpressionParameters.CreateScoped(parent); + scoped.Add(key, new ParameterValue(NumberValue.One)); + scoped[key] = new ParameterValue(NumberValue.Two); + + var result = scoped[key]; + var parentResult = parent[key]; + + Assert.Equal(new ParameterValue(NumberValue.Two), result); + Assert.Equal(new ParameterValue(NumberValue.One), parentResult); + } + + [Fact] + public void ScopedSetParentValue() + { + const string key = "x"; + + var parent = new ExpressionParameters(false) + { + [key] = new ParameterValue(NumberValue.One) + }; + + var scoped = ExpressionParameters.CreateScoped(parent); + scoped[key] = new ParameterValue(NumberValue.Two); + + var result = scoped[key]; + var parentResult = parent[key]; + + Assert.Equal(new ParameterValue(NumberValue.Two), result); + Assert.Equal(new ParameterValue(NumberValue.Two), parentResult); + } + [Fact] public void ScopedContainsParentValue() { diff --git a/xFunc.Tests/ProcessorTest.cs b/xFunc.Tests/ProcessorTest.cs index f93e5f0a8..be19f7788 100644 --- a/xFunc.Tests/ProcessorTest.cs +++ b/xFunc.Tests/ProcessorTest.cs @@ -357,4 +357,16 @@ public void LambdaClosureTest2() Assert.Equal("(x) => (y) => x + y", lambdaResult.Result.ToString()); Assert.Equal(3.0, result.Result); } + + [Fact] + public void ClosureTest() + { + var processor = new Processor(); + + processor.Solve("x := 1"); + processor.Solve("(() => x := 2)()"); + var result = processor.Solve("x"); + + Assert.Equal(2.0, result.Result); + } } \ No newline at end of file