Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please allow bracket in parameter name #25

Open
Thaina opened this issue Mar 14, 2019 · 3 comments
Open

Please allow bracket in parameter name #25

Thaina opened this issue Mar 14, 2019 · 3 comments

Comments

@Thaina
Copy link

Thaina commented Mar 14, 2019

I wish I could write parameter name with bracket like this

var array = new float[] { 1,2,3,4,5,6 };
var e = new Expression("1 + (2 * [array[0]])");
e.EvaluateParameter += delegate(string name, ParameterArgs args) {
      // got "array[0]" as name
      if (name.StartsWith("array[") && name.EndsWith("]"))
      {
          int i = int.Parse(name.Replace("array[","").Replace("]",""));
          args.Result = array[i];
      }
};

If possible I would like to have it allow arbitrary nested bracket

var keys = new string[] { key0,key1,key2 };
var dict = new Dictionary<string,float>();
var e = new Expression("1 + (2 * [dict[key[0]]])");
e.EvaluateParameter += delegate(string name, ParameterArgs args) {
      // got "dict[key[0]]" as name for doing lookup
};
Thaina added a commit to Thaina/NCalc2 that referenced this issue Mar 18, 2019
@Thaina
Copy link
Author

Thaina commented Aug 19, 2019

@sklose

Will changing this

NCalc2/grammer/NCalc.g

Lines 265 to 268 in 19359d6

NAME : '[' (options {greedy=false;} : ~(']')*) ']'
;

To this

NAME	:	'[' ( (options {greedy=false;} : ~('[' | ']')*) NAME* )* ']'
	;

Did the job?

@Thaina
Copy link
Author

Thaina commented Aug 20, 2019

Today I have clone your repo and change it to antlr4

This rule work for me

NAME	:	'[' (~('[' | ']') | NAME)*? ']'
	;

If you interest https://github.com/Thaina/NCalc2

@Bykiev
Copy link
Contributor

Bykiev commented Mar 26, 2024

@Thaina, I believe it'll be better to use EvaluateOptions.IterateParameters like this:

var e = Extensions.CreateExpression("x * x", EvaluateOptions.IterateParameters);
e.Parameters["x"] = new[] { 0, 1, 2, 3, 4 };
var result = (IList)e.Evaluate();
Assert.Equal(0, result[0]);
Assert.Equal(1, result[1]);
Assert.Equal(4, result[2]);
Assert.Equal(9, result[3]);
Assert.Equal(16, result[4]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants