Last active
August 29, 2015 14:21
-
-
Save starius/d9e81b83f078277d48b1 to your computer and use it in GitHub Desktop.
Brainfuck to C translator written in Lua
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
local brainfuckToC_map = { | |
['>'] = '++ptr;', | |
['<'] = '--ptr;', | |
['+'] = '++*ptr;', | |
['-'] = '--*ptr;', | |
['.'] = 'putchar(*ptr);', | |
[','] = '*ptr=getchar();', | |
['['] = 'while (*ptr) {', | |
[']'] = '}', | |
} | |
local function brainfuckToC(bf_code) | |
local c_code = bf_code:gsub('.', function(op) | |
return brainfuckToC_map[op] or '' | |
end) | |
return c_code | |
end | |
local PROLOGUE = [[ | |
#include <stdio.h> | |
int main() { | |
#define TAPE_SIZE 30000 | |
char array[TAPE_SIZE] = {0}; | |
char *ptr=array; | |
]] | |
local EPILOGUE = [[ | |
return 0; | |
} | |
]] | |
print(PROLOGUE) | |
for line in io.lines() do | |
print(brainfuckToC(line)) | |
end | |
print(EPILOGUE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment