-- simple PN math evaluator -- expects input in "input" tokens = {} for token in string.gmatch(input, "[^%s]+") do table.insert(tokens, token) end stack = {} function push(x) table.insert(stack, x) end function pop() local x = stack[#stack]; stack[#stack] = nil; return x end for i = #tokens, 1, -1 do token = tokens[i] if token:match('^[0-9]+$') then push(tonumber(token)) else arg1 = pop() arg2 = pop() if token == '+' then push(arg1+arg2) elseif token == '*' then push(arg1*arg2) else error("Unknown operation: "..token) end end end return pop()