Arithmetic evaluation: Difference between revisions

Line 787:
You can also display the syntax tree, for example:
<lang j> parse '1+1'</lang>
 
 
=={{header|Lua}}==
 
<lang lua>
require"lpeg"
 
P, R, C, S, V = lpeg.P, lpeg.R, lpeg.C, lpeg.S, lpeg.V
 
expression = P{"expr";
ws = P" "^0,
number = C(R"09"^1) * V"ws",
lp = C"(" * V"ws",
rp = C")" * V"ws",
sym = C(S"+-*/^") * V"ws",
--indirect left recursion is hard, let's go shopping
token = V"number" * (V"sym" * V"expr")^0,
expr = V"lp" * V"expr" * (V"sym" * V"expr")^0 * V"rp" * (V"sym" * V"expr")^0 + V"token"}
 
function concall(a, b, ...)
if(b) then return a .. concall(b, ...)
else return a
end
end
 
print(loadstring("return " .. concall(expression:match(io.read())))()) --proof: lua is a lisp relative
</lang>
 
=={{header|Oz}}==
Anonymous user