Advertisement
Bolodefchoco_LUAXML

[Function] Brainfuck

Jul 16th, 2016
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.75 KB | None | 0 0
  1. --Creator: Bolodefchoco
  2. --Made in: 16/07/2016
  3. --Last update: 16/07/2016
  4. --[[ Notes:
  5.     Does:
  6.         Compila a linguagem brainfuck
  7.     Args:
  8.         s --> Retorna a string, output deve ser em brainfuck
  9.     Example:
  10.         print(brainfuck.compile("++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++ .>+.+++++++..+++.>++.<<+++++++++++++++.>.+++. ------.--------.>+.>."))
  11.     Brainfuck list:
  12.         > - Incrementa o ponteiro (acessa a célula de memória seguinte)
  13.         < - Decrementa o ponteiro (acessa a célula de memória anterior)
  14.         + - Incrementa em um, o valor da célula de memória selecionada
  15.         - - Decrementa em um, o valor da célula de memória selecionada
  16.         [ - Inicia um loop até o ponteiro virar 0
  17.         ] - Finaliza o loop
  18.         , - Salva na célula de memória selecionada o código da próxima tecla a ser pressionada
  19.         . - Imprime na tela o caractere relativo à célula de memória selecionada
  20. ]]--
  21.  
  22. brainfuck = {}
  23.  
  24. brainfuck.errorC = {
  25.     [1] = "Closing brackets are missed!",
  26.     [2] = "Openin brackets are missed!",
  27.     [3] = "No source!",
  28.     [4] = "Unknown error"
  29. }
  30.  
  31. brainfuck.verify = function(s)
  32.     local error,l = 0,0
  33.     for i = 1,#s,1 do
  34.         if s:byte(i) == 91 then
  35.             l = l + 1
  36.         elseif s:byte(i) == 93 then
  37.             l = l - 1
  38.             if l < 0 then
  39.                 return 2
  40.             end
  41.         end
  42.     end
  43.     if l > 0 then
  44.         return 1
  45.     elseif l < 0 then
  46.         return 2
  47.     else
  48.         return 0
  49.     end
  50. end
  51.  
  52. brainfuck.error = function(x)
  53.     error(brainfuck.errorC[x or 4])
  54. end
  55.  
  56. brainfuck.token = function(s)
  57.     local size,max,m,p,l = 3e4,255,{},0,0,0
  58.     local toReturn = {}
  59.     for i = 0,size,1 do
  60.         m[i] = 0
  61.     end
  62.     i = 0
  63.     while i <= #s do
  64.         i = i + 1
  65.         if s:byte(i) == 43 then
  66.             if m[p] < max then
  67.                 m[p] = m[p] + 1
  68.             end
  69.         elseif s:byte(i) == 45 then
  70.             if m[p] > 0 then
  71.                 m[p] = m[p] - 1
  72.             end
  73.         elseif s:byte(i) == 44 then
  74.             local copy = s:match("(.-)\n")
  75.             m[p] = copy:byte(1)
  76.         elseif s:byte(i) == 46 then
  77.             table.insert(toReturn,tostring(m[p]):char())
  78.         elseif s:byte(i) == 60 then
  79.             p = p - 1
  80.             if p < 0 then
  81.                 p = 0
  82.             end
  83.         elseif s:byte(i) == 62 then
  84.             p = p + 1
  85.             if p > size then
  86.                 p = size
  87.             end
  88.         elseif s:byte(i) == 91 then
  89.             if m[p] == 0 then
  90.                 while s:byte(i)~=93 or l>0 do
  91.                     i = i + 1
  92.                     if s:byte(i) == 91 then
  93.                         l = l + 1
  94.                     end
  95.                     if s:byte(i) == 93 then
  96.                         l = l - 1
  97.                     end
  98.                 end
  99.             end
  100.         elseif s:byte(i) == 93 then
  101.             if m[p] ~= 0 then
  102.                 while s:byte(i)~=91 or l>0 do
  103.                     i = i - 1
  104.                     if s:byte(i) == 91 then
  105.                         l = l - 1
  106.                     end
  107.                     if s:byte(i) == 93 then
  108.                         l = l + 1
  109.                     end
  110.                 end
  111.             end
  112.         end
  113.     end
  114.     return table.concat(toReturn,nil,1,#toReturn-1)
  115. end
  116.  
  117. brainfuck.compile = function(s)
  118.     local error = brainfuck.verify(s)
  119.     if error == 0 then
  120.         return brainfuck.token(s)
  121.     else
  122.         brainfuck.error(error)
  123.     end
  124. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement