lua-users home
lua-l archive

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


On Tue, Aug 4, 2009 at 12:13 PM, Leo Razoumov<slonik.az@gmail.com> wrote:
> C++ has also "expressions template" approach which uses template
> mechanism to rewrite expressions but  it is compile time thing and is
> useless for Lua expressions that are run-time entities.

But we do have such facilities, as the posts by Javier and David M point out.

There is either 'express as string' or 'express as abstract expression
using operators'; the first is a little awkward, and controlling
variable scope is tricky.  So I would go with the second approach. It
is not difficult to construct 'AST' representations of Lua
expressions:

http://lua-users.org/wiki/SymbolicDifferentiation

Using that, one can rewrite the Lua expression (assuming vectors, for
simplicity)

u = 1 + 3*v*u

as the C code:

for (i = 0; i < N; i++)
  u[i] = 1 + 3*v[i]*u[i];

Then you 'just' have to compile the C on the fly, initially:

http://lua-users.org/lists/lua-l/2009-07/msg00073.html

And voila, you can have fast numerics in Lua without doing any
unnecessary allocation.

steve d.

PS I would do this over the weekend, but my coding stack is full ;)