-
-
Save kristofgilicze/79cd7b86dcfdeb8cbefd20319c783530 to your computer and use it in GitHub Desktop.
BF.C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*============================== | |
* The BrainF*ck interpreter | |
* Kristof Gilicze | |
* 2018.09.29. | |
*==============================*/ | |
#include <stdio.h> | |
enum StatusCode{ | |
SUCCESSFULL_RUN=0, | |
TAPE_UNDER=1, | |
TAPE_OVER=2, | |
MISSING_CLOSING_BRACKET=3, | |
MISSING_OPENING_BRACKET=4 | |
}; | |
enum StatusCode bf_interpreter(char bf_forraskod[]){ | |
char szalag[32768] = {0}; | |
// int std_bemenet = 0; | |
int ciklus_szint = 0; | |
int i = 0; | |
int mutato = 0; | |
while (bf_forraskod[i] != 0){ | |
switch(bf_forraskod[i]){ | |
case '>': /* Jobbra lép a szalagon */ | |
mutato++; | |
if (mutato >= 32768) return TAPE_UNDER; | |
break; | |
case '<': /* Balra lép a szalagon */ | |
mutato--; | |
if (mutato < 0) return TAPE_OVER; | |
break; | |
case '+': /* A mutatott bájt növelése eggyel */ | |
szalag[mutato]++; | |
break; | |
case '-': /* A mutatott bájt csökkentése eggyel */ | |
szalag[mutato]--; | |
break; | |
case '.': /* A mutatott karakter (bájt) kiírása a kimenetre */ | |
putchar(szalag[mutato]); | |
break; | |
case ',': /* Karakter beolvasása a bemenetről a mutatott bájtba */ | |
/* | |
EOF eleve -1 ezért felesleges vizsgálni. | |
std_bemenet = getchar(); | |
szalag[mutato] = std_bemenet == EOF ? -1:std_bemenet; | |
*/ | |
szalag[mutato] = getchar(); | |
// printf('----[%d]----', mutato); | |
break; | |
case '[': /* Elöltesztelő ciklus kezdete */ | |
if(szalag[mutato] == 0){ | |
i++; | |
while(bf_forraskod[i] != ']' || ciklus_szint > 0){ | |
if(bf_forraskod[i] == '[') ciklus_szint++; | |
if(bf_forraskod[i] == ']') ciklus_szint--; | |
i++; | |
if(bf_forraskod[i] == 0) return MISSING_CLOSING_BRACKET; | |
} | |
} | |
break; | |
case ']': /* Ciklus vége */ | |
if(szalag[mutato] != 0){ | |
i--; | |
while(bf_forraskod[i] != '[' || ciklus_szint > 0){ | |
if(bf_forraskod[i] == ']') ciklus_szint++; | |
if(bf_forraskod[i] == '[') ciklus_szint--; | |
i--; | |
if(i < 0) return MISSING_OPENING_BRACKET; | |
} | |
i--; | |
} | |
break; | |
default: /* Komment */ | |
break; | |
} | |
i++; | |
} | |
return SUCCESSFULL_RUN; | |
} | |
void run(const char *nev, char bf_forraskod[]) { | |
const char *status_szoveg[]={ | |
"BrainF*ck program sikeresen lefutott...", | |
"Tulcsuszott a programod a szalag elejen.", | |
"Tulcsuszott a programod a szalag vegen.", | |
"Szintaktikai hiba, hianyzo lezaro \"]\" karakter.", | |
"Szintaktikai hiba, hianyzo nyito \"[\" karakter.", | |
}; | |
printf("----------------[ %s ]---------------- \n\n", nev); | |
enum StatusCode status = bf_interpreter(bf_forraskod); | |
printf("\n\n[ STATUS ]: %s\n\n\n", status_szoveg[status]); | |
} | |
int main(){ | |
char print_nev[]="-[--->+<]>-------.-[--->+<]>.----------.++++++++.----.[--->+<]>---.[->+++<]>+.-[->+++<]>.+[----->+<]>+.---------.[--->+<]>-.+[->+++<]>.++++++++."; | |
char haromszog[]="[ThisprogramprintsSierpinskitriangleon80-columndisplay.]>++++[<++++++++>-]>++++++++[>++++<-]>>++>>>+>>>+<<<<<<<<<<[-[->+<]>[-<+>>>.<<]>>>[[->++++++++[>++++<-]>.<<[->+<]+>[->++++++++++<<+>]>.[-]>]]+<<<[-[->+<]+>[-<+>>>-[->+<]++>[-<->]<<<]<<<<]++++++++++.+++.[-]<]+++++*****Made*By:*NYYRIKKI*2002*****"; | |
char print_bf[]="++++++++[>++++++++<-]>++.++++."; | |
char hibas_kod1[]="<<<<<<<<>>>>[..]"; | |
char hibas_kod2[]=">><<]["; | |
char test_input_kod[]=">,[>,]<[.<]"; | |
run("BF kiirasa", print_bf); | |
run("Nevem kiirasa", print_nev); | |
run("Haromszog rajzolas", haromszog); | |
run("Hibas kod pelda 1.", hibas_kod1); | |
run("Hibas kod pelda 2.", hibas_kod2); | |
run("Input test", test_input_kod); | |
printf("\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment