diff --git a/README.md b/README.md index 560e7a2..c033b46 100644 --- a/README.md +++ b/README.md @@ -39,3 +39,30 @@ A card that will move to the left if it is zero and the right if it is one and t 0 0 ``` + +## Compiler/decompiler +To make it easier to code there is a compiler and a decompiler awailable, if you have cards that you want to convert to a .tm-file you can use the decompiler: +``` +./decompiler.py adder.tm cards/ +``` + +If you have a .tm-file that you want to convert to cards, you can similarly use the compiler: +``` +./compiler.py adder.tm adder +``` +This will give you a stack of cards for the adder.tm program in the adder director, which will be created for you + +The .tm file can have comments starting with a #-sign and empty lines with only whitespace in them + +Otherwise the format of a .tm-file is this: +``` +cardnumber:write_zero,move_zero,next_zero,write_one,move_one,next_one +``` + +For instance, the one card busy beaver would look like this: +``` +# This is a comment, blank lines ar ok too, like the next line + +1:1,1,0,1,0,0 +```` + diff --git a/adder.tm b/adder.tm index 8d7a68c..582a7a1 100644 --- a/adder.tm +++ b/adder.tm @@ -1,22 +1,37 @@ +# Start program writes 0 at pos 0 and moves left, and jumps to 11, everything from here to 15 inputs an 8 in the negative registry 1:0,1,11,0,0,0 11:0,1,12,0,0,0 12:0,1,13,0,0,0 13:0,1,14,0,0,0 14:1,0,15,0,0,0 + +# At card 15 we flip a bit at registry position -4 to indicate an 8 in the negative registry, then we jump to 140 15:0,0,140,0,0,0 + +# At card 21 write a one and move to 22 21:1,0,22,0,0,0 + +# At card 22 write a one and jump to 210 where we start to move back to registry pos 0 22:1,1,210,1,1,210 + +# At 30 - 38 we keep a decrementer 30:1,1,31,0,0,0 -31:1,1,32,0,0,0, -32:1,1,33,0,0,0, -33:1,1,34,0,0,0, -34:1,1,35,0,0,0, -35:1,1,36,0,0,0, -36:1,1,37,0,0,0, +31:1,1,32,0,0,0 +32:1,1,33,0,0,0 +33:1,1,34,0,0,0 +34:1,1,35,0,0,0 +35:1,1,36,0,0,0 +36:1,1,37,0,0,0 37:1,1,38,0,0,0 38:1,1,0,0,0,0 + +# 140 - 142 just moves us back to registry pos 0 140:0,0,141,1,0,141 141:0,0,142,1,0,142 + +# At 142 we jump to 21 142:0,0,21,0,0,21 + +# Here we move back to registry pos 0 and then jump to 30 210:0,1,211,1,1,211 211:0,1,30,1,1,30 diff --git a/compiler.py b/compiler.py index 47795bd..7532989 100755 --- a/compiler.py +++ b/compiler.py @@ -2,30 +2,33 @@ import sys import os +import re filename = sys.argv[1] dirname = sys.argv[2] os.mkdir(dirname) -# print("Card number: " + cardnumber + ", write zero: " + write_zero + ", move zero: " + move_zero + ", next zero: " + next_zero + ", write one: " + write_one + ", move one: " + move_one + ", next one: " + next_one ) +regex = re.compile('^[#\s]') + with open(filename, 'r') as ip: for line in ip: - arr1 = line.split(':') - arr2 = arr1[1].split(',') - cardnumber = arr1[0] - write_zero = arr2[0] - move_zero = arr2[1] - next_zero = arr2[2] - write_one = arr2[3] - move_one = arr2[4] - next_one = arr2[5] + if not regex.match(line): + arr1 = line.split(':') + arr2 = arr1[1].split(',') + cardnumber = arr1[0] + write_zero = arr2[0] + move_zero = arr2[1] + next_zero = arr2[2] + write_one = arr2[3] + move_one = arr2[4] + next_one = arr2[5] - with open(dirname + "/" + cardnumber, 'a') as op: - op.write(write_zero + '\n') - op.write(move_zero + '\n') - op.write(next_zero + '\n') - op.write(write_one + '\n') - op.write(move_one + '\n') - op.write(next_one + '\n') + with open(dirname + "/" + cardnumber, 'a') as op: + op.write(write_zero + '\n') + op.write(move_zero + '\n') + op.write(next_zero + '\n') + op.write(write_one + '\n') + op.write(move_one + '\n') + op.write(next_one)