-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathluab.c
308 lines (283 loc) · 7.49 KB
/
luab.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
* Copyright (c) 2017-2018 Simon Schmidt
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "luab.h"
#include "luasrc/lualib.h"
#include "luasrc/lauxlib.h"
#include "parser.h"
#include <stdio.h>
#include <stdlib.h>
#include "buildmacro.h"
#if 0
static int this_is_not_a_function (lua_State *L){
return 0;
}
#endif
static int solve(lua_State *L){
struct tokenizer tok;
tok.buffer = smust(luab_tosds(L,1));
tok.source = 0;
OutputStream dest = OutputStream_new();
lua_settop(L,0);
parser_parse(&tok,L,dest);
lua_settop(L,0);
sdsfree(tok.buffer);
luab_pushsds(L,(sds)(dest->data) );
OutputStream_detroy(dest);
return 1;
}
static int normalize (lua_State *L){
int i,n;
n = lua_gettop(L);
for(i=1;i<=n;i++){
switch(lua_type(L,i)){
case LUA_TNIL:
case LUA_TBOOLEAN:
case LUA_TTABLE:
case LUA_TFUNCTION:
case LUA_TUSERDATA:
case LUA_TTHREAD:
case LUA_TLIGHTUSERDATA:
lua_pushliteral(L,"");
lua_replace(L,i);
break;
case LUA_TNUMBER:
lua_tostring(L,i);
break;
}
}
lua_settop(L,n);
return 1;
}
static int import_file (lua_State *L){
lua_settop(L,1);
const char* fp = lua_tostring(L,1);
switch(luaL_loadfile(L,fp)) {
case LUA_OK: goto noerr;
case LUA_ERRFILE:
fprintf(stderr,"File not found: %s",fp);
lua_pushfstring(L,"File not found: %s",fp);
break;
default:
fprintf(stderr,"Error parsing file: %s",fp);
lua_pushfstring(L,"Error parsing file: %s",fp);
}
return lua_error(L);
noerr:
lua_call(L,0,0);
lua_settop(L,0);
return 0;
}
static int include_file(lua_State *L){
lua_settop(L,1);
const char* fp = lua_tostring(L,1);
struct tokenizer tok;
tok.buffer = smust(sdsempty());
tok.source = ptrmust(fopen(fp,"r"));
OutputStream dest = OutputStream_new();
lua_settop(L,0);
parser_parse(&tok,L,dest);
lua_settop(L,0);
sdsfree(tok.buffer);
fclose((FILE*)tok.source);
luab_pushsds(L,(sds)(dest->data) );
OutputStream_detroy(dest);
return 1;
}
static int bdmcr_build_macro_lua (lua_State *L){
int i,j,m,n;
static sds margs[1024];
sds *args=0;
sds *allc=0;
sds body,result;
n = lua_gettop(L);
if(n<1){
lua_pushliteral(L,"");
return 1;
}
body = smust(luab_tosds(L,1));
m = n-1;
if(m<1024) args = margs;
else args=allc=malloc(sizeof(sds)*m); /* XXX: Overflow hazard. */
for(i=0,j=2;i<m;++i) {
args[i] = smust(luab_tosds(L,j));
}
result = bdmcr_build_macro(args,m,body);
sdsfree(body);
for(i=0,j=2;i<m;++i)
sdsfree(args[i]);
if(allc)free(allc);
lua_settop(L,0);
luab_pushsds(L,result);
sdsfree(result);
return 1;
}
static int stringify (lua_State *L){
static const char hex[]="0123456789ABCDEF";
char buf[] = "\\ ";
char buf2[2];
int x;
const char* ccin;
size_t i;
size_t ccins;
OutputStream dest;
lua_settop(L,1);
dest = OutputStream_new();
ccin = lua_tolstring(L,1,&ccins);
OutputStream_write(dest,"\"",1);
for(i=0;i<ccins;++i) switch(x=(ccin[i]&0xff) ){
case '\\':
case '\"':
case '\'':
buf[1]=x;
OutputStream_write(dest,buf,2);
continue;
case 0 ... 31:
case 0x7f ... 0xff:
switch(x){
case '\r': OutputStream_write(dest,"\\r",2); continue;
case '\n': OutputStream_write(dest,"\\n",2); continue;
case '\t': OutputStream_write(dest,"\\t",2); continue;
}
buf2[0]=hex[(x>>4)&0xf];
buf2[1]=hex[(x )&0xf];
OutputStream_write(dest,"\\x",2);
OutputStream_write(dest,buf2,2);
continue;
default:
OutputStream_write(dest,ccin+i,1);
}
OutputStream_write(dest,"\"",1);
lua_settop(L,0);
luab_pushsds(L,(sds)(dest->data) );
OutputStream_detroy(dest);
return 1;
}
static int append (lua_State *L){
lua_Integer li;
lua_len(L,-2);
li = lua_tointeger(L,-1);
lua_pop(L,1);
lua_rawseti(L,-2,li+1);
return 0;
}
void luab_append(lua_State* L){
append(L);
}
static int checkfile(lua_State *L){
FILE * fobj;
const char* fp;
int ok = 0;
lua_settop(L,1);
fp = lua_tostring(L,1);
if(!fp) goto done;
fobj = fopen(fp,"r");
if(!fobj) goto done;
fclose(fobj);
ok = 1;
done:
lua_settop(L,0);
lua_pushboolean(L,ok);
return 1;
}
#define register_const(L,name) (lua_pushinteger(L, name),lua_setglobal(L, #name ))
lua_State* create_lua(){
lua_State *L = (lua_State*)ptrmust(luaL_newstate());
luaL_openlibs(L);
lua_settop(L,0);
lua_register(L,"solve",solve);
lua_register(L,"normalize",normalize);
lua_register(L,"import_file",import_file);
lua_register(L,"include_file",include_file);
lua_register(L,"bdmcr_build_macro",bdmcr_build_macro_lua);
lua_register(L,"stringify",stringify);
lua_register(L,"append",append);
lua_register(L,"checkfile",checkfile);
lua_newtable(L);
lua_setglobal(L,"MACROS");
lua_newtable(L);
lua_setglobal(L,"MACROT");
//luaL_loadstring(L, "function(n,f,t) MACROS[n]=f; MACROT[n]=t; return nil end");
luaL_loadstring(L, "local n,f,t = ...; MACROS[n]=f; MACROT[n]=t; return nil");
lua_setglobal(L,"REGISTER");
lua_newtable(L);
lua_setglobal(L,"INCLUDES");
luaL_loadstring(L, "local incl = ...; local i,n; for i = 1, #INCLUDES do n=INCLUDES[i]..'/'..incl; if checkfile(n) then return n end; end; return incl ");
lua_setglobal(L,"find_include");
register_const(L, MT_ARGS);
register_const(L, MT_BODY);
register_const(L, MT_SEMI);
return L;
}
sds luab_tosds(lua_State* L,int index){
size_t s;
const char* cs = lua_tolstring(L,index,&s);
if(!cs) return (sds)0;
return smust(sdsnewlen(cs,s));
}
void luab_pushsds(lua_State* L,sds str){
lua_pushlstring(L,str,sdslen(str));
}
void luab_call(lua_State* L){
int top = lua_gettop(L);
lua_call(L,top-1,1);
}
int luab_getmacro(lua_State* L, sds macro){
int i;
lua_settop(L,0);
lua_getglobal(L,"MACROT");
lua_getfield(L,1,macro);
i = lua_tointeger(L,2);
lua_settop(L,0);
lua_getglobal(L,"MACROS");
if(LUA_TFUNCTION!=lua_getfield(L,1,macro))
return -1;
lua_remove(L,1);
return i;
}
void luab_setmacro(lua_State* L, const char* macro, const char* code,int type){
int rc;
lua_settop(L,0);
lua_getglobal(L,"MACROS");
rc = luaL_loadstring(L,code);
switch(rc){
case LUA_OK: break;
case LUA_ERRSYNTAX: fprintf(stderr,"LUA_ERRSYNTAX:%s\n",code); abort(); break;
case LUA_ERRMEM: fprintf(stderr,"LUA_ERRMEM:%s\n",code); abort(); break;
case LUA_ERRGCMM: fprintf(stderr,"LUA_ERRGCMM:%s\n",code); abort(); break;
default: printf("%d:%s\n",rc,code); abort(); break;
}
lua_setfield(L,1,macro);
lua_settop(L,0);
lua_getglobal(L,"MACROT");
lua_pushinteger(L, type);
lua_setfield(L,1,macro);
lua_settop(L,0);
}
sds luab_eval(lua_State* L,sds code){
luaL_loadstring(L, code);
lua_pcall(L,0,1,0);
const char* c = lua_tostring(L,-1);
if(!c) c="";
sds ret = smust(sdsnew(c));
lua_settop(L,0);
return ret;
}