League of Legends Wiki

Want to contribute to this wiki?
Sign up for an account, and get started!
You can even turn off ads in your preferences.

Come join the LoL Wiki community Discord server!

READ MORE

League of Legends Wiki
No edit summary
(Undid revision 3117538 by AnataBakka (talk))
Line 6: Line 6:
 
function p.test(frame)
 
function p.test(frame)
 
local args; if frame.args == nil then args = lib.arguments(frame) else args = lib.arguments(frame.args) end
 
local args; if frame.args == nil then args = lib.arguments(frame) else args = lib.arguments(frame.args) end
local a = loadstring("return " .. args[1])
+
return expr(args[1])
return a()
 
 
end
 
end
   

Revision as of 12:59, 7 September 2020

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
    local val = string.gsub(string.gsub(string.gsub(string.gsub(string.gsub(string.gsub(string.gsub(string.gsub(val, " ", ""), "(%d+%.*%d*)", " %1 "), "%(", " %("), "%)", "%) "), "([^ ])%- ", "%1 -"), "([^ ])%+ ", "%1 "), "^%- ", " -"), "^%+ ", " ")
    local i = true
    local j = 1
    local a, b, switcher, switcher2, switcher3, temp, temp2
    
    while i == true do
        temp = val
        if string.find(val, "%)") ~= nil then
            temp = last(string.sub(val, 1, string.find(val, "%)") - 1), "(")
        end
        temp2 = temp
        
        while j do
            if string.find(temp2, " %^ ") ~= nil then
                switcher  = last(string.sub(temp2, 1, string.find(temp2, " %^ ") - 1), " ")
                switcher2 = string.sub(temp2, string.find(temp2, " %^ ") + 3)
                switcher2 = string.sub(switcher2, 1, (string.find(switcher2, " ") or 0) - 1)
                switcher3 = switcher ^ switcher2
                switcher  = string.gsub(switcher, "([%.%-])", "%%%1")
                switcher2 = string.gsub(switcher2, "([%.%-])", "%%%1")
                temp2     = string.gsub(temp2, " " .. switcher .. " %^ " .. switcher2 .. " ", " " .. switcher3 .. " ")
            else
                break
            end
        end
        
        while j do
            if string.find(temp2, " %/ ") ~= nil then
                switcher  = last(string.sub(temp2, 1, string.find(temp2, " %/ ") - 1), " ")
                switcher2 = string.sub(temp2, string.find(temp2, " %/ ") + 3)
                switcher2 = string.sub(switcher2, 1, (string.find(switcher2, " ") or 0) - 1)
                switcher3 = switcher / switcher2
                switcher  = string.gsub(switcher, "([%.%-])", "%%%1")
                switcher2 = string.gsub(switcher2, "([%.%-])", "%%%1")
                temp2     = string.gsub(temp2, " " .. switcher .. " %/ " .. switcher2 .. " ", " " .. switcher3 .. " ")
            else
                break
            end
        end
        
        while j do
            if string.find(temp2, " %* ") ~= nil then
                switcher  = last(string.sub(temp2, 1, string.find(temp2, " %* ") - 1), " ")
                switcher2 = string.sub(temp2, string.find(temp2, " %* ") + 3)
                switcher2 = string.sub(switcher2, 1, (string.find(switcher2, " ") or 0) - 1)
                switcher3 = switcher * switcher2
                switcher  = string.gsub(switcher, "([%.%-])", "%%%1")
                switcher2 = string.gsub(switcher2, "([%.%-])", "%%%1")
                temp2     = string.gsub(temp2, " " .. switcher .. " %* " .. switcher2 .. " ", " " .. switcher3 .. " ")
            else
                break
            end
        end
        
        while j do
           if string.find(temp2, " %- ") ~= nil then
                switcher  = last(string.sub(temp2, 1, string.find(temp2, " %- ") - 1), " ")
                switcher2 = string.sub(temp2, string.find(temp2, " %- ") + 3)
                switcher2 = string.sub(switcher2, 1, (string.find(switcher2, " ") or 0) - 1)
                switcher3 = switcher - switcher2
                switcher  = string.gsub(switcher, "([%.%-])", "%%%1")
                switcher2 = string.gsub(switcher2, "([%.%-])", "%%%1")
                temp2     = string.gsub(temp2, " " .. switcher .. " %- " .. switcher2 .. " ", " " .. switcher3 .. " ")
            else
                break
            end
        end
        
        while j do
            if string.find(temp2, " %+ ") ~= nil then
                switcher  = last(string.sub(temp2, 1, string.find(temp2, " %+ ") - 1), " ")
                switcher2 = string.sub(temp2, string.find(temp2, " %+ ") + 3)
                switcher2 = string.sub(switcher2, 1, (string.find(switcher2, " ") or 0) - 1)
                switcher3 = switcher + switcher2
                switcher  = string.gsub(switcher, "([%.%-])", "%%%1")
                switcher2 = string.gsub(switcher2, "([%.%-])", "%%%1")
                temp2     = string.gsub(temp2, " " .. switcher .. " %+ " .. switcher2 .. " ", " " .. switcher3 .. " ")
            else
                break
            end
        end
        
        if string.find(val, "%)") ~= nil and string.find(val, "%(") ~= nil then
            temp = string.gsub(temp, "([%.%-%+%*%/%^])", "%%%1")
            val  = string.gsub(val, " %(" .. temp .. "%) ", temp2)
        else
            val = temp2
            i = false
        end
    end
    if val * 2 ~= nil then
        val = string.gsub(val, " ", "")
        return val 
    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]]