narrowtux / abacus

Parses and evaluates mathematical expressions in Elixir. Inspired by math.js
MIT License
84 stars 18 forks source link

Support Polish Notation #19

Open ghost opened 3 years ago

ghost commented 3 years ago

Supporting Polish Notation would be a cool addition to this library in my opinion. Or a polish notation evaluation library may be a idea for another math library? But I dont know how something like this would be done. Just throwing the idea out here.

https://en.wikipedia.org/wiki/Polish_notation

Thanks for reading my issue!

narrowtux commented 3 years ago

I think it could be implemented as another parse function, like parse_polish or so. The resulting AST would be the same (as it's elixir AST anyway).

ghost commented 3 years ago

I think it could be implemented as another parse function, like parse_polish or so. The resulting AST would be the same (as it's elixir AST anyway).

Do you like the idea of having this in your library? Or would it be something that would be at application level? Update: I think I am going to create add-on library for fun and call it PolishAbacus or smth like this. It would would have a function that would parse to a syntax tree that could be used with eval etc

narrowtux commented 3 years ago

I'll gladly accept a PR that adds a second function to parse a different syntax into the same internal representation (the AST). If you need any pointers, feel free to ask. I think writing your own parser is an excellent excercise.

narrowtux commented 3 years ago

You can probably get away with copying and then modifying this file: https://github.com/narrowtux/abacus/blob/master/src/math_term_parser.yrl

This is in erlangs yecc language: https://erlang.org/doc/man/yecc.html

When it's compiled, a module named :math_term_parser will be available.

ghost commented 3 years ago

Thanks, I will try it out and send a PR! :)