-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.cpp
150 lines (143 loc) · 3.08 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
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
#include "token.h"
#include <sstream>
#include <utility>
Token::Token(TokenType type, string lexeme, iteral it, int line):type(type), lexeme(move(lexeme)), it(move(it)), line(line)
{
}
string Token::to_string() const
{
string s;
switch (this->type)
{
case LEFT_PAREN :
s = "LEFT_PAREN";
break;
case RIGHT_PAREN:
s = "RIGHT_PAREN";
break;
case LEFT_BRACE:
s = "LEFT_BRACE";
break;
case RIGHT_BRACE:
s = "RIGHT_BRACE";
break;
case COMMA:
s = "COMMA";
break;
case DOT:
s = "DOT";
break;
case MINUS:
s = "MINUS";
break;
case PLUS:
s = "PLUS";
break;
case SEMICOLON:
s = "SEMICOLON";
break;
case SLASH:
s = "SLASH";
break;
case STAR:
s = "STAR";
break;
case BANG:
s = "BANG";
break;
case BANG_EQUAL:
s = "BANG_EQUAL";
break;
case EQUAL:
s = "EQUAL";
break;
case EQUAL_EQUAL:
s = "EQUAL_EQUAL";
break;
case GREATER:
s = "GREATER";
break;
case GREATER_EQUAL:
s = "GREATER_EQUAL";
break;
case LESS:
s = "LESS";
break;
case LESS_EQUAL:
s = "LESS_EQUAL";
break;
case ENDOFFILE:
s = "ENDOFFILE";
break;
case STRING:
s = "STRING";
s += "\tlexeme:";
s += this->lexeme;
break;
case NUMBER:
s = "NUMBER";
s += "\tliteral:";
s += std::to_string(this->it.val);
break;
case IDENTIFIER:
s = "IDENTIFIER";
s += "\tlexeme:";
s += this->lexeme;
break;
case AND:
s = "AND";
break;
case CLASS:
s = "CLASS_TYPE";
break;
case ELSE:
s = "ELSE";
break;
case FALSE:
s = "FALSE";
break;
case FUN:
s = "FUN";
break;
case FOR:
s = "FOR";
break;
case IF:
s = "IF";
break;
case PRINT:
s = "PRINT";
break;
case RETURN:
s = "RETURN";
break;
case SUPER:
s = "SUPER";
break;
case THIS:
s = "THIS";
break;
case TRUE:
s = "TRUE";
break;
case VAR:
s = "VAR";
break;
case WHILE:
s = "WHILE";
break;
default:
break;
}
stringstream ss;
ss << this->line;
string tmp;
ss >> tmp;
s += "\tline: ";
s += tmp;
return s;
}
void Token::update_line(int l)
{
this->line = l;
}