]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | // |
2 | // Licensed to the Apache Software Foundation (ASF) under one | |
3 | // or more contributor license agreements. See the NOTICE file | |
4 | // distributed with this work for additional information | |
5 | // regarding copyright ownership. The ASF licenses this file | |
6 | // to you under the Apache License, Version 2.0 (the | |
7 | // "License"); you may not use this file except in compliance | |
8 | // with the License. You may obtain a copy of the License at | |
9 | // | |
10 | // http://www.apache.org/licenses/LICENSE-2.0 | |
11 | // | |
12 | // Unless required by applicable law or agreed to in writing, | |
13 | // software distributed under the License is distributed on an | |
14 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | // KIND, either express or implied. See the License for the | |
16 | // specific language governing permissions and limitations | |
17 | // under the License. | |
18 | // | |
19 | ||
20 | #include <lua.h> | |
21 | #include <lauxlib.h> | |
22 | ||
23 | static int l_not(lua_State *L) { | |
24 | int a = luaL_checkinteger(L, 1); | |
25 | a = ~a; | |
26 | lua_pushnumber(L, a); | |
27 | return 1; | |
28 | } | |
29 | ||
30 | static int l_xor(lua_State *L) { | |
31 | int a = luaL_checkinteger(L, 1); | |
32 | int b = luaL_checkinteger(L, 2); | |
33 | a ^= b; | |
34 | lua_pushnumber(L, a); | |
35 | return 1; | |
36 | } | |
37 | ||
38 | static int l_and(lua_State *L) { | |
39 | int a = luaL_checkinteger(L, 1); | |
40 | int b = luaL_checkinteger(L, 2); | |
41 | a &= b; | |
42 | lua_pushnumber(L, a); | |
43 | return 1; | |
44 | } | |
45 | ||
46 | static int l_or(lua_State *L) { | |
47 | int a = luaL_checkinteger(L, 1); | |
48 | int b = luaL_checkinteger(L, 2); | |
49 | a |= b; | |
50 | lua_pushnumber(L, a); | |
51 | return 1; | |
52 | } | |
53 | ||
54 | static int l_shiftr(lua_State *L) { | |
55 | int a = luaL_checkinteger(L, 1); | |
56 | int b = luaL_checkinteger(L, 2); | |
57 | a = a >> b; | |
58 | lua_pushnumber(L, a); | |
59 | return 1; | |
60 | } | |
61 | ||
62 | static int l_shiftl(lua_State *L) { | |
63 | int a = luaL_checkinteger(L, 1); | |
64 | int b = luaL_checkinteger(L, 2); | |
65 | a = a << b; | |
66 | lua_pushnumber(L, a); | |
67 | return 1; | |
68 | } | |
69 | ||
70 | static const struct luaL_Reg funcs[] = { | |
71 | {"band", l_and}, | |
72 | {"bor", l_or}, | |
73 | {"bxor", l_xor}, | |
74 | {"bnot", l_not}, | |
75 | {"shiftl", l_shiftl}, | |
76 | {"shiftr", l_shiftr}, | |
77 | {NULL, NULL} | |
78 | }; | |
79 | ||
80 | int luaopen_libluabitwise(lua_State *L) { | |
81 | luaL_register(L, "libluabitwise", funcs); | |
82 | return 1; | |
83 | } |