Line data Source code
1 : #include "version.hpp" 2 : #include <iostream> 3 : #include <chrono> 4 : #include "compiler/parser.hpp" 5 : 6 : struct flags_t { 7 : bool optimize; // -O 8 : bool poliz; // -p 9 : bool verbose; // -v 10 : bool infile; 11 : bool outfile; 12 : }; 13 : 14 0 : void help(void) { 15 0 : std::cout << "Компилятор языка программирования Flower " << VERSION << std::endl; 16 0 : std::cout << "flc [флаги] ВХОДНОЙ_ФАЙЛ [ВЫХОДНОЙ_ФАЙЛ]"; 17 0 : std::cout << "Флаги командной строки:\n\t-O\tОптимизировать\n\t-p\tПечатать ПОЛИЗ\n\t"; 18 0 : std::cout << "-v\tВыводить сообщения\n"; 19 0 : } 20 : 21 51 : void compile(flags_t options, const std::string &ifname, const std::string &ofname) { 22 51 : auto start = std::chrono::steady_clock::now(); 23 : 24 102 : Parser pworker; 25 51 : pworker.load(ifname); 26 51 : bool ok = pworker.parse(); 27 : 28 51 : if (ok) { 29 29 : pworker.giveBIN(ofname.data(), options.optimize, options.poliz, options.verbose); 30 : 31 29 : auto end = std::chrono::steady_clock::now(); 32 29 : auto diff = end-start; 33 29 : int time = std::chrono::duration_cast<std::chrono::milliseconds>(diff).count(); 34 29 : std::cout << "КОМПИЛЯЦИЯ: " << time << " мс"<< std::endl; 35 : } else { 36 22 : std::cout << "ОШИБКА КОМПИЛЯЦИИ" << std::endl; 37 : } 38 51 : } 39 : 40 51 : int main(int argc, const char** argv) { 41 51 : struct flags_t flags = {false, false, false, false, false}; 42 102 : std::string inname, outname; 43 : 44 51 : setbuf(stdout, NULL); 45 51 : setbuf(stdin, NULL); 46 : 47 51 : if (argc > 1) { 48 182 : for (int i = 1; i < argc; i++) { 49 131 : if (argv[i][0] == '-') { 50 51 : switch (argv[i][1]) { 51 51 : case 'O': 52 51 : flags.optimize = true; 53 51 : break; 54 0 : case 'p': flags.poliz = true; break; 55 0 : case 'v': flags.verbose = true; break; 56 0 : default: 57 0 : std::cout << argv[i] << std::endl; 58 0 : case 'h': 59 0 : help(); 60 0 : exit(0); 61 : } 62 : } else { 63 80 : if (!flags.infile) { 64 51 : flags.infile = true; 65 51 : inname = argv[i]; 66 29 : } else if (!flags.outfile){ 67 29 : flags.outfile = true; 68 29 : outname = argv[i]; 69 : } else { 70 0 : help(); 71 0 : exit(1); 72 : } 73 : } 74 : } 75 : } 76 : 77 51 : if (!flags.infile) { 78 0 : std::cout << "Не указан входной файл." << std::endl; 79 0 : exit(-1); 80 : } 81 : 82 51 : if (!flags.outfile) { 83 22 : flags.outfile = true; 84 22 : outname = "out.bin"; 85 : } 86 : 87 51 : compile(flags, inname, outname); 88 : 89 51 : return 0; 90 : }