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
|
#include <unistd.h>
#include "i_str.h"
#include "io.h"
#include "stdlib.h"
#include "stdio.h"
#include "config.h"
#include "stdint.h"
#include "table.h"
int l_readfile(lua_State* L){
size_t len;
char* a = (char*)luaL_checklstring(L, 1, &len);
FILE *fp;
const uint64_t chunk_iter = _file_malloc_chunk;
uint64_t chunk = chunk_iter;
uint64_t count = 0;
if(access(a, F_OK)) {
p_fatal("file not found");
}
if(access(a, R_OK)){
p_fatal("missing permissions");
}
fp = fopen(a, "r");
char* out = malloc(chunk);
for(;;){
char ch = fgetc(fp);
if(ch==EOF) break;
if(count >= chunk){
chunk += chunk_iter;
out = realloc(out, chunk);
}
out[count] = ch;
count++;
}
out[count] = '\0';
lua_pushstring(L, out);
fclose(fp);
free(out);
return 1;
};
lua_Debug i_get_debug(lua_State* L){
lua_Debug ar;
lua_getstack(L, 1, &ar);
lua_getinfo(L, "nSl", &ar);
return ar;
}
int l_debug(lua_State* L){
size_t input_len = 0;
char* input = (char*)luaL_checklstring(L, 1, &input_len);
lua_Debug debug = i_get_debug(L);
printf(color_gray"[ debug ] (%s:%i) %s\n"color_reset, debug.source, debug.currentline, input);
return 0;
}
int l_log(lua_State* L){
size_t input_len = 0;
char* input = (char*)luaL_checklstring(L, 1, &input_len);
lua_Debug debug = i_get_debug(L);
printf(color_green"[ log ] (%s:%i)"color_gray" %s\n"color_reset, debug.source, debug.currentline, input);
return 0;
}
int l_warn(lua_State* L){
size_t input_len = 0;
char* input = (char*)luaL_checklstring(L, 1, &input_len);
lua_Debug debug = i_get_debug(L);
printf(color_yellow"[ warn ] (%s:%i) %s\n"color_reset, debug.source, debug.currentline, input);
return 0;
}
int l_error(lua_State* L){
size_t input_len = 0;
char* input = (char*)luaL_checklstring(L, 1, &input_len);
lua_Debug debug = i_get_debug(L);
printf(color_red"[ error ] (%s:%i) %s\n"color_reset, debug.source, debug.currentline, input);
return 0;
}
void print_indentation(int i){
for(int z = 0; z < i; z++) printf(" ");
}
void i_pprint(lua_State* L, int indent, int skip_indent){
int last_idx = lua_gettop(L);
const char* type = lua_typename(L,lua_type(L,-1));
int t = lua_type(L,-1);
switch(lua_type(L,-1)){
case LUA_TTABLE:
printf(" ");
if(indent >= _max_depth && _max_depth >= 0) {
printf("{"color_gray);
if(_collapse_to_memory) printf(" %p ",lua_topointer(L, -1));
else printf(" ... ");
printf(color_reset"}");
break;
}
int skip = i_len(L,last_idx) < _start_nl_at || _collapse_all;
printf("{");
if(!skip) printf("\n");
lua_pushnil(L);
for(;lua_next(L,last_idx) != 0;){
if(lua_type(L,-2) == LUA_TSTRING){
if(!skip) print_indentation(indent);
printf(" %s"color_gray": "color_reset, lua_tostring(L,-2));
}
i_pprint(L,indent+1,1);
printf(",");
if(!skip) printf("\n");
lua_pop(L,1);
}
print_indentation(indent);
printf("}");
break;
case LUA_TSTRING:
if(!skip_indent) print_indentation(indent);
printf("\"%s\"", lua_tostring(L,-1));
break;
case LUA_TFUNCTION:
if(!skip_indent) print_indentation(indent);
printf(color_yellow"(fn)"color_reset);
break;
default:
if(!skip_indent) print_indentation(indent);
printf(color_yellow"%s"color_reset, lua_tostring(L,-1));
}
if(_print_type){
if(lua_istable(L,last_idx)) printf(color_gray" : <%s:%lu>"color_reset,type,i_len(L,last_idx));
else printf(color_gray" : <%s>"color_reset,type);
}
}
int l_pprint(lua_State* L){
i_pprint(L,0,0);
printf("\n");
return 0;
}
enum json_state {
normal, reading
};
#define push() \
if(is_array){\
lua_pushinteger(L, i);\
} else {\
char kbuf[key->len];\
memset(kbuf, 0, key->len);\
strncpy(kbuf, key->c + 1, key->len - 2);\
lua_pushstring(L, kbuf);\
}\
char buf[value->len];\
memset(buf, 0, value->len);\
switch(value->c[0]){\
case '{': case '[':\
json_parse(L, value);\
break;\
case '"':\
strncpy(buf, value->c + 1, value->len - 2);\
lua_pushstring(L, buf);\
break;\
default:\
lua_pushnumber(L, atof(value->c));\
break;\
}\
lua_settable(L, last_idx);
void json_parse(lua_State* L, str* raw){
enum json_state state = normal;
char topush[2] = {0,0};
int count = 0;
char last = '\0';
int token_depth = 0;
str* key = str_init("");
str* value = str_init("");
lua_newtable(L);
int last_idx = lua_gettop(L);
int is_array = raw->c[0] == '[';
int i = 1;
for(i = 1; i != raw->len - 1; i++){
topush[0] = *(raw->c + i);
if(state == normal && (topush[0] == ' ' || topush[0] == '\n')) continue;
switch(topush[0]){
case '"':
case '{': case '}':
case '[': case ']':
if(last == '{' && topush[0] == '}' || last == '[' && topush[0] == ']') token_depth--;
if((last == '\0' || last == '"' && topush[0] == '"'
|| last == '{' && topush[0] == '}' || last == '[' && topush[0] == ']')){
if(token_depth == 0){
if(last == '\0'){
last = topush[0];
} else {
last = '\0';
}
if(state==reading) state = normal;
else state = reading;
}
}
break;
case ',':
if(state == normal){
push()
str_clear(key);
str_clear(value);
count = 0;
continue;
}
break;
case ':':
if(state == normal){
count++;
continue;
}
break;
}
if(last == '{' && topush[0] == '{' || last == '[' && topush[0] == '[') token_depth++;
if(count == 0 && !is_array) str_push(key, topush);
else str_push(value, topush);
}
push()
str_free(key);
str_free(value);
//printf("key: %s, value : %s\n",key.c,value.c);
}
int l_json_parse(lua_State* L){
str* raw_json = str_init((char*)luaL_checklstring(L, 1, NULL));
json_parse(L, raw_json);
return 1;
}
|