lua-users home
lua-l archive

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



On Mon, Nov 19, 2018 at 11:36 AM Felipe Tavares <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 (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" title="http://lua-users.org/files/wiki_insecure/power_patches/5.2/compound-5.2.2.patch" target="_blank">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.


--
--