aboutsummaryrefslogtreecommitdiff
path: root/src/bfc.c
diff options
context:
space:
mode:
authorElis Eriksson <spelis@spelis.li>2026-06-20 11:26:50 +0200
committerElis Eriksson <spelis@spelis.li>2026-06-20 11:26:50 +0200
commitde91ec0542aea5959376ee0d2c9d486268dd9392 (patch)
tree2720ad71b20b9fcb6bf4a32d3890cf94a3e0d77a /src/bfc.c
parentcbdca328495a772716f14b1e95a7ab7d6450cc27 (diff)
downloadbfc-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.
Diffstat (limited to 'src/bfc.c')
-rw-r--r--src/bfc.c41
1 files changed, 40 insertions, 1 deletions
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 <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;
}