Execute Brain****/Lua: Difference between revisions
Content added Content deleted
No edit summary |
(Fixed bracket matching and made them a bit more robust.) |
||
Line 1: | Line 1: | ||
{{implementation|Brainf***}}{{collection|RCBF}} |
{{implementation|Brainf***}}{{collection|RCBF}} |
||
{{incorrect|Lua|Instead of jumping to the *next* close bracket when reaching a [ and memory[pointer] == 0, it should jump to the next *matching* close bracket such that embedded loops work}} |
|||
An implementation of a Brainf*** interpreter in [[Lua]]. |
An implementation of a Brainf*** interpreter in [[Lua]]. |
||
<lang lua>memory = {0} --memory is bounded on one side, at 1 |
<lang lua>memory = {0} --memory is bounded on one side, at 1 |
||
Line 30: | Line 30: | ||
if memory[pointer] ~= 0 then |
if memory[pointer] ~= 0 then |
||
table.insert(retpoints, instruction) |
table.insert(retpoints, instruction) |
||
else --if the pointer is zero, jump to the |
else -- if the memory at the pointer is zero, jump to the matching close bracket |
||
local b = 1 -- b stores number of unclosed brackets (when b == 0 the match has been found) |
|||
⚫ | |||
while instruction <= #program and b ~= 0 do |
|||
instruction = instruction + 1 |
instruction = instruction + 1 |
||
⚫ | |||
b = b + 1 |
|||
elseif program:sub(instruction, instruction) == "]" |
|||
b = b - 1 |
|||
end |
|||
end |
|||
if b ~= 0 then |
|||
error"Missing ']'!" |
|||
end |
end |
||
end |
end |
||
end, |
end, |
||
["]"] = function() |
["]"] = function() |
||
if |
if #retpoints > 0 then |
||
if memory[pointer] ~= 0 then |
|||
instruction = retpoints[#retpoints] |
instruction = retpoints[#retpoints] |
||
else |
|||
⚫ | |||
end |
|||
else |
else |
||
error"Missing '['!" |
|||
⚫ | |||
end |
end |
||
end, |
end, |
Revision as of 19:24, 15 February 2012
Execute Brain****/Lua is an implementation of Brainf***.
Other implementations of Brainf***.
Execute Brain****/Lua is part of RCBF. You may find other members of RCBF at Category:RCBF.
An implementation of a Brainf*** interpreter in Lua. <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 -- if the memory at the pointer is zero, jump to the matching close bracket local b = 1 -- b stores number of unclosed brackets (when b == 0 the match has been found) while instruction <= #program and b ~= 0 do instruction = instruction + 1 if program:sub(instruction, instruction) == "[" then b = b + 1 elseif program:sub(instruction, instruction) == "]" b = b - 1 end end if b ~= 0 then error"Missing ']'!" end end
end, ["]"] = function()
if #retpoints > 0 then if memory[pointer] ~= 0 then instruction = retpoints[#retpoints] else table.remove(retpoints) end else error"Missing '['!" 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>