How to start writing an expression evaluator using LPEG in Lua?

190 Views Asked by At

For example, I pass an expression string and a context table and it return a boolean value whether it was true/false. Can I do this using LPEG?

Something similar to this:

context = {
  x = 3,
  y = 3
}
local result = eval("x==y", context)
1

There are 1 best solutions below

0
On

You can write an LPeg grammar that parses the expression into AST, and your eval function reads the AST and evaluates it using the context. See lpeg.re doc for how to get an AST using LPeg.