diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-12-18 12:12:03 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-12-18 12:12:03 -0200 |
commit | c0edab0f6de39ba5ae2e2f1540fa98f1b507afec (patch) | |
tree | 9401b36cf92a0f37ae6a9cf052c99ff11787246e /lobject.c | |
parent | a948054a1951cd526c732d6a0e16d99cae837d49 (diff) | |
download | lua-c0edab0f6de39ba5ae2e2f1540fa98f1b507afec.tar.gz lua-c0edab0f6de39ba5ae2e2f1540fa98f1b507afec.tar.bz2 lua-c0edab0f6de39ba5ae2e2f1540fa98f1b507afec.zip |
first implementation of bitwise operators '&' (band), '|' (bor),
and '~' (bxor)
Diffstat (limited to 'lobject.c')
-rw-r--r-- | lobject.c | 49 |
1 files changed, 32 insertions, 17 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lobject.c,v 2.68 2013/07/10 17:15:12 roberto Exp roberto $ | 2 | ** $Id: lobject.c,v 2.69 2013/12/16 14:30:22 roberto Exp roberto $ |
3 | ** Some generic functions over Lua objects | 3 | ** Some generic functions over Lua objects |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -78,6 +78,10 @@ static lua_Integer intarith (lua_State *L, int op, lua_Integer v1, | |||
78 | case LUA_OPMUL:return intop(*, v1, v2); | 78 | case LUA_OPMUL:return intop(*, v1, v2); |
79 | case LUA_OPMOD: return luaV_mod(L, v1, v2); | 79 | case LUA_OPMOD: return luaV_mod(L, v1, v2); |
80 | case LUA_OPPOW: return luaV_pow(L, v1, v2); | 80 | case LUA_OPPOW: return luaV_pow(L, v1, v2); |
81 | case LUA_OPIDIV: return luaV_div(L, v1, v2); | ||
82 | case LUA_OPBAND: return intop(&, v1, v2); | ||
83 | case LUA_OPBOR: return intop(|, v1, v2); | ||
84 | case LUA_OPBXOR: return intop(^, v1, v2); | ||
81 | case LUA_OPUNM: return intop(-, 0, v1); | 85 | case LUA_OPUNM: return intop(-, 0, v1); |
82 | default: lua_assert(0); return 0; | 86 | default: lua_assert(0); return 0; |
83 | } | 87 | } |
@@ -100,25 +104,36 @@ static lua_Number numarith (int op, lua_Number v1, lua_Number v2) { | |||
100 | 104 | ||
101 | void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, | 105 | void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, |
102 | TValue *res) { | 106 | TValue *res) { |
103 | if (op == LUA_OPIDIV) { /* operates only on integers */ | 107 | switch (op) { |
104 | lua_Integer i1; lua_Integer i2; | 108 | case LUA_OPIDIV: case LUA_OPBAND: case LUA_OPBOR: |
105 | if (tointeger(p1, &i1) && tointeger(p2, &i2)) { | 109 | case LUA_OPBXOR: { /* operates only on integers */ |
106 | setivalue(res, luaV_div(L, i1, i2)); | 110 | lua_Integer i1; lua_Integer i2; |
107 | return; | 111 | if (tointeger(p1, &i1) && tointeger(p2, &i2)) { |
112 | setivalue(res, intarith(L, op, i1, i2)); | ||
113 | return; | ||
114 | } | ||
115 | else break; /* go to the end */ | ||
108 | } | 116 | } |
109 | /* else go to the end */ | 117 | case LUA_OPDIV: { /* operates only on floats */ |
110 | } | 118 | lua_Number n1; lua_Number n2; |
111 | else { /* other operations */ | 119 | if (tonumber(p1, &n1) && tonumber(p2, &n2)) { |
112 | lua_Number n1; lua_Number n2; | 120 | setnvalue(res, numarith(op, n1, n2)); |
113 | if (ttisinteger(p1) && ttisinteger(p2) && op != LUA_OPDIV) { | 121 | return; |
114 | setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2))); | 122 | } |
115 | return; | 123 | else break; /* go to the end */ |
116 | } | 124 | } |
117 | else if (tonumber(p1, &n1) && tonumber(p2, &n2)) { | 125 | default: { /* other operations */ |
118 | setnvalue(res, numarith(op, n1, n2)); | 126 | lua_Number n1; lua_Number n2; |
119 | return; | 127 | if (ttisinteger(p1) && ttisinteger(p2)) { |
128 | setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2))); | ||
129 | return; | ||
130 | } | ||
131 | else if (tonumber(p1, &n1) && tonumber(p2, &n2)) { | ||
132 | setnvalue(res, numarith(op, n1, n2)); | ||
133 | return; | ||
134 | } | ||
135 | else break; /* go to the end */ | ||
120 | } | 136 | } |
121 | /* else go to the end */ | ||
122 | } | 137 | } |
123 | /* could not perform raw operation; try metmethod */ | 138 | /* could not perform raw operation; try metmethod */ |
124 | lua_assert(L != NULL); /* should not fail when folding (compile time) */ | 139 | lua_assert(L != NULL); /* should not fail when folding (compile time) */ |