-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken.c
289 lines (258 loc) · 10.9 KB
/
token.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
/**
* Implementace překladače imperativního jazyka IFJ22
*
* @file token.c
* @author Josef Kuchař ([email protected])
* @author Matej Sirovatka ([email protected])
* @author Tomáš Běhal ([email protected])
* @author Šimon Benčík ([email protected])
* @brief Implementation of token functions
*/
#include "token.h"
#include <ctype.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "error.h"
bool is_valid_hex(char c) {
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
}
bool is_valid_octal(char c) {
return c >= '0' && c <= '7';
}
// Names for all token types
const char* token_names[] = {[TOK_EOF] = "EOF",
[TOK_VAR] = "variable",
[TOK_STR_LIT] = "string literal",
[TOK_INT_LIT] = "integer literal",
[TOK_FLOAT_LIT] = "float literal",
[TOK_BOOL_LIT] = "bool literal",
[TOK_FUN_NAME] = "function name",
[TOK_DOT] = ".",
[TOK_COMMA] = ",",
[TOK_COLON] = ":",
[TOK_SEMICOLON] = ";",
[TOK_ASSIGN] = "=",
[TOK_EQUALS] = "===",
[TOK_NEQUALS] = "!==",
[TOK_PLUS] = "+",
[TOK_MINUS] = "-",
[TOK_MULTIPLY] = "*",
[TOK_DIVIDE] = "/",
[TOK_LESS] = "<",
[TOK_GREATER] = ">",
[TOK_LESS_E] = "<=",
[TOK_GREATER_E] = ">=",
[TOK_LPAREN] = "(",
[TOK_RPAREN] = ")",
[TOK_LBRACE] = "{",
[TOK_RBRACE] = "}",
[TOK_ELSE] = "else",
[TOK_FLOAT] = "float",
[TOK_FUNCTION] = "function",
[TOK_IF] = "if",
[TOK_INT] = "int",
[TOK_NULL] = "null",
[TOK_RETURN] = "return",
[TOK_STRING] = "string",
[TOK_VOID] = "void",
[TOK_WHILE] = "while",
[TOK_FOR] = "for",
[TOK_BREAK] = "break",
[TOK_CONTINUE] = "continue",
[TOK_DOLLAR] = "dollar(parser)",
[TOK_HANDLE_START] = "handle_start",
[TOK_EXP_END] = "expression_end",
[TOK_E] = "expression(parser)"};
void token_print(token_t* token) {
const char* name = token_names[token->type];
fprintf(stderr, "[%zu:%zu]", token->line_nr, token->col_nr);
if (token->type == TOK_VAR || token->type == TOK_STR_LIT || token->type == TOK_FUN_NAME) {
fprintf(stderr, "{ %s, \"%s\" }\n", name, token->attr.val_s.val);
} else if (token->type == TOK_INT || token->type == TOK_FLOAT || token->type == TOK_STRING) {
fprintf(stderr, "{ %s, %s }\n", name, token->attr.val_b ? "optional" : "required");
} else if (token->type == TOK_INT_LIT) {
fprintf(stderr, "{ %s, %d }\n", name, token->attr.val_i);
} else if (token->type == TOK_FLOAT_LIT) {
fprintf(stderr, "{ %s, %f }\n", name, token->attr.val_f);
} else if (token->type == TOK_BOOL_LIT) {
fprintf(stderr, "{ %s, %s }\n", name, token->attr.val_b ? "true" : "false");
} else {
fprintf(stderr, "{ %s }\n", token_names[token->type]);
}
}
token_t token_new(token_type_t type, size_t line_nr, size_t col_nr) {
return (token_t){.type = type, .line_nr = line_nr, .col_nr = col_nr};
}
token_t token_new_with_string(token_type_t type, str_t* str, size_t line_nr, size_t col_nr) {
token_t token = {
.type = type, .attr.val_s = str_new_from_str(str), .line_nr = line_nr, .col_nr = col_nr};
str_clear(str);
return token;
}
token_t token_new_with_string_literal(token_type_t type,
str_t* str,
size_t line_nr,
size_t col_nr) {
str_t new_str = str_new();
// Loop through all characters in the string literal
for (size_t i = 0; i < str->len; i++) {
// Escape sequences
if (str->val[i] == '\\') {
// Characters in hex format (e.g. \x42)
if (str->val[i + 1] == 'x') {
// Check if the next two characters are valid hex
if (i + 3 < str->len && is_valid_hex(str->val[i + 2]) &&
is_valid_hex(str->val[i + 3])) {
// Convert the hex to a character
char hex[3] = {str->val[i + 2], str->val[i + 3], '\0'};
int number = (int)strtol(hex, NULL, 16);
// Check if the number is in the valid range
if (number >= 1 && number <= 255) {
char c = (char)number;
str_add_char(&new_str, c);
i += 3;
} else {
// If not in the valid range, add it as is
str_add_char(&new_str, '\\');
}
} else {
str_add_char(&new_str, '\\');
}
// Characters in decimal format (e.g. \064)
} else if (is_valid_octal(str->val[i + 1])) {
// Check if the next three characters are valid decimal
if (i + 3 < str->len && is_valid_octal(str->val[i + 2]) &&
is_valid_octal(str->val[i + 3])) {
// Convert the decimal to a character
char num[4] = {str->val[i + 1], str->val[i + 2], str->val[i + 3], '\0'};
int number = (int)strtol(num, NULL, 8);
// Check if the number is in the valid range
if (number >= 1 && number <= 255) {
char c = (char)number;
str_add_char(&new_str, c);
i += 3;
} else {
// If not in the valid range, add it as is
str_add_char(&new_str, '\\');
}
} else {
str_add_char(&new_str, '\\');
}
// Other escape sequences
} else {
switch (str->val[i + 1]) {
case 'n':
str_add_char(&new_str, '\n');
break;
case 'r':
str_add_char(&new_str, '\r');
break;
case 't':
str_add_char(&new_str, '\t');
break;
case 'v':
str_add_char(&new_str, '\v');
break;
case 'e':
str_add_char(&new_str, 27); // \e is not supported by C
break;
case 'f':
str_add_char(&new_str, '\f');
break;
case '\\':
str_add_char(&new_str, '\\');
break;
case '$':
str_add_char(&new_str, '$');
break;
default:
// If the escape sequence is not valid, just add it as is
str_add_char(&new_str, '\\');
str_add_char(&new_str, str->val[i + 1]);
}
i++;
}
} else if (str->val[i] == '$') {
// $ can't be used directly
error_exit(ERR_LEX);
} else {
// Normal characters are added as is
str_add_char(&new_str, str->val[i]);
}
}
str_clear(str);
return (token_t){.type = type, .attr.val_s = new_str, .line_nr = line_nr, .col_nr = col_nr};
}
token_t token_new_with_int(token_type_t type, str_t* str, size_t line_nr, size_t col_nr) {
char* err;
long num = strtoul(str->val, &err, 10);
if (err == str->val || *err != '\0') {
error_exit(ERR_LEX);
}
str_clear(str);
return (token_t){.type = type, .attr.val_i = (int)num, .line_nr = line_nr, .col_nr = col_nr};
}
token_t token_new_with_float(token_type_t type, str_t* str, size_t line_nr, size_t col_nr) {
char* err;
double num = strtod(str->val, &err);
if (err == str->val || *err != '\0') {
error_exit(ERR_LEX);
}
str_clear(str);
return (token_t){.type = type, .attr.val_f = num, .line_nr = line_nr, .col_nr = col_nr};
}
token_t token_new_with_bool(token_type_t type, bool val, size_t line_nr, size_t col_nr) {
return (token_t){.type = type, .attr.val_b = val, .line_nr = line_nr, .col_nr = col_nr};
}
bool type_is_datatype(token_type_t type) {
return type == TOK_INT || type == TOK_FLOAT || type == TOK_STRING;
}
bool type_is_literal(token_type_t type) {
return type == TOK_INT_LIT || type == TOK_FLOAT_LIT || type == TOK_STR_LIT ||
type == TOK_NULL || type == TOK_BOOL_LIT;
}
bool type_is_expression(token_type_t type) {
return type == TOK_LPAREN || type_is_literal(type) || type == TOK_VAR;
}
bool type_is_ar_operator(token_type_t type) {
return type == TOK_PLUS || type == TOK_MINUS || type == TOK_DIVIDE || type == TOK_MULTIPLY ||
type == TOK_DOT;
}
bool type_is_comparator(token_type_t type) {
return type == TOK_LESS || type == TOK_LESS_E || type == TOK_GREATER || type == TOK_GREATER_E ||
type == TOK_EQUALS || type == TOK_NEQUALS;
}
void token_free(token_t* token) {
if (token->type == TOK_VAR || token->type == TOK_STR_LIT || token->type == TOK_FUN_NAME) {
str_free(&token->attr.val_s);
}
}
char* token_to_string(token_type_t type) {
char* name = (char*)token_names[type];
return name;
}
bool type_is_number(token_type_t type) {
return type == TOK_FLOAT_LIT || type == TOK_INT_LIT;
}
bool token_is_datatype(token_t* token) {
return type_is_datatype(token->type);
}
bool token_is_returntype(token_t* token) {
return token->type == TOK_INT || token->type == TOK_FLOAT || token->type == TOK_STRING ||
token->type == TOK_VOID;
}
bool token_is_literal(token_t* token) {
return type_is_literal(token->type);
}
bool token_is_expression(token_t* token) {
return type_is_expression(token->type);
}
bool token_is_ar_operator(token_t* token) {
return type_is_ar_operator(token->type);
}
bool token_is_comparator(token_t* token) {
return type_is_comparator(token->type);
}