aboutsummaryrefslogtreecommitdiff
path: root/src/program.c
blob: 030d5aa639b1526b0505d37b6a6d29f3c3681cef (plain)
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
#include "../include/program.h"
#include "../include/bytecode.h"
#include "../include/command.h"
#include "../include/std_gc/list.h"
#ifdef DEBUG
#define GC_DEBUG
#endif
#include <gc.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>

#define cleanup_if_error(expr)                  \
    {                                           \
        if(!(expr)) {                           \
            result = TSL_PROGRAM_RESULT_ERR;    \
            goto cleanup;                       \
        }                                       \
    }

#define return_if_error(expr)                   \
    {                                           \
        if(!(expr))                             \
            return TSL_PROGRAM_RESULT_ERR;      \
    }

void tsl_program_init(TslProgram *self) {
    GC_INIT(); /*  TODO: Remove this */
    tsl_buffer_init(&self->function_bytecode_list);
}

void tsl_program_deinit(TslProgram *self) {
    TslBytecode *bytecode_writer = tsl_buffer_begin(&self->function_bytecode_list);
    TslBytecode *bytecode_writer_end = tsl_buffer_end(&self->function_bytecode_list);
    while(bytecode_writer != bytecode_writer_end) {
        tsl_bytecode_deinit(bytecode_writer);
        ++bytecode_writer;
    }
    tsl_buffer_deinit(&self->function_bytecode_list);
    GC_deinit(); /* TODO: Remove this */
}

static int program_output_temp(char *data, int size, void *userdata) {
    (void)size;
    (void)userdata;
    fputs(data, stdout);
    return 1;
}

#if 0
static void tsl_string_to_ref(const TslString *self, TslStringView *result) {
    result->data = self->data;
    result->size = self->size;
}

static TslProgramResult tsl_string_ref_get_item_at_index(const TslStringView *self, const TslValue *key, TslValue *result) {
    TslValue *list_item;
    if(key->type != TSL_TYPE_NUMBER) {
        /* TODO: Print type as string */
        fprintf(stderr, "Error: Unable to index string %s using a variable of type %d. The index has to be a number\n", "TODO", key->type);
        return TSL_PROGRAM_RESULT_ERR;
    }

    list_item = tsl_string_ref_get(self, key->data.number);
    if(list_item) {
        *result = *list_item;
    } else {
        result->type = TSL_TYPE_NULL;
    }

    return TSL_PROGRAM_RESULT_OK;
}
#endif

static TslProgramResult tsl_value_create_from_stack_value(TslProgram *self, TslValue *dst) {
    TslStackValue *src = tsl_buffer_pop(&self->stack_values, sizeof(TslStackValue));
    tsl_value_clear(dst);
    switch(src->type) {
        case TSL_STACK_VALUE_TYPE_NUMBER: {
            dst->data.number = src->data.number;
            dst->type = TSL_TYPE_NUMBER;
            break;
        }
        case TSL_STACK_VALUE_TYPE_BOOL: {
            dst->data.boolean = src->data.boolean;
            dst->type = TSL_TYPE_BOOL;
            break;
        }
        case TSL_STACK_VALUE_TYPE_STRING: {
            #if 0
            TslString *str = GC_MALLOC_ATOMIC(sizeof(TslString));
            str->data = GC_MALLOC_ATOMIC(src->data.str.size + 1);
            if(!str) {
                GC_FREE(str);
                GC_FREE(str->data);
                fprintf(stderr, "Error: out of memory\n");
                return TSL_PROGRAM_RESULT_ERR;
            }
            if(!str->data) {
                GC_FREE(str);
                GC_FREE(str->data);
                fprintf(stderr, "Error: out of memory\n");
                return TSL_PROGRAM_RESULT_ERR;
            }

            memcpy(str->data, src->data.str.data, src->data.str.size);
            str->data[src->data.str.size] = '\0';
            str->size = src->data.str.size;

            dst->data.string = str;
            dst->type = TSL_TYPE_STRING;
            #endif
            dst->data.string_ref = src->data.str;
            dst->type = TSL_TYPE_STRING_REF;
            break;
        }
        case TSL_STACK_VALUE_TYPE_FUNCTION: {
            dst->data.function = src->data.integer;
            dst->type = TSL_TYPE_FUNCTION;
            break;
        }
        case TSL_STACK_VALUE_TYPE_VARIABLE_NAME: {
            TslValue *var;
            TslValue map_key;
            map_key.type = TSL_TYPE_STRING_REF;
            map_key.data.string_ref = src->data.str;

            var = tsl_hash_map_get(self->variables, &map_key);
            if(!var) {
                fprintf(stderr, "Error: Trying to access a non-existing variable \"%.*s\"\n", (int)src->data.str.size, src->data.str.data);
                return TSL_PROGRAM_RESULT_ERR;
            }

            *dst = *var;
            break;
        }
        case TSL_STACK_VALUE_TYPE_VARIABLE: {
            *dst = src->data.variable;
            break;
        }
        case TSL_STACK_VALUE_TYPE_LIST: {
            TslValue *list_item;
            int i = src->data.integer - 1;
            dst->data.list = GC_MALLOC(sizeof(TslList));
            return_if_error(dst->data.list);

            tsl_list_init(dst->data.list);
            return_if_error(tsl_list_set_capacity_hint(dst->data.list, sizeof(TslValue) * src->data.integer));
            dst->data.list->size = sizeof(TslValue) * src->data.integer;
            list_item = tsl_list_end(dst->data.list) - sizeof(TslValue);

            while(i >= 0) {
                return_if_error(tsl_value_create_from_stack_value(self, list_item));
                --list_item;
                --i;
            }

            dst->type = TSL_TYPE_LIST;
            break;
        }
        case TSL_STACK_VALUE_TYPE_MAP: {
            int i = src->data.integer - 1;

            dst->data.map = GC_MALLOC(sizeof(TslHashMap));
            return_if_error(dst->data.map);
            tsl_hash_map_init(dst->data.map);

            assert(src->data.integer % 2 == 0);
            while(i >= 0) {
                TslValue map_key;
                TslValue map_value;
                return_if_error(tsl_value_create_from_stack_value(self, &map_value));
                return_if_error(tsl_value_create_from_stack_value(self, &map_key));
                return_if_error(tsl_hash_map_insert(dst->data.map, &map_key, &map_value));
                i -= 2;
            }

            dst->type = TSL_TYPE_MAP;
            break;
        }
        case TSL_STACK_VALUE_TYPE_INDEX: {
            TslValue var;
            TslValue key;
            return_if_error(tsl_value_create_from_stack_value(self, &key));
            return_if_error(tsl_value_create_from_stack_value(self, &var));

            if(var.type == TSL_TYPE_LIST) {
                TslValue *list_item;
                if(key.type != TSL_TYPE_NUMBER) {
                    /* TODO: Print type as string */
                    fprintf(stderr, "Error: Unable to index list %s using a variable of type %d. The index has to be a number\n", "TODO", key.type);
                    return TSL_PROGRAM_RESULT_ERR;
                }

                list_item = tsl_list_get(var.data.list, key.data.number);
                if(list_item) {
                    *dst = *list_item;
                } else {
                    dst->type = TSL_TYPE_NULL;
                }
                return TSL_PROGRAM_RESULT_OK;
            } else if(var.type == TSL_TYPE_MAP) {
                TslValue *map_value = tsl_hash_map_get(var.data.map, &key);
                if(map_value) {
                    *dst = *map_value;
                } else {
                    dst->type = TSL_TYPE_NULL;
                }
                return TSL_PROGRAM_RESULT_OK;
            /*} else if(var.type == TSL_TYPE_STRING) {
                TslStringView str_ref;
                tsl_string_to_ref(var.data.string, &str_ref);
                return tsl_string_ref_get_item_at_index(&str_ref, &key, dst);
            } else if(var.type == TSL_TYPE_STRING_REF) {
                return tsl_string_ref_get_item_at_index(&var.data.string_ref, &key, dst);
                */
            } else {
                /* TODO: Print type as string */
                fprintf(stderr, "Error: Unable to index data of type %d. Expected list or map\n", var.type);
                return TSL_PROGRAM_RESULT_ERR;
            }
        }
        case TSL_STACK_VALUE_TYPE_NULL: {
            dst->type = TSL_TYPE_NULL;
            break;
        }
    }
    return TSL_PROGRAM_RESULT_OK;
}

static TslProgramResult tsl_program_perform_operation(TslProgram *self, TslNumber(*operation_func)(TslNumber lhs, TslNumber rhs)) {
    TslValue lhs_value;
    TslValue rhs_value;
    TslStackValue stack_value;

    return_if_error(tsl_value_create_from_stack_value(self, &rhs_value));
    return_if_error(tsl_value_create_from_stack_value(self, &lhs_value));

    if(lhs_value.type != TSL_TYPE_NUMBER) {
        fprintf(stderr, "Error: Unable to perform operation '+' between a TODO and a TODO\n");
        return TSL_PROGRAM_RESULT_ERR;
    }
    if(rhs_value.type != TSL_TYPE_NUMBER) {
        fprintf(stderr, "Error: Unable to perform operation '+' between a TODO and a TODO\n");
        return TSL_PROGRAM_RESULT_ERR;
    }

    stack_value.data.number = operation_func(lhs_value.data.number, rhs_value.data.number);
    stack_value.type = TSL_STACK_VALUE_TYPE_NUMBER;
    return tsl_buffer_append(&self->stack_values, &stack_value, sizeof(stack_value));
}

static TslNumber number_operation_sub(TslNumber lhs, TslNumber rhs) {
    return lhs + rhs;
}

static TslNumber number_operation_mul(TslNumber lhs, TslNumber rhs) {
    return lhs * rhs;
}

static TslNumber number_operation_div(TslNumber lhs, TslNumber rhs) {
    return lhs / rhs;
}

static TslProgramResult tsl_program_run_function(TslProgram *self, int function_index) {
    TslProgramResult result = TSL_PROGRAM_RESULT_OK;
    TslBytecode *function_bytecode = (TslBytecode*)tsl_buffer_begin(&self->function_bytecode_list) + function_index;
    char *instruction = tsl_buffer_begin(&function_bytecode->buffer);
    char *instruction_end = tsl_buffer_end(&function_bytecode->buffer);
    size_t prev_stack_value_size = self->stack_values.size;

    /* TODO: Verify if these don't cause unaligned memory access on non-x86 platforms */
    while(instruction != instruction_end) {
        TslOpcode opcode = *(TslOpcode*)instruction;
        TslInstructionType1 *instruction_type1 = (TslInstructionType1*)instruction;
        TslInstructionType2 *instruction_type2 = (TslInstructionType2*)instruction;
        TslInstructionType3 *instruction_type3 = (TslInstructionType3*)instruction;
        TslInstructionType4 *instruction_type4 = (TslInstructionType4*)instruction;
        switch(opcode) {
            case TSL_OPCODE_LOADN: {
                TslStackValue stack_value;
                stack_value.data.number = instruction_type2->value;
                stack_value.type = TSL_STACK_VALUE_TYPE_NUMBER;
                cleanup_if_error(tsl_buffer_append(&self->stack_values, &stack_value, sizeof(stack_value)));
                /*printf("loadn %f\n", instruction_type2->value);*/
                instruction += sizeof(TslInstructionType2);
                break;
            }
            case TSL_OPCODE_LOADB: {
                TslStackValue stack_value;
                stack_value.data.boolean = instruction_type3->value;
                stack_value.type = TSL_STACK_VALUE_TYPE_BOOL;
                cleanup_if_error(tsl_buffer_append(&self->stack_values, &stack_value, sizeof(stack_value)));
                /*printf("loadb %s\n", instruction_type3->value ? "true" : "false");*/
                instruction += sizeof(TslInstructionType3);
                break;
            }
            case TSL_OPCODE_LOADS: {
                TslStackValue stack_value;
                stack_value.data.str = instruction_type4->value;
                stack_value.type = TSL_STACK_VALUE_TYPE_STRING;
                cleanup_if_error(tsl_buffer_append(&self->stack_values, &stack_value, sizeof(stack_value)));
                /*printf("loads \"%.*s\"\n", (int)instruction_type4->value.size, instruction_type4->value.data);*/
                instruction += sizeof(TslInstructionType4);
                break;
            }
            case TSL_OPCODE_LOADF: {
                TslStackValue stack_value;
                stack_value.data.integer = instruction_type1->value;
                stack_value.type = TSL_STACK_VALUE_TYPE_FUNCTION;
                cleanup_if_error(tsl_buffer_append(&self->stack_values, &stack_value, sizeof(stack_value)));
                /*printf("loadf %d\n", instruction_type1->value);*/
                instruction += sizeof(TslInstructionType1);
                break;
            }
            case TSL_OPCODE_LOADV: {
                TslStackValue stack_value;
                stack_value.data.str = instruction_type4->value;
                stack_value.type = TSL_STACK_VALUE_TYPE_VARIABLE_NAME;
                cleanup_if_error(tsl_buffer_append(&self->stack_values, &stack_value, sizeof(stack_value)));
                /*printf("loadv \"%.*s\"\n", (int)instruction_type4->value.size, instruction_type4->value.data);*/
                instruction += sizeof(TslInstructionType4);
                break;
            }
            case TSL_OPCODE_LOADNULL: {
                TslStackValue stack_value;
                stack_value.type = TSL_STACK_VALUE_TYPE_NULL;
                cleanup_if_error(tsl_buffer_append(&self->stack_values, &stack_value, sizeof(stack_value)));
                /*printf("loadnull\n");*/
                instruction += sizeof(TslInstructionType5);
                break;
            }
            case TSL_OPCODE_SETV: {
                TslValue map_key;
                TslValue *map_value;
                map_key.type = TSL_TYPE_STRING_REF;
                map_key.data.string_ref = instruction_type4->value;
                
                map_value = tsl_hash_map_get_or_create(self->variables, &map_key);
                cleanup_if_error(map_value);

                cleanup_if_error(tsl_value_create_from_stack_value(self, map_value));
                /*printf("setv \"%.*s\"\n", (int)instruction_type4->value.size, instruction_type4->value.data);*/
                instruction += sizeof(TslInstructionType4);
                break;
            }
            case TSL_OPCODE_LIST: {
                TslStackValue stack_value;
                stack_value.data.integer = instruction_type1->value;
                stack_value.type = TSL_STACK_VALUE_TYPE_LIST;
                cleanup_if_error(tsl_buffer_append(&self->stack_values, &stack_value, sizeof(stack_value)));
                /*printf("list %d\n", instruction_type1->value);*/
                instruction += sizeof(TslInstructionType1);
                break;
            }
            case TSL_OPCODE_MAP: {
                TslStackValue stack_value;
                stack_value.data.integer = instruction_type1->value;
                stack_value.type = TSL_STACK_VALUE_TYPE_MAP;
                cleanup_if_error(tsl_buffer_append(&self->stack_values, &stack_value, sizeof(stack_value)));
                /*printf("map %d\n", instruction_type1->value);*/
                instruction += sizeof(TslInstructionType1);
                break;
            }
            case TSL_OPCODE_MINDEX: {
                TslStackValue stack_value;
                stack_value.type = TSL_STACK_VALUE_TYPE_INDEX;
                cleanup_if_error(tsl_buffer_append(&self->stack_values, &stack_value, sizeof(stack_value)));
                /*printf("mindex\n");*/
                instruction += sizeof(TslInstructionType5);
                break;
            }
            case TSL_OPCODE_CALLF: {
                size_t args_size = 1 + instruction_type1->value;
                TslStackValue *args;
                assert(self->stack_values.size >= args_size);

                {
                    /* Resolve args (which may have more than one stack value each) and then readd them onto the stack */

                    TslValue resolved_args[255];
                    size_t resolved_arg_index = 0;
                    TslStackValue *arg = (TslStackValue*)tsl_buffer_end(&self->stack_values) - 1;
                    TslStackValue *args_begin = (TslStackValue*)tsl_buffer_end(&self->stack_values) - args_size - 1;
                    /* TODO: Support more than 255 args */
                    assert(self->stack_values.size / sizeof(TslStackValue) <= 255);
                    while(arg != args_begin) {
                        cleanup_if_error(tsl_value_create_from_stack_value(self, &resolved_args[resolved_arg_index]));
                        ++resolved_arg_index;
                        --arg;
                    }

                    {
                        size_t i = 0;
                        for(; i < resolved_arg_index; ++i) {
                            TslStackValue stack_value;
                            stack_value.data.variable = resolved_args[i];
                            stack_value.type = TSL_STACK_VALUE_TYPE_VARIABLE;
                            tsl_buffer_append(&self->stack_values, &stack_value, sizeof(stack_value));
                        }
                    }
                }


                args = (TslStackValue*)tsl_buffer_end(&self->stack_values) - args_size;
                assert(args[0].type == TSL_STACK_VALUE_TYPE_VARIABLE);
                if(args[0].data.variable.type != TSL_TYPE_FUNCTION) {
                    fprintf(stderr, "Error: Unable to call a non-function type\n");
                    result = TSL_PROGRAM_RESULT_ERR;
                    goto cleanup;
                }

                cleanup_if_error(tsl_program_run_function(self, args[0].data.variable.data.function));
                tsl_buffer_pop(&self->stack_values, sizeof(TslStackValue) * args_size);

                /* TODO: Implement this */
                {
                    /* TODO: This is only temporary. This should be replaced with push the result of the function call onto the stack */
                    TslStackValue stack_value;
                    stack_value.type = TSL_STACK_VALUE_TYPE_NULL;
                    cleanup_if_error(tsl_buffer_append(&self->stack_values, &stack_value, sizeof(stack_value)));
                }
                /*printf("callf %d\n", instruction_type1->value);*/
                instruction += sizeof(TslInstructionType1);
                break;
            }
            case TSL_OPCODE_ADD: {
                TslValue lhs_value;
                TslValue rhs_value;
                TslStackValue stack_value;

                cleanup_if_error(tsl_value_create_from_stack_value(self, &rhs_value));
                cleanup_if_error(tsl_value_create_from_stack_value(self, &lhs_value));
                cleanup_if_error(tsl_value_add(&lhs_value, &rhs_value, &stack_value.data.variable));

                stack_value.type = TSL_STACK_VALUE_TYPE_VARIABLE;
                cleanup_if_error(tsl_buffer_append(&self->stack_values, &stack_value, sizeof(stack_value)));
                /*printf("add\n");*/
                instruction += sizeof(TslInstructionType5);
                break;
            }
            case TSL_OPCODE_SUB: {
                cleanup_if_error(tsl_program_perform_operation(self, number_operation_sub));
                /*printf("sub\n");*/
                instruction += sizeof(TslInstructionType5);
                break;
            }
            case TSL_OPCODE_MUL: {
                cleanup_if_error(tsl_program_perform_operation(self, number_operation_mul));
                /*printf("mul\n");*/
                instruction += sizeof(TslInstructionType5);
                break;
            }
            case TSL_OPCODE_DIV: {
                cleanup_if_error(tsl_program_perform_operation(self, number_operation_div));
                /*printf("div\n");*/
                instruction += sizeof(TslInstructionType5);
                break;
            }
            case TSL_OPCODE_CALLC: {
                char *command_args[255];
                size_t command_arg_sizes[255];
                char temp_null_save[255];
                int arg_index = 254;
                

                {
                    /* Resolve args (which may have more than one stack value each) and then readd them onto the stack */
                    int args_size = instruction_type1->value;
                    assert(args_size > 0);
                    /* TODO: Support more than 255 args */
                    assert(self->stack_values.size / sizeof(TslStackValue) <= 255);
                    for(; args_size > 0; --args_size) {
                        TslValue resolved_arg;
                        cleanup_if_error(tsl_value_create_from_stack_value(self, &resolved_arg));

                        switch(resolved_arg.type) {
                            case TSL_TYPE_STRING: {
                                --arg_index;
                                command_args[arg_index] = resolved_arg.data.string->data;
                                command_arg_sizes[arg_index] = resolved_arg.data.string->size;
                                break;
                            }
                            case TSL_TYPE_STRING_REF: {
                                --arg_index;
                                temp_null_save[arg_index] = resolved_arg.data.string_ref.data[resolved_arg.data.string_ref.size];
                                resolved_arg.data.string_ref.data[resolved_arg.data.string_ref.size] = '\0';
                                command_args[arg_index] = resolved_arg.data.string_ref.data;
                                command_arg_sizes[arg_index] = resolved_arg.data.string_ref.size;
                                break;
                            }
                            default:
                                fprintf(stderr, "Error: Unable to convert %s to a string in a command\n", "TODO");
                                result = TSL_PROGRAM_RESULT_ERR;
                                goto cleanup;
                        }
                    }
                    command_args[254] = NULL;
                }

                tsl_command_exec(command_args + arg_index, program_output_temp, NULL);

                /* Restore null terminate character switched. TODO: Only do this once at program launch, when tsl owns the source code buffer */
                for(; arg_index < 254; ++arg_index) {
                    command_args[arg_index][command_arg_sizes[arg_index]] = temp_null_save[arg_index];
                }

                {
                    /* TODO: This is only temporary. This should be replaced with push the result of the command onto the stack */
                    TslStackValue stack_value;
                    stack_value.type = TSL_STACK_VALUE_TYPE_NULL;
                    cleanup_if_error(tsl_buffer_append(&self->stack_values, &stack_value, sizeof(stack_value)));
                }
                
                /*printf("callc %d\n", instruction_type1->value);*/
                instruction += sizeof(TslInstructionType1);
                break;
            }
        }
    }

    cleanup:
    /*
        This a bug in the compiler. The compiler pop'ed more values that pushed to the stack.
        It's fine if there were values that were pushed to the stack but not pop'ed. This happens for example if there is a function call
        and the result is not assigned to a variable (in other words, the result is ignored).
        This will automatically be cleaned up here and in loops it will be cleaned up at the end of the loop scope.
    */
    assert(self->stack_values.size >= prev_stack_value_size);
    self->stack_values.size = prev_stack_value_size;
    return result;
}

TslProgramResult tsl_program_run(TslProgram *self) {
    TslProgramResult result = TSL_PROGRAM_RESULT_OK;

    self->variables = GC_MALLOC_UNCOLLECTABLE(sizeof(TslHashMap));
    if(!self->variables) {
        fprintf(stderr, "Error: Failed to allocate root object\n");
        return TSL_PROGRAM_RESULT_ERR;
    }
    tsl_hash_map_init(self->variables);
    tsl_buffer_init(&self->stack_values);

    printf("###########################\n");
    result = tsl_program_run_function(self, 0);

    tsl_buffer_deinit(&self->stack_values);
    GC_FREE(self->variables); /* Free the root object, resulting in all objects in the program being free'd */
    self->variables = NULL;
    return result;
}