Anthony Del Ciotto's Website

An Interactive Calculator Written In Ruby

2019-10-12

I often use the spotlight calculator on my Mac for quick calculations. I recently wondered how interactive calculators actually work, so I decided to build one in Ruby. It functions as an interactive Read-Eval-Print loop (REPL) that reads in expressions and prints the result.

You can view the source code here.

The calculator evaluating some expressions

The calculator evaluating some expressions

The calculator handling some bad input

The calculator handling some bad input

Features

The program provides a nice calculator REPL which supports the following features:

Implementation

The program takes input as a string and does the following:

  1. Scans the input expressed in infix notation character by character and attempts to tokenize numbers, identifiers (words that can be constants, or functions), arithmetic operators, and other valid characters such as paranthesis and commas.
  2. Iterates through the list of tokens and uses Edsger Dijkstras Shunting Yard algorithm to parse the infix expression into a postfix form.
  3. Iterates over elements in the postfix form and uses a stack based evaluation algorithm to produce the result.

I decided to use this algorithm and implement all these steps manually to both improve my understanding, and investigate whether this algorithm could support a more interactive calculator. In addition to the definition on Wikipedia, I hacked in some extra features which I mentioned in the section above. However, there is a TODO list with a bunch of things that would improve the program. You know the drill, more and better unit tests, some of the code can be simplified, implement some more math functions and other features, etc... It would be cool to finish the list, but, who knows if I'll ever get around to it. Time to move onto the next thing!

References