diff options
Diffstat (limited to 'src/host/genminilua.lua')
-rw-r--r-- | src/host/genminilua.lua | 66 |
1 files changed, 65 insertions, 1 deletions
diff --git a/src/host/genminilua.lua b/src/host/genminilua.lua index 587d5a8d..2a8a5336 100644 --- a/src/host/genminilua.lua +++ b/src/host/genminilua.lua | |||
@@ -58,10 +58,68 @@ end) | |||
58 | local REMOVE_EXTINC = { ["<assert.h>"] = true, ["<locale.h>"] = true, } | 58 | local REMOVE_EXTINC = { ["<assert.h>"] = true, ["<locale.h>"] = true, } |
59 | 59 | ||
60 | local CUSTOM_MAIN = [[ | 60 | local CUSTOM_MAIN = [[ |
61 | typedef unsigned int UB; | ||
62 | static UB barg(lua_State *L,int idx){ | ||
63 | union{lua_Number n;U64 b;}bn; | ||
64 | bn.n=lua_tonumber(L,idx)+6755399441055744.0; | ||
65 | if (bn.n==0.0&&!lua_isnumber(L,idx))luaL_typerror(L,idx,"number"); | ||
66 | return(UB)bn.b; | ||
67 | } | ||
68 | #define BRET(b) lua_pushnumber(L,(lua_Number)(int)(b));return 1; | ||
69 | static int tobit(lua_State *L){ | ||
70 | BRET(barg(L,1))} | ||
71 | static int bnot(lua_State *L){ | ||
72 | BRET(~barg(L,1))} | ||
73 | static int band(lua_State *L){ | ||
74 | int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b&=barg(L,i);BRET(b)} | ||
75 | static int bor(lua_State *L){ | ||
76 | int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b|=barg(L,i);BRET(b)} | ||
77 | static int bxor(lua_State *L){ | ||
78 | int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b^=barg(L,i);BRET(b)} | ||
79 | static int lshift(lua_State *L){ | ||
80 | UB b=barg(L,1),n=barg(L,2)&31;BRET(b<<n)} | ||
81 | static int rshift(lua_State *L){ | ||
82 | UB b=barg(L,1),n=barg(L,2)&31;BRET(b>>n)} | ||
83 | static int arshift(lua_State *L){ | ||
84 | UB b=barg(L,1),n=barg(L,2)&31;BRET((int)b>>n)} | ||
85 | static int rol(lua_State *L){ | ||
86 | UB b=barg(L,1),n=barg(L,2)&31;BRET((b<<n)|(b>>(32-n)))} | ||
87 | static int ror(lua_State *L){ | ||
88 | UB b=barg(L,1),n=barg(L,2)&31;BRET((b>>n)|(b<<(32-n)))} | ||
89 | static int bswap(lua_State *L){ | ||
90 | UB b=barg(L,1);b=(b>>24)|((b>>8)&0xff00)|((b&0xff00)<<8)|(b<<24);BRET(b)} | ||
91 | static int tohex(lua_State *L){ | ||
92 | UB b=barg(L,1); | ||
93 | int n=lua_isnone(L,2)?8:(int)barg(L,2); | ||
94 | const char *hexdigits="0123456789abcdef"; | ||
95 | char buf[8]; | ||
96 | int i; | ||
97 | if(n<0){n=-n;hexdigits="0123456789ABCDEF";} | ||
98 | if(n>8)n=8; | ||
99 | for(i=(int)n;--i>=0;){buf[i]=hexdigits[b&15];b>>=4;} | ||
100 | lua_pushlstring(L,buf,(size_t)n); | ||
101 | return 1; | ||
102 | } | ||
103 | static const struct luaL_Reg bitlib[] = { | ||
104 | {"tobit",tobit}, | ||
105 | {"bnot",bnot}, | ||
106 | {"band",band}, | ||
107 | {"bor",bor}, | ||
108 | {"bxor",bxor}, | ||
109 | {"lshift",lshift}, | ||
110 | {"rshift",rshift}, | ||
111 | {"arshift",arshift}, | ||
112 | {"rol",rol}, | ||
113 | {"ror",ror}, | ||
114 | {"bswap",bswap}, | ||
115 | {"tohex",tohex}, | ||
116 | {NULL,NULL} | ||
117 | }; | ||
61 | int main(int argc, char **argv){ | 118 | int main(int argc, char **argv){ |
62 | lua_State *L = luaL_newstate(); | 119 | lua_State *L = luaL_newstate(); |
63 | int i; | 120 | int i; |
64 | luaL_openlibs(L); | 121 | luaL_openlibs(L); |
122 | luaL_register(L, "bit", bitlib); | ||
65 | if (argc < 2) return sizeof(void *); | 123 | if (argc < 2) return sizeof(void *); |
66 | lua_createtable(L, 0, 1); | 124 | lua_createtable(L, 0, 1); |
67 | lua_pushstring(L, argv[1]); | 125 | lua_pushstring(L, argv[1]); |
@@ -146,7 +204,13 @@ local function def_istrue(def) | |||
146 | def == "UNUSED" | 204 | def == "UNUSED" |
147 | end | 205 | end |
148 | 206 | ||
149 | local head, defs = {}, {} | 207 | local head, defs = {[[ |
208 | #ifdef _MSC_VER | ||
209 | typedef unsigned __int64 U64; | ||
210 | #else | ||
211 | typedef unsigned long long U64; | ||
212 | #endif | ||
213 | ]]}, {} | ||
150 | 214 | ||
151 | local function preprocess(src) | 215 | local function preprocess(src) |
152 | local t = { match(src, "^(.-)#") } | 216 | local t = { match(src, "^(.-)#") } |