diff --git a/copy.json b/copy.json new file mode 100644 index 0000000..ae4536b --- /dev/null +++ b/copy.json @@ -0,0 +1,37 @@ +{ + "rules": { + "start": [ + { + "rule": "copy" + } + ], + "copy": [ + { + "state": [ + "01", + " " + ], + "move": [ + 1, + 1 + ], + "mod": [ + "$0", + "$0" + ] + }, + { + "state": [ + " ", + " " + ], + "rule": "done", + "move": [ + 0, + -1 + ] + } + ] + }, + "lines": 2 +} diff --git a/copy.turingmachine b/copy.turingmachine new file mode 100644 index 0000000..4d553f0 --- /dev/null +++ b/copy.turingmachine @@ -0,0 +1,12 @@ +init: copy +accept: done + +copy,0,_ +copy,0,0,>,> + +copy,1,_ +copy,1,1,>,> + +copy,_,_ +done,_,_,-,< + diff --git a/turing.py b/turing.py index 177a9d9..aaee119 100644 --- a/turing.py +++ b/turing.py @@ -30,6 +30,8 @@ def step(self): if a!=b: # check if this rule fits if a=='*': pass + elif b in a: + pass else: break else: # rule is ok @@ -37,17 +39,23 @@ def step(self): break else: # rule not found self.state=2 - raise RuntimeError('invalid state', rule_set, state) + raise RuntimeError('invalid state', rule_set, state, self.rule) mod=todo.get('mod', [None]*self.lines) + realmod=[] motion=todo.get('move', [0]*self.lines) for i in range(self.lines): # edit all of the lines - self.data[i].set(mod[i], motion[i]) + val=mod[i] + if val!=None and val.startswith('$'): + val=self.data[int(val[1:])].get() + realmod.append(val if val!=None else self.data[i].get()) + self.data[i].set(val) + [self.data[i].move(motion[i]) for i in range(self.lines)] self.rule=todo.get('rule', self.rule) if self.rule=='done': self.state=1 - return (self.rule, mod, motion) + return (self.rule, mod, realmod, motion) class line: def __init__(self, data=[], head=0): @@ -128,7 +136,9 @@ def load_rule(file='rules.json'): try: steps=0 while not machine.state: - machine.step() + info=machine.step() + if False: # to enable debug set to True + print(info) steps+=1 except KeyboardInterrupt: print(machine.data, machine.state, machine.rule) @@ -141,7 +151,7 @@ def load_rule(file='rules.json'): print('empty') continue print(str().join([line.data.get(i, ' ') for i in range(min(buf), max(buf)+1)])) - print('head:', steps) + print('steps:', steps) # print([list(i.data.values()) for i in machine.data]) # print(machine.data) # print(machine.data)