list constructors do not adjust last expression

This commit is contained in:
Roberto Ierusalimschy 2001-04-06 15:25:00 -03:00
parent 2112142680
commit a68635a919
6 changed files with 46 additions and 58 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lbaselib.c,v 1.30 2001/03/07 12:43:52 roberto Exp roberto $ ** $Id: lbaselib.c,v 1.31 2001/03/26 14:31:49 roberto Exp roberto $
** Basic library ** Basic library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -366,30 +366,19 @@ static int luaB_require (lua_State *L) {
} }
static int luaB_pack (lua_State *L) { static int aux_unwrap (lua_State *L, int arg) {
int n = lua_gettop(L);
lua_newtable(L);
aux_setn(L, -3, n);
lua_insert(L, 1);
while (n)
lua_rawseti(L, 1, n--);
return 1;
}
static int aux_unpack (lua_State *L, int arg) {
int n, i; int n, i;
luaL_checktype(L, arg, LUA_TTABLE); luaL_checktype(L, arg, LUA_TTABLE);
n = lua_getn(L, arg); n = lua_getn(L, arg);
luaL_checkstack(L, n, l_s("too many arguments")); luaL_checkstack(L, n, l_s("table too big to unwrap"));
for (i=1; i<=n; i++) /* push arg[1...n] */ for (i=1; i<=n; i++) /* push arg[1...n] */
lua_rawgeti(L, arg, i); lua_rawgeti(L, arg, i);
return n; return n;
} }
static int luaB_unpack (lua_State *L) { static int luaB_unwrap (lua_State *L) {
return aux_unpack(L, 1); return aux_unwrap(L, 1);
} }
@ -408,7 +397,7 @@ static int luaB_call (lua_State *L) {
oldtop = lua_gettop(L); /* top before function-call preparation */ oldtop = lua_gettop(L); /* top before function-call preparation */
/* push function */ /* push function */
lua_pushvalue(L, 1); lua_pushvalue(L, 1);
n = aux_unpack(L, 2); /* push arg[1...n] */ n = aux_unwrap(L, 2); /* push arg[1...n] */
status = lua_call(L, n, LUA_MULTRET); status = lua_call(L, n, LUA_MULTRET);
if (err != 0) { /* restore old error method */ if (err != 0) { /* restore old error method */
lua_pushvalue(L, err); lua_pushvalue(L, err);
@ -762,8 +751,7 @@ static const luaL_reg base_funcs[] = {
{l_s("sort"), luaB_sort}, {l_s("sort"), luaB_sort},
{l_s("tinsert"), luaB_tinsert}, {l_s("tinsert"), luaB_tinsert},
{l_s("tremove"), luaB_tremove}, {l_s("tremove"), luaB_tremove},
{l_s("pack"), luaB_pack}, {l_s("unwrap"), luaB_unwrap},
{l_s("unpack"), luaB_unpack},
{l_s("xtype"), luaB_xtype}, {l_s("xtype"), luaB_xtype},
}; };

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lcode.c,v 1.65 2001/03/07 13:22:55 roberto Exp roberto $ ** $Id: lcode.c,v 1.66 2001/03/26 14:31:49 roberto Exp roberto $
** Code generator for Lua ** Code generator for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -474,13 +474,11 @@ int luaK_code2 (FuncState *fs, OpCode o, int arg1, int arg2) {
break; break;
} }
case OP_SETLIST: { case OP_SETLIST: {
if (arg2 == 0) return NO_JUMP; /* nothing to do */ pop = fs->stacklevel - 1 - arg2;
pop = arg2;
break; break;
} }
case OP_SETMAP: { case OP_SETMAP: {
if (arg1 == 0) return NO_JUMP; /* nothing to do */ pop = fs->stacklevel - 1 - arg1;
pop = 2*arg1;
break; break;
} }
case OP_PUSHNIL: { case OP_PUSHNIL: {

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ldebug.c,v 1.74 2001/03/07 18:09:25 roberto Exp roberto $ ** $Id: ldebug.c,v 1.75 2001/03/26 14:31:49 roberto Exp roberto $
** Debug Interface ** Debug Interface
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -419,13 +419,13 @@ static Instruction luaG_symbexec (lua_State *L, const Proto *pt,
break; break;
} }
case OP_SETLIST: { case OP_SETLIST: {
pop = arg2; check(arg2 >= 0);
check(top-pop >= 1); /* there must be a table below the list */ pop = top-arg2-1;
break; break;
} }
case OP_SETMAP: { case OP_SETMAP: {
pop = 2*arg1; check(arg1 >= 0);
check(top-pop >= 1); pop = top-arg1-1;
break; break;
} }
case OP_CONCAT: { case OP_CONCAT: {

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lopcodes.h,v 1.70 2001/01/15 16:13:24 roberto Exp roberto $ ** $Id: lopcodes.h,v 1.71 2001/03/07 13:22:55 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
*/ */
@ -110,8 +110,8 @@ OP_SETLOCAL,/* L x - LOC[l]=x */
OP_SETGLOBAL,/* K x - VAR[KSTR[k]]=x */ OP_SETGLOBAL,/* K x - VAR[KSTR[k]]=x */
OP_SETTABLE,/* A B v a_a-a_1 i t (pops b values) t[i]=v */ OP_SETTABLE,/* A B v a_a-a_1 i t (pops b values) t[i]=v */
OP_SETLIST,/* A B v_b-v_1 t t t[i+a*FPF]=v_i */ OP_SETLIST,/* A B v_n-v_1 v_b v_b v_b[i+a*FPF]=v_i */
OP_SETMAP,/* U v_u k_u - v_1 k_1 t t t[k_i]=v_i */ OP_SETMAP,/* U v_n k_n - v_1 k_1 v_u v_u v_u[k_i]=v_i */
OP_ADD,/* - y x x+y */ OP_ADD,/* - y x x+y */
OP_ADDI,/* S x x+s */ OP_ADDI,/* S x x+s */

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lparser.c,v 1.140 2001/03/26 14:31:49 roberto Exp roberto $ ** $Id: lparser.c,v 1.141 2001/04/05 16:49:14 roberto Exp roberto $
** LL(1) Parser and code generator for Lua ** LL(1) Parser and code generator for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -465,39 +465,40 @@ static void recfield (LexState *ls) {
static int recfields (LexState *ls) { static int recfields (LexState *ls) {
/* recfields -> recfield { `,' recfield } [`,'] */ /* recfields -> recfield { `,' recfield } [`,'] */
FuncState *fs = ls->fs; FuncState *fs = ls->fs;
int t = fs->stacklevel-1; /* level of table on the stack */
int n = 1; /* at least one element */ int n = 1; /* at least one element */
recfield(ls); recfield(ls);
while (ls->t.token == l_c(',')) { while (ls->t.token == l_c(',') &&
next(ls); (next(ls), (ls->t.token != l_c(';') && ls->t.token != l_c('}')))) {
if (ls->t.token == l_c(';') || ls->t.token == l_c('}')) if (n%RFIELDS_PER_FLUSH == 0)
break; luaK_code1(fs, OP_SETMAP, t);
recfield(ls); recfield(ls);
n++; n++;
if (n%RFIELDS_PER_FLUSH == 0)
luaK_code1(fs, OP_SETMAP, RFIELDS_PER_FLUSH);
} }
luaK_code1(fs, OP_SETMAP, n%RFIELDS_PER_FLUSH); luaK_code1(fs, OP_SETMAP, t);
return n; return n;
} }
static int listfields (LexState *ls) { static int listfields (LexState *ls) {
/* listfields -> exp1 { `,' exp1 } [`,'] */ /* listfields -> exp1 { `,' exp1 } [`,'] */
expdesc v;
FuncState *fs = ls->fs; FuncState *fs = ls->fs;
int t = fs->stacklevel-1; /* level of table on the stack */
int n = 1; /* at least one element */ int n = 1; /* at least one element */
exp1(ls); expr(ls, &v);
while (ls->t.token == l_c(',')) { while (ls->t.token == l_c(',') &&
next(ls); (next(ls), (ls->t.token != l_c(';') && ls->t.token != l_c('}')))) {
if (ls->t.token == l_c(';') || ls->t.token == l_c('}')) luaK_tostack(ls, &v, 1); /* only one value from intermediate expressions */
break;
exp1(ls);
n++;
luaX_checklimit(ls, n/LFIELDS_PER_FLUSH, MAXARG_A, luaX_checklimit(ls, n/LFIELDS_PER_FLUSH, MAXARG_A,
l_s("`item groups' in a list initializer")); l_s("`item groups' in a list initializer"));
if (n%LFIELDS_PER_FLUSH == 0) if (n%LFIELDS_PER_FLUSH == 0)
luaK_code2(fs, OP_SETLIST, n/LFIELDS_PER_FLUSH - 1, LFIELDS_PER_FLUSH); luaK_code2(fs, OP_SETLIST, (n-1)/LFIELDS_PER_FLUSH, t);
expr(ls, &v);
n++;
} }
luaK_code2(fs, OP_SETLIST, n/LFIELDS_PER_FLUSH, n%LFIELDS_PER_FLUSH); luaK_tostack(ls, &v, 0); /* allow multiple values for last expression */
luaK_code2(fs, OP_SETLIST, (n-1)/LFIELDS_PER_FLUSH, t);
return n; return n;
} }

19
lvm.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lvm.c,v 1.176 2001/03/07 18:16:22 roberto Exp roberto $ ** $Id: lvm.c,v 1.177 2001/03/26 14:31:49 roberto Exp roberto $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -446,18 +446,19 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
} }
case OP_SETLIST: { case OP_SETLIST: {
int aux = GETARG_A(i) * LFIELDS_PER_FLUSH; int aux = GETARG_A(i) * LFIELDS_PER_FLUSH;
int n = GETARG_B(i); TObject *t = base+GETARG_B(i);
Hash *arr = hvalue(top-n-1); Hash *h = hvalue(t);
for (; n; n--) int n;
setobj(luaH_setnum(L, arr, n+aux), --top); for (n = top-t-1; n; n--)
setobj(luaH_setnum(L, h, n+aux), --top);
break; break;
} }
case OP_SETMAP: { case OP_SETMAP: {
int n = GETARG_U(i); TObject *t = base+GETARG_U(i);
Hash *arr = hvalue((top-2*n)-1); Hash *h = hvalue(t);
for (; n; n--) { while (top-1 > t) {
top-=2; top-=2;
setobj(luaH_set(L, arr, top), top+1); setobj(luaH_set(L, h, top), top+1);
} }
break; break;
} }