Module:Sandbox/AnataBakka

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

function p.test(frame) local args; if frame.args == nil then args = lib.arguments(frame) else args = lib.arguments(frame.args) end local orig={} orig[1]="(1+(0.3 to 0.5))" i=1 times=5 values=orig[1].." " while string.find(orig[i], "to") ~= nil do           local to = mw.text.split(orig[i], "to") local _, occurences1 = string.gsub(to[1], "%(", "")           values=values..occurences1.." "            local _, occurences2 = string.gsub(to[2], "%)", "") values=values..occurences2.." " 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 values=values..j.." " while j >= 2 do               if j == occurences1+1 then start=para1[j] else start = "(" .. para1[j] .. start               end                values=values..start.." "                _, times2 = string.gsub(para1[j], "%)", "") values=values..times2.." " occurences3 = occurences3 + times2 values=values..occurences1+2-j.." " values=values..occurences3.." " if occurences1 + 2 - j > occurences3 then break end j = j - 1 end occurences3=0 j = 1 while j <= occurences2 do               if j == 1 then finish = para2[j] else finish = finish .. para2[j] .. ")"               end                values=values..finish.." "                _, times2 = string.gsub(para2[j], "%(", "") values=values..times2.." " values=values..j.." " occurences3 = occurences3 + times2 values=values..occurences3+times2.." " if j > occurences3 then break end j = j + 1 end orig[i] = string.gsub(orig[i], start .. "to" .. finish, start .. "+(" .. finish.. "-" .. start .. ")/(" .. times .. "-1)*(x-1)") end return values.."\n\n"..string.gsub(orig[i]," ","") end

