Documentation for this module may be created at Module:Sandbox/AnataBakka2/doc
-- <pre>
local p = {}
local lib = require('Module:Feature')
local iter = 50000
function p.test(frame)
local args; if frame.args == nil then args = lib.arguments(frame) else args = lib.arguments(frame.args) end
return expr(args[1])
end
function expr(val)
if string.find(val, "[0-9]") == nil then
return val
end
val = string.gsub(val," ","")
local symbol={
["^"]=1,
["/"]=2,
["*"]=3,
["+"]=4,
["-"]=5,
[")"]=6,
["("]=7
}
local getmath = {
["^"] = function (a,b) return a^b end,
["/"] = function (a,b) return a/b end,
["*"] = function (a,b) return a*b end,
["+"] = function (a,b) return a+b end,
["-"] = function (a,b) return a-b end
}
local symbolstack = {}
local numberstack = {}
local term=""
for i=1,#val,1 do
term = term .. string.sub(val,i,i)
if symbol[term] == nil or ((term == "-" or term == "+") and (symbol[string.sub(val,i-1,i-1)] ~= nil or i-1 == 0) and string.sub(val,i-1,i-1) ~= ")") then
if symbol[string.sub(val,i+1,i+1)] ~= nil or i == #val then
table.insert(numberstack,term)
term=""
end
else
if symbolstack[#symbolstack] ~= nil and symbol[symbolstack[#symbolstack]] < symbol[term] and term ~= "(" and term ~= ")" then
table.insert(numberstack,symbolstack[#symbolstack])
symbolstack[#symbolstack] = term
else
table.insert(symbolstack,term)
end
if term == ")" then
closed = true
end
term=""
end
if closed == true then
table.remove(symbolstack,#symbolstack)
while symbolstack[#symbolstack] ~= "(" do
table.insert(numberstack,symbolstack[#symbolstack])
table.remove(symbolstack,#symbolstack)
end
closed = false
table.remove(symbolstack,#symbolstack)
end
end
for i = #symbolstack, 1, -1 do
table.insert(numberstack,symbolstack[i])
end
local tempstack = {}
while numberstack[#numberstack] ~= nil do
if symbol[numberstack[1]] == nil then
table.insert(tempstack,numberstack[1])
else
table.insert(tempstack,getmath[numberstack[1]](tempstack[#tempstack-1], tempstack[#tempstack]))
table.remove(tempstack,#tempstack-1)
table.remove(tempstack,#tempstack-1)
end
table.remove(numberstack,1)
end
if tempstack[1] * 2 ~= nil then
return tempstack[1]
end
end
function last(val, sign)
return string.sub(val, 1 - (string.find(string.reverse(val), string.reverse(sign), 1, true) or 0))
end
return p
-- </pre>
-- [[Category:Lua sandbox]]