-
Notifications
You must be signed in to change notification settings - Fork 2
/
token.h
151 lines (135 loc) · 3.1 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
#pragma once
#include <array>
#include <string>
#include <unordered_map>
namespace akan {
enum Tag {
ERR,
END,
ID,
KW_INT,
KW_CHAR,
KW_VOID,
KW_EXTERN,
NUM,
CH,
STR,
NOT,
LEA,
ADD,
SUB,
MUL,
DIV,
MOD,
INC,
DEC,
GT,
GE,
LT,
LE,
EQU,
NEQU,
AND,
OR,
LPAREN,
RPAREN,
LBRACK,
RBRACK,
LBRACE,
RBRACE,
COMMA,
COLON,
SEMICON,
ASSIGN,
KW_IF,
KW_ELSE,
KW_SWITCH,
KW_CASE,
KW_DEFAULT,
KW_WHILE,
KW_DO,
KW_FOR,
KW_BREAK,
KW_CONTINUE,
KW_RETURN
};
class Token {
private:
Tag tag_;
protected:
static std::array<const char *, 48> tag_name_;
public:
Tag GetTag() { return tag_; }
static std::string GetTagName(Tag tag) { return tag_name_[tag]; }
static bool IsType(Tag tag) {
return tag == KW_INT || tag == KW_CHAR || tag == KW_VOID;
}
Token(Tag tag) : tag_(tag) {}
Token(const Token &) = default;
Token &operator=(const Token &) = default;
virtual std::string ToString() const;
virtual ~Token() = default;
};
class Keyword : public Token {
std::string name_;
static std::unordered_map<std::string, Tag> keywords_;
public:
std::string GetName() { return name_; }
Keyword(const std::string &name) : Token(keywords_[name]), name_(name) {}
Keyword(const Keyword &) = default;
Keyword &operator=(const Keyword &) = default;
virtual std::string ToString() const override;
virtual ~Keyword() = default;
static bool IsKeyword(const std::string &name);
};
class Delimiter : public Token {
std::string name_;
public:
std::string GetName() { return name_; }
Delimiter(Tag tag) : Token(tag), name_(tag_name_[tag]) {}
Delimiter(const Delimiter &) = default;
Delimiter &operator=(const Delimiter &) = default;
virtual std::string ToString() const override;
virtual ~Delimiter() = default;
};
class Identifier : public Token {
std::string name_;
public:
std::string GetName() { return name_; }
Identifier(const std::string &name) : Token(ID), name_(name) {}
Identifier(const Identifier &) = default;
Identifier &operator=(const Identifier &) = default;
virtual std::string ToString() const override;
virtual ~Identifier() = default;
};
class String : public Token {
std::string content_;
public:
std::string GetContent() { return content_; }
String(const std::string &content) : Token(STR), content_(content) {}
String(const String &) = default;
String &operator=(const String &) = default;
virtual std::string ToString() const override;
virtual ~String() = default;
};
class Number : public Token {
int value_;
public:
int GetValue() { return value_; }
Number(int value) : Token(NUM), value_(value) {}
Number(const Number &) = default;
Number &operator=(const Number &) = default;
virtual std::string ToString() const override;
virtual ~Number() = default;
};
class Character : public Token {
char ch_;
public:
char GetContent() { return ch_; }
Character(char ch) : Token(CH), ch_(ch) {}
Character(const Character &) = default;
Character &operator=(const Character &) = default;
virtual std::string ToString() const override;
virtual ~Character() = default;
};
} // namespace akan