2021-01-17 17:21:21 +00:00
|
|
|
#include <stdio.h>
|
2021-01-17 18:09:37 +00:00
|
|
|
#include <string.h>
|
2021-01-17 17:21:21 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2021-01-17 18:09:37 +00:00
|
|
|
enum action {TUK, INK, DEK, BREK, GLEK, PUK, KUK, op, cl, FUK};
|
2021-01-17 17:21:21 +00:00
|
|
|
|
|
|
|
|
2021-01-17 18:09:37 +00:00
|
|
|
void run_tukafuk(const char* code);
|
|
|
|
int convert_code(enum action* commands, const char* code);
|
|
|
|
void run_code(enum action* commands, int size);
|
2021-01-17 17:21:21 +00:00
|
|
|
|
|
|
|
int main(int argc, char* argv[]){
|
|
|
|
|
2021-01-17 18:09:37 +00:00
|
|
|
if(argc < 2){
|
|
|
|
printf("Please provide file with source code\n");
|
|
|
|
return 0;
|
2021-01-17 17:21:21 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 18:09:37 +00:00
|
|
|
char commands[999999];
|
2021-01-17 17:21:21 +00:00
|
|
|
|
2021-01-17 18:09:37 +00:00
|
|
|
FILE* file = fopen(argv[1], "r");
|
|
|
|
fread(commands, 1, 999999, file);
|
2021-01-17 17:21:21 +00:00
|
|
|
|
2021-01-17 18:09:37 +00:00
|
|
|
run_tukafuk(commands);
|
2021-01-17 17:21:21 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 18:09:37 +00:00
|
|
|
void run_tukafuk(const char* code){
|
|
|
|
enum action *commands = calloc(999999, sizeof(enum action));
|
|
|
|
int pos = 0;
|
2021-01-17 17:21:21 +00:00
|
|
|
|
2021-01-17 18:09:37 +00:00
|
|
|
int size = convert_code(commands, code);
|
2021-01-17 17:21:21 +00:00
|
|
|
|
2021-01-17 18:09:37 +00:00
|
|
|
run_code(commands, size);
|
2021-01-17 17:21:21 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 18:09:37 +00:00
|
|
|
void run_code(enum action* commands, int size){
|
|
|
|
char memory[1000] = {0};
|
|
|
|
int pos = 0;
|
2021-01-17 17:21:21 +00:00
|
|
|
|
2021-01-17 18:09:37 +00:00
|
|
|
int ended = 0;
|
2021-01-17 17:21:21 +00:00
|
|
|
|
2021-01-17 18:09:37 +00:00
|
|
|
for(int i = 0; i < size; i++){
|
|
|
|
if(commands[i] == INK){
|
|
|
|
memory[pos] += 1;
|
|
|
|
}
|
|
|
|
else if(commands[i] == DEK){
|
|
|
|
memory[pos] -= 1;
|
|
|
|
}
|
|
|
|
else if(commands[i] == BREK){
|
2021-01-17 17:21:21 +00:00
|
|
|
pos++;
|
2021-01-17 18:09:37 +00:00
|
|
|
}
|
|
|
|
else if(commands[i] == GLEK){
|
|
|
|
pos--;
|
|
|
|
}
|
|
|
|
else if(commands[i] == PUK){
|
|
|
|
putchar(memory[pos]);
|
|
|
|
}
|
|
|
|
else if(commands[i] == KUK){
|
|
|
|
memory[pos] = getchar();
|
|
|
|
}
|
|
|
|
else if(commands[i] == op && memory[pos] == 0){
|
|
|
|
int closed = 1;
|
|
|
|
while(closed > 0){
|
|
|
|
i++;
|
|
|
|
if(commands[i] == op){
|
|
|
|
closed++;
|
|
|
|
}
|
|
|
|
else if(commands[i] == cl){
|
|
|
|
closed--;
|
|
|
|
}
|
|
|
|
if(i > size){
|
|
|
|
printf("Error with opening bracket!\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2021-01-17 17:21:21 +00:00
|
|
|
}
|
2021-01-17 18:09:37 +00:00
|
|
|
}
|
|
|
|
else if(commands[i] == cl && memory[pos] != 0){
|
|
|
|
int opened = 1;
|
|
|
|
while(opened > 0){
|
|
|
|
i--;
|
|
|
|
if(commands[i] == cl){
|
|
|
|
opened++;
|
|
|
|
}
|
|
|
|
else if(commands[i] == op){
|
|
|
|
opened--;
|
|
|
|
}
|
|
|
|
if(i < 0){
|
|
|
|
printf("Error with opening bracket!\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(commands[i] == FUK){
|
|
|
|
ended = 1;
|
|
|
|
break;
|
2021-01-17 17:21:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-17 18:09:37 +00:00
|
|
|
if(!ended){
|
|
|
|
printf("No FUK at the end :(\n");
|
2021-01-17 17:21:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-17 18:09:37 +00:00
|
|
|
int convert_code(enum action* commands, const char* code){
|
|
|
|
int pos = 0;
|
|
|
|
int offset = 0;
|
|
|
|
int add_offset = 0;
|
|
|
|
char buff[100];
|
2021-01-17 17:21:21 +00:00
|
|
|
|
2021-01-17 18:09:37 +00:00
|
|
|
int size = strlen(code);
|
2021-01-17 17:21:21 +00:00
|
|
|
|
2021-01-17 18:09:37 +00:00
|
|
|
while(1){
|
|
|
|
int res = sscanf(code + offset ,"%s%n", buff, &add_offset);
|
|
|
|
if(res == 0 || strcmp(buff, "TUK") == 0){
|
|
|
|
break;
|
2021-01-17 17:21:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-17 18:09:37 +00:00
|
|
|
while (1){
|
|
|
|
if(offset >= size){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
int res = sscanf(code + offset ,"%s%n", buff, &add_offset);
|
2021-01-17 17:21:21 +00:00
|
|
|
|
2021-01-17 18:09:37 +00:00
|
|
|
if(res == 0){
|
|
|
|
break;
|
2021-01-17 17:21:21 +00:00
|
|
|
}
|
2021-01-17 18:09:37 +00:00
|
|
|
offset += add_offset;
|
2021-01-17 17:21:21 +00:00
|
|
|
|
2021-01-17 18:09:37 +00:00
|
|
|
if(strcmp(buff, "TUK") == 0){
|
|
|
|
commands[pos] = TUK;
|
|
|
|
}
|
|
|
|
else if(strcmp(buff, "INK") == 0){
|
|
|
|
commands[pos] = INK;
|
|
|
|
}
|
|
|
|
else if(strcmp(buff, "DEK") == 0){
|
|
|
|
commands[pos] = DEK;
|
|
|
|
}
|
|
|
|
else if(strcmp(buff, "BREK") == 0){
|
|
|
|
commands[pos] = BREK;
|
|
|
|
}
|
|
|
|
else if(strcmp(buff, "GLEK") == 0){
|
|
|
|
commands[pos] = GLEK;
|
|
|
|
}
|
|
|
|
else if(strcmp(buff, "PUK") == 0){
|
|
|
|
commands[pos] = PUK;
|
|
|
|
}
|
|
|
|
else if(strcmp(buff, "KUK") == 0){
|
|
|
|
commands[pos] = KUK;
|
|
|
|
}
|
|
|
|
else if(strcmp(buff, "[") == 0){
|
|
|
|
commands[pos] = op;
|
|
|
|
}
|
|
|
|
else if(strcmp(buff, "]") == 0){
|
|
|
|
commands[pos] = cl;
|
|
|
|
}
|
|
|
|
else if(strcmp(buff, "FUK") == 0){
|
|
|
|
commands[pos] = FUK;
|
|
|
|
}
|
|
|
|
else if(strcmp(buff, "[") == 0){
|
|
|
|
commands[pos] = op;
|
|
|
|
}
|
|
|
|
else if(strcmp(buff, "]") == 0){
|
|
|
|
commands[pos] = cl;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
pos--;
|
|
|
|
}
|
|
|
|
pos++;
|
2021-01-17 17:21:21 +00:00
|
|
|
}
|
2021-01-17 18:09:37 +00:00
|
|
|
return pos;
|
2021-01-17 17:21:21 +00:00
|
|
|
}
|