A RPN parser and executor written in crystal. It can parse RPN from both infix and RPN strings, and execute RPN to find a result.
Currently it supports +
-
*
/
and ^
(exponent) operators.
Add this to your application's shard.yml
:
dependencies:
rpn:
github: RX14/rpn.cr
version: 0.1.0
require "rpn"
Execute RPN given as an array of numbers and symbols
RPN.execute([0.5, 2, :"*", 4 :"+"]) # => 5.0
Parse RPN from a string containing RPN notation
RPN.from_string("-.5 2 * 4 +") # => [-0.5, 2.0, :"*", 4.0, :"+"]
Parse RPN from a string containing infix notation
RPN.from_infix("4 + (5 - 3)^4 * 2") # => [4.0, 5.0, 3.0, :"-", 4.0, :"^", 2.0, :"*", :"+"]
There are shortcut methods for executing strings directly.
RPN.execute_string("3 4 +")
# Same as
RPN.execute(RPN.from_string("3 4 +"))
RPN.execute_infix("3 + 4")
# Same as
RPN.execute(RPN.from_infix("3 + 4"))
Hack around, then crystal spec
.
- Fork it ( https://github.com/RX14/rpn.cr/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
- RX14 - creator, maintainer