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 <ge@weijers.org> wrote:


On Mon, Nov 19, 2018 at 11:36 AM Felipe Tavares <1542675988.local-94dd2bd2-293f-v1.5.1-da141eaf@getmailspring.com/0?redirect=mailto%3Afelipe.oltavares%40gmail.com&recipient=bHVhLWxAbGlzdHMubHVhLm9yZw%3D%3D" title="mailto:felipe.oltavares@gmail.com">felipe.oltavares@gmail.com> wrote:
Hi!

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 (1542675988.local-94dd2bd2-293f-v1.5.1-da141eaf@getmailspring.com/1?redirect=https%3A%2F%2Flink.getmailspring.com%2Flink%2F1542655470.local-2711d4b8-ab3c-v1.5.1-da141eaf%40getmailspring.com%2F0%3Fredirect%3Dhttp%253A%252F%252Flua-users.org%252Ffiles%252Fwiki_insecure%252Fpower_patches%252F5.2%252Fcompound-5.2.2.patch%26recipient%3DbHVhLWxAbGlzdHMubHVhLm9yZw%253D%253D&recipient=bHVhLWxAbGlzdHMubHVhLm9yZw%3D%3D" title="https://link.getmailspring.com/link/1542655470.local-2711d4b8-ab3c-v1.5.1-da141eaf@getmailspring.com/0?redirect=http%3A%2F%2Flua-users.org%2Ffiles%2Fwiki_insecure%2Fpower_patches%2F5.2%2Fcompound-5.2.2.patch&recipient=bHVhLWxAbGlzdHMubHVhLm9yZw%3D%3D">http://lua-users.org/files/wiki_insecure/power_patches/5.2/compound-5.2.2.patch).

+= 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