lua

A copy of the Lua development repository
Log | Files | Refs | README

commit 7d57ea70bc975922485d589c8a6d8dedaa0fba02
parent 2d5b923759a77d8f5f7bfa62906ea37f46b9281d
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date:   Tue,  8 Mar 2005 14:59:54 -0300

new `mod' (`%') operator

Diffstat:
Mlcode.h | 4++--
Mlopcodes.c | 4+++-
Mlopcodes.h | 3++-
Mlparser.c | 7++++---
Mltm.c | 4++--
Mltm.h | 3++-
Mluaconf.h | 3++-
Mlvm.c | 43+++++++++++++++++++++++++++++++------------
8 files changed, 48 insertions(+), 23 deletions(-)

diff --git a/lcode.h b/lcode.h @@ -1,5 +1,5 @@ /* -** $Id: lcode.h,v 1.39 2004/05/31 18:51:50 roberto Exp $ +** $Id: lcode.h,v 1.40 2004/10/04 19:01:53 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -24,7 +24,7 @@ ** grep "ORDER OPR" if you change these enums */ typedef enum BinOpr { - OPR_ADD, OPR_SUB, OPR_MULT, OPR_DIV, OPR_POW, + OPR_ADD, OPR_SUB, OPR_MULT, OPR_DIV, OPR_MOD, OPR_POW, OPR_CONCAT, OPR_NE, OPR_EQ, OPR_LT, OPR_LE, OPR_GT, OPR_GE, diff --git a/lopcodes.c b/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.29 2004/10/04 19:01:53 roberto Exp roberto $ +** $Id: lopcodes.c,v 1.30 2004/12/02 12:59:10 roberto Exp roberto $ ** See Copyright Notice in lua.h */ @@ -32,6 +32,7 @@ const char *const luaP_opnames[NUM_OPCODES+1] = { "SUB", "MUL", "DIV", + "MOD", "POW", "UNM", "NOT", @@ -76,6 +77,7 @@ const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */ ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */ ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */ + ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */ ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */ ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */ ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */ diff --git a/lopcodes.h b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.113 2004/10/04 19:07:42 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.114 2004/12/02 12:59:10 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -172,6 +172,7 @@ OP_ADD,/* A B C R(A) := RK(B) + RK(C) */ OP_SUB,/* A B C R(A) := RK(B) - RK(C) */ OP_MUL,/* A B C R(A) := RK(B) * RK(C) */ OP_DIV,/* A B C R(A) := RK(B) / RK(C) */ +OP_MOD,/* A B C R(A) := RK(B) % RK(C) */ OP_POW,/* A B C R(A) := RK(B) ^ RK(C) */ OP_UNM,/* A B R(A) := -R(B) */ OP_NOT,/* A B R(A) := not R(B) */ diff --git a/lparser.c b/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.13 2005/01/05 18:20:51 roberto Exp roberto $ +** $Id: lparser.c,v 2.14 2005/03/07 16:58:27 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -799,6 +799,7 @@ static BinOpr getbinopr (int op) { case '-': return OPR_SUB; case '*': return OPR_MULT; case '/': return OPR_DIV; + case '%': return OPR_MOD; case '^': return OPR_POW; case TK_CONCAT: return OPR_CONCAT; case TK_NE: return OPR_NE; @@ -818,9 +819,9 @@ static const struct { lu_byte left; /* left priority for each binary operator */ lu_byte right; /* right priority */ } priority[] = { /* ORDER OPR */ - {6, 6}, {6, 6}, {7, 7}, {7, 7}, /* arithmetic */ + {6, 6}, {6, 6}, {7, 7}, {7, 7}, {7, 7}, /* `+' `-' `/' `%' */ {10, 9}, {5, 4}, /* power and concat (right associative) */ - {3, 3}, {3, 3}, /* equality */ + {3, 3}, {3, 3}, /* equality and inequality */ {3, 3}, {3, 3}, {3, 3}, {3, 3}, /* order */ {2, 2}, {1, 1} /* logical (and/or) */ }; diff --git a/ltm.c b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.2 2004/02/16 19:09:52 roberto Exp roberto $ +** $Id: ltm.c,v 2.3 2004/04/30 20:13:38 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -31,7 +31,7 @@ void luaT_init (lua_State *L) { static const char *const luaT_eventname[] = { /* ORDER TM */ "__index", "__newindex", "__gc", "__mode", "__eq", - "__add", "__sub", "__mul", "__div", + "__add", "__sub", "__mul", "__div", "__mod", "__pow", "__unm", "__lt", "__le", "__concat", "__call" }; diff --git a/ltm.h b/ltm.h @@ -1,5 +1,5 @@ /* -** $Id: ltm.h,v 1.42 2003/12/01 18:22:56 roberto Exp roberto $ +** $Id: ltm.h,v 2.1 2003/12/10 12:13:36 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -25,6 +25,7 @@ typedef enum { TM_SUB, TM_MUL, TM_DIV, + TM_MOD, TM_POW, TM_UNM, TM_LT, diff --git a/luaconf.h b/luaconf.h @@ -1,5 +1,5 @@ /* -** $Id: luaconf.h,v 1.30 2005/02/28 15:59:11 roberto Exp roberto $ +** $Id: luaconf.h,v 1.31 2005/03/08 13:27:36 roberto Exp roberto $ ** Configuration file for Lua ** See Copyright Notice in lua.h */ @@ -261,6 +261,7 @@ __inline int l_lrint (double flt) #define num_lt(a,b) ((a)<(b)) #define num_le(a,b) ((a)<=(b)) #include <math.h> +#define num_mod(a,b) ((a) - floor((a)/(b))*(b)) #define num_pow(a,b) pow(a,b) diff --git a/lvm.c b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.27 2005/03/07 16:59:01 roberto Exp roberto $ +** $Id: lvm.c,v 2.28 2005/03/07 18:27:34 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -334,12 +334,14 @@ static StkId Arith (lua_State *L, StkId ra, const TValue *rb, L->ci->savedpc = pc; if ((b = luaV_tonumber(rb, &tempb)) != NULL && (c = luaV_tonumber(rc, &tempc)) != NULL) { + lua_Number nb = nvalue(b), nc = nvalue(c); switch (op) { - case TM_ADD: setnvalue(ra, num_add(nvalue(b), nvalue(c))); break; - case TM_SUB: setnvalue(ra, num_sub(nvalue(b), nvalue(c))); break; - case TM_MUL: setnvalue(ra, num_mul(nvalue(b), nvalue(c))); break; - case TM_DIV: setnvalue(ra, num_div(nvalue(b), nvalue(c))); break; - case TM_POW: setnvalue(ra, num_pow(nvalue(b), nvalue(c))); break; + case TM_ADD: setnvalue(ra, num_add(nb, nc)); break; + case TM_SUB: setnvalue(ra, num_sub(nb, nc)); break; + case TM_MUL: setnvalue(ra, num_mul(nb, nc)); break; + case TM_DIV: setnvalue(ra, num_div(nb, nc)); break; + case TM_MOD: setnvalue(ra, num_mod(nb, nc)); break; + case TM_POW: setnvalue(ra, num_pow(nb, nc)); break; default: lua_assert(0); break; } } @@ -475,7 +477,8 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { TValue *rb = RKB(i); TValue *rc = RKC(i); if (ttisnumber(rb) && ttisnumber(rc)) { - setnvalue(ra, num_add(nvalue(rb), nvalue(rc))); + lua_Number nb = nvalue(rb), nc = nvalue(rc); + setnvalue(ra, num_add(nb, nc)); } else base = Arith(L, ra, rb, rc, TM_ADD, pc); /***/ @@ -485,7 +488,8 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { TValue *rb = RKB(i); TValue *rc = RKC(i); if (ttisnumber(rb) && ttisnumber(rc)) { - setnvalue(ra, num_sub(nvalue(rb), nvalue(rc))); + lua_Number nb = nvalue(rb), nc = nvalue(rc); + setnvalue(ra, num_sub(nb, nc)); } else base = Arith(L, ra, rb, rc, TM_SUB, pc); /***/ @@ -495,7 +499,8 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { TValue *rb = RKB(i); TValue *rc = RKC(i); if (ttisnumber(rb) && ttisnumber(rc)) { - setnvalue(ra, num_mul(nvalue(rb), nvalue(rc))); + lua_Number nb = nvalue(rb), nc = nvalue(rc); + setnvalue(ra, num_mul(nb, nc)); } else base = Arith(L, ra, rb, rc, TM_MUL, pc); /***/ @@ -505,17 +510,30 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { TValue *rb = RKB(i); TValue *rc = RKC(i); if (ttisnumber(rb) && ttisnumber(rc)) { - setnvalue(ra, num_div(nvalue(rb), nvalue(rc))); + lua_Number nb = nvalue(rb), nc = nvalue(rc); + setnvalue(ra, num_div(nb, nc)); } else base = Arith(L, ra, rb, rc, TM_DIV, pc); /***/ continue; } + case OP_MOD: { + TValue *rb = RKB(i); + TValue *rc = RKC(i); + if (ttisnumber(rb) && ttisnumber(rc)) { + lua_Number nb = nvalue(rb), nc = nvalue(rc); + setnvalue(ra, num_mod(nb, nc)); + } + else + base = Arith(L, ra, rb, rc, TM_MOD, pc); /***/ + continue; + } case OP_POW: { TValue *rb = RKB(i); TValue *rc = RKC(i); if (ttisnumber(rb) && ttisnumber(rc)) { - setnvalue(ra, num_pow(nvalue(rb), nvalue(rc))); + lua_Number nb = nvalue(rb), nc = nvalue(rc); + setnvalue(ra, num_pow(nb, nc)); } else base = Arith(L, ra, rb, rc, TM_POW, pc); /***/ @@ -525,7 +543,8 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { const TValue *rb = RB(i); TValue temp; if (tonumber(rb, &temp)) { - setnvalue(ra, num_unm(nvalue(rb))); + lua_Number nb = nvalue(rb); + setnvalue(ra, num_unm(nb)); } else { setnilvalue(&temp);