Execute Brain****/Lua: Difference between revisions

m
Fixed syntax highlighting.
(Added RCBF templates....do we still need these?)
m (Fixed syntax highlighting.)
 
(6 intermediate revisions by 4 users not shown)
Line 1:
{{implementation|Brainf***}}{{collection|RCBF}}
<lang lua>
memory = {0} --memory is bounded on one side, at 1
 
An implementation of a Brainf*** interpreter in [[Lua]].
program = io.read() --loads an entire program at once, must not contain newlines
<syntaxhighlight lang="lua">memory = {0} --memory is bounded on one side, at 1
 
program = io.read("*all")
 
pointer = 1
Line 29 ⟶ 30:
if memory[pointer] ~= 0 then
table.insert(retpoints, instruction)
else -- if the memory at the pointer is zero, jump to the matching close bracket
else
local b = 1 -- b stores number of unclosed brackets (when b == 0 the match has been found)
while program:sub(instruction, instruction) ~= "]" do
while instruction <= instruction#program +and 1b --if the pointer is not zero, jump to the next~= close0 bracketdo
instruction = retpoints[#retpoints]instruction + 1
while if program:sub(instruction, instruction) ~== "][" dothen
b = b + 1
elseif program:sub(instruction, instruction) == "]" then
b = b - 1
end
end
if b ~= 0 then
error"Missing ']'!"
end
end
end,
["]"] = function()
if memory[pointer]#retpoints ~=> 0 then
if memory[pointer] ~= 0 then
instruction = retpoints[#retpoints]
instruction = retpoints[#retpoints]
else
table.remove(retpoints)
end
else
error"Missing '['!"
table.remove(retpoints)
end
end,
Line 52 ⟶ 66:
if instr then instr() end
instruction = instruction + 1
end</langsyntaxhighlight>
9,483

edits