-
Notifications
You must be signed in to change notification settings - Fork 24
/
ldpl.cpp
2155 lines (2060 loc) 路 97.6 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;
}
vector<string> extensions; //C++ extensions to add to the file
vector<string> extension_flags; //Flags to pass to the C++ Compiler
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 << endl;
cout << " This is \033[32;1mLDPL version " << VERSION << "\033[0m '\033[32;1m" << VERSIONNAME << "\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;1mdvkt\033[0m, \033[35;1mDami谩n Garro\033[0m," << endl;
cout << " \033[35;1mfireasembler\033[0m, \033[35;1miglosiggio\033[0m and other wonderful contributors." << endl << endl;
cout << " The LDPL Home Page can be found at \033[36;1mwww.ldpl-lang.org\033[0m." << endl;
cout << " The LDPL source code is available at \033[36;1mwww.github.com/lartu/ldpl\033[0m." << endl << endl;
cout << " Complete documentation for LDPL should be found on this system" << endl;
cout << " using '\033[33;1mman ldpl\033[0m'. If you have access to the internet, the"<< endl;
cout << " documentation can also be found online at \033[36;1mdocs.ldpl-lang.org\033[0m." << endl << endl;
cout << " LDPL may be copied only under the terms of the Apache License" << endl;
cout << " Version 2.0, which may be found in the LDPL repository." << endl <<endl;
cout << " This binary was compiled on \033[31;1m" << COMPILEDATE << "\033[0m at \033[31;1m" << COMPILEHOUR << "\033[0m." << endl;
cout << endl;
return 0;
}
else if(contains_any(args, {"-h", "--help"})){
cout << endl;
cout << " \033[33;1mUsage:\033[0m" << endl;
cout << " ldpl [-i='<included file>']... <source file>|-c" << endl;
cout << " [-o='<output name>'|-r] [-f='<c++ flag>']... [-n]" << endl;
cout << " ldpl [-v|-h]" << endl;
cout << endl;
cout << " \033[33;1mOptions:\033[0m" << endl;
cout << " -v --version Display LDPL version information" << 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;
#ifdef STATIC_BUILDS
cout << " -n --non-static Disables static binary building" << endl;
#endif
cout << " -c Compile from standard input" << endl;
cout << endl;
cout << " Complete documentation for LDPL should be found on this system" << endl;
cout << " using '\033[33;1mman ldpl\033[0m'. If you have access to the internet, the"<< endl;
cout << " documentation can also be found online at \033[36;1mdocs.ldpl-lang.org\033[0m." << endl;
cout << endl;
return 0;
}
compiler_state state; //Compiler state (holds variables, sections, functions, etc)
vector<string> files_to_compile;
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;
if(files_to_compile.size() > 0){
warning("passing multiple LDPL source files to the\ncompiler is deprecated and may be removed in the future.\nPlease use the IMPORT statement instead.");
}
files_to_compile.push_back(arg);
}
else if(arg == "-r"){
show_ir = true;
}
#ifdef STATIC_BUILDS
else if(arg == "-n" || arg == "--non-static"){
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")){
if(files_to_compile.size() > 0){
warning("passing multiple LDPL source files to the\ncompiler is deprecated and may be removed in the future.\nPlease use the IMPORT statement instead.");
}
files_to_compile.push_back(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)
extension_flags.push_back(arg.substr(3));
}
else if(arg == "-c"){
files_to_compile.push_back(arg);
}
else{
cout << "Unknown option: " << arg << endl;
cout << "Try 'ldpl -h' for more information." << endl;
return 0;
}
}
}
state.add_code("int main(int argc, char *argv[]){");
state.add_code("cout.precision(numeric_limits<ldpl_number>::digits10);");
state.variables[""]["ARGV"] = {2, 3}; //List of text
state.add_var_code("ldpl_list<chText> "+fix_identifier("ARGV", true)+";");
state.variables[""]["ERRORCODE"] = {1}; //Declared in ldpl_lib.cpp
state.variables[""]["ERRORTEXT"] = {2}; //Declared in ldpl_lib.cpp
state.add_code("for(int i = 1; i < argc; ++i)");
state.add_code(fix_identifier("ARGV", true) + ".inner_collection.push_back(argv[i]);");
//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)
{
//Reset state section for this file
state.section_state = 0;
if(filename != "-c"){
bullet_msg("Loading " + filename);
load_and_compile(filename, state);
}else{
bullet_msg("Waiting for standard input...");
state.current_file = "standard input";
accept_and_compile(state);
}
if(state.section_state < 2) error("PROCEDURE section not found" + (filename == "-c" ? "." : " in file '" + filename + "'."));
}
state.add_code("return 0; \n}");
//Add include for extensions
if(!extensions.empty()){
for(string & extension : extensions){
bullet_msg("Including C++ extension " + extension);
state.add_code("#include \"" + extension + "\"");
}
}
//If an expected subprocedure was not declared, raise an error
if(state.expected_subprocedures.size() > 0)
error("the subprocedure " + state.expected_subprocedures[0].first + " is called but never declared.");
//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";
}
//Compile the C++ code
string compile_line = "c++ ldpl-temp.cpp -std=gnu++11 -w -o " + final_filename;
#ifdef STATIC_BUILDS
if(!no_static) compile_line+=" -static-libgcc -static-libstdc++ ";
#endif
if(!extension_flags.empty()){
for(string & flag : extension_flags){
bullet_msg("using C++ switch " + flag);
compile_line += " " + flag;
}
}
bullet_msg("Building " + final_filename);
int compiled = system(compile_line.c_str());
#if defined(_WIN32)
system("del ldpl-temp.cpp");
#else
system("rm ldpl-temp.cpp");
#endif
if(compiled == 0){
bullet_msg("Saved as " + final_filename);
bullet_msg("\033[32;1mFile(s) compiled successfully.\033[0m");
}else{
error("compilation failed.");
}
}
#include <array>
string exec(const char* cmd) {
array<char, 128> buffer;
string result;
unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
if (!pipe) {
throw runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
}
string expandHomeDirectory(string & filename){
#if defined(_WIN32)
return filename;
#else
string homeDir = exec("echo $HOME");
trim(homeDir);
string newPath = "";
for(size_t i = 0; i < filename.length(); ++i){
if(filename[i] == '~'){
newPath += homeDir;
}else{
newPath += filename[i];
}
}
return newPath;
#endif
}
void load_and_compile(string & filename, compiler_state & state)
{
filename = expandHomeDirectory(filename);
state.current_file = filename;
//Accept input from stdin
ifstream file(filename);
//Fail if the file couldn't be loaded
if(!file.is_open()) error("The file '" + filename + "' couldn't be loaded.");
//Get file contents
vector<string> lines;
string line = "";
while(getline(file, line))
{
replace_whitespace(line);
lines.push_back(line);
}
bullet_msg("Compiling " + filename);
compile(lines, state);
}
void accept_and_compile(compiler_state & state){
//Get file contents
vector<string> lines;
string line = "";
while(getline(cin, line))
{
replace_whitespace(line);
lines.push_back(line);
}
bullet_msg("Compiling");
compile(lines, state);
}
//Replace all whitespace within string
void replace_whitespace(string & code)
{
for(char & c : code){
if(isspace(c)){
c = ' ';
}
}
}
//Shows message preceded by a *
void bullet_msg(const string & msg)
{
cerr << "\033[1;33m*\033[0m ";
cerr << msg << endl;
}
//Shows warning message
void warning(const string & msg)
{
cerr << "\033[1;35mLDPL Warning: ";
cerr << msg;
cerr << "\033[0m" << endl;
}
//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.current_subprocedure != "" ? state.subroutine_code.back() : 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, true, ' ');
for(string & token : tokens) if(token == "CRLF" || token == "LF") token = "\"\\n\"";
if(tokens.size() == 0) continue;
compile_line(tokens, line_num, state);
}
if(state.open_quote) error("a QUOTE block was not terminated.");
if(state.closing_subprocedure()) error("a SUB-PROCEDURE block was not terminated.");
if(state.closing_if()) error("a IF block was not terminated.");
if(state.closing_loop()) error("a WHILE or FOR block was not terminated.");
}
//Tokenizes a line splitting by 'splitChar' with optional convertion of tokens to uppercase (except in strings)
void tokenize(string & line, unsigned int line_num, vector<string> & tokens, string & current_file, bool uppercase, char splitChar)
{
++line_num;
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 == splitChar)
{
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 == '\\' && 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 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 += (uppercase && !in_string)? toupper(letter) : letter;
}
if(i == line.size() - 1 && letter != splitChar)
{
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);
}
}
}
//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;
//include
if(line_like("INCLUDE $string", tokens, state)) {
if(state.section_state != 0)
error("you can only use the INCLUDE statement before the DATA and PROCEDURE sections (\033[0m" + current_file + ":" + to_string(line_num)+"\033[1;31m)");
else {
string file_to_compile = tokens[1].substr(1, tokens[1].size() - 2);
string separators = "/";
#if defined(_WIN32)
separators += "\\";
#endif
size_t last_sep = current_file.find_last_of(separators);
if (last_sep != string::npos)
file_to_compile = current_file.substr(0, last_sep) + "/" + file_to_compile;
load_and_compile(file_to_compile, state);
state.section_state = 0;
state.current_file = current_file;
}
return;
}
//std include
if(line_like("USING PACKAGE $name", tokens, state)) {
if(state.section_state != 0)
error("you can only use the USING PACKAGE statement before the DATA and PROCEDURE sections (\033[0m" + current_file + ":" + to_string(line_num)+"\033[1;31m)");
else {
#if defined(_WIN32)
//TODO!
error("LPM packages are not yet supported on Windows.");
#else
system(((string)"mkdir -p " + LPMLOCATION).c_str());
#endif
string libFilename = tokens[2];
std::for_each(libFilename.begin(), libFilename.end(), [](char & c){
c = ::tolower(c);
});
string file_to_compile = LPMLOCATION + libFilename + "/" + libFilename + ".ldpl";
load_and_compile(file_to_compile, state);
state.section_state = 0;
state.current_file = current_file;
}
return;
}
//extension (INCLUDE but for c++ extensions)
if(line_like("EXTENSION $string", tokens, state)) {
if(state.section_state != 0)
error("you can only use the EXTENSION statement before the DATA and PROCEDURE sections (\033[0m" + current_file + ":" + to_string(line_num)+"\033[1;31m)");
else {
string file_to_add = tokens[1].substr(1, tokens[1].size() - 2);
string separators = "/";
#if defined(_WIN32)
separators += "\\";
#endif
size_t last_sep = current_file.find_last_of(separators);
if (last_sep != string::npos)
file_to_add = current_file.substr(0, last_sep) + "/" + file_to_add;
extensions.push_back(file_to_add);
}
return;
}
//extension flags (for the C++ compiler)
if(line_like("FLAG $string", tokens, state)) {
if(state.section_state != 0)
error("you can only use the FLAG statement before the DATA and PROCEDURE sections (\033[0m" + current_file + ":" + to_string(line_num)+"\033[1;31m)");
else {
string flag = tokens[1].substr(1, tokens[1].size() - 2);
extension_flags.push_back(flag);
}
return;
}
//os-specific extension flags
if(line_like("FLAG $name $string", tokens, state)) {
if(state.section_state != 0)
error("you can only use the FLAG statement before the DATA and PROCEDURE sections (\033[0m" + current_file + ":" + to_string(line_num)+"\033[1;31m)");
else {
if (tokens[1] == current_os()) {
string flag = tokens[2].substr(1, tokens[2].size() - 2);
extension_flags.push_back(flag);
}
}
return;
}
// Sections
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 PROCEDURE section declaration (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if (state.current_subprocedure != "" && state.section_state >= 3)
open_subprocedure_code(state, line_num, current_file);
state.section_state = 2;
return;
}
if(line_like("PARAMETERS:", tokens, state))
{
if(state.current_subprocedure == "")
error("PARAMETERS section outside subprocedure (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if(state.section_state == 4)
error("Duplicate PARAMETERS section declaration (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if(state.section_state == 1)
error("PARAMETERS section declaration within LOCAL DATA section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if(state.section_state == 2)
error("PARAMETERS section declaration within PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
state.section_state = 4;
return;
}
if(line_like("LOCAL DATA:", tokens, state))
{
if(state.current_subprocedure == "")
error("LOCAL DATA section outside subprocedure (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if(state.section_state == 1)
error("Duplicate LOCAL DATA section declaration (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if(state.section_state == 2)
error("LOCAL DATA section declaration within PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
state.section_state = 1;
open_subprocedure_code(state, line_num, current_file);
return;
}
// Variable Declaration
if(line_like("$name IS $anything", tokens, state)) // If it's a variable declaration
{
string extern_keyword = ""; // C++ extern keyword to prepend to the type (empty if not EXTERNAL)
vector<unsigned int> type_number; // All data types in LDPL have a list of numbers that represent its type
string assign_default; // Default Value to asign to the variable
size_t i = 2; // i is used to check all the words after 'IS' and thus starts in 2.
bool valid_type = true; // Used to check if it's a valid tipe
if (tokens[i] == "EXTERNAL" && state.current_subprocedure == "") { // EXTERNAL is only valid in DATA section (not in LOCAL DATA)
state.externals[tokens[0]] = true; // Add it to the list of external variables
extern_keyword = "extern "; // Set the prepended keyword to 'extern'
++i; // Check the next word.
}
if (tokens[i] == "NUMBER") { // If it's a number...
type_number.push_back(1); // Then the type number is 1
if (extern_keyword == "") assign_default = " = 0"; // if its not an external variable, set a default value for it.
} else if (tokens[i] == "TEXT") { // If we are dealing with a text variable...
type_number.push_back(2); // The type number is 2
if (extern_keyword == "") assign_default = " = \"\""; // And if it's not external, we set it to a default value.
} else if ((tokens[i]=="MAP"||tokens[i]=="LIST") && tokens.size() > i && tokens[i+1] == "OF") {
// nested 'of' syntax, ex: map of list of number
while (valid_type && i < tokens.size()) {
if (i % 2 == 1) {
if (tokens[i] != "OF")
valid_type = false;
} else if (i % 2 == 0) {
if (tokens[i] == "MAP"||(tokens[i] == "MAPS" && i>0)) {
type_number.push_back(4);
} else if (tokens[i] == "LIST"||(tokens[i] == "LISTS" && i>0)) {
type_number.push_back(3);
} else if (tokens.size()-1 > i) {
// text and number must be the final type listed
valid_type = false;
} else if (tokens[i] == "TEXT") {
type_number.push_back(2);
} else if (tokens[i]=="NUMBER"||tokens[i]=="NUMBERS") {
type_number.push_back(1);
} else {
valid_type = false;
}
} else {
valid_type = false;
}
++i;
}
reverse(begin(type_number), end(type_number));
} else {
valid_type = false; // If its not a NUMBER, a TEXT or a collection of these data types
} // then it's not a valid LDPL data type.
++i; // Move to the next keyword.
while (valid_type && i < tokens.size()) { // If up to this point we got a valid data type, we check for containers.
assign_default = ""; // Collections are not initialized with any default values.
if (tokens[i] == "MAP" || tokens[i] == "VECTOR") { // If its a MAP (aka VECTOR)
type_number.push_back(4); // We add the MAP type (4) to its type list
} else if (tokens[i] == "LIST") { // If its a LIST
type_number.push_back(3); // We add the LIST type (3) to its type list
} else { // If the container is not a VECTOR nor a MAP
valid_type = false; // then it's not a valid data type.
}
++i; // Move to the next keyword.
}
if (valid_type && i >= tokens.size() - 1) {
if(state.section_state != 1 && state.section_state != 4)
error("Variable declaration outside DATA, PARAMETERS or LOCAL DATA section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if(state.variables[state.current_subprocedure].count(tokens[0]) > 0)
error("Duplicate declaration for variable " + tokens[0] + " (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
state.variables[state.current_subprocedure][tokens[0]] = type_number;
if (state.section_state == 1) { // DATA or LOCAL DATA
string identifier = fix_identifier(tokens[0], true, state);
string type = state.get_c_type(type_number);
string code = extern_keyword + type + " " + identifier + assign_default + ";";
if (state.current_subprocedure == "") // DATA
state.add_var_code(code);
else
state.add_code(code); // LOCAL DATA
} else // PARAMETERS
state.subprocedures[state.current_subprocedure].emplace_back(tokens[0]);
return;
}
}
// SUB-PROCEDURE Declaration
if(line_like("SUB-PROCEDURE $name", tokens, state) || line_like("SUB $name", tokens, state))
{
if(!in_procedure_section(state, line_num, current_file))
error("SUB-PROCEDURE declaration outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if(is_subprocedure(tokens[1], state))
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_loop())
error("Subprocedure declaration inside WHILE or FOR (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
state.section_state = 3;
state.open_subprocedure(tokens[1]);
state.subprocedures.emplace(tokens[1], vector<string>());
return;
}
if(line_like("EXTERNAL SUB-PROCEDURE $external", tokens, state) || line_like("EXTERNAL SUB $external", tokens, state))
{
if(!in_procedure_section(state, line_num, current_file))
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_loop())
error("Subprocedure declaration inside WHILE or FOR (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
else
state.open_subprocedure(tokens[2]);
//C++ Code
state.add_code("void "+fix_external_identifier(tokens[2], false)+"(){");
return;
}
if(line_like("END SUB-PROCEDURE", tokens, state) || line_like("END SUB", tokens, state))
{
if(!in_procedure_section(state, line_num, current_file))
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;
}
// Control Flow Statements
if(line_like("STORE $expression IN $var", tokens, state))
{
if(!in_procedure_section(state, line_num, current_file))
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("IF $condition THEN", tokens, state))
{
string condition = get_c_condition(state, vector<string>(tokens.begin()+1, tokens.end()-1));
if (condition != "[ERROR]") {
if(!in_procedure_section(state, line_num, current_file))
error("IF outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C++ Code
state.open_if();
state.add_code("if (" + condition + "){");
return;
}
}
if(line_like("ELSE IF $condition THEN", tokens, state))
{
string condition = get_c_condition(state, vector<string>(tokens.begin()+2, tokens.end()-1));
if (condition != "[ERROR]") {
if(!in_procedure_section(state, line_num, current_file))
error("ELSE IF outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if(!state.closing_if())
error("ELSE IF without IF (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C++ Code
state.add_code("} else if (" + condition + "){");
return;
}
}
if(line_like("ELSE", tokens, state))
{
if(!in_procedure_section(state, line_num, current_file))
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)");
//C++ Code
state.open_else();
state.add_code("}else{");
return;
}
if(line_like("END IF", tokens, state) || line_like("END-IF", tokens, state))
{
if(!in_procedure_section(state, line_num, current_file))
error("END IF outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if(!state.closing_if() && !state.closing_else())
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))
{
string condition = get_c_condition(state, vector<string>(tokens.begin()+1, tokens.end()-1));
if (condition != "[ERROR]") {
if(!in_procedure_section(state, line_num, current_file))
error("WHILE outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C++ Code
state.open_loop();
state.add_code("while (" + condition + "){");
return;
}
}
if(line_like("REPEAT", tokens, state))
{
if(!in_procedure_section(state, line_num, current_file))
error("REPEAT outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if(!state.closing_loop())
error("REPEAT without WHILE or FOR (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C++ Code
state.close_loop();
state.add_code("}");
return;
}
if(line_like("FOR $num-var FROM $num-expr TO $num-expr STEP $num-expr DO", tokens, state))
{
if(!in_procedure_section(state, line_num, current_file))
error("FOR outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
state.open_loop();
string var = get_c_variable(state, tokens[1]);
string from = get_c_expression(state, tokens[3]);
string to = get_c_expression(state, tokens[5]);
string step = get_c_expression(state, tokens[7]);
string init = var + " = " + from;
string condition = step + " >= 0 ? " +
var + " < " + to + " : " + var + " > " + to;
string increment = var + " += " + step;
//C++ Code
state.add_code("for (" + init + "; " + condition + "; " + increment + ") {");
return;
}
if(line_like("FOR EACH $anyVar IN $collection DO", tokens, state))
{
if(!in_procedure_section(state, line_num, current_file))
error("FOR EACH outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
vector<unsigned int> iteration_type = variable_type(tokens[2], state);
vector<unsigned int> collected_type = variable_type(tokens[4], state);
unsigned int collection_type = collected_type.back(); // LIST or MAP
collected_type.pop_back();
if(iteration_type != collected_type)
error("FOR EACH iteration variable type doesn't match collection type (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
state.open_loop();
//C Code
string range_var = state.new_range_var();
string collection = get_c_variable(state, tokens[4]) + ".inner_collection";
string iteration_var = range_var;
if (collection_type == 4) {
iteration_var += ".second";
}
state.add_code("for (auto& " + range_var + " : " + collection + ") {");
state.add_code(get_c_variable(state, tokens[2]) + " = " + iteration_var + ";");
return;
}
if(line_like("BREAK", tokens, state))
{
if(!in_procedure_section(state, line_num, current_file))
error("BREAK outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if(state.open_loops == 0)
error("BREAK without WHILE or FOR (\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(!in_procedure_section(state, line_num, current_file))
error("CONTINUE outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if(state.open_loops == 0)
error("CONTINUE without WHILE or FOR (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C++ Code
state.add_code("continue;");
return;
}
if(line_like("CALL SUB-PROCEDURE $anything", tokens, state) || line_like("CALL $anything", tokens, state))
{
size_t i = 1;
if(tokens[i] == "SUB-PROCEDURE") i++;
string subprocedure = tokens[i];
// Valid options: No WITH or WITH with at least one paramter
if (i == tokens.size()-1 || (i < tokens.size()-2 && tokens[i+1] == "WITH")) {
if(!in_procedure_section(state, line_num, current_file))
error("CALL outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
vector<string> parameters(i != tokens.size()-1 ? tokens.begin() + i + 2 : tokens.end(), tokens.end());
vector<vector<unsigned int>> types;
for (string & parameter : parameters) {
if (is_number(parameter))
types.push_back({1});
else if(is_string(parameter))
types.push_back({2});
else if(variable_exists(parameter, state))
types.push_back(variable_type(parameter, state));
else
error("CALL with invalid parameter \"" + parameter + "\" (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
}
bool correct_types = state.correct_subprocedure_types(subprocedure, types);
if(!is_subprocedure(subprocedure, state)) {
if(!correct_types)
error("CALL parameter types doesn't match previous CALL (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
state.add_expected_subprocedure(subprocedure, fix_identifier(subprocedure, false), types);
} else {
if(!correct_types)
error("CALL parameter types doesn't match SUB-PROCEDURE declaration (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
}
add_call_code(subprocedure, parameters, state, line_num);
return;
}
}
if(line_like("CALL EXTERNAL $external", tokens, state))
{
if(!in_procedure_section(state, line_num, current_file))
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("RETURN", tokens, state))
{
if(!in_procedure_section(state, line_num, current_file))
error("RETURN outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
if(state.current_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("EXIT", tokens, state))
{
if(!in_procedure_section(state, line_num, current_file))
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("WAIT $num-expr MILLISECONDS", tokens, state))
{
if(!in_procedure_section(state, line_num, current_file))
error("WAIT statement outside PROCEDURE section (\033[0m" + current_file + ":"+ to_string(line_num)+"\033[1;31m)");
//C++ Code
#if defined(_WIN32)
state.add_code("_sleep((long int)" + get_c_expression(state, tokens[1]) + ");");
#else
state.add_code("std::this_thread::sleep_for(std::chrono::milliseconds((long int)" + get_c_expression(state, tokens[1]) + "));");
#endif
return;
}
if(line_like("GOTO $label", tokens, state))
{
if(!in_procedure_section(state, line_num, current_file))
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;
}
if(line_like("LABEL $label", tokens, state))
{
if(!in_procedure_section(state, line_num, current_file))
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;
}
// Arithmetic Statements
if(line_like("MODULO $num-expr BY $num-expr IN $num-var", tokens, state)) //TODO move this into the standard library
{
if(!in_procedure_section(state, line_num, current_file))
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("GET RANDOM IN $num-var", tokens, state))
{
if(!in_procedure_section(state, line_num, current_file))
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(!in_procedure_section(state, line_num, current_file))
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("IN $num-var SOLVE $math", tokens, state))
{
if(!in_procedure_section(state, line_num, current_file))
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 += " " + get_c_variable(state, tokens[i]);
else if(is_txt_expr(tokens[i], state))
code += " " + get_c_number(state, tokens[i]);
else
code += " " + tokens[i];
}
state.add_code(get_c_variable(state, tokens[1]) + " =" + code + ";");
return;
}
// Text Statements
if(line_like("JOIN $expression AND $expression IN $str-var", tokens, state))
{
if(!in_procedure_section(state, line_num, current_file))
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(!in_procedure_section(state, line_num, current_file))
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("GET LENGTH OF $str-expr IN $num-var", tokens, state))
{
if(!in_procedure_section(state, line_num, current_file))
error("GET LENGTH OF 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]) + " = ((chText)" + get_c_expression(state, tokens[3]) + ").size();");
return;
}
if(line_like("GET ASCII CHARACTER $num-expr IN $str-var", tokens, state))
{
if(!in_procedure_section(state, line_num, current_file))