-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cat.c
180 lines (163 loc) · 4.01 KB
/
Cat.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
#define _CRT_SECURE_NO_WARNINGS
#define COMMAND_NOT_FOUND -1
#define TURING_MACHINE_SIZE 100
#define SYMBOL 1
#define END_LOOP_COMMAND 7
#define END_IF_COMMAND 9
#include <string.h>
#include "AlonQueues.h"
char* commands[] = { "MEOW", "meow", "Meow", "meoW", "MeoW", "mEow", "MEow", "meOW", "MeOw","mEoW"};
char turingMachine[TURING_MACHINE_SIZE];
char* memoryLocation;
char copyRegister;
void end_program(char* message) {
printf("\nError - %s\n", message);
exit(1);
}
void searchForCommand(char* buffer, int* commandNumber) {
if (!strcmp(buffer, commands[0])) {
*commandNumber = 0;
}
else if (!strcmp(buffer, commands[1])) {
*commandNumber = 1;
}
else if (!strcmp(buffer, commands[2])) {
*commandNumber = 2;
}
else if (!strcmp(buffer, commands[3])) {
*commandNumber = 3;
}
else if (!strcmp(buffer, commands[4])) {
*commandNumber = 4;
}
else if (!strcmp(buffer, commands[5])) {
*commandNumber = 5;
}
else if (!strcmp(buffer, commands[6])) {
*commandNumber = 6;
}
else if (!strcmp(buffer, commands[7])) {
*commandNumber = 7;
}
else if (!strcmp(buffer, commands[8])) {
*commandNumber = 8;
}
else if (!strcmp(buffer, commands[9])) {
*commandNumber = 9;
}
}
void executeCommands(Queue* commandQueuePtr) {
while (!isQueueEmpty(commandQueuePtr)) {
switch (removeQueue(commandQueuePtr)) {
case 0:
(*memoryLocation)++;
break;
case 1:
(*memoryLocation)--;
break;
case 2:
memoryLocation++;
break;
case 3:
memoryLocation--;
break;
case 4:
printf("%c", *memoryLocation);
break;
case 5:
if (*memoryLocation != '0') {
copyRegister = *memoryLocation;
}
else if (copyRegister != '0') {
*memoryLocation = copyRegister;
copyRegister = '0';
}
break;
case 6:
{
BOOLEAN foundLoopEndCommand = FALSE;
Queue tempQueue;
Queue oneLoopQueue;
initQueue(&tempQueue);
initQueue(&oneLoopQueue);
char timesToExecuteRegister = *memoryLocation;
while (!isQueueEmpty(commandQueuePtr) && !foundLoopEndCommand) {
int currentCommand = removeQueue(commandQueuePtr);
if (currentCommand != END_LOOP_COMMAND) {
insertQueue(&oneLoopQueue, currentCommand);
}
else {
foundLoopEndCommand = TRUE;
while (timesToExecuteRegister-- > '0')
{
copyQueue(&oneLoopQueue, &tempQueue);
executeCommands(&tempQueue);
}
}
}
if (!foundLoopEndCommand) {
end_program("Missing finish loop instruction");
}
}
break;
case 7:
end_program("Missing start loop instruction");
case 8:
{
BOOLEAN foundEndIfCommand = FALSE;
Queue tempQueue;
initQueue(&tempQueue);
while (!isQueueEmpty(commandQueuePtr) && !foundEndIfCommand) {
int currentCommand = removeQueue(commandQueuePtr);
if (currentCommand != END_IF_COMMAND) {
insertQueue(&tempQueue, currentCommand);
}
else {
foundEndIfCommand = TRUE;
if (*memoryLocation > copyRegister) {
executeCommands(&tempQueue);
}
}
}
if (!foundEndIfCommand) {
end_program("Missing finish if instruction");
}
break;
}
case 9:
end_program("Missing start if instruction");
}
}
}
int main(int argc, char* argv[]) {
Queue commandQueue;
char currentSymbol;
FILE* file;
char buffer[5];
int currentCommand;
initQueue(&commandQueue);
file = fopen(argv[1], "rb");
currentCommand = COMMAND_NOT_FOUND;
buffer[4] = '\0';
memoryLocation = turingMachine + TURING_MACHINE_SIZE / 2;
copyRegister = '0';
memset(turingMachine, '0', TURING_MACHINE_SIZE);
if (file) {
while (fread(¤tSymbol, sizeof(char), SYMBOL, file) == SYMBOL) {
buffer[3] = currentSymbol;
searchForCommand(buffer, ¤tCommand);
if (currentCommand != COMMAND_NOT_FOUND) {
insertQueue(&commandQueue, currentCommand);
memset(buffer, 0, 4);
currentCommand = COMMAND_NOT_FOUND;
}
else {
buffer[0] = buffer[1];
buffer[1] = buffer[2];
buffer[2] = buffer[3];
}
}
}
executeCommands(&commandQueue);
return 0;
}