[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Cleanly-integrated mutate (operate-and-assign) operators?
- From: Eric Lasota <ejlasota@...>
- Date: Thu, 29 Jul 2010 20:05:07 -0400
Hi,
I noticed there's a power patch for adding mutators to the language, though that came with some caveats like requiring metamethods for mutate operations. I've tinkered around with the compiler a bit and it doesn't seem like it would be too difficult to add operate-and-assign to the language without requiring any special functionality for recognizing them. Aside from being advantageous from an interoperability standpoint (i.e. existing metamethods would function as expected), this would be advantageous from a performance perspective when doing it to tables since the table/key would only need to be evaluated once.
For example:
t[k] += v
.... would become syntactic sugar for:
do
local tempT = t
local tempK = k
local tempV = v
tempT[tempK] = tempT[tempK] + tempV
end
The order of operations might be a point of contention: It's easiest to add this functionality in storevar, but that means the value must be evaluated before the value being operated on is loaded, leading to the order of operations shown above. This also means concat-assigns would require two concat operations instead of the usual optimization to a single concat.
Diffs of this implementation against 5.1.4 as follows:
http://svn.icculus.org/teu/trunk/tertius/src/lua/src/lcode.c?r1=40&r2=243&pathrev=243
http://svn.icculus.org/teu/trunk/tertius/src/lua/src/lcode.h?r1=40&r2=243&pathrev=243
http://svn.icculus.org/teu/trunk/tertius/src/lua/src/llex.c?r1=40&r2=243&pathrev=243
http://svn.icculus.org/teu/trunk/tertius/src/lua/src/llex.h?r1=40&r2=243&pathrev=243
http://svn.icculus.org/teu/trunk/tertius/src/lua/src/lparser.c?r1=40&r2=243&pathrev=243
If anyone's a bit more familiar with the compiler, some proofing of the concat changes in lcode.c would be greatly appreciated, I'm not sure if I did that part properly (despite it testing OK so far).