An implementation of a register machine with three instructions: increment, decrement, and halt.
python3 machine.py input_file.reg
The first line needs to be an initial configuration. This is a tuple whose 0th element is the label of the first instruction, and subsequent elements are initial register values. For example, (0, 0, 2, 4)
means the first instruction is label 0
and the registers start with the values (r0, r1, r2) = (0, 2, 4)
.
All parsed lines after this are instructions. Instructions start with a label (which can be made up of any ASCII characters except colons and whitespace characters such as spaces and line breaks), followed by a colon (:
), and then the instruction.
The possible instructions are:
halt
: proper haltinc(rx), i
: increment registerx
and then go to instruction with labeli
dec(ry), i, j
: if registery
> 0 then decrement it and go to instructioni
, otherwise go to instructionj
.
Blank lines are ignored, as are comment lines starting with #
.
The machine outputs every operation in the comma-separated format Execution Number, Instruction Label, Register State
.
Set the SHOW_TRACE
constant in machine.py
to False
to only see the final state after the program has terminated and not every operation along the way.
See the examples
directory in this repo for examples.