-
Notifications
You must be signed in to change notification settings - Fork 0
/
wax.cpp
2532 lines (2518 loc) · 88.6 KB
/
wax.cpp
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 <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <array>
#include <map>
#include <functional>
template<typename T> std::string tostring(const T& x) {
std::stringstream ss;
ss << x;
return ss.str();
}
#define TOKEN_CHAR_TAB '\t'
#define TOKEN_CHAR_NL '\n'
#define TOKEN_CHAR_CR '\r'
#define TOKEN_CHAR_SPACE ' '
#define TOKEN_CHAR_DOUBLE_QUOTE '"'
#define TOKEN_CHAR_SINGLE_QUOTE '\''
#define TOKEN_CHAR_PAREN_OPEN '('
#define TOKEN_CHAR_PAREN_CLOSE ')'
#define TOKEN_CHAR_SEMICOLON ';'
#define TOKEN_CHAR_BACKSLASH '\\'
#define TOKEN_TYPE_NORMAL 0
#define TOKEN_TYPE_PAREN 1
#define TOKEN_TYPE_COMMENT 2
struct token_file {
std::string name;
};
struct token {
int type;
std::string value;
struct token_file* file;
int newlines;
int linenumber;
};
struct token* token_new(int type, std::string value, struct token_file* file, int newlines, int linenumber) {
struct token* t = new token();
t->type = type;
t->value = value;
t->file = file;
t->newlines = newlines;
t->linenumber = linenumber;
return t;
}
std::vector<struct token*> token_parse(std::string input, std::string filename, int comments) {
struct token_file* file = new token_file();
file->name = filename;
std::string token = "";
std::vector<struct token*> tokens = std::vector<struct token*>();
int newlines = 0;
int linenumber = 1;
int in_comment = 0;
int in_single_quote = 0;
int in_double_quote = 0;
int in_quote = 0;
int in_quote_escape = 0;
for (int i = 0; (i < input.size()); i += 1) {
int c = input[i];
if (in_quote_escape) {
in_quote_escape = 0;
} else {
if ((!in_double_quote) && (c == TOKEN_CHAR_SINGLE_QUOTE)) {
in_single_quote = (!in_single_quote);
};
if ((!in_single_quote) && (c == TOKEN_CHAR_DOUBLE_QUOTE)) {
in_double_quote = (!in_double_quote);
};
in_quote = (in_single_quote || in_double_quote);
if (in_quote) {
if (c == TOKEN_CHAR_BACKSLASH) {
in_quote_escape = 1;
};
};
if (!in_quote) {
if (c == TOKEN_CHAR_SEMICOLON) {
in_comment = 1;
};
};
if (c == TOKEN_CHAR_NL) {
if (in_comment) {
if (comments != 0) {
tokens.insert(tokens.begin()+tokens.size(),token_new(TOKEN_TYPE_COMMENT, token, file, newlines, linenumber));
newlines = 0;
};
token = "";
};
in_comment = 0;
};
};
int whitespace = ((c == TOKEN_CHAR_TAB) || ((c == TOKEN_CHAR_SPACE) || ((c == TOKEN_CHAR_NL) || (c == TOKEN_CHAR_CR))));
int paren = ((c == TOKEN_CHAR_PAREN_OPEN) || (c == TOKEN_CHAR_PAREN_CLOSE));
if ((!in_quote) && ((!in_comment) && (whitespace || paren))) {
if (token.size() > 0) {
tokens.insert(tokens.begin()+tokens.size(),token_new(TOKEN_TYPE_NORMAL, token, file, newlines, linenumber));
newlines = 0;
};
if (paren) {
token = "";
(token += input[i]);
tokens.insert(tokens.begin()+tokens.size(),token_new(TOKEN_TYPE_PAREN, token, file, newlines, linenumber));
newlines = 0;
};
token = "";
} else {
(token += input[i]);
};
if (c == TOKEN_CHAR_NL) {
newlines = (newlines + 1);
linenumber = (linenumber + 1);
};
};
return tokens;
}
void token_write_to_output(std::vector<struct token*>& tokens) {
std::cout << "count:" << std::endl;
std::cout << tokens.size() << std::endl;
std::cout << "" << std::endl;
for (int i = 0; (i < tokens.size()); i += 1) {
std::cout << tokens[i]->value << std::endl;
};
}
std::string token_to_string(std::vector<struct token*>& tokens, int width) {
std::string s = "";
std::string line = "";
for (int i = 0; (i < tokens.size()); i += 1) {
std::string token = "";
for (int j = 0; (j < tokens[i]->value.size()); j += 1) {
int c = tokens[i]->value[j];
std::string encoded = "";
(encoded += c);
if (c == '\"') {
encoded = "\\\"";
};
if (c == '\\') {
encoded = "";
(encoded += '\\');
(encoded += '\\');
};
(token += encoded);
};
(line += token);
if (line.size() > width) {
(s += "\"");
(s += line);
(s += "\\n\"\n");
line = "";
} else {
if ((token != "(") && (token != ")")) {
(line += " ");
};
};
};
(s += "\"");
(s += line);
(s += "\\n\"\n");
return s;
}
#define NODE_ROOT 0
#define NODE_EXPR 1
#define NODE_PREPROCESS 2
#define NODE_CHAR 3
#define NODE_STR 4
#define NODE_STR_QUOTE 5
#define NODE_INT 6
#define NODE_FLOAT 7
#define NODE_COMMENT 8
struct node {
int type;
std::string value;
std::vector<struct node*> child;
struct token* token;
};
struct node* node_new(int type, std::string value) {
struct node* node_new = new node();
node_new->type = type;
node_new->value = value;
node_new->child = std::vector<struct node*>();
node_new->token = new token();
return node_new;
}
struct node* node_copy(struct node* src) {
struct node* res = new node();
res->type = src->type;
res->value = src->value;
res->child = std::vector<struct node*>();
for (int i = 0; (i < src->child.size()); i += 1) {
res->child.insert(res->child.begin()+i,node_copy(src->child[i]));
};
res->token = src->token;
return res;
}
#define NODE_CHAR_DOUBLE_QUOTE '"'
#define NODE_CHAR_SINGLE_QUOTE '\''
#define NODE_CHAR_MINUS '-'
#define NODE_CHAR_DECIMAL '.'
#define NODE_CHAR_ZERO '0'
#define NODE_CHAR_NINE '9'
#define NODE_CHAR_AT '@'
int node_type_from_token(std::string token) {
if (token.size() > 0) {
int c = token[0];
if (c == NODE_CHAR_AT) {
return NODE_PREPROCESS;
};
if (c == NODE_CHAR_SINGLE_QUOTE) {
return NODE_CHAR;
};
if (c == NODE_CHAR_DOUBLE_QUOTE) {
return NODE_STR_QUOTE;
};
if (((c >= NODE_CHAR_ZERO) && (c <= NODE_CHAR_NINE)) || (c == NODE_CHAR_MINUS)) {
for (int i = 0; (i < token.size()); i += 1) {
c = token[i];
if (c == NODE_CHAR_DECIMAL) {
return NODE_FLOAT;
};
};
return NODE_INT;
};
};
return NODE_STR;
}
struct node* node_from_tokens(std::vector<struct token*>& tokens) {
std::vector<struct node*> stack = std::vector<struct node*>();
stack.insert(stack.begin()+stack.size(),node_new(NODE_ROOT, ""));
int i = 0;
while (i < tokens.size()) {
std::string token = tokens[i]->value;
if (tokens[i]->type == TOKEN_TYPE_COMMENT) {
struct node* child = node_new(NODE_COMMENT, token);
child->token = tokens[i];
struct node* node = stack[(stack.size() - 1)];
node->child.insert(node->child.begin()+node->child.size(),child);
} else {
if (token == "(") {
std::string value = "";
std::string next_token = tokens[(i + 1)]->value;
if ((next_token != "(") && (next_token != ")")) {
value = next_token;
};
struct node* node = node_new(NODE_EXPR, value);
node->token = tokens[i];
if (value != "") {
i = (i + 1);
};
struct node* parent = stack[(stack.size() - 1)];
parent->child.insert(parent->child.begin()+parent->child.size(),node);
stack.insert(stack.begin()+stack.size(),node);
} else {
if (token == ")") {
stack.erase(stack.begin()+(stack.size() - 1),stack.begin()+(stack.size() - 1)+1);
} else {
struct node* child = node_new(node_type_from_token(token), token);
child->token = tokens[i];
struct node* node = stack[(stack.size() - 1)];
node->child.insert(node->child.begin()+node->child.size(),child);
};
};
};
i = (i + 1);
};
return stack[0];
}
struct node* node_expand(struct node* node_org) {
if (node_org->child.size() < 2) {
return node_org;
};
struct node* expanded = node_new(NODE_EXPR, node_org->value);
expanded->child.insert(expanded->child.begin()+expanded->child.size(),node_org->child[0]);
if (node_org->child.size() == 2) {
expanded->child.insert(expanded->child.begin()+expanded->child.size(),node_org->child[1]);
} else {
struct node* expanded_child = node_new(NODE_EXPR, node_org->value);
for (int i = 1; (i < node_org->child.size()); i += 1) {
expanded_child->child.insert(expanded_child->child.begin()+expanded_child->child.size(),node_org->child[i]);
};
expanded->child.insert(expanded->child.begin()+expanded->child.size(),expanded_child);
};
return expanded;
}
int node_indent_needed(struct node* node, struct node* child) {
if (child->type != NODE_EXPR) {
return 0;
};
if ((child->value == "param") || (child->value == "result")) {
return 0;
};
if ((node->value == "") || ((node->value == "func") || ((node->value == "do") || ((node->value == "then") || ((node->value == "else") || (node->value == "struct")))))) {
return 1;
};
return 0;
}
int node_to_wax_indent = 0;
std::string node_to_str(int depth, struct node* node) {
std::string s = "";
if (node->type == NODE_ROOT) {
for (int i = 0; (i < node->child.size()); i += 1) {
std::string child = node_to_str(0, node->child[i]);
for (int j = 0; (j < node->child[i]->token->newlines); j += 1) {
(s += "\n");
};
(s += child);
};
};
if (node->type == NODE_EXPR) {
(s += "(");
(s += node->value);
for (int i = 0; (i < node->child.size()); i += 1) {
struct node* node_child = node->child[i];
int indent_needed = (node_child->type == NODE_EXPR);
if (node_to_wax_indent) {
indent_needed = node_indent_needed(node, node_child);
indent_needed = (indent_needed || ((node->value == "&&") || (node->value == "||")));
};
indent_needed = (node_child->token->newlines > 0);
int indent_depth = indent_needed ? depth : (depth - 1);
std::string indent = "\t";
if (indent_needed) {
for (int i = 0; (i < node_child->token->newlines); i += 1) {
(s += "\n");
};
for (int i = 0; (i <= indent_depth); i += 1) {
(s += indent);
};
} else {
(s += " ");
};
std::string child = node_to_str((indent_depth + 1), node_child);
(s += child);
if (indent_needed && (i == (node->child.size() - 1))) {
(s += "\n");
for (int i = 0; (i < indent_depth); i += 1) {
(s += indent);
};
};
};
(s += ")");
};
if (node->type != NODE_EXPR) {
(s += node->value);
};
return s;
}
std::string node_to_wax(int depth, struct node* node) {
node_to_wax_indent = 1;
return node_to_str(depth, node);
}
void error_exit(std::string msg) { std::cerr << msg; exit(1); }
void compile_error(std::string module, struct node* node, std::string msg) {
std::string out = module;
(out += " error: ");
(out += node->token->file->name);
(out += ", line ");
(out += tostring(node->token->linenumber));
(out += "\n ");
(out += msg);
(out += "\n\n");
error_exit(out);
}
#define MODULE_SYMBOL "Symbol"
struct node_symbol {
std::string name;
struct node* node;
};
std::string dump_symbol(std::vector<struct node_symbol*>& symbol) {
std::string s = "";
for (int i = 0; (i < symbol.size()); i += 1) {
(s += " ");
(s += symbol[i]->name);
};
return s;
}
std::vector<struct node_symbol*> node_symbol_scope_node(std::vector<struct node_symbol*>& symbol, struct node* node) {
std::vector<struct node_symbol*> copy_symbol = std::vector<struct node_symbol*>();
for (int i = 0; (i < symbol.size()); i += 1) {
struct node_symbol* s = new node_symbol();
s->name = symbol[i]->name;
s->node = symbol[i]->node;
copy_symbol.insert(copy_symbol.begin()+copy_symbol.size(),s);
};
for (int i = 0; (i < node->child.size()); i += 1) {
struct node* child = node->child[i];
if ((child->value == "struct") || ((child->value == "func") || ((child->value == "let") || ((child->value == "param") || (child->value == "for"))))) {
struct node_symbol* s = new node_symbol();
s->name = child->child[0]->value;
s->node = child;
copy_symbol.insert(copy_symbol.begin()+copy_symbol.size(),s);
};
};
return copy_symbol;
}
struct node* node_symbol_get_node(std::vector<struct node_symbol*>& symbol, std::string type, struct node* node) {
for (int i = (symbol.size() - 1); (i >= 0); i += -1) {
if (symbol[i]->name == node->value) {
if ((type == "") || (type == symbol[i]->node->value)) {
return symbol[i]->node;
};
};
};
compile_error(MODULE_SYMBOL, node, "symbol not found");
return symbol[0]->node;
}
#define NODE_TYPE_NONE 0
#define NODE_TYPE_STR 1
#define NODE_TYPE_INT 2
#define NODE_TYPE_FLOAT 3
#define NODE_TYPE_VEC 4
#define NODE_TYPE_ARRAY 5
struct node* node_symbol_node_type_src(std::vector<struct node_symbol*>& symbol, struct node* node) {
if ((node->type == NODE_PREPROCESS) || ((node->type == NODE_STR_QUOTE) || ((node->type == NODE_CHAR) || ((node->type == NODE_INT) || ((node->type == NODE_FLOAT) || ((node->value == "str") || ((node->value == "int") || (node->value == "float")))))))) {
return node;
};
if (node->type == NODE_STR) {
struct node* symbol_node = node_symbol_get_node(symbol, "", node);
if (symbol_node->value == "func") {
return node;
};
if ((symbol_node->value == "let") || ((symbol_node->value == "param") || (symbol_node->value == "for"))) {
return node_symbol_node_type_src(symbol, symbol_node->child[1]);
};
};
if (node->type == NODE_EXPR) {
if (node->value == "=") {
return node;
};
if (node->value == "#") {
return node;
};
if (node->value == "vec") {
return node;
};
if (node->value == "arr") {
return node;
};
if (node->value == "struct") {
return node;
};
if (node->value == "cast") {
return node->child[1];
};
if (node->value == "?") {
return node->child[1];
};
if (node->value == "call") {
struct node* symbol_node = node_symbol_get_node(symbol, "", node->child[0]);
for (int i = 1; (i < symbol_node->child.size()); i += 1) {
if (symbol_node->child[i]->value == "result") {
return symbol_node->child[i]->child[0];
};
};
compile_error(MODULE_SYMBOL, node, "function result not found");
};
if (node->value == "get") {
struct node* target_node;
if (node->child[0]->type == NODE_STR) {
struct node* symbol_node = node_symbol_get_node(symbol, "", node->child[0]);
if ((symbol_node->value != "let") && (symbol_node->value != "param")) {
compile_error(MODULE_SYMBOL, node, "symbol not variable");
};
target_node = symbol_node->child[1];
} else {
if (node->child[0]->value == "get") {
struct node* get_node_type = node_symbol_node_type_src(symbol, node->child[0]);
target_node = get_node_type;
} else {
compile_error(MODULE_SYMBOL, node, "bad get expression");
};
};
for (int i = 1; (i < node->child.size()); i += 1) {
if (target_node->value == "vec") {
target_node = target_node->child[1];
} else {
if (target_node->value == "arr") {
target_node = target_node->child[0];
} else {
if (target_node->value == "str") {
return node_new(NODE_TYPE_INT, "");
} else {
if (target_node->value == "struct") {
struct node* symbol_node = node_symbol_get_node(symbol, "struct", target_node->child[0]);
int found = 0;
for (int j = 1; (j < symbol_node->child.size()); j += 1) {
if ((symbol_node->child[j]->value == "let") && (symbol_node->child[j]->child[0]->value == node->child[i]->value)) {
target_node = symbol_node->child[j]->child[1];
found = 1;
};
};
if (!found) {
compile_error(MODULE_SYMBOL, node, "struct member not found");
};
};
};
};
};
};
return node_symbol_node_type_src(symbol, target_node);
};
return node_symbol_node_type_src(symbol, node->child[0]);
};
compile_error(MODULE_SYMBOL, node, "source node not established");
return node;
}
int node_symbol_node_type(std::vector<struct node_symbol*>& symbol, struct node* node) {
struct node* src_node = node_symbol_node_type_src(symbol, node);
if (src_node->type == NODE_PREPROCESS) {
return NODE_PREPROCESS;
};
if (src_node->type == NODE_STR_QUOTE) {
return NODE_TYPE_STR;
};
if (src_node->type == NODE_CHAR) {
return NODE_TYPE_INT;
};
if (src_node->type == NODE_INT) {
return NODE_TYPE_INT;
};
if (src_node->type == NODE_FLOAT) {
return NODE_TYPE_FLOAT;
};
if (src_node->value == "=") {
return NODE_TYPE_INT;
};
if (src_node->value == "#") {
return NODE_TYPE_INT;
};
if (src_node->value == "str") {
return NODE_TYPE_STR;
};
if (src_node->value == "int") {
return NODE_TYPE_INT;
};
if (src_node->value == "float") {
return NODE_TYPE_FLOAT;
};
if (src_node->value == "call") {
return NODE_TYPE_INT;
};
return NODE_TYPE_NONE;
}
#include <fstream>
#include <streambuf>
std::string readfile(std::string filename) {
std::ifstream t(filename);
std::string str((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());
return str;
}
#define MODULE_PREPROCESS "Preprocess"
#define PREPROCESS_CHAR_STOP '.'
#define PREPROCESS_CHAR_SLASH '/'
#define PREPROCESS_CHAR_UNDERSCORE '_'
void preprocess_replace_node(struct node* node, std::map<std::string,struct node*>& define) {
if (node->type == NODE_PREPROCESS) {
std::string k = "";
for (int i = 1; (i < node->value.size()); i += 1) {
(k += node->value[i]);
};
struct node* replace = define[k]->child[1];
node->type = replace->type;
node->value = replace->value;
};
for (int i = 0; (i < node->child.size()); i += 1) {
preprocess_replace_node(node->child[i], define);
};
}
struct node* preprocess_replace(struct node* node) {
std::map<std::string,struct node*> define = {};
int i = 0;
while (i < node->child.size()) {
if (node->child[i]->value == "@define") {
define[node->child[i]->child[0]->value] = node->child[i];
node->child.erase(node->child.begin()+i,node->child.begin()+i+1);
} else {
preprocess_replace_node(node->child[i], define);
i = (i + 1);
};
};
return node;
}
void preprocess_package_rewrite_identifier(std::string package, std::map<std::string,int>& identifier, struct node* node) {
std::string v = node->value;
if (node->type == NODE_PREPROCESS) {
v.erase(v.begin()+0,v.begin()+0+1);
};
if (identifier[v] == 1) {
std::string value;
if (node->type == NODE_PREPROCESS) {
value = "@";
};
(value += "__");
(value += package);
(value += "__");
(value += v);
node->value = value;
};
for (int i = 0; (i < node->child.size()); i += 1) {
preprocess_package_rewrite_identifier(package, identifier, node->child[i]);
};
}
void preprocess_package_rewrite(std::string package, struct node* node) {
std::map<std::string,int> identifier = {};
for (int i = 0; (i < node->child.size()); i += 1) {
if ((node->child[i]->value == "@define") || ((node->child[i]->value == "func") || (node->child[i]->value == "struct"))) {
identifier[node->child[i]->child[0]->value] = 1;
};
};
preprocess_package_rewrite_identifier(package, identifier, node);
}
std::array<std::string,3> preprocess_package_from_path(std::string pathprefix, struct node* node) {
std::string package = "";
std::string filename = "";
std::string basepath = "";
std::string quotedfilename = node->child[0]->value;
if (node->child.size() == 2) {
for (int i = 1; (i < (node->child[0]->value.size() - 1)); i += 1) {
(package += node->child[0]->value[i]);
};
quotedfilename = node->child[1]->value;
};
int start = 0;
int end = quotedfilename.size();
for (int i = 0; (i < quotedfilename.size()); i += 1) {
if (quotedfilename[i] == PREPROCESS_CHAR_SLASH) {
start = (i + 1);
};
if (quotedfilename[i] == PREPROCESS_CHAR_STOP) {
end = i;
};
};
if (node->child.size() == 1) {
for (int i = start; (i < end); i += 1) {
(package += quotedfilename[i]);
};
};
for (int i = 0; (i < pathprefix.size()); i += 1) {
(filename += pathprefix[i]);
};
for (int i = 1; (i < (quotedfilename.size() - 1)); i += 1) {
(filename += quotedfilename[i]);
};
for (int i = 0; (i < pathprefix.size()); i += 1) {
(basepath += pathprefix[i]);
};
for (int i = 1; (i < start); i += 1) {
(basepath += quotedfilename[i]);
};
return {package,filename,basepath};
}
void preprocess_include_import(std::string basepath, struct node* node) {
int i = 0;
while (i < node->child.size()) {
if ((node->child[i]->value == "@include") || (node->child[i]->value == "@import")) {
std::array<std::string,3> res = preprocess_package_from_path(basepath, node->child[i]);
std::string package = res[0];
std::string filename = res[1];
std::string prefix = res[2];
std::string include_str = readfile(filename);
if (include_str.size() == 0) {
std::string err = "Cannot open file: ";
(err += filename);
compile_error(MODULE_PREPROCESS, node->child[i], err);
};
std::vector<struct token*> tokens = token_parse(include_str, filename, 0);
struct node* include = node_from_tokens(tokens);
preprocess_include_import(prefix, include);
if (node->child[i]->value == "@import") {
preprocess_package_rewrite(package, include);
};
node->child.erase(node->child.begin()+i,node->child.begin()+i+1);
for (int j = 0; (j < include->child.size()); j += 1) {
node->child.insert(node->child.begin()+(i + j),include->child[j]);
};
i = (i + include->child.size());
} else {
i = (i + 1);
};
};
}
void preprocess_identifier_rewrite(struct node* node) {
if (node->type == NODE_STR) {
for (int i = 0; (i < node->value.size()); i += 1) {
if (node->value[i] == PREPROCESS_CHAR_STOP) {
node->value.erase(node->value.begin()+i,node->value.begin()+i+1);
node->value.insert(node->value.begin()+i,PREPROCESS_CHAR_UNDERSCORE);
node->value.insert(node->value.begin()+i,PREPROCESS_CHAR_UNDERSCORE);
node->value.insert(node->value.begin()+0,PREPROCESS_CHAR_UNDERSCORE);
node->value.insert(node->value.begin()+0,PREPROCESS_CHAR_UNDERSCORE);
};
};
};
if (node->type == NODE_PREPROCESS) {
for (int i = 0; (i < node->value.size()); i += 1) {
if (node->value[i] == PREPROCESS_CHAR_STOP) {
node->value.erase(node->value.begin()+i,node->value.begin()+i+1);
node->value.insert(node->value.begin()+i,PREPROCESS_CHAR_UNDERSCORE);
node->value.insert(node->value.begin()+i,PREPROCESS_CHAR_UNDERSCORE);
node->value.insert(node->value.begin()+1,PREPROCESS_CHAR_UNDERSCORE);
node->value.insert(node->value.begin()+1,PREPROCESS_CHAR_UNDERSCORE);
};
};
};
for (int i = 0; (i < node->child.size()); i += 1) {
preprocess_identifier_rewrite(node->child[i]);
};
}
struct node* preprocess(struct node* node) {
preprocess_include_import("", node);
preprocess_identifier_rewrite(node);
return node;
}
std::string preprocess_vdom_quote(std::string value) {
std::string out = "";
(out += "\"");
(out += value);
(out += "\"");
return out;
}
int preprocess_vdom_isaction(std::string type) {
if ((type == "do") || ((type == "then") || (type == "else"))) {
return 1;
};
return 0;
}
int preprocess_vdom_reserverd(std::string type) {
if ((type == "let") || ((type == "set") || ((type == "<<") || ((type == "if") || ((type == "else") || ((type == "for") || ((type == "while") || (type == "do")))))))) {
return 1;
};
return 0;
}
struct node* preprocess_vdom_element(struct node* node) {
struct node* out = node_new(NODE_EXPR, "call");
out->child.insert(out->child.begin()+0,node_new(NODE_STR, "__vdom__element_new"));
out->child.insert(out->child.begin()+1,node_new(NODE_STR_QUOTE, preprocess_vdom_quote(node->value)));
return out;
}
std::vector<struct node*> preprocess_vdom_element_child(int depth, std::string name, struct node* node) {
std::vector<struct node*> out = std::vector<struct node*>();
int i = 0;
while (i < node->child.size()) {
std::string type = "";
if (node->child[i]->type == NODE_STR) {
type = "attr";
if ((node->child[(i + 1)]->type == NODE_EXPR) && (node->child[(i + 1)]->value == "func")) {
type = "event";
};
};
if (node->child[i]->type == NODE_EXPR) {
type = "element";
if (node->child[i]->value == "child") {
type = "child";
};
if (preprocess_vdom_reserverd(node->child[i]->value) != 0) {
type = "reserved";
};
};
if ((type == "attr") || (type == "event")) {
struct node* attr_set = node_new(NODE_EXPR, "set");
attr_set->token->newlines = 1;
attr_set->child.insert(attr_set->child.begin()+0,node_new(NODE_STR, name));
attr_set->child.insert(attr_set->child.begin()+1,node_new(NODE_STR, type));
attr_set->child.insert(attr_set->child.begin()+2,node_new(NODE_STR_QUOTE, preprocess_vdom_quote(node->child[i]->value)));
if ((type == "event") || (node->child[(i + 1)]->type == NODE_STR_QUOTE)) {
attr_set->child.insert(attr_set->child.begin()+3,node->child[(i + 1)]);
} else {
struct node* value = node_new(NODE_EXPR, "cast");
value->child.insert(value->child.begin()+0,node->child[(i + 1)]);
value->child.insert(value->child.begin()+1,node_new(NODE_STR, "str"));
attr_set->child.insert(attr_set->child.begin()+3,value);
};
out.insert(out.begin()+out.size(),attr_set);
i = (i + 1);
};
if (type == "reserved") {
std::vector<struct node*> child = node->child[i]->child;
node->child[i]->child = std::vector<struct node*>();
for (int j = 0; (j < child.size()); j += 1) {
if ((child[j]->type == NODE_EXPR) && preprocess_vdom_isaction(child[j]->value)) {
struct node* action_node = node_new(NODE_EXPR, child[j]->value);
std::vector<struct node*> element_child = preprocess_vdom_element_child((depth + 1), name, child[j]);
for (int k = 0; (k < element_child.size()); k += 1) {
action_node->child.insert(action_node->child.begin()+action_node->child.size(),element_child[k]);
};
node->child[i]->child.insert(node->child[i]->child.begin()+node->child[i]->child.size(),action_node);
} else {
node->child[i]->child.insert(node->child[i]->child.begin()+node->child[i]->child.size(),child[j]);
};
};
out.insert(out.begin()+out.size(),node->child[i]);
};
if (type == "child") {
struct node* element_insert = node_new(NODE_EXPR, "insert");
element_insert->token->newlines = 1;
element_insert->token->newlines = 1;
struct node* element_insert_get = node_new(NODE_EXPR, "get");
element_insert_get->child.insert(element_insert_get->child.begin()+0,node_new(NODE_STR, name));
element_insert_get->child.insert(element_insert_get->child.begin()+1,node_new(NODE_STR, "child"));
element_insert->child.insert(element_insert->child.begin()+0,element_insert_get);
struct node* element_insert_count = node_new(NODE_EXPR, "#");
struct node* element_insert_count_get = node_new(NODE_EXPR, "get");
element_insert_count_get->child.insert(element_insert_count_get->child.begin()+0,node_new(NODE_STR, name));
element_insert_count_get->child.insert(element_insert_count_get->child.begin()+1,node_new(NODE_STR, "child"));
element_insert_count->child.insert(element_insert_count->child.begin()+0,element_insert_count_get);
element_insert->child.insert(element_insert->child.begin()+1,element_insert_count);
element_insert->child.insert(element_insert->child.begin()+2,node_new(NODE_STR, node->child[i]->child[0]->value));
out.insert(out.begin()+out.size(),element_insert);
};
if (type == "element") {
struct node* block = node_new(NODE_EXPR, "");
if (preprocess_vdom_isaction(node->value) != 0) {
block->value = node->value;
};
block->token->newlines = 1;
out.insert(out.begin()+out.size(),block);
std::string child_name = "_";
(child_name += tostring(depth));
(child_name += "_");
(child_name += tostring(i));
(child_name += "_");
(child_name += node->child[i]->value);
struct node* element = node_new(NODE_EXPR, "let");
element->token->newlines = 1;
element->child.insert(element->child.begin()+0,node_new(NODE_STR, child_name));
struct node* element_struct = node_new(NODE_EXPR, "struct");
element_struct->child.insert(element_struct->child.begin()+0,node_new(NODE_STR, "__vdom__element"));
element->child.insert(element->child.begin()+1,element_struct);
element->child.insert(element->child.begin()+2,preprocess_vdom_element(node->child[i]));
block->child.insert(block->child.begin()+block->child.size(),element);
struct node* element_insert = node_new(NODE_EXPR, "insert");
element_insert->token->newlines = 1;
struct node* element_insert_get = node_new(NODE_EXPR, "get");
element_insert_get->child.insert(element_insert_get->child.begin()+0,node_new(NODE_STR, name));
element_insert_get->child.insert(element_insert_get->child.begin()+1,node_new(NODE_STR, "child"));
element_insert->child.insert(element_insert->child.begin()+0,element_insert_get);
struct node* element_insert_count = node_new(NODE_EXPR, "#");
struct node* element_insert_count_get = node_new(NODE_EXPR, "get");
element_insert_count_get->child.insert(element_insert_count_get->child.begin()+0,node_new(NODE_STR, name));
element_insert_count_get->child.insert(element_insert_count_get->child.begin()+1,node_new(NODE_STR, "child"));
element_insert_count->child.insert(element_insert_count->child.begin()+0,element_insert_count_get);
element_insert->child.insert(element_insert->child.begin()+1,element_insert_count);
element_insert->child.insert(element_insert->child.begin()+2,node_new(NODE_STR, child_name));
block->child.insert(block->child.begin()+block->child.size(),element_insert);
std::vector<struct node*> element_child = preprocess_vdom_element_child((depth + 1), child_name, node->child[i]);
for (int j = 0; (j < element_child.size()); j += 1) {
block->child.insert(block->child.begin()+block->child.size(),element_child[j]);
};
};
i = (i + 1);
};
return out;
}
struct node* preprocess_vdom(struct node* node) {
for (int i = 0; (i < node->child.size()); i += 1) {
if ((node->child[i]->value == "let") && ((node->child[i]->child.size() == 3) && ((node->child[i]->child[1]->value == "struct") && (((node->child[i]->child[1]->child[0]->value == "vdom.element") || (node->child[i]->child[1]->child[0]->value == "__vdom__element")) && ((node->child[i]->child[2]->value != "call") && (node->child[i]->child[2]->value != "alloc")))))) {
std::vector<struct node*> element_child = preprocess_vdom_element_child(0, node->child[i]->child[0]->value, node->child[i]->child[2]);
node->child[i]->child[2] = preprocess_vdom_element(node->child[i]->child[2]);
node->child[i]->child[2]->token->newlines = 0;
for (int j = 0; (j < element_child.size()); j += 1) {
i = (i + 1);
node->child.insert(node->child.begin()+i,element_child[j]);
};
} else {
node->child[i] = preprocess_vdom(node->child[i]);
};
};
return node;
}
#define MODULE_VALIDATION "Validation"
void validate(struct node* node) {
if (node->type == NODE_EXPR) {
if (node->value == "let") {
if ((node->child.size() < 2) || (node->child.size() > 3)) {
compile_error(MODULE_VALIDATION, node, "'let' requires 2 or 3 args");
};
};
};
for (int i = 0; (i < node->child.size()); i += 1) {
std::string child = node_to_str(0, node->child[i]);
validate(node->child[i]);
};
}
std::string node_to_js(int depth, struct node* node) {
std::string s = "";
depth = (depth + 1);
node = node_copy(node);
if (node->type == NODE_ROOT) {
int has_main = 0;
for (int i = 0; (i < node->child.size()); i += 1) {
if ((node->child[i]->value == "func") && (node->child[i]->child[0]->value == "main")) {
has_main = 1;
};
std::string child = node_to_js(0, node->child[i]);
(s += child);
(s += "\n");
};
if (has_main) {
(s += "(typeof process == 'undefined') ? main([]) : process.exit(main(process.argv.slice(1)));\n");
};
};
if ((node->value == "+") || ((node->value == "&&") || (node->value == "||"))) {
node = node_expand(node);
};
if (node->type == NODE_EXPR) {
std::string block_start = "(";
(block_start += node->value);
std::string block_end = ")";
std::string block_seperator = " ";
if (node->value == "") {
block_start = "{";
block_end = "}";
};
if (node->value == "@define") {
block_start = "const ";
(block_start += node->child[0]->value);
(block_start += " = ");
(block_start += node->child[1]->value);
(block_start += ";");
block_end = "";
while (node->child.size() > 0) {
node->child.erase(node->child.begin()+0,node->child.begin()+0+1);
};
};
if (node->value == "@if") {
block_start = "";
block_end = "";
if (node->child[0]->value == "TARGET_JS") {
node->child.erase(node->child.begin()+0,node->child.begin()+0+1);
node->child.erase(node->child.begin()+0,node->child.begin()+0+1);
} else {
while (node->child.size() > 0) {
node->child.erase(node->child.begin()+0,node->child.begin()+0+1);
};
};
};
if (node->value == "extern") {
block_start = "";
block_end = "";
while (node->child.size() > 0) {
node->child.erase(node->child.begin()+0,node->child.begin()+0+1);
};
};
if (node->value == "asm") {
std::string quotedline = node->child[0]->value;
std::string line = "";
for (int j = 1; (j < (quotedline.size() - 1)); j += 1) {
(line += quotedline[j]);
};
block_start = line;
block_end = "\n";
while (node->child.size() > 0) {
node->child.erase(node->child.begin()+0,node->child.begin()+0+1);
};
};
if (node->value == "struct") {
block_start = "";
block_end = "";
while (node->child.size() > 0) {
node->child.erase(node->child.begin()+0,node->child.begin()+0+1);
};
};
if (node->value == "func") {
block_start = "function ";
if (node->child[0]->type == NODE_STR) {
struct node* fname_node = node->child[0];
(block_start += fname_node->value);
node->child.erase(node->child.begin()+0,node->child.begin()+0+1);
};
(block_start += "(");
int param_count = 0;
int i = 0;
while (i < node->child.size()) {
struct node* child_node = node->child[i];
if (child_node->value == "param") {
if (child_node->child.size() == 2) {
struct node* param_node = child_node->child[0];
if (param_count > 0) {
(block_start += ", ");
};
(block_start += param_node->value);
param_count = (param_count + 1);
};
node->child.erase(node->child.begin()+i,node->child.begin()+i+1);
i = (i - 1);
};
if (child_node->value == "result") {
node->child.erase(node->child.begin()+i,node->child.begin()+i+1);
i = (i - 1);
};
i = (i + 1);