Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * bfun.c ; BrainFuck UNleashed! (or BrainFuck fUN w/e)
- */
- #include <stdio.h>
- #include <string.h>
- static char cell[3000];
- static char *ptr = cell;
- static FILE *input;
- static char pBck, pAdv, cInc, cDec, tInp, tOut, jInz, jOiz;
- static char flow = 0, numpad = 0, wysiwyg = 0;
- void process(char c, FILE *f);
- int main(int argc, char **argv)
- {
- switch(argc)
- {
- int argn;
- char *split;
- case 1:
- printf("bfun -- a C interpreter for Brainfuck ver 1.0\n"
- "\n"
- "Usage:\n"
- "\tbfun filename [options]\n"
- "Options:\n"
- "\t-f \t Dump workflow to STDOUT\n"
- "\t-n \t Use alternative charset for language (numpad keys)\n"
- "\t-w \t WYSIWYG - I/O integers by real value (not ASCII)\n");
- return 0;
- default:
- for(argn=2;argn < argc;++argn)
- {
- if (strlen(argv[argn]) == 2)
- {
- if((split = strstr(argv[argn], "-")) == NULL)
- goto Fail;
- switch(split[strlen(split) -1])
- {
- case 'f':
- flow = 1;
- break;
- case 'n':
- numpad = 1;
- break;
- case 'w':
- wysiwyg = 1;
- break;
- default:
- goto Fail;
- }
- }
- else
- {
- Fail:
- printf("bfun: invalid option '%s'\n"
- "Usage: bfun filename [-f] [-n] [-w]\n", argv[argn]);
- return 0;
- }
- }
- case 2:
- input = fopen(argv[1], "r");
- if (!input) {
- fprintf(stderr, "Error: No such file %s\n", argv[1]);
- return 0;
- }
- }
- ptr = &cell[0];
- if (numpad)
- pBck = '4',pAdv = '6',cInc = '+',cDec = '-',tInp = '1',tOut = '2',jInz = '9',jOiz = '3';
- else
- pBck = '<',pAdv = '>',cInc = '+',cDec = '-',tInp = ',',tOut = '.',jInz = '[',jOiz = ']';
- while(!feof(input))
- process(getc(input), input);
- fclose(input);
- return 1;
- }
- void process(char cur, FILE * input)
- {
- long pos;
- char loopcom;
- if (cur == pBck)
- --ptr;
- else if (cur == pAdv)
- ++ptr;
- else if (cur == cInc)
- ++*ptr;
- else if (cur == cDec)
- --*ptr;
- else if (cur == tInp)
- *ptr = getchar();
- else if (cur == tOut)
- putchar(*ptr);
- else if (cur == jInz)
- {
- pos = ftell(input);
- while (*ptr != 0)
- {
- fseek (input, pos, SEEK_SET);
- loopcom = getc(input);
- while(loopcom != jOiz && loopcom != EOF)
- {
- process(loopcom, input);
- loopcom = getc(input);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement