Execute Brain****/Lua: Difference between revisions
Content added Content deleted
(Created page with '<lang lua> memory = {0} --memory is bounded on one side, at 1 program = io.read"*a" --loads an entire program at once pointer = 1 instruction = 1 retpoints = {} functions = …') |
No edit summary |
||
Line 2: | Line 2: | ||
memory = {0} --memory is bounded on one side, at 1 |
memory = {0} --memory is bounded on one side, at 1 |
||
program = io.read |
program = io.read() --loads an entire program at once, must not contain newlines |
||
pointer = 1 |
pointer = 1 |
Revision as of 04:17, 29 January 2010
<lang lua> memory = {0} --memory is bounded on one side, at 1
program = io.read() --loads an entire program at once, must not contain newlines
pointer = 1
instruction = 1
retpoints = {}
functions = { [">"] = function()
pointer = pointer + 1 if not memory[pointer] then memory[pointer] = 0 end
end, ["<"] = function()
if pointer == 1 then error"Memory out-of-bounds!" end pointer = pointer - 1
end, ["+"] = function()
memory[pointer] = memory[pointer] + 1
end, ["-"] = function()
memory[pointer] = memory[pointer] - 1
end, ["["] = function()
if memory[pointer] ~= 0 then table.insert(retpoints, instruction) else while program:sub(instruction, instruction) ~= "]" do instruction = instruction + 1 --if the pointer is not zero, jump to the next close bracket end end
end, ["]"] = function()
if memory[pointer] ~= 0 then instruction = retpoints[#retpoints] else table.remove(retpoints) end
end, ["."] = function()
io.write(string.char(memory[pointer]))
end, [","] = function()
memory[pointer] = io.read():byte()
end} while instruction <= #program do
local instr = functions[program:sub(instruction,instruction)] if instr then instr() end instruction = instruction + 1
end</lang>