Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function brainFuck(data, input)
- local pos = 1
- local ptr = 0
- local lvl = 0
- local ram = {}
- local stack = {}
- local input = input or ""
- local result = ""
- local read = 1
- while pos <= #data do
- if ram[ptr] == nil then
- ram[ptr] = 0
- end
- local cmd = data:sub(pos, pos)
- local processed = true
- if cmd == '>' then
- ptr = ptr + 1
- elseif cmd == '<' then
- ptr = ptr - 1
- elseif cmd == '+' then
- ram[ptr] = ram[ptr] + 1
- elseif cmd == '-' then
- ram[ptr] = ram[ptr] - 1
- elseif cmd == '.' then
- local byte = ram[ptr]
- result = result .. string.char(byte)
- elseif cmd == ',' then
- local char = input:sub(read, read)
- ram[ptr] = char:byte()
- read = read + 1
- elseif cmd == '[' then
- if ram[ptr] > 0 then
- lvl = lvl + 1
- stack[lvl] = pos
- else
- pos = select(2, data:find("%b[]", pos))
- end
- elseif cmd == ']' then
- if ram[ptr] > 0 then
- pos = stack[lvl]
- else
- lvl = lvl - 1
- end
- end
- pos = pos + 1
- end
- print(result)
- end
Add Comment
Please, Sign In to add comment