-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.c
More file actions
1351 lines (1088 loc) · 41.5 KB
/
executor.c
File metadata and controls
1351 lines (1088 loc) · 41.5 KB
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "executor.h"
#include "context.h"
#include "parser.h"
#include "debug.h"
void exec_expression(struct ExecutionContext* context);
void exec_call_cleanup(struct ExecutionContext* context, int frame_start_stack_index, int args_stack_size);
#pragma region --- Block ---
void exec_block(struct ExecutionContext* context)
{
char current;
#ifdef TOKEN_DEBUG
debug("Parsing block\n");
#endif
int block_stack_index = context->stack_index;
int block_stack_variables = context->stack_variables;
struct ExecutionContextScope* scope = context_get_scope(context);
while (context->position < context->code_len)
{
int stack_index = context->stack_index;
#ifdef TOKEN_DEBUG
debug("\n\n--- Parsing statement (stack_index: %d) ---\n", stack_index);
#endif
#ifdef TEST_DEBUG
// getchar();
#endif
exec_expression(context);
context_skip_spaces(context);
current = context->code[context->position];
if (current == ';')
{
context->position++;
struct ExecutionContextStackIterator iterator = context_stack_iterate(context);
// Do a cleanup from last statement
while(context->stack_index > scope->min_stack_index + scope->variable_count)
{
context_stack_pop_value(context);
}
}
context_skip_spaces(context);
current = context->code[context->position];
#ifdef TOKEN_DEBUG
debug("--- End parsing statement (stack_index: %d) ---\n", context->stack_index);
#endif
if (current == '}')
{
context->position++;
break;
}
}
// Do a block cleanup
exec_call_cleanup(context, block_stack_index, (scope->min_stack_index + scope->variable_count) - block_stack_index);
// context->stack_index = block_stack_index;
context->stack_variables = block_stack_variables;
#ifdef TOKEN_DEBUG
debug("End parsing block\n");
#endif
}
#pragma endregion --- Block ---
#pragma region --- Literals ---
#pragma region Simple literals
void exec_number(struct ExecutionContext* context)
{
char number[32];
char flags = 0;
int index = 0;
char current = context->code[context->position];
while ((isdigit(current) || current == '.') && context->position < context->code_len)
{
number[index] = current;
if (current == '.')
{
flags |= 0x1;
}
index++;
context->position++;
current = context->code[context->position];
}
if (current == 'f')
{
flags |= 0x2;
context->position++;
current = context->code[context->position];
}
if (current == 'l')
{
flags |= 0x4;
context->position++;
current = context->code[context->position];
}
if (current == 'u')
{
flags |= 0x8;
context->position++;
current = context->code[context->position];
}
number[index] = 0;
uint64_t value;
double double_value;
float float_value;
switch (flags)
{
case 0x0:
value = (int32_t)atoll(number);
break;
case 0x1:
double_value = atof(number);
value = *(uint64_t*)&double_value;
break;
case 0x3:
float_value = (float)atof(number);
value = *(uint64_t*)&float_value;
break;
case 0x4:
value = (int64_t)atoll(number);
break;
case 0x8:
value = (uint32_t)atoll(number);
break;
case 0x12:
value = (uint64_t)atoll(number);
break;
default:
break;
}
#ifdef TOKEN_DEBUG
switch (flags)
{
case 0x1:
case 0x3:
debug("Push number '%s' (flags: %d, value: %f) to stack\n", number, flags, value);
break;
case 0x0:
case 0x4:
case 0x8:
case 0x12:
debug("Push number '%s' (flags: %d, value: %d) to stack\n", number, flags, value);
default:
break;
}
#endif
context_stack_push_value(
context,
(struct ExecutionContextStackValue) { .ptr = &value, .type = NATIVE_TYPE_I32, .size = get_size_of_native_type(NATIVE_TYPE_I32) }
);
}
#pragma endregion Simple literals
#pragma region Function literal
void exec_function(
struct ExecutionContext* context,
struct ExecutionContextTypeInfo return_type
) {
char current = context->code[context->position];
if (current == '(')
{
context->position++;
}
context_skip_spaces(context);
uint64_t code_start = context->position;
#ifdef TOKEN_DEBUG
debug("Function declaration at: %d\n", code_start);
#endif
current = context->code[context->position];
while (current != ')')
{
context->position++;
current = context->code[context->position];
}
context->position++;
context_skip_spaces(context);
current = context->code[context->position];
if (current == '=')
{
context->position++;
}
current = context->code[context->position];
if (current == '>')
{
context->position++;
}
context_skip_spaces(context);
current = context->code[context->position];
if (current == '{')
{
context->position++;
context_skip_spaces(context);
int nested = 1;
while (nested > 0)
{
current = context->code[context->position];
context->position++;
if (current == '{')
{
nested++;
}
else if (current == '}')
{
nested--;
}
}
}
else
{
while (current != ';' && !context_eof(context))
{
current = context->code[context->position];
context->position++;
}
}
context_stack_push_value(
context,
(struct ExecutionContextStackValue) { .ptr = &code_start, .type = NATIVE_TYPE_FUNCTION, .size = get_size_of_native_type(NATIVE_TYPE_FUNCTION) }
);
}
void exec_bound_function(
struct ExecutionContext* context,
struct ExecutionContextTypeInfo return_type
) {
char current = context->code[context->position];
if (current == '[')
{
context->position++;
}
context_skip_spaces(context);
current = context->code[context->position];
if (current == ']')
{
context->position++;
}
context_skip_spaces(context);
exec_function(context, return_type);
}
#pragma endregion Function literal
#pragma region Struct literal
void exec_struct_field(struct ExecutionContext* context, struct ExecutionContextStructDefinition* definition)
{
char type_identifier[MAX_IDENTIFIER_LENGTH];
context_skip_spaces(context);
int type_identifier_length = parse_identifier(context, type_identifier, MAX_IDENTIFIER_LENGTH);
context_skip_spaces(context);
char current = context->code[context->position];
struct ExecutionContextTypeInfo type_info =
context_get_type_from_identifier(context, type_identifier, type_identifier_length, NULL);
if (type_info.complex)
{
context_stack_pop_value(context);
}
char identifier[MAX_IDENTIFIER_LENGTH];
context_skip_spaces(context);
parse_identifier(context, identifier, MAX_IDENTIFIER_LENGTH);
context_skip_spaces(context);
current = context->code[context->position];
struct ExecutionContextStructFieldDefinition field_definition;
strncpy(field_definition.name, identifier, MAX_IDENTIFIER_LENGTH);
field_definition.flags = 0;
if (current == '(')
{
// Method definition
exec_function(context, type_info);
field_definition.type = (struct ExecutionContextTypeInfo) { .native = NATIVE_TYPE_FUNCTION, .complex = NULL };
field_definition.offset = definition->static_size;
definition->static_size += get_size_of_type(field_definition.type);
context_struct_definition_field_list_add(&definition->static_fields, field_definition);
}
else
{
field_definition.type = type_info;
field_definition.offset = definition->size;
definition->size += get_size_of_type(type_info);
context_struct_definition_field_list_add(&definition->fields, field_definition);
}
}
void exec_struct(struct ExecutionContext* context)
{
context_skip_spaces(context);
char current = context->code[context->position];
#ifdef TOKEN_DEBUG
debug("Parsing struct\n");
#endif
if (current != '{')
{
char identifier[MAX_IDENTIFIER_LENGTH];
int identifier_length = parse_identifier(context, identifier, MAX_IDENTIFIER_LENGTH);
}
context_skip_spaces(context);
current = context->code[context->position];
struct ExecutionContextStructDefinition* definition = object_create(sizeof(struct ExecutionContextStructDefinition));
definition->flags = 0;
definition->size = 0;
definition->static_size = 0;
definition->fields.capacity = 8;
definition->fields.count = 0;
definition->static_fields.capacity = 8;
definition->static_fields.count = 0;
if (current != '{')
{
debug("ERR!: Epected '{'\n");
return;
}
context->position++;
current = context->code[context->position];
if (current != '}')
{
int start_stack_pointer = context->stack_index;
// Parse fields
do
{
if (current == ';')
{
context->position++;
context_skip_spaces(context);
current = context->code[context->position];
}
if (current == '}')
{
break;
}
exec_struct_field(context, definition);
context_skip_spaces(context);
current = context->code[context->position];
}
while (current == ';');
definition->static_data = malloc(definition->static_size);
memcpy(definition->static_data, &context->stack[start_stack_pointer], definition->static_size);
context->stack_index = start_stack_pointer;
if (current == '}')
{
context->position++;
}
else
{
debug("ERR!: Syntax error missing ')'\n");
// FIXME: should panic
return;
}
}
else
{
context->position++;
}
uint64_t value = (uint64_t)definition;
context_stack_push_value(
context,
(struct ExecutionContextStackValue) { .ptr = &value, .type = STACK_TYPE_STRUCT, .size = get_size_of_native_type(STACK_TYPE_STRUCT) }
);
#ifdef TOKEN_DEBUG
debug("End parsing struct, fields: %d, static_fields: %d\n", definition->fields.count, definition->static_fields.count);
#endif
}
#pragma endregion Struct literal
#pragma endregion --- Literals ---
#pragma region --- Access ---
#pragma region Variables
void exec_access_variable(struct ExecutionContext* context, struct ExecutionContextVariable* variable)
{
context_variable_push_into_stack(context, variable);
}
void exec_assignment(struct ExecutionContext* context, struct ExecutionContextVariable* variable)
{
struct ExecutionContextStackValue value = context_stack_get_last_value(context);
#ifdef TOKEN_DEBUG
debug("Assign value '[%s] %d' to '%s'\n", get_stack_type_name(value.type), *value.ptr, variable->name);
#endif
context_variable_set_value(context, variable, value);
context_stack_pop_value(context);
}
void exec_variable_declaration(struct ExecutionContext* context, const char* identifier, struct ExecutionContextTypeInfo declaration_type)
{
context_skip_spaces(context);
char current = context->code[context->position];
if (current != '=')
{
debug("ERR!: Expected '=' for '%s' variable declaration.\n", identifier);
}
else
{
context->position++;
}
exec_expression(context);
struct ExecutionContextStackValue value = context_stack_get_last_value(context);
if (value.type == STACK_TYPE_STRUCT_INSTANCE)
{
}
context->stack_index -= value.size;
struct ExecutionContextVariable* variable = context_add_variable(
context,
context_get_scope(context),
identifier,
declaration_type.native,
value.size * 8,
true
);
if (!variable)
{
debug("ERR!: Cannot add local variable '%s'.\n", identifier);
return;
}
context_variable_set_value(context, variable, value);
#ifdef TOKEN_DEBUG
debug("Declared variable '%s' with value '[%s] %d'\n", variable->name, get_stack_type_name(value.type), context->stack[context->stack_index - 1]);
#endif
}
#pragma endregion Variables
#pragma region Struct fields
struct ExecutionContextIdentifierResult exec_field_access(struct ExecutionContext* context)
{
struct ExecutionContextStackValue value = context_stack_get_last_value(context);
struct ExecutionContextStructDefinitionFieldList* fields = NULL;
uint8_t* data;
if (value.type == STACK_TYPE_STRUCT)
{
struct ExecutionContextStructDefinition* definition = *(struct ExecutionContextStructDefinition**)value.ptr;
fields = &definition->static_fields;
data = definition->static_data;
}
else if (value.type == STACK_TYPE_STRUCT_INSTANCE)
{
// TODO: ???
// struct ExecutionContextStructDefinition* definition = *(struct ExecutionContextStructDefinition**)value.ptr;
// fields = &definition->fields;
// data = ((uint8_t*)value.ptr) + sizeof(struct ExecutionContextStructDefinition*);
}
else if (value.type == STACK_TYPE_OBJECT)
{
uint8_t* heap_data = (uint8_t*)value.ptr;
struct ExecutionContextStructDefinition* definition = *(struct ExecutionContextStructDefinition**)heap_data;
fields = &definition->fields;
data = heap_data + sizeof(struct ExecutionContextStructDefinition*);
}
char identifier[MAX_IDENTIFIER_LENGTH];
char current = context->code[context->position];
if (current == '.')
{
context->position++;
}
context_skip_spaces(context);
int identifier_length = parse_identifier(context, identifier, MAX_IDENTIFIER_LENGTH);
struct ExecutionContextStructFieldDefinition* field = NULL;
// TODO: implement getting field form fields list
for (int i = 0; i < fields->count; i++)
{
if (strncmp(fields->data[i].name, identifier, identifier_length) == 0)
{
field = &fields->data[i];
break;
}
}
uint8_t* field_data = data + field->offset;
context_stack_push_value(
context,
(struct ExecutionContextStackValue) { .ptr = (uint64_t*)field_data, .type = field->type.native, .size = get_size_of_type(field->type) }
);
if (field->type.native == STACK_TYPE_STRUCT)
{
return (struct ExecutionContextIdentifierResult)
{
.data_type = EXECUTION_CONTEXT_IDENTIFIER_RESULT_TYPE,
.type_data = field->type
};
}
return (struct ExecutionContextIdentifierResult) { .data_type = EXECUTION_CONTEXT_IDENTIFIER_RESULT_VALUE };
}
#pragma endregion Struct fields
#pragma endregion --- Access ---
#pragma region --- Operators ---
#pragma region Call
int exec_call_args(struct ExecutionContext* context, int start_stack_index)
{
// Parsing an expression here, because ',' operator pushes all expressions
// into a stack, which means this will populate arguments for this call
exec_expression(context);
char current = context->code[context->position];
// We should be at ')', if not there is something wrong with syntax
if (current == ')')
{
context->position++;
}
else
{
debug("ERR!: Syntax error missing ')'\n");
// FIXME: should panic
return 0;
}
// Provided args size pushed to the stack can be calculated
int args_stack_size = context->stack_index - start_stack_index;
return args_stack_size;
}
void exec_call_cleanup(struct ExecutionContext* context, int frame_start_stack_index, int args_stack_size)
{
struct ExecutionContextStackIterator iterator = context_stack_iterate(context);
int return_count = 0;
int frame_args_end_stack_index = frame_start_stack_index + args_stack_size;
while(iterator.stack_index != frame_args_end_stack_index)
{
// bypass returned values
context_stack_iterator_next(context, &iterator);
return_count++;
}
int return_size = context->stack_index - iterator.stack_index;
#ifdef TOKEN_DEBUG
debug("Returned values %d, overall size: %d byte(s)\n", return_count, return_size * 8);
#endif
while(iterator.stack_index != frame_start_stack_index)
{
// destruct all args values
context_stack_iterator_next(context, &iterator);
context_stack_unset_value_at_index(context, iterator.stack_index);
}
memmove(&context->stack[frame_start_stack_index], &context->stack[frame_args_end_stack_index], return_size);
context->stack_index = frame_start_stack_index + return_size;
#ifdef TOKEN_DEBUG
if (return_count)
{
struct ExecutionContextStackValue value = context_stack_get_last_value(context);
debug("Last returned value: %d (type: %s, size: %d)\n", *value.ptr, get_stack_type_name(value.type), value.size);
}
#endif
}
void exec_call_native_function(struct ExecutionContextStackValue stack_value, struct ExecutionContext* context)
{
#ifdef TOKEN_DEBUG
debug("Calling function at address: %p\n", *stack_value.ptr);
#endif
if (stack_value.type != NATIVE_TYPE_NATIVE_FUNCTION)
{
debug("ERR!: Value is not a function\n");
return;
}
char current = context->code[context->position];
void(*func)(struct ExecutionContext*) = *(void**)stack_value.ptr;
int frame_start_stack_index = context->stack_index;
#ifdef TOKEN_DEBUG
debug("Prepare to call %p\n", func);
#endif
int args_count = exec_call_args(context, frame_start_stack_index);
#ifdef TOKEN_DEBUG
debug("Calling %p with %d arguments\n", func, args_count);
#endif
func(context);
exec_call_cleanup(context, frame_start_stack_index, args_count);
}
void exec_call_function(struct ExecutionContextStackValue stack_value, struct ExecutionContext* context)
{
#ifdef TOKEN_DEBUG
debug("Calling function at position: %d\n", *stack_value.ptr);
#endif
if (stack_value.type != NATIVE_TYPE_FUNCTION)
{
debug("ERR!: Value is not a function\n");
return;
}
// Stack value contains start text position for the function, currently
// it should point to place directly after '(' character
int func_position = *stack_value.ptr;
// Save start stack index for later, we need to restore it after
// function call is done
int frame_start_stack_index = context->stack_index;
#ifdef TOKEN_DEBUG
debug("Prepare to call %p\n", func_position);
#endif
// Create new scope to which we will push args values
struct ExecutionContextScope* scope = context_push_scope(context);
// Read argument values and push them to the stack from call expression
int args_stack_size = exec_call_args(context, frame_start_stack_index);
#ifdef TOKEN_DEBUG
debug("Parsed call args, jumping to the function code %p\n", func_position);
#endif
int return_position = context->position;
// Jump to function code
context->position = func_position;
context_skip_spaces(context);
char current = context->code[context->position];
if (current != ')')
{
// Args on the stack needs to be assinged to scope variables
int index = 0;
char type_identifier[MAX_IDENTIFIER_LENGTH];
char identifier[MAX_IDENTIFIER_LENGTH];
while (index < args_stack_size)
{
// Read the type of the variable
context_skip_spaces(context);
int type_identifier_length = parse_identifier(context, type_identifier, MAX_IDENTIFIER_LENGTH);
context_skip_spaces(context);
struct ExecutionContextTypeInfo type_info = context_get_type_from_identifier(context, type_identifier, type_identifier_length, NULL);
if (type_info.complex)
{
context_stack_pop_value(context);
}
// Read the name of the variable
context_skip_spaces(context);
parse_identifier(context, identifier, MAX_IDENTIFIER_LENGTH);
context_skip_spaces(context);
int current_stack_index = context->stack_index;
// Move stack back to frame start, so we can assign pushed values to variables
context->stack_index = frame_start_stack_index + index;
// Get pushed value at current index which will be assigned to variable
struct ExecutionContextStackValue arg_stack_value = context_stack_get_value_at_index(context, frame_start_stack_index + index);
index += arg_stack_value.size;
// Override values on stack so we create new variables without extra value copy,
// this moves stack pointer automatically
struct ExecutionContextVariable* variable = context_add_variable(
context,
scope,
identifier,
type_info.native,
arg_stack_value.size,
true
);
context->stack_index = current_stack_index;
current = context->code[context->position];
if (current != ',')
{
break;
}
context->position++;
}
if (current == ')')
{
context->position++;
}
else
{
debug("ERR!: Syntax error missing ')'\n");
// FIXME: should panic
return;
}
}
else
{
context->position++;
}
context_skip_spaces(context);
current = context->code[context->position];
if (current == '=')
{
context->position++;
}
current = context->code[context->position];
if (current == '>')
{
context->position++;
}
exec_expression(context);
context->position = return_position;
exec_call_cleanup(context, frame_start_stack_index, args_stack_size);
context_pop_scope(context);
}
void exec_call(struct ExecutionContext* context)
{
struct ExecutionContextStackValue stack_value = context_stack_get_last_value(context);
if (stack_value.type == NATIVE_TYPE_NATIVE_FUNCTION)
{
exec_call_native_function(stack_value, context);
}
else if (stack_value.type == NATIVE_TYPE_FUNCTION)
{
exec_call_function(stack_value, context);
}
else
{
debug("ERR!: Value is not a function\n");
}
}
#pragma endregion Call
#pragma endregion --- Operators ---
struct ExecutionContextIdentifierResult exec_identifier(struct ExecutionContext* context, char* identifier, int max_len)
{
int identifier_length = parse_identifier(context, identifier, max_len);
// Check if identifier is a keyword
if (identifier_length == 6 && strncmp(identifier, "struct", identifier_length) == 0)
{
exec_struct(context);
return (struct ExecutionContextIdentifierResult) { .data_type = EXECUTION_CONTEXT_IDENTIFIER_RESULT_HANDLED };
}
// If not a keyword the try to get type from it
// context_get_type_from_identifier searcher for variable and we can remember the search
// result to not search again
struct ExecutionContextVariable* variable = NULL;
struct ExecutionContextTypeInfo type_info =
context_get_type_from_identifier(context, identifier, identifier_length, &variable);
struct ExecutionContextIdentifierResult result;
if (type_info.native != 255)
{
result.data_type = EXECUTION_CONTEXT_IDENTIFIER_RESULT_TYPE;
result.type_data = type_info;
return result;
}
// When identifier was not a type then we should look for a variable
// and push it to the stack
if (!variable)
{
variable = context_lookup_variable(context, identifier);
}
if (!variable)
{
debug("ERR!: Variable %s is not defined in current scope.\n", identifier);
result.data_type = EXECUTION_CONTEXT_IDENTIFIER_RESULT_ERROR;
return result;
}
exec_access_variable(context, variable);
result.data_type = EXECUTION_CONTEXT_IDENTIFIER_RESULT_VARIABLE;
result.variable_data = variable;
return result;
}
void exec_expression(struct ExecutionContext* context)
{
char current;
char last_expression = 0;
struct ExecutionContextIdentifierResult last_identifier_result;
last_identifier_result.data_type = EXECUTION_CONTEXT_IDENTIFIER_RESULT_HANDLED;
#ifdef TOKEN_DEBUG
debug("Parsing expression\n");
#endif
while (context->position < context->code_len)
{
context_skip_spaces(context);
current = context->code[context->position];
struct ExecutionContextStackValue last_stack_value = context_scope_get_last_value_on_stack_in_scope(context);
#ifdef TOKEN_DEBUG
debug("TOKEN: %c\n", current);
#endif
if (isalpha(current))
{
char identifier[MAX_IDENTIFIER_LENGTH];
if (last_stack_value.type == NATIVE_TYPE_TYPEDEF || last_stack_value.type == STACK_TYPE_STRUCT || last_identifier_result.data_type == EXECUTION_CONTEXT_IDENTIFIER_RESULT_TYPE)
{
last_identifier_result.data_type = EXECUTION_CONTEXT_IDENTIFIER_RESULT_HANDLED;
// Last expression was identifier representing a type, so this is a variable declaration
context_skip_spaces(context);
#ifdef TOKEN_DEBUG
debug("Parsing variable declaration\n");
#endif
int identifier_length = parse_identifier(context, identifier, MAX_IDENTIFIER_LENGTH);
struct ExecutionContextVariable* variable = context_lookup_variable(context, identifier);
if (variable)
{
debug("ERR!: Variable %s is aready defined.\n", identifier);
return;
}
if (last_stack_value.type == NATIVE_TYPE_TYPEDEF || last_stack_value.type == STACK_TYPE_STRUCT)
{
struct ExecutionContextStructDefinition* def = (struct ExecutionContextStructDefinition*)*last_stack_value.ptr;
exec_variable_declaration(context, identifier, (struct ExecutionContextTypeInfo) { .native = last_stack_value.type, .complex = def });
}
else
{
exec_variable_declaration(context, identifier, last_identifier_result.type_data);
}
#ifdef TOKEN_DEBUG
debug("End parsing variable declaration\n");
#endif
continue;
}
last_identifier_result = exec_identifier(context, identifier, MAX_IDENTIFIER_LENGTH);
#ifdef TOKEN_DEBUG
debug("Found identifier: %s\n", identifier);
#endif
if (last_identifier_result.data_type >= EXECUTION_CONTEXT_IDENTIFIER_RESULT_ERROR)
{
debug("ERR!: Identifier %s is not known identifier\n", identifier);
return;
}
continue;
}
else if (isdigit(current) || current == '.')
{
if (current == '.')
{
if (last_stack_value.type == STACK_TYPE_STRUCT || last_stack_value.type == STACK_TYPE_STRUCT_INSTANCE || last_stack_value.type == STACK_TYPE_OBJECT)