-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken.cpp
More file actions
101 lines (91 loc) · 3.74 KB
/
token.cpp
File metadata and controls
101 lines (91 loc) · 3.74 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
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
#include "token.h"
#include <sstream>
namespace AR437 {
std::unordered_map<std::string, TokenType> TokenUtil::keywords;
void TokenUtil::initializeKeywords() {
if (keywords.empty()) {
keywords["let"] = TokenType::LET;
keywords["func"] = TokenType::FUNC;
keywords["if"] = TokenType::IF;
keywords["else"] = TokenType::ELSE;
keywords["while"] = TokenType::WHILE;
keywords["for"] = TokenType::FOR;
keywords["return"] = TokenType::RETURN;
keywords["true"] = TokenType::TRUE;
keywords["false"] = TokenType::FALSE;
keywords["null"] = TokenType::NULL_TOK;
keywords["display"] = TokenType::DISPLAY;
keywords["read"] = TokenType::READ;
}
}
std::string Token::toString() const {
std::ostringstream oss;
oss << "Token{" << TokenUtil::tokenTypeToString(type)
<< ", '" << value << "', " << line << ":" << column << "}";
return oss.str();
}
std::string TokenUtil::tokenTypeToString(TokenType type) {
switch (type) {
case TokenType::NUMBER: return "NUMBER";
case TokenType::STRING: return "STRING";
case TokenType::BOOLEAN: return "BOOLEAN";
case TokenType::IDENTIFIER: return "IDENTIFIER";
case TokenType::LET: return "LET";
case TokenType::FUNC: return "FUNC";
case TokenType::IF: return "IF";
case TokenType::ELSE: return "ELSE";
case TokenType::WHILE: return "WHILE";
case TokenType::FOR: return "FOR";
case TokenType::RETURN: return "RETURN";
case TokenType::TRUE: return "TRUE";
case TokenType::FALSE: return "FALSE";
case TokenType::NULL_TOK: return "NULL";
case TokenType::DISPLAY: return "DISPLAY";
case TokenType::READ: return "READ";
case TokenType::PLUS: return "PLUS";
case TokenType::MINUS: return "MINUS";
case TokenType::MULTIPLY: return "MULTIPLY";
case TokenType::DIVIDE: return "DIVIDE";
case TokenType::MODULO: return "MODULO";
case TokenType::ASSIGN: return "ASSIGN";
case TokenType::EQUAL: return "EQUAL";
case TokenType::NOT_EQUAL: return "NOT_EQUAL";
case TokenType::LESS: return "LESS";
case TokenType::LESS_EQUAL: return "LESS_EQUAL";
case TokenType::GREATER: return "GREATER";
case TokenType::GREATER_EQUAL: return "GREATER_EQUAL";
case TokenType::AND: return "AND";
case TokenType::OR: return "OR";
case TokenType::NOT: return "NOT";
case TokenType::SEMICOLON: return "SEMICOLON";
case TokenType::COMMA: return "COMMA";
case TokenType::LPAREN: return "LPAREN";
case TokenType::RPAREN: return "RPAREN";
case TokenType::LBRACE: return "LBRACE";
case TokenType::RBRACE: return "RBRACE";
case TokenType::LBRACKET: return "LBRACKET";
case TokenType::RBRACKET: return "RBRACKET";
case TokenType::NEWLINE: return "NEWLINE";
case TokenType::EOF_TOK: return "EOF";
case TokenType::INVALID: return "INVALID";
default: return "UNKNOWN";
}
}
TokenType TokenUtil::getKeywordType(const std::string& word) {
initializeKeywords();
auto it = keywords.find(word);
return (it != keywords.end()) ? it->second : TokenType::IDENTIFIER;
}
bool TokenUtil::isKeyword(const std::string& word) {
initializeKeywords();
return keywords.find(word) != keywords.end();
}
bool TokenUtil::isOperator(char c) {
return c == '+' || c == '-' || c == '*' || c == '/' || c == '%' ||
c == '=' || c == '!' || c == '<' || c == '>' || c == '&' || c == '|';
}
bool TokenUtil::isDelimiter(char c) {
return c == ';' || c == ',' || c == '(' || c == ')' ||
c == '{' || c == '}' || c == '[' || c == ']';
}
} // namespace AR437