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 | 1060c0e | 2021-12-09 09:46:42 -0800 | [diff] [blame] | 4 | #include <QDebug> |
| 5 | |
swissChili | 07d325f | 2021-12-08 20:02:05 -0800 | [diff] [blame] | 6 | template class TokenBase<Token>; |
| 7 | |
| 8 | // This is kind of ugly and breaks separation of concerns; but if I don't do |
| 9 | // this I have to define everything in headers which is worse. |
| 10 | template class TokenBase<AstNode>; |
| 11 | |
| 12 | template <typename T> |
| 13 | TokenBase<T>::TokenBase(const T &other) |
| 14 | { |
| 15 | _type = other._type; |
| 16 | _stringVal = other._stringVal; |
| 17 | _listVal = other._listVal; |
| 18 | _charVal = other._charVal; |
| 19 | } |
| 20 | |
| 21 | template <typename T> |
| 22 | TokenBase<T>::TokenBase(QChar symbol) |
| 23 | { |
| 24 | _type = SYM; |
| 25 | _charVal = symbol; |
| 26 | } |
| 27 | |
| 28 | template <typename T> |
| 29 | TokenBase<T>::TokenBase(QString identifier) |
| 30 | { |
| 31 | _type = IDENT; |
| 32 | _stringVal = identifier; |
| 33 | } |
| 34 | |
| 35 | template <typename T> |
| 36 | TokenBase<T>::TokenBase(QList<T> parenthesized) |
| 37 | { |
| 38 | _type = PAREN; |
| 39 | _listVal = std::move(parenthesized); |
| 40 | } |
| 41 | |
| 42 | template <typename T> |
swissChili | 9dddbf7 | 2021-12-08 23:03:25 -0800 | [diff] [blame] | 43 | TokenBase<T>::TokenBase(QString integer, int base) |
| 44 | { |
| 45 | _type = INTEGER; |
| 46 | _intVal = integer.toInt(nullptr, base); |
| 47 | } |
| 48 | |
| 49 | template <typename T> |
swissChili | 1060c0e | 2021-12-09 09:46:42 -0800 | [diff] [blame] | 50 | T TokenBase<T>::fromInteger(int integer) |
| 51 | { |
| 52 | T tok; |
| 53 | |
| 54 | tok._type = INTEGER; |
| 55 | tok._intVal = integer; |
| 56 | |
| 57 | return tok; |
| 58 | } |
| 59 | |
| 60 | template <typename T> |
swissChili | 07d325f | 2021-12-08 20:02:05 -0800 | [diff] [blame] | 61 | TokenBase<T>::TokenBase(char varType, const QString name) |
| 62 | { |
| 63 | _type = VAR; |
| 64 | _charVal = varType; |
| 65 | _stringVal = name; |
| 66 | } |
| 67 | |
| 68 | template <typename T> |
| 69 | bool TokenBase<T>::isSym() const |
| 70 | { |
| 71 | return _type == SYM; |
| 72 | } |
| 73 | |
| 74 | template <typename T> |
| 75 | bool TokenBase<T>::isIdent() const |
| 76 | { |
| 77 | return _type == IDENT; |
| 78 | } |
| 79 | |
| 80 | template <typename T> |
| 81 | bool TokenBase<T>::isParen() const |
| 82 | { |
| 83 | return _type == PAREN; |
| 84 | } |
| 85 | template <typename T> |
| 86 | bool TokenBase<T>::isVar() const |
| 87 | { |
| 88 | return _type == VAR; |
| 89 | } |
| 90 | |
| 91 | template <typename T> |
swissChili | 9dddbf7 | 2021-12-08 23:03:25 -0800 | [diff] [blame] | 92 | bool TokenBase<T>::isInteger() const |
| 93 | { |
| 94 | return _type == INTEGER; |
| 95 | } |
| 96 | |
| 97 | template <typename T> |
swissChili | 07d325f | 2021-12-08 20:02:05 -0800 | [diff] [blame] | 98 | TokenBase<T>::TokenBase() : TokenBase("Null") |
| 99 | { |
| 100 | } |
| 101 | |
| 102 | template <typename T> |
| 103 | bool TokenBase<T>::operator==(const T &other) const |
| 104 | { |
swissChili | 1060c0e | 2021-12-09 09:46:42 -0800 | [diff] [blame] | 105 | // Why is this needed? Beats me. |
| 106 | if (isInteger() && other.isInteger()) |
| 107 | return _intVal == other._intVal; |
| 108 | |
| 109 | return _type == other._type && |
| 110 | _stringVal == other._stringVal && |
| 111 | _charVal == other._charVal && |
| 112 | _listVal == other._listVal && |
| 113 | _intVal == other._intVal; |
swissChili | 07d325f | 2021-12-08 20:02:05 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | template <typename T> |
| 117 | QList<T> TokenBase<T>::parenContent() |
| 118 | { |
| 119 | if (isParen()) |
| 120 | { |
| 121 | return _listVal; |
| 122 | } |
| 123 | else |
| 124 | { |
| 125 | return {}; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | template <typename T> |
swissChili | 9dddbf7 | 2021-12-08 23:03:25 -0800 | [diff] [blame] | 130 | int TokenBase<T>::integer() const |
| 131 | { |
| 132 | return _intVal; |
| 133 | } |
| 134 | |
| 135 | template <typename T> |
swissChili | 07d325f | 2021-12-08 20:02:05 -0800 | [diff] [blame] | 136 | char TokenBase<T>::varType() const |
| 137 | { |
| 138 | return _charVal.toLatin1(); |
| 139 | } |
| 140 | |
| 141 | template <typename T> |
| 142 | const QString &TokenBase<T>::name() const |
| 143 | { |
| 144 | return _stringVal; |
| 145 | } |
| 146 | |
| 147 | template <typename T> |
| 148 | bool TokenBase<T>::operator!=(const T &other) const |
| 149 | { |
| 150 | return !(this->operator==(other)); |
| 151 | } |
| 152 | |
| 153 | template <typename T> |
| 154 | TokenBase<T>::operator QString() const |
| 155 | { |
| 156 | if (isIdent()) |
| 157 | return _stringVal; |
| 158 | if (isSym()) |
| 159 | return _charVal; |
| 160 | if (isVar()) |
| 161 | return QString(_charVal) + "." + _stringVal; |
swissChili | 9dddbf7 | 2021-12-08 23:03:25 -0800 | [diff] [blame] | 162 | if (isInteger()) |
| 163 | return QString::number(_intVal, 10); |
swissChili | 07d325f | 2021-12-08 20:02:05 -0800 | [diff] [blame] | 164 | if (isParen()) |
| 165 | { |
| 166 | QStringList parts; |
| 167 | for (const T &tok : _listVal) |
| 168 | { |
| 169 | parts.append(static_cast<QString>(tok)); |
| 170 | } |
| 171 | |
| 172 | return "(" + parts.join(" ") + ")"; |
| 173 | } |
| 174 | |
| 175 | return "Null"; |
| 176 | } |
| 177 | |
| 178 | template <typename T> |
| 179 | int TokenBase<T>::type() const |
| 180 | { |
| 181 | return _type; |
| 182 | } |
| 183 | |
| 184 | template <typename T> |
| 185 | QString TokenBase<T>::typeToString(int type) |
| 186 | { |
swissChili | 9dddbf7 | 2021-12-08 23:03:25 -0800 | [diff] [blame] | 187 | static const QString typeNames[] = {"SYMBOL", "IDENT", "PAREN", "VAR", "INTEGER"}; |
swissChili | 07d325f | 2021-12-08 20:02:05 -0800 | [diff] [blame] | 188 | return typeNames[type]; |
| 189 | } |
| 190 | |
| 191 | template <typename T> |
| 192 | QChar TokenBase<T>::symbol() const |
| 193 | { |
| 194 | return _charVal; |
| 195 | } |