The RPN calculator implementation is a good exercise for working through a languages capabilities and exploring OO strategies.
I have done four variations of the RPN calculator as exploratory into languages:
This implementation utilizes Python.
- brew
xcode-select --install
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- python
brew install python
To run the tests, run the following command:
make test
To check style and formatting
make lint
To check formatting
make check-format
To auto format
make format
-
Create a new operator implementation class. Add this class to the
src/rpn/operators
directory. Follow the conventation of(operator name)-operator.py
(example:addition-operator.py
). -
The operator implementation class needs to implement the interface
Operator
, which will have the class implement the following methods
doOperation(RpnStack)
: this method is responsible forpop
-ing the operands from the stack, execute the operation, andpush
-ing the result back to the stackhandlesOperatorCharacter(String)
: this method is responsible for checking if this class handles a particular operator character. For example, an addition operation class would returntrue
for+
.
A sample base implementation for addition is shown below:
class AdditionOperator(Operator):
def doOperation(self, numbers: RpnStack) -> int:
rhs = numbers.pop()
lhs = numbers.pop()
result = lhs + rhs
numbers.push(result)
return result
def handlesOperatorCharacter(self, operand) -> bool:
return operand == "+"
- Register operator in
OperatorRegistry
. To do this, add the following statement:
self._register(AdditionOperator())