Line data Source code
1 : #ifndef PARSER_H 2 : #define PARSER_H 3 : 4 : #include <iostream> 5 : #include <fstream> 6 : 7 : #include "config.hpp" 8 : #include "common/exprtype.hpp" 9 : #include "common/tables.hpp" 10 : #include "common/poliz.hpp" 11 : #include "common/stack.hpp" 12 : #include "compiler/cursor.hpp" 13 : #include "optimizer/optimizer.hpp" 14 : 15 : #define NEW_IDENT(IT, type, id, val, fs) { \ 16 : IdTable.pushType(type); \ 17 : IdTable.pushVal(val); \ 18 : IdTable.pushId(id); \ 19 : IdentTable * IT = IdTable.confirm(); \ 20 : for (int i = 0; i < *fs; i++) \ 21 : poliz.pushVal(IT); \ 22 : } 23 : 24 : #define BYTECODE_OP_BIN(op) { \ 25 : type_t left, right; \ 26 : if (types.size() != 0) { \ 27 : right = types.back(); \ 28 : types.pop_back(); \ 29 : } else right = _NONE_; \ 30 : if (types.size() != 0) { \ 31 : left = types.back(); \ 32 : types.pop_back(); \ 33 : } else left = _NONE_; \ 34 : poliz.pushOp(left, right, op); \ 35 : type_t restype = expressionType(left, right, op); \ 36 : if (restype != _NONE_) types.push_back(restype); \ 37 : } 38 : 39 : class Parser { 40 : std::ifstream code; // Код 41 : std::ofstream bin; // Бинарник 42 : Cursor c; // Курсор 43 : IdentTable IdTable; // Таблица идентификаторов 44 : POLIZ poliz; // ПОЛИЗ 45 : 46 : Stack exits; // Стек выходов из циклов 47 : Stack steps; // Стек входов в циклы 48 : StructTable StTable; // Таблица структур 49 : bool ok; // Произошла ли ошибка во время чтения программы 50 : bool inFunc; // Читает ли в данный момент тело функции 51 : bool inThread; // Читает ли в данный момент определение потока 52 : std::vector<std::pair<type_t, char*>> retTypes; // Тип возвращаемых параметров из функций 53 : std::vector<std::ifstream*> fileQueue; // Файлы в очереди к обработке 54 : IdentTable * threads[MAXTHREADS]; 55 : 56 : // Вспомогательные функции 57 : static int fastPow(int x, int n); // Быстрое возведение в степень 58 : bool readWord(const char * word); // Чтение непрерывной последовательности символов 59 : void revert(int x); // Возврат курсора 60 : public: 61 51 : Parser(): ok(true), inFunc(false), inThread(false) { 62 51 : c.line = 1; 63 255 : for (int i = 0; i < MAXTHREADS; i++) 64 204 : threads[i] = nullptr; 65 51 : }; 66 : 67 : void load(std::string name); // Загрузка исходного кода 68 : bool parse(void); // Разбор программы 69 : void include(void); 70 : void defs(void); // Определения 71 : IdentTable * def(void); // Определение переменных одного типа 72 : void defStruct(void); // Определение структуры 73 : void defFunction(void); // Определение функции 74 : 75 : bool typeModificator(void); // Модификатор типа 76 : bool type(void); // Тип 77 : IdentTable * variable(void);// Переменная 78 : char * identificator(void); // Идентификатор 79 : void constVal(void); // Инициализатор (константа) 80 : int constInt(void); // Целое число 81 : char * constString(void); // Строка 82 : float constReal(void); // Вещественное число 83 : bool constBool(void); // Логическая константа 84 : void constStruct(IdentTable * fields); // Структура 85 : IdentTable * getFieldInStruct(void); // Получить элемент структуры 86 : void callIdent(IdentTable * val); // Вызов объекта 87 : void assign(IdentTable * lval); // Присваивание 88 : void assignStruct(IdentTable * lval, IdentTable * rval); // Присваивание структур 89 : int unrollStruct(IdentTable * lval, int ord = -1); 90 : void handleStruct(type_t lval, type_t rval, operation_t op, int * fieldSize, char * structName); 91 : void program(void); // Программа 92 : 93 : void operations(void); // Операции 94 : void operation(void); // Операция 95 : IdentTable * saveLabel(char * label, int addr); // Сохранение метки 96 : type_t expr (int * fieldSize = nullptr, char * structName = nullptr); // Выражение ( a or b ) 97 : type_t andExpr (int * fieldSize = nullptr, char * structName = nullptr); // a and b 98 : type_t boolExpr (int * fieldSize = nullptr, char * structName = nullptr); // Сравнение 99 : type_t add (int * fieldSize = nullptr, char * structName = nullptr); // a +- b 100 : type_t mul (int * fieldSize = nullptr, char * structName = nullptr); // a */ b 101 : type_t constExpr(int * fieldSize = nullptr, char * structName = nullptr); // Константа или идентификатор 102 : void repack(int fieldSize); 103 : IdentTable * cycleparam(void); // Циклическое выражение 104 : 105 : void condOp(void); // if - else 106 : void forOp(void); // for 107 : void whileOp(void); // while 108 : void breakOp(void); // break 109 : void writeOp(void); // write 110 : void gotoOp(void); // goto 111 : void readOp(void); // read 112 : void continueOp(void); // continue 113 : void bytecodeOp(void); // bytecode 114 : void returnOp(void); 115 : void threadOp(void); 116 : void forkOp(void); 117 : void lockOp(void); 118 : 119 : void optimize(bool verbose); 120 : void giveBIN(const char * filename, bool optimize, bool printPoliz, bool verbose); // Запись бинарника 121 : 122 : ~Parser(void); 123 : }; 124 : 125 : #endif