new function 'luaO_arith' to perform basic arithmetic operations

This commit is contained in:
Roberto Ierusalimschy 2009-06-17 14:48:34 -03:00
parent 77fd93188e
commit 6a024df440
2 changed files with 17 additions and 2 deletions

View File

@ -1,5 +1,5 @@
/*
** $Id: lobject.c,v 2.29 2009/02/19 17:18:25 roberto Exp $
** $Id: lobject.c,v 2.30 2009/05/27 17:11:27 roberto Exp roberto $
** Some generic functions over Lua objects
** See Copyright Notice in lua.h
*/
@ -88,6 +88,20 @@ int luaO_rawequalObj (const TValue *t1, const TValue *t2) {
}
lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2) {
switch (op) {
case LUA_OPADD: return luai_numadd(NULL, v1, v2);
case LUA_OPSUB: return luai_numsub(NULL, v1, v2);
case LUA_OPMUL: return luai_nummul(NULL, v1, v2);
case LUA_OPDIV: return luai_numdiv(NULL, v1, v2);
case LUA_OPMOD: return luai_nummod(NULL, v1, v2);
case LUA_OPPOW: return luai_numpow(NULL, v1, v2);
case LUA_OPUNM: return luai_numunm(N, v1);
default: lua_assert(0); return 0;
}
}
int luaO_str2d (const char *s, lua_Number *result) {
char *endptr;
*result = lua_str2number(s, &endptr);

View File

@ -1,5 +1,5 @@
/*
** $Id: lobject.h,v 2.24 2008/08/05 19:26:23 roberto Exp roberto $
** $Id: lobject.h,v 2.25 2009/03/26 12:56:38 roberto Exp roberto $
** Type definitions for Lua objects
** See Copyright Notice in lua.h
*/
@ -361,6 +361,7 @@ LUAI_DATA const TValue luaO_nilobject_;
LUAI_FUNC int luaO_int2fb (unsigned int x);
LUAI_FUNC int luaO_fb2int (int x);
LUAI_FUNC int luaO_ceillog2 (lu_int32 x);
LUAI_FUNC lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2);
LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2);
LUAI_FUNC int luaO_str2d (const char *s, lua_Number *result);
LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,