From de91ec0542aea5959376ee0d2c9d486268dd9392 Mon Sep 17 00:00:00 2001 From: Elis Eriksson Date: Sat, 20 Jun 2026 11:26:50 +0200 Subject: Add a README, replace the shell script with C code. Add a LICENSE. --- LICENSE | 8 ++++++++ Makefile | 2 +- README.md | 44 ++++++++++++++++++++++++++++++++++++++++++++ bfc.sh | 27 --------------------------- src/bfc.c | 41 ++++++++++++++++++++++++++++++++++++++++- 5 files changed, 93 insertions(+), 29 deletions(-) create mode 100644 LICENSE create mode 100644 README.md delete mode 100755 bfc.sh diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..054f6db --- /dev/null +++ b/LICENSE @@ -0,0 +1,8 @@ +Copyright 2026 Elis Eriksson + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + diff --git a/Makefile b/Makefile index 8c7470c..ff523fb 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CFLAGS := -Isrc -MMD -MP -Wall -Wextra -LDFLAGS := +LDFLAGS := -Wall -Wextra TARGET = build/bfc diff --git a/README.md b/README.md new file mode 100644 index 0000000..3ab27f6 --- /dev/null +++ b/README.md @@ -0,0 +1,44 @@ +# BFC - Brainf*ck compiler + +A Brainf*ck compiler for Linux. + +`bfc` translates Brainfuck into C and immediately feeds it into GCC, producing small standalone ELF executables. No interpreters, no virtual machines, no runtime dependencies. + +## Build the compiler + +Just one command: + +`make` + +## Build a brainfuck program + +Just one more command: + +`cat file.bf | ./build/bfc > output` + +Then running the program is as simple as: + +`./output` + + +## How it works + +Brainf*ck source -> Lexer -> Parser -> AST -> C codegen -> GCC -> ELF executable + +A simple Hello World! program is about 9.3kb big. + +## Limitations + +* Linux only +* Soft-requires GCC (could be substituted for something else) +* Finite memory tape (30kb) + +## Why? + +Because interpreters suck. + +Also because turning Brainf*ck into an ELF executable is just objectively fun. + +## License + +MIT diff --git a/bfc.sh b/bfc.sh deleted file mode 100755 index 49c1370..0000000 --- a/bfc.sh +++ /dev/null @@ -1,27 +0,0 @@ -#!/usr/bin/env sh - -set -e - -BFC="./build/bfc" -SRC="$1" - -if [ -z "$SRC" ]; then - echo "usage: $0 " >&2 - exit 1 -fi - -# silently build compiler if missing -if [ ! -f "$BFC" ]; then - make -s || { - echo "build failed" >&2 - exit 1 - } -fi - -# compile pipeline -OUT=$(cat "$SRC" | "$BFC") - -LDFLAGS="-Os -march=native -mtune=native -flto -fuse-linker-plugin -DNDEBUG -fomit-frame-pointer -nostdlib -nostartfiles -no-pie -fno-stack-protector" - -echo "$OUT" > out.c -echo "$OUT" | gcc $LDFLAGS -o ./bf.out -x c - diff --git a/src/bfc.c b/src/bfc.c index d567f88..fc7d29d 100644 --- a/src/bfc.c +++ b/src/bfc.c @@ -1,16 +1,55 @@ #include "all.h" #include "str.h" +#include +#include +#include +#include +#include int main(void) { str input = {0}; str_append_fread(&input, stdin); + + // Run the lexer to get tokens (honestly useless considering the simplicity of + // BF) lexer_t lexer = lex_init(&input); lex_run(&lexer); + + // Run the parser and turn tokens into an AST representation parser_t p = parser_init(&lexer); ast_node_t *ast = parse_program(&p); str c_code = gen_code(ast); + + // Run gcc + int fd[2]; + pipe(fd); + + if (fork() == 0) { + dup2(fd[0], STDIN_FILENO); + close(fd[0]); + close(fd[1]); + + char *argv[] = {"gcc", "-Os", + "-flto", "-fuse-linker-plugin", + "-DNDEBUG", "-fomit-frame-pointer", + "-nostdlib", "-nostartfiles", + "-no-pie", "-fno-stack-protector", + "-o", "/proc/self/fd/1", + "-x", "c", + "-", NULL}; + + execvp("gcc", argv); + perror("execvp"); + _exit(1); + } + + // Cleanup + close(fd[0]); + write(fd[1], c_code.data, strlen(c_code.data)); + close(fd[1]); + wait(NULL); + ast_free(ast); - printf("%s", c_code.data); free_str(&input); return 0; } -- cgit v1.3-7-ge9ab