Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- An interpreter to run the Brainfuck language in lua
- -- The size of the memory array is in this example 1024 (1 - 1025)
- local _args = {...}
- local _nSize = 1024
- local _nMaxVal = 255
- local _nVars = {}
- local _nPtr = 1
- local _nChar = 1
- local _sScript
- local _nEntries = {}
- local _usage = [[
- Brainfuck - Usage
- 'bfq <filename>'
- 'bfq :<usage|info|help|test|learn>'
- ]]
- local _info = [[
- Brainfuck - Info
- Brainfuck is an esoteric programming language
- created in 1993 by Urban Mueller, and notable
- for its extreme minimalism.
- The language consists of only eight simple
- commands and an instruction pointer.
- While it is fully Turing-complete, it is not
- intended for practical use, but to challenge
- and amuse programmers.
- https://en.wikipedia.org/wiki/Brainfuck
- 8th of December 2016
- ]]
- local _help = [[
- Brainfuck - Help
- :usage - How to use this program
- :info - Information about Brainfuck
- :help - This message
- :test - Runs a small program
- :learn - Explains how to use Brainfuck
- ]]
- local _test = "Brainfuck - Test\n\n Program output:"
- local _learn = [[
- Brainfuck - Learn
- There are eight commands:
- < - Decrease the pointer
- > - Increase the pointer
- + - Increase the current cell value
- - - Decrease the current cell value
- [ - Declare a loop
- ] - Declare the end of a lopp
- . - Display the current cell value in ASCII
- , - Read the user's input into the cell
- A loop gets executed when the current cell has
- a value greater than zero. Cells overflow and
- underflow. The pointer does so too.
- Cell range: 0-255
- Pointer range: 1-1025
- ]]
- local _notFound = [[
- Brainfuck - Error
- Command/File not found
- Try ':help' instead
- ]]
- -- Several cleaning/ready functions (mostly for aestetic reasons)
- local function _printf(_sInput)
- term.write(_sInput)
- end
- local function _readyAll()
- for i = 1,_nSize do
- _nVars[i] = 0
- end
- _nPtr = 1
- end
- -- The six functions for memory manipulation/output
- -- < > - + ,
- local function _decPtr()
- _nPtr = _nPtr-1
- if _nPtr < 1 then
- _nPtr = _nSize
- end
- end
- local function _nncPtr()
- _nPtr = _nPtr+1
- if _nPtr > _nSize then
- _nPtr = 1
- end
- end
- local function _decVal()
- _nVars[_nPtr] = _nVars[_nPtr] - 1
- if _nVars[_nPtr] < 0 then
- _nVars[_nPtr] = _nMaxVal
- end
- end
- local function _nncVal()
- _nVars[_nPtr] = _nVars[_nPtr] + 1
- if _nVars[_nPtr] > _nMaxVal then
- _nVars[_nPtr] = 0
- end
- end
- local function _getChar()
- local _sInput = read()
- if string.len(_sInput) < 1 then
- _nVars[_nPtr] = 0
- else
- _nVars[_nPtr] = (string.byte(string.sub(_sInput,1,1)) > _nMaxVal and 0)or string.byte(string.sub(_sInput,1,1))
- end
- end
- local function _putChar()
- _printf(string.char(_nVars[_nPtr]))
- end
- -- The remaining functions for the loop
- -- [ ]
- local function _jmpLoop()
- local _nCount = 0
- while _nChar < string.len(_sScript) do
- _sChar = string.sub(_sScript,_nChar,_nChar)
- if _sChar == "[" then
- _nCount = _nCount + 1
- elseif _sChar == "]" then
- _nCount = _nCount -1
- else
- end
- _nChar = _nChar +1
- end
- end
- -- The interpreter
- local function _doString(_sInput)
- _sScript = _sInput
- _readyAll()
- repeat
- c = string.sub(_sScript,_nChar,_nChar)
- if c == nil then
- elseif c=="<" then
- _decPtr()
- elseif c==">" then
- _nncPtr()
- elseif c=="-" then
- _decVal()
- elseif c=="+" then
- _nncVal()
- elseif c=="." then
- _putChar()
- elseif c=="," then
- _getChar()
- elseif c=="[" then
- if _nVars[_nPtr] > 0 then
- _nEntries[#_nEntries + 1] = _nChar
- else
- _jmpLoop()
- end
- elseif c=="]" then
- if _nVars[_nPtr] > 0 then
- _nChar = _nEntries[#_nEntries]
- else
- end
- end
- _nChar = _nChar+1
- until _nChar > string.len(_sScript)
- print("")
- end
- -- The frontend
- local function _commands()
- if _args[1] == ":usage" then
- print(_usage)
- elseif _args[1] == ":info" then
- print(_info)
- elseif _args[1] == ":help" then
- print(_help)
- elseif _args[1] == ":test" then
- print(_test)
- _doString("+++++[->+++++<]>+++++++..<++++++[->++++++<]>+++++.<+++++[->+++++<]>++++.<++++++++[->--------<]>------.<+++++++++[->+++++++++<]>++++++++.<+++[->---<]>-.<>++++++.<+++++++++[->---------<]>----.<+++++++++[->+++++++++<]>++.<+++[->---<]>-----.<>.<++++++++[->--------<]>-----.<+++++++++[->+++++++++<]>+++.<+++[->---<]>---.<>+.<+++[->+++<]>+.<+++++++++[->---------<]>--.<++++++++[->++++++++<]>+++++++++++++.<>--------.<+++[->+++<]>+++++.<>.<++++[->----<]>--.<>++++++.<>--.<+++++++[->-------<]>--------.<+++[->---<]>---.<+++++++++[->+++++++++<]>+++.<+++[->---<]>---.<>---.<++++++++[->--------<]>-----.<++++++++[->++++++++<]>+++++++++.<>+++++.<>++++++.<+++[->---<]>------.<+++[->+++<]>++++.<>--.<>++.<+++[->---<]>----.<+++[->+++<]>++++++.<+++[->---<]>------.<+++[->+++<]>++++.<+++++++++[->---------<]>-.<+++++++++[->+++++++++<]>++++++.<>--------.<>+++.<>-------.<>++++++++.<+++++++++[->---------<]>-.<++++[->----<]>-------.<")
- elseif _args[1] == ":learn" then
- print(_learn)
- else
- print(_notFound)
- end
- end
- local function _run()
- if fs.exists(_args[1]) then
- _fFile = fs.open(_args[1],"r")
- _sFile = _fFile.readAll()
- _fFile.close()
- _doString(_sFile)
- else
- print(_notFound)
- end
- end
- local function _main()
- shell.run("clear")
- if #_args < 1 then
- print(_usage)
- return
- end
- if string.sub(_args[1],1,1) == ":" then
- _commands()
- else
- _run()
- end
- end
- _main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement