Advertisement
GravityScore

Brainfuck Interpreter in Lua

Apr 28th, 2013
648
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.02 KB | None | 0 0
  1.  
  2. --  Brainfuck Interpreter
  3. --  Written by GravityScore
  4.  
  5.  
  6. --  Variables
  7.  
  8. local w, h = term.getSize()
  9. local args = {...}
  10.  
  11. local loopLocations = {}
  12. local dataCells = {}
  13. local dataPointer = 1
  14. local instructionPointer = 1
  15.  
  16.  
  17. -- Not enough arguments
  18. if #args < 1 then
  19.     print("Usage:")
  20.     print("brainfuck <path>")
  21.     error()
  22. end
  23.  
  24. -- Get path
  25. local path = "/" .. shell.resolve(args[1])
  26. if not fs.exists(path) then
  27.     print("File does not exist!")
  28.     error()
  29. elseif fs.isDir(path) then
  30.     print("File is a directory!")
  31.     error()
  32. end
  33.  
  34. -- Get file contents
  35. local f = io.open(path, "r")
  36. local content = f:read("*a")
  37. f:close()
  38.  
  39.  
  40. -- Find all loops
  41. local loc = 1
  42. local line = 1
  43. for let in content:gmatch(".") do
  44.     if let == "[" then
  45.         loopLocations[loc] = true
  46.     elseif let == "]" then
  47.         local found = false
  48.         for i = loc, 1, -1 do
  49.             if loopLocations[i] == true then
  50.                 loopLocations[i] = loc
  51.                 found = true
  52.             end
  53.         end
  54.  
  55.         if not found then
  56.             print(fs.getName(path) .. ":" .. line .. ": No matching ']'")
  57.         end
  58.     end
  59.  
  60.     if let == "\n" then line = line + 1 end
  61.     loc = loc + 1
  62. end
  63.  
  64. -- Run
  65. while true do
  66.     local let = content:sub(instructionPointer, instructionPointer)
  67.  
  68.     if let == ">" then
  69.         dataPointer = dataPointer + 1
  70.         if not dataCells[tostring(dataPointer)] then dataCells[tostring(dataPointer)] = 0 end
  71.     elseif let == "<" then
  72.         dataPointer = dataPointer - 1
  73.         if not dataCells[tostring(dataPointer)] then dataCells[tostring(dataPointer)] = 0 end
  74.     elseif let == "+" then
  75.         if not dataCells[tostring(dataPointer)] then dataCells[tostring(dataPointer)] = 0 end
  76.         dataCells[tostring(dataPointer)] = dataCells[tostring(dataPointer)] + 1
  77.     elseif let == "-" then
  78.         if not dataCells[tostring(dataPointer)] then dataCells[tostring(dataPointer)] = 0 end
  79.         dataCells[tostring(dataPointer)] = dataCells[tostring(dataPointer)] - 1
  80.     elseif let == "." then
  81.         if not dataCells[tostring(dataPointer)] then dataCells[tostring(dataPointer)] = 0 end
  82.         if term.getCursorPos() >= w then print("") end
  83.         write(string.char(dataCells[tostring(dataPointer)]))
  84.     elseif let == "," then
  85.         if not dataCells[tostring(dataPointer)] then dataCells[tostring(dataPointer)] = 0 end
  86.         term.setCursorBlink(true)
  87.         local e, but = os.pullEvent("char")
  88.         term.setCursorBlink(false)
  89.         dataCells[tostring(dataPointer)] = string.byte(but)
  90.         if term.getCursorPos() >= w then print("") end
  91.         write(but)
  92.     elseif let == "/" then
  93.         if not dataCells[tostring(dataPointer)] then dataCells[tostring(dataPointer)] = 0 end
  94.         if term.getCursorPos() >= w then print("") end
  95.         write(dataCells[tostring(dataPointer)])
  96.     elseif let == "[" then
  97.         if dataCells[tostring(dataPointer)] == 0 then
  98.             for k, v in pairs(loopLocations) do
  99.                 if k == instructionPointer then instructionPointer = v end
  100.             end
  101.         end
  102.     elseif let == "]" then
  103.         for k, v in pairs(loopLocations) do
  104.             if v == instructionPointer then instructionPointer = k - 1 end
  105.         end
  106.     end
  107.  
  108.     instructionPointer = instructionPointer + 1
  109.     if instructionPointer > content:len() then print("") break end
  110. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement