-
Notifications
You must be signed in to change notification settings - Fork 24
/
ldpl.cpp
1446 lines (1360 loc) · 55.9 KB
/
ldpl.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 "ldpl.h"
#include "ldpl_included_lib.cpp"
//Show internal representation
bool show_ir = false;
template <typename T>
static bool contains(vector<T> const& vec, T const& val)
{
return vec.end() != find(vec.begin(), vec.end(), val);
}
template <typename T>
static bool contains_any(vector<T> const& vec, vector<T> vals)
{
for (auto const& v : vals)
{
if (contains(vec, v))
return true;
}
return false;
}
int main(int argc, const char* argv[])
{
//Get command line arguments as string vector
vector<string> args(argv + 1, argv + argc);
if(contains_any(args, {"-v", "--version"})){
cout << "This is \033[32;1mLDPL version " << VERSION << "\033[0m." << endl << endl;
cout << "Copyright 2018-2019, \033[35;1mMartín del Río\033[0m (www.lartu.net)." << endl;
cout << "Built with amazing contributions from \033[35;1mʇʞʌp\033[0m, \033[35;1mDamián Garro\033[0m and a bunch of other contributors." << endl << endl;
cout << "Website and documentation can be found on \033[36;1mwww.ldpl-lang.org\033[0m." << endl;
cout << "Source code can be found at \033[36;1mhttps://github.com/lartu/ldpl\033[0m." << endl << endl;
cout << "LDPL may be copied only under the terms of the GNU General Public License 3.0." << endl <<endl;
cout << "Compiled on " << COMPILEDATE << endl;
return 0;
}
else if(contains_any(args, {"-h", "--help"})){
cout << "Usage: ldpl [options] file [arguments]" << endl;
cout << "Options:" << endl;
cout << " -h --help Display this information" << endl;
cout << " -r Display generated C++ code" << endl;
cout << " -o=<name> Set output file for compiled binary" << endl;
cout << " -i=<file> Include file in current compilation" << endl;
cout << " -f=<flag> Pass a flag to the C++ compiler" << endl;
cout << " -v --version Display LDPL version information" << endl;
#ifdef STATIC_BUILDS
cout << " -ns Disables static binary building" << endl;
#endif
return 0;
}
compiler_state state; //Compiler state (holds variables, sections, functions, etc)
vector<string> files_to_compile;
vector<string> extensions;
add_ldpllib(state);
#ifdef STATIC_BUILDS
bool no_static = false;
#endif
string output_filename = "";
string final_filename = "";
//Check arguments
if(args.size() >= 1){
for(string & arg : args){
if(arg.size() > 1 && arg[0] != '-'){
if(output_filename == "") output_filename = arg;
files_to_compile.push_back(arg);
}
else if(arg == "-r"){
show_ir = true;
}
#ifdef STATIC_BUILDS
else if(arg == "-ns"){
no_static = true;
}
#endif
else if(arg.substr(0, 3) == "-o="){
final_filename = arg.substr(3);
}
else if(arg.substr(0, 3) == "-i="){
if(0 == arg.compare(arg.length()-5, 5, ".ldpl")||0 == arg.compare(arg.length()-4, 4, ".lsc")){
files_to_compile.insert(files_to_compile.begin(), arg.substr(3));
}else{
//pass everything but .ldpl and .lsc files to the c++ compiler
extensions.push_back(arg.substr(3)); // kill -i= prefix
}
}
else if(arg.substr(0, 3) == "-f="){
//pass flags to the c++ compiler (for example -f=-lSDL)
extensions.push_back(arg.substr(3));
}
}
}
state.add_code("int main(int argc, char *argv[]){");
state.add_code("cout.precision(numeric_limits<ldpl_number>::digits10);");
state.add_code("srand (time(NULL));");
state.variables["ARGC"] = 1;
state.add_var_code("ldpl_number "+fix_identifier("ARGC", true)+";");
state.variables["FILE-LOADED"] = 1;
state.add_var_code("ldpl_number "+fix_identifier("FILE-LOADED", true)+";");
state.variables["ARGV"] = 4;
state.add_var_code("ldpl_vector<string> "+fix_identifier("ARGV", true)+";");
state.add_code("for(int i = 1; i < argc; ++i)");
state.add_code(fix_identifier("ARGV", true) + "[i-1] = argv[i];");
state.add_code(fix_identifier("ARGC", true) + " = argc - 1;");
//Fail if file was not passed
if(files_to_compile.size() == 0) error("Filename expected.");
//For each file, compile each file into one big code
for(string & filename : files_to_compile)
{
//If it's an argument
if(filename[0] == '-') continue;
//Reset state section for this file
state.section_state = 0;
state.current_file = filename;
load_and_compile(filename, state);
}
state.add_code("return 0; \n}");
//If only IR was required
if(show_ir){
for(string line : state.variable_code) cout << line << endl;
for(string line : state.subroutine_code) cout << line << endl;
for(string line : state.output_code) cout << line << endl;
exit(0);
}
//Run the compiled code
ofstream myfile;
myfile.open ("ldpl-temp.cpp");
for(string line : state.variable_code) myfile << line << endl;
for(string line : state.subroutine_code) myfile << line << endl;
for(string line : state.output_code) myfile << line << endl;
myfile.close();
//Generate output filename if not set by -o=
if(final_filename.empty()) {
for(unsigned int i = 0; i < output_filename.size(); ++i){
if(output_filename[i] != '.')
final_filename += output_filename[i];
else
break;
}
if(final_filename.size() == 0) final_filename = "ldpl-output";
final_filename += "-bin";
}
cout << "LDPL: Compiling..." << endl;
string compile_line = "c++ ldpl-temp.cpp -std=gnu++11 -o " + final_filename;
#ifdef STATIC_BUILDS
if(!no_static) compile_line+=" -static-libgcc -static-libstdc++ ";
#endif
if(!extensions.empty()){
for(string & extension : extensions) compile_line += " "+extension;
}
int compiled = system(compile_line.c_str());
system("rm ldpl-temp.cpp");
if(compiled == 0){
cout << "*\033[32;1m File(s) compiled successfully.\033[0m" << endl;
cout << "* Saved as " << final_filename << endl;
}else{
error("compilation failed.");
}
}
void load_and_compile(string & filename, compiler_state & state)
{
//Load file
ifstream file(filename);
//Fail if the file couldn't be loaded
if(!file.is_open()) error("The file '" + filename + "' couldn't be opened.");
//Get file contents
vector<string> lines;
string line = "";
while(getline(file, line))
{
replace_whitespace(line);
lines.push_back(line);
}
compile(lines, state);
}
//Replace all whitespace within string
void replace_whitespace(string & code)
{
for(char & c : code){
if(isspace(c)){
c = ' ';
}
}
}
//Shows error message and exits
void error(const string & msg)
{
cerr << "\033[1;31mLDPL Error: ";
cerr << msg;
cerr << "\033[0m" << endl;
exit(1);
}
//Compilation process
void compile(vector<string> & lines, compiler_state & state)
{
//For each line in the source code
for(unsigned int line_num = 0; line_num < lines.size(); ++line_num)
{
string & line = lines[line_num];
if(state.open_quote){
//Check for END QUOTE first
if(line.size() >= 9 && (line[0] == 'E' || line[0] == 'e')){
string upper = "";
for(char c : line) upper += toupper(c);
trim(upper);
if(upper == "END QUOTE"){
state.open_quote = false;
//Kill final newline. Programs can add crlf if needed.
string & prev = state.output_code.back();
size_t pos = prev.rfind("\\n");
if(pos != string::npos) prev.erase(pos, 2);
prev += ";";
continue;
}
}
//No END QUOTE, emit the line as C++
state.add_code("\"" + escape_c_quotes(line) + "\\n\"");
continue;
}
trim(line);
//Split line in various tokens
vector<string> tokens;
tokenize(line, line_num, tokens, state.current_file);
capitalize_tokens(tokens);
if(tokens.size() == 0) continue;
compile_line(tokens, line_num, state);
}
if(state.open_quote) error("your QUOTE block was not terminated.");
if(state.closing_subprocedure()) error("your SUB-PROCEDURE block was not terminated.");
if(state.closing_if()) error("your IF block was not terminated.");
if(state.closing_while()) error("your WHILE block was not terminated.");
}
//Tokenizes a line
void tokenize(string & line, unsigned int line_num, vector<string> & tokens, string & current_file)
{
bool in_string = false;
string current_token = "";
//For each letter in the line
for(unsigned int i = 0; i < line.size(); ++i)
{
char letter = line[i];
if(letter == ' ')
{
if(in_string) current_token += letter;
else
{
if(current_token.size() > 0)
tokens.push_back(current_token);
current_token = "";
}
}
else if(letter == '"')
{
in_string = !in_string;
current_token += letter;
}
else if(letter == '\\')
{
if(in_string){
if(i < line.size() - 1)
{
char next_letter = line[++i];
switch(next_letter)
{
case '\\': case '"': case '0':
case 'a': case 'b': case 't': case 'n':
case 'v': case 'f': case 'r': case 'e':
current_token += "\\" + string(1, next_letter);
break;
default:
error("unknown escape sequence (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
}
}
else error("\\ found alone on a string (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
}else{
current_token += letter;
}
}
else if(letter == '#') //Comment character
{
if(in_string) current_token += letter;
else
{
if(current_token.size() > 0)
tokens.push_back(current_token);
return;
}
}
else
{
current_token += letter;
}
if(i == line.size() - 1 && letter != ' ')
{
if(in_string) error("Unterminated string (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if(current_token.size() > 0)
tokens.push_back(current_token);
}
}
}
//Tokens to upper case
void capitalize_tokens(vector<string> & tokens)
{
for(string & token : tokens)
{
if(is_vector_index(token))
{
for(char & l : token){
if (l == '"' && *(&l - 1) == ':') break;
l = toupper(l);
}
}
else if(!is_number(token) && !is_string(token))
{
for(char & l : token){
l = toupper(l);
}
if (token == "CRLF") token = "\"\\r\\n\"";
}
}
}
//Compiles line per line
void compile_line(vector<string> & tokens, unsigned int line_num, compiler_state & state)
{
string & current_file = state.current_file;
++line_num;
if(line_like("DATA:", tokens, state))
{
if(state.section_state == 1)
error("Duplicate DATA section declaration (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if(state.section_state == 2)
error("DATA section declaration within PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
state.section_state = 1;
return;
}
if(line_like("PROCEDURE:", tokens, state))
{
if(state.section_state == 2)
error("Duplicate SECTION section declaration (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
state.section_state = 2;
return;
}
if(line_like("$name IS NUMBER", tokens, state))
{
if(state.section_state != 1)
error("Variable declaration outside DATA section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if(!variable_exists(tokens[0], state))
state.variables[tokens[0]] = 1;
else
error("Duplicate declaration for variable " + tokens[0] + " (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.add_var_code("ldpl_number " + fix_identifier(tokens[0], true) + " = 0;");
return;
}
if(line_like("$name IS TEXT", tokens, state))
{
if(state.section_state != 1)
error("Variable declaration outside DATA section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if(!variable_exists(tokens[0], state))
state.variables[tokens[0]] = 2;
else
error("Duplicate declaration for variable " + tokens[0] + " (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.add_var_code("string " + fix_identifier(tokens[0], true) + " = \"\";");
return;
}
if(line_like("$name IS NUMBER VECTOR", tokens, state))
{
if(state.section_state != 1)
error("Variable declaration outside DATA section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if(!variable_exists(tokens[0], state)){
state.variables[tokens[0]] = 3;
}
else
error("Duplicate declaration for variable " + tokens[0] + " (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.add_var_code("ldpl_vector<ldpl_number> " + fix_identifier(tokens[0], true) + ";");
return;
}
if(line_like("$name IS TEXT VECTOR", tokens, state))
{
if(state.section_state != 1)
error("Variable declaration outside DATA section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if(!variable_exists(tokens[0], state)){
state.variables[tokens[0]] = 4;
}
else
error("Duplicate declaration for variable " + tokens[0] + " (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.add_var_code("ldpl_vector<string> " + fix_identifier(tokens[0], true) + ";");
return;
}if(line_like("$name IS EXTERNAL NUMBER", tokens, state))
{
if(state.section_state != 1)
error("Variable declaration outside DATA section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if(!variable_exists(tokens[0], state))
state.variables[tokens[0]] = 1;
else
error("Duplicate declaration for variable " + tokens[0] + " (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
state.add_var_code("extern ldpl_number " + fix_external_identifier(tokens[0], true) + ";");
state.externals[tokens[0]] = true;
return;
}
if(line_like("$name IS EXTERNAL TEXT", tokens, state))
{
if(state.section_state != 1)
error("Variable declaration outside DATA section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if(!variable_exists(tokens[0], state))
state.variables[tokens[0]] = 2;
else
error("Duplicate declaration for variable " + tokens[0] + " (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
state.add_var_code("extern string " + fix_external_identifier(tokens[0], true) + ";");
state.externals[tokens[0]] = true;
return;
}
if(line_like("$name IS EXTERNAL NUMBER VECTOR", tokens, state))
{
if(state.section_state != 1)
error("Variable declaration outside DATA section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if(!variable_exists(tokens[0], state)){
state.variables[tokens[0]] = 3;
}
else
error("Duplicate declaration for variable " + tokens[0] + " (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
state.add_var_code("extern ldpl_vector<ldpl_number> " + fix_external_identifier(tokens[0], true) + ";");
state.externals[tokens[0]] = true;
return;
}
if(line_like("$name IS EXTERNAL TEXT VECTOR", tokens, state))
{
if(state.section_state != 1)
error("Variable declaration outside DATA section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if(!variable_exists(tokens[0], state)){
state.variables[tokens[0]] = 4;
}
else
error("Duplicate declaration for variable " + tokens[0] + " (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
state.add_var_code("extern ldpl_vector<string> " + fix_external_identifier(tokens[0], true) + ";");
state.externals[tokens[0]] = true;
return;
}
//We know at this point that the line is not a valid variable declaration
//increment open IF count if this is an if statement
if(tokens[0] == "IF")
state.open_if();
//handle ELSE and ELSE IF
if(tokens[0] == "ELSE"){
if(state.section_state != 2)
error("ELSE outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if(!state.closing_if())
error("ELSE without IF (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if(tokens.size() == 1){ // ELSE
state.add_code("}else{");
return;
} else if(tokens[1] == "IF"){ // ELSE IF
state.add_code("}else ");
//remove the ELSE input token so it becomes a regular IF,
//then continue compiling this line.
tokens.erase(tokens.begin()+0);
}
}
if(line_like("DISPLAY $display", tokens, state))
{
if(state.section_state != 2)
error("DISPLAY statement outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
for(unsigned int i = 1; i < tokens.size(); ++i){
//TODO ADD COLORS HERE
if(is_variable(tokens[i], state)){
state.add_code("cout << " + get_c_variable(state, tokens[i]) + " << flush;");
}
else{
state.add_code("cout << " + tokens[i] + " << flush;");
}
}
return;
}
if(line_like("ACCEPT $var", tokens, state))
{
if(state.section_state != 2)
error("ACCEPT statement outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
if(is_num_var(tokens[1], state))
state.add_code(get_c_variable(state, tokens[1]) + " = input_number();");
else
state.add_code(get_c_variable(state, tokens[1]) + " = input_string();");
return;
}
if(line_like("ACCEPT $str-var UNTIL EOF", tokens, state))
{
if(state.section_state != 2)
error("ACCEPT statement outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.add_code(get_c_variable(state, tokens[1])+" = input_until_eof();");
return;
}
if(line_like("STORE $expression IN $var", tokens, state))
{
if(state.section_state != 2)
error("STORE statement outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
string rhand;
if (is_num_var(tokens[3], state))
rhand = get_c_number(state, tokens[1]);
else
rhand = get_c_string(state, tokens[1]);
state.add_code(get_c_variable(state, tokens[3]) + " = " + rhand + ";");
return;
}
if(line_like("ADD $num-expr AND $num-expr IN $num-var", tokens, state))
{
if(state.section_state != 2)
error("ADD statement outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.add_code(get_c_variable(state, tokens[5]) + " = " + get_c_expression(state, tokens[1]) + " + " + get_c_expression(state, tokens[3]) + ";");
return;
}
if(line_like("SUBTRACT $num-expr FROM $num-expr IN $num-var", tokens, state))
{
if(state.section_state != 2)
error("SUBTRACT statement outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.add_code(get_c_variable(state, tokens[5]) + " = " + get_c_expression(state, tokens[3]) + " - " + get_c_expression(state, tokens[1]) + ";");
return;
}
if(line_like("DIVIDE $num-expr BY $num-expr IN $num-var", tokens, state))
{
if(state.section_state != 2)
error("DIVIDE statement outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.add_code(get_c_variable(state, tokens[5]) + " = " + get_c_expression(state, tokens[1]) + " / " + get_c_expression(state, tokens[3]) + ";");
return;
}
if(line_like("MULTIPLY $num-expr BY $num-expr IN $num-var", tokens, state))
{
if(state.section_state != 2)
error("MULTIPLY statement outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.add_code(get_c_variable(state, tokens[5]) + " = " + get_c_expression(state, tokens[1]) + " * " + get_c_expression(state, tokens[3]) + ";");
return;
}
if(line_like("MODULO $num-expr BY $num-expr IN $num-var", tokens, state))
{
if(state.section_state != 2)
error("MODULO statement outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.add_code(get_c_variable(state, tokens[5]) + " = modulo(" + get_c_expression(state, tokens[1]) + ", " + get_c_expression(state, tokens[3]) + ");");
return;
}
if(line_like("ABS $num-var", tokens, state))
{
if(state.section_state != 2)
error("ABS statement outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.add_code(get_c_variable(state, tokens[1]) + " = fabs("+get_c_variable(state, tokens[1])+");");
return;
}
if(line_like("JOIN $expression AND $expression IN $str-var", tokens, state))
{
if(state.section_state != 2)
error("JOIN statement outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.add_code("join(" + get_c_string(state, tokens[1]) + ", " + get_c_string(state, tokens[3]) + ", " + get_c_variable(state, tokens[5]) + ");");
return;
}
if(line_like("GET CHARACTER AT $num-expr FROM $str-expr IN $str-var", tokens, state))
{
if(state.section_state != 2)
error("GET CHARACTER statement outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.add_code(get_c_variable(state, tokens[7]) + " = charat(" + get_c_expression(state, tokens[5]) + ", " + get_c_expression(state, tokens[3]) + ");");
return;
}
if(line_like("SUB-PROCEDURE $name", tokens, state))
{
if(state.section_state != 2)
error("SUB-PROCEDURE declaration outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if(!is_subprocedure(tokens[1], state))
state.subprocedures.push_back(tokens[1]);
else
error("Duplicate declaration for subprocedure " + tokens[1] + " (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if(state.closing_subprocedure())
error("Subprocedure declaration inside subprocedure (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
else if(state.closing_if())
error("Subprocedure declaration inside IF (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
else if(state.closing_while())
error("Subprocedure declaration inside WHILE (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
else
state.open_subprocedure();
//C Code
state.add_code("void "+fix_identifier(tokens[1], false)+"(){");
return;
}
if(line_like("EXTERNAL SUB-PROCEDURE $external", tokens, state))
{
if(state.section_state != 2)
error("EXTERNAL SUB-PROCEDURE declaration outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if(state.closing_subprocedure())
error("Subprocedure declaration inside subprocedure (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
else if(state.closing_if())
error("Subprocedure declaration inside IF (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
else if(state.closing_while())
error("Subprocedure declaration inside WHILE (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
else
state.open_subprocedure();
//C Code
state.add_code("void "+fix_external_identifier(tokens[2], false)+"(){");
return;
}
if(line_like("RETURN", tokens, state))
{
if(state.section_state != 2)
error("RETURN outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if(!state.in_subprocedure)
error("RETURN found outside subprocedure (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.add_code("return;");
return;
}
if(line_like("END SUB-PROCEDURE", tokens, state))
{
if(state.section_state != 2)
error("END SUB-PROCEDURE outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if(!state.closing_subprocedure())
error("END SUB-PROCEDURE without SUB-PROCEDURE (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.add_code("}");
//Cierro la subrutina
state.close_subprocedure();
return;
}
if(line_like("IF $condition THEN", tokens, state))
{
if(state.section_state != 2)
error("IF outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.add_code("if (" + get_c_condition(state, vector<string>(tokens.begin()+1, tokens.end()-1)) + "){");
return;
}
if(line_like("END IF", tokens, state) || line_like("END-IF", tokens, state))
{
if(state.section_state != 2)
error("END IF outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if(!state.closing_if())
error("END IF without IF (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.close_if();
state.add_code("}");
return;
}
if(line_like("WHILE $condition DO", tokens, state))
{
if(state.section_state != 2)
error("WHILE outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.open_while();
state.add_code("while (" + get_c_condition(state, vector<string>(tokens.begin()+1, tokens.end()-1)) + "){");
return;
}
if(line_like("REPEAT", tokens, state))
{
if(state.section_state != 2)
error("REPEAT outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if(!state.closing_while())
error("REPEAT without WHILE (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.close_while();
state.add_code("}");
return;
}
if(line_like("BREAK", tokens, state))
{
if(state.section_state != 2)
error("BREAK outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if(state.open_whiles == 0)
error("BREAK without WHILE (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.add_code("break;");
return;
}
if(line_like("CONTINUE", tokens, state))
{
if(state.section_state != 2)
error("CONTINUE outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if(state.open_whiles == 0)
error("CONTINUE without WHILE (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.add_code("continue;");
return;
}
if(line_like("CALL SUB-PROCEDURE $subprocedure", tokens, state))
{
if(state.section_state != 2)
error("CALL SUB-PROCEDURE outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.add_code(fix_identifier(tokens[2], false) + "();");
return;
}
if(line_like("CALL EXTERNAL $external", tokens, state))
{
if(state.section_state != 2)
error("CALL EXTERNAL outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
state.add_code(fix_external_identifier(tokens[2], false) + "();");
//prototype of function defined in extension
state.add_var_code("void "+fix_external_identifier(tokens[2], false)+"();");
return;
}
if(line_like("EXECUTE $str-expr", tokens, state))
{
if(state.section_state != 2)
error("EXECUTE outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.add_code("system(" + get_c_char_array(state, tokens[1]) + ");");
return;
}
if(line_like("EXECUTE $str-expr AND STORE OUTPUT IN $str-var", tokens, state))
{
if(state.section_state != 2)
error("EXECUTE outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.add_code(get_c_variable(state, tokens[6]) + " = exec(" + get_c_char_array(state, tokens[1]) + ");");
return;
}
if(line_like("EXECUTE $str-expr AND STORE EXIT CODE IN $num-var", tokens, state))
{
if(state.section_state != 2)
error("EXECUTE outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.add_code(get_c_variable(state, tokens[7]) + " = system(" + get_c_char_array(state, tokens[1]) + ");");
return;
}
if(line_like("EXIT", tokens, state))
{
if(state.section_state != 2)
error("EXIT outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.add_code("exit(0);");
return;
}
if(line_like("STORE LENGTH OF $str-expr IN $num-var", tokens, state))
{
if(state.section_state != 2)
error("EXECUTE outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.add_code(get_c_variable(state, tokens[5]) + " = str_len(" + get_c_expression(state, tokens[3]) + ");");
return;
}
if(line_like("STORE RANDOM IN $num-var", tokens, state))
{
if(state.section_state != 2)
error("RANDOM outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.add_code(get_c_variable(state, tokens[3]) + " = get_random();");
return;
}
if(line_like("FLOOR $num-var", tokens, state))
{
if(state.section_state != 2)
error("FLOOR statement outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.add_code(get_c_variable(state, tokens[1]) + " = floor(" + get_c_variable(state, tokens[1]) +");");
return;
}
if(line_like("CEIL $num-var", tokens, state))
{
if(state.section_state != 2)
error("CEIL statement outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.add_code(get_c_variable(state, tokens[1]) + " = ceil(" + get_c_variable(state, tokens[1]) +");");
return;
}
if(line_like("STORE CHARACTER $num-expr IN $str-var", tokens, state))
{
if(state.section_state != 2)
error("STORE statement outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.add_code(get_c_variable(state, tokens[4]) + " = (char)" + get_c_expression(state, tokens[2]) + ";");
return;
}
if(line_like("STORE CHARACTER CODE OF $str-expr IN $num-var", tokens, state))
{
if(state.section_state != 2)
error("STORE statement outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.add_code(get_c_variable(state, tokens[6]) + " = get_char_num(" + get_c_expression(state, tokens[4]) + ");");
return;
}
if(line_like("STORE QUOTE IN $str-var", tokens, state))
{
if(state.section_state != 2)
error("STORE statement outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
state.open_quote = true;
//C code. More strings will get emitted
state.add_code(get_c_variable(state, tokens[3]) + " = \"\"");
return;
}
if(line_like("END QUOTE", tokens, state))
error("END QUOTE statement without preceding STORE QUOTE statement (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if(line_like("IN $str-var JOIN $display", tokens, state))
{
if(state.section_state != 2)
error("IN-JOIN statement outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if(tokens.size() < 5)
error("IN-JOIN expects at least two values to join (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.add_code("joinvar = \"\";");
for(unsigned int i = 3; i < tokens.size(); ++i){
state.add_code("join(joinvar, " + get_c_string(state, tokens[i]) + ", joinvar);");
}
state.add_code(get_c_variable(state, tokens[1]) + " = joinvar;");
return;
}
if(line_like("IN $num-var SOLVE $math", tokens, state))
{
if(state.section_state != 2)
error("IN-SOLVE statement outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
string code = "";
for(unsigned int i = 3; i < tokens.size(); ++i){
if(is_num_var(tokens[i], state))
code += " " + fix_identifier(tokens[i], true);
else
code += " " + tokens[i];
}
state.add_code(get_c_variable(state, tokens[1]) + " =" + code + ";");
return;
}
//REPLACE x FROM y WITH z IN w
if(line_like("REPLACE $str-var FROM $str-expr WITH $str-expr IN $str-var", tokens, state))
{
if(state.section_state != 2)
error("REPLACE statement outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
//TODO
return;
}
//GET INDEX OF x FROM y IN z
if(line_like("GET INDEX OF $str-expr FROM $str-expr IN $num-var", tokens, state))
{
if(state.section_state != 2)
error("GET INDEX OF statement outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
//TODO
return;
}
//COUNT x FROM y IN z
if(line_like("COUNT $str-expr FROM $str-expr IN $num-var", tokens, state))
{
if(state.section_state != 2)
error("COUNT statement outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
//TODO
return;
}
//CALL x
if(line_like("CALL $subprocedure", tokens, state))
{
if(state.section_state != 2)
error("CALL outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.add_code(fix_identifier(tokens[1], false) + "();");
return;
}
//WRITE x TO FILE y
if(line_like("WRITE $expression TO FILE $str-expr", tokens, state))
{
if(state.section_state != 2)
error("WRITE statement outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.add_code("file_writing_stream.open(" + get_c_expression(state, tokens[4]) + ", ios_base::out);");
state.add_code("file_writing_stream <<" + get_c_expression(state, tokens[1]) + ";");
state.add_code("file_writing_stream.close();");
return;
}
//APPEND x TO FILE y
if(line_like("APPEND $expression TO FILE $str-expr", tokens, state))
{
if(state.section_state != 2)
error("APPEND statement outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.add_code("file_writing_stream.open(" + get_c_expression(state, tokens[4]) + ", ios_base::app);");
state.add_code("file_writing_stream <<" + get_c_expression(state, tokens[1]) + ";");
state.add_code("file_writing_stream.close();");
return;
}
//LOAD FILE x IN y
if(line_like("LOAD FILE $str-expr IN $str-var", tokens, state))
{
if(state.section_state != 2)
error("LOAD FILE statement outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.add_code("load_file(" + get_c_expression(state, tokens[2]) + ", " + get_c_variable(state, tokens[4]) +");");
state.add_code(fix_identifier("FILE-LOADED", true) + " = fileOk;");
return;
}
//WAIT x MILLISECONDS
if(line_like("WAIT $num-expr MILLISECONDS", tokens, state))
{
if(state.section_state != 2)
error("WAIT statement outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C Code
state.add_code("std::this_thread::sleep_for(std::chrono::milliseconds((long int)" + get_c_expression(state, tokens[1]) + "));");
return;
}
if(line_like("LABEL $label", tokens, state))
{
if(state.section_state != 2)
error("LABEL outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
state.add_code("label_"+fix_identifier(tokens[1])+":");
return;
}
if(line_like("GOTO $label", tokens, state))
{
if(state.section_state != 2)
error("GOTO outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
state.add_code("goto label_"+fix_identifier(tokens[1])+";");
return;
}
error("Malformed statement (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
}
//External identifiers are used by C++ extensions and thus have a simpler but
//more restrictive name mangling algorithm: The only characters allowed are
//`A-Z`, `0-9`, and `_`. All other characters are converted to `_`.
string fix_external_identifier(string identifier, bool isVariable){
const string validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890:";
string new_id;
for(unsigned int i = 0; i < identifier.size(); ++i){
if(validChars.find(identifier[i]) != string::npos){
new_id += identifier[i];
}else{
new_id += "_";
}
}
return new_id;
}
//when state is provided to fix_identifier it can fix external variables too.
string fix_identifier(string ident, bool isVar, compiler_state & state){
if(is_external(ident, state)){
return fix_external_identifier(ident, isVar);
}else{
return fix_identifier(ident, isVar);
}
}
string fix_identifier(string identifier, bool isVariable){
return string(isVariable ? "VAR_" : "SUBPR_") + fix_identifier(identifier);
}
string fix_identifier(string identifier){
const string validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890:";
ostringstream new_id;
for(unsigned int i = 0; i < identifier.size(); ++i){
if(validChars.find(identifier[i]) != string::npos){
new_id << identifier[i];
}else{