Module:Sandbox/Wnt/Expression

--- this began long ago when a first test program for the Mandelbrot set went pear-shaped local p={}

function chunk(text) local text=mw.ustring.gsub(text,"(%d%s*)E(%s*[%d%+%-])","%1 E %2") -- force space around E notation to avoid going nuts later local debuglog="" local chunks={type={}} local prowl=mw.ustring.gmatch(text,"(%S+)") local firstbit local num=1 local op=2 local var=3 local warning=4 local defs={num={"^([%d%.]*)(.*)$"},op={"^(E?)(.*)$","^([%(%)%*%+%-%?%%/!^]?)(.*)$"},var={"^(%a*)(.*)$"}} t=prowl while t do       local ot=t for i,j in pairs(defs) do           for _,v in pairs(j) do                firstbit,t=mw.ustring.match(t,v) debuglog=debuglog.."<"..tostring(firstbit)..","..tostring(t)..">"..tostring(i).."i"..tostring(j).."j"..tostring(t).."t"..tostring(v).."v" assert(t,debuglog) -- should always deliver a null string even if the target isn't there if firstbit ~= "" then table.insert(chunks,firstbit) table.insert(chunks.type,i) end end end if t==ot then firstbit,t=mw.ustring.match(t,"^(.)(.*)$") table.insert(chunks,'ignored:'..firstbit) table.insert(chunks.type,warning) end if t=="" then t=prowl end -- if you run out of bits between spaces, pull a new interspace bit. end return chunks end

function p.main(frame) local args=frame.args or {} local parent=frame.getParent(frame) or {} local pargs=parent.args or {} local expr=args.expr or pargs.expr or "" -- (Handle nothing in with nothing out?) local chunks=chunk(expr) return #chunks,"types"..table.concat(chunks.type,",").."values"..table.concat(chunks,",") -- check 1: getting the input sorted out into chunks end

return p