Unobfuscated Code
init2 * * r init2
init2 _ . l init0
init0 * * l init0
init0 _ * l init1
init1 * 0 r readop
readop _ * r readop
readop + _ l +
readop - _ l -
readop x _ l x
readop / _ l /
+ _ * l +
+ * * * inc
inc - * l dec
inc _ 1 r return
inc 0 1 l zero
inc 1 0 l inc
- _ * l -
- * * * dec
dec - * l inc
dec _ * r neg
dec 0 1 l dec
dec 1 0 l zero
x * * l x
x 1 0 l x1
x 0 0 l x0
x1 _ 1 r return
x1 1 1 l x1
x1 0 1 l x0
x0 _ _ r return
x0 1 0 l x1
x0 0 0 l x0
/ _ * l /
/ * * l //
// * * l //
// _ * r div
div 0 0 r div0
div 1 0 r div1
div0 * * l zero
div0 0 0 r div0
div0 1 0 r div1
div1 _ * l zero
div1 - * l inc
div1 0 1 r div0
div1 1 1 r div1
neg * * r neg
neg - _ r readop
neg _ - r readop
zero * * l zero
zero _ * r zero1
zero1 0 _ r zero1
zero1 1 * r return
zero1 * _ l zero2
zero2 * 0 r return
return * * r return
return _ * r readop
readop . _ l fin
fin _ * l fin
fin - _ l min
fin * * l halt
min * * l min
min _ - r halt
Explanation of the states:
Initialization:
These states are visited once at the beginning of each run, starting with init2
- init2: Move all the way to the right and put a '.'. That way the TM knows when to stop. Change to 'init0'.
- init0: Move all the back to the left until the head reads a space. Then move one cell to the left. Change to 'init1'.
- init1: Put a zero and move one cell to the right and change to 'readop'.
Reading instructions:
These states will be visited multiple times throughout the program
readop: Moves all the way to the right until it reads an operator or the '.'. If it hits an operator, change to the corresponding state (+,-,x,/). If it hits a '.', change to state 'fin'.
return: Returns the head to the empty space between the running total and the operators. Then changes to 'readop'.
Operations:
These operations do the actual dirty work
+: Move to the left until the head reads any non- whitespace character. If this character is a '-', move left and change to 'dec'. Otherwise, change to 'inc'.
-: Similar to '+', except change to 'inc' if there is a '-' and 'dec' otherwise.
inc: If the digit under the head is a 0 (or a whitespace), change it to 1 and change to 'zero'. If the digit is a 1, change it to 0, then repeat on the next digit.
dec: Similar to inc, except 1 goes to 0, 0 goes to 1, and if the head reads a whitespace, change to 'neg'.
x, x0, x1: Bitshift the number one to the left. Change to 'return'.
/, //, div, div0, div1: Move all the way to the right of the number, then bitshift one to the right. If there is a '-', change to 'inc'. This simulates rounding down negative numbers. Otherwise, change to 'zero'
neg: Place a '-' after the number then change to 'readop'
zero, zero1, zero2: Remove leading zeros and change to 'readop'
Cleanup: Makes the output presentable
- fin, min: Move the '-' in front of the number if necessary. Halt.