aboutsummaryrefslogtreecommitdiff
path: root/lvm.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-02-22 16:12:46 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-02-22 16:12:46 -0200
commit4d4e6f07c022f94999c52fb593a20fce85c5092a (patch)
tree538175d34df7d5fb0a24c6c4c03b07b34f8d2665 /lvm.c
parent5cecf0ea9f14d9ea5363654c429c3fd7d2c0e1a7 (diff)
downloadlua-4d4e6f07c022f94999c52fb593a20fce85c5092a.tar.gz
lua-4d4e6f07c022f94999c52fb593a20fce85c5092a.tar.bz2
lua-4d4e6f07c022f94999c52fb593a20fce85c5092a.zip
all order operators use a single tag method (<)
Diffstat (limited to 'lvm.c')
-rw-r--r--lvm.c77
1 files changed, 27 insertions, 50 deletions
diff --git a/lvm.c b/lvm.c
index 79f6800e..f88f0cd1 100644
--- a/lvm.c
+++ b/lvm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lvm.c,v 1.87 2000/02/14 16:51:08 roberto Exp roberto $ 2** $Id: lvm.c,v 1.88 2000/02/22 13:31:30 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*/
@@ -138,7 +138,6 @@ void luaV_gettable (lua_State *L, StkId top) {
138 138
139/* 139/*
140** Receives table at *t, index at *(t+1) and value at `top'. 140** Receives table at *t, index at *(t+1) and value at `top'.
141** WARNING: caller must assure 3 extra stack slots (to call a tag method)
142*/ 141*/
143void luaV_settable (lua_State *L, StkId t, StkId top) { 142void luaV_settable (lua_State *L, StkId t, StkId top) {
144 const TObject *im; 143 const TObject *im;
@@ -158,6 +157,7 @@ void luaV_settable (lua_State *L, StkId t, StkId top) {
158 } 157 }
159 /* object is not a table, or it has a `settable' method */ 158 /* object is not a table, or it has a `settable' method */
160 /* prepare arguments and call the tag method */ 159 /* prepare arguments and call the tag method */
160 luaD_checkstack(L, 3);
161 *(top+2) = *(top-1); 161 *(top+2) = *(top-1);
162 *(top+1) = *(t+1); 162 *(top+1) = *(t+1);
163 *(top) = *t; 163 *(top) = *t;
@@ -177,15 +177,13 @@ void luaV_rawsettable (lua_State *L, StkId t) {
177} 177}
178 178
179 179
180/*
181** WARNING: caller must assure 3 extra stack slots (to call a tag method)
182*/
183void luaV_getglobal (lua_State *L, GlobalVar *gv, StkId top) { 180void luaV_getglobal (lua_State *L, GlobalVar *gv, StkId top) {
184 const TObject *value = &gv->value; 181 const TObject *value = &gv->value;
185 TObject *im = luaT_getimbyObj(L, value, IM_GETGLOBAL); 182 TObject *im = luaT_getimbyObj(L, value, IM_GETGLOBAL);
186 if (ttype(im) == LUA_T_NIL) /* is there a tag method? */ 183 if (ttype(im) == LUA_T_NIL) /* is there a tag method? */
187 *top = *value; /* default behavior */ 184 *top = *value; /* default behavior */
188 else { /* tag method */ 185 else { /* tag method */
186 luaD_checkstack(L, 3);
189 *top = *im; 187 *top = *im;
190 ttype(top+1) = LUA_T_STRING; 188 ttype(top+1) = LUA_T_STRING;
191 tsvalue(top+1) = gv->name; /* global name */ 189 tsvalue(top+1) = gv->name; /* global name */
@@ -196,15 +194,13 @@ void luaV_getglobal (lua_State *L, GlobalVar *gv, StkId top) {
196} 194}
197 195
198 196
199/*
200** WARNING: caller must assure 3 extra stack slots (to call a tag method)
201*/
202void luaV_setglobal (lua_State *L, GlobalVar *gv, StkId top) { 197void luaV_setglobal (lua_State *L, GlobalVar *gv, StkId top) {
203 const TObject *oldvalue = &gv->value; 198 const TObject *oldvalue = &gv->value;
204 const TObject *im = luaT_getimbyObj(L, oldvalue, IM_SETGLOBAL); 199 const TObject *im = luaT_getimbyObj(L, oldvalue, IM_SETGLOBAL);
205 if (ttype(im) == LUA_T_NIL) /* is there a tag method? */ 200 if (ttype(im) == LUA_T_NIL) /* is there a tag method? */
206 gv->value = *(top-1); 201 gv->value = *(top-1);
207 else { 202 else {
203 luaD_checkstack(L, 3);
208 *(top+2) = *(top-1); /* new value */ 204 *(top+2) = *(top-1); /* new value */
209 *(top+1) = *oldvalue; 205 *(top+1) = *oldvalue;
210 ttype(top) = LUA_T_STRING; 206 ttype(top) = LUA_T_STRING;
@@ -258,30 +254,31 @@ static int luaV_strcomp (const TaggedString *ls, const TaggedString *rs) {
258 } 254 }
259} 255}
260 256
261void luaV_comparison (lua_State *L) { 257
262 const TObject *l = L->top-2; 258int luaV_lessthan (lua_State *L, TObject *l, TObject *r) {
263 const TObject *r = L->top-1;
264 int result;
265 if (ttype(l) == LUA_T_NUMBER && ttype(r) == LUA_T_NUMBER) 259 if (ttype(l) == LUA_T_NUMBER && ttype(r) == LUA_T_NUMBER)
266 result = nvalue(l) < nvalue(r); 260 return (nvalue(l) < nvalue(r));
267 else if (ttype(l) == LUA_T_STRING && ttype(r) == LUA_T_STRING) 261 else if (ttype(l) == LUA_T_STRING && ttype(r) == LUA_T_STRING)
268 result = luaV_strcomp(tsvalue(l), tsvalue(r)) < 0; 262 return (luaV_strcomp(tsvalue(l), tsvalue(r)) < 0);
269 else { 263 else {
264 /* update top and put arguments in correct order to call Tag Method */
265 if (l<r) /* are arguments in correct order? */
266 L->top = r+1; /* yes; 2nd is on top */
267 else { /* no; exchange them */
268 TObject temp = *r;
269 *r = *l;
270 *l = temp;
271 L->top = l+1; /* 1st is on top */
272 }
270 call_binTM(L, L->top, IM_LT, "unexpected type in comparison"); 273 call_binTM(L, L->top, IM_LT, "unexpected type in comparison");
271 return; 274 L->top--;
275 return (ttype(L->top) != LUA_T_NIL);
272 } 276 }
273 L->top--;
274 if (result) {
275 nvalue(L->top-1) = 1.0;
276 ttype(L->top-1) = LUA_T_NUMBER;
277 }
278 else
279 ttype(L->top-1) = LUA_T_NIL;
280} 277}
281 278
282 279
283#define setbool(o,cond) if (cond) { \ 280#define setbool(o,cond) if (cond) { \
284 ttype(o) = LUA_T_NUMBER; nvalue(o) = 1.0; } \ 281 ttype(o) = LUA_T_NUMBER; nvalue(o) = 1.0; } \
285 else ttype(o) = LUA_T_NIL 282 else ttype(o) = LUA_T_NIL
286 283
287 284
@@ -484,42 +481,22 @@ StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf,
484 481
485 case LTOP: 482 case LTOP:
486 top--; 483 top--;
487 if (ttype(top-1) == LUA_T_NUMBER && ttype(top) == LUA_T_NUMBER) 484 setbool(top-1, luaV_lessthan(L, top-1, top));
488 setbool(top-1, nvalue(top-1) < nvalue(top));
489 else if (ttype(top-1) == LUA_T_STRING && ttype(top) == LUA_T_STRING)
490 setbool(top-1, luaV_strcomp(tsvalue(top-1), tsvalue(top)) < 0);
491 else
492 call_binTM(L, top+1, IM_LT, "unexpected type in comparison");
493 break; 485 break;
494 486
495 case LEOP: 487 case LEOP: /* a <= b === !(b<a) */
496 top--; 488 top--;
497 if (ttype(top-1) == LUA_T_NUMBER && ttype(top) == LUA_T_NUMBER) 489 setbool(top-1, !luaV_lessthan(L, top, top-1));
498 setbool(top-1, nvalue(top-1) <= nvalue(top));
499 else if (ttype(top-1) == LUA_T_STRING && ttype(top) == LUA_T_STRING)
500 setbool(top-1, luaV_strcomp(tsvalue(top-1), tsvalue(top)) <= 0);
501 else
502 call_binTM(L, top+1, IM_LE, "unexpected type in comparison");
503 break; 490 break;
504 491
505 case GTOP: 492 case GTOP: /* a > b === (b<a) */
506 top--; 493 top--;
507 if (ttype(top-1) == LUA_T_NUMBER && ttype(top) == LUA_T_NUMBER) 494 setbool(top-1, luaV_lessthan(L, top, top-1));
508 setbool(top-1, nvalue(top-1) > nvalue(top));
509 else if (ttype(top-1) == LUA_T_STRING && ttype(top) == LUA_T_STRING)
510 setbool(top-1, luaV_strcomp(tsvalue(top-1), tsvalue(top)) > 0);
511 else
512 call_binTM(L, top+1, IM_GT, "unexpected type in comparison");
513 break; 495 break;
514 496
515 case GEOP: 497 case GEOP: /* a >= b === !(a<b) */
516 top--; 498 top--;
517 if (ttype(top-1) == LUA_T_NUMBER && ttype(top) == LUA_T_NUMBER) 499 setbool(top-1, !luaV_lessthan(L, top-1, top));
518 setbool(top-1, nvalue(top-1) >= nvalue(top));
519 else if (ttype(top-1) == LUA_T_STRING && ttype(top) == LUA_T_STRING)
520 setbool(top-1, luaV_strcomp(tsvalue(top-1), tsvalue(top)) >= 0);
521 else
522 call_binTM(L, top+1, IM_GE, "unexpected type in comparison");
523 break; 500 break;
524 501
525 case ADDOP: 502 case ADDOP: