generated from chaos-lang/template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.c
374 lines (325 loc) · 11.2 KB
/
json.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include <stdlib.h>
#include <string.h>
#include "Chaos.h"
// Dictionary Operations
// list json.keys(dict d)
char *keys_params_name[] = {
"d"
};
unsigned keys_params_type[] = {
K_DICT
};
unsigned keys_params_secondary_type[] = {
K_ANY
};
unsigned short keys_params_length = (unsigned short) sizeof(keys_params_type) / sizeof(unsigned);
int KAOS_EXPORT Kaos_keys()
{
unsigned long dict_length = kaos.getDictLength(keys_params_name[0]);
enum Type dict_type = kaos.getDictType(keys_params_name[0]);
kaos.startBuildingList();
for (unsigned long i = 0; i < dict_length; i++) {
char *key = kaos.getDictKeyByIndex(keys_params_name[0], (long long) i);
kaos.createVariableString(NULL, key);
free(key);
}
kaos.returnList(dict_type);
return 0;
}
// list json.values(dict d)
char *values_params_name[] = {
"d"
};
unsigned values_params_type[] = {
K_DICT
};
unsigned values_params_secondary_type[] = {
K_ANY
};
unsigned short values_params_length = (unsigned short) sizeof(values_params_type) / sizeof(unsigned);
int KAOS_EXPORT Kaos_values()
{
unsigned long dict_length = kaos.getDictLength(values_params_name[0]);
enum Type dict_type = kaos.getDictType(values_params_name[0]);
kaos.startBuildingList();
for (unsigned long i = 0; i < dict_length; i++) {
char *key = kaos.getDictKeyByIndex(values_params_name[0], (long long) i);
enum ValueType value_type = kaos.getDictElementValueType(values_params_name[0], key);
char *value = NULL;
switch (value_type)
{
case V_BOOL:
kaos.createVariableBool(NULL, kaos.getDictElementBool(values_params_name[0], key));
break;
case V_INT:
kaos.createVariableInt(NULL, kaos.getDictElementInt(values_params_name[0], key));
break;
case V_FLOAT:
kaos.createVariableFloat(NULL, kaos.getDictElementFloat(values_params_name[0], key));
break;
case V_STRING:
value = kaos.getDictElementString(values_params_name[0], key);
kaos.createVariableString(NULL, value);
free(value);
break;
default:
break;
}
free(key);
}
kaos.returnList(dict_type);
return 0;
}
// dict json.flip(dict d)
char *flip_params_name[] = {
"d"
};
unsigned flip_params_type[] = {
K_DICT
};
unsigned flip_params_secondary_type[] = {
K_ANY
};
unsigned short flip_params_length = (unsigned short) sizeof(flip_params_type) / sizeof(unsigned);
int KAOS_EXPORT Kaos_flip()
{
unsigned long dict_length = kaos.getDictLength(flip_params_name[0]);
enum Type dict_type = kaos.getDictType(flip_params_name[0]);
kaos.startBuildingDict();
for (unsigned long i = 0; i < dict_length; i++) {
char *key = kaos.getDictKeyByIndex(flip_params_name[0], (long long) i);
char *value = kaos.getDictElementStringByTypeCasting(flip_params_name[0], key);
kaos.createVariableString(value, key);
free(key);
free(value);
}
kaos.returnDict(dict_type);
return 0;
}
// JSON Related
// str json.encode(dict d)
char *encode_params_name[] = {
"d"
};
unsigned encode_params_type[] = {
K_DICT
};
unsigned encode_params_secondary_type[] = {
K_ANY
};
unsigned short encode_params_length = (unsigned short) sizeof(encode_params_type) / sizeof(unsigned);
int KAOS_EXPORT Kaos_encode()
{
char *json = kaos.dumpVariableToString(encode_params_name[0], false, true, true);
kaos.returnVariableString(json);
free(json);
return 0;
}
// dict json.decode(str json)
char *decode_params_name[] = {
"json"
};
unsigned decode_params_type[] = {
K_STRING
};
unsigned decode_params_secondary_type[] = {
K_ANY
};
unsigned short decode_params_length = (unsigned short) sizeof(decode_params_type) / sizeof(unsigned);
int KAOS_EXPORT Kaos_decode()
{
char *json = kaos.getVariableString(decode_params_name[0]);
kaos.parseJson(json);
free(json);
return 0;
}
// Searching & Replacing
// str json.search(dict haystack, any needle)
char *search_params_name[] = {
"haystack",
"needle"
};
unsigned search_params_type[] = {
K_DICT,
K_ANY
};
unsigned search_params_secondary_type[] = {
K_ANY,
K_ANY
};
unsigned short search_params_length = (unsigned short) sizeof(search_params_type) / sizeof(unsigned);
int KAOS_EXPORT Kaos_search()
{
unsigned long dict_length = kaos.getDictLength(search_params_name[0]);
for (unsigned long i = 0; i < dict_length; i++) {
char *key = kaos.getDictKeyByIndex(search_params_name[0], (long long) i);
enum ValueType value_type = kaos.getDictElementValueType(search_params_name[0], key);
bool x_b, y_b;
long long x_i, y_i;
long double x_f, y_f;
char *x_s, *y_s;
switch (value_type)
{
case V_BOOL:
x_b = kaos.getVariableBool(search_params_name[1]);
y_b = kaos.getDictElementBool(search_params_name[0], key);
if (x_b == y_b) {
kaos.returnVariableString(key);
free(key);
return 0;
}
break;
case V_INT:
x_i = kaos.getVariableInt(search_params_name[1]);
y_i = kaos.getDictElementInt(search_params_name[0], key);
if (x_i == y_i) {
kaos.returnVariableString(key);
free(key);
return 0;
}
break;
case V_FLOAT:
x_f = kaos.getVariableFloat(search_params_name[1]);
y_f = kaos.getDictElementFloat(search_params_name[0], key);
if (x_f == y_f) {
kaos.returnVariableString(key);
free(key);
return 0;
}
break;
case V_STRING:
x_s = kaos.getVariableString(search_params_name[1]);
y_s = kaos.getDictElementString(search_params_name[0], key);
if (strcmp(x_s, y_s) == 0) {
free(x_s);
free(y_s);
kaos.returnVariableString(key);
free(key);
return 0;
}
free(x_s);
free(y_s);
break;
default:
break;
}
free(key);
}
kaos.returnVariableString("");
return 0;
}
// dict json.replace(dict haystack, any needle, any replacement)
char *replace_params_name[] = {
"haystack",
"needle",
"replacement"
};
unsigned replace_params_type[] = {
K_DICT,
K_ANY,
K_ANY
};
unsigned replace_params_secondary_type[] = {
K_ANY,
K_ANY,
K_ANY
};
unsigned short replace_params_length = (unsigned short) sizeof(replace_params_type) / sizeof(unsigned);
int KAOS_EXPORT Kaos_replace()
{
unsigned long dict_length = kaos.getDictLength(replace_params_name[0]);
enum Type dict_type = kaos.getDictType(replace_params_name[0]);
kaos.startBuildingDict();
for (unsigned long i = 0; i < dict_length; i++) {
char *key = kaos.getDictKeyByIndex(replace_params_name[0], (long long) i);
enum ValueType value_type = kaos.getDictElementValueType(replace_params_name[0], key);
bool x_b, y_b;
long long x_i, y_i;
long double x_f, y_f;
char *x_s, *y_s;
char *replacement;
switch (value_type)
{
case V_BOOL:
x_b = kaos.getVariableBool(replace_params_name[1]);
y_b = kaos.getDictElementBool(replace_params_name[0], key);
if (x_b == y_b) {
kaos.createVariableBool(key, kaos.getVariableBool(replace_params_name[2]));
} else {
kaos.createVariableBool(key, y_b);
}
break;
case V_INT:
x_i = kaos.getVariableInt(replace_params_name[1]);
y_i = kaos.getDictElementInt(replace_params_name[0], key);
if (x_i == y_i) {
kaos.createVariableInt(key, kaos.getVariableInt(replace_params_name[2]));
} else {
kaos.createVariableInt(key, y_i);
}
break;
case V_FLOAT:
x_f = kaos.getVariableFloat(replace_params_name[1]);
y_f = kaos.getDictElementFloat(replace_params_name[0], key);
if (x_f == y_f) {
kaos.createVariableFloat(key, kaos.getVariableFloat(replace_params_name[2]));
} else {
kaos.createVariableFloat(key, y_f);
}
break;
case V_STRING:
x_s = kaos.getVariableString(replace_params_name[1]);
y_s = kaos.getDictElementString(replace_params_name[0], key);
if (strcmp(x_s, y_s) == 0) {
replacement = kaos.getVariableString(replace_params_name[2]);
kaos.createVariableString(key, replacement);
free(replacement);
} else {
kaos.createVariableString(key, y_s);
}
free(x_s);
free(y_s);
break;
default:
break;
}
free(key);
}
kaos.returnDict(dict_type);
return 0;
}
// Information Functions
// num json.count(dict d)
char *count_params_name[] = {
"d"
};
unsigned count_params_type[] = {
K_DICT
};
unsigned count_params_secondary_type[] = {
K_ANY
};
unsigned short count_params_length = (unsigned short) sizeof(count_params_type) / sizeof(unsigned);
int KAOS_EXPORT Kaos_count()
{
unsigned long dict_length = kaos.getDictLength(count_params_name[0]);
kaos.returnVariableInt((long long) dict_length);
return 0;
}
int KAOS_EXPORT KaosRegister(struct Kaos _kaos)
{
kaos = _kaos;
// Dictionary Operations
kaos.defineFunction("keys", K_LIST, K_ANY, keys_params_name, keys_params_type, keys_params_secondary_type, keys_params_length, NULL, 0);
kaos.defineFunction("values", K_LIST, K_ANY, values_params_name, values_params_type, values_params_secondary_type, values_params_length, NULL, 0);
kaos.defineFunction("flip", K_DICT, K_ANY, flip_params_name, flip_params_type, flip_params_secondary_type, flip_params_length, NULL, 0);
// JSON Related
kaos.defineFunction("encode", K_STRING, K_ANY, encode_params_name, encode_params_type, encode_params_secondary_type, encode_params_length, NULL, 0);
kaos.defineFunction("decode", K_DICT, K_ANY, decode_params_name, decode_params_type, decode_params_secondary_type, decode_params_length, NULL, 0);
// Searching & Replacing
kaos.defineFunction("search", K_STRING, K_ANY, search_params_name, search_params_type, search_params_secondary_type, search_params_length, NULL, 0);
kaos.defineFunction("replace", K_DICT, K_ANY, replace_params_name, replace_params_type, replace_params_secondary_type, replace_params_length, NULL, 0);
// Information Functions
kaos.defineFunction("count", K_NUMBER, K_ANY, count_params_name, count_params_type, count_params_secondary_type, count_params_length, NULL, 0);
return 0;
}