This repository has been archived by the owner on Nov 22, 2023. It is now read-only.
forked from gek169/seabass
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcode_validator.c
3334 lines (3159 loc) · 117 KB
/
code_validator.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 "stringutil.h"
#include "targspecifics.h"
#include "libmin.h"
#include "parser.h"
#include "data.h"
#include "metaprogramming.h"
#include "ast_opt.h"
/*
Code validator.
This stuff makes sure that the parsed code is syntactically correct and adds all the proper linkages.
For instance, "break" and "continue" need to reference what loop they're breaking or continuing.
Goto needs to identify what label it's going to.
and every jump needs to keep track of how much scope it's popping.
*/
/*from the VM.*/
uint64_t vm_get_offsetof(
typedecl* the_struct,
char* membername
);
char** discovered_labels = NULL;
uint64_t n_discovered_labels = 0;
uint64_t discovered_labels_capacity = 0;
static stmt* curr_stmt = NULL;
/*target word and sword. Used for bitwise operations and if/switch/elif/while.*/
static uint64_t TARGET_WORD_BASE = BASE_U64;
static uint64_t TARGET_WORD_SIGNED_BASE = BASE_I64;
static uint64_t TARGET_MAX_FLOAT_TYPE = BASE_F64;
static uint64_t TARGET_DISABLE_FLOAT = 0;
void set_target_word(uint64_t val){
TARGET_WORD_BASE = val;
TARGET_WORD_SIGNED_BASE = val + 1;
}
uint64_t get_target_word(){
return TARGET_WORD_BASE;
}
uint64_t get_signed_target_word(){
return TARGET_WORD_SIGNED_BASE;
}
void set_max_float_type(uint64_t val){
if(val != BASE_F64 && val != BASE_F32){
TARGET_MAX_FLOAT_TYPE = 0;
TARGET_DISABLE_FLOAT = 1;
} else {
TARGET_MAX_FLOAT_TYPE = val;
TARGET_DISABLE_FLOAT = 0;
}
}
uint64_t get_target_max_float_type(){
return TARGET_MAX_FLOAT_TYPE;
}
/*
Has a compatible streq function been implemented for the target?
*/
static inline int impl_streq_exists(){
for(unsigned long i = 0; i < nsymbols; i++){
if(streq("impl_streq", symbol_table[i]->name)){
if(symbol_table[i]->t.is_function == 0) return 0;
if(symbol_table[i]->is_codegen > 0) return 0;
if(symbol_table[i]->is_pure == 0) return 0;
if(symbol_table[i]->t.basetype != TARGET_WORD_SIGNED_BASE) return 0;
if(symbol_table[i]->t.pointerlevel != 0) return 0;
if(symbol_table[i]->nargs != 2) return 0;
if(symbol_table[i]->fargs[0]->basetype != BASE_U8) return 0;
if(symbol_table[i]->fargs[0]->pointerlevel != 1) return 0;
if(symbol_table[i]->fargs[1]->basetype != BASE_U8) return 0;
if(symbol_table[i]->fargs[1]->pointerlevel != 1) return 0;
return 1;
}
}
return 0;
}
static inline void validator_exit_err(){
char buf[80];
if(curr_stmt){
mutoa(buf, curr_stmt->linenum);
if(curr_stmt->filename){
fputs("File:Line:Col\n",stdout);
fputs(curr_stmt->filename,stdout);
fputs(":",stdout);
mutoa(buf,curr_stmt->linenum);
fputs(buf,stdout);
fputs(":",stdout);
mutoa(buf,curr_stmt->colnum);
fputs(buf,stdout);
fputs("\n",stdout);
puts("~~~~\nNote that the line number and column number");
puts("are where the validator invoked validator_exit_err.");
puts("\n\n(the actual error may be slightly before,\nor on the previous line)");
puts("I recommend looking near the location provided,\nnot just that exact spot!");
}
}
exit(1);
}
static int this_specific_scope_has_label(char* c, scope* s){
uint64_t i;
stmt* stmtlist = s->stmts;
for(i = 0; i < s->nstmts; i++)
if(stmtlist[i].kind == STMT_LABEL)
if(streq(c, stmtlist[i].referenced_label_name)) /*this is the label we're looking for!*/
return 1;
return 0;
}
static int64_t this_specific_scope_get_label_index(char* c, scope* s){
uint64_t i;
stmt* stmtlist = s->stmts;
for(i = 0; i < s->nstmts; i++)
if(stmtlist[i].kind == STMT_LABEL)
if(streq(c, stmtlist[i].referenced_label_name)) /*this is the label we're looking for!*/
return i;
return -1;
}
static void checkswitch(stmt* sw){
unsigned long i;
if(sw->kind != STMT_SWITCH){
puts("<VALIDATOR ERROR> checkswitch erroneously passed non-switch statement");
validator_exit_err();
}
/*switch never has a scopediff or vardiff because the labels must be in the same scope. Thus, no variables ever have to be popped.*/
sw->goto_scopediff = 0;
sw->goto_vardiff = 0;
for(i = 0; i < sw->switch_nlabels; i++){
int64_t index = this_specific_scope_get_label_index(sw->switch_label_list[i],sw->whereami);
if(index == -1)
{
puts("Switch statement in function uses label not in its home scope.");
puts("A switch statement is not allowed to jump out of its own scope, either up or down!");
puts("Function name:");
puts(symbol_table[active_function]->name);
puts("Label in particular:");
puts(sw->switch_label_list[i]);
validator_exit_err();
}
sw->switch_label_indices[i] = index;
/*
if( ((stmt*)sw->whereami->stmts)[sw->switch_label_indices[i]].kind != STMT_LABEL){
puts("Internal Validator Error");
puts("this_specific_scope_get_label_index returned the index of a non-label.");
validator_exit_err();
}
if(
!streq(
((stmt*)sw->whereami->stmts)
[sw->switch_label_indices[i]]
.referenced_label_name
,
sw->switch_label_list[i]
)
){
puts("Internal Validator Error");
puts("this_specific_scope_get_label_index returned the index of wrong label!");
validator_exit_err();
}
*/
}
return;
}
static void check_label_declarations(scope* lvl){
unsigned long i;
unsigned long j;
stmt* stmtlist = lvl->stmts;
for(i = 0; i < lvl->nstmts; i++)
{
curr_stmt = stmtlist + i;
if(stmtlist[i].myscope){
check_label_declarations(stmtlist[i].myscope);
}else if(stmtlist[i].kind == STMT_LABEL){
for(j = 0; j < n_discovered_labels;j++){
if(streq(stmtlist[i].referenced_label_name, discovered_labels[j]))
{
puts("Duplicate label in function:");
puts(symbol_table[active_function]->name);
puts("Label is:");
puts(stmtlist[i].referenced_label_name);
validator_exit_err();
}
}
/*add it to the list of discovered labels*/
n_discovered_labels++;
if(n_discovered_labels >= discovered_labels_capacity){
discovered_labels = realloc(discovered_labels, (n_discovered_labels) * sizeof(char*));
}
/*
if(0)
if(discovered_labels == NULL){
puts("failed realloc");
validator_exit_err();
}
*/
discovered_labels[n_discovered_labels-1] = stmtlist[i].referenced_label_name;
}
}
return;
}
static void check_switches(scope* lvl){
unsigned long i;
stmt* stmtlist = lvl->stmts;
for(i = 0; i < lvl->nstmts; i++)
{
curr_stmt = stmtlist + i;
if(stmtlist[i].myscope){
check_switches(stmtlist[i].myscope);
}else if(stmtlist[i].kind == STMT_SWITCH){
/**/
checkswitch(stmtlist+i);
}
}
return;
}
static void assign_lsym_gsym(expr_node* ee){
int64_t i;
int64_t j;
for(i = 0; i < MAX_FARGS; i++){
if(ee->subnodes[i])
assign_lsym_gsym(ee->subnodes[i]);
}
int64_t depth = 0;
/*Now, do our assignment.*/
if(ee->kind == EXPR_SYM)
for(i = (nscopes - 1); i >= 0; i--){
for(j = scopestack[i]->nsyms - 1; j >= 0 ; j--){
if(streq(ee->symname, scopestack[i]->syms[j].name)){
ee->kind = EXPR_LSYM;
ee->t = scopestack[i]->syms[j].t;
ee->symid = depth;
return;
}
depth++;
}
}
/*Must search active_function's fargs...*/
if(ee->kind == EXPR_SYM)
for(i = 0; i < (int64_t)symbol_table[active_function]->nargs; i++){
if(
streq(
ee->symname,
symbol_table[active_function]->fargs[i]->membername
)
){
ee->kind = EXPR_LSYM;
ee->t = *symbol_table[active_function]->fargs[i];
ee->symid = depth;
//ee->t.membername;
return;
} else {
depth++; //function arguments are on the stack in reverse order. so we search linearly.
}
}
if(ee->kind == EXPR_SYM)
for(i = 0; i < (int64_t)nsymbols; i++)
if(streq(ee->symname, symbol_table[i]->name)){
/*Do some validation. It can't be a function...*/
if(
symbol_table[i]->t.is_function &&
symbol_table[i]->nargs == 0
){
//transform into an EXPR_FCALL....
ee->kind = EXPR_FCALL;
ee->is_function = 1;
ee->symid = i;
ee->t = symbol_table[i]->t;
ee->t.is_function = 0;
return;
} else if(symbol_table[i]->t.is_function)
{
//
puts("<INTERNAL ERROR> Uncaught usage of function (with >0 args) name as identifier");
puts("Active Function:");
puts(symbol_table[active_function]->name);
puts("Identifier:");
if(ee->symname)
puts(ee->symname);
validator_exit_err();
}
if(symbol_table[active_function]->is_pure > 0){
puts("VALIDATOR ERROR!");
puts("You may not use global variables in pure functions.");
puts("This is the variable you are not allowed to access:");
puts(ee->symname);
puts("This is the function you tried to access it from:");
puts(symbol_table[active_function]->name);
validator_exit_err();
}
ee->kind = EXPR_GSYM;
ee->symid = i;
//this function is impure because it uses global variables.
symbol_table[active_function]->is_impure_globals_or_asm = 1;
symbol_table[active_function]->is_impure = 1;
return;
}
if(ee->kind == EXPR_SYM){
puts("<VALIDATION ERROR> Unknown identifier...");
puts("Active Function:");
puts(symbol_table[active_function]->name);
puts("Identifier:");
if(ee->symname)
puts(ee->symname);
validator_exit_err();
}
return;
}
static void throw_type_error(char* msg){
puts("TYPING ERROR!");
puts(msg);
puts("Function:");
puts(symbol_table[active_function]->name);
validator_exit_err();
}
static void throw_type_error_with_expression_enums(char* msg, unsigned a, unsigned b){
unsigned c;
puts("TYPING ERROR!");
puts(msg);
puts("Function:");
puts(symbol_table[active_function]->name);
c = a;
puts("a=");
if(c == EXPR_SIZEOF) puts("EXPR_SIZEOF");
if(c == EXPR_INTLIT) puts("EXPR_INTLIT");
if(c == EXPR_FLOATLIT) puts("EXPR_FLOATLIT");
if(c == EXPR_STRINGLIT) puts("EXPR_STRINGLIT");
if(c == EXPR_LSYM) puts("EXPR_LSYM");
if(c == EXPR_GSYM) puts("EXPR_GSYM");
if(c == EXPR_SYM) puts("EXPR_SYM");
if(c == EXPR_POST_INCR) puts("EXPR_POST_INCR");
if(c == EXPR_POST_DECR) puts("EXPR_POST_DECR");
if(c == EXPR_PRE_DECR) puts("EXPR_PRE_DECR");
if(c == EXPR_PRE_INCR) puts("EXPR_PRE_INCR");
if(c == EXPR_INDEX) puts("EXPR_INDEX");
if(c == EXPR_MEMBER) puts("EXPR_MEMBER");
if(c == EXPR_MEMBERPTR) puts("EXPR_MEMBERPTR");
if(c == EXPR_METHOD) puts("EXPR_METHOD");
if(c == EXPR_CAST) puts("EXPR_CAST");
if(c == EXPR_NEG) puts("EXPR_NEG");
if(c == EXPR_NOT) puts("EXPR_NOT");
if(c == EXPR_COMPL) puts("EXPR_COMPL");
if(c == EXPR_MUL) puts("EXPR_MUL");
if(c == EXPR_DIV) puts("EXPR_DIV");
if(c == EXPR_MOD) puts("EXPR_MOD");
if(c == EXPR_ADD) puts("EXPR_ADD");
if(c == EXPR_SUB) puts("EXPR_SUB");
if(c == EXPR_BITOR) puts("EXPR_BITOR");
if(c == EXPR_BITAND) puts("EXPR_BITAND");
if(c == EXPR_BITXOR) puts("EXPR_BITXOR");
if(c == EXPR_LSH) puts("EXPR_LSH");
if(c == EXPR_RSH) puts("EXPR_RSH");
if(c == EXPR_LOGOR) puts("EXPR_LOGOR");
if(c == EXPR_LOGAND) puts("EXPR_LOGAND");
if(c == EXPR_LT) puts("EXPR_LT");
if(c == EXPR_LTE) puts("EXPR_LTE");
if(c == EXPR_GT) puts("EXPR_GT");
if(c == EXPR_GTE) puts("EXPR_GTE");
if(c == EXPR_EQ) puts("EXPR_EQ");
if(c == EXPR_STREQ) puts("EXPR_STREQ");
if(c == EXPR_STRNEQ) puts("EXPR_STRNEQ");
if(c == EXPR_NEQ) puts("EXPR_NEQ");
if(c == EXPR_ASSIGN) puts("EXPR_ASSIGN");
if(c == EXPR_MOVE) puts("EXPR_MOVE");
if(c == EXPR_FCALL) puts("EXPR_FCALL");
if(c == EXPR_GETFNPTR) puts("EXPR_GETFNPTR");
if(c == EXPR_CALLFNPTR) puts("EXPR_CALLFNPTR");
if(c == EXPR_BUILTIN_CALL) puts("EXPR_BUILTIN_CALL");
if(c == EXPR_CONSTEXPR_FLOAT) puts("EXPR_CONSTEXPR_FLOAT");
if(c == EXPR_CONSTEXPR_INT) puts("EXPR_CONSTEXPR_INT");
if(b == EXPR_BAD) validator_exit_err();
puts("b=");
c = b;
if(c == EXPR_SIZEOF) puts("EXPR_SIZEOF");
if(c == EXPR_INTLIT) puts("EXPR_INTLIT");
if(c == EXPR_FLOATLIT) puts("EXPR_FLOATLIT");
if(c == EXPR_STRINGLIT) puts("EXPR_STRINGLIT");
if(c == EXPR_LSYM) puts("EXPR_LSYM");
if(c == EXPR_GSYM) puts("EXPR_GSYM");
if(c == EXPR_SYM) puts("EXPR_SYM");
if(c == EXPR_POST_INCR) puts("EXPR_POST_INCR");
if(c == EXPR_POST_DECR) puts("EXPR_POST_DECR");
if(c == EXPR_PRE_DECR) puts("EXPR_PRE_DECR");
if(c == EXPR_PRE_INCR) puts("EXPR_PRE_INCR");
if(c == EXPR_INDEX) puts("EXPR_INDEX");
if(c == EXPR_MEMBER) puts("EXPR_MEMBER");
if(c == EXPR_MEMBERPTR) puts("EXPR_MEMBERPTR");
if(c == EXPR_METHOD) puts("EXPR_METHOD");
if(c == EXPR_CAST) puts("EXPR_CAST");
if(c == EXPR_NEG) puts("EXPR_NEG");
if(c == EXPR_NOT) puts("EXPR_NOT");
if(c == EXPR_COMPL) puts("EXPR_COMPL");
if(c == EXPR_MUL) puts("EXPR_MUL");
if(c == EXPR_DIV) puts("EXPR_DIV");
if(c == EXPR_MOD) puts("EXPR_MOD");
if(c == EXPR_ADD) puts("EXPR_ADD");
if(c == EXPR_SUB) puts("EXPR_SUB");
if(c == EXPR_BITOR) puts("EXPR_BITOR");
if(c == EXPR_BITAND) puts("EXPR_BITAND");
if(c == EXPR_BITXOR) puts("EXPR_BITXOR");
if(c == EXPR_LSH) puts("EXPR_LSH");
if(c == EXPR_RSH) puts("EXPR_RSH");
if(c == EXPR_LOGOR) puts("EXPR_LOGOR");
if(c == EXPR_LOGAND) puts("EXPR_LOGAND");
if(c == EXPR_LT) puts("EXPR_LT");
if(c == EXPR_LTE) puts("EXPR_LTE");
if(c == EXPR_GT) puts("EXPR_GT");
if(c == EXPR_GTE) puts("EXPR_GTE");
if(c == EXPR_EQ) puts("EXPR_EQ");
if(c == EXPR_STREQ) puts("EXPR_STREQ");
if(c == EXPR_STRNEQ) puts("EXPR_STRNEQ");
if(c == EXPR_NEQ) puts("EXPR_NEQ");
if(c == EXPR_ASSIGN) puts("EXPR_ASSIGN");
if(c == EXPR_MOVE) puts("EXPR_MOVE");
if(c == EXPR_FCALL) puts("EXPR_FCALL");
if(c == EXPR_GETFNPTR) puts("EXPR_GETFNPTR");
if(c == EXPR_CALLFNPTR) puts("EXPR_CALLFNPTR");
if(c == EXPR_BUILTIN_CALL) puts("EXPR_BUILTIN_CALL");
if(c == EXPR_CONSTEXPR_FLOAT) puts("EXPR_CONSTEXPR_FLOAT");
if(c == EXPR_CONSTEXPR_INT) puts("EXPR_CONSTEXPR_INT");
validator_exit_err();
}
static void propagate_types(expr_node* ee){
unsigned long i;
unsigned long j;
type t = {0};
type t2 = {0};
uint64_t WORD_BASE;
uint64_t SIGNED_WORD_BASE;
uint64_t FLOAT_BASE;
if(symbol_table[active_function]->is_codegen){
WORD_BASE = BASE_U64;
SIGNED_WORD_BASE = BASE_I64;
FLOAT_BASE = BASE_F64;
} else {
WORD_BASE = TARGET_WORD_BASE;
SIGNED_WORD_BASE = TARGET_WORD_SIGNED_BASE;
FLOAT_BASE = TARGET_MAX_FLOAT_TYPE;
}
for(i = 0; i < MAX_FARGS; i++){
if(ee->subnodes[i])
propagate_types(ee->subnodes[i]);
if(!ee->subnodes[i]) continue; //in case we ever implement something that has null members...
/*
Do double-checking of the types in the subnodes.
particularly, we may not have struct rvalues, or lvalue structs.
must have pointer-to-struct or better.
*/
if(ee->subnodes[i]->t.basetype == BASE_STRUCT)
if(ee->subnodes[i]->t.pointerlevel == 0)
{
throw_type_error_with_expression_enums(
"expression node validated as having BASE_STRUCT without a pointer level..."
,ee->subnodes[i]->kind,
EXPR_BAD
);
}
if(ee->subnodes[i]->t.arraylen > 0){
throw_type_error_with_expression_enums(
"Expression node validated as having an array length.",
ee->subnodes[i]->kind,
EXPR_BAD
);
}
if(ee->subnodes[i]->t.is_function){
throw_type_error_with_expression_enums(
"Expression node validated as being a function. What?",
ee->subnodes[i]->kind,
EXPR_BAD
);
}
}
/*
TYPE GUARD
prevent most illegal type operations.
*/
back_to_the_top:;
if(ee->kind != EXPR_FCALL)/*Disallow void from this point onward.*/
if(ee->kind != EXPR_BUILTIN_CALL)/*Disallow void from this point onward.*/
if(ee->kind != EXPR_CALLFNPTR)/*Disallow void from this point onward.*/
{
if(ee->subnodes[0])
if(ee->subnodes[0]->t.basetype == BASE_VOID)
throw_type_error_with_expression_enums(
"Math expressions may never have void in them. a=Parent,b=Child:",
ee->kind,
ee->subnodes[0]->kind
);
if(ee->subnodes[1])
if(ee->subnodes[1]->t.basetype == BASE_VOID)
throw_type_error_with_expression_enums(
"Math expressions may never have void in them. a=Parent,b=Child:",
ee->kind,
ee->subnodes[0]->kind
);
}
/*Forbid pointers for arithmetic purposes.*/
if(ee->kind != EXPR_STREQ)
if(ee->kind != EXPR_STRNEQ)
if(ee->kind != EXPR_EQ)
if(ee->kind != EXPR_NEQ)
if(ee->kind != EXPR_ADD)
if(ee->kind != EXPR_SUB)
if(ee->kind != EXPR_FCALL)
if(ee->kind != EXPR_BUILTIN_CALL)
if(ee->kind != EXPR_CALLFNPTR)
if(ee->kind != EXPR_PRE_INCR)
if(ee->kind != EXPR_PRE_DECR)
if(ee->kind != EXPR_POST_INCR)
if(ee->kind != EXPR_POST_DECR)
if(ee->kind != EXPR_ASSIGN) /*you can assign pointers...*/
if(ee->kind != EXPR_MOVE) /*you can assign pointers...*/
if(ee->kind != EXPR_CAST) /*You can cast pointers...*/
if(ee->kind != EXPR_INDEX) /*You can index pointers...*/
if(ee->kind != EXPR_METHOD) /*You invoke methods on or with pointers.*/
if(ee->kind != EXPR_MEMBER) /*You can get members of structs which are pointed to.*/
if(ee->kind != EXPR_MEMBERPTR) /*You can get pointers to members of structs which are pointed to.*/
{
if(ee->subnodes[0])
if(ee->subnodes[0]->t.pointerlevel > 0)
throw_type_error_with_expression_enums(
"Pointer math is limited to addition, subtraction, assignment,moving, casting, equality comparison, and indexing.\n"
"a=Parent,b=Child",
ee->kind,
ee->subnodes[0]->kind
);
if(ee->subnodes[1])
if(ee->subnodes[1]->t.pointerlevel > 0)
throw_type_error_with_expression_enums(
"Pointer math is limited to addition, subtraction, assignment,moving, casting, equality comparison, and indexing.\n"
"a=Parent,b=Child",
ee->kind,
ee->subnodes[1]->kind
);
}
if(ee->kind == EXPR_INTLIT){
//t.basetype = BASE_U64;
t.basetype = WORD_BASE;
t.is_lvalue = 0;
t.pointerlevel = 0;
ee->constint_propagator = 1;
ee->t = t;
return;
}
if(ee->kind == EXPR_FLOATLIT){
if(symbol_table[active_function]->is_codegen == 0)
if(TARGET_DISABLE_FLOAT)
{
puts("Validator error:");
puts("Target has disabled floating point.");
validator_exit_err();
}
//t.basetype = BASE_F64;
t.basetype = FLOAT_BASE;
t.is_lvalue = 0;
t.pointerlevel = 0;
ee->t = t;
return;
}
if(ee->kind == EXPR_GSYM){
t = symbol_table[ee->symid]->t;
if(t.arraylen){
t.arraylen = 0;
t.pointerlevel++; //this prevents the next part from triggering...
t.is_lvalue = 0;
}
if(t.pointerlevel == 0){
if(t.basetype == BASE_STRUCT){
t.pointerlevel = 1;
t.is_lvalue = 0;
ee->was_struct_var = 1;
}
}
ee->t = t;
return;
}
if(ee->kind == EXPR_GETGLOBALPTR){
t = symbol_table[ee->symid]->t;
t.is_lvalue = 0;
if(t.arraylen){ //arrays need arrayness removed.
t.arraylen = 0;
}
if(symbol_table[ee->symid]->is_data == 0){ //pointer level is not increased for data variables. they are already pointers!
t.pointerlevel++;
}
ee->t = t;
return;
}
if(ee->kind == EXPR_LSYM){
t = ee->t;
if(t.arraylen){
t.arraylen = 0;
t.pointerlevel++; //this prevents the next part from triggering...
t.is_lvalue = 0;
}
if(t.pointerlevel == 0){
if(t.basetype == BASE_STRUCT){
t.pointerlevel = 1;
t.is_lvalue = 0;
ee->was_struct_var = 1;
}
}
ee->t = t;
ee->t.membername = NULL; /*If it was a function argument, we dont want the membername thing to be there.*/
return;
}
if(ee->kind == EXPR_SIZEOF){
t.basetype = WORD_BASE;
t.is_lvalue = 0;
t.pointerlevel = 0;
ee->t = t;
return;
}
if(ee->kind == EXPR_STRINGLIT){
t.basetype = BASE_U8;
t.pointerlevel = 1;
t.is_lvalue = 0;
ee->t = t;
return;
}
if(ee->kind == EXPR_BUILTIN_CALL){
uint64_t q = get_builtin_retval(ee->symname);
t.is_lvalue = 0;
if(symbol_table[active_function]->is_pure > 0){
puts("Validator error:");
puts("Attempted to use a builtin call in a pure function.");
validator_exit_err();
}
if(symbol_table[active_function]->is_codegen == 0){
puts("Cannot use builtin calls in non-codegen function!");
validator_exit_err();
}
if(q == BUILTIN_PROTO_VOID){
t.basetype = BASE_VOID;
t.arraylen = 0;
t.pointerlevel = 0;
ee->t = t;
return;
}
if(q == BUILTIN_PROTO_U8_PTR){
t.basetype = BASE_U8;
t.arraylen = 0;
t.pointerlevel = 1;
ee->t = t;
return;
}
if(q == BUILTIN_PROTO_U8_PTR2){
t.basetype = BASE_U8;
t.arraylen = 0;
t.pointerlevel = 2;
ee->t = t;
return;
}
if(q == BUILTIN_PROTO_U64_PTR){
t.basetype = BASE_U64;
t.arraylen = 0;
t.pointerlevel = 1;
ee->t = t;
return;
}
if(q == BUILTIN_PROTO_U64){
t.basetype = BASE_U64;
t.arraylen = 0;
t.pointerlevel = 0;
ee->t = t;
return;
}
if(q == BUILTIN_PROTO_I64){
t.basetype = BASE_I64;
t.arraylen = 0;
t.pointerlevel = 0;
ee->t = t;
return;
}
if(q == BUILTIN_PROTO_DOUBLE){
t.basetype = BASE_F64;
t.arraylen = 0;
t.pointerlevel = 0;
ee->t = t;
return;
}
if(q == BUILTIN_PROTO_I32){
t.basetype = BASE_I32;
t.arraylen = 0;
t.pointerlevel = 0;
ee->t = t;
return;
}
puts("Internal Error: Unhandled __builtin function return value in type propagator.");
puts("The unhandled one is:");
puts(ee->symname);
validator_exit_err();
}
if(ee->kind == EXPR_CONSTEXPR_FLOAT){
//ee->t.basetype = BASE_F64;
ee->t.basetype = FLOAT_BASE;
t.is_lvalue = 0;
t.pointerlevel = 0;
return;
}
if(ee->kind == EXPR_CONSTEXPR_INT){
ee->t.basetype = SIGNED_WORD_BASE;
t.is_lvalue = 0;
t.pointerlevel = 0;
ee->constint_propagator = 1;
return;
}
if(ee->kind == EXPR_POST_INCR ||
ee->kind == EXPR_PRE_INCR ||
ee->kind == EXPR_POST_DECR ||
ee->kind == EXPR_PRE_DECR
){
t = ee->subnodes[0]->t;
if(t.pointerlevel == 0){
if(t.basetype == BASE_VOID){
throw_type_error("Can't increment/decrement void!");
}
if(t.basetype == BASE_STRUCT){
throw_type_error("Can't increment/decrement struct");
}
if(t.is_lvalue == 0){
throw_type_error("Outside of a constant expression, it is invalid to increment or decrement a non-lvalue.");
}
}
t.is_lvalue = 0; /*no longer an lvalue.*/
ee->t = t;
return;
}
if(ee->kind == EXPR_INDEX){
t = ee->subnodes[0]->t;
if(t.pointerlevel == 0){
throw_type_error("Can't index non-pointer!");
}
t.pointerlevel--;
t.is_lvalue = 1; /*if it wasn't an lvalue before, it is now!*/
if(t.pointerlevel == 0){
if(t.basetype == BASE_STRUCT)
{
ee->kind = EXPR_ADD;
t.pointerlevel++;
t.is_lvalue = 0;
ee->t = t;
goto back_to_the_top;
}
if(t.basetype == BASE_VOID)
throw_type_error("Can't deref pointer to void.");
}
ee->t = t;
if(
ee->subnodes[1]->t.pointerlevel > 0 ||
ee->subnodes[1]->t.basetype == BASE_F32 ||
ee->subnodes[1]->t.basetype == BASE_F64 ||
ee->subnodes[1]->t.basetype == BASE_VOID ||
ee->subnodes[1]->t.basetype == BASE_STRUCT ||
ee->subnodes[1]->t.basetype > BASE_STRUCT
){
throw_type_error("Indexing requires an integer.");
}
return;
}
if(ee->kind == EXPR_MEMBER){
//int found = 0;
t = ee->subnodes[0]->t;
if(t.basetype != BASE_STRUCT)
throw_type_error_with_expression_enums(
"Can't access member of non-struct! a=me",
ee->kind,
ee->subnodes[0]->kind
);
if(t.structid >= ntypedecls)
throw_type_error("Internal error, invalid struct ID for EXPR_MEMBER");
if(ee->symname == NULL)
throw_type_error("Internal error, EXPR_MEMBER had null symname...");
for(j = 0; j < type_table[t.structid].nmembers; j++){
if(
streq(
type_table[t.structid].members[j].membername,
ee->symname
)
){
// found = 1;
ee->t = type_table[t.structid].members[j];
ee->idata = vm_get_offsetof(type_table + t.structid, ee->symname);
ee->t.is_lvalue = 1;
//handle: struct member is array.
if(ee->t.arraylen){
ee->t.is_lvalue = 0;
ee->t.arraylen = 0;
ee->t.pointerlevel++;
}
//handle: struct member is also a struct, but not pointer to struct.
//note that for an array of structs, pointerlevel was already set,
//so we don't have to worry about that here.
if(ee->t.basetype == BASE_STRUCT)
if(ee->t.pointerlevel == 0){
ee->t.is_lvalue = 0;
ee->t.arraylen = 0;
ee->t.pointerlevel = 1;
}
//handle:struct member is function
if(ee->t.is_function){
puts("Error: Struct member is function. How did that happen?");
throw_type_error("Struct member is function.");
}
ee->t.membername = NULL; /*We don't want it!*/
return;
}
}
{
puts("Struct:");
puts(type_table[t.structid].name);
puts("Does not have member:");
puts(ee->symname);
throw_type_error_with_expression_enums(
"Struct lacking member. a=me",
ee->kind,
EXPR_BAD
);
validator_exit_err();
}
return;
}
if(ee->kind == EXPR_MEMBERPTR){
//int found = 0;
t = ee->subnodes[0]->t;
if(t.basetype != BASE_STRUCT)
throw_type_error_with_expression_enums(
"Can't access memberptr of non-struct! a=me",
ee->kind,
ee->subnodes[0]->kind
);
if(t.structid >= ntypedecls)
throw_type_error("Internal error, invalid struct ID for EXPR_MEMBERPTR");
if(ee->symname == NULL)
throw_type_error("Internal error, EXPR_MEMBERPTR had null symname...");
for(j = 0; j < type_table[t.structid].nmembers; j++){
if(
streq(
type_table[t.structid].members[j].membername,
ee->symname
)
){
// found = 1;
ee->t = type_table[t.structid].members[j];
ee->t.is_lvalue = 0;
ee->idata = vm_get_offsetof(type_table + t.structid, ee->symname);
//handle: struct member is array.
if(ee->t.arraylen > 0){
ee->t.arraylen = 0;
}
ee->t.pointerlevel++; //for arrays, this is what we would do if we were decaying an array, anyway!
//handle:struct member is function
if(ee->t.is_function){
puts("Error: Struct member is function. How did that happen?");
throw_type_error("Struct member is function.");
}
ee->t.membername = NULL; /*We don't want it!*/
return;
}
}
{
puts("Struct:");
puts(type_table[t.structid].name);
puts("Does not have member, therefore cannot retrieve pointer:");
puts(ee->symname);
throw_type_error_with_expression_enums(
"Struct lacking member. a=me",
ee->kind,
EXPR_BAD
);
validator_exit_err();
}
return;
}
if(ee->kind == EXPR_METHOD){
char* c;
t = ee->subnodes[0]->t;
if(t.basetype != BASE_STRUCT)
throw_type_error("Can't call method on non-struct!");
if(t.structid >= ntypedecls)
throw_type_error("Internal error, invalid struct ID for EXPR_METHOD");
if(ee->method_name == NULL)
throw_type_error("Internal error, EXPR_METHOD had null methodname...");
/*
do name mangling.
*/
if(ee->symname == 0){
c = strcata("__method_", type_table[t.structid].name);
c = strcataf1(c, "_____");
c = strcataf1(c, ee->method_name);
ee->symname = c;
} else {
c = ee->symname;
}
for(j = 0; j < nsymbols; j++)
if(streq(c, symbol_table[j]->name)){
if(symbol_table[j]->t.is_function == 0){
puts("Weird things have been happening with the method table.");
puts("It appears you've been using reserved (__) names...");
puts("Don't do that!");
puts("The method I was looking for is:");
puts(c);
puts("And I found a symbol by that name, but it is not a function.");
throw_type_error("Method table messed up");
}
if(symbol_table[j]->nargs == 0){
puts("This method:");
puts(c);
puts("Takes no arguments? Huh?");
throw_type_error("Method table messed up");
}
if(symbol_table[j]->fargs[0]->basetype != BASE_STRUCT){
puts("This method:");
puts(c);
puts("Does not take a struct pointer as its first argument. Huh?");
throw_type_error("Method table messed up");
}
if(symbol_table[j]->fargs[0]->structid != t.structid){
puts("This method:");
puts(c);
puts("Was apparently somehow called, as a method, on a non-matching struct.");
throw_type_error("Method shenanigans");
}
if(symbol_table[active_function]->is_pure)
if(symbol_table[j]->is_pure == 0){
puts("<VALIDATOR ERROR>");
puts("You tried to invoke this method:");
puts(ee->method_name);
puts("On a struct of this type:");
puts(type_table[t.structid].name);
puts("But that method is not declared 'pure', and you are trying to invoke it in a pure function.");
puts("The pure function's name is:");
puts(symbol_table[active_function]->name);
throw_type_error("Purity check failure.");
}
if(!!symbol_table[active_function]->is_codegen != !!symbol_table[j]->is_codegen){
puts("Validator Error");
puts("You tried to invoke this method:");
puts(ee->method_name);
puts("On a struct of this type:");
puts(type_table[t.structid].name);
if(!!symbol_table[j]->is_codegen)
puts("But that method was declared codegen,");
else
puts("But that method was not declared codegen,");
puts("And the active function:");
puts(symbol_table[active_function]->name);
if(!!symbol_table[active_function]->is_codegen)
puts("is declared codegen.");