An accumulator is a very simple computer. It contains a single register and a program.
A program is a list of instructions.
When the accumulator runs its program, each instruction is executed.
Executing an instruction updates the register.
There are two types of instructions: Add(arg) (adds arg to register) and Mul(arg) (multiplies register by arg).
Here are a few sample runs:
// computing ((3 * 5) + 1) * 2
Accumulator.program = List(Add(3), Mul(5), Add(1),
Mul(2))
Accumulator.run()
Accumulator.register
//> res6: Int = 32
// computing (((10 * 2) + 3) * 5)
Accumulator.register = 0
Accumulator.program = List(Add(10),
Mul(2), Add(3),
Mul(5))
Accumulator.run()
Accumulator.register
//> res7: Int = 115
Create a worksheet called accumulator.
Implement Accumulator and any other classes, objects, or traits you may need.
Test your implementation with the samples above.
Add a Halt instruction to the accumulator in instruction set. When executed, this instruction sets the HALT flag. The HALT flag is a new variable. When set to true program execution immediately stops.
Add a Goto(arg) instruction to the accumulator in instruction set. When executed, this instruction sets the instruction pointer (IP) to arg. IP is a new variable that contains the index of the next instruction in the program to be executed.