-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenizer.c
245 lines (240 loc) · 8.78 KB
/
tokenizer.c
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
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "structures.h"
#include "tokenizer.h"
void ClearLine(char* expression) {
char trimmed[1000] = { '\0' };
int trimIndex = 0;
for (int i = 0; expression[i] != '\0'; i++) { //clear from spaces and tabs
if (expression[i] != ' ' && expression[i] != '\t') {
if (expression[i] == '*' && expression[i + 1] == '*') {
trimmed[trimIndex] = '^'; // '^' â trimmed
i++; // Skip the next '*' character since '**' has already been processed
}
else {
trimmed[trimIndex] = expression[i];
}
trimIndex++; // Increment trimIndex after each character copy
}
}
strcpy(expression, trimmed);
for (int i = 0; i < strlen(expression); i++) { //swap , to . and Upper to Lowers
if (expression[i] == ',') expression[i] = '.';
if (expression[i] >= 'A' && expression[i] <= 'Z') expression[i] = expression[i] + 32;
}
}
double convertStringToDouble(const char* str) {
return strtod(str, NULL);
}
void ClearCharArray(char* arr) {
int len = strlen(arr);
for (int i = 0; i < len; i++) {
arr[i] = '\0';
}
}
Token* tokenizer(char* str, int size) {
Token* array = (Token*)malloc(size*2 * sizeof(Token));
if (array == NULL) {
printf("\nKURWA HEAP KURWA!!1!\n");
exit(EXIT_FAILURE);
}
Token temp;
ClearToken(&temp);
char MathFunctins[32][16] = {
"sin", "cos", "tg", "ctg", "arcsin", "arccos","arctg","arcctg","sqrt"
};
char _pi[] = "pi";
int ArrayPositionRN = 0;
int counter = 0;
int ParsingNumberRN = 0;
int ParsingFunctionRN = 0;
char TempCharLine[256] = { '\0' }; // bringing number in char array, for ex 3.14159265
for (int i = 0; i <= strlen(str); i++) { //<= because we need to parse \0 to add determine token
if (ParsingFunctionRN) {
if (isalpha(str[i])) {
TempCharLine[counter] = str[i];
counter++;
continue;
}
else {
if (strcmp(_pi, TempCharLine)==0) {
temp.type = VALUE;
temp.value = 3.14159265;
array[ArrayPositionRN] = temp;
ArrayPositionRN++;
ClearToken(&temp);
ClearCharArray(TempCharLine);
}
else {
for (int i = 0; i < 10; i++) {
if (strcmp(TempCharLine, MathFunctins[i]) == 0) {
temp.type = FUNCTION;
temp.func = i;
array[ArrayPositionRN] = temp;
ArrayPositionRN++;
ClearToken(&temp);
ClearCharArray(TempCharLine);
break;
}
}
}
if (TempCharLine[0] != '\0') { // if user entired some shit
temp.type = __ERROR;
temp.func = 8;
array[ArrayPositionRN] = temp;
ArrayPositionRN++;
ClearToken(&temp);
ClearCharArray(TempCharLine);
}
ParsingFunctionRN = 0;
counter = 0;
}
}
if (isdigit(str[i]) || str[i] == '.') {
ParsingNumberRN = 1;
TempCharLine[counter] = str[i];
counter++;
}
else if (((!(isdigit(str[i]) || str[i] == '.')) && ParsingNumberRN == 1) && str[i] != '\0') {
temp.type = VALUE;
temp.value = convertStringToDouble(TempCharLine);
array[ArrayPositionRN] = temp;
ArrayPositionRN++;
counter = 0;
ClearCharArray(TempCharLine);
ClearToken(&temp);
ParsingNumberRN = 0; //turn flag that we are parsing rn some number off
}
if (str[i] == '\0') { // if we reaised end of the line
if (ParsingNumberRN) {
temp.type = VALUE;
temp.value = convertStringToDouble(TempCharLine);
array[ArrayPositionRN] = temp;
ArrayPositionRN++;
counter = 0;
ClearCharArray(TempCharLine);
ClearToken(&temp);
}
temp.type = END;
array[ArrayPositionRN] = temp;
break;
}
if (!ParsingNumberRN) { //If we not parsing some number right now we can parse other data
if (isalpha(str[i])) {
if (!(isalpha(str[i + 1]))) {
temp.type = VARIABLE;
temp.data = str[i];
array[ArrayPositionRN] = temp;
ArrayPositionRN++;
ClearToken(&temp);
continue;
}
ParsingFunctionRN = 1;
TempCharLine[counter] = str[i];
counter++;
continue;
}
if (str[i] == '(') {
temp.type = BRACKET_OPEN;
temp.data = '(';
array[ArrayPositionRN] = temp;
ArrayPositionRN++;
temp.type = NONE;
ClearToken(&temp);
}
else if (str[i] == ')') {
temp.type = BRACKET_CLOSE;
temp.data = ')';
array[ArrayPositionRN] = temp;
ArrayPositionRN++;
ClearToken(&temp);
}
else if (str[i] == '+') {
temp.type = OPERAND;
temp.data = '+';
array[ArrayPositionRN] = temp;
ArrayPositionRN++;
ClearToken(&temp);
}
else if (str[i] == '-') {
if (i == 0) {
temp.type = VALUE;
temp.value = 0;
array[ArrayPositionRN] = temp;
ArrayPositionRN++;
ClearToken(&temp);
temp.type = OPERAND;
temp.data = '-';
array[ArrayPositionRN] = temp;
ArrayPositionRN++;
ClearToken(&temp);
}
else if (isdigit(str[i - 1]) == 0 && str[i - 1] != ')' && isalpha(str[i - 1]) == 0) {
temp.type = VALUE;
temp.value = 0;
array[ArrayPositionRN] = temp;
ArrayPositionRN++;
ClearToken(&temp);
temp.type = OPERAND;
temp.data = '-';
array[ArrayPositionRN] = temp;
ArrayPositionRN++;
ClearToken(&temp);
} else {
temp.type = OPERAND;
temp.data = '-';
array[ArrayPositionRN] = temp;
ArrayPositionRN++;
ClearToken(&temp);
}
}
else if (str[i] == '*') {
temp.type = OPERAND;
temp.data = '*';
array[ArrayPositionRN] = temp;
ArrayPositionRN++;
ClearToken(&temp);
}
else if (str[i] == '^') {
temp.type = OPERAND;
temp.data = '^';
array[ArrayPositionRN] = temp;
ArrayPositionRN++;
ClearToken(&temp);
}
else if (str[i] == '!') {
temp.type = OPERAND;
temp.data = '!';
array[ArrayPositionRN] = temp;
ArrayPositionRN++;
ClearToken(&temp);
}
else if (str[i] == '/') {
temp.type = OPERAND;
temp.data = '/';
array[ArrayPositionRN] = temp;
ArrayPositionRN++;
ClearToken(&temp);
}
}
}
return array;
}
int CheckTokenPositions(Token* tokens) { // avoid 2++ and some other incorrect input
int i = 0;
while (tokens[i].type != END) {
if ( //trying to find wrong token positions
(tokens[i].type == FUNCTION && tokens[i + 1].type != BRACKET_OPEN) || // if sqrt2 or sinx instead of sqrt(2) and sin(x)
(tokens[i].type == tokens[i+1].type && (!((tokens[i].type == BRACKET_CLOSE && tokens[i+1].type == BRACKET_CLOSE) ||(tokens[i].type == BRACKET_OPEN && tokens[i+1].type == BRACKET_OPEN)))) ||// if *+ or +- (exception with )( )
(tokens[i].type == __ERROR)
) return 0;
i++;
}
return 1; // everything good
}
void flush_input_buffer() {
int c;
while ((c = getchar()) != '\n' && c != EOF) {}
}