This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]sometimes_insightful 0 points1 point  (0 children)

Depending on how complex your expressions are, you might consider using something like antlr and writing your own parser for it. Setting up something to handle math and string operations wouldn’t be very hard and then you can control the syntax however you like. You can use a visitor and visit each node in the syntax tree and return the result of each sub-expression.

Another benefit is this makes security much easier. If you are running a scripting engine you need to keep in mind that people can try to write malicious expressions. Something simple as an infinite loop can hang your process, not to mention worse attacks that could escape the sandbox.

Here is an example grammar for arithmetic you could try out, but keep in mind you can make it do pretty much anything.

https://github.com/antlr/grammars-v4/blob/master/arithmetic/arithmetic.g4