Skip to content

Instantly share code, notes, and snippets.

@starius
Last active August 29, 2015 14:21
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 starius/d9e81b83f078277d48b1 to your computer and use it in GitHub Desktop.
Save starius/d9e81b83f078277d48b1 to your computer and use it in GitHub Desktop.
Brainfuck to C translator written in Lua
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