This app uses the Vorpal REPL environment to simulate an RPN calculator. It was developed for an interview exercise.
- Clone the repo
- Run
npm install
in the app's folder
- Run
npm start
(ornode index
) in the app's folder - You'll be put into the ap, with a $ prompt
- You can type
help
to see available commands - Type
rpn
to start using the calculator - Type
q
orexit
to exit the app - Type any number to add it to the queue
- Type one of the 4 operands (+, - ,/ , *) to carry out the operations on the last 2 operands
> npm start
$ help
Commands:
help [command] Provides help for a given command.
exit [options] Exit program
rpn Start RPN functionality
$ rpn
Welcome to RPN. Enter numbers or operators. To exit, type `exit`.
$ rpn: 3
3
$ rpn: 7
7
$ rpn: +
10
$ rpn: 2
2
$ rpn: -
-8
$ rpn: 2
2
$ rpn: /
-0.25
$ rpn: q
KBye!
Error handling is rudimentary.
- Only numbers that can be parsed, operators, and end commands are checked for.
- The execution code always checks to see that there are 2 available operands on the stack before calculating: If there are none, an error is issued. If there's only one, an error is issued, and it is placed back on stack.
- It may be possible to break this REPL, or send option parameters. But this was built as quick exercise, and not intended for production use (although who uses an RPN calculator in production?).
- Support more operators - can be done by removing initial check in
handleInput
and adding a case to thecalculate
function - Support direct handling of tuples ("+ 7 2" => 9) by parsing the args string in
handleInput
- Support full RPN notation in one line ("+ 7 - 2 3" => 6)
- Support more than 2 operands per operation (if needed)
- Print out entire stack as another command
- Allow clearing stack as another command
- Allow poping a number as another command