diff options
-rw-r--r-- | README.md | 39 | ||||
-rw-r--r-- | README.md~ | 39 | ||||
-rwxr-xr-x | build/GNULinux/cook | bin | 0 -> 11136 bytes | |||
-rw-r--r-- | build/Windows/cook.exe | bin | 0 -> 31912 bytes | |||
-rw-r--r-- | helloworld.ook | 1 | ||||
-rw-r--r-- | source/Makefile | 6 | ||||
-rw-r--r-- | source/bfcompiler.c | 142 | ||||
-rw-r--r-- | source/main.c | 24 | ||||
-rw-r--r-- | source/main.c~ | 24 | ||||
-rw-r--r-- | source/ook2bf.c | 79 |
10 files changed, 354 insertions, 0 deletions
diff --git a/README.md b/README.md new file mode 100644 index 0000000..5ff9981 --- /dev/null +++ b/README.md @@ -0,0 +1,39 @@ +# cOok +An Ook! compiler + +As a reminder, ook is just [Brainf\*\*\*](https://esolangs.org/wiki/Brainfuck), but with these rules: +| Ook | Brainf\*\*\* | +|-----|-----------| +| Ook. Ook? | > | +| Ook? Ook. | < | +| Ook. Ook. | + | +| Ook! Ook! | - | +| Ook! Ook. | . | +| Ook. Ook! | , | +| Ook! Ook? | [ | +| Ook? Ook! | ] | + +*Make sure that there is exactly one space/newline between each ook, and that you add no extra code* + +# Windows + +The file extension of the file must be exactly 3 letters. A recommended file extension is *.ook. +To compile ook, `cd` to the directory where cOok is installed, then run: +``` +cd build/Windows +cook <file> +``` + +# GNU/Linux + +To compile ook, `cd` to the directory where cOok is installed, then run: +``` +cd build/GNULinux +./cook <file> +``` + +If you want to compile the source, just run: +``` +make +``` +in the source directory. diff --git a/README.md~ b/README.md~ new file mode 100644 index 0000000..c9c15f8 --- /dev/null +++ b/README.md~ @@ -0,0 +1,39 @@ +# cOok +The compiler for ook. + +As a reminder, ook is just [Brainf\*\*\*](https://esolangs.org/wiki/Brainfuck), but with these rules: +| Ook | Brainf\*\*\* | +|-----|-----------| +| Ook. Ook? | > | +| Ook? Ook. | < | +| Ook. Ook. | + | +| Ook! Ook! | - | +| Ook! Ook. | . | +| Ook. Ook! | , | +| Ook! Ook? | [ | +| Ook? Ook! | ] | + +*Make sure that there is exactly one space/newline between each ook, and that you add no extra code* + +# Windows + +The file extension of the file must be exactly 3 letters. A recommended file extension is *.ook. +To compile ook, `cd` to the directory where cOok is installed, then run: +``` +cd build/Windows +cook <file> +``` + +# GNU/Linux + +To compile ook, `cd` to the directory where cOok is installed, then run: +``` +cd build/GNULinux +./cook <file> +``` + +If you want to compile the source, just run: +``` +make +``` +in the source directory. diff --git a/build/GNULinux/cook b/build/GNULinux/cook Binary files differnew file mode 100755 index 0000000..ca06e69 --- /dev/null +++ b/build/GNULinux/cook diff --git a/build/Windows/cook.exe b/build/Windows/cook.exe Binary files differnew file mode 100644 index 0000000..dee53e2 --- /dev/null +++ b/build/Windows/cook.exe diff --git a/helloworld.ook b/helloworld.ook new file mode 100644 index 0000000..09d7213 --- /dev/null +++ b/helloworld.ook @@ -0,0 +1 @@ +Ook. Ook? Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook! Ook? Ook? Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook? Ook! Ook! Ook? Ook! Ook? Ook. Ook! Ook. Ook. Ook? Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook! Ook? Ook? Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook? Ook! Ook! Ook? Ook! Ook? Ook. Ook. Ook. Ook! Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook! Ook. Ook! Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook! Ook. Ook. Ook? Ook. Ook? Ook. Ook? Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook! Ook? Ook? Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook? Ook! Ook! Ook? Ook! Ook? Ook. Ook! Ook. Ook. Ook? Ook. Ook? Ook. Ook? Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook! Ook? Ook? Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook? Ook! Ook! Ook? Ook! Ook? Ook. Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook. Ook? Ook. Ook? Ook. Ook? Ook. Ook? Ook. Ook! Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook! Ook. Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook. Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook. Ook. Ook? Ook. Ook? Ook. Ook. Ook! Ook.
\ No newline at end of file diff --git a/source/Makefile b/source/Makefile new file mode 100644 index 0000000..dbc7162 --- /dev/null +++ b/source/Makefile @@ -0,0 +1,6 @@ +CC=gcc + +default: cook + +cook: main.c ook2bf.c bfcompiler.c + $(CC) -o cook main.c diff --git a/source/bfcompiler.c b/source/bfcompiler.c new file mode 100644 index 0000000..9325faf --- /dev/null +++ b/source/bfcompiler.c @@ -0,0 +1,142 @@ +#define START_SIZE 10 +typedef struct List +{ + unsigned int size; + char* array; + +} List; + + +void create_list(List* s) +{ + s->size = START_SIZE; + s->array = malloc(START_SIZE); + int i; + for (i = 0; i < s->size; i++) + s->array[i] = 0; + +} + +void right_change_size(List* s, int new_size) +{ + char* new_array = malloc(new_size); + unsigned int i; + for (i = 0; i < s->size; i++) + new_array[i] = s->array[i]; + for (i = s->size; i < new_size; i++) + new_array[i] = 0; + + free(s->array); + s->array = new_array; + + s->size = new_size; + + + +} + +int left_change_size(List* s, int new_size) +{ + char* new_array = malloc(new_size); + unsigned int i; + for (i = 0; i < s->size; i++) + new_array[i+new_size-s->size-1] = s->array[i]; + + for (i = 0; i < new_size-s->size-1; i++) + new_array[i] = 0; + + free(s->array); + s->array = new_array; + + int ret = new_size - s->size; + + s->size = new_size; + + return ret; +} + +int right_matching(char* text, int pos) +{ + int level = 0; + int i; + for (i = pos+1;;i++) + { + if (i >= strlen(text)) + return strlen(text)-1; + if (text[i] == '[') + level++; + if (text[i] == ']' && level-- == 0) + break; + + } + + return i; +} + +int left_matching(char* text, int pos) +{ + int level = 0; + int i; + for (i = pos-1;;i--) + { + if (i < 0) + return 0; + if (text[i] == ']') + level++; + if (text[i] == '[' && level-- == 0) + break; + + } + + return i; +} + + +void compilebf(char* bf) +{ + int size; + int i; + int ptr; + List* l; + + size = strlen(bf); + + ptr = START_SIZE / 2; + create_list(l); + + for (i = 0; i < size; i++) + { + switch(bf[i]) + { + case '>': + ptr++; + if (ptr >= l->size) + right_change_size(l, l->size*2); + break; + case '<': + ptr--; + if (ptr < 0) + ptr += left_change_size(l, l->size*2); + break; + case '+': + l->array[ptr]++; + break; + case '-': + l->array[ptr]--; + break; + case '.': + putchar(l->array[ptr]); + break; + case '[': + if (!l->array[ptr]) + i = right_matching(bf, i); + break; + case ']': + i = left_matching(bf, i)-1; + break; + + } + + } + +} diff --git a/source/main.c b/source/main.c new file mode 100644 index 0000000..c832a5e --- /dev/null +++ b/source/main.c @@ -0,0 +1,24 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "ook2bf.c" +#include "bfcompiler.c" + +int main(int argc, char** argv) +{ + if (argc < 2) + { + fprintf(stderr, "Error: Usage: %s <file>\n", argv[0]); + return 0; + } + + char* bf; + + + bf = ook2bf(argv[1]); + compilebf(bf); + + + + return 0; +} diff --git a/source/main.c~ b/source/main.c~ new file mode 100644 index 0000000..6d75950 --- /dev/null +++ b/source/main.c~ @@ -0,0 +1,24 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "ook2bf.c" +#include "bfcompiler.c" + +int main(int argc, char** argv) +{ + if (argc < 2) + { + fprintf(stderr, "Error: Usage: %s <file> <output_file>\n", argv[0]); + return 0; + } + + char* bf; + + + bf = ook2bf(argv[1]); + compilebf(bf); + + + + return 0; +} diff --git a/source/ook2bf.c b/source/ook2bf.c new file mode 100644 index 0000000..dcc8c73 --- /dev/null +++ b/source/ook2bf.c @@ -0,0 +1,79 @@ + + + +int isValid(char c) +{ + return c == '.' || c == '!' || c == '?'; +} + + +char ook2bfchar(char a, char b) +{ + if (a == '.' && b == '?') + return '>'; + if (a == '?' && b == '.') + return '<'; + if (a == '.' && b == '.') + return '+'; + if (a == '!' && b == '!') + return '-'; + if (a == '!' && b == '.') + return '.'; + if (a == '.' && b == '!') + return ','; + if (a == '!' && b == '?') + return '['; + if (a == '?' && b == '!') + return ']'; + return '.'; +} + +int bfsize(int filesize) +{ + int i; + int sz = 0; + for (i = 3; i < filesize; i+=10) + sz++; + + return sz; +} + +int getSize(FILE *fp) +{ + fseek(fp, 0L, SEEK_END); + int sz = ftell(fp); + fseek(fp, 0L, SEEK_SET); + return sz; +} + + +char* ook2bf(char* in_filename) +{ + + FILE* in_file; + int filesize; + int i; + + in_file = fopen(in_filename, "r"); + + filesize = getSize(in_file); + + char ook[filesize]; + fread(ook, 1, filesize, in_file); + + + + fclose(in_file); + char* bf = malloc(bfsize(filesize)); + char c; + for (i = 3; (i+5) < filesize; i+=10) + { + if (!isValid(ook[i]) || !isValid(ook[i+5])) + continue; + c = ook2bfchar(ook[i], ook[i+5]); + bf[(i-3)/10] = c; + } + + return bf; + +} |