imring

BrainFuck Gen

Dec 21st, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.12 KB | None | 0 0
  1. --------------------------------------------------------------------
  2.  
  3. local byte, len, char, sub = string.byte, string.len, string.char, string.sub
  4. local floor, log10, ceil = math.floor, math.log10, math.ceil
  5. local concat = table.concat
  6.  
  7. local function numbs(number) -- math.numbs
  8.   return floor(log10(number)) + 1
  9. end
  10.  
  11. local function first(number, numeral) -- math.first
  12.   numeral = numeral or 1
  13.   local numbs = numbs(number)
  14.   return floor(number / ( 10 ^ ( numbs - numeral ) ))
  15. end
  16.  
  17. local function last(number, numeral) -- math.last
  18.   numeral = numeral or 1
  19.   return number % ( 10 ^ numeral )
  20. end
  21.  
  22. --------------------------------------------------------------------
  23.  
  24. local function get_numb(char)
  25.   local a = first(char, numbs(char) - 1)
  26.   local b = last(char)
  27.   if b > 5 then a = a + 1 end
  28.   return a
  29. end
  30.  
  31. local function find_table_value(tab, value)
  32.   for i = 1, #tab do
  33.     if tab[i] == value then return i end
  34.   end
  35. end
  36.  
  37. --------------------------------------------------------------------
  38.  
  39. local params, chars = {}, {}
  40.  
  41. if not _ARGS then return end
  42. local text = { _ARGS }
  43. local ret = '++++++++++['
  44.  
  45. local h, l = 0, 0
  46.  
  47. text = Utf8ToAnsi(concat(text, ' '))
  48.  
  49. for i = 1, #text do
  50.   local ch = byte(text, i)
  51.   local a = get_numb(ch)
  52.   if not find_table_value(params, a) then
  53.     params[#params + 1] = a
  54.   end
  55. end
  56.  
  57. for i = 1, #params do
  58.   h = h + 1
  59.   ret = ret .. '>'
  60.   for l = 1, params[i] do ret = ret .. '+' end
  61. end
  62.  
  63. for i = 1, h do
  64.   ret = ret .. '<'
  65. end
  66.  
  67. ret = ret .. '-]'
  68.  
  69. for i = 1, #text do
  70.   local ch = byte(text, i)
  71.   local a = get_numb(ch)
  72.   local m = find_table_value(params, a)
  73.   if m then
  74.     if not chars[m] then chars[m] = a end
  75.     while l ~= m do
  76.       if l > m then
  77.         l = l - 1
  78.         ret = ret .. '<'
  79.       elseif l < m then
  80.         l = l + 1
  81.         ret = ret ..'>'
  82.       end
  83.     end
  84.     local d = chars[m] * 10
  85.     while d ~= ch do
  86.       if d > ch then
  87.         d = d - 1
  88.         ret = ret .. '-'
  89.       elseif d < ch then
  90.         d = d + 1
  91.         ret = ret .. '+'
  92.       end
  93.     end
  94.     chars[m] = d / 10
  95.     ret = ret .. '.'
  96.   end
  97. end
  98.  
  99. return ret
Add Comment
Please, Sign In to add comment