Whitemambaa

cas.Lua

Dec 22nd, 2013
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.56 KB | None | 0 0
  1. --Lua Computer Algebra System (LuCAS) By xXxMoNkEyMaNxXx doz
  2. local cas={}
  3. --[[
  4. function cas.bool(value)
  5.     return setmetatable({type="bool",value},cas)
  6. end
  7. --]]
  8. function cas.cst(value)
  9.     return setmetatable({type="cst",value},cas)
  10. end
  11. function cas.var(name)
  12.     --[[
  13.     if name:lower()=="true" then
  14.         return cas.bool(true)
  15.     elseif name:lower()=="false" then
  16.         return cas.bool(false)
  17.     end
  18.     --]]
  19.     return setmetatable({type="var",name},cas)
  20. end
  21.  
  22. function cas.vec(...)
  23.     --
  24. end
  25.  
  26. function cas.pow(base,power)
  27.     local u=base
  28.     if type(base)=="number" then
  29.         u=cas.cst(base)
  30.     elseif type(base)=="string" then
  31.         u=cas.var(base)
  32.     end
  33.     local v=power
  34.     if type(power)=="number" then
  35.         if power==0 then
  36.             return cas.cst(1)
  37.         elseif power==1 then
  38.             return u
  39.         else
  40.             v=cas.cst(power)
  41.         end
  42.     elseif type(power)=="string" then
  43.         v=cas.var(power)
  44.     end
  45.     return setmetatable({type="pow",u,v},cas)
  46. end
  47. function cas.prod(list)
  48.     local cont={type="prod"}
  49.     local constant=1
  50.     for i=1,#list do
  51.         if type(list[i])=="table" then
  52.             if list[i].type==cont.type then
  53.                 for i2=1,#list[i] do
  54.                     if list[i][i2].type=="cst" then
  55.                         if list[i][i2][1]==0 then
  56.                             return cas.cst(0)
  57.                         else
  58.                             constant=constant*list[i][i2][1]
  59.                         end
  60.                     else
  61.                         cont[#cont+1]=list[i][i2]
  62.                     end
  63.                 end
  64.             elseif list[i].type=="cst" then
  65.                 if list[i][1]==0 then
  66.                     return cas.cst(0)
  67.                 else
  68.                     constant=constant*list[i][1]
  69.                 end
  70.             else
  71.                 cont[#cont+1]=list[i]
  72.             end
  73.         elseif type(list[i])=="string" then
  74.             cont[#cont+1]=cas.var(list[i])
  75.         elseif type(list[i])=="number" then
  76.             if list[i]==0 then
  77.                 return cas.cst(0)
  78.             else
  79.                 constant=constant*list[i]
  80.             end
  81.         end
  82.     end
  83.     if constant~=1 then
  84.         cont[#cont+1]=cas.cst(constant)
  85.     end
  86.     if #cont==1 then
  87.         return cont[1]
  88.     else
  89.         return setmetatable(cont,cas)
  90.     end
  91. end
  92. function cas.sum(list)
  93.     local cont={type="sum"}
  94.     local constant=0
  95.     for i=1,#list do
  96.         if type(list[i])=="table" then
  97.             if list[i].type==cont.type then
  98.                 for i2=1,#list[i] do
  99.                     cont[#cont+1]=list[i][i2]
  100.                 end
  101.             elseif list[i].type=="cst" then
  102.                 constant=constant+list[i][1]
  103.             else
  104.                 cont[#cont+1]=list[i]
  105.             end
  106.         elseif type(list[i])=="string" then
  107.             cont[#cont+1]=cas.var(list[i])
  108.         elseif type(list[i])=="number" then
  109.             constant=constant+list[i]
  110.         end
  111.     end
  112.     if constant~=0 then
  113.         cont[#cont+1]=cas.cst(constant)
  114.     end
  115.     if #cont==1 then
  116.         return cont[1]
  117.     elseif #cont>1 then
  118.         return setmetatable(cont,cas)
  119.     end
  120.     return cas.cst(0)
  121. end
  122.  
  123.  
  124. function cas.eq(list)--all items in list are to be considered equal
  125.     local cont={type="eq"}
  126.     if type(list)=="table" then
  127.         if list.type then
  128.             cont[1]=list
  129.         else
  130.             for i=1,#list do
  131.                 if type(list[i])=="table" then
  132.                     if list[i].type==cont.type then
  133.                         for i2=1,#list[i] do
  134.                             cont[#cont+1]=list[i][i2]
  135.                         end
  136.                     else
  137.                         cont[#cont+1]=list[i]
  138.                     end
  139.                 elseif type(list[i])=="string" then
  140.                     cont[#cont+1]=cas.var(list[i])
  141.                 elseif type(list[i])=="number" then
  142.                     cont[#cont+1]=cas.cst(list[i])
  143.                 end
  144.             end
  145.         end
  146.     end
  147.     return cont
  148. end
  149. --[[
  150. function cas.not(item)
  151.     return setmetatable({type="not",item},cas)
  152. end
  153.  
  154. function cas.and(list)
  155.     --
  156. end
  157. function cas.equ(list,oplist)--equality/inequality
  158.     local op={}
  159.     local nlist=type(list)=="table" and not list.type and #list or 1
  160.     if type(oplist)=="table" then
  161.         for i=1,math.min(nlist,#oplist) do
  162.             local o=oplist[i]
  163.             if o:match'^[~!]?[<=>]=?$' then
  164.                 if o=="~=" or o=="!==" or o=="~==" then
  165.                     op[i]="!="
  166.                 elseif o=="!<" or o=="~<" then
  167.                     op[i]=">="
  168.                 elseif o=="!>" or o=="~>" then
  169.                     op[i]="<="
  170.                 elseif o=="!<=" or o=="~<=" then
  171.                     op[i]=">"
  172.                 elseif o=="!>=" or o=="~>=" then
  173.                     op[i]="<"
  174.                 else
  175.                     op[i]=o
  176.                 end
  177.             else
  178.                 error("'"..tostring(o).."' is not a valid eq operation. Operations must match this pattern: [~!]?[<=>]=?")
  179.             end
  180.         end
  181.     elseif type(oplist)=="string" and oplist:match'^[~!]?[<=>]=?$' then
  182.         op[1]=oplist
  183.     elseif oplist then
  184.         error("'"..tostring(oplist).."' is not a valid eq operation. Operations must match this pattern: [~!]?[<=>]=?")
  185.     end
  186.     if nlist-1==#op then
  187.         op[nlist]="="
  188.     elseif nlist>#op then
  189.         error("Not enough operations: "..#op.." were given, but "..(nlist-1).." or "..nlist.." are required.")
  190.     end
  191.     local cont={type="equ",op=op}
  192.     if type(list)=="table" and not list.type then
  193.         for i=1,#list do
  194.             cont[i]=list[i]
  195.         end
  196.     else
  197.         cont[1]=list
  198.     end
  199.     return setmetatable(cont,cas)
  200. end
  201. --]]
  202. function cas.ufo(name,argv,args)--Undefined Functional Object
  203.     return setmetatable({type="ufo",name=name,argv=argv,args=args},cas)
  204. end
  205.  
  206. local myfuncs={}
  207. function cas.func(argv,func)--func must be an cas object
  208.     if func and type(func)=="table" and func.type then
  209.         return function(...)
  210.             return setmetatable({type="myfunc",func=func,argv=argv,args={...}},cas)
  211.         end
  212.     else
  213.         error'Invalid function, must be an cas object.'
  214.     end
  215. end
  216.  
  217. local function func(names,func,rules)
  218.     return function(...)
  219.         return setmetatable({type="func",names=type(names)=="table" and names or {names},func=func,rules=rules,...},cas)
  220.     end
  221. end
  222.  
  223. function cas:__unm()
  224.     return cas.prod{self,-1}
  225. end
  226. function cas:__add(item)
  227.     return cas.sum{self,item}
  228. end
  229. function cas:__sub(item)
  230.     return cas.sum{self,cas.prod{item,-1}}
  231. end
  232. function cas:__mul(item)
  233.     return cas.prod{self,item}
  234. end
  235. function cas:__div(item)
  236.     return cas.prod{self,cas.pow(item,-1)}
  237. end
  238. function cas:__pow(item)
  239.     return cas.pow(self,item)
  240. end
  241.  
  242.  
  243. local funcs={}
  244. local funcdefs={
  245.     log={names={"log","ln"},func=math.log,rules={
  246.         derive=function(self,rel) return self[1]:derive(rel)/self[1] end,
  247.         expand=function(self)
  248.             if self[1].type=="pow" then
  249.                 return self[1][2]*funcs.log(self[1][1])
  250.             elseif self[1].type=="prod" then
  251.                 local cont={}
  252.                 for i=1,#self[1] do
  253.                     cont[i]=funcs.log(self[1][i])
  254.                 end
  255.                 return cas.sum(cont)
  256.             end
  257.         end,
  258.     }},
  259.     sin={names={"sin"},func=math.sin,rules={}},
  260.     cos={names={"cos"},func=math.cos,rules={}},
  261.     tan={names={"tan"},func=math.tan,rules={}},
  262.     asin={names={"asin"},func=math.asin,rules={derive=function(self,rel) return self[1]:derive(rel)/(1-self[1]^2)^0.5 end}},
  263.     acos={names={"acos"},func=math.acos,rules={derive=function(self,rel) return -self[1]:derive(rel)/(1-self[1]^2)^0.5 end}},
  264.     atan={names={"atan"},func=math.atan,rules={derive=function(self,rel) return self[1]:derive(rel)/(self[1]^2+1) end}},
  265.     atan2={names={"atan2"},func=math.atan2,rules={derive=function(self,rel) return (self[1]:derive(rel)*self[2]-self[1]*self[2]:derive(rel))/(self[1]^2+self[2]^2) end}},
  266.     --sum={names={"sum"},func=function(var,low,high,stuff) local sum=stuff{[var]=low} for i=low+1,high do sum=sum+stuff{[var]=i} end return sum end}
  267. }
  268. for i,v in next,funcdefs do
  269.     funcs[i]=func(v.names,v.func,v.rules)
  270. end
  271.  
  272. myfuncs.sqrt=cas.func({"x"},cas.var'x'^0.5)
  273. myfuncs.csc=cas.func({"x"},1/funcs.sin(cas.var'x'))
  274. myfuncs.sec=cas.func({"x"},1/funcs.cos(cas.var'x'))
  275. myfuncs.cot=cas.func({"x"},1/funcs.tan(cas.var'x'))
  276. funcdefs.sin.rules.derive=function(self,rel) return funcs.cos(self[1])*self[1]:derive(rel) end
  277. funcdefs.cos.rules.derive=function(self,rel) return -funcs.sin(self[1])*self[1]:derive(rel) end
  278. funcdefs.tan.rules.derive=function(self,rel) return myfuncs.sec(self[1])^2*self[1]:derive(rel) end
  279.  
  280. for i,v in next,funcs do
  281.     cas[i]=funcs[i]
  282. end
  283. local rules={}
  284. --Derivative--
  285. rules.derive={}
  286. function rules.derive:func(rel)
  287.     if self.rules and self.rules.derive then
  288.         return self.rules.derive(self,rel)
  289.     else
  290.         error("Cannot differentiate: no derivative provided for built-in function '"..self.names[1].."'")
  291.     end
  292. end
  293. function rules.derive:myfunc(rel)
  294.     local args={}
  295.     for i=1,#self.args do
  296.         args[self.argv[i].."`"..(rel and "("..rel..")" or "")]=self.args[i]:derive(rel)
  297.     end
  298.     return self.func:derive()(args)
  299. end
  300. function rules.derive:pow(rel)
  301.     return self[1]^(self[2]-1)*(self[1]:derive(rel)*self[2]+self[1]*cas.log(self[1])*self[2]:derive(rel))
  302. end
  303. function rules.derive:prod(rel)
  304.     local Sigma={}
  305.     for i=1,#self do
  306.         local Pi={}
  307.         for i2=1,#self do
  308.             if i==i2 then
  309.                 Pi[i2]=self[i2]:derive(rel)
  310.             else
  311.                 Pi[i2]=self[i2]
  312.             end
  313.         end
  314.         Sigma[i]=cas.prod(Pi)
  315.     end
  316.     return cas.sum(Sigma)
  317. end
  318. function rules.derive:sum(rel)
  319.     local Sigma={}
  320.     for i=1,#self do
  321.         Sigma[i]=self[i]:derive(rel)
  322.     end
  323.     return cas.sum(Sigma)
  324. end
  325. function rules.derive:var(rel)
  326.     if rel==self[1] then
  327.         return cas.cst(1)
  328.     else
  329.         local test=self()
  330.         if test==self then
  331.             return cas.var(self[1].."`"..(rel and "("..rel..")" or ""))
  332.         else
  333.             return test
  334.         end
  335.     end
  336. end
  337. function rules.derive:cst(rel)
  338.     return cas.cst(0)
  339. end
  340. ----------
  341.  
  342. --Expand--
  343. rules.expand={}
  344. function rules.expand:func()
  345.     if self.rules and self.rules.expand then
  346.         return self.rules.expand(self) or self
  347.     end
  348.     return self
  349. end
  350. function rules.expand:myfunc()
  351.     local args={}
  352.     for i=1,#self.args do
  353.         args[self.argv[i]]=self.args[i]
  354.     end
  355.     return self.func:expand()(args)
  356. end
  357. function rules.expand:pow()
  358.     if self[1].type=="cst" and self[2].type=="cst" then
  359.         return cas.cst(self())
  360.     elseif self[2].type=="cst" and self[2][1]>=1 and self[2][1]<=100 and self[2][1]%1==0 then-- <=100, don't get carried away folks
  361.         local cont={}
  362.         for i=1,self[2][1] do
  363.             cont[i]=self[1]
  364.         end
  365.         return cas.prod(cont):expand()
  366.     elseif self[2].type=="sum" then
  367.         local cont={}
  368.         for i=1,#self[2] do
  369.             cont[i]=cas.pow(self[1],self[2][i])
  370.         end
  371.         return cas.prod(cont):expand()
  372.     end
  373.     return self
  374. end
  375. function rules.expand:prod()
  376.     local all=true
  377.     for i1=1,#self do
  378.         if self[i1].type=="sum" then
  379.             local cont0={}
  380.             for i2=1,#self[i1] do
  381.                 local cont1={[i1]=self[i1][i2]}
  382.                 for i3=1,#self do
  383.                     if i1~=i3 then
  384.                         cont1[i3]=self[i3]
  385.                     end
  386.                 end
  387.                 cont0[i2]=cas.prod(cont1):expand()
  388.             end
  389.             return cas.sum(cont0)
  390.         end
  391.         all=all and self[i1].type=="cst"
  392.     end
  393.     if all then
  394.         return cas.cst(self())
  395.     end
  396.     return self
  397. end
  398. function rules.expand:sum()
  399.     local all=true
  400.     local cont={}
  401.     for i=1,#self do
  402.         cont[i]=self[i]:expand()
  403.         all=all and cont[i].type=="cst"
  404.     end
  405.     if all then
  406.         return cas.cst(self())
  407.     else
  408.         return cas.sum(cont)
  409.     end
  410. end
  411. function rules.expand:var()
  412.     return self()
  413. end
  414. function rules.expand:cst()
  415.     return self
  416. end
  417. ----------
  418.  
  419. function cas:__index(index)
  420.     local test1=rules[index]
  421.     if test1 and test1[self.type] then
  422.         return test1[self.type]
  423.     elseif self.type=="vec" then
  424.         --
  425.     end
  426. end
  427.  
  428. function cas:__call(env)
  429.     env=env or {}
  430.     if self.type=="ufo" then
  431.         local args={}
  432.         for i=1,#self.args do
  433.             local arg=self.args[i](env)
  434.             args[i]=arg
  435.         end
  436.         return cas.ufo(self.name,self.argv,args)
  437.     elseif self.type=="func" then
  438.         local all=true
  439.         local args={}
  440.         for i=1,#self do
  441.             local arg=self[i](env)
  442.             args[i]=arg
  443.             all=all and type(arg)=="number"
  444.         end
  445.         if all then
  446.             return self.func(unpack(args))
  447.         else
  448.             return funcs[self.names[1]](unpack(args))
  449.         end
  450.     elseif self.type=="myfunc" then
  451.         local args={}
  452.         for i=1,#self.args do
  453.             local arg=self.args[i](env)
  454.             args[self.argv[i]]=arg
  455.         end
  456.         return self.func(args)--New env
  457.     elseif self.type=="pow" then
  458.     return self[1](env)^self[2](env)
  459.     elseif self.type=="prod" then
  460.         local product=1
  461.         for i=1,#self do
  462.             product=product*self[i](env)
  463.         end
  464.         return product
  465.     elseif self.type=="sum" then
  466.         local sum=0
  467.         for i=1,#self do
  468.             sum=sum+self[i](env)
  469.         end
  470.         return sum
  471.     elseif self.type=="var" then
  472.         local def=env[self[1]] or getfenv()[self[1]] or _G[self[1]]
  473.         if type(def)=="table" then
  474.             if def.type then
  475.                 local ran,ret=pcall(function() return def(env) end)
  476.                 if ran then
  477.                     return ret
  478.                 elseif DebugMode then
  479.                     print(ret)
  480.                 end
  481.             end
  482.         elseif type(def)=="number" then
  483.             return def
  484.         end
  485.         return self
  486.     elseif self.type=="cst" then
  487.         return self[1]
  488.     end
  489. end
  490.  
  491. function cas:__tostring()
  492.     if self.type=="func" then
  493.         local args=tostring(self[1])
  494.         for i=2,#self do
  495.             args=args..","..tostring(self[i])
  496.         end
  497.         return self.names[1].."("..args..")"
  498.     elseif self.type=="myfunc" then
  499.         return tostring(self.func)
  500.     elseif self.type=="pow" then
  501.         return tostring(self[1]).."^"..tostring(self[2])
  502.     elseif self.type=="prod" then
  503.         local cont=tostring(self[1])
  504.         for i=2,#self do
  505.             cont=cont.."*"..tostring(self[i])
  506.         end
  507.         return "("..cont..")"
  508.     elseif self.type=="sum" then
  509.         local cont=tostring(self[1])
  510.         for i=2,#self do
  511.             cont=cont.."+"..tostring(self[i])
  512.         end
  513.         return "("..cont..")"
  514.     elseif self.type=="var" then
  515.         return self[1]
  516.     elseif self.type=="cst" then
  517.         return tostring(self[1])
  518.     end
  519. end
  520.  
  521. function cas.parse(exp)
  522.     --cas.parse all brackets first
  523.     local parsed={}
  524.     local num
  525.  
  526.     --Brackets
  527.     exp,num=exp:gsub("([%w_]*)%s*(%b())",function(pre,exp)
  528.         local nonfunc=#pre==0
  529.         if not nonfunc then
  530.             local best
  531.             for n,f in next,funcdefs do
  532.                 for _,name in next,f.names do
  533.                     if pre:sub(-#name)==name and (not best or #best<#name) then
  534.                         best=n
  535.                     end
  536.                 end
  537.             end
  538.             for n,f in next,myfuncs do
  539.                 if pre:sub(-#n)==n and (not best or #best<#n) then
  540.                     best=n
  541.                 end
  542.             end
  543.             if best then
  544.                 nonfunc=false
  545.             end
  546.         end
  547.         if nonfunc then
  548.             local n=#parsed+1
  549.             parsed[n]=cas.parse(exp:sub(2,-2))
  550.             return pre.."["..n.."]"
  551.         end
  552.     end)
  553.     if DebugMode then
  554.         print(exp)
  555.     end
  556.  
  557.     --Functions
  558.     exp,num=exp:gsub("([%a_][%w_]*`*)%s*(%b())",function(pre,arglist)
  559.         local best
  560.         for n,f in next,funcdefs do
  561.             for _,name in next,f.names do
  562.                 if pre:sub(-#name)==name and (not best or #best<#name) then
  563.                     best=n
  564.                 end
  565.             end
  566.         end
  567.         if best then
  568.             local n=#parsed+1
  569.             parsed[n]=funcs[best](cas.parse(arglist:sub(2,-2)))
  570.             return pre:sub(1,-#best-1).."["..n.."]"
  571.         else
  572.             for n,f in next,myfuncs do
  573.                 if pre:sub(-#n)==n and (not best or #best<#n) then
  574.                     best=n
  575.                 end
  576.             end
  577.             if best then
  578.                 local n=#parsed+1
  579.                 parsed[n]=myfuncs[best](cas.parse(arglist))
  580.                 return pre:sub(1,-#best-1).."["..n.."]"
  581.             end
  582.         end
  583.     end)
  584.     if DebugMode then
  585.         print(exp,num)
  586.     end
  587.  
  588.     --Variables
  589.     exp,num=exp:gsub("[%a_][%w_]*`*",function(var)
  590.         local n=#parsed+1
  591.         parsed[n]=cas.var(var)
  592.         return "["..n.."]"
  593.     end)
  594.     if DebugMode then
  595.         print(exp)
  596.     end
  597.  
  598.     --Constants
  599.     exp,num=exp:gsub("()%f[%d%.]%s*(%d*%.?%d*e?%d?%d?%d?)%s*%f[^%]%d%.]()",function(_0,cst,_1)
  600.         local v=tonumber(cst)
  601.         if v and exp:sub(_0,_0)~="[" and exp:sub(_1,_1)~="]" then
  602.             local n=#parsed+1
  603.             parsed[n]=cas.cst(v)
  604.             return "["..n.."]"
  605.         else
  606.             return cst
  607.         end
  608.     end)
  609.     if DebugMode then
  610.         print(exp)
  611.     end
  612.  
  613.     --unm
  614.     repeat
  615.         local bad=0
  616.         exp,num=exp:gsub("()%s*%-%s*%[(%d+)%]",function(_0,s1)
  617.             local n1=tonumber(s1)
  618.             if n1 and exp:sub(_0-1,_0-1)~="]" then
  619.                 local n=#parsed+1
  620.                 parsed[n]=-parsed[n1]
  621.                 return "["..n.."]"
  622.             else
  623.                 bad=bad+1
  624.                 return "-["..s1.."]"
  625.             end
  626.         end)
  627.     until num-bad<=0
  628.     if DebugMode then
  629.         print(exp)
  630.     end
  631.  
  632.     --pow
  633.     repeat
  634.         exp,num=exp:gsub("^(.*)%[(%d+)%]%s*%^%s*%[(%d+)%](.-)$",function(pre,s1,s2,aft)
  635.             local n1,n2=tonumber(s1),tonumber(s2)
  636.             local n=#parsed+1
  637.             parsed[n]=parsed[n1]^parsed[n2]
  638.             return pre.."["..n.."]"..aft
  639.         end,1)
  640.     until num==0
  641.     if DebugMode then
  642.         print(exp)
  643.     end
  644.  
  645.     --div
  646.     repeat
  647.         exp,num=exp:gsub("%[(%d+)%]%s*%/%s*%[(%d+)%]",function(s1,s2)
  648.             local n1,n2=tonumber(s1),tonumber(s2)
  649.             local n=#parsed+1
  650.             parsed[n]=parsed[n1]/parsed[n2]
  651.             return "["..n.."]"
  652.         end,1)
  653.     until num==0
  654.     if DebugMode then
  655.         print(exp)
  656.     end
  657.  
  658.     --mul
  659.     repeat
  660.         exp,num=exp:gsub("%[(%d+)%]%s*%*?%s*%[(%d+)%]",function(s1,s2)
  661.             local n1,n2=tonumber(s1),tonumber(s2)
  662.             local n=#parsed+1
  663.             parsed[n]=parsed[n1]*parsed[n2]
  664.             return "["..n.."]"
  665.         end,1)
  666.     until num==0
  667.     if DebugMode then
  668.         print(exp)
  669.     end
  670.  
  671.     --add
  672.     repeat
  673.         exp,num=exp:gsub("%[(%d+)%]%s*%+%s*%[(%d+)%]",function(s1,s2)
  674.             local n1,n2=tonumber(s1),tonumber(s2)
  675.             local n=#parsed+1
  676.             parsed[n]=parsed[n1]+parsed[n2]
  677.             return "["..n.."]"
  678.         end,1)
  679.     until num==0
  680.     if DebugMode then
  681.         print(exp)
  682.     end
  683.  
  684.     --sub
  685.     repeat
  686.         exp,num=exp:gsub("%[(%d+)%]%s*%-%s*%[(%d+)%]",function(s1,s2)
  687.             local n1,n2=tonumber(s1),tonumber(s2)
  688.             local n=#parsed+1
  689.             parsed[n]=parsed[n1]-parsed[n2]
  690.             return "["..n.."]"
  691.         end,1)
  692.     until num==0
  693.     if DebugMode then
  694.         print(exp)
  695.     end
  696.  
  697.     --[[
  698.     repeat
  699.         exp,num=exp:gsub("%[(%d+)%]%s*=%s*%[(%d+)%]",function(s1,s2)
  700.             local n1,n2=tonumber(s1),tonumber(s2)
  701.             local n=#parsed+1
  702.             parsed[n]=cas.eq(parsed[n1],parsed[n2])
  703.             return "["..n.."]"
  704.         end)
  705.     until num==0
  706.     --]]
  707.  
  708.     --Return everything left
  709.     local explist={}
  710.     for s in exp:gmatch'%[(%d+)%]' do
  711.         explist[#explist+1]=parsed[tonumber(s)]
  712.     end
  713.     return unpack(explist)
  714. end
  715.  
  716. _G.cas=cas
  717. return cas
Advertisement
Add Comment
Please, Sign In to add comment