-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsts_embedding_extras.h
411 lines (291 loc) · 8.87 KB
/
sts_embedding_extras.h
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/* this file is released into the public domain */
/* this file isnt a critical part of the interpreter.
It is just heplful to use when embedding */
#ifndef STS_EMBEDDING_EXTRAS_H__
#define STS_EMBEDDING_EXTRAS_H__
#include "simpletinyscript.h"
sts_value_t *sts_value_create(sts_script_t *script, unsigned int type);
sts_value_t *sts_value_from_nstring(sts_script_t *script, char *string, unsigned int size);
sts_value_t *sts_value_from_string(sts_script_t *script, char *string);
sts_value_t *sts_value_from_number(sts_script_t *script, double num);
void sts_array_resize(sts_script_t *script, sts_value_t *value, unsigned int size);
void sts_array_append_insert(sts_script_t *script, sts_value_t *value_ptr, sts_value_t *value_insert, unsigned int position);
int sts_array_remove(sts_script_t *script, sts_value_t *value_ptr, unsigned int position);
unsigned int sts_hash(void *data, unsigned int size);
void sts_string_assemble(sts_script_t *script, char **dest, unsigned int current_size, char *middle_str, unsigned int middle_size, char *end_str, unsigned int end_size);
int sts_destroy_map(sts_script_t *script, sts_map_row_t *row);
sts_scope_t *sts_scope_push(sts_script_t *script, sts_scope_t *scope);
sts_scope_t *sts_scope_pop(sts_script_t *script, sts_scope_t *scope);
sts_map_row_t *sts_scope_search(sts_script_t *script, sts_scope_t *scope, void *key, unsigned int key_size);
int sts_hashmap_set(sts_script_t *script, sts_value_t *hashmap, char *key, unsigned int key_length, sts_value_t *value);
int sts_hashmap_del(sts_script_t *script, sts_value_t *hashmap, char *key, unsigned int key_length);
sts_value_t *sts_hashmap_get(sts_script_t *script, sts_value_t *hashmap, char *key, unsigned int key_length);
#endif /* end STS_EMBEDDING_EXTRAS_H__ */
#ifdef STS_EMBEDDING_EXTRAS_IMPLEMENTATION
#define STS_IMPLEMENTATION
#include "simpletinyscript.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
sts_value_t *sts_value_create(sts_script_t *script, unsigned int type)
{
sts_value_t *ret = NULL;
if(!STS_CREATE_VALUE(ret))
{
fprintf(stderr, "could not create value of type %u\n", type);
return NULL;
}
ret->references = 1;
ret->type = type;
return ret;
}
sts_value_t *sts_value_from_nstring(sts_script_t *script, char *string, unsigned int size)
{
sts_value_t *ret = NULL;
if(!(ret = sts_value_create(script, STS_STRING)))
{
fprintf(stderr, "could not create string value\n");
return NULL;
}
if(!(ret->string.data = (char *)sts_memdup(string, size)))
{
fprintf(stderr, "could not duplicate string value\n");
STS_FREE(ret);
return NULL;
}
ret->string.length = size;
return ret;
}
sts_value_t *sts_value_from_string(sts_script_t *script, char *string)
{
return sts_value_from_nstring(script, string, strlen(string));
}
sts_value_t *sts_value_from_number(sts_script_t *script, double num)
{
sts_value_t *ret = NULL;
if(!(ret = sts_value_create(script, STS_NUMBER)))
{
fprintf(stderr, "could not create number value\n");
return NULL;
}
ret->number = num;
return ret;
}
sts_value_t *sts_value_from_boolean(sts_script_t *script, char boolean)
{
sts_value_t *ret = NULL;
if(!(ret = sts_value_create(script, STS_BOOLEAN)))
{
fprintf(stderr, "could not create boolean value\n");
return NULL;
}
ret->boolean = boolean;
return ret;
}
void sts_array_resize(sts_script_t *script, sts_value_t *value, unsigned int size)
{
STS_ARRAY_RESIZE(value, size);
}
void sts_array_append_insert(sts_script_t *script, sts_value_t *value_ptr, sts_value_t *value_insert, unsigned int position)
{
STS_ARRAY_APPEND_INSERT(value_ptr, value_insert, position);
}
int sts_array_remove(sts_script_t *script, sts_value_t *value_ptr, unsigned int position)
{
STS_ARRAY_REMOVE(script, value_ptr, position, {STS_ERROR_SIMPLE("could not remove value in array"); return 0;});
return 1;
}
unsigned int sts_hash(void *data, unsigned int size)
{
unsigned int ret = 0;
STS_HASH(ret, data, size);
return ret;
}
void sts_string_assemble(sts_script_t *script, char **dest, unsigned int current_size, char *middle_str, unsigned int middle_size, char *end_str, unsigned int end_size)
{
STS_STRING_ASSEMBLE(*dest, current_size, middle_str, middle_size, end_str, end_size);
}
int sts_destroy_map(sts_script_t *script, sts_map_row_t *row)
{
if(!row) return 1;
STS_DESTROY_MAP(row, {return 0;});
return 1;
}
sts_scope_t *sts_scope_push(sts_script_t *script, sts_scope_t *scope)
{
sts_scope_t *ret = scope;
STS_SCOPE_PUSH(ret, {return NULL;});
return ret;
}
sts_scope_t *sts_scope_pop(sts_script_t *script, sts_scope_t *scope)
{
sts_scope_t *ret = scope;
STS_SCOPE_POP(ret, {return NULL;});
return ret;
}
sts_map_row_t *sts_scope_search(sts_script_t *script, sts_scope_t *scope, void *key, unsigned int key_size)
{
sts_map_row_t *ret = NULL;
STS_SCOPE_SEARCH(scope, key, key_size, ret, {});
return ret;
}
int sts_hashmap_set(sts_script_t *script, sts_value_t *hashmap, char *key, unsigned int key_length, sts_value_t *value)
{
/*
# hashmap structure:
# [array
# [array *hash* *key string* *value*]
# [array *hash* *key string* *value*]
# ...
# ]
*/
sts_value_t *row_val = NULL, *hash_val = NULL, *key_val = NULL;
unsigned int hash = STS_FNV_OFFSET;
if(!hashmap || hashmap->type != STS_ARRAY)
{
STS_ERROR_SIMPLE("hashmap value is null or not array");
return 1;
}
if(!key)
{
STS_ERROR_SIMPLE("key is null");
return 1;
}
if(!value)
{
STS_ERROR_SIMPLE("value value is null");
return 1;
}
/* create the parts */
STS_HASH(hash, key, key_length);
if(!(hash_val = sts_value_from_number(script, (double)hash)))
{
STS_ERROR_SIMPLE("could not create hash value from number");
return 1;
}
if(!(key_val = sts_value_from_nstring(script, key, key_length)))
{
STS_ERROR_SIMPLE("could not create string value from key str");
return 1;
}
/* create the row */
if(!(row_val = sts_value_create(script, STS_ARRAY)))
{
STS_ERROR_SIMPLE("could not create row for hashmap");
return 1;
}
sts_array_append_insert(script, row_val, hash_val, 0);
sts_array_append_insert(script, row_val, key_val, 1);
sts_array_append_insert(script, row_val, value, 2);
/* add the row */
sts_array_append_insert(script, hashmap, row_val, hashmap->array.length);
return 0;
}
int sts_hashmap_del(sts_script_t *script, sts_value_t *hashmap, char *key, unsigned int key_length)
{
/*
# hashmap structure:
# [array
# [array *hash* *key string* *value*]
# [array *hash* *key string* *value*]
# ...
# ]
*/
sts_value_t *row = NULL;
unsigned int i, hash = STS_FNV_OFFSET;
double converted_hash;
STS_HASH(hash, key, key_length);
converted_hash = (double)hash;
if(!hashmap || hashmap->type != STS_ARRAY)
{
STS_ERROR_SIMPLE("hashmap value is null or not array");
return 1;
}
if(!key)
{
STS_ERROR_SIMPLE("key is null");
return 1;
}
for(i = 0; i < hashmap->array.length; ++i)
{
if(hashmap->array.data[i]->type != STS_ARRAY && !hashmap->array.data[i]->array.length)
{
STS_ERROR_SIMPLE("invalid row in hashmap");
return 1;
}
if(hashmap->array.data[i]->array.data[0]->type != STS_NUMBER)
{
STS_ERROR_SIMPLE("invalid row hash index in hashmap");
return 1;
}
if(hashmap->array.data[i]->array.data[0]->type != STS_NUMBER)
{
STS_ERROR_SIMPLE("invalid row hash index in hashmap");
return 1;
}
if(hashmap->array.data[i]->array.data[0]->number == converted_hash)
{
row = hashmap->array.data[i];
/* remove the pointer from the array */
sts_array_remove(script, hashmap, i);
/* refdec the row */
if(!sts_value_reference_decrement(script, row))
{
STS_ERROR_SIMPLE("could not refdec row in hashmap");
return 1;
}
}
}
return 0;
}
sts_value_t *sts_hashmap_get(sts_script_t *script, sts_value_t *hashmap, char *key, unsigned int key_length)
{
/*
# hashmap structure:
# [array
# [array *hash* *key string* *value*]
# [array *hash* *key string* *value*]
# ...
# ]
*/
sts_value_t *row = NULL, *ret = NULL;
unsigned int i, hash = STS_FNV_OFFSET;
double converted_hash;
STS_HASH(hash, key, key_length);
converted_hash = (double)hash;
if(!hashmap || hashmap->type != STS_ARRAY)
{
STS_ERROR_SIMPLE("hashmap value is null or not array");
return ret;
}
if(!key)
{
STS_ERROR_SIMPLE("key is null");
return ret;
}
for(i = 0; i < hashmap->array.length; ++i)
{
if(hashmap->array.data[i]->type != STS_ARRAY && !hashmap->array.data[i]->array.length)
{
STS_ERROR_SIMPLE("invalid row in hashmap");
return ret;
}
if(hashmap->array.data[i]->array.data[0]->type != STS_NUMBER)
{
STS_ERROR_SIMPLE("invalid row hash index in hashmap");
return ret;
}
if(hashmap->array.data[i]->array.data[0]->type != STS_NUMBER)
{
STS_ERROR_SIMPLE("invalid row hash index in hashmap");
return ret;
}
if(hashmap->array.data[i]->array.data[0]->number == converted_hash)
{
ret = hashmap->array.data[i];
break;
}
}
return ret;
}
#endif