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

This commit is contained in:
Roberto Ierusalimschy 2013-12-16 17:06:52 -02:00
parent a8f8c7fd80
commit a948054a19
9 changed files with 41 additions and 35 deletions

View File

@ -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
** 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_POW && ivalue(&v2) < 0))) /* ...and negative exponents */
return 0;
lua_assert(OP_IDIV - OP_ADD + LUA_OPADD == LUA_OPIDIV);
luaO_arith(NULL, op - OP_ADD + LUA_OPADD, &v1, &v2, &res);
if (ttisinteger(&res)) {
e1->k = VKINT;

View File

@ -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
** See Copyright Notice in lua.h
*/
@ -24,7 +24,9 @@
** grep "ORDER OPR" if you change these enums (ORDER OP)
*/
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_EQ, OPR_LT, OPR_LE,
OPR_NE, OPR_GT, OPR_GE,

View File

@ -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
** See Copyright Notice in lua.h
*/
@ -31,10 +31,10 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = {
"ADD",
"SUB",
"MUL",
"DIV",
"IDIV",
"MOD",
"POW",
"DIV",
"IDIV",
"UNM",
"NOT",
"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_SUB */
,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_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_NOT */
,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */

View File

@ -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
** 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_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_IDIV,/* 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_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_NOT,/* A B R(A) := not R(B) */
OP_LEN,/* A B R(A) := length of R(B) */

View File

@ -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
** See Copyright Notice in lua.h
*/
@ -990,10 +990,10 @@ static BinOpr getbinopr (int op) {
case '+': return OPR_ADD;
case '-': return OPR_SUB;
case '*': return OPR_MUL;
case '/': return OPR_DIV;
case TK_IDIV: return OPR_IDIV;
case '%': return OPR_MOD;
case '^': return OPR_POW;
case '/': return OPR_DIV;
case TK_IDIV: return OPR_IDIV;
case TK_CONCAT: return OPR_CONCAT;
case TK_NE: return OPR_NE;
case TK_EQ: return OPR_EQ;
@ -1012,12 +1012,14 @@ 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}, {7, 7}, {7, 7}, /* '*' '/' '//' '%' */
{10, 9}, {5, 4}, /* ^, .. (right associative) */
{3, 3}, {3, 3}, {3, 3}, /* ==, <, <= */
{3, 3}, {3, 3}, {3, 3}, /* ~=, >, >= */
{2, 2}, {1, 1} /* and, or */
{6, 6}, {6, 6}, /* '+' '-' */
{7, 7}, {7, 7}, /* '*' '%' */
{10, 9}, /* '^' (right associative) */
{7, 7}, {7, 7}, /* '/' '//' */
{5, 4}, /* '..' (right associative) */
{3, 3}, {3, 3}, {3, 3}, /* ==, <, <= */
{3, 3}, {3, 3}, {3, 3}, /* ~=, >, >= */
{2, 2}, {1, 1} /* and, or */
};
#define UNARY_PRIORITY 8 /* priority for unary operators */

View File

@ -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
** 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") {
static char ops[] = "+-*/\\%^_"; /* '\' -> '//'; '_' -> '..' */
static char ops[] = "+-*%^/\\_"; /* '\' -> '//'; '_' -> '..' */
int op;
skip(&pc);
op = strchr(ops, *pc++) - ops;

7
ltm.c
View File

@ -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
** See Copyright Notice in lua.h
*/
@ -36,8 +36,9 @@ void luaT_init (lua_State *L) {
static const char *const luaT_eventname[] = { /* ORDER TM */
"__index", "__newindex",
"__gc", "__mode", "__len", "__eq",
"__add", "__sub", "__mul", "__div", "__idiv", "__mod",
"__pow", "__unm", "__lt", "__le",
"__add", "__sub", "__mul", "__mod", "__pow",
"__div", "__idiv",
"__unm", "__lt", "__le",
"__concat", "__call"
};
int i;

8
ltm.h
View File

@ -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
** See Copyright Notice in lua.h
*/
@ -13,7 +13,7 @@
/*
* WARNING: if you change the order of this enumeration,
* grep "ORDER TM"
* grep "ORDER TM" and "ORDER OP"
*/
typedef enum {
TM_INDEX,
@ -25,10 +25,10 @@ typedef enum {
TM_ADD,
TM_SUB,
TM_MUL,
TM_DIV,
TM_IDIV,
TM_MOD,
TM_POW,
TM_DIV,
TM_IDIV,
TM_UNM,
TM_LT,
TM_LE,

12
lua.h
View File

@ -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.org, PUC-Rio, Brazil (http://www.lua.org)
** 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
*/
#define LUA_OPADD 0 /* ORDER TM */
#define LUA_OPADD 0 /* ORDER TM, ORDER OP */
#define LUA_OPSUB 1
#define LUA_OPMUL 2
#define LUA_OPDIV 3
#define LUA_OPIDIV 4
#define LUA_OPMOD 5
#define LUA_OPPOW 6
#define LUA_OPMOD 3
#define LUA_OPPOW 4
#define LUA_OPDIV 5
#define LUA_OPIDIV 6
#define LUA_OPUNM 7
LUA_API void (lua_arith) (lua_State *L, int op);