summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiguel <m.i@gmx.at>2018-09-21 11:32:45 +0200
committerMiguel <m.i@gmx.at>2018-09-21 11:32:45 +0200
commite68c8e3468076afa171ffff82b9bafa4cbc335bb (patch)
tree91d4fb289c87fd4dbc080c1d041104716dadaef6
parent6125ac9b8c864eeeb4619021cdde0f40e784fc14 (diff)
"improved" our brainfuck interpreter
-rw-r--r--userspace/Makefile1
-rw-r--r--userspace/brainfuck.c74
-rw-r--r--userspace/files/file.txt82
-rw-r--r--userspace/files/hello.brain (renamed from userspace/brain.brain)2
4 files changed, 117 insertions, 42 deletions
diff --git a/userspace/Makefile b/userspace/Makefile
index f69ea9d..4afb98c 100644
--- a/userspace/Makefile
+++ b/userspace/Makefile
@@ -40,6 +40,7 @@ ext2.img: $(PROGS)
@sudo mount ext2.img mnt
@sudo chown miguel mnt
@mkdir -p mnt/home/miguel
+ @cp files/* mnt/home/miguel/
@mkdir -p mnt/boot
@mkdir -p mnt/bin
@mkdir -p mnt/doc/fonts
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 --;
}
diff --git a/userspace/files/file.txt b/userspace/files/file.txt
new file mode 100644
index 0000000..8cdd524
--- /dev/null
+++ b/userspace/files/file.txt
@@ -0,0 +1,82 @@
+Sometimes someone would speak in a boat. But most
+of the boats were silent except for the dip of the
+oars. They spread apart after they were out of the
+mouth of the harbour and each one headed for the
+part of the ocean where he hoped to find fish. The
+old man knew he was going far out and he left the
+smell of the land behind and rowed out into the
+clean early morning smell of the ocean. He saw the
+phosphorescence of the Gulf weed in the water as
+he rowed over the part of the ocean that the
+fishermen called the great well because there was
+a sudden deep of seven hundred fathoms where all
+sorts of fish congregated because of the swirl the
+current made against the steep walls of the floor
+of the ocean. Here there were concentrations of
+shrimp and bait fish and sometimes schools of
+squid in the deepest holes and these rose close to
+the surface at night where all the wandering fish
+fed on them.
+
+In the dark the old man could feel the morning
+coming and as he rowed he heard the trembling
+sound as flying fish left the water and the
+hissing that their stiff set wings made as they
+soared away in the darkness. He was very fond of
+flying fish as they were his principal friends on
+the ocean. He was sorry for the birds, especially
+the small delicate dark terns that were always
+flying and looking and almost never finding, and
+he thought, "The birds have a harder life than we
+do except for the robber birds and the heavy
+strong ones. Why did they make birds so delicate
+and fine as those sea swallows when the ocean can
+be so cruel? She is kind and very beautiful. But
+she can be so cruel and it comes so suddenly and
+such birds that fly, dipping and hunting, with
+their small sad voices are made too delicately for
+the sea."
+
+He always thought of the sea as la mar which is
+what people call her in Spanish when they love
+her. Sometimes those who love her say bad things
+of her but they are always said as though she were
+a woman. Some of the younger fishermen, those who
+used buoys as floats for their lines and had
+motorboats, bought when the shark livers had
+brought much money, spoke of her as el mar which
+is masculine. They spoke of her as a contestant or
+a place or even an enemy. But the old man always
+thought of her as feminine and as something that
+gave or withheld great favours, and if she did
+wild or wicked things it was because she could not
+help them. The moon affects her as it does a
+woman, he thought.
+
+He was rowing steadily and it was no effort for
+him since he kept well within his speed and the
+surface of the ocean was flat except for the
+occasional swirls of the current. He was letting
+the current do a third of the work and as it
+started to be light he saw he was already further
+out than he had hoped to be at this hour.
+
+I worked the deep wells for a week and did
+nothing, he thought. Today I'll work out where the
+schools of bonita and albacore are and maybe there
+will be a big one with them.
+
+Before it was really light he had his baits out
+and was drifting with the current. One bait was
+down forty fathoms. The second was at seventy-five
+and the third and fourth were down in the blue
+water at one hundred and one hundred and
+twenty-five fathoms. Each bait hung head down with
+the shank of the hook inside the bait fish, tied
+and sewed solid and all the projecting part of the
+hook, the curve and the point, was covered with
+fresh sardines. Each sardine was hooked through
+both eyes so that they made a half-garland on the
+projecting steel. There was no part of the hook
+that a great fish could feel which was not sweet
+smelling and good tasting.
diff --git a/userspace/brain.brain b/userspace/files/hello.brain
index cf7d005..8fa0f72 100644
--- a/userspace/brain.brain
+++ b/userspace/files/hello.brain
@@ -1 +1 @@
-++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.X
+++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.