Created
January 6, 2013 00:04
-
-
Save shmup/4464419 to your computer and use it in GitHub Desktop.
A simple brainfuck interpretor in Lua.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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