-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.hpp
269 lines (233 loc) · 7.28 KB
/
token.hpp
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: token.hpp
* Author: Andrew
*
* Created on March 8, 2017, 11:38 AM
*/
#ifndef TOKEN_HPP
#define TOKEN_HPP
#include <string>
#include <sstream>
#include <bitset>
enum Token_Types {
Int_Token, // integer token
Bool_Token, // Boolean token
Var_Token, //Token For Variables
Var_Ref_Token, //Token For refrence to Variables
Plus_Token, // + token
Minus_Token, // - token
Mul_Token, // * token
Div_Token, // / token
Mod_Token, // % token
And_Token, // & token
Or_Token, // | token
Xor_Token, // ^ token
Not_Token, // ! token
Equal_Token, // = token
Not_Equal_Token, // != token
LT_Token, // < token
GT_Token, // > token
Lteq_Token, // <= token
Gteq_Token, // >= token
Question_Token, // ? token
Colon_Token, // : token
EQ_EQ_Token, // == token
And_And_Token, // && token
Or_Or_Token, // || token
LPar_Token, // ( token
RPar_Token, // ) token
Tilda_Token, // ~ token
Int_KW_Token, // "int" Keyword Token
Bool_KW_Token, // "bool" Keyword Token
Var_KW_Token, // "var" Keyword Token
Null_KW_Token, // "null" Keyword Token
Break_KW_Token, // 'break' keyword token
Continue_KW_Token, // 'continue' keyword token
Def_KW_Token, // 'def' keyword token
Else_KW_Token, // 'else' keyword token
If_KW_Token, // 'if' keyword token
Return_KW_Token, // 'return' keyword token
While_KW_Token, // 'while' keyword token
EndOfFile_Token, // Eof Token
Semi_Token, // ; token
L_Brace_Token,
R_Brace_Token
};
class Token {
protected:
std::string Token_String;
~Token() {
;
}
public:
int Token_Type;
virtual std::string Send_Value() {
};
virtual std::string Send_Type() {
};
std::string Get_Token_String(int val);
std::string Print_Token() {
return Token_String;
}
};
class Boolean_Token : public Token {
bool value;
public:
Boolean_Token(bool v) : value(v) {
Token_Type = Bool_Token;
Token_String = Get_Token_String(Bool_Token);
};
std::string Send_Value() {
if (value) {
return "true";
} else {
return "false";
};
};
bool Send_Bool_Value() {
return value;
}
};
class Integer_Token : public Token {
int value;
int opt;
public:
Integer_Token(int i, int op) : value(i) {
opt = op;
Token_Type = Int_Token;
Token_String = Get_Token_String(Int_Token);
}
std::string Send_Value() {
std::stringstream ss;
if (opt == 0) {
ss << value;
} else if (opt == 1) {
ss << "0x" << std::hex << value;
} else if (opt == 2) {
std::string s = "0b" + std::bitset< 32 >(value).to_string();
return s;
} else {
std::cout << "Error no option for int ";
}
std::string str = ss.str();
return str;
};
int Send_Int_Value() {
return value;
}
};
class Variable_Token : public Token {
std::string Token_Content;
public:
Variable_Token(std::string Variable_Name) {
Token_Content = Variable_Name;
Token_Type = Var_Token;
Token_String = Get_Token_String(Var_Token);
}
std::string Send_Value() {
return Token_Content;
}
};
class Punc_Token : public Token {
public:
Punc_Token(Token_Types Val) {
Token_Type = Val;
Token_String = Get_Token_String(Val);
}
}
;
std::string Token::Get_Token_String(int val) {
switch (val) {
case Int_Token: // integer token
return "Integer Token";
case Bool_Token: // Boolean token
return "Boolean sToken";
case Plus_Token: // + token
return "Plus sign Token";
case Minus_Token: // - token
return "Minus sign Token";
case Mul_Token: // * token
return "Multiply Token";
case Div_Token: // / token
return "Divide Token";
case Mod_Token: // % token
return "Modulus Token";
case And_Token: // & token
return "And Token";
case Or_Token: // | token
return "Or Token";
case Xor_Token: // ^ token
return "Xor Token";
case Not_Token: // ! token
return "Not Token";
case Equal_Token: // = token
return "Equal Token";
case Not_Equal_Token: // != token
return "Not Equal Token";
case LT_Token: // < token
return "Less Than Token";
case GT_Token: // > token
return "Greater Than Token";
case Lteq_Token: // <= token
return "Less Than Or Equal To Token";
case Gteq_Token: // >= token
return "Greater Than Or Equal To Token";
case Question_Token: // ? token
return "Question Token";
case Colon_Token: // : token
return "Colon Token";
case EQ_EQ_Token: // == token
return "Double Equal Token";
case And_And_Token: // && token
return "Double And Token";
case Or_Or_Token: // || token
return "Double Or Token";
case LPar_Token: // ( token
return "Left Parenthese Token";
case RPar_Token: // ) token
return "Right Parenthese_Token";
case Tilda_Token: // ~ token
return "Tilda Token";
case Var_Token:
return "Variable Token";
case Var_Ref_Token:
"Variable Reference Token";
case EndOfFile_Token:
return " End Of File Token"; // Eof Token
case Int_KW_Token: // 'int' keyword token
return "Integer Keyword Token";
case Bool_KW_Token: // 'bool' keyword token
return "Boolean Keyword Token";
case Var_KW_Token: // 'var' keyword token
return "Var Keyword Token";
case Null_KW_Token: // 'null' keyword token
return "null Keyword Token";
case Break_KW_Token: // 'break' keyword token
return "break Keyword Token";
case Continue_KW_Token: // 'continue' keyword token
return "continue Keyword Token";
case Def_KW_Token: // 'def' keyword token
return "def Keyword Token";
case Else_KW_Token: // 'else' keyword token
return "else Keyword Token";
case If_KW_Token: // 'if' keyword token
return "if Keyword Token";
case Return_KW_Token: // 'return' keyword token
return "return Keyword Token";
case While_KW_Token: // 'while' keyword token
return "while Keyword Token";
case Semi_Token:
return "Semicolon Token";
case L_Brace_Token:
return "Left Brace";
case R_Brace_Token:
return "Right Brace";
default: return "ERRROR TOKEN NOT FOUND";
}
}
#endif /* TOKEN_HPP */