Skip to content

Commit

Permalink
fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitax44 authored Jun 16, 2021
1 parent 598a71c commit cf2cc40
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
37 changes: 37 additions & 0 deletions copy.json
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 12 additions & 0 deletions copy.turingmachine
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
init: copy
accept: done

copy,0,_
copy,0,0,>,>

copy,1,_
copy,1,1,>,>

copy,_,_
done,_,_,-,<

20 changes: 15 additions & 5 deletions turing.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,32 @@ 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
todo=rulea
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):
Expand Down Expand Up @@ -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)
Expand All @@ -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)

0 comments on commit cf2cc40

Please sign in to comment.