-
Notifications
You must be signed in to change notification settings - Fork 0
/
tm.c
188 lines (151 loc) · 4.86 KB
/
tm.c
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
#include "tm.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
TM_Def tm_def_create_random(size_t numStates, size_t numSymbols) {
size_t numTransitions = numStates * numSymbols;
TM_Transition *delta = malloc(sizeof(TM_Transition) * numTransitions);
TM_State state;
TM_Symbol symbol;
for (state = 0; state < numStates; state++) {
for (symbol = 0; symbol < numSymbols; symbol++) {
TM_Transition *trans = &delta[state * numSymbols + symbol];
trans->direction = rand() % 4;
trans->newState = rand() % numStates;
trans->newSymbol = rand() % (numSymbols - 1) + 1;
}
}
TM_Def def = {
numStates,
numSymbols,
delta
};
return def;
}
void tm_def_free(TM_Def *def) {
free(def->delta);
def->delta = NULL;
}
void tm_def_print(const TM_Def *def) {
TM_State state;
TM_Symbol symbol;
for (state = 0; state < def->numStates; state++) {
printf("%d: ", state);
for (symbol = 0; symbol < def->numSymbols; symbol++) {
TM_Transition *trans = &def->delta[state * def->numSymbols + symbol];
printf("%d -> (%d, %d, %d), ", symbol, trans->newState,
trans->newSymbol, trans->direction);
}
printf("\n");
}
}
int tm_def_save(const TM_Def *def, const char *filename) {
FILE *file = fopen(filename, "w+");
if (!file)
return 0;
fprintf(file, "%d,%d\n", def->numStates, def->numSymbols);
TM_State state;
TM_Symbol symbol;
for (state = 0; state < def->numStates; state++) {
for (symbol = 0; symbol < def->numSymbols; symbol++) {
TM_Transition *trans = &def->delta[state * def->numSymbols + symbol];
fprintf(file, "%d,%d,%d\n", trans->newState, trans->newSymbol,
trans->direction);
}
}
fclose(file);
return 1;
}
int tm_def_load(const char *filename, TM_Def *def) {
FILE *file = fopen(filename, "r");
if (!file)
return 0;
size_t numStates, numSymbols;
fscanf(file, "%d,%d\n", &numStates, &numSymbols);
size_t numTransitions = numStates * numSymbols;
TM_Transition *delta = malloc(sizeof(TM_Transition) * numTransitions);
TM_State state;
TM_Symbol symbol;
for (state = 0; state < numStates; state++) {
for (symbol = 0; symbol < numSymbols; symbol++) {
TM_Transition *trans = &delta[state * numSymbols + symbol];
fscanf(file, "%d,%d,%d\n", &trans->newState, &trans->newSymbol,
&trans->direction);
}
}
def->numStates = numStates;
def->numSymbols = numSymbols;
def->delta = delta;
if (ferror(file)) {
fclose(file);
return 0;
}
fclose(file);
return 1;
}
TM_Config tm_config_create(const TM_Def *def,
size_t tapeWidth, size_t tapeHeight) {
TM_Symbol *tape = malloc(sizeof(size_t) * tapeWidth * tapeHeight);
memset(tape, 0, sizeof(size_t) * tapeWidth * tapeHeight);
TM_Config config = {
def,
tapeWidth,
tapeHeight,
tape,
tapeWidth / 2,
tapeHeight / 2,
0
};
return config;
}
void tm_config_free(TM_Config *tm) {
free(tm->tape);
tm->tape = NULL;
}
void tm_config_step(TM_Config *config, int nSteps) {
const TM_Def *def = config->def;
TM_State curState = config->state;
size_t tapePosX = config->tapePosX,
tapePosY = config->tapePosY;
while (nSteps-- > 0) {
size_t tapeIndex = tapePosY * config->tapeWidth + tapePosX;
TM_Symbol curSymbol = config->tape[tapeIndex];
size_t transIndex = curState * def->numSymbols + curSymbol;
TM_Transition *transition = &def->delta[transIndex];
curState = transition->newState;
config->tape[tapeIndex] = transition->newSymbol;
switch (transition->direction) {
case TM_DIR_LEFT:
if (tapePosX > 0)
tapePosX--;
else
tapePosX = config->tapeWidth - 1;
break;
case TM_DIR_UP:
if (tapePosY > 0)
tapePosY--;
else
tapePosY = config->tapeHeight - 1;
break;
case TM_DIR_RIGHT:
if (tapePosX < config->tapeWidth - 1)
tapePosX++;
else
tapePosX = 0;
break;
case TM_DIR_DOWN:
if (tapePosY < config->tapeHeight - 1)
tapePosY++;
else
tapePosY = 0;
break;
}
}
config->state = curState;
config->tapePosX = tapePosX;
config->tapePosY = tapePosY;
}
void tm_config_print(const TM_Config *config) {
printf("[%d|%d] (%d|%d) @%d\n", config->tapeWidth, config->tapeHeight,
config->tapePosX, config->tapePosY, config->state);
}