lua-users home
lua-l archive

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


Mark Hamburg <mark@grubmah.com> wrote:
>
> The other thing I find myself wanting is:
>
> 	obj:[ method_expr ]( ... )
>
> as syntax for obj[ method_expr ]( obj, ... ).
>
> This is actually easier to implement than obj:method as a way to create
> a closure since it mostly just takes as I recall some small parser
> revisions and a slight revision to OP_SELF.

Here's one I prepared earlier...

--- a/src/lcode.c
+++ b/src/lcode.c
@@ -500,16 +500,16 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
 }


-void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
-  int func;
+void luaK_self1 (FuncState *fs, expdesc *e) {
   luaK_exp2anyreg(fs, e);
   freeexp(fs, e);
-  func = fs->freereg;
   luaK_reserveregs(fs, 2);
-  luaK_codeABC(fs, OP_SELF, func, e->u.s.info, luaK_exp2RK(fs, key));
+}
+
+
+void luaK_self2 (FuncState *fs, expdesc *e, expdesc *key) {
+  luaK_codeABC(fs, OP_SELF, e->u.s.info, e->u.s.info, luaK_exp2RK(fs, key));
   freeexp(fs, key);
-  e->u.s.info = func;
-  e->k = VNONRELOC;
 }


--- a/src/lcode.h
+++ b/src/lcode.h
@@ -55,7 +55,8 @@ LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e);
 LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e);
 LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e);
 LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e);
-LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key);
+LUAI_FUNC void luaK_self1 (FuncState *fs, expdesc *e);
+LUAI_FUNC void luaK_self2 (FuncState *fs, expdesc *e, expdesc *key);
 LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k);
 LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e);
 LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e);
--- a/src/lparser.c
+++ b/src/lparser.c
@@ -780,8 +780,12 @@ static void primaryexp (LexState *ls, expdesc *v) {
       case ':': {  /* `:' NAME funcargs */
         expdesc key;
         luaX_next(ls);
-        checkname(ls, &key);
-        luaK_self(fs, v, &key);
+        luaK_self1(fs, v);
+        if (ls->t.token == '[')
+          yindex(ls, &key);
+        else
+          checkname(ls, &key);
+        luaK_self2(fs, v, &key);
         funcargs(ls, v);
         break;
       }

Tony.
-- 
f.anthony.n.finch  <dot@dotat.at>  http://dotat.at/
West FitzRoy, West Sole: Southerly 4 or 5, increasing 6 or 7, perhaps gale 8
later. Moderate or rough. Rain or showers. Good, occasionally poor.