-
Notifications
You must be signed in to change notification settings - Fork 2
/
token.cpp
48 lines (39 loc) · 1.79 KB
/
token.cpp
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
#include "token.h"
namespace akan {
std::array<const char *, 48> Token::tag_name_ = {
"ERROR", "EOF", "ID", "int", "char", "void",
"extern", "Number", "Character", "String", "!", "&",
"+", "-", "*", "/", "%", "++",
"--", ">", ">=", "<", "<=", "==",
"!=", "&&", "||", "(", ")", "[",
"]", "{", "}", ",", ":", ";",
"=", "if", "else", "switch", "else", "default",
"while", "do", "for", "break", "continue", "return"};
std::unordered_map<std::string, Tag> Keyword::keywords_ = {
{"int", KW_INT}, {"char", KW_CHAR},
{"void", KW_VOID}, {"extern", KW_EXTERN},
{"if", KW_IF}, {"else", KW_ELSE},
{"switch", KW_SWITCH}, {"case", KW_CASE},
{"default", KW_DEFAULT}, {"while", KW_WHILE},
{"do", KW_DO}, {"for", KW_FOR},
{"break", KW_BREAK}, {"continue", KW_CONTINUE},
{"return", KW_RETURN}};
std::string Token::ToString() const { return tag_name_[tag_]; }
std::string Keyword::ToString() const { return "[Keyword]: " + name_; }
std::string Delimiter::ToString() const { return "[Delimiter]: " + name_; }
std::string Identifier::ToString() const {
return "[" + Token::ToString() + "]: " + name_;
}
std::string String::ToString() const {
return "[" + Token::ToString() + "]: " + content_;
}
std::string Number::ToString() const {
return "[" + Token::ToString() + "]: " + std::to_string(value_);
}
std::string Character::ToString() const {
return "[" + Token::ToString() + "] " + std::string(1, ch_);
}
bool Keyword::IsKeyword(const std::string &name) {
return keywords_.find(name) != keywords_.end();
}
} // namespace akan