[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: The += operator, again
- From: Gé Weijers <ge@...>
- Date: Mon, 19 Nov 2018 15:23:03 -0800
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 (firstname.lastname@example.org
/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.