-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.h
169 lines (105 loc) · 3.92 KB
/
token.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#pragma once
#include <string>
#include <map>
#include <utility>
class Token {
static std::map<char, Token *> *tokenMap;
public:
enum Type {
NUMBER, // 0
IDENTIFIER, // 1
OPERATOR, // 2
OPENING_BRACKET, // 3
CLOSING_BRACKET, // 4
ASSIGNMENT, // 5
OPENING_CURLY_BRACKET, // 6
CLOSING_CURLY_BRACKET, // 7
SEMICOLON, // 8
IF_TOKEN, // 9
ELSE_TOKEN, // 10
};
[[nodiscard]] virtual Type getType() const = 0;
static Token *get(char symbol);
static bool isNonOperatorToken(char symbol) { return tokenMap->contains(symbol); }
static void destroyMap();
virtual ~Token() = default;
};
class Number : public Token {
const double value;
public:
explicit Number(double value) : value(value) {}
[[nodiscard]] double getValue() const { return value; };
[[nodiscard]] Type getType() const override { return Type::NUMBER; };
~Number() override = default;
};
class Operator : public Token {
const int precedence;
const char signature;
static std::map<char, Operator> *operatorMap;
public:
Operator(const int precedence, char signature) : precedence(precedence), signature(signature) {}
static Operator *get(char signature);
static bool isOperator(char signature) { return operatorMap->contains(signature); }
[[nodiscard]] int getPrecedence() const { return precedence; }
[[nodiscard]] char getSignature() const { return signature; }
[[nodiscard]] double apply(double a, double b) const;
[[nodiscard]] Type getType() const override { return Type::OPERATOR; };
static void destroyMap() { delete operatorMap; }
~Operator() override = default;
};
class OpeningBracket : public Token {
static OpeningBracket *instance;
OpeningBracket() = default;
public:
static OpeningBracket *getInstance() { return instance; };
[[nodiscard]] Type getType() const override { return Type::OPENING_BRACKET; };
};
class ClosingBracket : public Token {
static ClosingBracket *instance;
public:
static ClosingBracket *getInstance() { return instance; };
[[nodiscard]] Type getType() const override { return Type::CLOSING_BRACKET; };
};
class Identifier : public Token {
std::string name;
public:
explicit Identifier(std::string name) : name(std::move(name)) {};
[[nodiscard]] std::string getName() const { return name; };
[[nodiscard]] Type getType() const override { return Type::IDENTIFIER; };
};
class Assignment : public Token {
static Assignment *instance;
public:
static Assignment *getInstance() { return instance; };
[[nodiscard]] Type getType() const override { return Type::ASSIGNMENT; };
};
class IfToken : public Token {
static IfToken *instance;
public:
static IfToken *getInstance() { return instance; };
[[nodiscard]] Type getType() const override { return Type::IF_TOKEN; };
};
class ElseToken : public Token {
static ElseToken *instance;
public:
static ElseToken *getInstance() { return instance; };
[[nodiscard]] Type getType() const override { return Type::ELSE_TOKEN; };
};
class OpeningCurlyBracket : public Token {
static OpeningCurlyBracket *instance;
public:
static OpeningCurlyBracket *getInstance() { return instance; };
[[nodiscard]] Type getType() const override { return Type::OPENING_CURLY_BRACKET; };
};
class ClosingCurlyBracket : public Token {
static ClosingCurlyBracket *instance;
public:
static ClosingCurlyBracket *getInstance() { return instance; };
[[nodiscard]] Type getType() const override { return Type::CLOSING_CURLY_BRACKET; };
};
class Semicolon : public Token {
static Semicolon *instance;
public:
static Semicolon *getInstance() { return instance; };
[[nodiscard]] Type getType() const override { return Type::SEMICOLON; };
};