swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame^] | 1 | #include "Token.h" |
| 2 | |
| 3 | Token::Token(const Token &other) { |
| 4 | *this = other; |
| 5 | } |
| 6 | |
| 7 | Token::Token(QChar symbol) { |
| 8 | _type = SYM; |
| 9 | _charVal = symbol; |
| 10 | } |
| 11 | |
| 12 | Token::Token(QString &&identifier) { |
| 13 | _type = IDENT; |
| 14 | _stringVal = identifier; |
| 15 | } |
| 16 | |
| 17 | Token::Token(QList<Token> &&parenthesized) { |
| 18 | _type = PAREN; |
| 19 | _listVal = new QList<Token>(parenthesized); |
| 20 | } |
| 21 | |
| 22 | Token::Token(char varType, const QString &&name) { |
| 23 | _type = VAR; |
| 24 | _charVal = varType; |
| 25 | _stringVal = name; |
| 26 | } |
| 27 | |
| 28 | Token::~Token() { |
| 29 | // Стерать нулевые пойнтеры безопасно |
| 30 | delete _listVal; |
| 31 | } |
| 32 | |
| 33 | bool Token::isSym() { |
| 34 | return _type == SYM; |
| 35 | } |
| 36 | |
| 37 | bool Token::isIdent() { |
| 38 | return _type == IDENT; |
| 39 | } |
| 40 | |
| 41 | bool Token::isParen() { |
| 42 | return _type == PAREN; |
| 43 | } |
| 44 | |
| 45 | bool Token::isVar() { |
| 46 | return _type == VAR; |
| 47 | } |
| 48 | |
| 49 | Token::Token() : Token("Null") { |
| 50 | } |
| 51 | |
| 52 | bool Token::operator==(const Token &other) { |
| 53 | return _type == other._type |
| 54 | && _stringVal == other._stringVal |
| 55 | && _charVal == other._charVal |
| 56 | && (_listVal == nullptr || *_listVal == (*other._listVal)); |
| 57 | } |
| 58 | |
| 59 | QList<Token> Token::parenContent() { |
| 60 | if (isParen() && _listVal) { |
| 61 | return *_listVal; |
| 62 | } else { |
| 63 | return {}; |
| 64 | } |
| 65 | } |