From d25994b7fdfceb954f044c7188a4d4fda5d8ea6c Mon Sep 17 00:00:00 2001 From: DiamondNether90 Date: Tue, 17 Feb 2026 16:44:35 +1100 Subject: [PATCH] Initial Commit --- bfc | Bin 0 -> 16768 bytes src/file.c | 28 ++++++++++++++ src/file.h | 4 ++ src/main.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++ src/struct.c | 22 +++++++++++ src/struct.h | 19 ++++++++++ tests/aaaaaa.bf | 1 + 7 files changed, 173 insertions(+) create mode 100755 bfc create mode 100644 src/file.c create mode 100644 src/file.h create mode 100644 src/main.c create mode 100644 src/struct.c create mode 100644 src/struct.h create mode 100644 tests/aaaaaa.bf diff --git a/bfc b/bfc new file mode 100755 index 0000000000000000000000000000000000000000..2a9d51984ca63150ec0900c0bd99f647b88f6650 GIT binary patch literal 16768 zcmeHOe{2-joqxOhut^MS2(%0rpO*gq5 zd!O%n?>nBIc&oeqak@YDSu@}F{d|AC@B7Ys>v=QtzHA=Y8w>;lmsw(mAa1HzAqETX z&;YO4w=ivXpAnyzC%aFUzt72zIplS z&L6z+`ER|u>C0P7=TdjCPbS*8Y+9d;txG0Sef{hDw{BUtWm6=Zj%*Zk{TJaNe==_6 zSdJ3H?|plarv$hUmz97>&tF;r%!L0Kc-XHD{&pNQ>7S^9)3}&PpTeIB&(y$=*1(fB z@Sg(@Jj90Q=IM3v|exP{2-fT3WXMoX5!J9=LA06WA{W8 zDUo$z@eH(kGl`Vb38x+XQM)sdiY60}#zm(SPbNi&GVbh1rnA7Z@%Y1n{LuK@BK95F zziYR>F|x_6-7{U?7}+fB{YMVkXeHj2$U5=Nk%POF=~Vnkv^^O|dtE*0ln%&NsxvAI zjB}bHLHx~@XR0ZFx<;7_K<^AF&&$P$#C-b28rIlPzy1WK$t>}d=KJnQ%p*U43!V!9 z?sC2-7AkyP|E@AVtnqF`$r)$9aKFN7PRhxsSaH>Z>z`gJDSGf*wJwoy56*oee8Pk4 zPzZL%3;pk^8lMJ9g`j_1G{?9!8m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75A$kHjJS`M8^yj#ze?ig&D6A?G8 z-VENoWSkQk+PQ20I-1zhrJ7aBCD+k%v?tMlu+nG@r-+O|wT5dVBADDgn6sWAls~dc z>jS!{#?O~5we}rq@rT|c;QK{^pL*B6);>_SCtYvZ0tT;;(%e3;JDA?7mHGb@-|w>z z0~K0xusyF!n9~1aDzto|D%&R z=`q;TnuK{?moPPSQBy6(J%=i1K)m+6@0saqZeU8E?X#fX)8JR z6uy28J&THW{`>b48?30Q-~Jzb_PmZ`8ur=y`+p5`+=a?HNUHqYg7;nf+hd;TzriNt zvFCN$I6mv8!g`9E<{7^Z0Syse-}Cy&-M_8#ryqZ~{&*bSfGw%8J?}FttOtm)RuT8i z?J|8260be4w(O0;~y}Tq_>8BSc%>9 zeBHiQcX77&&wq-QikI@(R}ys|)(#DwczRB?7zg*8f4}MZ0mrT^)w#G?(c67OZkKgw Tnd4KDqS98S@e!Yaj}`w7*k+{} literal 0 HcmV?d00001 diff --git a/src/file.c b/src/file.c new file mode 100644 index 0000000..b26b424 --- /dev/null +++ b/src/file.c @@ -0,0 +1,28 @@ +#include "file.h" + +char* read_file(char* filename) { + FILE* fptr = fopen(filename, "r"); + if (fptr == NULL) { + return NULL; + } + + // Get length + fseek(fptr, 0L, SEEK_END); + long file_size = ftell(fptr); + rewind(fptr); + + // Allocate buffer + char* buffer = malloc(file_size+1); + if (buffer == NULL) { + return NULL; + } + + // Read file + size_t read = fread(buffer, sizeof(char), file_size, fptr); + if (read != file_size) return NULL; + + buffer[file_size] = '\0'; + fclose(fptr); + + return buffer; +} \ No newline at end of file diff --git a/src/file.h b/src/file.h new file mode 100644 index 0000000..6f304de --- /dev/null +++ b/src/file.h @@ -0,0 +1,4 @@ +#include +#include + +char* read_file(char* filename); \ No newline at end of file diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..a9aa518 --- /dev/null +++ b/src/main.c @@ -0,0 +1,99 @@ +#include "struct.h" +#include "file.h" +#include +#include +#include + +int main(int argc, char* argv[]) { + if (argc != 3) { + fprintf(stderr, "Usage: %s \n", argv[0]); + return 1; + } + + char* code = read_file(argv[1]); + if (code == NULL) { + fprintf(stderr, "Cannot open file \"%s\"\n", argv[1]); + return 2; + } + + char* input = argv[2]; + int inputidx = 0; + + Celltape cells = { + .cells = calloc(30000, sizeof(bitarr)), + .ptr = 0 + }; + + int scope = 0; + int max_scope = 0; // Highest ever reached scope + int* loop_start = malloc(0); + + PartialByte to_print = { + .data = 0, + .idx = 0 + }; + + bool ignore = false; + + for (int idx = 0; code[idx] != '\0'; idx++) { + if (ignore) { + if (code[idx] == ']') { + ignore = false; + } + continue; + } + switch (code[idx]) { + case '+': + case '-': + flip_bit(&(cells.cells), cells.ptr); + break; + + case '>': + cells.ptr += 1; + break; + + case '<': + cells.ptr -= 1; + break; + + case '[': + if (!get_bit(cells.cells, cells.ptr)) { + ignore = true; + break; + } + scope += 1; + if (scope > max_scope) { + max_scope += scope; + loop_start = realloc(loop_start, scope); + } + loop_start[scope-1] = idx; + break; + + case ']': + if (get_bit(cells.cells, cells.ptr)) { + idx = loop_start[scope-1]; + } else { + scope -= 1; + } + break; + + case '.': + case ';': + append_bit(&to_print, get_bit(cells.cells, cells.ptr)); + if (to_print.idx == 8) { + to_print.idx = 0; + printf("%c", to_print.data); + } + break; + + case ',': + if ((input[inputidx/8] & (1 << (inputidx % 8))) >> (inputidx % 8)) { + one_bit(&cells.cells, cells.ptr); + } else { + zero_bit(&cells.cells, cells.ptr); + } + inputidx += 1; + break; + } + } +} \ No newline at end of file diff --git a/src/struct.c b/src/struct.c new file mode 100644 index 0000000..bb69c5c --- /dev/null +++ b/src/struct.c @@ -0,0 +1,22 @@ +#include "struct.h" + +void flip_bit(bitarr** cells, int idx) { + (*cells)[idx / 8] ^= (bitarr)(0x80 >> (idx % 8)); +} + +int get_bit(bitarr* cells, int ptr) { + return ((cells[ptr/8]) & (128 >> (ptr % 8))) >> 7; +} + +void append_bit(PartialByte* byte, int bit) { + (*byte).data |= bit << ((*byte).idx); + (*byte).idx += 1; +} + +void zero_bit(bitarr** cells, int idx) { + (*cells)[idx / 8] &= 255 ^ (128 >> idx); +} + +void one_bit(bitarr** cells, int idx) { + (*cells)[idx / 8] |= (128 >> idx); +} \ No newline at end of file diff --git a/src/struct.h b/src/struct.h new file mode 100644 index 0000000..49aee4c --- /dev/null +++ b/src/struct.h @@ -0,0 +1,19 @@ +#define bitarr unsigned char +#define u64 uint64_t +#include + +typedef struct { + bitarr data; + bitarr idx; +} PartialByte; + +typedef struct { + bitarr* cells; + int ptr; +} Celltape; + +void flip_bit(bitarr** cells, int idx); +int get_bit(bitarr* cells, int ptr); +void append_bit(PartialByte* byte, int bit); +void zero_bit(bitarr** cells, int idx); +void one_bit(bitarr** cells, int idx); \ No newline at end of file diff --git a/tests/aaaaaa.bf b/tests/aaaaaa.bf new file mode 100644 index 0000000..d0cbe89 --- /dev/null +++ b/tests/aaaaaa.bf @@ -0,0 +1 @@ ++>+>+>+>+>+>+>+<<<<<<<,;,;,;,;,;,;,;,; \ No newline at end of file