https://github.com/rx14/rpn.cr
https://github.com/rx14/rpn.cr
crystal efficiency expression infix-notation math rpn
Last synced: 11 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/rx14/rpn.cr
- Owner: RX14
- License: mit
- Created: 2016-10-29T00:33:04.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2019-09-08T12:58:24.000Z (over 6 years ago)
- Last Synced: 2025-01-04T19:43:18.840Z (about 1 year ago)
- Topics: crystal, efficiency, expression, infix-notation, math, rpn
- Language: Crystal
- Size: 15.6 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# rpn.cr
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.
## Installation
Add this to your application's `shard.yml`:
```yaml
dependencies:
rpn:
github: RX14/rpn.cr
version: 0.1.0
```
## Usage
```crystal
require "rpn"
```
Execute RPN given as an array of numbers and symbols
```cr
RPN.execute([0.5, 2, :"*", 4 :"+"]) # => 5.0
```
Parse RPN from a string containing RPN notation
```cr
RPN.from_string("-.5 2 * 4 +") # => [-0.5, 2.0, :"*", 4.0, :"+"]
```
Parse RPN from a string containing infix notation
```cr
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.
```cr
RPN.execute_string("3 4 +")
# Same as
RPN.execute(RPN.from_string("3 4 +"))
```
```cr
RPN.execute_infix("3 + 4")
# Same as
RPN.execute(RPN.from_infix("3 + 4"))
```
## Development
Hack around, then `crystal spec`.
## Contributing
1. Fork it ( https://github.com/RX14/rpn.cr/fork )
2. Create your feature branch (git checkout -b my-new-feature)
3. Commit your changes (git commit -am 'Add some feature')
4. Push to the branch (git push origin my-new-feature)
5. Create a new Pull Request
## Contributors
- [RX14](https://github.com/RX14) - creator, maintainer