blob: ac104a4fd6f6e47befac3e370d872ba75c3a5e19 [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>
41TokenBase<T>::TokenBase(char varType, const QString name)
42{
43 _type = VAR;
44 _charVal = varType;
45 _stringVal = name;
46}
47
48template <typename T>
49bool TokenBase<T>::isSym() const
50{
51 return _type == SYM;
52}
53
54template <typename T>
55bool TokenBase<T>::isIdent() const
56{
57 return _type == IDENT;
58}
59
60template <typename T>
61bool TokenBase<T>::isParen() const
62{
63 return _type == PAREN;
64}
65template <typename T>
66bool TokenBase<T>::isVar() const
67{
68 return _type == VAR;
69}
70
71template <typename T>
72TokenBase<T>::TokenBase() : TokenBase("Null")
73{
74}
75
76template <typename T>
77bool TokenBase<T>::operator==(const T &other) const
78{
79 return _type == other._type && _stringVal == other._stringVal && _charVal == other._charVal && _listVal == other._listVal;
80}
81
82template <typename T>
83QList<T> TokenBase<T>::parenContent()
84{
85 if (isParen())
86 {
87 return _listVal;
88 }
89 else
90 {
91 return {};
92 }
93}
94
95template <typename T>
96char TokenBase<T>::varType() const
97{
98 return _charVal.toLatin1();
99}
100
101template <typename T>
102const QString &TokenBase<T>::name() const
103{
104 return _stringVal;
105}
106
107template <typename T>
108bool TokenBase<T>::operator!=(const T &other) const
109{
110 return !(this->operator==(other));
111}
112
113template <typename T>
114TokenBase<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
136template <typename T>
137int TokenBase<T>::type() const
138{
139 return _type;
140}
141
142template <typename T>
143QString TokenBase<T>::typeToString(int type)
144{
145 static const QString typeNames[] = {"SYMBOL", "IDENT", "PAREN", "VAR"};
146 return typeNames[type];
147}
148
149template <typename T>
150QChar TokenBase<T>::symbol() const
151{
152 return _charVal;
153}