dynamicexpresso / DynamicExpresso

C# expressions interpreter
http://dynamic-expresso.azurewebsites.net/
MIT License
2.01k stars 379 forks source link

Parse unknown identifiers for lambda expressions #226

Closed ealeykin closed 2 years ago

ealeykin commented 2 years ago

This is a very helpful feature, but when used against expressions with lambdas e.g. list.Any(x => x == null) - x here is an unknown identifier.

So I'm note sure whether this is an expected behaviour, because from expression inputs perspective - yes it's unknown, however it won't raise any compilation errors (since I enabled lambdas) and it's more likely a runtime variable rather than an unknown input identifier.

metoule commented 2 years ago

Repro example:

var target = new Interpreter(InterpreterOptions.Default | InterpreterOptions.LambdaExpressions);
target.SetVariable("list", new List<string>());
target.DetectIdentifiers("list.Any(x => x == null)").Dump();

Results:

{
  "UnknownIdentifiers": [
    "x"
  ],
  "Identifiers": [
    {
      "Name": "list",
      "Expression": {
        "Type": "List<string>",
        "NodeType": "Constant",
        "Value": [],
        "CanReduce": false
      }
    },
    {
      "Name": "null",
      "Expression": {
        "Type": "object",
        "NodeType": "Constant",
        "CanReduce": false
      }
    }
  ],
  "Types": []
}

x is detected as an unknown identifier.

@halamah that's an oversight on my part, I didn't think of that when I implemented the lambda expression support.