-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToken.cc
More file actions
182 lines (153 loc) · 4.47 KB
/
Copy pathToken.cc
File metadata and controls
182 lines (153 loc) · 4.47 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <sstream>
#include <iostream>
#include <stdlib.h>
#include "Token.h"
#include "AST.h"
#include "parser.h"
using namespace std;
static string map[300];
string tokenName(int token) {
if (map[TokAND] != "TokAND") {
for(size_t i = 256; i < 300; i++)
map[i] = "<illegal>";
map[TokAND] = "TokAND";
map[TokBOOL] = "TokBOOL";
map[TokBREAK] = "TokBREAK";
map[TokBoolConst] = "TokBoolConst";
map[TokCLASS] = "TokCLASS";
map[TokDOUBLE] = "TokDOUBLE";
map[TokDoubleConst] = "TokDoubleConst";
map[TokELSE] = "TokELSE";
map[TokEQUAL] = "TokEQUAL";
map[TokEXTENDS] = "TokEXTENDS";
map[TokFOR] = "TokFOR";
map[TokGREATEREQUAL] = "TokGREATEREQUAL";
map[TokID] = "TokID";
map[TokIF] = "TokIF";
map[TokIMPLEMENTS] = "TokIMPLEMENTS";
map[TokINT] = "TokINT";
map[TokINTERFACE] = "TokINTERFACE";
map[TokIntConst] = "TokIntConst";
map[TokLESSEQUAL] = "TokLESSEQUAL";
map[TokNEW] = "TokNEW";
map[TokNEWARRAY] = "TokNEWARRAY";
map[TokNOTEQUAL] = "TokNOTEQUAL";
map[TokNULL] = "TokNULL";
map[TokOR] = "TokOR";
map[TokPRINT] = "TokPRINT";
map[TokREADINTEGER] = "TokREADINTEGER";
map[TokREADLINE] = "TokREADLINE";
map[TokRETURN] = "TokRETURN";
map[TokSTRING] = "TokSTRING";
map[TokStringConst] = "TokStringConst";
map[TokTHIS] = "TokTHIS";
map[TokTYPEID] = "TokTYPEID";
map[TokVOID] = "TokVOID";
map[TokWHILE] = "TokWHILE";
}
if (token < 256)
return string(1, token);
return map[token];
}
bool ValidTokenType(int type) {
return (' ' <= type && type <= '~') ||
(256 <= type && type < 300 && map[type] != "<illegal>");
}
Token::Token(int ty, string txt, std::string *lineTxt, int lin, int col)
: Type(ty), Text(txt), Line(lin), LineText(lineTxt), Column(col) {
if (!ValidTokenType(ty)) {
cerr << "Error: Illegal token type: " << ty << endl;
exit(1);
}
}
int Token::line() {
return Line;
}
int Token::column() {
return Column;
}
string Token::text() {
return Text;
}
int Token::type() {
return Type;
}
string Token::lineText() {
return *LineText;
}
TokString::TokString(string text, string *lineText, int line, int column)
: Token(TokStringConst, text, lineText, line, column), Value(text) {}
string TokString::toString() {
stringstream ss;
ss << "StringConst(text = \"" << Text << "\", line = " << Line
<< ", column = " << Column << ", line text = \"" << *LineText << "\")";
return ss.str();
}
string TokString::value() {
return Value;
}
TokIdent::TokIdent(string text, string *lineText, int line, int column)
: Token(TokID, text, lineText, line, column), Name(text) {};
string TokIdent::toString() {
stringstream ss;
ss << "ID(" << Name << ", line = " << Line << ", column = " << Column
<< ", line text = \"" << *LineText << "\")";
return ss.str();
}
string TokIdent::name() {
return Name;
}
TokInt::TokInt(string txt, string *lineTxt, int li, int col, bool hex) :
Token(TokIntConst, txt, lineTxt, li, col)
{
stringstream ss;
if (hex) ss << std::hex << txt;
else ss << txt;
ss >> Value;
}
string TokInt::toString() {
stringstream ss;
ss << "IntConst(" << Value << ", text = \"" << Text << "\", line = " << Line
<< ", column = " << Column << ", line text = \"" << *LineText << "\")";
return ss.str();
}
int TokInt::value() {
return Value;
}
TokDouble::TokDouble(string txt, string *lineTxt, int li, int col) :
Token(TokDoubleConst, txt, lineTxt, li, col)
{
stringstream ss(txt);
ss >> Value;
}
string TokDouble::toString() {
stringstream ss;
ss << "DoubleConst(" << Value << ", text = \"" << Text << "\", " << Line
<< ", " << Column << ", \"" << *LineText << "\")";
return ss.str();
}
double TokDouble::value() {
return Value;
}
TokBool::TokBool(bool value, string *lineText, int line, int column)
: Token(TokBoolConst, value ? "true" : "false", lineText, line, column), Value(value) {};
string TokBool::toString() {
stringstream ss;
ss << "BoolConst(" << (Value ? "true" : "false") << ", line = " << Line
<< ", column = " << Column << ", line text = \"" << *LineText << "\")";
return ss.str();
}
bool TokBool::value() {
return Value;
}
string Token::toString() {
stringstream ss;
ss << "Token(" << "type = " << tokenName(Type) << ", text = \"" << Text
<< "\", line = " << Line << ", column = " << Column << ", line text = \""
<< *LineText << "\")";
return ss.str();
}
ostream& operator << (ostream& stream, Token& token) {
stream << token.toString();
return stream;
}