Skip to content

Instantly share code, notes, and snippets.

@Snigelson
Last active January 1, 2016 10:49
Show Gist options
  • Save Snigelson/8133664 to your computer and use it in GitHub Desktop.
Save Snigelson/8133664 to your computer and use it in GitHub Desktop.
Brainf*ck compiler with example file
#!/bin/sh
# Convert brainf*ck to c and compile. Works astonishingly well.
# Usage: $0 < file.bf
sed '1imain(){char*m=calloc(30000,1);
s|[^][+><.,-]||g
s|]|}|g
s|\[|while(*m){|g
s|+|(*m)++;|g
s|-|(*m)--;|g
s|>|m++;|g
s|<|m--;|g
s|\.|putchar(*m);|g
s|,|*m=getchar();|g
$a}'|gcc -xc -O2 -
>>>>+>+++>+++>>>>>+++[
>,+>++++[>++++<-]>[<<[-[->]]>[<]>-]<<[
>+>+>>+>+[<<<<]<+>>[+<]<[>]>+[[>>>]>>+[<<<<]>-]+<+>>>-[
<<+[>]>>+<<<+<+<--------[
<<-<<+[>]>+<<-<<-[
<<<+<-[>>]<-<-<<<-<----[
<<<->>>>+<-[
<<<+[>]>+<<+<-<-[
<<+<-<+[>>]<+<<<<+<-[
<<-[>]>>-<<<-<-<-[
<<<+<-[>>]<+<<<+<+<-[
<<<<+[>]<-<<-[
<<+[>]>>-<<<<-<-[
>>>>>+<-<<<+<-[
>>+<<-[
<<-<-[>]>+<<-<-<-[
<<+<+[>]<+<+<-[
>>-<-<-[
<<-[>]<+<++++[<-------->-]++<[
<<+[>]>>-<-<<<<-[
<<-<<->>>>-[
<<<<+[>]>+<<<<-[
<<+<<-[>>]<+<<<<<-[
>>>>-<<<-<-
]]]]]]]]]]]]]]]]]]]]]]>[>[[[<<<<]>+>>[>>>>>]<-]<]>>>+>>>>>>>+>]<
]<[-]<<<<<<<++<+++<+++[
[>]>>>>>>++++++++[<<++++>++++++>-]<-<<[-[<+>>.<-]]<<<<[
-[-[>+<-]>]>>>>>[.[>]]<<[<+>-]>>>[<<++[<+>--]>>-]
<<[->+<[<++>-]]<<<[<+>-]<<<<
]>>+>>>--[<+>---]<.>>[[-]<<]<
]
[Enter a number using ()-./0123456789abcdef and space, and hit return.
Daniel B Cristofani (cristofdathevanetdotcom)
http://www.hevanet.com/cristofd/brainfuck/]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment