Module:Sandbox/AnataBakka2

-- local p    = {} local lib  = require('Module:Feature') local iter = 2200

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 string_to_formula(args,times) local val = args while string.find(val, "to") ~= nil do       local to = mw.text.split(val, "to") local _, occurences1 = string.gsub(to[1], "%(", "")       local _, occurences2 = string.gsub(to[2], "%)", "") local para1 = mw.text.split(to[1], "%(")       local para2 = mw.text.split(to[2], "%)") local start,finish="","" local times2 local occurences3=0 local j = occurences1 + 1 while j >= 2 do           if j == occurences1+1 then start=para1[j] else start = "(" .. para1[j] .. start           end            _, times2 = string.gsub(para1[j], "%)", "") occurences3 = occurences3 + times2 if occurences1 + 2 - j > occurences3 then break end j = j - 1 end if pcall(expr, start .. "*2") == false then return args end occurences3=0 j = 1 while j <= occurences2 do           if j == 1 then finish = para2[j] else finish = finish .. para2[j] .. ")"           end            _, times2 = string.gsub(para2[j], "%(", "") occurences3 = occurences3 + times2 if j > occurences3 then break end j = j + 1 end if pcall(expr, finish .. "*2") == false then return args end start = string.gsub(start,"([%-%+%*%/%^%(%)])","%%%1") finish = string.gsub(finish,"([%-%+%*%/%^%(%)])","%%%1") val = string.gsub(val, start .. "to" .. finish, "(" .. start .. ")" .. "+(" .. "(" .. finish .. ")" .. "-" .. "(" .. start .. ")" .. ")/(" .. times .. "-1)*(x-1)") end return val 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(val," ",""),"(%d+%.*%d*)"," %1 "),"%(%- ","( -"),"%(%+ ","( ") local i=1 local j=1 local a,b,switcher,switcher2,switcher3,temp local s = "" while i>0 do       while j>0 do            temp=val s = s .. " " .. temp if string.find(val,"%)")~=nil then               temp=last(string.sub(val,1,string.find(val," %)")-1),"%( ")            end            s = s .. "  " .. temp            if string.find(temp," %^ ")~=nil then                switcher=last(string.sub(temp,1,string.find(temp," %^ ")-1)," ")                switcher2=string.sub(temp,string.find(temp," %^ ")+3)                switcher2 = string.sub(switcher2,1,(string.find(switcher2," ") or 0)-1)                switcher3=switcher^switcher2                if string.find(switcher,"%-")~=nil then                     switcher="%-"..switcher*(-1)                end                if string.find(switcher2,"%-")~=nil then                     switcher2="%-"..switcher2*(-1)                end                s = s .. "  " .. switcher.." %^ "..switcher2                s = s .. "  " .. switcher3                val=string.gsub(val," " .. switcher.." %^ "..switcher2 .. " "," " .. switcher3 .. " ")               break            end            if string.find(temp," %/ ")~=nil then                switcher=last(string.sub(temp,1,string.find(temp," %/ ")-1)," ")                switcher2=string.sub(temp,string.find(temp," %/ ")+3)                switcher2 = string.sub(switcher2,1,(string.find(switcher2," ") or 0)-1)                switcher3=switcher/switcher2                if string.find(switcher,"%-")~=nil then                     switcher="%-"..switcher*(-1)                end                if string.find(switcher2,"%-")~=nil then                     switcher2="%-"..switcher2*(-1)                end                s = s .. "  " .. switcher.." %/ "..switcher2                s = s .. "  " .. switcher3                val=string.gsub(val," " .. switcher.." %/ "..switcher2 .. " "," " .. switcher3 .. " ")               break            end            if string.find(temp," %* ")~=nil then                switcher=last(string.sub(temp,1,string.find(temp," %* ")-1)," ")                switcher2=string.sub(temp,string.find(temp," %* ")+3)                switcher2 = string.sub(switcher2,1,(string.find(switcher2," ") or 0)-1)                switcher3=switcher*switcher2                if string.find(switcher,"%-")~=nil then                     switcher="%-"..switcher*(-1)                end                if string.find(switcher2,"%-")~=nil then                     switcher2="%-"..switcher2*(-1)                end                s = s .. "  " .. switcher.." %* "..switcher2                s = s .. "  " .. switcher3                val=string.gsub(val," " .. switcher.." %* "..switcher2 .. " "," " .. switcher3 .. " ")               break            end           if string.find(temp," %- ")~=nil then                switcher=last(string.sub(temp,1,string.find(temp," %- ")-1)," ")                switcher2=string.sub(temp,string.find(temp," %- ")+3)                switcher2 = string.sub(switcher2,1,(string.find(switcher2," ") or 0)-1)                switcher3=switcher-switcher2                if string.find(switcher,"%-")~=nil then                     switcher="%-"..switcher*(-1)                end                if string.find(switcher2,"%-")~=nil then                     switcher2="%-"..switcher2*(-1)                end                s = s .. "  " .. switcher.." %- "..switcher2                s = s .. "  " .. switcher3                val=string.gsub(val," " .. switcher.." %- "..switcher2 .. " "," " .. switcher3 .. " ")               break            end            if string.find(temp," %+ ")~=nil then                switcher=last(string.sub(temp,1,string.find(temp," %+ ")-1)," ")                switcher2=string.sub(temp,string.find(temp," %+ ")+3)                switcher2 = string.sub(switcher2,1,(string.find(switcher2," ") or 0)-1)                switcher3=switcher+switcher2                if string.find(switcher,"%-")~=nil then                     switcher="%-"..switcher*(-1)                end                if string.find(switcher2,"%-")~=nil then                     switcher2="%-"..switcher2*(-1)                end                s = s .. "  " .. switcher.." %+"..switcher2                s = s .. "  " .. switcher3                val=string.gsub(val," " .. switcher.." %+ "..switcher2 .. " "," " .. switcher3 .. " ")               break            end            if string.find(val,"%)")~=nil and string.find(val,"%(")~=nil then                if string.find(temp,"%-")~=nil then                     temp="%-"..temp*(-1)                end                s = s .. "  " .. temp                val=string.gsub(val,"%( "..temp.." %)"," " .. temp .. " ")               break            else                i=0            end            break        end    end    val = string.gsub(string.gsub(val,"%- ","-"),"[%(%)]")    return s .. "  x" ..val.. "x" end

function last(val,sign) local a=mw.text.split(val,sign) return a[#a] end

return p -- --