-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.c
374 lines (365 loc) · 9.72 KB
/
debug.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include "header.h"
char *type_to_string(Type type)
{
struct
{
char *string;
Type type;
} Types[] = {
// data types
{"IDENTIFIER", identifier_},
{"CHARACTERS", characters_},
{"BOOLEAN", boolean_},
{"NUMBER", number_},
{"ARRAY", array_},
{"OBJ", obj_},
{"VOID", void_},
{"TUPLE", tuple_},
// assigns
{"ASSIGNEMENT", assign_},
{"ADD ASSIGN", add_assign_},
{"SUB ASSIGN", sub_assign_},
{"MUL ASSIGN", mul_assign_},
{"DIV ASSIGN", div_assign_},
{"MOD ASSIGN", mod_assign_},
// statements
{"WHILE", while_},
{"IF", if_},
{"ELIF", elif_},
{"ELSE", else_},
{"FOR", for_},
{"IN", in_},
// end statements
{"DOTS", dots_},
{"COMMA", comma_},
// parents, brackets
{"LEFT PARENT", lparent_},
{"RIGHT PARENT", rparent_},
{"LEFT BRACKET", lbracket_},
{"RIGHT BRACKET", rbracket_},
{"LEFT CURLY BRACKET", lcbracket_},
{"RIGHT CURLY BRACKET", rcbracket_},
// built in functions
{"INPUT", input_},
{"OUTPUT", output_},
{"RANGE", range_},
// math operators
{"ADDITION", add_},
{"SUBSTRACTION", sub_},
{"MULTIPLACTION", mul_},
{"DIVISION", div_},
{"MODULO", mod_},
{"MORE THAN", more_than_},
{"LESS THAN", less_than_},
{"MORE THAN OR EQUAL", more_than_or_equal_},
{"LESS THAN OR EQUAL", less_than_or_equal_},
// logic operators
{"AND", and_},
{"OR", or_},
{"NOT", not_},
{"COMPARE", equal_},
{"NOT EQUAL", not_equal_},
// functions
{"FUNCTION DECLARATION", func_dec_},
{"FUNCTION CALL", func_call_},
// key words
{"BREAK", break_},
{"CONTINUE", continue_},
{"RETURN", return_},
// attributes
{"DOT", dot_},
{"ATTRIBUTE NODE", attribute_},
{"ATTRIBUTE TYPE", att_type_},
// built in attributes
{"indexof", indexof_},
{"split", split_},
{"trim", trim_},
{"count", count_},
{"base", base_},
{"starts with", starts_with_},
{"ends with", ends_with_},
// EOF
{"EOF", eof_},
{0, 0},
};
for (int i = 0; Types[i].string; i++)
{
if (Types[i].type == type)
return (Types[i].string);
}
ft_putstr(err, "Error: type not found '");
ft_putnbr(err, type);
ft_putstr(err, "'\n");
my_free_all();
exit(1);
return NULL;
}
void undeclared_error(char *location, Token *token, char *type)
{
ft_putchar(out, '\n');
txt_pos = token->txt_pos;
while (txt_pos > 0 && text[txt_pos - 1] != '\n')
txt_pos--;
while (text[txt_pos] && text[txt_pos] != '\n')
{
ft_putchar(out, text[txt_pos]);
txt_pos++;
}
ft_putchar(out, '\n');
print_space(token->column - ft_strlen(token->name));
ft_putstr(out, "^\n");
ft_fprintf(err, "Error %s: Undeclared %s in line '%d'\n", location, type, token->line);
}
int numberOfDigits(long long n)
{
int count = 0;
while (n != 0)
{
count++;
n /= 10;
}
return count;
}
void printFixedPoint(long number, int exponent)
{
long int_part = exponent ? number / pow_ten(exponent) : number;
long frac_part = exponent ? number % pow_ten(exponent) : 0;
ft_putnbr(out, int_part);
if (exponent)
{
for (int i = 0; i < exponent - numberOfDigits(frac_part); i++)
ft_putchar(out, '0');
ft_putnbr(out, frac_part);
}
}
// built in functions
void output(Token *token)
{
int fd = 0;
ft_putstr(fd, "\033[31m");
if (token)
{
switch (token->type)
{
case identifier_:
#if DEBUG
ft_printf("identifier: has name %s\n", token->name);
#else
undeclared_error("output", token, "variable");
#endif
break;
case characters_:
{
char *string = token->characters;
if (token->is_char)
ft_putchar(fd, string[0]);
else
{
int i = 0;
while (string[i])
{
for (int j = 0; special_characters[j].special; j++)
{
if (ft_strncmp(&string[i], special_characters[j].special, ft_strlen(special_characters[j].special)) == 0)
{
ft_putchar(fd, special_characters[j].replace);
i += ft_strlen(special_characters[j].special);
break;
}
else
{
ft_putchar(fd, string[i]);
i++;
break;
}
}
if (string[i] == '\0')
break;
}
}
break;
}
case number_:
{
// token->is_float = ft_putfloat(fd, token->number, 6);
// ft_printf("hey fix printing float\n");
printFixedPoint(token->number, token->exponent);
break;
}
case boolean_:
{
if (token->boolean)
ft_putstr(fd, "true");
else
ft_putstr(fd, "false");
break;
}
case array_:
{
ft_putstr(fd, "[ ");
for (int i = 0; token->array && token->array[i]; i++)
{
output(token->array[i]);
if (token->array[i + 1])
ft_putstr(fd, "\033[31m, ");
}
ft_putstr(fd, " \033[31m]");
break;
}
case obj_:
{
ft_putstr(fd, " { ");
for (int i = 0; token->object && token->object[i]; i++)
{
ft_putstr(fd, token->keys[i]);
ft_putstr(fd, ": ");
output(token->object[i]);
if (token->object[i + 1])
ft_putstr(fd, "\033[31m,");
}
ft_putstr(fd, " \033[31m}");
break;
}
case void_:
{
ft_putstr(fd, "(void)\n");
break;
}
case func_dec_:
{
ft_fprintf(out, "function declaration has name: %s\n", token->name);
break;
}
default:
{
ft_putstr(err, "Error in output can't output ");
ft_putstr(err, type_to_string(token->type));
ft_putstr(err, "\n");
my_free_all();
exit(1);
}
}
}
else
ft_putstr(fd, "NULL");
ft_putstr(fd, "\033[0m");
}
void see_token(Token *token)
{
#if DEBUG
if (token)
{
ft_printf("%s in line [", type_to_string(token->type));
if (token->line < 10)
ft_putstr(out, "00");
else if (token->line < 100)
ft_putstr(out, "0");
ft_printf("%d] in column [", token->line);
if (token->column < 10)
ft_putstr(out, "0");
ft_printf("%d]", token->column);
ft_printf(" in tab [");
if (token->tab < 10)
ft_putstr(out, "0");
ft_printf("%d]", token->tab);
if (token->name)
ft_printf(" name: %s, ", token->name);
switch (token->type)
{
case characters_:
{
ft_putstr(out, " value: ");
output(token);
break;
}
case number_:
{
ft_putstr(out, " value: ");
output(token);
break;
}
case boolean_:
{
ft_putstr(out, " value: ");
output(token);
break;
}
case array_:
{
ft_putstr(out, " value: \n");
ft_putstr(out, " ");
output(token);
break;
}
case obj_:
{
ft_putstr(out, " value: \n");
for (int i = 0; token->object && token->object[i]; i++)
{
ft_printf(" %s : ", token->keys[i]);
output(token->object[i]);
ft_putchar(out, '\n');
}
ft_putstr(out, " ");
break;
}
case void_:
{
ft_putstr(out, " (void)\n");
break;
}
default:
{
if (type_to_string(token->type) == NULL)
{
ft_putstr(err, "Unkown token type: ");
ft_putstr(err, type_to_string(token->type));
my_free_all();
exit(1);
}
}
}
}
else
ft_putstr(out, "(null token)");
ft_putchar(out, '\n');
#endif
}
void visualize_variables(void)
{
#if DEBUG
int i = 0;
ft_fprintf(out, "\n\nvariables: \n");
List *tmp = current;
while (tmp->prev)
tmp = tmp->prev;
// var_level is changeable don't think about putting it here
while (tmp)
{
ft_fprintf(out, " level %d, has %d variables:\n", i, tmp->val_index);
int j = 0;
while (j < tmp->val_index)
{
ft_putstr(out, " ");
output(tmp->variables[j]);
ft_putchar(out, '\n');
j++;
}
tmp = tmp->next;
i++;
}
if (FUNCTIONS[0])
{
ft_fprintf(out, "\nfunctions: \n");
i = 0;
while (FUNCTIONS[i])
{
ft_putstr(out, " ");
output(FUNCTIONS[i]->token);
ft_putchar(out, '\n');
i++;
}
}
ft_fprintf(out, "\n");
#endif
}