Improve compiler to support comments and blank lines and update readme

master
Micke Nordin 7 years ago
parent b9454659a3
commit 36dab4ec15

@ -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
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
````

@ -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 1:0,1,11,0,0,0
11:0,1,12,0,0,0 11:0,1,12,0,0,0
12:0,1,13,0,0,0 12:0,1,13,0,0,0
13:0,1,14,0,0,0 13:0,1,14,0,0,0
14:1,0,15,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 15:0,0,140,0,0,0
# At card 21 write a one and move to 22
21:1,0,22,0,0,0 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 22:1,1,210,1,1,210
# At 30 - 38 we keep a decrementer
30:1,1,31,0,0,0 30:1,1,31,0,0,0
31:1,1,32,0,0,0, 31:1,1,32,0,0,0
32:1,1,33,0,0,0, 32:1,1,33,0,0,0
33:1,1,34,0,0,0, 33:1,1,34,0,0,0
34:1,1,35,0,0,0, 34:1,1,35,0,0,0
35:1,1,36,0,0,0, 35:1,1,36,0,0,0
36:1,1,37,0,0,0, 36:1,1,37,0,0,0
37:1,1,38,0,0,0 37:1,1,38,0,0,0
38:1,1,0,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 140:0,0,141,1,0,141
141:0,0,142,1,0,142 141:0,0,142,1,0,142
# At 142 we jump to 21
142:0,0,21,0,0,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 210:0,1,211,1,1,211
211:0,1,30,1,1,30 211:0,1,30,1,1,30

@ -2,14 +2,17 @@
import sys import sys
import os import os
import re
filename = sys.argv[1] filename = sys.argv[1]
dirname = sys.argv[2] dirname = sys.argv[2]
os.mkdir(dirname) 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: with open(filename, 'r') as ip:
for line in ip: for line in ip:
if not regex.match(line):
arr1 = line.split(':') arr1 = line.split(':')
arr2 = arr1[1].split(',') arr2 = arr1[1].split(',')
cardnumber = arr1[0] cardnumber = arr1[0]
@ -27,5 +30,5 @@ with open(filename, 'r') as ip:
op.write(next_zero + '\n') op.write(next_zero + '\n')
op.write(write_one + '\n') op.write(write_one + '\n')
op.write(move_one + '\n') op.write(move_one + '\n')
op.write(next_one + '\n') op.write(next_one)

Loading…
Cancel
Save