dynamicexpresso / DynamicExpresso

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

Expression must be writable error. #321

Open mercurial-moon opened 2 weeks ago

mercurial-moon commented 2 weeks ago

Code

var ipt = new Interpreter()
ipt.SetVariable("a", 0);
ipt.SetVariable("b", 10);
ipt.SetVariable("c", 20);
var result = ipt.Eval("a = b + c"); // <= Error: DynamicExpresso.Exceptions.ParseException: 'Expression must be writable (at index 2).'

also tried changing first line to

var ipt = new Interpreter().EnableAssignment(AssignmentOperators.All);

but result is same. if i remove the assignment it works

var result = ipt.Eval("b + c"); // 30
davideicardi commented 2 weeks ago

Yes, sorry, this is not supported. Variables are "read only". What I think you can do is create a object and assign one of its property. Something like:

var ipt = new Interpreter().EnableAssignment(AssignmentOperators.All);
ipt.SetVariable("registry", someObject);
ipt.SetVariable("b", 10);
ipt.SetVariable("c", 20);
var result = ipt.Eval("registry.a = b + c");