lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

And here is some code that converts infix arithmetic expressions into
prefix tables. Here's automatic parsing for you. :-)
The example converts x+y*z into {add,x,{mul,y,z}}.
To use this code with the code I posted earlier, you need to use the
add, sub, etc. functions instead of plain op in arithfb.
Plus you only need the name field for variables, not for subexpressions.
(I should have used tag methods instead of fallbacks, but I had a similar code
already and it's closer to what appears in the SPE paper and the slides.)


function S(a)
 if type(a)=="table" then return else return tostring(a) end

function arithfb(a,b,op)
 return {op,a,b;name="{"..op..","..S(a)..","..S(b).."}"}

function create(v)
 return setglobal(v,{name=v})


-- test

create("x") create("y") create("z")

E=x+y*z        print(