-
Notifications
You must be signed in to change notification settings - Fork 1
/
kalos_parse.c
1557 lines (1454 loc) · 64.5 KB
/
kalos_parse.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
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 "_kalos_defines.h"
#include "_kalos_lex.h"
#include "_kalos_string_format.h"
#include "kalos_parse.h"
#define KALOS_MAX_IMPORTS 16
#define KALOS_OP(x, args) #x ,
const char* kalos_op_strings[] = {
#include "_kalos_constants.inc"
"<invalid>",
};
typedef struct kalos_builtin {
const char* name;
int param_count;
kalos_op op;
} kalos_builtin;
// Keep sorted for binary search
#define KALOS_BUILTIN(x, y, z) { .name=#x, .param_count=y, .op=KALOS_OP_##z },
static kalos_builtin kalos_builtins[] = {
#include "_kalos_constants.inc"
{ NULL },
};
#define THROW(msg) { LOG("%d (kalos_parse.c:%d): Throw %s (last token was %s)", parse_state->lex_state.line, __LINE__, msg, kalos_token_strings[parse_state->last_token]); parse_state->failure_message = msg; goto fail; }
#define TRY(x) {x; if (parse_state->failure_message) { LOG("%d: Caught %s", __LINE__, parse_state->failure_message); goto fail; } }
#define TRY_EXIT fail: { }
#define NO_OPERATOR_PRECEDENCE -1
#define loop for (;;)
static const char* ERROR_INVALID_TOKEN = "Invalid token";
static const char* ERROR_UNEXPECTED_TOKEN = "Unexpected token";
static const char* ERROR_BREAK_CONTINUE_WITHOUT_LOOP = "Break or continue but no loop";
static const char* ERROR_INTERNAL_ERROR = "Internal error";
static const char* ERROR_UNKNOWN_VARIABLE = "Unknown variable";
static const char* ERROR_UNKNOWN_HANDLE = "Unknown handler";
static const char* ERROR_UNKNOWN_PROPERTY = "Unknown property";
static const char* ERROR_UNKNOWN_FUNCTION = "Unknown function";
static const char* ERROR_MISSING_HANDLE = "Missing handler statement";
static const char* ERROR_MISSING_RETURN = "Missing return statement";
static const char* ERROR_INVALID_STRING_FORMAT = "Invalid string format";
static const char* ERROR_TOO_MANY_VARS = "Too many vars/consts";
static const char* ERROR_VAR_NAME_TOO_LONG = "Symbol name too long";
static const char* ERROR_INVALID_CONST_EXPRESSION = "Invalid const expression";
static const char* ERROR_INVALID_IMPORT = "Invalid or missing import";
static const char* ERROR_ILLEGAL_IN_THIS_CONTEXT = "Illegal in this context";
static const char* ERROR_DUPLICATE_IMPORT = "Duplicate import";
static const char* ERROR_UNEXPECTED_PARAMETERS = "Unexpected parameters";
static const char* ERROR_UNEXPECTED_EXPORT_TYPE = "Unexpected export type";
static const char* ERROR_NO_LOADER = "No loader configured";
#define KALOS_VAR_SLOT_COUNT 128
#define KALOS_VAR_MAX_LENGTH 32
typedef enum var_type {
VAR_EMPTY = 0,
VAR_MUTABLE,
VAR_CONST,
VAR_FN,
} var_type;
struct fn_state {
kalos_int pc;
kalos_int param_count;
kalos_function_type ret;
};
struct var_state {
char name[KALOS_VAR_MAX_LENGTH];
struct fn_state fn_state; // slot or function index
var_type type;
bool export;
};
struct vars_state {
struct var_state vars[KALOS_VAR_SLOT_COUNT];
int var_index;
};
typedef struct name_resolution_result {
enum {
NAME_RESOLUTION_NOT_FOUND,
NAME_RESOLUTION_BUILTIN,
NAME_RESOLUTION_VAR,
NAME_RESOLUTION_MODULE,
NAME_RESOLUTION_MODULE_EXPORT,
NAME_RESOLUTION_UNIMPORTED_MODULE,
} type;
union {
struct {
struct var_state* var;
uint8_t var_slot;
kalos_op load_op, store_op;
};
struct {
kalos_module* module;
};
struct {
uint8_t export_module_index;
kalos_export* export;
};
kalos_builtin* builtin;
};
} name_resolution_result;
struct pending_op {
kalos_op op;
kalos_int data[3];
const char* sdata[2];
};
struct pending_ops {
struct pending_op load, store;
bool is_const;
bool req_dup;
};
struct parse_state {
kalos_state* state;
kalos_lex_state lex_state;
kalos_parse_options options;
bool dispatch_name;
uint8_t kalos_far* output_script;
uint16_t* script_offset;
uint16_t function_invoke_id;
int output_script_index;
kalos_module_parsed all_modules;
kalos_module* imported_modules[KALOS_MAX_IMPORTS];
uint8_t module_index;
kalos_module* extra_builtins;
struct vars_state globals;
struct vars_state locals;
const char* failure_message;
kalos_token last_token;
int loop_break, loop_continue;
kalos_section_header* last_section_header;
char token[256];
bool const_mode;
kalos_function_type ret;
kalos_token last_statement_type;
};
static void parse_push_format(struct parse_state* parse_state, kalos_op op, kalos_string_format* string_format);
static void parse_push_op(struct parse_state* parse_state, kalos_op op);
static void parse_push_op_1(struct parse_state* parse_state, kalos_op op, kalos_int number);
static void parse_push_string(struct parse_state* parse_state, const char* s);
static void parse_push_token(struct parse_state* parse_state);
static int parse_push_goto_forward(struct parse_state* parse_state, kalos_op op);
static void parse_fixup_offset(struct parse_state* parse_state, int offset, kalos_int pc);
static void write_next_handler_section(struct parse_state* parse_state, kalos_export_address handler_address);
static void parse_assert_token(struct parse_state* parse_state, int token);
static void parse_expression(struct parse_state* parse_state);
static void parse_expression_part(struct parse_state* parse_state);
static int parse_list_of_args(struct parse_state* parse_state, kalos_token open, kalos_token close);
static struct pending_op parse_function_call_local(struct parse_state* parse_state, struct var_state* fn);
static kalos_op parse_function_call_builtin(struct parse_state* parse_state, kalos_builtin* fn);
static struct pending_op parse_function_call_export(struct parse_state* parse_state, kalos_export* fn, uint8_t module_index);
static kalos_function_type parse_map_type(struct parse_state* parse_state, kalos_token token);
static void parse_function_statement(struct parse_state* parse_state, bool export);
static void parse_if_statement(struct parse_state* parse_state);
static void parse_loop_statement(struct parse_state* parse_state, bool iterator, uint8_t iterator_slot);
static void parse_statement_block(struct parse_state* parse_state);
static bool parse_statement(struct parse_state* parse_state);
static int parse_var_allocate(struct parse_state* parse_state, struct vars_state* var_state);
static void parse_var_statement(struct parse_state* parse_state, struct vars_state* var_state, bool export);
static struct pending_ops parse_word_recursively(struct parse_state* parse_state);
static void parse_flush_pending_op(struct parse_state* parse_state, struct pending_ops* pending, bool write, bool reset);
static int lex(struct parse_state* parse_state) {
parse_state->last_token = kalos_lex(&parse_state->lex_state, &parse_state->token[0]);
if (parse_state->last_token == KALOS_TOKEN_ERROR) {
LOG("c=%02x c-1=%02x %02x", *parse_state->lex_state.s, *(parse_state->lex_state.s - 1), *(parse_state->lex_state.s - 2));
THROW(ERROR_INVALID_TOKEN);
}
TRY_EXIT;
return parse_state->last_token;
}
static int lex_peek(struct parse_state* parse_state) {
char buffer[256];
kalos_token token = kalos_lex_peek(&parse_state->lex_state, buffer);
if (token == KALOS_TOKEN_ERROR) {
THROW(ERROR_INVALID_TOKEN);
}
TRY_EXIT;
return token;
}
static int8_t get_operator_precedence(kalos_token token) {
#define KALOS_TOKEN_OP(x, y, z) case KALOS_TOKEN_##y: return x;
switch (token) {
#include "_kalos_constants.inc"
default:
return NO_OPERATOR_PRECEDENCE;
}
}
static kalos_op get_operator_from_token(kalos_token token) {
#define KALOS_TOKEN_OP(x, y, z) case KALOS_TOKEN_##y: return KALOS_OP_##z;
switch (token) {
#include "_kalos_constants.inc"
default:
return KALOS_OP_MAX;
}
}
static bool is_assignment_token(kalos_token token) {
#define KALOS_TOKEN_OP_ASG(x, y) case KALOS_TOKEN_EQ_##x: return true;
switch (token) {
#include "_kalos_constants.inc"
case KALOS_TOKEN_EQ: return true;
default:
return false;
}
}
/********************************************************************
* Script modification
*
* All functions in this section must correctly account for data written
* to the script so that the parser can compile in "sizing mode".
********************************************************************/
static bool parse_allocate_space(struct parse_state* parse_state, kalos_int size) {
if (parse_state->output_script) {
return true;
}
parse_state->output_script_index += size;
return false;
}
static void parse_fixup_offset(struct parse_state* parse_state, int offset, kalos_int pc) {
if (parse_allocate_space(parse_state, 0)) {
parse_state->output_script[offset] = pc & 0xff;
parse_state->output_script[offset + 1] = (pc >> 8) & 0xff;
LOG("FIXUP: @%d %d", offset, pc);
}
}
static void write_next_handler_section(struct parse_state* parse_state, kalos_export_address handler_address) {
if (parse_allocate_space(parse_state, sizeof(*parse_state->last_section_header))) {
if (parse_state->last_section_header) {
parse_state->last_section_header->next_section = parse_state->output_script_index;
}
parse_state->last_section_header = (kalos_section_header*)&parse_state->output_script[parse_state->output_script_index];
memset(parse_state->last_section_header, 0, sizeof(*parse_state->last_section_header));
parse_state->last_section_header->handler_address = handler_address;
parse_state->output_script_index += sizeof(*parse_state->last_section_header);
}
}
static void parse_push_format(struct parse_state* parse_state, kalos_op op, kalos_string_format* string_format) {
if (parse_allocate_space(parse_state, sizeof(kalos_string_format) + 1)) {
parse_state->output_script[parse_state->output_script_index++] = op;
memcpy((void*)&parse_state->output_script[parse_state->output_script_index], (void*)string_format, sizeof(kalos_string_format));
parse_state->output_script_index += sizeof(kalos_string_format);
}
}
static void parse_push_string(struct parse_state* parse_state, const char* s) {
kalos_int len = strlen(s);
if (parse_allocate_space(parse_state, len + 2)) {
parse_state->output_script[parse_state->output_script_index++] = KALOS_OP_PUSH_STRING;
strcpy((char*)&parse_state->output_script[parse_state->output_script_index], s);
parse_state->output_script_index += len + 1;
}
}
static void parse_push_op(struct parse_state* parse_state, kalos_op op) {
if (parse_allocate_space(parse_state, 1)) {
parse_state->output_script[parse_state->output_script_index++] = op;
LOG("OP: %s", kalos_op_strings[op]);
}
}
static void parse_push_op_1(struct parse_state* parse_state, kalos_op op, kalos_int data) {
if (parse_allocate_space(parse_state, 3)) {
parse_state->output_script[parse_state->output_script_index++] = op;
LOG("OP: %s %d", kalos_op_strings[op], data);
parse_state->output_script[parse_state->output_script_index++] = data & 0xff;
parse_state->output_script[parse_state->output_script_index++] = (data >> 8) & 0xff;
}
}
static void parse_push_op_2(struct parse_state* parse_state, kalos_op op, kalos_int data1, kalos_int data2) {
if (parse_allocate_space(parse_state, 5)) {
parse_state->output_script[parse_state->output_script_index++] = op;
LOG("OP: %s %d %d", kalos_op_strings[op], data1, data2);
parse_state->output_script[parse_state->output_script_index++] = data1 & 0xff;
parse_state->output_script[parse_state->output_script_index++] = (data1 >> 8) & 0xff;
parse_state->output_script[parse_state->output_script_index++] = data2 & 0xff;
parse_state->output_script[parse_state->output_script_index++] = (data2 >> 8) & 0xff;
}
}
/********************************************************************
* End script modification
********************************************************************/
static void parse_push_token(struct parse_state* parse_state) {
if (parse_state->last_token == KALOS_TOKEN_INTEGER) {
// This is legal because it'll be a NUL byte for a base-10 zero
if (parse_state->token[1] == 'x' || parse_state->token[1] == 'X') {
TRY(parse_push_op_1(parse_state, KALOS_OP_PUSH_INTEGER, strtol(parse_state->token + 2, NULL, 16)));
} else {
TRY(parse_push_op_1(parse_state, KALOS_OP_PUSH_INTEGER, atoi(parse_state->token)));
}
} else if (parse_state->last_token == KALOS_TOKEN_TRUE) {
TRY(parse_push_op(parse_state, KALOS_OP_PUSH_TRUE));
} else if (parse_state->last_token == KALOS_TOKEN_FALSE) {
TRY(parse_push_op(parse_state, KALOS_OP_PUSH_FALSE));
} else if (parse_state->last_token == KALOS_TOKEN_STRING_LITERAL
|| parse_state->last_token == KALOS_TOKEN_STRING_INTERPOLATION
|| parse_state->last_token == KALOS_TOKEN_WORD) {
TRY(parse_push_string(parse_state, parse_state->token));
} else if (parse_state->last_token == KALOS_TOKEN_STRING_FORMAT_SPEC) {
kalos_string_format format;
if (!kalos_parse_string_format(parse_state->token, &format)) {
THROW(ERROR_INVALID_STRING_FORMAT);
}
TRY(parse_push_format(parse_state, KALOS_OP_FORMAT, &format));
} else {
THROW(ERROR_INTERNAL_ERROR);
}
LOG("TOKEN: %s %d (%s)", kalos_token_strings[parse_state->last_token], parse_state->last_token, parse_state->token);
TRY_EXIT;
}
static void parse_make_list(struct parse_state* parse_state, kalos_int size) {
TRY(parse_push_op_1(parse_state, KALOS_OP_PUSH_INTEGER, size));
TRY(parse_push_op(parse_state, KALOS_OP_MAKE_LIST));
TRY_EXIT;
}
static int parse_push_goto_forward(struct parse_state* parse_state, kalos_op op) {
TRY(parse_push_op_1(parse_state, op, 0));
TRY_EXIT;
return parse_state->output_script_index - 2;
}
// Resolution order: builtins, locals, globals, modules
static struct name_resolution_result resolve_word(struct parse_state* parse_state, kalos_module* context) {
struct name_resolution_result res = { .type = NAME_RESOLUTION_NOT_FOUND, 0 };
const char* token = parse_state->token;
if (context) {
kalos_export* export = kalos_module_find_export(parse_state->all_modules, context, token);
if (export) {
res.type = NAME_RESOLUTION_MODULE_EXPORT;
res.export = export;
res.export_module_index = context->index;
return res;
}
} else {
for (int i = 0; ; i++) {
if (!kalos_builtins[i].name) {
break;
}
if (strcmp(kalos_builtins[i].name, token) == 0) {
LOG("%d: Resolved %s as builtin", parse_state->lex_state.line, token);
res.type = NAME_RESOLUTION_BUILTIN;
res.builtin = &kalos_builtins[i];
return res;
}
}
for (int i = 0; i < parse_state->locals.var_index; i++) {
if (strcmp(parse_state->locals.vars[i].name, token) == 0) {
LOG("%d: Resolved %s as local var", parse_state->lex_state.line, token);
res.type = NAME_RESOLUTION_VAR;
res.var = &parse_state->locals.vars[i];
res.var_slot = i;
res.load_op = KALOS_OP_LOAD_LOCAL;
res.store_op = KALOS_OP_STORE_LOCAL;
return res;
}
}
for (int i = 0; i < parse_state->globals.var_index; i++) {
if (strcmp(parse_state->globals.vars[i].name, token) == 0) {
LOG("%d: Resolved %s as global var", parse_state->lex_state.line, token);
res.type = NAME_RESOLUTION_VAR;
res.var = &parse_state->globals.vars[i];
res.var_slot = i;
res.load_op = KALOS_OP_LOAD_GLOBAL;
res.store_op = KALOS_OP_STORE_GLOBAL;
return res;
}
}
for (int i = 0; i < parse_state->module_index; i++) {
kalos_module* module = parse_state->imported_modules[i];
if (strcmp(kalos_module_get_string(parse_state->all_modules, module->name_index), token) == 0) {
LOG("%d: Resolved %s as module", parse_state->lex_state.line, token);
res.type = NAME_RESOLUTION_MODULE;
res.module = module;
return res;
}
}
// Fall back to extra builtins
if (parse_state->extra_builtins) {
res = resolve_word(parse_state, parse_state->extra_builtins);
if (res.type != NAME_RESOLUTION_NOT_FOUND) {
LOG("%d: Resolved %s as extra builtin", parse_state->lex_state.line, token);
return res;
}
}
kalos_module* module = kalos_module_find_module(parse_state->all_modules, token);
if (module) {
LOG("%d: Resolved %s as unimported module", parse_state->lex_state.line, token);
res.type = NAME_RESOLUTION_UNIMPORTED_MODULE;
res.module = module;
return res;
}
}
return res;
}
static kalos_token wordify(struct parse_state* parse_state) {
switch (parse_state->last_token) {
#define KALOS_TOKEN_WORD(x) case KALOS_TOKEN_##x:
#include "_kalos_constants.inc"
{
kalos_int len = parse_state->lex_state.s - parse_state->lex_state.token_start;
script_memcpy(parse_state->token, (const uint8_t kalos_far*)parse_state->lex_state.token_start, len);
parse_state->token[len] = 0;
parse_state->last_token = KALOS_TOKEN_WORD;
return KALOS_TOKEN_WORD;
}
default:
return parse_state->last_token;
}
}
static void parse_assert_token(struct parse_state* parse_state, int token) {
if (lex(parse_state) != token) {
if (token == KALOS_TOKEN_WORD) {
if (wordify(parse_state) == KALOS_TOKEN_WORD) {
return;
}
}
THROW(ERROR_UNEXPECTED_TOKEN);
}
TRY_EXIT;
}
static int parse_var_allocate(struct parse_state* parse_state, struct vars_state* var_state) {
if (var_state->var_index >= KALOS_VAR_SLOT_COUNT) {
THROW(ERROR_TOO_MANY_VARS);
}
int slot = var_state->var_index++;
if (parse_state->last_token == KALOS_TOKEN_CONST) {
var_state->vars[slot].type = VAR_CONST;
} else if (parse_state->last_token == KALOS_TOKEN_VAR) {
var_state->vars[slot].type = VAR_MUTABLE;
} else if (parse_state->last_token == KALOS_TOKEN_FN) {
var_state->vars[slot].type = VAR_FN;
} else {
THROW(ERROR_UNEXPECTED_TOKEN);
}
TRY(parse_assert_token(parse_state, KALOS_TOKEN_WORD));
if (strlen(parse_state->token) >= KALOS_VAR_MAX_LENGTH) {
THROW(ERROR_VAR_NAME_TOO_LONG);
}
strcpy(var_state->vars[slot].name, parse_state->token);
TRY_EXIT;
return slot;
}
static void parse_var_statement(struct parse_state* parse_state, struct vars_state* var_state, bool export) {
int slot;
TRY(slot = parse_var_allocate(parse_state, var_state));
var_state->vars[slot].export = export;
kalos_token peek;
TRY(peek = lex_peek(parse_state));
if (peek == KALOS_TOKEN_EQ) {
TRY(parse_assert_token(parse_state, KALOS_TOKEN_EQ));
if (var_state->vars[slot].type == VAR_CONST) {
parse_state->const_mode = true;
}
TRY(parse_expression(parse_state));
parse_state->const_mode = false;
TRY(parse_push_op_1(parse_state, KALOS_OP_PUSH_INTEGER, slot));
TRY(parse_push_op(parse_state, var_state == &parse_state->globals ? KALOS_OP_STORE_GLOBAL : KALOS_OP_STORE_LOCAL));
}
TRY(parse_assert_token(parse_state, KALOS_TOKEN_SEMI));
TRY_EXIT;
}
static int parse_list_of_args(struct parse_state* parse_state, kalos_token open, kalos_token close) {
kalos_token peek, param_count = 0;
if (open) {
TRY(parse_assert_token(parse_state, open));
}
loop {
TRY(peek = lex_peek(parse_state));
if (peek == close) {
break;
}
TRY(parse_expression(parse_state));
param_count++;
TRY(peek = lex_peek(parse_state));
if (peek == KALOS_TOKEN_COMMA) {
TRY(lex(parse_state));
} else if (peek != close) {
THROW(ERROR_INVALID_TOKEN);
}
}
TRY(parse_assert_token(parse_state, close));
TRY_EXIT;
return param_count;
}
static struct pending_op parse_function_call_local(struct parse_state* parse_state, struct var_state* fn) {
if (parse_state->const_mode) {
THROW(ERROR_INVALID_CONST_EXPRESSION);
}
int param_count = 0;
TRY(param_count = parse_list_of_args(parse_state, KALOS_TOKEN_PAREN_OPEN, KALOS_TOKEN_PAREN_CLOSE));
if (param_count != fn->fn_state.param_count) {
THROW(ERROR_UNEXPECTED_PARAMETERS);
}
TRY_EXIT;
struct pending_op op = {0};
op.op = KALOS_OP_GOSUB;
op.data[0] = fn->fn_state.param_count;
op.data[1] = fn->fn_state.pc;
return op;
}
static kalos_op parse_function_call_builtin(struct parse_state* parse_state, kalos_builtin* fn) {
if (parse_state->const_mode) {
THROW(ERROR_INVALID_CONST_EXPRESSION);
}
int param_count = 0;
TRY(param_count = parse_list_of_args(parse_state, KALOS_TOKEN_PAREN_OPEN, KALOS_TOKEN_PAREN_CLOSE));
if (param_count != fn->param_count) {
THROW(ERROR_UNEXPECTED_PARAMETERS);
}
TRY_EXIT;
return fn->op;
}
static struct pending_op parse_function_call_export(struct parse_state* parse_state, kalos_export* fn, uint8_t module_index) {
if (parse_state->const_mode) {
THROW(ERROR_INVALID_CONST_EXPRESSION);
}
int param_count = 0;
TRY(param_count = parse_list_of_args(parse_state, KALOS_TOKEN_PAREN_OPEN, KALOS_TOKEN_PAREN_CLOSE));
kalos_function* overload = kalos_module_get_list_item(parse_state->all_modules, fn->entry.function_overload_list.head);
while (overload) {
if (overload->vararg_type != FUNCTION_TYPE_VOID) {
if (param_count >= overload->arg_list.count) {
TRY(parse_push_op_1(parse_state, KALOS_OP_PUSH_INTEGER, param_count - overload->arg_list.count));
goto found;
}
} else {
if (param_count == overload->arg_list.count) {
goto found;
}
}
overload = overload->fn_overload_list.next ? kalos_module_get_list_item(parse_state->all_modules, overload->fn_overload_list.next) : NULL;
}
THROW(ERROR_UNEXPECTED_PARAMETERS);
found: ;
struct pending_op op = {0};
op.op = KALOS_OP_CALL;
op.data[0] = module_index;
op.data[1] = overload->binding.invoke_id;
op.data[2] = overload->arg_list.count;
if (parse_state->dispatch_name) {
op.sdata[0] = kalos_module_get_string(parse_state->all_modules, kalos_module_get_module(parse_state->all_modules, module_index)->name_index);
op.sdata[1] = kalos_module_get_string(parse_state->all_modules, fn->name_index);
}
TRY_EXIT;
return op;
}
static void parse_word_statement(struct parse_state* parse_state) {
struct pending_ops pending;
kalos_token peek;
TRY(pending = parse_word_recursively(parse_state));
TRY(peek = lex_peek(parse_state));
if (peek == KALOS_TOKEN_EQ) {
TRY(parse_assert_token(parse_state, KALOS_TOKEN_EQ));
TRY(parse_expression(parse_state));
TRY(parse_flush_pending_op(parse_state, &pending, true, true));
return;
} else if (is_assignment_token(peek)) {
TRY(parse_assert_token(parse_state, peek));
// We'll need two copies of the data
if (pending.req_dup) {
TRY(parse_push_op(parse_state, KALOS_OP_DUP));
}
TRY(parse_flush_pending_op(parse_state, &pending, false, false));
TRY(parse_expression(parse_state));
kalos_op op = 0;
#define KALOS_TOKEN_OP_ASG(x, y) case KALOS_TOKEN_EQ_##x: op = KALOS_OP_##y; break;
switch (peek) {
#include "_kalos_constants.inc"
default: break;
}
if (op) {
TRY(parse_push_op(parse_state, op));
}
TRY(parse_flush_pending_op(parse_state, &pending, true, true));
return;
} else if (peek == KALOS_TOKEN_SEMI && pending.load.op == KALOS_OP_CALL) {
pending.load.op = KALOS_OP_CALL_NORET;
TRY(parse_flush_pending_op(parse_state, &pending, false, true));
return;
} else if (peek == KALOS_TOKEN_SEMI && pending.load.op == KALOS_OP_GOSUB) {
pending.load.op = KALOS_OP_GOSUB_NORET;
TRY(parse_flush_pending_op(parse_state, &pending, false, true));
return;
} else if (peek == KALOS_TOKEN_SEMI && pending.load.op == KALOS_OP_APPEND) {
// TODO: We should either handle all builtins via a different path (a builtin invoke op?), or come up with a way to specify
// that builtins have no return value.
TRY(parse_flush_pending_op(parse_state, &pending, false, true));
return;
}
THROW(ERROR_UNEXPECTED_TOKEN);
TRY_EXIT;
}
static void parse_word_expression(struct parse_state* parse_state) {
struct pending_ops pending;
TRY(pending = parse_word_recursively(parse_state));
TRY(parse_flush_pending_op(parse_state, &pending, false, true));
TRY_EXIT;
}
static void parse_if_statement(struct parse_state* parse_state) {
int fixup_offset, fixup_offset_else;
TRY(parse_expression(parse_state));
TRY(parse_push_op(parse_state, KALOS_OP_LOGICAL_NOT));
TRY(fixup_offset = parse_push_goto_forward(parse_state, KALOS_OP_GOTO_IF));
TRY(parse_statement_block(parse_state));
kalos_token peek;
TRY(peek = lex_peek(parse_state));
if (peek != KALOS_TOKEN_ELSE) {
TRY(parse_fixup_offset(parse_state, fixup_offset, parse_state->output_script_index));
return;
}
TRY(parse_assert_token(parse_state, KALOS_TOKEN_ELSE));
TRY(fixup_offset_else = parse_push_goto_forward(parse_state, KALOS_OP_GOTO));
TRY(parse_fixup_offset(parse_state, fixup_offset, parse_state->output_script_index));
TRY(peek = lex_peek(parse_state));
if (peek == KALOS_TOKEN_IF) {
// Recurse for else if
TRY(parse_assert_token(parse_state, KALOS_TOKEN_IF));
TRY(parse_if_statement(parse_state));
} else {
TRY(parse_statement_block(parse_state));
}
TRY(parse_fixup_offset(parse_state, fixup_offset_else, parse_state->output_script_index));
TRY_EXIT;
}
static void parse_loop_statement(struct parse_state* parse_state, bool iterator, uint8_t iterator_slot) {
int saved_break = parse_state->loop_break;
int saved_continue = parse_state->loop_continue;
int initial_fixup, break_fixup;
// Initial entry and break
TRY(initial_fixup = parse_push_goto_forward(parse_state, KALOS_OP_GOTO));
parse_state->loop_break = parse_state->output_script_index;
TRY(break_fixup = parse_push_goto_forward(parse_state, KALOS_OP_GOTO));
TRY(parse_fixup_offset(parse_state, initial_fixup, parse_state->output_script_index));
parse_state->loop_continue = parse_state->output_script_index;
if (iterator) {
TRY(parse_push_op_2(parse_state, KALOS_OP_ITERATOR_NEXT, iterator_slot, parse_state->loop_break));
}
TRY(parse_statement_block(parse_state));
TRY(parse_push_op_1(parse_state, KALOS_OP_GOTO, parse_state->loop_continue));
TRY(parse_fixup_offset(parse_state, break_fixup, parse_state->output_script_index));
parse_state->loop_break = saved_break;
parse_state->loop_continue = saved_continue;
TRY_EXIT;
}
static void parse_flush_pending_op(struct parse_state* parse_state, struct pending_ops* ops, bool write, bool reset) {
struct pending_op* pending = write ? &ops->store : &ops->load;
if (parse_state->const_mode && !ops->is_const) {
THROW(ERROR_INVALID_CONST_EXPRESSION);
}
if (pending->op == KALOS_OP_LOAD_GLOBAL || pending->op == KALOS_OP_STORE_GLOBAL ||
pending->op == KALOS_OP_LOAD_LOCAL || pending->op == KALOS_OP_STORE_LOCAL) {
TRY(parse_push_op_1(parse_state, KALOS_OP_PUSH_INTEGER, pending->data[0]));
TRY(parse_push_op(parse_state, pending->op));
} else if (pending->op == KALOS_OP_CALL || pending->op == KALOS_OP_CALL_NORET) {
if (parse_state->dispatch_name) {
TRY(parse_push_string(parse_state, pending->sdata[0]));
TRY(parse_push_string(parse_state, pending->sdata[1]));
TRY(parse_push_op_1(parse_state, pending->op == KALOS_OP_CALL ? KALOS_OP_CALL_BYNAME : KALOS_OP_CALL_BYNAME_NORET, pending->data[2]));
} else {
TRY(parse_push_op_1(parse_state, KALOS_OP_PUSH_INTEGER, pending->data[0]));
TRY(parse_push_op_1(parse_state, KALOS_OP_PUSH_INTEGER, pending->data[1]));
TRY(parse_push_op_1(parse_state, pending->op, pending->data[2]));
}
} else if (pending->op == KALOS_OP_GOSUB || pending->op == KALOS_OP_GOSUB_NORET) {
TRY(parse_push_op_1(parse_state, KALOS_OP_PUSH_INTEGER, pending->data[0]));
TRY(parse_push_op_1(parse_state, pending->op, pending->data[1]));
} else if (pending->op == KALOS_OP_OBJCALL) {
if (parse_state->dispatch_name) {
TRY(parse_push_string(parse_state, pending->sdata[0]));
TRY(parse_push_op_1(parse_state, KALOS_OP_OBJCALL_BYNAME, pending->data[1]));
} else {
TRY(parse_push_op_1(parse_state, KALOS_OP_PUSH_INTEGER, pending->data[0]));
TRY(parse_push_op_1(parse_state, pending->op, pending->data[1]));
}
} else if (pending->op == KALOS_OP_PUSH_STRING) {
TRY(parse_push_string(parse_state, kalos_module_get_string(parse_state->all_modules, pending->data[0])));
} else if (pending->op == KALOS_OP_PUSH_INTEGER) {
TRY(parse_push_op_1(parse_state, KALOS_OP_PUSH_INTEGER, pending->data[0]));
} else if (pending->op != 0) {
// No associated data
TRY(parse_push_op(parse_state, pending->op));
} else {
THROW(ERROR_INTERNAL_ERROR);
}
if (reset) {
ops->is_const = ops->req_dup = ops->load.op = ops->store.op = 0;
}
TRY_EXIT;
}
static struct pending_ops parse_word_recursively(struct parse_state* parse_state) {
// Dotted identifiers have the form |-- qualifier (0-1) --| |-- expression chain (0-n) --| |-- l-value (0-1) --|
// Examples: module.prop.value, module.prop[foo(1)], module.fn(1,2,3).bar
// The right-most element must be l-value compatible when used in an assignment context, but we don't
// always know whether we're in an assignment context until we've parsed further along.
name_resolution_result res;
struct pending_ops pending = {0};
TRY(res = resolve_word(parse_state, NULL));
if (res.type == NAME_RESOLUTION_NOT_FOUND) {
THROW(ERROR_UNKNOWN_VARIABLE);
} else if (res.type == NAME_RESOLUTION_UNIMPORTED_MODULE) {
THROW(ERROR_INVALID_IMPORT);
} else if (res.type == NAME_RESOLUTION_VAR) {
pending.load.data[0] = pending.store.data[0] = res.var_slot;
pending.load.op = res.load_op;
pending.store.op = res.store_op; // consts still have a store op because they are technically vars
pending.is_const = res.var->type == VAR_CONST;
} else if (res.type == NAME_RESOLUTION_MODULE || res.type == NAME_RESOLUTION_BUILTIN || res.type == NAME_RESOLUTION_MODULE_EXPORT) {
// These are acceptable, no pending ops
} else {
THROW(ERROR_INTERNAL_ERROR);
}
kalos_token peek;
loop {
TRY(peek = lex_peek(parse_state));
if (peek == KALOS_TOKEN_PERIOD) {
if (pending.load.op) {
TRY(parse_flush_pending_op(parse_state, &pending, false, true));
}
TRY(parse_assert_token(parse_state, KALOS_TOKEN_PERIOD));
TRY(parse_assert_token(parse_state, KALOS_TOKEN_WORD));
if (res.type == NAME_RESOLUTION_MODULE) {
// Module property
TRY(res = resolve_word(parse_state, res.module));
if (res.type == NAME_RESOLUTION_MODULE_EXPORT) {
if (res.export->type == KALOS_EXPORT_TYPE_PROPERTY) {
pending.load.op = pending.store.op = KALOS_OP_CALL;
pending.load.data[0] = pending.store.data[0] = res.export_module_index;
if (parse_state->dispatch_name) {
pending.load.sdata[0] = pending.store.sdata[0] = kalos_module_get_string(parse_state->all_modules, kalos_module_get_module(parse_state->all_modules, res.export_module_index)->name_index);
pending.load.sdata[1] = kalos_module_get_string(parse_state->all_modules, res.export->name_index);
pending.store.sdata[1] = kalos_module_get_string(parse_state->all_modules, res.export->name_index);
}
pending.load.data[1] = res.export->entry.property.read.invoke_id;
pending.load.data[2] = 0;
pending.store.data[1] = res.export->entry.property.write.invoke_id;
pending.store.data[2] = 1;
} else if (res.export->type == KALOS_EXPORT_TYPE_CONST_NUMBER) {
pending.is_const = true;
pending.load.op = KALOS_OP_PUSH_INTEGER;
pending.load.data[0] = res.export->entry.const_number;
} else if (res.export->type == KALOS_EXPORT_TYPE_CONST_STRING) {
pending.is_const = true;
pending.load.op = KALOS_OP_PUSH_STRING;
pending.load.data[0] = res.export->entry.const_string_index;
} else if (res.export->type == KALOS_EXPORT_TYPE_FUNCTION) {
continue;
} else {
THROW(ERROR_UNKNOWN_PROPERTY);
}
} else {
THROW(ERROR_UNKNOWN_PROPERTY);
}
} else {
// Object property
pending.req_dup = true;
TRY(pending.load.data[0] = kalos_module_lookup_property(parse_state->all_modules, false, parse_state->token));
if (pending.load.data[0]) {
pending.load.sdata[0] = parse_state->token;
pending.store.data[1] = 0;
pending.load.op = KALOS_OP_OBJCALL;
}
TRY(pending.store.data[0] = kalos_module_lookup_property(parse_state->all_modules, true, parse_state->token));
if (pending.store.data[0]) {
pending.store.data[1] = 1;
pending.store.sdata[0] = parse_state->token;
pending.store.op = KALOS_OP_OBJCALL;
}
if (!pending.load.op && !pending.store.op) {
THROW(ERROR_UNKNOWN_PROPERTY);
}
}
} else if (peek == KALOS_TOKEN_PAREN_OPEN) {
if (res.type == NAME_RESOLUTION_BUILTIN) {
TRY(pending.load.op = parse_function_call_builtin(parse_state, res.builtin));
} else if (res.type == NAME_RESOLUTION_MODULE_EXPORT) {
TRY(pending.load = parse_function_call_export(parse_state, res.export, res.export_module_index));
} else if (res.type == NAME_RESOLUTION_VAR && res.var->type == VAR_FN) {
TRY(pending.load = parse_function_call_local(parse_state, res.var));
} else {
THROW(ERROR_UNKNOWN_FUNCTION);
}
continue;
} else if (peek == KALOS_TOKEN_SQBRA_OPEN) {
if (pending.load.op) {
TRY(parse_flush_pending_op(parse_state, &pending, false, true));
}
TRY(parse_assert_token(parse_state, peek));
TRY(parse_expression(parse_state));
TRY(parse_assert_token(parse_state, KALOS_TOKEN_SQBRA_CLOSE));
pending.load.op = KALOS_OP_GETINDEX;
} else {
break;
}
}
TRY_EXIT;
return pending;
}
static void parse_for_statement(struct parse_state* parse_state) {
// Convert:
// for i in it { ... }
// into
// temp = iter(it); loop { i = next(it); if (done) break; ... } drop_iterator();
TRY(parse_assert_token(parse_state, KALOS_TOKEN_WORD));
struct name_resolution_result res;
TRY(res = resolve_word(parse_state, NULL));
if (res.type == NAME_RESOLUTION_NOT_FOUND) {
THROW(ERROR_UNKNOWN_VARIABLE);
}
TRY(parse_assert_token(parse_state, KALOS_TOKEN_IN));
TRY(parse_expression(parse_state));
TRY(parse_push_op(parse_state, KALOS_OP_ITERATOR));
TRY(parse_loop_statement(parse_state, true, res.var_slot));
TRY(parse_push_op(parse_state, KALOS_OP_DROP));
TRY_EXIT;
}
static void parse_string_expression(struct parse_state* parse_state, kalos_token token) {
if (token != KALOS_TOKEN_STRING_LITERAL && token != KALOS_TOKEN_STRING_INTERPOLATION) {
THROW(ERROR_UNEXPECTED_TOKEN);
}
bool first = true;
loop {
if (token == KALOS_TOKEN_STRING_INTERPOLATION) {
bool needs_add = false;
if (parse_state->token[0]) {
TRY(parse_push_token(parse_state));
needs_add = true;
}
TRY(parse_expression(parse_state));
TRY(parse_assert_token(parse_state, KALOS_TOKEN_STRING_FORMAT_SPEC));
if (parse_state->token[0]) {
TRY(parse_push_token(parse_state));
}
if (needs_add) {
TRY(parse_push_op(parse_state, KALOS_OP_ADD));
} else {
TRY(parse_push_op(parse_state, KALOS_OP_TO_STRING));
}
} else {
TRY(parse_push_token(parse_state));
}
if (first) {
first = false;
} else {
TRY(parse_push_op(parse_state, KALOS_OP_ADD));
}
kalos_token peek;
TRY(peek = lex_peek(parse_state));
if (peek == KALOS_TOKEN_STRING_LITERAL || peek == KALOS_TOKEN_STRING_INTERPOLATION) {
// Continue through the loop
TRY(parse_assert_token(parse_state, peek));
token = peek;
} else {
break;
}
}
TRY_EXIT;
}
static void parse_expression_part(struct parse_state* parse_state) {
kalos_token token;
TRY(token = lex(parse_state));
if (token == KALOS_TOKEN_PLUS) {
TRY(parse_expression_part(parse_state));
TRY(parse_push_op(parse_state, KALOS_OP_POSIVATE));
} else if (token == KALOS_TOKEN_MINUS) {
TRY(parse_expression_part(parse_state));
TRY(parse_push_op(parse_state, KALOS_OP_NEGATE));
} else if (token == KALOS_TOKEN_BANG) {
TRY(parse_expression_part(parse_state));
TRY(parse_push_op(parse_state, KALOS_OP_LOGICAL_NOT));
} else if (token == KALOS_TOKEN_TILDE) {
TRY(parse_expression_part(parse_state));
TRY(parse_push_op(parse_state, KALOS_OP_BITWISE_NOT));
} else if (token == KALOS_TOKEN_INTEGER || token == KALOS_TOKEN_TRUE || token == KALOS_TOKEN_FALSE) {
TRY(parse_push_token(parse_state));
} else if (token == KALOS_TOKEN_STRING_LITERAL || token == KALOS_TOKEN_STRING_INTERPOLATION) {
TRY(parse_string_expression(parse_state, token));
} else if (wordify(parse_state) == KALOS_TOKEN_WORD) {
TRY(parse_word_expression(parse_state));
} else if (token == KALOS_TOKEN_PAREN_OPEN) {
TRY(parse_expression(parse_state));
TRY(parse_assert_token(parse_state, KALOS_TOKEN_PAREN_CLOSE));
} else if (token == KALOS_TOKEN_SQBRA_OPEN) {
int count;
TRY(count = parse_list_of_args(parse_state, 0, KALOS_TOKEN_SQBRA_CLOSE));
TRY(parse_make_list(parse_state, count));
} else if (token == KALOS_TOKEN_AT_AT) {
TRY(parse_assert_token(parse_state, KALOS_TOKEN_PAREN_OPEN));
TRY(parse_assert_token(parse_state, KALOS_TOKEN_PERIOD));
TRY(token = lex(parse_state));
if (wordify(parse_state) != KALOS_TOKEN_WORD) {
THROW(ERROR_UNKNOWN_PROPERTY);
}
kalos_int property = kalos_module_lookup_property(parse_state->all_modules, false, parse_state->token);
if (parse_state->dispatch_name) {
TRY(parse_push_string(parse_state, parse_state->token));
} else {
TRY(parse_push_op_1(parse_state, KALOS_OP_PUSH_INTEGER, property));
}
TRY(parse_assert_token(parse_state, KALOS_TOKEN_PAREN_CLOSE));
}
TRY_EXIT;
}
static void parse_expression(struct parse_state* parse_state) {
kalos_token peek;
kalos_token op_stack[16] = {0};
uint8_t op_stack_idx = 0;
// Start with the first element
TRY(parse_expression_part(parse_state));
loop {
// Peek ahead to see if we have an operator
TRY(peek = lex_peek(parse_state));
int8_t precedence = get_operator_precedence(peek);
if (precedence == NO_OPERATOR_PRECEDENCE) {
goto exit;
}
TRY(parse_assert_token(parse_state, peek));
while (op_stack_idx > 0 && precedence >= get_operator_precedence(op_stack[op_stack_idx - 1])) {
TRY(parse_push_op(parse_state, get_operator_from_token(op_stack[--op_stack_idx])))
}
op_stack[op_stack_idx++] = peek;
TRY(parse_expression_part(parse_state));
}
exit:
while (op_stack_idx > 0) {
TRY(parse_push_op(parse_state, get_operator_from_token(op_stack[--op_stack_idx])))
}
TRY_EXIT;
}
static void parse_statement_block(struct parse_state* parse_state) {
parse_state->last_statement_type = KALOS_TOKEN_ERROR;
TRY(parse_assert_token(parse_state, KALOS_TOKEN_BRA_OPEN));
loop {
TRY(if (!parse_statement(parse_state)) { break; });