new functions 'lua_arith' and 'lua_compare'

This commit is contained in:
Roberto Ierusalimschy 2009-06-17 14:53:14 -03:00
parent 59a59fafc6
commit 6980cb1aa7
3 changed files with 55 additions and 28 deletions

36
lapi.c
View File

@ -1,5 +1,5 @@
/*
** $Id: lapi.c,v 2.78 2009/06/01 19:09:26 roberto Exp roberto $
** $Id: lapi.c,v 2.79 2009/06/15 19:51:31 roberto Exp roberto $
** Lua API
** See Copyright Notice in lua.h
*/
@ -275,32 +275,34 @@ LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
}
LUA_API int lua_equal (lua_State *L, int index1, int index2) {
LUA_API void lua_arith (lua_State *L, int op) {
lua_lock(L);
api_checknelems(L, 2);
luaV_arith(L, L->top - 2, L->top - 2, L->top - 1, op - LUA_OPADD + TM_ADD);
L->top--;
lua_unlock(L);
}
LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
StkId o1, o2;
int i;
lua_lock(L); /* may call tag method */
o1 = index2adr(L, index1);
o2 = index2adr(L, index2);
i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 : equalobj(L, o1, o2);
if (o1 == luaO_nilobject || o2 == luaO_nilobject)
i = 0;
else switch (op) {
case LUA_OPEQ: i = equalobj(L, o1, o2); break;
case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
default: api_check(L, 0); i = 0;
}
lua_unlock(L);
return i;
}
LUA_API int lua_lessthan (lua_State *L, int index1, int index2) {
StkId o1, o2;
int i;
lua_lock(L); /* may call tag method */
o1 = index2adr(L, index1);
o2 = index2adr(L, index2);
i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
: luaV_lessthan(L, o1, o2);
lua_unlock(L);
return i;
}
LUA_API lua_Number lua_tonumber (lua_State *L, int idx) {
TValue n;
const TValue *o = index2adr(L, idx);

View File

@ -1,5 +1,5 @@
/*
** $Id: ltests.c,v 2.64 2009/06/10 16:57:53 roberto Exp roberto $
** $Id: ltests.c,v 2.65 2009/06/15 19:51:31 roberto Exp roberto $
** Internal Module for Debugging of the Lua Implementation
** See Copyright Notice in lua.h
*/
@ -971,13 +971,17 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
else if EQ("concat") {
lua_concat(L1, getnum);
}
else if EQ("lessthan") {
int a = getindex;
lua_pushboolean(L1, lua_lessthan(L1, a, getindex));
else if EQ("arith") {
static char ops[] = "+-*/%^_";
int op;
skip(&pc);
op = strchr(ops, *pc++) - ops;
lua_arith(L, op);
}
else if EQ("equal") {
else if EQ("compare") {
int a = getindex;
lua_pushboolean(L1, lua_equal(L1, a, getindex));
int b = getindex;
lua_pushboolean(L1, lua_compare(L1, a, b, getnum));
}
else if EQ("call") {
int narg = getnum;

31
lua.h
View File

@ -1,5 +1,5 @@
/*
** $Id: lua.h,v 1.237 2009/05/21 20:06:11 roberto Exp roberto $
** $Id: lua.h,v 1.238 2009/06/15 19:51:31 roberto Exp roberto $
** Lua - An Extensible Extension Language
** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
** See Copyright Notice at the end of this file
@ -147,10 +147,6 @@ LUA_API int (lua_isuserdata) (lua_State *L, int idx);
LUA_API int (lua_type) (lua_State *L, int idx);
LUA_API const char *(lua_typename) (lua_State *L, int tp);
LUA_API int (lua_equal) (lua_State *L, int idx1, int idx2);
LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2);
LUA_API int (lua_lessthan) (lua_State *L, int idx1, int idx2);
LUA_API lua_Number (lua_tonumber) (lua_State *L, int idx);
LUA_API lua_Integer (lua_tointeger) (lua_State *L, int idx);
LUA_API int (lua_toboolean) (lua_State *L, int idx);
@ -162,6 +158,28 @@ LUA_API lua_State *(lua_tothread) (lua_State *L, int idx);
LUA_API const void *(lua_topointer) (lua_State *L, int idx);
/*
** Comparison and arithmetic functions
*/
#define LUA_OPADD 0 /* ORDER TM */
#define LUA_OPSUB 1
#define LUA_OPMUL 2
#define LUA_OPDIV 3
#define LUA_OPMOD 4
#define LUA_OPPOW 5
#define LUA_OPUNM 6
LUA_API void (lua_arith) (lua_State *L, int op);
#define LUA_OPEQ 0
#define LUA_OPLT 1
#define LUA_OPLE 2
LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2);
LUA_API int (lua_compare) (lua_State *L, int idx1, int idx2, int op);
/*
** push functions (C -> stack)
*/
@ -310,6 +328,9 @@ LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud);
#define lua_Chunkreader lua_Reader
#define lua_Chunkwriter lua_Writer
#define lua_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ)
#define lua_lessthan(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPLT)
#endif