Advertisement
Guest User

eval.lua

a guest
Jul 21st, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.05 KB | None | 0 0
  1. --(C) 2017 Taigaclaw some rights reserved (licenced WTFPL).
  2.  
  3. function table.slice(tbl, first, last, step)
  4.     local sliced = {}
  5.     for i = first or 1, last or #tbl, step or 1 do
  6.         sliced[#sliced+1] = tbl[i]
  7.     end
  8.     return sliced
  9. end
  10.  
  11. function split(s)
  12.     s = s:gsub('%s+', '')
  13.     local parts = {}
  14.     local i = 1
  15.     while i <= #s do
  16.         local char = s:sub(i, i)
  17.         if tonumber(char) or char == '.' then
  18.             if tonumber(parts[#parts]) and parts[#parts] ~= '-' then
  19.                 parts[#parts] = parts[#parts]..char
  20.             else
  21.                 table.insert(parts, char)
  22.             end
  23.         else
  24.             table.insert(parts, char)
  25.         end
  26.         i = i + 1
  27.     end
  28.     return parts
  29. end
  30.  
  31. function evaluate(e)
  32.     if type(e) ~= 'table' then
  33.         e = split(e)
  34.     end
  35.     local total = 0
  36.     local scan = true
  37.     local i = 0
  38.     while i <= #e do
  39.         local char = e[i]
  40.         if scan == true then
  41.             if char == '(' then
  42.                 local scope = 0
  43.                 local starter = i
  44.                 local ender = 0
  45.                 local tape = i
  46.                 while true do
  47.                     if     e[tape] == '(' then
  48.                         scope = scope + 1
  49.                     elseif e[tape] == ')' then
  50.                         scope = scope - 1
  51.                         if scope == 0 then
  52.                             ender = tape
  53.                             break
  54.                         end
  55.                     end
  56.                     tape = tape + 1
  57.                 end
  58.                 local ans = evaluate(table.slice(e, starter + 1, ender - 1, 1))
  59.                 local first = table.slice(e, 1, starter - 1, 1)
  60.                 local last = table.slice(e, ender + 1, #e, 1)
  61.                 e = {}
  62.                 for _, i in ipairs(first) do
  63.                     table.insert(e, i)
  64.                 end
  65.                 table.insert(e, tostring(ans))
  66.                 for _, i in ipairs(last) do
  67.                     table.insert(e, i)
  68.                 end
  69.             end
  70.             i = i + 1
  71.             if i >= #e then
  72.                 i = 1
  73.                 scan = false
  74.             end
  75.         else
  76.             if char == '+' then
  77.                 total = total + e[i + 1]
  78.                 i = i + 1
  79.             elseif char == '-' then
  80.                 total = total - e[i + 1]
  81.                 i = i + 1
  82.             elseif char == '/' then
  83.                 total = total / e[i + 1]
  84.                 i = i + 1
  85.             elseif char == '*' then
  86.                 total = total * e[i + 1]
  87.                 i = i + 1
  88.             elseif char == '%' then
  89.                 total = total % e[i + 1]
  90.                 i = i + 1
  91.             else
  92.                 total = e[i]
  93.             end
  94.             i = i + 1
  95.         end
  96.     end
  97.     return total
  98. end
  99.  
  100. print(evaluate('1 + 1'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement