summaryrefslogtreecommitdiff
path: root/userspace/brainfuck.c
diff options
context:
space:
mode:
Diffstat (limited to 'userspace/brainfuck.c')
-rw-r--r--userspace/brainfuck.c74
1 files changed, 33 insertions, 41 deletions
diff --git a/userspace/brainfuck.c b/userspace/brainfuck.c
index eda0556..c608064 100644
--- a/userspace/brainfuck.c
+++ b/userspace/brainfuck.c
@@ -18,29 +18,45 @@
// by brainfuck standards (doesn't that sound funny?), the data pointer has
// 3,000 bytes at its disposal, but I hate hard-coding such stuff.
-
// FOOLOS: decreased to 3000 (from 30.000)
-#define DATA_SIZE 3000
-void usage(char *progname)
-{
- printf("Usage: %s [FILE]\n",progname);
-}
+#define DATA_SIZE 3000
+#define BUF_SIZE 3000
int main(int argc, char **argv)
{
- puts("\n\nbrainfuck: Welcome to the BRAINFUCK INTERPRETER by Felix Oghina");
- puts("brainfuck: Licensed under the (brain)fuck licenses!");
- puts("brainfuck: Adapted & Compiled for FoolOS by Miguel");
+ // if any arg than show info message
+ if(argc>1)
+ {
+ puts("BRAINFUCK INTERPRETER by Felix Oghina");
+ puts("Licensed under the (brain)fuck licenses!");
+ puts("Adapted & Compiled for FoolOS by Miguel");
+ puts("Reads stdin and write to stdout");
+ return EXIT_SUCCESS;
+ }
+
+ // buffer input so we can seek
+ unsigned char *prog= malloc(BUF_SIZE);
+ unsigned char *progread= prog;
+ unsigned int progpos=0;
+
// used by the bf program
- unsigned char *dataptr = malloc(sizeof(char) * DATA_SIZE);
+ unsigned char *dataptr = calloc(1,sizeof(char) * DATA_SIZE);
// position of the data pointer
unsigned int datapos = 0;
- // input file
- FILE *input;
+ // input/output files
+ FILE *input=stdin;
+ FILE *output=stdout;
+
+ while(1)
+ {
+ int l=fread(progread,1,255,input);
+ if(l==0)break;
+ progread+=l;
+ }
// level - deepness of brackets
// i - uh, you need explanation for this one?
@@ -48,37 +64,14 @@ int main(int argc, char **argv)
// we will read chars from the input into r
unsigned char r;
-
- // check argument count
- if(argc!=2)
- {
- usage(argv[0]);
- return EXIT_FAILURE;
- }
-
- // open brainfuck source
- input = fopen(argv[1], "r");
-
- if (input == NULL)
- {
- puts("Error opening input file");
- return EXIT_FAILURE;
- }
-
- // zero the data pointer
- for (i=0; i < DATA_SIZE; i++) {
- dataptr[i] = 0;
- }
// start interpreting
while (1) {
- r = (unsigned char) fgetc(input);
+ r=prog[progpos++];
+ if(r==0)break;
switch(r) {
- case 'X':
- puts("END!");
- return 0;
case '>':
if (datapos < DATA_SIZE - 1) datapos++;
else {
@@ -109,7 +102,7 @@ int main(int argc, char **argv)
if (dataptr[datapos] == 0) {
level = 1;
while (level != 0) {
- r = (unsigned char) fgetc(input);
+ r=prog[progpos++];
if (r == '[') level ++;
else if (r == ']') level --;
}
@@ -119,9 +112,8 @@ int main(int argc, char **argv)
if (dataptr[datapos] != 0) {
level = 1;
while (level != 0) {
- //fseek(input, -2, SEEK_CUR);
- _lseek(3,-2,SEEK_CUR);
- r = (unsigned char) fgetc(input);
+ progpos-=2;
+ r=prog[progpos++];
if (r == ']') level ++;
else if (r == '[') level --;
}