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> |
swissChili | 9dddbf7 | 2021-12-08 23:03:25 -0800 | [diff] [blame^] | 41 | TokenBase<T>::TokenBase(QString integer, int base) |
| 42 | { |
| 43 | _type = INTEGER; |
| 44 | _intVal = integer.toInt(nullptr, base); |
| 45 | } |
| 46 | |
| 47 | template <typename T> |
swissChili | 07d325f | 2021-12-08 20:02:05 -0800 | [diff] [blame] | 48 | TokenBase<T>::TokenBase(char varType, const QString name) |
| 49 | { |
| 50 | _type = VAR; |
| 51 | _charVal = varType; |
| 52 | _stringVal = name; |
| 53 | } |
| 54 | |
| 55 | template <typename T> |
| 56 | bool TokenBase<T>::isSym() const |
| 57 | { |
| 58 | return _type == SYM; |
| 59 | } |
| 60 | |
| 61 | template <typename T> |
| 62 | bool TokenBase<T>::isIdent() const |
| 63 | { |
| 64 | return _type == IDENT; |
| 65 | } |
| 66 | |
| 67 | template <typename T> |
| 68 | bool TokenBase<T>::isParen() const |
| 69 | { |
| 70 | return _type == PAREN; |
| 71 | } |
| 72 | template <typename T> |
| 73 | bool TokenBase<T>::isVar() const |
| 74 | { |
| 75 | return _type == VAR; |
| 76 | } |
| 77 | |
| 78 | template <typename T> |
swissChili | 9dddbf7 | 2021-12-08 23:03:25 -0800 | [diff] [blame^] | 79 | bool TokenBase<T>::isInteger() const |
| 80 | { |
| 81 | return _type == INTEGER; |
| 82 | } |
| 83 | |
| 84 | template <typename T> |
swissChili | 07d325f | 2021-12-08 20:02:05 -0800 | [diff] [blame] | 85 | TokenBase<T>::TokenBase() : TokenBase("Null") |
| 86 | { |
| 87 | } |
| 88 | |
| 89 | template <typename T> |
| 90 | bool TokenBase<T>::operator==(const T &other) const |
| 91 | { |
| 92 | return _type == other._type && _stringVal == other._stringVal && _charVal == other._charVal && _listVal == other._listVal; |
| 93 | } |
| 94 | |
| 95 | template <typename T> |
| 96 | QList<T> TokenBase<T>::parenContent() |
| 97 | { |
| 98 | if (isParen()) |
| 99 | { |
| 100 | return _listVal; |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | return {}; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | template <typename T> |
swissChili | 9dddbf7 | 2021-12-08 23:03:25 -0800 | [diff] [blame^] | 109 | int TokenBase<T>::integer() const |
| 110 | { |
| 111 | return _intVal; |
| 112 | } |
| 113 | |
| 114 | template <typename T> |
swissChili | 07d325f | 2021-12-08 20:02:05 -0800 | [diff] [blame] | 115 | char TokenBase<T>::varType() const |
| 116 | { |
| 117 | return _charVal.toLatin1(); |
| 118 | } |
| 119 | |
| 120 | template <typename T> |
| 121 | const QString &TokenBase<T>::name() const |
| 122 | { |
| 123 | return _stringVal; |
| 124 | } |
| 125 | |
| 126 | template <typename T> |
| 127 | bool TokenBase<T>::operator!=(const T &other) const |
| 128 | { |
| 129 | return !(this->operator==(other)); |
| 130 | } |
| 131 | |
| 132 | template <typename T> |
| 133 | TokenBase<T>::operator QString() const |
| 134 | { |
| 135 | if (isIdent()) |
| 136 | return _stringVal; |
| 137 | if (isSym()) |
| 138 | return _charVal; |
| 139 | if (isVar()) |
| 140 | return QString(_charVal) + "." + _stringVal; |
swissChili | 9dddbf7 | 2021-12-08 23:03:25 -0800 | [diff] [blame^] | 141 | if (isInteger()) |
| 142 | return QString::number(_intVal, 10); |
swissChili | 07d325f | 2021-12-08 20:02:05 -0800 | [diff] [blame] | 143 | if (isParen()) |
| 144 | { |
| 145 | QStringList parts; |
| 146 | for (const T &tok : _listVal) |
| 147 | { |
| 148 | parts.append(static_cast<QString>(tok)); |
| 149 | } |
| 150 | |
| 151 | return "(" + parts.join(" ") + ")"; |
| 152 | } |
| 153 | |
| 154 | return "Null"; |
| 155 | } |
| 156 | |
| 157 | template <typename T> |
| 158 | int TokenBase<T>::type() const |
| 159 | { |
| 160 | return _type; |
| 161 | } |
| 162 | |
| 163 | template <typename T> |
| 164 | QString TokenBase<T>::typeToString(int type) |
| 165 | { |
swissChili | 9dddbf7 | 2021-12-08 23:03:25 -0800 | [diff] [blame^] | 166 | static const QString typeNames[] = {"SYMBOL", "IDENT", "PAREN", "VAR", "INTEGER"}; |
swissChili | 07d325f | 2021-12-08 20:02:05 -0800 | [diff] [blame] | 167 | return typeNames[type]; |
| 168 | } |
| 169 | |
| 170 | template <typename T> |
| 171 | QChar TokenBase<T>::symbol() const |
| 172 | { |
| 173 | return _charVal; |
| 174 | } |