The Scheme-Syntax Calculator (or simply Calculator) is an expression language for the arithmetic operations of addition, subtraction, multiplication, and division. Calculator shares Scheme's call expression syntax and operator behavior. Addition (+) and multiplication (*) operations each take an arbitrary number of arguments:
> (+ 1 2 3 4) 10 > (+) 0 > (* 1 2 3 4) 24 > (*) 1
Subtraction (-) has two behaviors. With one argument, it negates the argument. With at least two arguments, it subtracts all but the first from the first. Division (/) has a similar pair of two behaviors: compute the multiplicative inverse of a single argument or divide all but the first into the first:
> (- 10 1 2 3) 4 > (- 3) -3 > (/ 15 12) 1.25 > (/ 30 5 2) 3 > (/ 10) 0.1
A call expression is evaluated by evaluating its operand sub-expressions, then applying the operator to the resulting arguments:
> (- 100 (* 7 (+ 8 (/ -12 -3)))) 16.0
We will implement an interpreter for the Calculator language in Python. That is, we will write a Python program that takes string lines as input and returns the result of evaluating those lines as a Calculator expression. Our interpreter will raise an appropriate exception if the calculator expression is not well formed.