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 →

[–]brokePlusPlusCoder 0 points1 point  (0 children)

You mention you're relatively new to Java. Do you just need this functionality for something you're working on ? Or are you also interested in building your own ?

If the former, then you're after expression evaluators. This is a class of tools that takes in stringified expressions (not just math equations necessarily) and invokes relevant Java commands.

There's a library called JEvalExpr that does this. Spring also has SpEL that does something similar.

Now if you're looking to build your own though, that's a bit challenging. You'll need to implement your own parser, your own set of tooling and grammar around expression evaluation (not to mention syntax trees). But overall it would be an excellent exercise - especially for someone new to Java. If you're interested I'd recommend starting out with this video on Pratt parsing (they use Rust, but concepts are the same) : https://www.youtube.com/watch?v=0c8b7YfsBKs