-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.cpp
166 lines (157 loc) · 4.61 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "token.h"
#include <algorithm>
#include "error.h"
short counter = 0; // Initialize counter
std::unordered_map<short, std::string> functions;
short mainLocation = -1;
Token::Token(TokenType type) {
this->type = type;
this->value.empty = nullptr;
}
Token::Token(std::string literal) {
this->type = FUNCTION;
// Check if literal already exists in the map to avoid duplicate entries
auto it = std::find_if(functions.begin(), functions.end(),[&literal](const auto& pair) { return pair.second == literal; });
if (it != functions.end()) {
this->value.identifierKey = it->first; // Use existing key
} else {
functions[counter] = literal; // Add new functions
this->value.identifierKey = counter;
counter++;
}
}
Token::Token (TokenType type,int number){
this->type = type;
this->value.number = number;
}
Token::Token(int number) {
this->type = NUMBER;
this->value.number = number;
}
Token::Token (TokenType type,std::string str){
this->type = DECLARE;
}
#include <iostream>
#include "token.h"
void Token::printToken() {
// Print token type
switch (type) {
case PLUS:
std::cout << "Token Type: PLUS";
break;
case MINUS:
std::cout << "Token Type: MINUS";
break;
case DIVIDE:
std::cout << "Token Type: DIVIDE";
break;
case MULTIPLY:
std::cout << "Token Type: MULTIPLY";
break;
case EQUAL:
std::cout << "Token Type: EQUAL";
break;
case FUNCTION:
std::cout << "Token Type: FUNCTION";
std::cout << ", Value: " << value.identifierKey;
break;
case SEMICOLON:
std::cout << "Token Type: SEMICOLON";
break;
case BOPEN:
std::cout << "Token Type: BOPEN (";
break;
case BCLOSE:
std::cout << "Token Type: BCLOSE )";
break;
case NUMBER:
std::cout << "Token Type: NUMBER, Value: " << value.number;
break;
case BANG:
std::cout << "Token Type: BANG (!)";
break;
case EQUALEQUAL:
std::cout << "Token Type: EQUALEQUAL (==)";
break;
case BANGEQUAL:
std::cout << "Token Type: BANGEQUAL (!=)";
break;
case LESSTHAN:
std::cout << "Token Type: LESSTHAN (<)";
break;
case LESSEQUAL:
std::cout << "Token Type: LESSEQUAL (<=)";
break;
case GREATERTHAN:
std::cout << "Token Type: GREATERTHAN (>)";
break;
case GREATEREQUAL:
std::cout << "Token Type: GREATEREQUAL (>=)";
break;
case COPEN:
std::cout << "Token Type: COPEN ({)";
break;
case CCLOSE:
std::cout << "Token Type: CCLOSE (})";
break;
case DOLLAR:
std::cout << "Token Type: DOLLAR ($)";
break;
case CALLER:
std::cout << "Token Type: CALLER (@)";
break;
// Keywords
case TRASH:
std::cout << "Token Type: TRASH";
break;
case IF:
std::cout << "Token Type: IF";
break;
case ELSE:
std::cout << "Token Type: ELSE";
break;
case LOOP:
std::cout << "Token Type: LOOP";
break;
case INPUT:
std::cout << "Token Type: INPUT";
break;
case PRINT:
std::cout << "Token Type: PRINT";
break;
case SOL:
std::cout << "Token Type: SOL";
break;
case LUNA:
std::cout << "Token Type: LUNA";
break;
case NOX:
std::cout << "Token Type: NOX";
break;
case CONSTAS:
std::cout << "Token Type: CONSTAS";
break;
case OMNIS:
std::cout << "Token Type: OMNIS";
break;
// Special tokens
case DECLARE:
std::cout << "Token Type: DECLARE (~abc~)";
break;
case LEFTPUSH:
std::cout << "Token Type: LEFTPUSH (<-)";
break;
case RIGHTPUSH:
std::cout << "Token Type: RIGHTPUSH (->)";
break;
case LEFTCOPY:
std::cout << "Token Type: LEFTCOPY (<<)";
break;
case RIGHTCOPY:
std::cout << "Token Type: RIGHTCOPY (>>)";
break;
default:
std::cout << "Unknown Token Type";
}
std::cout << std::endl;
}