lua-users home
lua-l archive

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

The assignment operator is implemented in this same function, as:

else { /* assignment -> '=' explist */
int nexps;
checknext(ls, '=');
nexps = explist(ls, &e);
if (nexps != nvars)
adjust_assign(ls, nvars, nexps, &e);
else {
luaK_setoneret(ls->fs, &e); /* close last _expression_ */
luaK_storevar(ls->fs, &lh->v, &e);
return; /* avoid default */

If you take a closer look the important part is:

luaK_storevar(ls->fs, &lh->v, &e);

which is a function in lcode.c that:

** Generate code to store result of _expression_ 'ex' into variable 'var'.
void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {

So if I could just update this _expression_ `e` with a extra addition operation, it should work, keeping all the syntax etc because it's built with the same tools as the rest of the language instead of attacking the problem from the ground up.

That is of course, except if I am missing a big point here.

On Nov 19 2018, at 8:23 pm, Gé Weijers <> wrote:

On Mon, Nov 19, 2018 at 11:36 AM Felipe Tavares <" title="">> wrote:

I am writing an lightweight (and bare bones) implementation of the += operator.

I already got it working, many thanks to the power patch by Olsen (" title="">

+= etc. is not simple to implement if you want to keep the regular Lua semantics, for instance in this case:

t[f()] += 1

This has to be translated into: 

local tmp = f()
t[tmp] = t[tmp] + 1
tmp = nil

If 't' has a metatable it may trigger __index and __newindex metamethod calls, so you can't just obtain the location of t[tmp] and update it.

It may be exceedingly tricky to efficiently generate this kind of code in a single-pass top-down translator, but I have not studied that in detail so I don't know.

Open Tracking