173 lines
4.0 KiB
C
173 lines
4.0 KiB
C
|
#include <stdio.h>
|
||
|
#include <string.h>
|
||
|
#include <stdlib.h>
|
||
|
|
||
|
enum action {TUK, INK, DEK, BREK, GLEK, PUK, KUK, op, cl, FUK};
|
||
|
|
||
|
|
||
|
void run_tukafuk(const char* code);
|
||
|
int convert_code(enum action* commands, const char* code);
|
||
|
void run_code(enum action* commands, int size);
|
||
|
|
||
|
int main(int argc, char* argv[]){
|
||
|
|
||
|
if(argc < 2){
|
||
|
printf("Please provide file with source code\n");
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
char commands[999999];
|
||
|
|
||
|
FILE* file = fopen(argv[1], "r");
|
||
|
fread(commands, 1, 999999, file);
|
||
|
|
||
|
run_tukafuk(commands);
|
||
|
}
|
||
|
|
||
|
void run_tukafuk(const char* code){
|
||
|
enum action *commands = calloc(999999, sizeof(enum action));
|
||
|
int pos = 0;
|
||
|
|
||
|
int size = convert_code(commands, code);
|
||
|
|
||
|
run_code(commands, size);
|
||
|
}
|
||
|
|
||
|
void run_code(enum action* commands, int size){
|
||
|
char memory[1000] = {0};
|
||
|
int pos = 0;
|
||
|
|
||
|
int ended = 0;
|
||
|
|
||
|
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){
|
||
|
pos++;
|
||
|
}
|
||
|
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);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if(!ended){
|
||
|
printf("No FUK at the end :(\n");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int convert_code(enum action* commands, const char* code){
|
||
|
int pos = 0;
|
||
|
int offset = 0;
|
||
|
int add_offset = 0;
|
||
|
char buff[100];
|
||
|
|
||
|
int size = strlen(code);
|
||
|
|
||
|
while(1){
|
||
|
int res = sscanf(code + offset ,"%s%n", buff, &add_offset);
|
||
|
if(res == 0 || strcmp(buff, "TUK") == 0){
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
while (1){
|
||
|
if(offset >= size){
|
||
|
break;
|
||
|
}
|
||
|
int res = sscanf(code + offset ,"%s%n", buff, &add_offset);
|
||
|
|
||
|
if(res == 0){
|
||
|
break;
|
||
|
}
|
||
|
offset += add_offset;
|
||
|
|
||
|
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++;
|
||
|
}
|
||
|
return pos;
|
||
|
}
|