crystal-community / crystal-libraries-needed

A list of libraries that are needed or wanted for the Crystal-Language
141 stars 4 forks source link

Math parser / evaluator #113

Closed alexanderadam closed 4 years ago

alexanderadam commented 4 years ago

I'm just curious: is there any Shard for evaluating math formulas (like dentaku)?

RX14 commented 4 years ago

There's execute_infix in my rpn.cr.

It doesn't support variables yet, though i'd definitely be open a PR.

alexanderadam commented 4 years ago

Wonderful! I guess I can close this then! Thank you for responding so fast :+1:

straight-shoota commented 4 years ago

Crinja is a full-fledged template engine, but it's also capable of evaluating math expressions. Variables are obviously supported, as well as many more complex features.

require "crinja"
Crinja.new.evaluate "1 + x", {"x" => 3} # => 4

There is no explicit mode to dumb it down to only allow math expressions, but individual features can be disabled in the config, creating a sandboxing mode.

alexanderadam commented 4 years ago

Thank you for the advice. I didn't thought of using template engines for evaling. But when I needed a math evaluator, I needed it to be as 'dumb' as possible, to rule out misuse. So it should do mathematics and nothing else.

RX14 commented 4 years ago

@alexanderadam if you need variables implemented just open an issue and i'll probably have a crack at it

alexanderadam commented 4 years ago

@RX14 I will. At the moment I was just wondering whether there is some math evaluator at all. But I will open an issue in case I will need variables for sure!

btw while we are on it: thank you (both) for your awesome libraries! :pray: