Advertisement
Freack100

L33t Interpreter

Jul 23rd, 2015
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.89 KB | None | 0 0
  1. local args = {...}
  2. local memory = {}
  3. local instPointer = 1
  4. local memPointer = 1
  5. local running = true
  6.  
  7. local function incInst(i)
  8.     instPointer = instPointer+1
  9. end
  10.  
  11. local function decInst(i)
  12.     instPointer = instPointer-1
  13. end
  14.  
  15. local function incMemPoint(i)
  16.     memPointer = memPointer+(i and i or 1)
  17. end
  18.  
  19. local function decMemPoint(i)
  20.     memPointer = memPointer-(i and i or 1)
  21. end
  22.  
  23. local function incMem(i)
  24.     if(memory[memPointer] == nil) then memory[memPointer] = 0 end
  25.     memory[memPointer] = memory[memPointer]+(i and i or 1)
  26. end
  27.  
  28. local function decMem(i)
  29.     if(memory[memPointer] == nil) then memory[memPointer] = 0 end
  30.     memory[memPointer] = memory[memPointer]-(i and i or 1)
  31. end
  32.  
  33. local function getCurMem()
  34.     return memory[memPointer]
  35. end
  36.  
  37. local function getCurInst()
  38.     return memory[instPointer]
  39. end
  40.  
  41. local function wrt()
  42.     write(string.char(getCurMem()))
  43. end
  44.  
  45. local opcodes = {
  46.     NOP = 0,
  47.     WRT = 1,
  48.     RD = 2,
  49.     IF = 3,
  50.     EIF = 4,
  51.     FWD = 5,
  52.     BAK = 6,
  53.     INC = 7,
  54.     DEC = 8,
  55.     CON = 9,
  56.     END = 10
  57. }
  58.  
  59. local operations = {
  60.     [opcodes.NOP] = function()
  61.         incInst()
  62.     end,
  63.     [opcodes.WRT] = function()
  64.         wrt()
  65.         incInst()
  66.     end,
  67.     [opcodes.RD] = function()
  68.         memory[memPointer] = ((read()):sub(1,1)):byte()
  69.         incInst()
  70.     end,
  71.     [opcodes.IF] = function()
  72.         if(getCurMem() == 0) then
  73.             local found = false
  74.             while not found do
  75.                 incInst()
  76.                 if(getCurInst() == opcodes.EIF) then
  77.                     found = true
  78.                 end
  79.             end
  80.             incInst()
  81.         else
  82.             incInst()
  83.         end
  84.     end,
  85.     [opcodes.EIF] = function()
  86.         if(getCurMem() ~= 0) then
  87.             local found = false
  88.             while not found do
  89.                 decInst()
  90.                 if(getCurInst() == opcodes.IF) then
  91.                     found = true
  92.                 end
  93.             end
  94.             incInst()
  95.         else
  96.             incInst()
  97.         end
  98.     end,
  99.     [opcodes.FWD] = function()
  100.         incInst()
  101.         incMemPoint(getCurInst()+1)
  102.         incInst()
  103.     end,
  104.     [opcodes.BAK] = function()
  105.         incInst()
  106.         decMemPoint(getCurInst()+1)
  107.         incInst()
  108.     end,
  109.     [opcodes.INC] = function()
  110.         incInst()
  111.         incMem(getCurInst()+1)
  112.         incInst()
  113.     end,
  114.     [opcodes.DEC] = function()
  115.         incInst()
  116.         decMem(getCurInst()+1)
  117.         incInst()
  118.     end,
  119.     [opcodes.CON] = function()
  120.         --Nope
  121.     end,
  122.     [opcodes.END] = function()
  123.         running = false
  124.     end
  125. }
  126.  
  127. if(#args == 0) then error("N0p3.",0) end
  128. if(not fs.exists(args[1])) then error("L0L!!1!1!! n0 l33t pr0gr4m l04d3d, sUxX0r!",0) end
  129.  
  130. local lines = {}
  131. local handle = fs.open(shell.resolve(args[1]),"r")
  132. local content = handle.readAll()
  133. handle.close()
  134.  
  135. for line in content:gmatch("[^ \n]+") do
  136.     if line:sub(0,2) ~= "//" then
  137.         for word in line:gmatch("[^ \t]+") do
  138.             local i = 0
  139.             for ch in word:gmatch(".") do
  140.                 if(tonumber(ch)) then
  141.                     i = i+tonumber(ch)
  142.                 end
  143.             end
  144.             memory[#memory+1] = i
  145.         end
  146.     end
  147. end
  148.  
  149. memPointer = #memory+1
  150. memory[memPointer] = 0
  151.  
  152. while running do
  153.     local opc = getCurInst()
  154.     if(opc > 10) then
  155.         error("F00l! teh c0d3 c4n'7 b3 0v3r t3n!!!1!",0)
  156.     end
  157.     operations[opc]()
  158. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement