tl;dr; is there a simple way to evaluate a string as either a Lua chunk or a Lua expression without having to first manually figure out which it is?
I’m using the Lua C API (i.e. luaL_dostring
) to evaluate externally supplied strings for there side effects and I have it almost working. The remaining issue is that luaL_dostring
assumes that it’s provided with a chunk (which I want to support in some complex cases) that assumption is causing issues in simple cases with expressions that are more complicated than a simple function call:
Examples:
Fn()
worksFn() and Thing()
FAILSif Fn() then Thing() end
worksFn1() and Fn2() and Fn3()
FAILSif Fn1() and Fn2() and Fn3() then ; end
worksif Fn1() and Fn2() then Fn3() end
worksreturn Fn1() and Fn2() and Fn3()
works
I could reasonably require the use of #3 in place of #2, but in more complicated but common case of wanting to evaluate a sequence of function calls up to the first that returns false things become more complicated (e.g. #4 becomes one of #5 thought #7) and/or quickly diverge from “it does what it says on the tin” (the expression is evaluated for it’s side effects so requiring “returning” something is an unwanted distraction for most users).
After a bit of playing around, I’ve been able to find string manglings that allows each of those to work, but no one thing that makes all of the above work.