swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame] | 1 | #include "Token.h" |
swissChili | 07d325f | 2021-12-08 20:02:05 -0800 | [diff] [blame] | 2 | #include "AstNode.h" |
swissChili | 7babd92 | 2021-12-02 22:46:48 -0800 | [diff] [blame] | 3 | |
swissChili | 07d325f | 2021-12-08 20:02:05 -0800 | [diff] [blame] | 4 | template class TokenBase<Token>; |
| 5 | |
| 6 | // This is kind of ugly and breaks separation of concerns; but if I don't do |
| 7 | // this I have to define everything in headers which is worse. |
| 8 | template class TokenBase<AstNode>; |
| 9 | |
| 10 | template <typename T> |
| 11 | TokenBase<T>::TokenBase(const T &other) |
| 12 | { |
| 13 | _type = other._type; |
| 14 | _stringVal = other._stringVal; |
| 15 | _listVal = other._listVal; |
| 16 | _charVal = other._charVal; |
| 17 | } |
| 18 | |
| 19 | template <typename T> |
| 20 | TokenBase<T>::TokenBase(QChar symbol) |
| 21 | { |
| 22 | _type = SYM; |
| 23 | _charVal = symbol; |
| 24 | } |
| 25 | |
| 26 | template <typename T> |
| 27 | TokenBase<T>::TokenBase(QString identifier) |
| 28 | { |
| 29 | _type = IDENT; |
| 30 | _stringVal = identifier; |
| 31 | } |
| 32 | |
| 33 | template <typename T> |
| 34 | TokenBase<T>::TokenBase(QList<T> parenthesized) |
| 35 | { |
| 36 | _type = PAREN; |
| 37 | _listVal = std::move(parenthesized); |
| 38 | } |
| 39 | |
| 40 | template <typename T> |
| 41 | TokenBase<T>::TokenBase(char varType, const QString name) |
| 42 | { |
| 43 | _type = VAR; |
| 44 | _charVal = varType; |
| 45 | _stringVal = name; |
| 46 | } |
| 47 | |
| 48 | template <typename T> |
| 49 | bool TokenBase<T>::isSym() const |
| 50 | { |
| 51 | return _type == SYM; |
| 52 | } |
| 53 | |
| 54 | template <typename T> |
| 55 | bool TokenBase<T>::isIdent() const |
| 56 | { |
| 57 | return _type == IDENT; |
| 58 | } |
| 59 | |
| 60 | template <typename T> |
| 61 | bool TokenBase<T>::isParen() const |
| 62 | { |
| 63 | return _type == PAREN; |
| 64 | } |
| 65 | template <typename T> |
| 66 | bool TokenBase<T>::isVar() const |
| 67 | { |
| 68 | return _type == VAR; |
| 69 | } |
| 70 | |
| 71 | template <typename T> |
| 72 | TokenBase<T>::TokenBase() : TokenBase("Null") |
| 73 | { |
| 74 | } |
| 75 | |
| 76 | template <typename T> |
| 77 | bool TokenBase<T>::operator==(const T &other) const |
| 78 | { |
| 79 | return _type == other._type && _stringVal == other._stringVal && _charVal == other._charVal && _listVal == other._listVal; |
| 80 | } |
| 81 | |
| 82 | template <typename T> |
| 83 | QList<T> TokenBase<T>::parenContent() |
| 84 | { |
| 85 | if (isParen()) |
| 86 | { |
| 87 | return _listVal; |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | return {}; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | template <typename T> |
| 96 | char TokenBase<T>::varType() const |
| 97 | { |
| 98 | return _charVal.toLatin1(); |
| 99 | } |
| 100 | |
| 101 | template <typename T> |
| 102 | const QString &TokenBase<T>::name() const |
| 103 | { |
| 104 | return _stringVal; |
| 105 | } |
| 106 | |
| 107 | template <typename T> |
| 108 | bool TokenBase<T>::operator!=(const T &other) const |
| 109 | { |
| 110 | return !(this->operator==(other)); |
| 111 | } |
| 112 | |
| 113 | template <typename T> |
| 114 | TokenBase<T>::operator QString() const |
| 115 | { |
| 116 | if (isIdent()) |
| 117 | return _stringVal; |
| 118 | if (isSym()) |
| 119 | return _charVal; |
| 120 | if (isVar()) |
| 121 | return QString(_charVal) + "." + _stringVal; |
| 122 | if (isParen()) |
| 123 | { |
| 124 | QStringList parts; |
| 125 | for (const T &tok : _listVal) |
| 126 | { |
| 127 | parts.append(static_cast<QString>(tok)); |
| 128 | } |
| 129 | |
| 130 | return "(" + parts.join(" ") + ")"; |
| 131 | } |
| 132 | |
| 133 | return "Null"; |
| 134 | } |
| 135 | |
| 136 | template <typename T> |
| 137 | int TokenBase<T>::type() const |
| 138 | { |
| 139 | return _type; |
| 140 | } |
| 141 | |
| 142 | template <typename T> |
| 143 | QString TokenBase<T>::typeToString(int type) |
| 144 | { |
| 145 | static const QString typeNames[] = {"SYMBOL", "IDENT", "PAREN", "VAR"}; |
| 146 | return typeNames[type]; |
| 147 | } |
| 148 | |
| 149 | template <typename T> |
| 150 | QChar TokenBase<T>::symbol() const |
| 151 | { |
| 152 | return _charVal; |
| 153 | } |