aboutsummaryrefslogtreecommitdiff
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
parent5cecf0ea9f14d9ea5363654c429c3fd7d2c0e1a7 (diff)
downloadlua-4d4e6f07c022f94999c52fb593a20fce85c5092a.tar.gz
lua-4d4e6f07c022f94999c52fb593a20fce85c5092a.tar.bz2
lua-4d4e6f07c022f94999c52fb593a20fce85c5092a.zip
all order operators use a single tag method (<)
-rw-r--r--lbuiltin.c13
-rw-r--r--ltm.c40
-rw-r--r--ltm.h7
-rw-r--r--lvm.c77
-rw-r--r--lvm.h4
-rw-r--r--makefile6
6 files changed, 68 insertions, 79 deletions
diff --git a/lbuiltin.c b/lbuiltin.c
index 260dd8a7..7e0c832c 100644
--- a/lbuiltin.c
+++ b/lbuiltin.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbuiltin.c,v 1.91 1999/12/30 18:27:03 roberto Exp roberto $ 2** $Id: lbuiltin.c,v 1.92 2000/01/19 16:50:30 roberto Exp roberto $
3** Built-in functions 3** Built-in functions
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -539,13 +539,16 @@ static int sort_comp (lua_State *L, lua_Object f, const TObject *a,
539 *(L->top+2) = *b; 539 *(L->top+2) = *b;
540 L->top += 3; 540 L->top += 3;
541 luaD_call(L, L->top-3, 1); 541 luaD_call(L, L->top-3, 1);
542 L->top--;
543 return (ttype(L->top) != LUA_T_NIL);
542 } 544 }
543 else { /* a < b? */ 545 else { /* a < b? */
544 *(L->top++) = *a; 546 int res;
545 *(L->top++) = *b; 547 *(L->top) = *a;
546 luaV_comparison(L); 548 *(L->top+1) = *b;
549 res = luaV_lessthan(L, L->top, L->top+1);
550 return res;
547 } 551 }
548 return ttype(--(L->top)) != LUA_T_NIL;
549} 552}
550 553
551static void auxsort (lua_State *L, Hash *a, int l, int u, lua_Object f) { 554static void auxsort (lua_State *L, Hash *a, int l, int u, lua_Object f) {
diff --git a/ltm.c b/ltm.c
index 93a806d8..f9812bc9 100644
--- a/ltm.c
+++ b/ltm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltm.c,v 1.30 1999/12/23 18:19:57 roberto Exp roberto $ 2** $Id: ltm.c,v 1.31 2000/01/19 12:00:45 roberto Exp roberto $
3** Tag methods 3** Tag methods
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -18,9 +18,8 @@
18 18
19 19
20const char *const luaT_eventname[] = { /* ORDER IM */ 20const char *const luaT_eventname[] = { /* ORDER IM */
21 "gettable", "settable", "index", "getglobal", "setglobal", "add", 21 "gettable", "settable", "index", "getglobal", "setglobal", "add", "sub",
22 "sub", "mul", "div", "pow", "unm", "lt", "le", "gt", "ge", 22 "mul", "div", "pow", "unm", "lt", "concat", "gc", "function", NULL
23 "concat", "gc", "function", NULL
24}; 23};
25 24
26 25
@@ -38,13 +37,13 @@ static int luaI_checkevent (lua_State *L, const char *name, const char *const li
38*/ 37*/
39/* ORDER LUA_T, ORDER IM */ 38/* ORDER LUA_T, ORDER IM */
40static const char luaT_validevents[NUM_TAGS][IM_N] = { 39static const char luaT_validevents[NUM_TAGS][IM_N] = {
41{1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* LUA_T_USERDATA */ 40{1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* LUA_T_USERDATA */
42{1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_NUMBER */ 41{1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, /* LUA_T_NUMBER */
43{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_STRING */ 42{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_STRING */
44{0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* LUA_T_ARRAY */ 43{0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* LUA_T_ARRAY */
45{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_LPROTO */ 44{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_LPROTO */
46{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_CPROTO */ 45{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_CPROTO */
47{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} /* LUA_T_NIL */ 46{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} /* LUA_T_NIL */
48}; 47};
49 48
50int luaT_validevent (int t, int e) { /* ORDER LUA_T */ 49int luaT_validevent (int t, int e) { /* ORDER LUA_T */
@@ -122,7 +121,13 @@ int luaT_effectivetag (const TObject *o) {
122 121
123 122
124const TObject *luaT_gettagmethod (lua_State *L, int t, const char *event) { 123const TObject *luaT_gettagmethod (lua_State *L, int t, const char *event) {
125 int e = luaI_checkevent(L, event, luaT_eventname); 124 int e;
125#ifdef LUA_COMPAT_ORDER_TM
126 static const char *old_order[] = {"le", "gt", "ge", NULL};
127 if (luaL_findstring(event, old_order) >= 0)
128 return &luaO_nilobject;
129#endif
130 e = luaI_checkevent(L, event, luaT_eventname);
126 checktag(L, t); 131 checktag(L, t);
127 if (luaT_validevent(t, e)) 132 if (luaT_validevent(t, e))
128 return luaT_getim(L, t,e); 133 return luaT_getim(L, t,e);
@@ -133,7 +138,13 @@ const TObject *luaT_gettagmethod (lua_State *L, int t, const char *event) {
133 138
134void luaT_settagmethod (lua_State *L, int t, const char *event, TObject *func) { 139void luaT_settagmethod (lua_State *L, int t, const char *event, TObject *func) {
135 TObject temp; 140 TObject temp;
136 int e = luaI_checkevent(L, event, luaT_eventname); 141 int e;
142#ifdef LUA_COMPAT_ORDER_TM
143 static const char *old_order[] = {"le", "gt", "ge", NULL};
144 if (luaL_findstring(event, old_order) >= 0)
145 return; /* do nothing for old operators */
146#endif
147 e = luaI_checkevent(L, event, luaT_eventname);
137 checktag(L, t); 148 checktag(L, t);
138 if (!luaT_validevent(t, e)) 149 if (!luaT_validevent(t, e))
139 luaL_verror(L, "cannot change tag method `%.20s' for type `%.20s'%.20s", 150 luaL_verror(L, "cannot change tag method `%.20s' for type `%.20s'%.20s",
@@ -146,7 +157,8 @@ void luaT_settagmethod (lua_State *L, int t, const char *event, TObject *func) {
146} 157}
147 158
148 159
149const char *luaT_travtagmethods (lua_State *L, int (*fn)(lua_State *, TObject *)) { /* ORDER IM */ 160const char *luaT_travtagmethods (lua_State *L,
161 int (*fn)(lua_State *, TObject *)) { /* ORDER IM */
150 int e; 162 int e;
151 for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) { 163 for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) {
152 int t; 164 int t;
diff --git a/ltm.h b/ltm.h
index fde74b84..3d517e3e 100644
--- a/ltm.h
+++ b/ltm.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltm.h,v 1.7 1999/09/20 14:57:29 roberto Exp roberto $ 2** $Id: ltm.h,v 1.8 1999/11/22 13:12:07 roberto Exp roberto $
3** Tag methods 3** Tag methods
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -28,15 +28,12 @@ typedef enum {
28 IM_POW, 28 IM_POW,
29 IM_UNM, 29 IM_UNM,
30 IM_LT, 30 IM_LT,
31 IM_LE,
32 IM_GT,
33 IM_GE,
34 IM_CONCAT, 31 IM_CONCAT,
35 IM_GC, 32 IM_GC,
36 IM_FUNCTION 33 IM_FUNCTION
37} IMS; 34} IMS;
38 35
39#define IM_N 18 36#define IM_N 15
40 37
41 38
42struct IM { 39struct IM {
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:
diff --git a/lvm.h b/lvm.h
index 682364a7..14b83bb7 100644
--- a/lvm.h
+++ b/lvm.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lvm.h,v 1.14 2000/01/19 16:50:30 roberto Exp roberto $ 2** $Id: lvm.h,v 1.15 2000/01/24 20:14:07 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*/
@@ -28,6 +28,6 @@ void luaV_getglobal (lua_State *L, GlobalVar *gv, StkId top);
28void luaV_setglobal (lua_State *L, GlobalVar *gv, StkId top); 28void luaV_setglobal (lua_State *L, GlobalVar *gv, StkId top);
29StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf, StkId base); 29StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf, StkId base);
30void luaV_closure (lua_State *L, int nelems); 30void luaV_closure (lua_State *L, int nelems);
31void luaV_comparison (lua_State *L); 31int luaV_lessthan (lua_State *L, TObject *l, TObject *r);
32 32
33#endif 33#endif
diff --git a/makefile b/makefile
index e2d1593a..6ecb282e 100644
--- a/makefile
+++ b/makefile
@@ -1,5 +1,5 @@
1# 1#
2## $Id: makefile,v 1.21 1999/10/04 17:51:04 roberto Exp roberto $ 2## $Id: makefile,v 1.22 2000/02/22 13:31:43 roberto Exp roberto $
3## Makefile 3## Makefile
4## See Copyright Notice in lua.h 4## See Copyright Notice in lua.h
5# 5#
@@ -24,8 +24,8 @@
24# define COMPAT_READPATTERN if you need read patterns 24# define COMPAT_READPATTERN if you need read patterns
25# (only for compatibility with previous versions) 25# (only for compatibility with previous versions)
26 26
27CONFIG = -DPOPEN -D_POSIX_SOURCE 27CONFIG = -DPOPEN -D_POSIX_SOURCE -DDEBUG -DLUA_COMPAT_ORDER_TM
28#CONFIG = -DOLD_ANSI -DDEBUG -DLUA_COMPAT_GC -DCOMPAT_READPATTERN 28#CONFIG = -DOLD_ANSI -DDEBUG -DLUA_COMPAT_GC -DCOMPAT_READPATTERN -DLUA_COMPAT_ORDER_TM
29 29
30 30
31# Compilation parameters 31# Compilation parameters