Last active
July 30, 2017 16:43
-
-
Save ConorOBrien-Foxx/f26c1a1b63adcd9c524d1c8b33269022 to your computer and use it in GitHub Desktop.
Ruby BF compiler
This file contains 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
preamble = <<'EOF' | |
#include <stdint.h> | |
#include <stdio.h> | |
int main(){ | |
#define S 65536 | |
uint8_t t[S] = {0}; | |
uint8_t m[S] = {0}; | |
uint16_t p = 0, f = 0, z = 0; | |
int i, q = 0, d = 0, g = 0; | |
#define D for(z=0;z<12+(f+11)/12*12;){if(z%12==0){printf("%04i: ",z);}if(z==p)printf("\33[32;1m");if(m[z])printf("\33[41m");printf("%03i\33[0m ",t[z]);if(++z%12==0){for(q=0;q<12;q++){g=z-12+q;printf("\33[30;%im",g==p?46:m[g]?41:47);printf("%c",(i=t[z-12+q])<33||i>126?46:i);}puts("\33[0m");}} | |
EOF | |
$line_number = 1 | |
$col_number = 1 | |
$output = preamble | |
def echo(str) | |
$output += str + "\n" | |
end | |
File.read(ARGV[0]).chars.each { |char| | |
if char == "\n" | |
$line_number += 1 | |
$col_number = 1 | |
next | |
end | |
case char | |
when ">" | |
echo '++p;f<=p&&(f=p);' | |
when "<" | |
echo '--p;' | |
when "+" | |
echo '++t[p];' | |
when "-" | |
echo '--t[p];' | |
when "," | |
echo 'i=getchar(),t[p]=i*(i>0);' | |
when "." | |
echo 'putchar(t[p]);' | |
when "[" | |
echo 'while(t[p]){' | |
when "]" | |
echo '}' | |
when "#" | |
echo "printf(\"\\33[33mDump %i: (at line %i, char %i)\\33[0m\\n\", d++, #{$line_number}, #{$col_number}); D" | |
when "@" | |
echo "m[p]=1-m[p];" | |
end | |
$col_number += 1 | |
} | |
echo "printf(\"\\33[33mFinal dump:\\33[0m\\n\");D;return 0;}" | |
File.write "#{ARGV[0]}.c", $output | |
`cc -O3 #{ARGV[0]}.c -o #{ARGV[0]}.exe` | |
system("./#{ARGV[0]}.exe") | |
# cc -O3 -o "$1.exe" "$1.c" | |
# "./$1.exe" | |
# rm "$1.c" | |
# ./test.bat "$1" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment