Skip to content

Instantly share code, notes, and snippets.

@shmup
Created January 6, 2013 00:04
Show Gist options
  • Save shmup/4464419 to your computer and use it in GitHub Desktop.
Save shmup/4464419 to your computer and use it in GitHub Desktop.
A simple brainfuck interpretor in Lua.
program = [[ >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-
]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+
++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.]]
p, counter, marker = 0, 1, 1
cells = {}
for i = 1, 30000 do
cells[i] = 0
end
repeat
local c = string.sub(program, counter, counter)
if c == '>' then
marker = marker + 1
elseif c == '<' and marker ~= 1 then
marker = marker - 1
elseif c == '+' then
cells[marker] = cells[marker] + 1
elseif c == '-' then
if cells[marker] ~= 0 then cells[marker] = cells[marker] - 1 end
elseif c == '[' then
p = counter
elseif c == ']' then
if cells[marker] ~= 0 then counter = p end
elseif c == '.' then
io.write(string.char(cells[marker]))
elseif c == ',' then
cells[marker] = string.byte(io.read())
end
counter = counter + 1
until counter == #program + 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment