Quick actions

cmd+k|ctrl+k

Navigation

Languages

Lua Brainfuck Compiler

Snippet info

Language

Lua

Visibility

public

Author

captain.g

Created

2021-03-04T15:26:46.929949Z

Updated

2021-03-04T15:26:46.929949Z

-- Brainfuck compiler

function brainfuck(src)
    src = src:gsub("%s+","")
    local char_map = {
        [">"] = "i = i+1;";
        ["<"] = "i = i-1;";
        ["+"] = "t[i] = t[i]+1;";
        ["-"] = "t[i] = t[i]-1;";
        [","] = "t[i] = io.read():byte()";
        ["."] = "io.write( string.char( tostring( t[i] ) ) )";
        ["["] = "while t[i] ~= 0 do ";
        ["]"] = "end;";
    }
    local str = ""
    for i=1, src:len() do 
        str = str..char_map[src:sub(i,i)] 
    end
    load("local i,t=1,setmetatable({},{__index=function() return 0 end});"..str)()
end

brainfuck("++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.")
INFO