usaa20/sk4/program.c

239 lines
6.7 KiB
C
Raw Permalink Normal View History

2021-01-05 11:05:26 +00:00
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
2021-01-08 23:44:49 +00:00
#include <stdbool.h>
2021-01-05 11:05:26 +00:00
2021-01-08 19:03:11 +00:00
typedef enum {
2021-01-09 20:03:10 +00:00
INK, DEK,//Enum je pouzity ,
BREK, GLEK,//polia 2 rozmerne atd
2021-01-08 23:44:49 +00:00
PUK, KUK,
LP, RP,
TUK, FUK
}inst;
2021-01-05 11:05:26 +00:00
2021-01-08 23:44:49 +00:00
char dtape[30000] = {0};//hlava dh
inst ptape[30000] = {0};//hlava ph
2021-01-09 19:33:08 +00:00
size_t validRP[15000]={0};
inst* loops[2][15000] = {{0},{0}};//adresy zatvoriek
2021-01-09 20:03:10 +00:00
size_t loopscount=0;//v globalnych premennych su ulozene udaje z pasky
char* dhead = dtape;//smerniky
2021-01-08 23:44:49 +00:00
inst* phead = ptape;
2021-01-08 19:03:11 +00:00
2021-01-09 20:03:10 +00:00
2021-01-09 19:33:08 +00:00
void run_tukafuk(const char* code) {
2021-01-05 11:05:26 +00:00
char current_char;
2021-01-08 23:44:49 +00:00
bool running = false;
2021-01-05 11:05:26 +00:00
size_t i;
2021-01-09 19:33:08 +00:00
for (i = 0; code[i] != 0; i++){
2021-01-08 23:44:49 +00:00
current_char = code[i];
size_t j;
size_t loop;
switch(current_char){
case 'T':
if(code[i+1]=='U')
if(code[i+2]=='K'){
*phead = TUK;
phead++;
i+=2;
}
break;
case 'I':
if(code[i+1]=='N')
if(code[i+2]=='K'){
*phead = INK;
phead++;
i+=2;
}
break;
case 'D':
if(code[i+1]=='E')
if(code[i+2]=='K'){
*phead = DEK;
phead++;
i+=2;
}
break;
case 'B':
if(code[i+1]=='R')
if(code[i+2]=='E')
if(code[i+3] =='K'){
*phead = BREK;
phead++;
i+=3;
}
break;
case 'G':
if(code[i+1]=='L')
if(code[i+2]=='E')
if(code[i+3] =='K'){
*phead = GLEK;
phead++;
i+=3;
}
break;
case 'P':
if(code[i+1]=='U')
if(code[i+2]=='K'){
*phead = PUK;
phead++;
i+=2;
}
break;
case 'K':
if(code[i+1]=='U')
if(code[i+2]=='K'){
*phead = KUK;
phead++;
i+=2;
}
break;
case 'F':
if(code[i+1]=='U')
if(code[i+2]=='K'){
*phead = FUK;
phead++;
i+=2;
}
break;
case ']':
for(size_t k=0;k<loopscount;k++){
2021-01-09 19:33:08 +00:00
if(validRP[k]==i){
2021-01-08 23:44:49 +00:00
*phead = RP;
2021-01-09 19:33:08 +00:00
loops[1][k]=phead;
2021-01-08 23:44:49 +00:00
phead++;
}
}
break;
case '[':
j = i;
loop = 1;
while (loop > 0){//aby J nepreslo von.
current_char = code[++j];//
if (current_char==0){
break;
}
if (current_char == ']'){
loop--;
} else if (current_char == '['){
loop++;
}
2021-01-05 11:05:26 +00:00
}
2021-01-08 23:44:49 +00:00
if(loop==0){
*phead = LP;
2021-01-09 19:33:08 +00:00
loops[0][loopscount] = phead;
validRP[loopscount++] = j;
2021-01-08 23:44:49 +00:00
phead++;
}
break;
}
2021-01-08 17:37:10 +00:00
}
2021-01-09 19:33:08 +00:00
phead = ptape;
for (i = 0; i<30000; i++){
if(running){
switch(*phead){
case INK:
2021-01-09 20:03:10 +00:00
//printf("Ink..%c",*dhead);
2021-01-09 19:33:08 +00:00
(*dhead)++;
2021-01-09 20:03:10 +00:00
//printf("->%c\n",*dhead);
2021-01-09 19:33:08 +00:00
break;
case DEK:
2021-01-09 20:03:10 +00:00
//printf("Dek..%c",*dhead);
2021-01-09 19:33:08 +00:00
(*dhead)--;
//printf("->%c\n",*dhead);
break;
case BREK:
dhead++;
break;
case GLEK:
dhead--;
break;
case PUK:
//printf("Puk\n");
putchar(*dhead);
//printf("\nPukEnd\n");
break;
case KUK:
2021-01-09 20:03:10 +00:00
printf("Zadaj vstup:\n");
2021-01-09 19:33:08 +00:00
*dhead=getchar();
2021-01-09 20:03:10 +00:00
while(getchar()!='\n'){}
2021-01-09 19:33:08 +00:00
//printf("Kukol %c\n",*dhead);
break;
case FUK:
running=false;
break;
case LP:
if(*dhead==0){
for(int j=0;j<loopscount;j++){
if(loops[0][j]==phead){
phead=loops[1][j];
break;
}
}
}
break;
case RP:
if(*dhead!=0){
for(int j=0;j<loopscount;j++){
if(loops[1][j]==phead){
phead=loops[0][j];
break;
}
}
}
2021-01-05 11:05:26 +00:00
}
2021-01-09 19:33:08 +00:00
if(!running)
break;
2021-01-05 11:05:26 +00:00
}
2021-01-09 19:33:08 +00:00
else{
if(*phead==TUK)
running=true;
}
phead++;
2021-01-05 11:05:26 +00:00
}
2021-01-09 19:33:08 +00:00
printf(" \n");
2021-01-05 11:05:26 +00:00
}
int main() {
2021-01-09 10:23:46 +00:00
char buff[30000];
FILE *fp;
2021-01-09 19:33:08 +00:00
fp = fopen("kod.txt","r");
//memset(buff,0,sizeof(buff));
size_t fr = fread(buff,1,30000,fp);
buff[fr]=0;
2021-01-09 10:23:46 +00:00
2021-01-09 19:33:08 +00:00
//printf("buffer:\n %s\n",buff);
2021-01-09 10:23:46 +00:00
run_tukafuk(buff);
fclose(fp);
2021-01-09 19:33:08 +00:00
/*printf("Programova Paska: \n");
2021-01-09 10:23:46 +00:00
2021-01-09 19:33:08 +00:00
for(inst *ptr=ptape;ptr<=phead;ptr++){
2021-01-08 23:44:49 +00:00
switch (*ptr){
case TUK:printf("TUK ");
break;
case INK:printf("INK ");
break;
case DEK:printf("DEK ");
break;
case BREK:printf("BREK ");
break;
case GLEK:printf("GLEK ");
break;
case PUK:printf("PUK ");
break;
case KUK:printf("KUK ");
break;
case FUK:printf("FUK ");
break;
case LP:printf("LP ");
break;
case RP:printf("RP ");
break;
default:printf("NULL ");
break;
}
}
2021-01-09 19:33:08 +00:00
*/
2021-01-05 11:05:26 +00:00
return 0;
}