function p.ap(frame) local args; if frame.args == nil then args = lib.arguments(frame) else args = lib.arguments(frame.args) end local values="" local orig=args local count=6 local fill local formula if args["skill"]=="R" then fill=3 else fill=5 end i=1 local temp,a,b,c local round=args["round"] or nil while orig[i]~=nil and orig[i]~="" do       if (string.find(orig[i],"to")~=nil or string.find(orig[i],"x")~=nil) and string.find(orig[i],"%<")==nil then local _, occurences = string.gsub(orig[i], "to", "") if string.find(orig[i],"x")==nil and occurences == 1 then start,finish,times=orig[i]:match"(.*) *to *([^ ]*) *([^ ]*)" if start~=nil and finish~=nil and pcall(expr,start.."*2")~=false and pcall(expr,finish.."*2")~=false then start=tonumber(expr(start)) finish=tonumber(expr(finish)) if times=="" or times==nil then times=fill fill=0 else times=tonumber(expr(times)) end count=count-times if count<0 then return userError("Maximum size exceeded", "LuaError") end x=1 while x<=times do                       if x==1 then formula = start scale = (finish - start) / (times - 1) else formula = formula + scale end if values=="" then values=fd(rounding(formula,round)) else values=values.." / "..fd(rounding(formula,round)) end x=x+1 end else count=count-1 if count<0 then return userError("Maximum size exceeded", "LuaError") end if values=="" then values=fd(rounding(expr(orig[i]),round)) else values=values.." / "..fd(rounding(expr(orig[i]),round)) end end else if pcall(expr,string.gsub(last(orig[i]," "),"x",1) .. "*2") ~= false and last(orig[i]," ") ~= orig[i] then times = last(orig[i]," ") orig[i] = string.sub(orig[i],1,#orig[i]-#times) end if times=="" or times==nil then times=fill fill=0 else times=tonumber(expr(times)) end if occurences >1 then while string.find(orig[i], "to") ~= nil do                       local to = mw.text.split(orig[i], "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                           start = "(" .. para1[j] .. start                            _, times2 = string.gsub(para1[j], "%)", "") occurences3 = occurences3 + times2 if occurences1 + 2 - j > occurences3 then break end j = j - 1 end occurences3=0 j = 1 while j <= occurences2 do                           finish = finish .. para2[j] .. ")"                           _, times2 = string.gsub(para2[j], "%(", "") occurences3 = occurences3 + times2 if j > occurences3 then break end j = j + 1 end orig[i] = string.gsub(orig[i], start .. "to" .. finish, "(" .. start .. "+(" .. finish.. "-" .. start .. ")/(" .. times .. "-1)*(x-1))") end end useformula = orig[i] if useformula~=nil and pcall(expr,string.gsub(useformula,"x","1").."*2")~=false then x=1 while x<=times do                       count=count-1 if count<0 then return userError("Maximum size exceeded", "LuaError") end formula=string.gsub(useformula,"x",x) if values=="" then values=fd(rounding(expr(formula),round)) else values=values.." / "..fd(rounding(expr(formula),round)) end x=x+1 end else count=count-1 if count<0 then return userError("Maximum size exceeded", "LuaError") end if values=="" then values=fd(rounding(expr(args[i]),round)) else values=values.." / "..fd(rounding(expr(args[i]),round)) end end end else count=count-1 if count<0 then return userError("Maximum size exceeded", "LuaError") end if values=="" then values=fd(rounding(expr(args[i]),round)) else values=values.." / "..fd(rounding(expr(args[i]),round)) end end i=i+1 end return values 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%(%)]) %- ","%1-"),"([^%d%(%)]) %+ ","%1+") local i=1 local j=1 local a,b,switcher,switcher2,switcher3,temp while i>0 do       while j>0 do            temp=val if string.find(val,"%)")~=nil then               switchertemp=mw.text.split( val, "%)") temp=last(switchertemp[1],"%(")           end            if string.find(temp," %^ ")~=nil then                a=mw.text.split(temp," %^ ")                b=mw.text.split(a[2]," ")                switcher=last( a[1]," ")                switcher2=b[1]                switcher3=switcher^switcher2                if string.find(switcher2,"%-")~=nil then                     switcher2="%-"..switcher2*(-1)                end                val=string.gsub(val,switcher.." %^ "..switcher2,switcher3)                break            end            if string.find(temp," %/ ")~=nil then                a=mw.text.split(temp," %/ ")                b=mw.text.split(a[2]," ")                switcher=last( a[1]," ")                switcher2=b[1]                switcher3=switcher/switcher2                if string.find(switcher2,"%-")~=nil then                     switcher2="%-"..switcher2*(-1) end val=string.gsub(val,switcher.." %/ "..switcher2,switcher3) break end if string.find(temp," %* ")~=nil then a=mw.text.split( temp," %* ") b=mw.text.split(a[2]," ") switcher=last( a[1]," ") switcher2=b[1] switcher3=switcher*switcher2 if string.find(switcher2,"%-")~=nil then switcher2="%-"..switcher2*(-1) end val=string.gsub(val,switcher.." %* "..switcher2,switcher3) break end if string.find(temp," %- ")~=nil then a=mw.text.split( temp," %- ") b=mw.text.split(a[2]," ") switcher=last( a[1]," ") switcher2=b[1] if switcher=="" then if string.find(switcher2,"%-")~=nil then switcher2="%-"..switcher2*(-1) end val=string.gsub(val," %- "..switcher2,"%-"..switcher2) break else switcher3=switcher-switcher2 if string.find(switcher2,"%-")~=nil then switcher2="%-"..switcher2*(-1) end val=string.gsub(val,switcher.." %- "..switcher2,switcher3) break end end if string.find(temp," %+ ")~=nil then a=mw.text.split( temp," %+ ") b=mw.text.split(a[2]," ") switcher=last( a[1]," ") switcher2=b[1] if switcher=="" then if string.find(switcher2,"%-")~=nil then switcher2="%-"..switcher2*(-1) end val=string.gsub(val," %+ "..switcher2,switcher2) break else switcher3=switcher+switcher2 if string.find(switcher2,"%-")~=nil then switcher2="%-"..switcher2*(-1) end val=string.gsub(val,switcher.." %+ "..switcher2,switcher3) break end end if string.find(val,"%)")~=nil then               if string.find(temp,"%-")~=nil then                     temp="%-"..temp*(-1)                end                val=string.gsub(val,"%("..temp.."%)",temp)                break            else                i=0            end            break        end    end    return temp end

function last(val,sign) local a=mw.text.split(val,sign) local j=1 while j>0 do       if a[j] ~=nil then j=j+1 else break end end return a[j-1] end

function fd(val) if string.find(val,"[0-9]") == nil or string.find(val,"%.") == nil then return val end local a,b,c if string.find(val,"span") ~= nil then a,b,c = val:match"([^.]*).(.*) (.*)" if string.find(val,"span")<=string.find(val,"%.") then return a .. ". " .. b .. " " .. c       else return a .. ". " .. b .. " " .. c       end else a,b,c = val:match"([^.]*).([0-9]*)(.*)" return a .. ". " .. b .. " " .. c   end end

function fdmulti(val) if string.find(val,"[0-9]") == nil or string.find(val,"%.") == nil then return val end local val   = mw.text.split(val,"%.") local values = val[1] local i     = 2 local b,c while val[i] ~= nil do       if string.find(val[i],"span") ~= nil then b,c   = val[i]:match"(.*) (.*)" if string.find(tostring(b),"[0-9]")==nil then values=values.."."..val[i] else if string.find(val[i],"span")<=string.find(val[i],"%.") then values = values .. ". " .. b .. " " .. c               else values = values .. ". " .. b .. " " .. c               end end else b,c   = val[i]:match"([0-9]*)(.*)" if string.find(tostring(b),"[0-9]")==nil then values=values.."."..val[i] else values = values .. ". " .. b .. " " .. c           end end i = i + 1 end return values end

function rounding(val, decimals) local round local val = tostring(val) if decimals == nil or string.find(val,"[0-9]") == nil or string.find(val,"<span") ~= nil or string.find(val,"%.") == nil then return val end local a,b,c,d a,b,c,d = val:match"([^0-9]*)([0-9]*).([0-9]*)(.*)" val=tonumber(b.."."..c)   if decimals == "abs" then round = math.abs(val) end if decimals == "floor" then round = math.floor(val) end if decimals == "ceil" then round = math.ceil(val) end if decimals == "trunc" then round = string.sub(val,1,(string.find(val,"%.") or 0) - 1) end if round == nil then local mult = 10^(decimals) round = math.floor(val * mult + 0.5) / mult end return a..round..d end

return p --