forked from adam-mcdaniel/program-evolution
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathturing.py
More file actions
200 lines (155 loc) · 4.94 KB
/
turing.py
File metadata and controls
200 lines (155 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
class Tape:
def __init__(self, length=1000, blank_symbol=0, head_position=0):
self.tape = [blank_symbol] * length
self.head_position = head_position
self.register = self.blank_symbol = blank_symbol
def __getitem__(self, index):
# Check if index is out of bounds
if index >= len(self.tape):
# If so, fill with blank symbols
self.tape += [self.blank_symbol] * (index - len(self.tape) + 1)
elif index < 0:
return self.blank_symbol
return self.tape[index]
def __setitem__(self, index, value):
# Check if index is out of bounds
if index >= len(self.tape):
# If so, fill with blank symbols
self.tape += [self.blank_symbol] * (index - len(self.tape) + 1)
elif index < 0:
return
self.tape[index] = value
def get(self):
return self[self.head_position]
def set(self, value):
self[self.head_position] = value
def move_head(self, direction):
if type(direction) == str:
direction = direction.upper()
if direction == 'L':
self.head_position -= 1
elif direction == 'R':
self.head_position += 1
elif type(direction) == int:
self.head_position += direction
else:
raise TypeError("Direction must be a string ('l' or 'r') or signed integer (number of steps)")
def set_head_position(self, position):
self.head_position = position
def get_head_position(self):
return self.head_position
def __str__(self):
return str(self.tape)
def __repr__(self):
return str(self.tape)
def __len__(self):
return len(self.tape)
class TuringMachine:
def __init__(self, operations):
self.operations = operations
def run(self, tape):
for operation in self.operations:
operation.apply(tape)
class Operation:
def apply(self, tape):
pass
def __str__(self):
return self.__class__.__name__
def __repr__(self):
return str(self)
class SetTape(Operation):
def __init__(self, value):
self.value = value
def apply(self, tape):
tape.set(self.value)
def __str__(self):
return super().__str__() + f"({self.value})"
class SetRegister(Operation):
def __init__(self, value):
self.value = value
def apply(self, tape):
tape.register = self.value
def __str__(self):
return super().__str__() + f"({self.value})"
class Restore(Operation):
def apply(self, tape):
tape.register = tape.get()
class Move(Operation):
def __init__(self, direction):
self.direction = direction
def apply(self, tape):
tape.move_head(self.direction)
class Arithmetic(Operation):
def __init__(self, operation):
self.operation = operation
def apply(self, tape):
self.operation(tape.register, tape.get())
def __str__(self):
return super().__str__() + f"({self.operation.__name__})"
class Loop(Operation):
def __init__(self, operations):
self.operations = operations
def apply(self, tape):
while tape.register != 0:
for operation in self.operations:
operation.apply(tape)
class If(Operation):
def __init__(self, operations):
self.operations = operations
def apply(self, tape):
if tape.register != 0:
for operation in self.operations:
operation.apply(tape)
class Dereference(Operation):
def __init__(self, operations):
self.operations = operations
def apply(self, tape):
old_head = tape.get_head_position()
new_head = tape.get()
tape.move_head(new_head - old_head)
for operation in self.operations:
operation.apply(tape)
tape.set_head_position(old_head)
class Print(Operation):
def apply(self, tape):
print(tape.register)
class Input(Operation):
def apply(self, tape):
tape.register = int(input("Input: "))
class DecrementRegister(Operation):
def apply(self, tape):
tape.register = tape.register - 1
class IncrementRegister(Operation):
def apply(self, tape):
tape.register = tape.register + 1
class DecrementTape(Operation):
def apply(self, tape):
tape.set(tape.get() - 1)
class IncrementTape(Operation):
def apply(self, tape):
tape.set(tape.get() + 1)
class Save(Operation):
def apply(self, tape):
tape.set(tape.register)
tm = TuringMachine([
SetTape(1),
Move('R'),
SetRegister(2),
Save(),
Move('R'),
SetTape(1),
SetRegister(10),
SetTape(100),
Loop([
Dereference([
Save()
]),
DecrementRegister(),
IncrementTape(),
]),
SetTape(0),
])
tape = Tape()
print(tape, tm.operations)
tm.run(tape)
print(tape, tm.operations)