Browse Source

new order for binary operations (grouping them by type of result)

pull/9/head
Roberto Ierusalimschy 11 years ago
parent
commit
a948054a19
  1. 3
      lcode.c
  2. 6
      lcode.h
  3. 10
      lopcodes.c
  4. 6
      lopcodes.h
  5. 12
      lparser.c
  6. 4
      ltests.c
  7. 7
      ltm.c
  8. 8
      ltm.h
  9. 12
      lua.h

3
lcode.c

@ -1,5 +1,5 @@
/* /*
** $Id: lcode.c,v 2.72 2013/08/30 16:01:37 roberto Exp roberto $ ** $Id: lcode.c,v 2.73 2013/12/16 14:30:22 roberto Exp roberto $
** Code generator for Lua ** Code generator for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -762,6 +762,7 @@ static int constfolding (OpCode op, expdesc *e1, expdesc *e2) {
((op == OP_MOD && ivalue(&v2) == 0) || /* ...avoid module by 0... */ ((op == OP_MOD && ivalue(&v2) == 0) || /* ...avoid module by 0... */
(op == OP_POW && ivalue(&v2) < 0))) /* ...and negative exponents */ (op == OP_POW && ivalue(&v2) < 0))) /* ...and negative exponents */
return 0; return 0;
lua_assert(OP_IDIV - OP_ADD + LUA_OPADD == LUA_OPIDIV);
luaO_arith(NULL, op - OP_ADD + LUA_OPADD, &v1, &v2, &res); luaO_arith(NULL, op - OP_ADD + LUA_OPADD, &v1, &v2, &res);
if (ttisinteger(&res)) { if (ttisinteger(&res)) {
e1->k = VKINT; e1->k = VKINT;

6
lcode.h

@ -1,5 +1,5 @@
/* /*
** $Id: lcode.h,v 1.59 2013/04/25 19:35:19 roberto Exp roberto $ ** $Id: lcode.h,v 1.60 2013/04/26 13:07:53 roberto Exp roberto $
** Code generator for Lua ** Code generator for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -24,7 +24,9 @@
** grep "ORDER OPR" if you change these enums (ORDER OP) ** grep "ORDER OPR" if you change these enums (ORDER OP)
*/ */
typedef enum BinOpr { typedef enum BinOpr {
OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_IDIV, OPR_MOD, OPR_POW, OPR_ADD, OPR_SUB, OPR_MUL, OPR_MOD, OPR_POW,
OPR_DIV,
OPR_IDIV,
OPR_CONCAT, OPR_CONCAT,
OPR_EQ, OPR_LT, OPR_LE, OPR_EQ, OPR_LT, OPR_LE,
OPR_NE, OPR_GT, OPR_GE, OPR_NE, OPR_GT, OPR_GE,

10
lopcodes.c

@ -1,5 +1,5 @@
/* /*
** $Id: lopcodes.c,v 1.49 2012/05/14 13:34:18 roberto Exp roberto $ ** $Id: lopcodes.c,v 1.50 2013/04/26 13:07:53 roberto Exp roberto $
** Opcodes for Lua virtual machine ** Opcodes for Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -31,10 +31,10 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = {
"ADD", "ADD",
"SUB", "SUB",
"MUL", "MUL",
"DIV",
"IDIV",
"MOD", "MOD",
"POW", "POW",
"DIV",
"IDIV",
"UNM", "UNM",
"NOT", "NOT",
"LEN", "LEN",
@ -80,10 +80,10 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = {
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */ ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */ ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */ ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_IDIV */
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */ ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */ ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_IDIV */
,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */ ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */
,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */ ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */
,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */ ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */

6
lopcodes.h

@ -1,5 +1,5 @@
/* /*
** $Id: lopcodes.h,v 1.142 2011/07/15 12:50:29 roberto Exp roberto $ ** $Id: lopcodes.h,v 1.143 2013/04/26 13:07:53 roberto Exp roberto $
** Opcodes for Lua virtual machine ** Opcodes for Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -187,10 +187,10 @@ OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C)] */
OP_ADD,/* A B C R(A) := RK(B) + RK(C) */ OP_ADD,/* A B C R(A) := RK(B) + RK(C) */
OP_SUB,/* 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_MUL,/* A B C R(A) := RK(B) * RK(C) */
OP_DIV,/* A B C R(A) := RK(B) / RK(C) */
OP_IDIV,/* A B C R(A) := RK(B) // RK(C) */
OP_MOD,/* 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_POW,/* A B C R(A) := RK(B) ^ RK(C) */
OP_DIV,/* A B C R(A) := RK(B) / RK(C) */
OP_IDIV,/* A B C R(A) := RK(B) // RK(C) */
OP_UNM,/* A B R(A) := -R(B) */ OP_UNM,/* A B R(A) := -R(B) */
OP_NOT,/* A B R(A) := not R(B) */ OP_NOT,/* A B R(A) := not R(B) */
OP_LEN,/* A B R(A) := length of R(B) */ OP_LEN,/* A B R(A) := length of R(B) */

12
lparser.c

@ -1,5 +1,5 @@
/* /*
** $Id: lparser.c,v 2.134 2013/08/16 18:55:49 roberto Exp roberto $ ** $Id: lparser.c,v 2.135 2013/08/30 16:01:37 roberto Exp roberto $
** Lua Parser ** Lua Parser
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -990,10 +990,10 @@ static BinOpr getbinopr (int op) {
case '+': return OPR_ADD; case '+': return OPR_ADD;
case '-': return OPR_SUB; case '-': return OPR_SUB;
case '*': return OPR_MUL; case '*': return OPR_MUL;
case '/': return OPR_DIV;
case TK_IDIV: return OPR_IDIV;
case '%': return OPR_MOD; case '%': return OPR_MOD;
case '^': return OPR_POW; case '^': return OPR_POW;
case '/': return OPR_DIV;
case TK_IDIV: return OPR_IDIV;
case TK_CONCAT: return OPR_CONCAT; case TK_CONCAT: return OPR_CONCAT;
case TK_NE: return OPR_NE; case TK_NE: return OPR_NE;
case TK_EQ: return OPR_EQ; case TK_EQ: return OPR_EQ;
@ -1013,8 +1013,10 @@ static const struct {
lu_byte right; /* right priority */ lu_byte right; /* right priority */
} priority[] = { /* ORDER OPR */ } priority[] = { /* ORDER OPR */
{6, 6}, {6, 6}, /* '+' '-' */ {6, 6}, {6, 6}, /* '+' '-' */
{7, 7}, {7, 7}, {7, 7}, {7, 7}, /* '*' '/' '//' '%' */ {7, 7}, {7, 7}, /* '*' '%' */
{10, 9}, {5, 4}, /* ^, .. (right associative) */ {10, 9}, /* '^' (right associative) */
{7, 7}, {7, 7}, /* '/' '//' */
{5, 4}, /* '..' (right associative) */
{3, 3}, {3, 3}, {3, 3}, /* ==, <, <= */ {3, 3}, {3, 3}, {3, 3}, /* ==, <, <= */
{3, 3}, {3, 3}, {3, 3}, /* ~=, >, >= */ {3, 3}, {3, 3}, {3, 3}, /* ~=, >, >= */
{2, 2}, {1, 1} /* and, or */ {2, 2}, {1, 1} /* and, or */

4
ltests.c

@ -1,5 +1,5 @@
/* /*
** $Id: ltests.c,v 2.158 2013/09/11 14:09:55 roberto Exp roberto $ ** $Id: ltests.c,v 2.159 2013/09/11 14:47:08 roberto Exp roberto $
** Internal Module for Debugging of the Lua Implementation ** Internal Module for Debugging of the Lua Implementation
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -1198,7 +1198,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
} }
} }
else if EQ("arith") { else if EQ("arith") {
static char ops[] = "+-*/\\%^_"; /* '\' -> '//'; '_' -> '..' */ static char ops[] = "+-*%^/\\_"; /* '\' -> '//'; '_' -> '..' */
int op; int op;
skip(&pc); skip(&pc);
op = strchr(ops, *pc++) - ops; op = strchr(ops, *pc++) - ops;

7
ltm.c

@ -1,5 +1,5 @@
/* /*
** $Id: ltm.c,v 2.21 2013/08/21 20:09:51 roberto Exp roberto $ ** $Id: ltm.c,v 2.22 2013/08/27 18:53:35 roberto Exp roberto $
** Tag methods ** Tag methods
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -36,8 +36,9 @@ void luaT_init (lua_State *L) {
static const char *const luaT_eventname[] = { /* ORDER TM */ static const char *const luaT_eventname[] = { /* ORDER TM */
"__index", "__newindex", "__index", "__newindex",
"__gc", "__mode", "__len", "__eq", "__gc", "__mode", "__len", "__eq",
"__add", "__sub", "__mul", "__div", "__idiv", "__mod", "__add", "__sub", "__mul", "__mod", "__pow",
"__pow", "__unm", "__lt", "__le", "__div", "__idiv",
"__unm", "__lt", "__le",
"__concat", "__call" "__concat", "__call"
}; };
int i; int i;

8
ltm.h

@ -1,5 +1,5 @@
/* /*
** $Id: ltm.h,v 2.15 2013/04/26 13:07:53 roberto Exp roberto $ ** $Id: ltm.h,v 2.16 2013/04/29 16:56:50 roberto Exp roberto $
** Tag methods ** Tag methods
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -13,7 +13,7 @@
/* /*
* WARNING: if you change the order of this enumeration, * WARNING: if you change the order of this enumeration,
* grep "ORDER TM" * grep "ORDER TM" and "ORDER OP"
*/ */
typedef enum { typedef enum {
TM_INDEX, TM_INDEX,
@ -25,10 +25,10 @@ typedef enum {
TM_ADD, TM_ADD,
TM_SUB, TM_SUB,
TM_MUL, TM_MUL,
TM_DIV,
TM_IDIV,
TM_MOD, TM_MOD,
TM_POW, TM_POW,
TM_DIV,
TM_IDIV,
TM_UNM, TM_UNM,
TM_LT, TM_LT,
TM_LE, TM_LE,

12
lua.h

@ -1,5 +1,5 @@
/* /*
** $Id: lua.h,v 1.294 2013/09/13 16:21:52 roberto Exp roberto $ ** $Id: lua.h,v 1.295 2013/12/09 14:21:10 roberto Exp roberto $
** Lua - A Scripting Language ** Lua - A Scripting Language
** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
** See Copyright Notice at the end of this file ** See Copyright Notice at the end of this file
@ -182,13 +182,13 @@ LUA_API const void *(lua_topointer) (lua_State *L, int idx);
** Comparison and arithmetic functions ** Comparison and arithmetic functions
*/ */
#define LUA_OPADD 0 /* ORDER TM */ #define LUA_OPADD 0 /* ORDER TM, ORDER OP */
#define LUA_OPSUB 1 #define LUA_OPSUB 1
#define LUA_OPMUL 2 #define LUA_OPMUL 2
#define LUA_OPDIV 3 #define LUA_OPMOD 3
#define LUA_OPIDIV 4 #define LUA_OPPOW 4
#define LUA_OPMOD 5 #define LUA_OPDIV 5
#define LUA_OPPOW 6 #define LUA_OPIDIV 6
#define LUA_OPUNM 7 #define LUA_OPUNM 7
LUA_API void (lua_arith) (lua_State *L, int op); LUA_API void (lua_arith) (lua_State *L, int op);

Loading…
Cancel
Save