Skip to content

Commit

Permalink
Allow a lambda expression to access the initial expression's parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
metoule committed Dec 8, 2021
1 parent ae65ecb commit dd180af
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
13 changes: 10 additions & 3 deletions src/DynamicExpresso.Core/Parsing/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private Expression ParseLambdaExpression()
}

var lambdaBodyExp = _expressionText.Substring(startExpr, _token.pos - startExpr);
return new InterpreterExpression(_arguments.Settings, lambdaBodyExp, parameters);
return new InterpreterExpression(_arguments, lambdaBodyExp, parameters);
}
catch (ParseException)
{
Expand Down Expand Up @@ -3012,12 +3012,19 @@ private class InterpreterExpression : Expression
private readonly IList<Parameter> _parameters;
private Type _type;

public InterpreterExpression(ParserSettings parentSettings, string expressionText, params Parameter[] parameters)
public InterpreterExpression(ParserArguments parserArguments, string expressionText, params Parameter[] parameters)
{
_interpreter = new Interpreter(parentSettings);
_interpreter = new Interpreter(parserArguments.Settings.Clone());
_expressionText = expressionText;
_parameters = parameters;

// convert the parent's parameters to variables
// note: this doesn't impact the initial settings, because they're cloned
foreach (var pe in parserArguments.DeclaredParameters)
{
_interpreter.SetVariable(pe.Name, pe.Value, pe.Type);
}

// prior to evaluation, we don't know the generic arguments types
_type = typeof(Func<>).Assembly.GetType($"System.Func`{parameters.Length + 1}");
}
Expand Down
21 changes: 17 additions & 4 deletions src/DynamicExpresso.Core/Parsing/ParserSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ namespace DynamicExpresso.Parsing
{
internal class ParserSettings
{
private readonly Dictionary<string, Identifier> _identifiers;
private readonly Dictionary<string, ReferenceType> _knownTypes;
private readonly HashSet<MethodInfo> _extensionMethods;
private Dictionary<string, Identifier> _identifiers;
private Dictionary<string, ReferenceType> _knownTypes;
private HashSet<MethodInfo> _extensionMethods;

public ParserSettings(bool caseInsensitive,bool lateBindObject)
{
CaseInsensitive = caseInsensitive;
Expand All @@ -33,6 +33,19 @@ public ParserSettings(bool caseInsensitive,bool lateBindObject)
LambdaExpressions = false;
}

/// <summary>
/// Creates a deep copy of the current settings, so that the identifiers/types/methods can be changed
/// without impacting the existing settings.
/// </summary>
internal ParserSettings Clone()
{
var clone = (ParserSettings)MemberwiseClone();
clone._identifiers = new Dictionary<string, Identifier>(_identifiers);
clone._knownTypes = new Dictionary<string, ReferenceType>(_knownTypes);
clone._extensionMethods = new HashSet<MethodInfo>(_extensionMethods);
return clone;
}

public IDictionary<string, ReferenceType> KnownTypes
{
get { return _knownTypes; }
Expand Down
31 changes: 31 additions & 0 deletions test/DynamicExpresso.UnitTest/LambdaExpressionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,37 @@ public void Zip()
Assert.AreEqual(3, results.Count());
Assert.AreEqual(strList.Zip(intList, (str, i) => str + i), results);
}

[Test]
public void Lambda_with_parameter()
{
var target = new Interpreter(InterpreterOptions.Default | InterpreterOptions.LambdaExpressions);
var listInt = target.Eval<IEnumerable<int>>("list.Where(n => n > x)", new Parameter("list", new[] { 1, 2, 3 }), new Parameter("x", 1));
Assert.AreEqual(new[] { 2, 3 }, listInt);

// ensure the parameters can be reused with different values
listInt = target.Eval<IEnumerable<int>>("list.Where(n => n > x)", new Parameter("list", new[] { 2, 4, 5 }), new Parameter("x", 2));
Assert.AreEqual(new[] { 4, 5 }, listInt);
}

[Test]
public void Lambda_with_parameter_2()
{
var target = new Interpreter(InterpreterOptions.Default | InterpreterOptions.LambdaExpressions);
var listInt = target.Eval<IEnumerable<int>>("list.Select(n => n - 1).Where(n => n > x).Select(n => n + x)", new Parameter("list", new[] { 1, 2, 3 }), new Parameter("x", 1));
Assert.AreEqual(new[] { 3 }, listInt);
}

[Test]
public void Lambda_with_variable()
{
var target = new Interpreter(InterpreterOptions.Default | InterpreterOptions.LambdaExpressions);
target.SetVariable("list", new[] { 1, 2, 3 });
target.SetVariable("x", 1);

var listInt = target.Eval<IEnumerable<int>>("list.Where(n => n > x)");
Assert.AreEqual(new[] { 2, 3 }, listInt);
}
}

/// <summary>
Expand Down

0 comments on commit dd180af

Please sign in to comment.