Skip to content

Instantly share code, notes, and snippets.

@ConorOBrien-Foxx
Created May 5, 2017 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ConorOBrien-Foxx/cca95c7d8c4c1a66b8aa3ecc2e379dd3 to your computer and use it in GitHub Desktop.
Save ConorOBrien-Foxx/cca95c7d8c4c1a66b8aa3ecc2e379dd3 to your computer and use it in GitHub Desktop.
random BF transpiler
#!/usr/bin/env bash
cat > "$1.c" << EOF
#include <stdint.h>
#include <stdio.h>
#include <time.h>
int main(){
srand(time(NULL));
uint8_t t[65536] = {0};
uint16_t p = 0;
int i;
EOF
while read -n1 char; do
case "$char" in
\>) echo '++p;';;
\<) echo '--p;';;
\+) echo '++t[p];';;
\-) echo '--t[p];';;
\,) echo 'i=getchar(),t[p]=i*(i>0);';;
\.) echo 'putchar(t[p]);';;
\[) echo 'while(t[p]){';;
\]) echo '}';;
\?) echo 't[p]=rand();';;
esac
done < "$1" >> "$1.c"
echo 'return 0;}' >> "$1.c"
cc -O3 -o "$1.out" "$1.c" && "./$1.out"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment