commit 7ed58a61b24f3977eec97217f2539081f6e06fd8 Author: Random936 Date: Sun Dec 1 15:54:04 2024 -0800 Initial commit diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0542048 --- /dev/null +++ b/Makefile @@ -0,0 +1,6 @@ +SRC_Files=$(wildcard src/*.c) +CC=clang +CF=-Wall -g + +install: + $(CC) $(CF) $(SRC_Files) -o brainfuck diff --git a/brainfuck b/brainfuck new file mode 100644 index 0000000..30e98d0 Binary files /dev/null and b/brainfuck differ diff --git a/examples/helloworld.bf b/examples/helloworld.bf new file mode 100644 index 0000000..265e751 --- /dev/null +++ b/examples/helloworld.bf @@ -0,0 +1 @@ +++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>. diff --git a/rules.txt b/rules.txt new file mode 100644 index 0000000..34a6af6 --- /dev/null +++ b/rules.txt @@ -0,0 +1,3 @@ +INST = + | - | < | > | . | , | LOOP +LOOP = [INST] + diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..43aa256 --- /dev/null +++ b/src/main.c @@ -0,0 +1,57 @@ +#include +#include + +unsigned char cells[1024]; +unsigned char *ptr = cells; + +void error(char c) { + fprintf(stderr, "ERROR: Unexpected token, %c\n", c); + exit(1); +} + +char *parse(char *src) { + + char *start = src; + + while (*src != '\0') { + switch (*src) { + case 0: return NULL; + case '.': printf("%c", (*ptr)); break; + case ',': *ptr = getc(stdin); break; + case '+': (*ptr)++; break; + case '-': (*ptr)--; break; + case '>': ptr++; break; + case '<': ptr--; break; + case '[': src = parse(++src); break; + case ']': + if (*ptr == 0) return src; + src = start; + continue; + } + + src++; + } + + return NULL; +} + +int main(int argc, char *argv[]) { + + if (argc != 2) return 1; + + FILE *file = fopen(argv[1], "r"); + if (!file) return 1; + + fseek(file, 0, SEEK_END); + size_t size = ftell(file); + rewind(file); + + char *src = malloc(size); + fread(src, 1, size, file); + + if (parse(src) != NULL) { + error('['); + } + + return 0; +} diff --git a/test.bf b/test.bf new file mode 100644 index 0000000..f36f02a --- /dev/null +++ b/test.bf @@ -0,0 +1 @@ +,.