-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToken.cpp
More file actions
53 lines (45 loc) · 1.39 KB
/
Token.cpp
File metadata and controls
53 lines (45 loc) · 1.39 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
#include "headers/Token.h"
Token::Token(TokenType a_type, std::string a_lexeme, std::any a_literal, int a_line)
: type(a_type),
lexeme(a_lexeme),
literal(a_literal),
line(a_line)
{ }
std::string token_to_string(TokenType type) {
static const std::string strings[] = {
"LEFT_PAREN", "RIGHT_PAREN", "LEFT_BRACE", "RIGHT_BRACE",
"COMMA", "DOT", "MINUS", "PLUS", "SEMICOLON", "SLASH", "STAR",
"BANG", "BANG_EQUAL",
"EQUAL", "EQUAL_EQUAL",
"GREATER", "GREATER_EQUAL",
"LESS", "LESS_EQUAL",
"IDENTIFIER", "STRING", "NUMBER",
"AND", "CLASS", "ELSE", "FALSE", "FUN", "FOR", "IF", "NIL", "OR",
"PRINT", "RETURN", "SUPER", "THIS", "TRUE", "VAR", "WHILE",
"END_OF_FILE"
};
return strings[static_cast<int>(type)];
}
// ** This Method is for debugging
std::string Token::to_string() const
{
std::string literal_text;
switch (type) {
case (IDENTIFIER):
literal_text = lexeme;
break;
case (STRING):
literal_text = std::any_cast<std::string>(literal);
break;
case (NUMBER):
literal_text = std::to_string(std::any_cast<double>(literal));
break;
case (LOX_TRUE):
case (LOX_FALSE):
literal_text = std::to_string(std::any_cast<bool>(literal));
break;
default:
literal_text = "not_literal";
}
return token_to_string(type) + " " + lexeme + " " + literal_text;
}