瀏覽代碼

new semantics for `generic for' (with state)

Roberto Ierusalimschy 23 年之前
父節點
當前提交
7b65328c8e
共有 5 個文件被更改,包括 35 次插入31 次删除
  1. 2 3
      ldebug.c
  2. 3 1
      lopcodes.c
  3. 4 3
      lopcodes.h
  4. 13 11
      lparser.c
  5. 13 13
      lvm.c

+ 2 - 3
ldebug.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ldebug.c,v 1.105 2002/03/25 17:47:14 roberto Exp roberto $
+** $Id: ldebug.c,v 1.106 2002/04/04 17:21:31 roberto Exp roberto $
 ** Debug Interface
 ** See Copyright Notice in lua.h
 */
@@ -347,8 +347,7 @@ static Instruction luaG_symbexec (const Proto *pt, int lastpc, int reg) {
         break;
       }
       case OP_TFORLOOP: {
-        checkreg(pt, a+c);
-        checkreg(pt, a+2);  /* at least 2 for table generators */
+        checkreg(pt, a+2+c);
         check(pc+2 < pt->sizecode);  /* check skip */
         break;
       }

+ 3 - 1
lopcodes.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lopcodes.c,v 1.13 2002/03/21 20:32:22 roberto Exp roberto $
+** $Id: lopcodes.c,v 1.14 2002/03/25 17:47:14 roberto Exp roberto $
 ** extracted automatically from lopcodes.h by mkprint.lua
 ** DO NOT EDIT
 ** See Copyright Notice in lua.h
@@ -49,6 +49,7 @@ const char *const luaP_opnames[] = {
   "RETURN",
   "FORLOOP",
   "TFORLOOP",
+  "OP_TFORPREP",
   "SETLIST",
   "SETLISTO",
   "CLOSE",
@@ -98,6 +99,7 @@ const lu_byte luaP_opmodes[NUM_OPCODES] = {
  ,opmode(0,0,0,0, 0,0,iABC)		/* OP_RETURN */
  ,opmode(0,0,0,0, 0,0,iAsBc)		/* OP_FORLOOP */
  ,opmode(0,0,0,0, 0,0,iABC)		/* OP_TFORLOOP */
+ ,opmode(0,0,0,0, 0,0,iABC)		/* OP_TFORPREP */
  ,opmode(0,0,0,0, 0,0,iABc)		/* OP_SETLIST */
  ,opmode(0,0,0,0, 0,0,iABc)		/* OP_SETLISTO */
  ,opmode(0,0,0,0, 0,0,iABC)		/* OP_CLOSE */

+ 4 - 3
lopcodes.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lopcodes.h,v 1.92 2002/03/21 20:32:22 roberto Exp roberto $
+** $Id: lopcodes.h,v 1.93 2002/03/25 17:47:14 roberto Exp roberto $
 ** Opcodes for Lua virtual machine
 ** See Copyright Notice in lua.h
 */
@@ -173,8 +173,9 @@ OP_RETURN,/*	A B	return R(A), ... ,R(A+B-2)	(see (3))	*/
 
 OP_FORLOOP,/*	A sBc	R(A)+=R(A+2); if R(A) <?= R(A+1) then PC+= sBc	*/
 
-OP_TFORLOOP,/*	A C	R(A+1), ... ,R(A+C) := R(A)(); 
-                        if R(A+1) ~= nil then pc++			*/
+OP_TFORLOOP,/*	A C	R(A+2), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2)); 
+                        if R(A+2) ~= nil then pc++			*/
+OP_TFORPREP,/*	A	if type(R(A)) == table then R(A+1):=R(A), R(A):=next */
 
 OP_SETLIST,/*	A Bc	R(A)[Bc-Bc%FPF+i] := R(A+i), 1 <= i <= Bc%FPF+1	*/
 OP_SETLISTO,/*	A Bc							*/

+ 13 - 11
lparser.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lparser.c,v 1.173 2002/03/25 17:47:14 roberto Exp roberto $
+** $Id: lparser.c,v 1.174 2002/04/02 20:34:15 roberto Exp roberto $
 ** Lua Parser
 ** See Copyright Notice in lua.h
 */
@@ -331,6 +331,7 @@ static void enterblock (FuncState *fs, BlockCnt *bl, int isbreakable) {
   bl->upval = 0;
   bl->previous = fs->bl;
   fs->bl = bl;
+  lua_assert(fs->freereg == fs->nactloc);
 }
 
 
@@ -1028,24 +1029,25 @@ static void fornum (LexState *ls, TString *varname, int line) {
 
 
 static void forlist (LexState *ls, TString *indexname) {
-  /* forlist -> NAME {,NAME} IN exp1 DO body */
+  /* forlist -> NAME {,NAME} IN explist1 DO body */
   FuncState *fs = ls->fs;
+  expdesc e;
   int nvars = 0;
   int prep;
   int base = fs->freereg;
-  new_localvarstr(ls, "(for generator)", 0);
-  new_localvar(ls, indexname, ++nvars);
+  new_localvarstr(ls, "(for generator)", nvars++);
+  new_localvarstr(ls, "(for state)", nvars++);
+  new_localvar(ls, indexname, nvars++);
   while (optional(ls, ',')) {
-    new_localvar(ls, str_checkname(ls), ++nvars);
+    new_localvar(ls, str_checkname(ls), nvars++);
     next(ls);
   }
   check(ls, TK_IN);
-  exp1(ls);  /* table */
-  luaK_checkstack(fs, 2);  /* at least two slots, to traverse tables */
-  luaK_reserveregs(fs, nvars);  /* registers for vars */
-  luaK_codeABC(fs, OP_LOADNIL, base+1, base+nvars, 0);
-  adjustlocalvars(ls, nvars+1);  /* scope for control variables */
-  luaK_codeABC(fs, OP_TFORLOOP, base, 0, nvars);
+  adjust_assign(ls, 3, explist1(ls, &e), &e);
+  luaK_reserveregs(fs, nvars - 3);  /* registers for other variables */
+  luaK_codeABC(fs, OP_TFORPREP, base, 0, 0);
+  luaK_codeABC(fs, OP_TFORLOOP, base, 0, nvars - 3);
+  adjustlocalvars(ls, nvars);  /* scope for all variables */
   prep = luaK_jump(fs);
   check(ls, TK_DO);
   block(ls);

+ 13 - 13
lvm.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lvm.c,v 1.222 2002/03/22 16:54:31 roberto Exp roberto $
+** $Id: lvm.c,v 1.223 2002/03/25 17:47:14 roberto Exp roberto $
 ** Lua virtual machine
 ** See Copyright Notice in lua.h
 */
@@ -549,21 +549,21 @@ StkId luaV_execute (lua_State *L) {
         break;
       }
       case OP_TFORLOOP: {
+        setobj(ra+4, ra+2);
+        setobj(ra+3, ra+1);
+        setobj(ra+2, ra);
+        L->top = ra+5;
+        luaD_call(L, ra+2, GETARG_C(i) + 1);
+        L->top = L->ci->top;
+        if (ttype(ra+2) != LUA_TNIL) pc++;  /* skip jump (keep looping) */
+        break;
+      }
+      case OP_TFORPREP: {
         if (ttype(ra) == LUA_TTABLE) {
-          Table *t = hvalue(ra);
-          if (luaH_next(L, t, ra+1))
-            pc++;  /* skip jump (keep looping) */
-        }
-        else if (ttype(ra) == LUA_TFUNCTION) {
           setobj(ra+1, ra);
-          L->top = ra+2;  /* no arguments */
-          luaD_call(L, ra+1, GETARG_C(i));
-          L->top = L->ci->top;
-          if (ttype(ra+1) != LUA_TNIL)
-            pc++;  /* skip jump (keep looping) */
+          setsvalue(ra, luaS_new(L, "next"));
+          luaV_gettable(L, gt(L), ra, ra);
         }
-        else
-          luaD_error(L, "`for' generator must be a table or function");
         break;
       }
       case OP_SETLIST: