-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.c
290 lines (281 loc) · 6.16 KB
/
lexer.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include "stdio.h"
#include "stdlib.h"
#include "stdbool.h"
#include "ctype.h"
#include "types.h"
#include "tokens.h"
#include "error.h"
#define LN_SIZE 512 // max line size
#define VAL_SIZE 64 // max value size
#define VAL_EMPTY ""
void tokenize(FILE *f);
static void tokenizeLine(char*);
static void tokenizeWord();
static void tokenizeString();
static void tokenizeNumber();
static void tokenizeHex();
static bool isAvailableName(char);
static bool isAvailableNumber(char);
static bool isAvailableHexNumber(char);
static int indexOf(const char*, const char);
static TokenType getTokenByChar(const char c);
static TokenType getTokenByWord(const char *s);
static enum {numINT, numFLOAT}; // number types
static const char OPERATION_CHARS[] = "+-*/:.,()[]{}#=&$%@!?<>";
static const TokenType OPERATION_TOKEN[] = {
ttPLUS,
ttMINUS,
ttMULTIPLY,
ttDIVIDE,
ttEXTEND,
ttDOT,
ttCOMMA,
ttPOPEN,
ttPCLOSE,
ttSQOPEN,
ttSQCLOSE,
ttOPENBLOCK,
ttCLOSEBLOCK,
ttGHOST,
ttASSIGN,
ttPUBLIC,
ttPRIVATE,
ttPACK,
ttEVENT,
ttNOT,
ttISTRUE,
ttLT,
ttGT
};
static const char *WORD_STRINGS[] = {
"if",
"else",
"switch",
"case",
"or",
"and",
"for",
"while",
"do",
"function",
"fn",
"this",
"self",
"use",
"nil",
"true",
"false",
"print",
"error",
"delete",
"break",
"continue",
"return",
"in"
};
static const TokenType WORD_TOKEN[] = {
ttIF,
ttELSE,
ttSWITCH,
ttCASE,
ttOR,
ttAND,
ttFOR,
ttWHILE,
ttDO,
ttFUNCTION,
ttFNDEF,
ttTHIS,
ttSELF,
ttUSE,
ttNIL,
ttTRUE,
ttFALSE,
ttPRINT,
ttERROR,
ttDELETE,
ttBREAK,
ttCONTINUE,
ttRETURN,
ttIN
};
static bool skip = false;
static int nline = 0;
static char *source;
static int position = 0;
static TokenType tt;
void tokenize(FILE *f) {
char tmp_line[LN_SIZE];
while (fgets(tmp_line, LN_SIZE, f) != NULL) {
tokenizeLine(tmp_line);
//printf("line: %d\n", nline);
}
addToken(ttEOF, "");
}
static void tokenizeLine(char *line) {
source = line;
nline++;
while (source[position] != '\0') { // поки не символ закінчення
// обробка коментарів
if (source[position] == '/' && source[position + 1] == '/') {
break; // якщо коментар // то пропускаєм строку
}
else if (source[position] == '*' && source[position + 1] == '/') { // закрити багаторядковий коментар
skip = false;
position += 2;
}
else if (source[position] == '/' && source[position + 1] == '*') {
skip = true;
position += 2;
}
if (skip) { // if comment - skeep it
position++;
continue;
}
/*-----------------------------------------------------------------*/
if (isalpha(source[position]) || source[position] == '_') { // checking for variables
tokenizeWord();
}
else if (source[position] == '0' && source[position + 1] == 'x') {
tokenizeHex();
}
else if (isdigit(source[position])) { // value
tokenizeNumber();
}
else if (source[position] == '\"') { // string
tokenizeString();
}
else if (tt = getTokenByChar(source[position])) { // operation
if (tt != ttNULL)
addToken(tt, "");
position++;
}
else {
position++; // якщо попадаєть лєва шняга то пропускаєм
}
}
//if (position > 0 && !skip)
addToken(ttEOL, VAL_EMPTY); // end of line
position = 0;
}
static void tokenizeWord() {
TokenType tt;
char buffer[VAL_SIZE] = "";
int bufpos = 0;
while (isAvailableName(source[position])) {
buffer[bufpos++] = source[position++];
}
tt = getTokenByWord(buffer);
if (tt != ttNULL) {
addToken(tt, "");
}
else
addToken(ttVARIABLE, buffer);
}
static void tokenizeString() {
char buffer[VAL_SIZE] = "";
int bufpos = 0;
position++;
for (;;) {
// чекаєм чи є спец символи тіпа \n \t \\ \"
if (source[position] == '\\') {
position++; // пропускаєм ту фігню
switch (source[position]) {
case '"': buffer[bufpos++] = '\"'; position++; continue;
case 'n': buffer[bufpos++] = '\n'; position++; continue;
case 't': buffer[bufpos++] = '\t'; position++; continue;
}
buffer[bufpos++] = '\\';
}
if (source[position] == '\"')
break;
buffer[bufpos++] = source[position++];
}
position++;
addToken(ttSTRING, buffer);
}
static void tokenizeNumber() {
// перепписати ту фігню тре
int num_type = numINT;
char buffer[VAL_SIZE] = "";
int bufpos = 0;
int pc = 0;
while (isAvailableNumber(source[position])) {
if (source[position] == '.') { // чекаєм чи дійсне число
if (pc > 0) { // якщо більше чим 1 то кажем: Каліцькие Число (насправді той хто писав таку прогу каліка)
writeError(erNULL, "invalid FloatNumber");
}
pc++;
num_type = numFLOAT;
}
buffer[bufpos++] = source[position++];
}
switch (num_type) {
case numINT:
addToken(ttNUMBER, buffer);
break;
case numFLOAT:
addToken(ttFLOATNUM, buffer);
break;
}
}
static void tokenizeHex() {
char buffer[VAL_SIZE] = "";
int bufpos = 0;
position += 2;
while (isAvailableHexNumber(source[position])) {
buffer[bufpos++] = source[position++];
}
addToken(ttHEXNUM, buffer);
}
static TokenType getTokenByChar(const char c) {
int idx = indexOf(OPERATION_CHARS, c);
if (idx == -1)
return ttNULL;
return OPERATION_TOKEN[idx];
}
static TokenType getTokenByWord(const char *s) {
for (int i = 0; i < (sizeof(WORD_STRINGS) / sizeof(*WORD_STRINGS));i++) {
if (!strcmp(WORD_STRINGS[i], s)) {
return WORD_TOKEN[i];
}
}
return ttNULL;
}
// tools
static bool isAvailableName(char c) {
if (c >= 'a' && c <= 'z')
return true;
if (c >= 'A' && c <= 'Z')
return true;
if (c >= '0' && c <= '9')
return true;
if (c == '_')
return true;
return false;
}
static bool isAvailableNumber(char c) {
if (c >= '0' && c <= '9')
return true;
if (c == '.')
return true;
return false;
}
static bool isAvailableHexNumber(char c) {
if (c >= '0' && c <= '9')
return true;
if (c >= 'A' && c <= 'F')
return true;
if (c >= 'a' && c <= 'f')
return true;
if (c == '.')
writeError(erNULL, "invalid HexNumber");
return false;
}
static int indexOf(const char *s, const char c) {
const char *p = strchr(s, c);
if (p) {
return p - s;
}
return -1;
}