Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Brainf*ck interpreter (c) 2013 by victorious3
- --Usage: use brainfuck <filename> to run brainf*ck program
- -- add extra "-debug" for testing
- local tArgs = {...}
- local yieldTime = os.clock() --yield timer
- local function yield()
- if os.clock() - yieldTime > 2 then
- os.queueEvent("yield") --post fake "yield" event
- os.pullEvent()
- yieldTime=os.clock() --reset yield timer
- end
- end
- local function run(program, debug)
- local lenght = program:len() --Total program lenght
- local pointers = {} --all memory cells
- local pointer = 1 --currently selected memory cell
- local counter = 1 --program counter
- local jumpers = {} --jumpers for the "]" command
- local jumper = 0 --current jumperlevel
- --fill pointers and jumpers with 0
- for i = 1, 30000 do
- pointers[i] = 0
- jumpers[i] = 0
- end
- while counter ~= lenght + 1 do --while program not at the end
- --current character in the program
- local command = string.sub(program, counter, counter)
- local isCommandValid = true
- if command == "+" then
- pointers[pointer] = pointers[pointer] + 1 --increment current cell
- elseif command == "-" then
- pointers[pointer] = pointers[pointer] - 1 --decrement current cell
- elseif command == ">" then
- if pointer < 30001 then --increment cell pointer
- pointer = pointer + 1
- else
- pointer = 1
- end
- elseif command == "<" then
- if pointer > 1 then --decrement cell pointer
- pointer = pointer - 1
- else
- pointer = 30000
- end
- elseif command == "." then
- write(string.char(pointers[pointer])) --ouput current cell as ASCII character
- elseif command == "[" then
- if pointers[pointer] ~= 0 then --if current cell ~= 0 then add new jumper at jumperlevel
- jumper = jumper + 1
- jumpers[jumper] = counter
- else
- local i = counter + 1
- local j = 1
- while i ~= lenght do
- if string.sub(program, i, i) == "]" then
- j = j - 1
- elseif string.sub(program, i, i) == "[" then
- j = j + 1
- end
- if j == 0 then
- counter = i --if current cell == n�B40 then go to the matching "]"
- break
- end
- i = i + 1
- end
- end
- elseif command == "]" then
- if pointers[pointer] ~= 0 then
- counter = jumpers[jumper] --if current cell ~= 0 then go to matching "[" (jumper)
- else
- jumper = jumper - 1
- end
- elseif command == "," then
- local event, key = os.pullEvent("char") --wait for character input and store ASCII Code in current cell
- pointers[pointer] = string.byte(key)
- else
- isCommandValid = false
- end
- if debug and isCommandValid then
- os.pullEvent() --wait for os event
- write("{"..command.." "..pointer..":"..pointers[pointer].."@"..counter.."} ") --write debug information
- end
- counter = counter + 1 --increment program counter
- yield() --used to prevent "Too long without yielding" - error
- end
- end
- if tArgs[1] == nil then
- error("Argument 1 must be a filename!")
- end
- local file = fs.open(tArgs[1], "r") --open program file
- local debug = tArgs[2] == "-debug"
- if file == nil then
- error("Argument 1 is not a valid file!")
- end
- local program = file.readAll() --read program file into a string
- file.close()
- term.clear()
- term.setCursorPos(1,1)
- run(program, debug) --run the program
- write("\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement