mirror of https://github.com/rusefi/lua.git
new semantics for `generic for' (with state)
This commit is contained in:
parent
d2e05589d7
commit
7b65328c8e
5
ldebug.c
5
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
|
** Debug Interface
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
@ -347,8 +347,7 @@ static Instruction luaG_symbexec (const Proto *pt, int lastpc, int reg) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case OP_TFORLOOP: {
|
case OP_TFORLOOP: {
|
||||||
checkreg(pt, a+c);
|
checkreg(pt, a+2+c);
|
||||||
checkreg(pt, a+2); /* at least 2 for table generators */
|
|
||||||
check(pc+2 < pt->sizecode); /* check skip */
|
check(pc+2 < pt->sizecode); /* check skip */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
** extracted automatically from lopcodes.h by mkprint.lua
|
||||||
** DO NOT EDIT
|
** DO NOT EDIT
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
|
@ -49,6 +49,7 @@ const char *const luaP_opnames[] = {
|
||||||
"RETURN",
|
"RETURN",
|
||||||
"FORLOOP",
|
"FORLOOP",
|
||||||
"TFORLOOP",
|
"TFORLOOP",
|
||||||
|
"OP_TFORPREP",
|
||||||
"SETLIST",
|
"SETLIST",
|
||||||
"SETLISTO",
|
"SETLISTO",
|
||||||
"CLOSE",
|
"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,iABC) /* OP_RETURN */
|
||||||
,opmode(0,0,0,0, 0,0,iAsBc) /* OP_FORLOOP */
|
,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_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_SETLIST */
|
||||||
,opmode(0,0,0,0, 0,0,iABc) /* OP_SETLISTO */
|
,opmode(0,0,0,0, 0,0,iABc) /* OP_SETLISTO */
|
||||||
,opmode(0,0,0,0, 0,0,iABC) /* OP_CLOSE */
|
,opmode(0,0,0,0, 0,0,iABC) /* OP_CLOSE */
|
||||||
|
|
|
@ -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
|
** Opcodes for Lua virtual machine
|
||||||
** See Copyright Notice in lua.h
|
** 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_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)();
|
OP_TFORLOOP,/* A C R(A+2), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2));
|
||||||
if R(A+1) ~= nil then pc++ */
|
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_SETLIST,/* A Bc R(A)[Bc-Bc%FPF+i] := R(A+i), 1 <= i <= Bc%FPF+1 */
|
||||||
OP_SETLISTO,/* A Bc */
|
OP_SETLISTO,/* A Bc */
|
||||||
|
|
24
lparser.c
24
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
|
** Lua Parser
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
@ -331,6 +331,7 @@ static void enterblock (FuncState *fs, BlockCnt *bl, int isbreakable) {
|
||||||
bl->upval = 0;
|
bl->upval = 0;
|
||||||
bl->previous = fs->bl;
|
bl->previous = fs->bl;
|
||||||
fs->bl = 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) {
|
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;
|
FuncState *fs = ls->fs;
|
||||||
|
expdesc e;
|
||||||
int nvars = 0;
|
int nvars = 0;
|
||||||
int prep;
|
int prep;
|
||||||
int base = fs->freereg;
|
int base = fs->freereg;
|
||||||
new_localvarstr(ls, "(for generator)", 0);
|
new_localvarstr(ls, "(for generator)", nvars++);
|
||||||
new_localvar(ls, indexname, ++nvars);
|
new_localvarstr(ls, "(for state)", nvars++);
|
||||||
|
new_localvar(ls, indexname, nvars++);
|
||||||
while (optional(ls, ',')) {
|
while (optional(ls, ',')) {
|
||||||
new_localvar(ls, str_checkname(ls), ++nvars);
|
new_localvar(ls, str_checkname(ls), nvars++);
|
||||||
next(ls);
|
next(ls);
|
||||||
}
|
}
|
||||||
check(ls, TK_IN);
|
check(ls, TK_IN);
|
||||||
exp1(ls); /* table */
|
adjust_assign(ls, 3, explist1(ls, &e), &e);
|
||||||
luaK_checkstack(fs, 2); /* at least two slots, to traverse tables */
|
luaK_reserveregs(fs, nvars - 3); /* registers for other variables */
|
||||||
luaK_reserveregs(fs, nvars); /* registers for vars */
|
luaK_codeABC(fs, OP_TFORPREP, base, 0, 0);
|
||||||
luaK_codeABC(fs, OP_LOADNIL, base+1, base+nvars, 0);
|
luaK_codeABC(fs, OP_TFORLOOP, base, 0, nvars - 3);
|
||||||
adjustlocalvars(ls, nvars+1); /* scope for control variables */
|
adjustlocalvars(ls, nvars); /* scope for all variables */
|
||||||
luaK_codeABC(fs, OP_TFORLOOP, base, 0, nvars);
|
|
||||||
prep = luaK_jump(fs);
|
prep = luaK_jump(fs);
|
||||||
check(ls, TK_DO);
|
check(ls, TK_DO);
|
||||||
block(ls);
|
block(ls);
|
||||||
|
|
26
lvm.c
26
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
|
** Lua virtual machine
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
@ -549,21 +549,21 @@ StkId luaV_execute (lua_State *L) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case OP_TFORLOOP: {
|
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) {
|
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);
|
setobj(ra+1, ra);
|
||||||
L->top = ra+2; /* no arguments */
|
setsvalue(ra, luaS_new(L, "next"));
|
||||||
luaD_call(L, ra+1, GETARG_C(i));
|
luaV_gettable(L, gt(L), ra, ra);
|
||||||
L->top = L->ci->top;
|
|
||||||
if (ttype(ra+1) != LUA_TNIL)
|
|
||||||
pc++; /* skip jump (keep looping) */
|
|
||||||
}
|
}
|
||||||
else
|
|
||||||
luaD_error(L, "`for' generator must be a table or function");
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case OP_SETLIST:
|
case OP_SETLIST:
|
||||||
|
|
Loading…
Reference in New Issue