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 value=""
for i=1,#val,1 do
value = value .. string.sub(val,i,i)
if symbol[value] ~= nil then
if symbolstack[#symbolstack] ~= nil and symbol[symbolstack[#symbolstack]] < symbol[value] and value ~= "(" and value ~= ")" then
table.insert(numberstack,symbolstack[#symbolstack])
symbolstack[#symbolstack] = value
else
table.insert(symbolstack,value)
end
if value == ")" then
closed = true
end
value=""
else
if symbol[string.sub(val,i+1,i+1)] ~= nil then
table.insert(numberstack,value)
value=""
end
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
while symbolstack[#symbolstack] ~= nil do
table.insert(numberstack,symbolstack[#symbolstack])
table.remove(symbolstack,#symbolstack)
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
tempstack[1] = string.gsub(tempstack[1], " ", "")
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]]