Created
January 21, 2020 03:58
-
-
Save ewpratten/44a2659dfe8ce6739a85bc34bc350f9b to your computer and use it in GitHub Desktop.
My bash BrainFuck compiler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
# Evan Pratten's Bash BrainFuck compiler | |
# Duplicate input file | |
cat $1 >inp.bf | |
# Strip comments | |
sed -i -e 's/[A-Za-z]*//g' inp.bf | |
# Incr and decr break the simple sed. Use temps instead | |
sed -i -e 's/+/%/g' inp.bf | |
sed -i -e 's/-/$/g' inp.bf | |
# Handle regex for all parts of file | |
sed -i -e 's/>/++ptr;/g' inp.bf | |
sed -i -e 's/</--ptr;/g' inp.bf | |
sed -i -e 's/%/++*ptr;/g' inp.bf | |
sed -i -e 's/\$/--*ptr;/g' inp.bf | |
sed -i -e 's/\./putchar(*ptr);/g' inp.bf | |
sed -i -e 's/,/*ptr=getchar();/g' inp.bf | |
sed -i -e 's/\[/while (*ptr){/g' inp.bf | |
sed -i -e 's/\]/}/g' inp.bf | |
# Create a temp file with first half of the C wrapper | |
echo "#include <stdio.h>" >outp.c | |
echo "int main(int argc, char const *argv[]) {char array[30000] = {0};char *ptr = array;" >>outp.c | |
# Add generated C code | |
cat inp.bf >>outp.c | |
# Add C footer | |
echo "return *ptr;}" >>outp.c | |
# Compile to binary | |
gcc outp.c | |
# Clean up files | |
rm inp.bf | |
rm outp.c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works wonderfully. Thanks! :)