Copy some valid Turing machine (from this site) into your clipboard and run the script. After that your clipboard will have the golly rule. You can paste it into some text editor, or directly into golly (during script execution pasting the script is not allowed).

It could cause an error stating that state is out of range, it's because the script trying to setup an initial state into your currently loaded rule, if number of states of your current rule is smaller golly will alert. Don't worry just paste the rule (which is in your clipboard), and copy the Turing rule again, run the script again - now everything will work fine.

Code: Select all

```
import golly as g
import string
class GollyParser:
def __init__(self):
self.rules = ""
self.rule = ""
self.initState = ""
self.allStates = ["0"]
def ParseClipboard(self):
str = g.getclipstr()
lines = str.splitlines()
rulesWithStar = ""
rulesWithStars = ""
for line in lines:
str = self.TranslateToRules(line)
if str.startswith("@RULE"):
self.rule = str
continue
if str.startswith("init_"):
self.initState = str.strip("init_")
continue
if "* *" in str:
rulesWithStars += str
continue
if "*" in str:
rulesWithStar += str
continue
self.rules += str
self.rules += rulesWithStar
self.rules += rulesWithStars
def AppendNewStates(self, state):
if state == "" or state == "anyS" or state == "0":
return
if not state in self.allStates:
self.allStates.append(state)
def TranslateToRules(self, str):
str = str.strip()
if str.startswith(';'):
if str.startswith(';RULE'):
return "@" + str.strip(";")
if len(str.split("$INITIAL_TAPE:")) == 2:
return "init_" + str.split("$INITIAL_TAPE:")[1].strip()
else:
return ""
str = str.split(';')[0]
str = str.strip()
vals = str.split(None)
if len(vals) != 5:
return ""
Hx = "H" + vals[0]
Sx = "A" + vals[1]
Sy = "A" + vals[2]
lr = vals[3]
Hy = "H" + vals[4]
if lr != "l" and lr != "r" and lr != "x" and lr != "*":
return ""
if Sx == "A*":
Sx = "anyS"
if Sy == "A*":
Sy = "anyS"
if Hy == "Hhalt":
Hy = "0"
if Sx == "A_":
Sx = "0"
if Sy == "A_":
Sy = "0"
self.AppendNewStates(Sx)
self.AppendNewStates(Sy)
self.AppendNewStates(Hx)
self.AppendNewStates(Hy)
result = ""
result += "\n# {0} \n".format(str)
if lr == "r":
result += "0,0,0,0,any1,any2,{0},{1},0,{2}\n".format(Sx,Hx,Hy)
result += "0,0,0,{0},{1},any1,any2,0,0,{2}\n".format(Hx,Sx,0)
result += "{0},0,0,0,any1,{1},any2,0,0,{2}\n".format(Hx,Sx,0)
if lr == "l":
result += "0,0,0,{0},{1},any1,any2,0,0,{2}\n".format(Hx,Sx,Hy)
result += "0,0,0,0,any1,any2,{0},{1},0,{2}\n".format(Sx,Hx,0)
result += "{0},0,0,0,any1,{1},any2,0,0,{2}\n".format(Hx,Sx,0)
if lr == "x" or lr == "*":
result += "{0},0,0,0,any1,{1},any2,0,0,{2}\n".format(Hx,Sx,Hy)
result += "0,0,0,{0},{1},any1,any2,0,0,{2}\n".format(Hx,Sx,0)
result += "0,0,0,0,any1,any2,{0},{1},0,{2}\n".format(Sx,Hx,0)
result += "{0},{1},0,any1,0,0,0,any2,0,{2}\n".format(Sx,Hx,Sy)
return result
def Rule(self):
if self.rule == "":
self.rule = "@RULE SomeTuringMachine"
self.allStates.remove("0")
self.allStates.sort()
self.allStates.insert(0, "0")
result = ""
result += self.rule + "\n"
result += "@TABLE\n"
result += "n_states:{0}\n".format(len(self.allStates))
result += "neighborhood:Moore\n"
result += "symmetries:none\n"
any = "0,"
anyS = "0,"
anyH = ""
for i in xrange(1, len(self.allStates)):
result += "var " + self.allStates[i] + " = {" + str(i) + "}\n"
any += str(i) + ","
if self.allStates[i].startswith("A"):
anyS += str(i) + ","
if self.allStates[i].startswith("H"):
anyH += str(i) + ","
any = any.strip(",")
anyS = anyS.strip(",")
anyH = anyH.strip(",")
result += "var any1 = {" + any + "}\n"
result += "var any2 = {" + any + "}\n"
result += "var anyS = {" + anyS + "}\n"
result += "var anyH1 = {" + anyH + "}\n"
result += "var anyH2 = {" + anyH + "}\n"
result += self.rules
result += "\n#Not valid Turing Head will die\n"
result += "anyH1, 0,0,0,any1, any2, anyS, anyH2, 0, 0, 0\n"
result += "anyH1, 0,0,anyH2,any1, any2, anyS, 0, 0, 0, 0\n"
return result
def SetGollyRule(self):
self.ParseClipboard()
rule = self.Rule()
g.setclipstr(rule)
istates = list(self.initState)
i = 0
for istate in istates:
if istate != " ":
idx = self.allStates.index("A" + istate)
g.setcell(i, 0, idx)
i += 1
idx = self.allStates.index("H0")
g.setcell(0, -1, idx)
parser = GollyParser()
parser.SetGollyRule()
```

Turing:

Code: Select all

```
;RULE BinaryAdder
0 * * r 0
0 _ _ l A
A _ 1 r 0
A 0 1 r 0
A 1 0 l A
;$INITIAL_TAPE: 111
```

Code: Select all

```
@RULE BinaryAdder
@TABLE
n_states:5
neighborhood:Moore
symmetries:none
var A0 = {1}
var A1 = {2}
var H0 = {3}
var HA = {4}
var any1 = {0,1,2,3,4}
var any2 = {0,1,2,3,4}
var anyS = {0,1,2}
var anyH1 = {3,4}
var anyH2 = {3,4}
# 0 _ _ l A
0,0,0,H0,0,any1,any2,0,0,HA
0,0,0,0,any1,any2,0,H0,0,0
H0,0,0,0,any1,0,any2,0,0,0
0,H0,0,any1,0,0,0,any2,0,0
# A _ 1 r 0
0,0,0,0,any1,any2,0,HA,0,H0
0,0,0,HA,0,any1,any2,0,0,0
HA,0,0,0,any1,0,any2,0,0,0
0,HA,0,any1,0,0,0,any2,0,A1
# A 0 1 r 0
0,0,0,0,any1,any2,A0,HA,0,H0
0,0,0,HA,A0,any1,any2,0,0,0
HA,0,0,0,any1,A0,any2,0,0,0
A0,HA,0,any1,0,0,0,any2,0,A1
# A 1 0 l A
0,0,0,HA,A1,any1,any2,0,0,HA
0,0,0,0,any1,any2,A1,HA,0,0
HA,0,0,0,any1,A1,any2,0,0,0
A1,HA,0,any1,0,0,0,any2,0,A0
# 0 * * r 0
0,0,0,0,any1,any2,anyS,H0,0,H0
0,0,0,H0,anyS,any1,any2,0,0,0
H0,0,0,0,any1,anyS,any2,0,0,0
anyS,H0,0,any1,0,0,0,any2,0,anyS
#Not valid Turing Head will die
anyH1, 0,0,0,any1, any2, anyS, anyH2, 0, 0, 0
anyH1, 0,0,anyH2,any1, any2, anyS, 0, 0, 0, 0
```

Code: Select all

```
x = 3, y = 2, rule = BinaryAdder
C$3B!
```

So I'm currently stuck to just copy the rule into clipboard which is ugly but works...