-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprinter.c
515 lines (494 loc) · 15.5 KB
/
printer.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#define PRINT_PREF for(int _i=0;_i<size;_i++)printf("%c",prefix[_i]);
//#define PRINT_NEXT(expr) if(expr){printf("\t├- ");prefix[size + 1] = '|';}else{printf("\t└- ");prefix[size + 1] = ' ';}prefix[size] = '\t';
#ifdef WIN32
#ifdef DEBUG
#define PRINT_NEXT(expr) if(expr){printf("\t├- ");prefix[size + 1] = '|';}else{printf("\t└- ");prefix[size + 1] = ' ';}prefix[size] = '\t';
#else
#define PRINT_NEXT(expr) if(expr){printf("\t%c- ",195);prefix[size + 1] = '|';}else{printf("\t%c- ",192);prefix[size + 1] = ' ';}prefix[size] = '\t';
#endif
#else
#define PRINT_NEXT(expr) if(expr){printf("\t├- ");prefix[size + 1] = '|';}else{printf("\t└- ");prefix[size + 1] = ' ';}prefix[size] = '\t';
#endif
char prefix[100];
void print_str(const struct string_st *res) {
printf("string (%zu): ", res->size);
for (int i = 0; i < res->size; i++) printf("%c", res->data[i]);
printf("\n");
}
void print_int(const struct integer_st *res) {
printf("integer : %ld\n", res->data);
}
void print_bool(const struct bool_st *res) {
printf("bool : %hhd\n", res->data);
}
void print_real(const struct real_st *res) {
printf("real : %f\n", res->data);
}
void print_array(const struct array_st *res, int size) {
printf("array (%zu):\n", res->size);
for (int i = 0; i < res->size; i++) {
PRINT_PREF
PRINT_NEXT(i + 1 < res->size)
print_obj(res->data[i], size + 2);
}
}
void print_error(const struct error_st* error){
printf("%s\n", error->type->data);
printf("%s\n", error->message->data);
printf("%zu\n", error->pos);
printf("%zu\n", error->line_num);
printf("%zu\n", error->line_pos);
}
void print_error_log(struct error_st* error, struct la_parser* F_parser){
if (!error->present) return;
printf("%s\n", error->type->data);
if (error->message->size > 0)
printf("%s\n", error->message->data);
printf("Line %zu: \n", error->line_num + 1);
for (size_t i = error->line_pos; i < F_parser->str_size; i++) {
if (F_parser->data[i] == '\n') break;
printf("%c", F_parser->data[i]);
}
printf("\n");
for (size_t i = error->line_pos; i < error->pos; i++) printf(" ");
printf("^\n");
}
void print_darray(const struct darray_st *res, int size) {
printf("double list (%zu):\n", res->size);
if(res->size != 0){
PRINT_PREF
PRINT_NEXT(1)
printf("[0] :\n");
size += 2;
for (int i = 0; i < res->size; i++) {
PRINT_PREF
PRINT_NEXT(i + 1 < res->size)
print_obj(res->data[0][i], size + 2);
}
size -= 2;
PRINT_PREF
PRINT_NEXT(0)
printf("[1] :\n");
size += 2;
for (int i = 0; i < res->size; i++) {
PRINT_PREF
PRINT_NEXT(i + 1 < res->size)
print_obj(res->data[1][i], size + 2);
}
}
}
void print_token(const struct token_st *res, int size) {
printf("Token : ");
switch (res->type) {
case TokenType_None:
printf("TokenType_None ");
break;
case TokenType_KeyWords:
printf("TokenType_KeyWords ");
break;
case TokenType_Identifier:
printf("TokenType_Identifier ");
break;
case TokenType_String:
printf("TokenType_String ");
break;
case TokenType_Int:
printf("TokenType_Int ");
break;
case TokenType_Special:
printf("TokenType_Special ");
break;
}
if (res->type == TokenType_Int) {
switch (res->subtype) {
case IntType_bin:
printf("IntType_bin ");
break;
case IntType_oct:
printf("IntType_oct ");
break;
case IntType_dec:
printf("IntType_dec ");
break;
case IntType_hex:
printf("IntType_hex ");
break;
case IntType_float:
printf("IntType_float ");
break;
}
}
if (res->type == TokenType_KeyWords) {
switch (res->subtype) {
case KeyWord_IF:
printf("KeyWord_IF ");
break;
case KeyWord_IN:
printf("KeyWord_IN ");
break;
case KeyWord_IS:
printf("KeyWord_IS ");
break;
case KeyWord_OR:
printf("KeyWord_OR ");
break;
case KeyWord_AND:
printf("KeyWord_AND ");
break;
case KeyWord_END:
printf("KeyWord_END ");
break;
case KeyWord_FOR:
printf("KeyWord_FOR ");
break;
case KeyWord_INT:
printf("KeyWord_INT ");
break;
case KeyWord_NOT:
printf("KeyWord_NOT ");
break;
case KeyWord_VAR:
printf("KeyWord_VAR ");
break;
case KeyWord_XOR:
printf("KeyWord_XOR ");
break;
case KeyWord_BOOL:
printf("KeyWord_BOOL ");
break;
case KeyWord_ELSE:
printf("KeyWord_ELSE ");
break;
case KeyWord_FUNC:
printf("KeyWord_FUNC ");
break;
case KeyWord_LOOP:
printf("KeyWord_LOOP ");
break;
case KeyWord_REAL:
printf("KeyWord_REAL ");
break;
case KeyWord_THEN:
printf("KeyWord_THEN ");
break;
case KeyWord_TRUE:
printf("KeyWord_TRUE ");
break;
case KeyWord_EMPTY:
printf("KeyWord_EMPTY ");
break;
case KeyWord_FALSE:
printf("KeyWord_FALSE ");
break;
case KeyWord_PRINT:
printf("KeyWord_PRINT ");
break;
case KeyWord_WHILE:
printf("KeyWord_WHILE ");
break;
}
}
printf("\n");
if (res->type == TokenType_Int || res->type == TokenType_Identifier || res->type == TokenType_String) {
PRINT_PREF
PRINT_NEXT(0)
for (int i = 0; i < res->size; i++) printf("%c", res->data[i]);
printf("\n");
} else if (res->type == TokenType_Special) {
PRINT_PREF
PRINT_NEXT(0)
switch (res->subtype) {
case Special_MOD:
printf("%c", '%');
break;
case Special_AND:
printf("&");
break;
case Special_MUL:
printf("*");
break;
case Special_ADD:
printf("+");
break;
case Special_SUB:
printf("-");
break;
case Special_DIV:
printf("/");
break;
case Special_XOR:
printf("^");
break;
case Special_OR:
printf("|");
break;
case Special_LSHIFT:
printf("<<");
break;
case Special_RSHIFT:
printf(">>");
break;
case Special_LSB:
printf("(");
break;
case Special_RSB:
printf(")");
break;
case Special_LSQB:
printf("[");
break;
case Special_RSQB:
printf("]");
break;
case Special_LCB:
printf("{");
break;
case Special_RCB:
printf("}");
break;
case Special_LESS:
printf("<");
break;
case Special_GREATER:
printf(">");
break;
case Special_EQ_LESS:
printf("<=");
break;
case Special_EQ_GREATER:
printf(">=");
break;
case Special_EQ_NOT:
printf("/=");
break;
case Special_EQ_EQ:
printf("=");
break;
case Special_COMMA:
printf(",");
break;
case Special_DOT:
printf(".");
break;
case Special_SEMI:
printf(";");
break;
case Special_EQ:
printf(":=");
break;
}
printf("\n");
}
}
void print_node(const struct node_st *res, int size) {
printf("Node : ");
switch (res->main_type) {
case MainType_None:
printf("MainType_None ");
break;
case MainType_Expr:
printf("MainType_Expr ");
break;
case MainType_Oper:
printf("MainType_Oper ");
break;
case MainType_Stmt:
printf("MainType_Stmt ");
break;
case MainType_Type:
printf("MainType_Type ");
break;
}
if (res->main_type == MainType_Expr) {
switch (res->type) {
case PrimType_Tuple:
printf("PrimType_Tuple ");
break;
case PrimType_List:
printf("PrimType_List ");
break;
case PrimType_Ident_new:
printf("PrimType_Ident_new ");
break;
case PrimType_Ident_get:
printf("PrimType_Ident_get ");
break;
case PrimType_Literal:
printf("PrimType_Literal ");
break;
case PrimType_Attrib:
printf("PrimType_Attrib ");
break;
case PrimType_Subscript:
printf("PrimType_Subscript ");
break;
case PrimType_Call:
printf("PrimType_Call ");
break;
case PrimType_Is:
printf("PrimType_Is ");
break;
case PrimType_Lambda:
printf("PrimType_Lambda ");
break;
}
}
if (res->main_type == MainType_Oper) {
switch (res->type) {
case ExprType_U:
printf("ExprType_U ");
break;
case ExprType_M:
printf("ExprType_M ");
break;
case ExprType_A:
printf("ExprType_A ");
break;
case ExprType_Shift:
printf("ExprType_Shift ");
break;
case ExprType_And:
printf("ExprType_And ");
break;
case ExprType_Xor:
printf("ExprType_Xor ");
break;
case ExprType_Or:
printf("ExprType_Or ");
break;
case ExprType_Comp:
printf("ExprType_Comp ");
break;
case ExprType_NotTest:
printf("ExprType_NotTest ");
break;
case ExprType_AndTest:
printf("ExprType_AndTest ");
break;
case ExprType_OrTest:
printf("ExprType_OrTest ");
break;
case ExprType_XorTest:
printf("ExprType_XorTest ");
break;
}
}
if (res->main_type == MainType_Stmt) {
switch (res->type) {
case StmtType_If:
printf("StmtType_If ");
break;
case StmtType_While:
printf("StmtType_While ");
break;
case StmtType_For:
printf("StmtType_For ");
break;
case StmtType_Try:
printf("StmtType_Try ");
break;
case StmtType_Params:
printf("StmtType_Params ");
break;
case StmtType_Func:
printf("StmtType_Func ");
break;
case StmtType_Func_Body:
printf("StmtType_Func_Body ");
break;
case StmtType_Decl:
printf("StmtType_Decl ");
break;
case StmtType_Annot:
printf("StmtType_Annot ");
break;
case StmtType_Assign:
printf("StmtType_Assign ");
break;
case StmtType_Return:
printf("StmtType_Return ");
break;
case StmtType_List:
printf("StmtType_List ");
break;
case StmtType_Print:
printf("StmtType_Print ");
break;
}
}
if (res->main_type == MainType_Type) {
switch (res->type) {
case Type_int:
printf("Type_int ");
break;
case Type_real:
printf("Type_real ");
break;
case Type_bool:
printf("Type_bool ");
break;
case Type_string:
printf("Type_string ");
break;
case Type_array:
printf("Type_array ");
break;
case Type_tuple:
printf("Type_tuple ");
break;
case Type_func:
printf("Type_func ");
break;
}
}
printf("\n");
if (res->data != NULL) {
PRINT_PREF
PRINT_NEXT(!array_is_null(res->tokens) || !array_is_null(res->next) || res->closure != NULL)
print_obj(res->data, size + 2);
}
if (!array_is_null(res->tokens)) {
PRINT_PREF
PRINT_NEXT(!array_is_null(res->next) || res->closure != NULL)
print_array(res->tokens, size + 2);
}
if (!array_is_null(res->next)) {
PRINT_PREF
PRINT_NEXT(res->closure != NULL)
print_array(res->next, size + 2);
}
if (res->closure != NULL) {
PRINT_PREF
PRINT_NEXT(0)
printf("Closure : ");
print_obj(res->closure, size + 2);
}
}
void print_op_attrib(const struct op_attrib *res, int size) {
printf("Attrib : ");
print_str(res->name);
if (res->data != NULL) {
PRINT_PREF
PRINT_NEXT(0)
print_obj(res->data, size + 2);
}
}
void print_obj(const struct object_st *res, int size) {
// printf("object : (%p)\n", res);
// PRINT_PREF
// PRINT_NEXT(0)
if (res == NULL || res->type == NONE_TYPE) printf("None\n");
else if (res->type == OBJECT_TYPE) return print_obj(res->data, size);
else if (res->type == STRING_TYPE) return print_str(res->data);
else if (res->type == INTEGER_TYPE) return print_int(res->data);
else if (res->type == BOOL_TYPE) return print_bool(res->data);
else if (res->type == REAL_TYPE) return print_real(res->data);
else if (res->type == ARRAY_TYPE) return print_array(res->data, size);
else if (res->type == DARRAY_TYPE) return print_darray(res->data, size);
else if (res->type == TOKEN_TYPE) return print_token(res->data, size);
else if (res->type == NODE_TYPE) return print_node(res->data, size);
else if (res->type == OP_ATTRIB_TYPE) return print_op_attrib(res->data, size);
else if (res->type == OP_BLOCK_TYPE) printf("block\n");
else if (res->type == MAP_TYPE) printf("map\n");
else printf("something\n");
}