diff options
| author | Elis Eriksson <spelis@spelis.li> | 2026-06-20 11:26:50 +0200 |
|---|---|---|
| committer | Elis Eriksson <spelis@spelis.li> | 2026-06-20 11:26:50 +0200 |
| commit | de91ec0542aea5959376ee0d2c9d486268dd9392 (patch) | |
| tree | 2720ad71b20b9fcb6bf4a32d3890cf94a3e0d77a | |
| parent | cbdca328495a772716f14b1e95a7ab7d6450cc27 (diff) | |
| download | bfc-de91ec0542aea5959376ee0d2c9d486268dd9392.tar bfc-de91ec0542aea5959376ee0d2c9d486268dd9392.tar.gz bfc-de91ec0542aea5959376ee0d2c9d486268dd9392.tar.bz2 bfc-de91ec0542aea5959376ee0d2c9d486268dd9392.tar.lz bfc-de91ec0542aea5959376ee0d2c9d486268dd9392.tar.xz bfc-de91ec0542aea5959376ee0d2c9d486268dd9392.tar.zst bfc-de91ec0542aea5959376ee0d2c9d486268dd9392.zip | |
Add a README, replace the shell script with C code. Add a LICENSE.
| -rw-r--r-- | LICENSE | 8 | ||||
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | README.md | 44 | ||||
| -rwxr-xr-x | bfc.sh | 27 | ||||
| -rw-r--r-- | src/bfc.c | 41 |
5 files changed, 93 insertions, 29 deletions
@@ -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. + @@ -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 @@ -1,27 +0,0 @@ -#!/usr/bin/env sh - -set -e - -BFC="./build/bfc" -SRC="$1" - -if [ -z "$SRC" ]; then - echo "usage: $0 <file.bf>" >&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 - @@ -1,16 +1,55 @@ #include "all.h" #include "str.h" +#include <stdio.h> +#include <string.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <unistd.h> 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; } |
