diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-02-08 15:07:59 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-02-08 15:07:59 -0200 |
commit | fb1cf6ab2d12bd61e3d429830ab20d4ff3c39fd9 (patch) | |
tree | 20aa6d78060600e477164e8d032fd8c33aa7c6d4 | |
parent | 19ca2087de4002600b891cf78b3c9b1d939ba2ca (diff) | |
download | lua-fb1cf6ab2d12bd61e3d429830ab20d4ff3c39fd9.tar.gz lua-fb1cf6ab2d12bd61e3d429830ab20d4ff3c39fd9.tar.bz2 lua-fb1cf6ab2d12bd61e3d429830ab20d4ff3c39fd9.zip |
clearer way to set tables.
-rw-r--r-- | lapi.c | 5 | ||||
-rw-r--r-- | lopcodes.h | 6 | ||||
-rw-r--r-- | lparser.c | 6 | ||||
-rw-r--r-- | lvm.c | 70 | ||||
-rw-r--r-- | lvm.h | 4 |
5 files changed, 46 insertions, 45 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lapi.c,v 1.33 1999/02/03 16:42:42 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 1.34 1999/02/04 17:47:59 roberto Exp roberto $ |
3 | ** Lua API | 3 | ** Lua API |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -181,7 +181,8 @@ lua_Object lua_rawgettable (void) | |||
181 | 181 | ||
182 | void lua_settable (void) { | 182 | void lua_settable (void) { |
183 | checkCparams(3); | 183 | checkCparams(3); |
184 | luaV_settable(L->stack.top-3, 0); | 184 | luaV_settable(L->stack.top-3); |
185 | L->stack.top -= 2; /* pop table and index */ | ||
185 | } | 186 | } |
186 | 187 | ||
187 | 188 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lopcodes.h,v 1.20 1999/02/02 19:41:17 roberto Exp roberto $ | 2 | ** $Id: lopcodes.h,v 1.21 1999/02/04 16:36:16 roberto Exp roberto $ |
3 | ** Opcodes for Lua virtual machine | 3 | ** Opcodes for Lua virtual machine |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -55,8 +55,8 @@ SETGLOBAL,/* b x - VAR[CNST[b]]=x */ | |||
55 | SETGLOBALDUPW,/*w x x VAR[CNST[w]]=x */ | 55 | SETGLOBALDUPW,/*w x x VAR[CNST[w]]=x */ |
56 | SETGLOBALDUP,/* b x x VAR[CNST[b]]=x */ | 56 | SETGLOBALDUP,/* b x x VAR[CNST[b]]=x */ |
57 | 57 | ||
58 | SETTABLE0,/* - v i t - t[i]=v */ | 58 | SETTABLEPOP,/* - v i t - t[i]=v */ |
59 | SETTABLEDUP,/* - v i t v t[i]=v */ | 59 | SETTABPPDUP,/* - v i t v t[i]=v */ |
60 | 60 | ||
61 | SETTABLE,/* b v a_b...a_1 i t a_b...a_1 i t t[i]=v */ | 61 | SETTABLE,/* b v a_b...a_1 i t a_b...a_1 i t t[i]=v */ |
62 | 62 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lparser.c,v 1.15 1999/02/04 16:36:16 roberto Exp roberto $ | 2 | ** $Id: lparser.c,v 1.16 1999/02/04 17:47:59 roberto Exp roberto $ |
3 | ** LL(1) Parser and code generator for Lua | 3 | ** LL(1) Parser and code generator for Lua |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -490,7 +490,7 @@ static void genstorevar (LexState *ls, vardesc *var, OpCode *codes) { | |||
490 | 490 | ||
491 | 491 | ||
492 | static void storevar (LexState *ls, vardesc *var) { | 492 | static void storevar (LexState *ls, vardesc *var) { |
493 | static OpCode codes[] = {SETLOCAL, SETGLOBAL, SETTABLE0}; | 493 | static OpCode codes[] = {SETLOCAL, SETGLOBAL, SETTABLEPOP}; |
494 | genstorevar(ls, var, codes); | 494 | genstorevar(ls, var, codes); |
495 | } | 495 | } |
496 | 496 | ||
@@ -954,7 +954,7 @@ static void exp0 (LexState *ls, vardesc *v) { | |||
954 | 954 | ||
955 | static void Gexp (LexState *ls, vardesc *v) { | 955 | static void Gexp (LexState *ls, vardesc *v) { |
956 | /* Gexp -> exp0 | var '=' exp1 */ | 956 | /* Gexp -> exp0 | var '=' exp1 */ |
957 | static OpCode codes[] = {SETLOCALDUP, SETGLOBALDUP, SETTABLEDUP}; | 957 | static OpCode codes[] = {SETLOCALDUP, SETGLOBALDUP, SETTABPPDUP}; |
958 | exp0(ls, v); | 958 | exp0(ls, v); |
959 | if (v->k != VEXP && optional(ls, '=')) { /* assignment expression? */ | 959 | if (v->k != VEXP && optional(ls, '=')) { /* assignment expression? */ |
960 | unloaddot(ls, v); | 960 | unloaddot(ls, v); |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lvm.c,v 1.44 1999/02/04 16:36:16 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 1.45 1999/02/04 17:47:59 roberto Exp roberto $ |
3 | ** Lua virtual machine | 3 | ** Lua virtual machine |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -110,64 +110,62 @@ void luaV_closure (int nelems) { | |||
110 | ** Receives the table at top-2 and the index at top-1. | 110 | ** Receives the table at top-2 and the index at top-1. |
111 | */ | 111 | */ |
112 | void luaV_gettable (void) { | 112 | void luaV_gettable (void) { |
113 | struct Stack *S = &L->stack; | 113 | TObject *table = L->stack.top-2; |
114 | TObject *im; | 114 | TObject *im; |
115 | if (ttype(S->top-2) != LUA_T_ARRAY) /* not a table, get "gettable" method */ | 115 | if (ttype(table) != LUA_T_ARRAY) { /* not a table, get gettable method */ |
116 | im = luaT_getimbyObj(S->top-2, IM_GETTABLE); | 116 | im = luaT_getimbyObj(table, IM_GETTABLE); |
117 | if (ttype(im) == LUA_T_NIL) | ||
118 | lua_error("indexed expression not a table"); | ||
119 | } | ||
117 | else { /* object is a table... */ | 120 | else { /* object is a table... */ |
118 | int tg = (S->top-2)->value.a->htag; | 121 | int tg = table->value.a->htag; |
119 | im = luaT_getim(tg, IM_GETTABLE); | 122 | im = luaT_getim(tg, IM_GETTABLE); |
120 | if (ttype(im) == LUA_T_NIL) { /* and does not have a "gettable" method */ | 123 | if (ttype(im) == LUA_T_NIL) { /* and does not have a "gettable" method */ |
121 | TObject *h = luaH_get(avalue(S->top-2), S->top-1); | 124 | TObject *h = luaH_get(avalue(table), table+1); |
122 | if (ttype(h) != LUA_T_NIL) { | 125 | if (ttype(h) == LUA_T_NIL && |
123 | --S->top; | 126 | (ttype(im=luaT_getim(tg, IM_INDEX)) != LUA_T_NIL)) { |
124 | *(S->top-1) = *h; | 127 | /* result is nil and there is an "index" tag method */ |
128 | luaD_callTM(im, 2, 1); /* calls it */ | ||
125 | } | 129 | } |
126 | else if (ttype(im=luaT_getim(tg, IM_INDEX)) != LUA_T_NIL) | ||
127 | luaD_callTM(im, 2, 1); | ||
128 | else { | 130 | else { |
129 | --S->top; | 131 | L->stack.top--; |
130 | ttype(S->top-1) = LUA_T_NIL; | 132 | *table = *h; /* "push" result into table position */ |
131 | } | 133 | } |
132 | return; | 134 | return; |
133 | } | 135 | } |
134 | /* else it has a "gettable" method, go through to next command */ | 136 | /* else it has a "gettable" method, go through to next command */ |
135 | } | 137 | } |
136 | /* object is not a table, or it has a "gettable" method */ | 138 | /* object is not a table, or it has a "gettable" method */ |
137 | if (ttype(im) == LUA_T_NIL) | ||
138 | lua_error("indexed expression not a table"); | ||
139 | luaD_callTM(im, 2, 1); | 139 | luaD_callTM(im, 2, 1); |
140 | } | 140 | } |
141 | 141 | ||
142 | 142 | ||
143 | /* | 143 | /* |
144 | ** Function to store indexed based on values at the stack.top | 144 | ** Receives table at *t, index at *(t+1) and value at top. |
145 | ** deep = 1: "deep L->stack.stack" store (with tag methods) | ||
146 | */ | 145 | */ |
147 | void luaV_settable (TObject *t, int deep) { | 146 | void luaV_settable (TObject *t) { |
148 | struct Stack *S = &L->stack; | 147 | struct Stack *S = &L->stack; |
149 | TObject *im; | 148 | TObject *im; |
150 | if (ttype(t) != LUA_T_ARRAY) /* not a table, get "settable" method */ | 149 | if (ttype(t) != LUA_T_ARRAY) { /* not a table, get "settable" method */ |
151 | im = luaT_getimbyObj(t, IM_SETTABLE); | 150 | im = luaT_getimbyObj(t, IM_SETTABLE); |
151 | if (ttype(im) == LUA_T_NIL) | ||
152 | lua_error("indexed expression not a table"); | ||
153 | } | ||
152 | else { /* object is a table... */ | 154 | else { /* object is a table... */ |
153 | im = luaT_getim(avalue(t)->htag, IM_SETTABLE); | 155 | im = luaT_getim(avalue(t)->htag, IM_SETTABLE); |
154 | if (ttype(im) == LUA_T_NIL) { /* and does not have a "settable" method */ | 156 | if (ttype(im) == LUA_T_NIL) { /* and does not have a "settable" method */ |
155 | luaH_set(avalue(t), t+1, S->top-1); | 157 | luaH_set(avalue(t), t+1, S->top-1); |
156 | /* if deep, pop only value; otherwise, pop table, index and value */ | 158 | S->top--; /* pop value */ |
157 | S->top -= (deep) ? 1 : 3; | ||
158 | return; | 159 | return; |
159 | } | 160 | } |
160 | /* else it has a "settable" method, go through to next command */ | 161 | /* else it has a "settable" method, go through to next command */ |
161 | } | 162 | } |
162 | /* object is not a table, or it has a "settable" method */ | 163 | /* object is not a table, or it has a "settable" method */ |
163 | if (ttype(im) == LUA_T_NIL) | 164 | /* prepare arguments and call the tag method */ |
164 | lua_error("indexed expression not a table"); | 165 | *(S->top+1) = *(L->stack.top-1); |
165 | if (deep) { /* table and index were not on top; copy them */ | 166 | *(S->top) = *(t+1); |
166 | *(S->top+1) = *(L->stack.top-1); | 167 | *(S->top-1) = *t; |
167 | *(S->top) = *(t+1); | 168 | S->top += 2; /* WARNING: caller must assure stack space */ |
168 | *(S->top-1) = *t; | ||
169 | S->top += 2; /* WARNING: caller must assure stack space */ | ||
170 | } | ||
171 | luaD_callTM(im, 3, 0); | 169 | luaD_callTM(im, 3, 0); |
172 | } | 170 | } |
173 | 171 | ||
@@ -421,19 +419,21 @@ StkId luaV_execute (Closure *cl, TProtoFunc *tf, StkId base) { | |||
421 | luaV_setglobal(tsvalue(&consts[aux])); | 419 | luaV_setglobal(tsvalue(&consts[aux])); |
422 | break; | 420 | break; |
423 | 421 | ||
424 | case SETTABLE0: | 422 | case SETTABLEPOP: |
425 | luaV_settable(S->top-3, 0); | 423 | luaV_settable(S->top-3); |
424 | S->top -= 2; /* pop table and index */ | ||
426 | break; | 425 | break; |
427 | 426 | ||
428 | case SETTABLEDUP: { | 427 | case SETTABPPDUP: { |
429 | TObject temp = *(S->top-1); | 428 | TObject temp = *(S->top-1); |
430 | luaV_settable(S->top-3, 0); | 429 | luaV_settable(S->top-3); |
431 | *(S->top++) = temp; | 430 | S->top--; /* pop index (temp goes into "table" position) */ |
431 | *(S->top-1) = temp; | ||
432 | break; | 432 | break; |
433 | } | 433 | } |
434 | 434 | ||
435 | case SETTABLE: | 435 | case SETTABLE: |
436 | luaV_settable(S->top-3-(*pc++), 1); | 436 | luaV_settable(S->top-3-(*pc++)); |
437 | break; | 437 | break; |
438 | 438 | ||
439 | case SETLISTW: aux += highbyte(*pc++); | 439 | case SETLISTW: aux += highbyte(*pc++); |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lvm.h,v 1.6 1998/12/30 13:16:50 roberto Exp roberto $ | 2 | ** $Id: lvm.h,v 1.7 1998/12/30 17:26:49 roberto Exp roberto $ |
3 | ** Lua virtual machine | 3 | ** Lua virtual machine |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -22,7 +22,7 @@ int luaV_tonumber (TObject *obj); | |||
22 | int luaV_tostring (TObject *obj); | 22 | int luaV_tostring (TObject *obj); |
23 | void luaV_setn (Hash *t, int val); | 23 | void luaV_setn (Hash *t, int val); |
24 | void luaV_gettable (void); | 24 | void luaV_gettable (void); |
25 | void luaV_settable (TObject *t, int deep); | 25 | void luaV_settable (TObject *t); |
26 | void luaV_rawsettable (TObject *t); | 26 | void luaV_rawsettable (TObject *t); |
27 | void luaV_getglobal (TaggedString *ts); | 27 | void luaV_getglobal (TaggedString *ts); |
28 | void luaV_setglobal (TaggedString *ts); | 28 | void luaV_setglobal (TaggedString *ts); |