blob: 20e2a16be35bd7733b604f851cfd8cca2392d05c [file] [log] [blame]
swissChili7babd922021-12-02 22:46:48 -08001#include "Token.h"
swissChili07d325f2021-12-08 20:02:05 -08002#include "AstNode.h"
swissChili7babd922021-12-02 22:46:48 -08003
swissChili07d325f2021-12-08 20:02:05 -08004template 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.
8template class TokenBase<AstNode>;
9
10template <typename T>
11TokenBase<T>::TokenBase(const T &other)
12{
13 _type = other._type;
14 _stringVal = other._stringVal;
15 _listVal = other._listVal;
16 _charVal = other._charVal;
17}
18
19template <typename T>
20TokenBase<T>::TokenBase(QChar symbol)
21{
22 _type = SYM;
23 _charVal = symbol;
24}
25
26template <typename T>
27TokenBase<T>::TokenBase(QString identifier)
28{
29 _type = IDENT;
30 _stringVal = identifier;
31}
32
33template <typename T>
34TokenBase<T>::TokenBase(QList<T> parenthesized)
35{
36 _type = PAREN;
37 _listVal = std::move(parenthesized);
38}
39
40template <typename T>
swissChili9dddbf72021-12-08 23:03:25 -080041TokenBase<T>::TokenBase(QString integer, int base)
42{
43 _type = INTEGER;
44 _intVal = integer.toInt(nullptr, base);
45}
46
47template <typename T>
swissChili07d325f2021-12-08 20:02:05 -080048TokenBase<T>::TokenBase(char varType, const QString name)
49{
50 _type = VAR;
51 _charVal = varType;
52 _stringVal = name;
53}
54
55template <typename T>
56bool TokenBase<T>::isSym() const
57{
58 return _type == SYM;
59}
60
61template <typename T>
62bool TokenBase<T>::isIdent() const
63{
64 return _type == IDENT;
65}
66
67template <typename T>
68bool TokenBase<T>::isParen() const
69{
70 return _type == PAREN;
71}
72template <typename T>
73bool TokenBase<T>::isVar() const
74{
75 return _type == VAR;
76}
77
78template <typename T>
swissChili9dddbf72021-12-08 23:03:25 -080079bool TokenBase<T>::isInteger() const
80{
81 return _type == INTEGER;
82}
83
84template <typename T>
swissChili07d325f2021-12-08 20:02:05 -080085TokenBase<T>::TokenBase() : TokenBase("Null")
86{
87}
88
89template <typename T>
90bool TokenBase<T>::operator==(const T &other) const
91{
92 return _type == other._type && _stringVal == other._stringVal && _charVal == other._charVal && _listVal == other._listVal;
93}
94
95template <typename T>
96QList<T> TokenBase<T>::parenContent()
97{
98 if (isParen())
99 {
100 return _listVal;
101 }
102 else
103 {
104 return {};
105 }
106}
107
108template <typename T>
swissChili9dddbf72021-12-08 23:03:25 -0800109int TokenBase<T>::integer() const
110{
111 return _intVal;
112}
113
114template <typename T>
swissChili07d325f2021-12-08 20:02:05 -0800115char TokenBase<T>::varType() const
116{
117 return _charVal.toLatin1();
118}
119
120template <typename T>
121const QString &TokenBase<T>::name() const
122{
123 return _stringVal;
124}
125
126template <typename T>
127bool TokenBase<T>::operator!=(const T &other) const
128{
129 return !(this->operator==(other));
130}
131
132template <typename T>
133TokenBase<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;
swissChili9dddbf72021-12-08 23:03:25 -0800141 if (isInteger())
142 return QString::number(_intVal, 10);
swissChili07d325f2021-12-08 20:02:05 -0800143 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
157template <typename T>
158int TokenBase<T>::type() const
159{
160 return _type;
161}
162
163template <typename T>
164QString TokenBase<T>::typeToString(int type)
165{
swissChili9dddbf72021-12-08 23:03:25 -0800166 static const QString typeNames[] = {"SYMBOL", "IDENT", "PAREN", "VAR", "INTEGER"};
swissChili07d325f2021-12-08 20:02:05 -0800167 return typeNames[type];
168}
169
170template <typename T>
171QChar TokenBase<T>::symbol() const
172{
173 return _charVal;
174}