-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToken.h
More file actions
70 lines (56 loc) · 1.79 KB
/
Token.h
File metadata and controls
70 lines (56 loc) · 1.79 KB
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
#include<string>
#include<variant>
#include<iostream>
enum TokenType {
INT,
PLUS,
SUB,
MUL,
DIV,
LPAREN,
RPAREN,
NONE,
EOL
};
class Token{
std::variant<int, std::string> value;
TokenType type;
public:
Token (): value(""), type(NONE) { }
Token(std::variant<int, std::string> value, TokenType type) : value(value), type(type) { }
std::variant<int, std::string> getValue(){
return this->value;
}
TokenType getType(){
return this->type;
}
void setType(TokenType type){
this->type = type;
}
void setValue(std::variant<int, std::string> value){
this->value = value;
}
friend std::ostream& operator<<(std::ostream& os , const Token& token){
os << "Token(";
if (std::holds_alternative<int>(token.value)) {
os << std::get<int>(token.value);
} else {
os << "\"" << std::get<std::string>(token.value) << "\"";
}
os << ", Type: ";
switch (token.type) {
case TokenType::INT: os << "INTEGER"; break;
case TokenType::PLUS: os << "PLUS"; break;
case TokenType::SUB: os << "MINUS"; break;
case TokenType::MUL: os << "MUL"; break;
case TokenType::DIV: os << "DIV"; break;
case TokenType::LPAREN: os << "LPAREN"; break;
case TokenType::RPAREN: os << "RPAREN"; break;
case TokenType::EOL: os << "EOL"; break;
case TokenType::NONE: os << "NONE"; break;
default: os << "UNKNOWN"; break;
}
os << ")";
return os;
}
};