mirror of
https://github.com/lua/lua
synced 2024-12-25 20:06:50 +03:00
new macros for changing numbers
This commit is contained in:
parent
61a036eaa5
commit
b346834a09
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lobject.h,v 1.106 2001/06/15 20:36:57 roberto Exp roberto $
|
||||
** $Id: lobject.h,v 1.107 2001/06/26 13:20:45 roberto Exp roberto $
|
||||
** Type definitions for Lua objects
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@ -55,6 +55,8 @@ typedef struct lua_TObject {
|
||||
#define setnvalue(obj,x) \
|
||||
{ TObject *_o=(obj); _o->tt=LUA_TNUMBER; _o->value.n=(x); }
|
||||
|
||||
#define chgnvalue(obj,x) ((obj)->value.n=(x))
|
||||
|
||||
#define setsvalue(obj,x) \
|
||||
{ TObject *_o=(obj); _o->tt=LUA_TSTRING; _o->value.ts=(x); }
|
||||
|
||||
|
17
lvm.c
17
lvm.c
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lvm.c,v 1.187 2001/06/20 17:22:46 roberto Exp roberto $
|
||||
** $Id: lvm.c,v 1.188 2001/06/26 13:20:45 roberto Exp roberto $
|
||||
** Lua virtual machine
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@ -39,9 +39,10 @@ static void luaV_checkGC (lua_State *L, StkId top) {
|
||||
|
||||
|
||||
const TObject *luaV_tonumber (const TObject *obj, TObject *n) {
|
||||
lua_Number num;
|
||||
if (ttype(obj) == LUA_TNUMBER) return obj;
|
||||
if (ttype(obj) == LUA_TSTRING && luaO_str2d(svalue(obj), &nvalue(n))) {
|
||||
setttype(n, LUA_TNUMBER);
|
||||
if (ttype(obj) == LUA_TSTRING && luaO_str2d(svalue(obj), &num)) {
|
||||
setnvalue(n, num);
|
||||
return n;
|
||||
}
|
||||
else
|
||||
@ -580,10 +581,11 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
|
||||
luaD_error(L, l_s("`for' limit must be a number"));
|
||||
if (luaV_tonumber(ra+2, ra+2) == NULL)
|
||||
luaD_error(L, l_s("`for' step must be a number"));
|
||||
nvalue(ra) -= nvalue(ra+2);/* decrement index (to be incremented) */
|
||||
/* decrement index (to be incremented) */
|
||||
chgnvalue(ra, nvalue(ra) - nvalue(ra+2));
|
||||
pc += -GETARG_sBc(i); /* `jump' to loop end (delta is negated here) */
|
||||
/* store in `ra+1' total number of repetitions */
|
||||
nvalue(ra+1) = ((nvalue(ra+1)-nvalue(ra))/nvalue(ra+2));
|
||||
chgnvalue(ra+1, (nvalue(ra+1)-nvalue(ra))/nvalue(ra+2));
|
||||
/* go through */
|
||||
}
|
||||
case OP_FORLOOP: {
|
||||
@ -591,8 +593,9 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
|
||||
ttype(ra+2) == LUA_TNUMBER);
|
||||
if (ttype(ra) != LUA_TNUMBER)
|
||||
luaD_error(L, l_s("`for' index must be a number"));
|
||||
if (--nvalue(ra+1) >= 0) {
|
||||
nvalue(ra) += nvalue(ra+2); /* increment index */
|
||||
chgnvalue(ra+1, nvalue(ra+1) - 1); /* decrement counter */
|
||||
if (nvalue(ra+1) >= 0) {
|
||||
chgnvalue(ra, nvalue(ra) + nvalue(ra+2)); /* increment index */
|
||||
dojump(pc, i); /* repeat loop */
|
||||
}
|
||||
break;
|
||||
|
Loading…
Reference in New Issue
Block a user