More Arithmetic Operators
Interpreters, Iteration 11
More User Stories About Operators
User Story #11: Interpreter evaluates a two-integer subtraction.
A Hobbes program can be the subtraction of one integer from another.
Example: 2 - 3
evaluates to -1
.
User Story #12: Interpreter evaluates a two-integer division.
A Hobbes program can be the division of one integer by another, resulting in a truncated integer.
Example: 11 / 3
evaluates to 3
.
Slight Walkthrough
Start with acceptance tests
For both stories, write three CIAT tests. Run the CIAT tests for a red bar.
You should use the errors you get back to see what you should
work on next. If it complains about not recognizing certain
characters (like -
and /
), then you
probably need a new Hobbes front end. If the Hobbes interpreter
complains that it doesn't know what to do with a subtraction or
division, you need to fix your Hobbes interpreter.
Whenever your unit tests are green, run your acceptance tests to see what to do next. Whatever gives you your first error is the next thing to work on. If the acceptance tests are also green, then you're ready for a new story.
Subtraction, just like addition, only simpler
You only have to worry about integer subtraction. However, you should still go through PolyD. There are at least three reasons for this:
myOperators
inHobbesInterpreter
wantsOperatorAlgorithms
.- PolyD will handle the casting to
IntegerETIR
and the error case (twoExpressionTIR
s) cleanly. - It gives you some future flexibility.
Create SubtractionAlgorithm
and
SubtractionAlgorithmTest
. Fill in their contents much
like you did for AdditionAlgorithm
and
AdditionAlgorithmTest
. Unit-test green bar.
Are you further along with the acceptance tests?
Run the CIAT test for the same red bar.
Check the error messages for the subtraction tests. The interpreter doesn't know how to do the subtraction yet!
Add another assertion to
shouldInterpretOperatorExpressions()
for a
subtraction. Red bar. Fix the interpreter. Unit-test green bar.
Only division complaints with CIAT tests.
Division, same as subtraction
Repeat all of that for the division operator.