local program = [[ ++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.]] local tape = {} local tapePointer = 1 local output = "" for i = 1, 30000 do tape[i] = 0 end local function interpretBF(code) local codePointer = 1 local loopStack = {} while codePointer <= #code do local instruction = string.sub(code, codePointer, codePointer) if instruction == ">" then tapePointer = tapePointer + 1 elseif instruction == "<" then tapePointer = tapePointer - 1 elseif instruction == "+" then tape[tapePointer] = tape[tapePointer] + 1 elseif instruction == "-" then tape[tapePointer] = tape[tapePointer] - 1 elseif instruction == "." then output = output .. string.char(tape[tapePointer]) elseif instruction == "[" then if tape[tapePointer] == 0 then local count = 1 while count > 0 do codePointer = codePointer + 1 local nextInstruction = string.sub(code, codePointer, codePointer) if nextInstruction == "[" then count = count + 1 elseif nextInstruction == "]" then count = count - 1 end end else table.insert(loopStack, codePointer) end elseif instruction == "]" then if tape[tapePointer] ~= 0 then codePointer = loopStack[#loopStack] else table.remove(loopStack, #loopStack) end end codePointer = codePointer + 1 end end interpretBF(program) print(output)