-
Notifications
You must be signed in to change notification settings - Fork 0
/
visitor.c
575 lines (504 loc) · 16.9 KB
/
visitor.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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "visitor.h"
static int depth = 0;
static void increment() {
depth++;
}
static void decrement() {
depth--;
}
void print_depth() {
int i = 0;
for(i = 0; i < depth; i++) {
fprintf(stderr, " ");
}
}
static void enter_mulexpr(Expression* expr) {
print_depth();
fprintf(stderr, "enter mulexpr : *\n");
increment();
}
static void leave_mulexpr(Expression* expr) {
decrement();
print_depth();
fprintf(stderr, "leave mulexpr\n");
}
static void enter_divexpr(Expression* expr) {
print_depth();
fprintf(stderr, "enter divexpr : / \n");
increment();
}
static void leave_divexpr(Expression* expr) {
decrement();
print_depth();
fprintf(stderr, "leave divexpr\n");
}
static void enter_modexpr(Expression* expr) {
print_depth();
fprintf(stderr, "enter modexpr : mod \n");
increment();
}
static void leave_modexpr(Expression* expr) {
decrement();
print_depth();
fprintf(stderr, "leave modexpr\n");
}
static void enter_addexpr(Expression* expr) {
print_depth();
fprintf(stderr, "enter addexpr : +\n");
increment();
}
static void leave_addexpr(Expression* expr) {
decrement();
print_depth();
fprintf(stderr, "leave addexpr\n");
}
static void enter_subepr(Expression* expr) {
print_depth();
fprintf(stderr, "enter subexpr : -\n");
increment();
}
static void leave_subexpr(Expression* expr) {
decrement();
print_depth();
fprintf(stderr, "leave subexpr\n");
}
static void enter_intexpr(Expression* expr) {
print_depth();
fprintf(stderr, "enter intexpr : %d\n", expr->u.int_value);
increment();
}
static void leave_intexpr(Expression* expr) {
decrement();
print_depth();
fprintf(stderr, "leave intexpr\n");
}
static void enter_doubleexpr(Expression* expr) {
print_depth();
fprintf(stderr, "enter doubleexpr : %lf\n", expr->u.double_value);
increment();
}
static void leave_doubleexpr(Expression* expr) {
decrement();
print_depth();
fprintf(stderr, "leave doubleexpr\n");
}
static void enter_stringexpr(Expression *expr) {
int i;
print_depth();
fprintf(stderr, "enter stringexpr : str = %s, len = %d\n", expr->u.string_value, (int)strlen(expr->u.string_value));
/*
for (i = 0; i < strlen(expr->u.string_value); ++i) {
fprintf(stderr, "0x%x\n", expr->u.string_value[i]);
}
*/
increment();
}
static void leave_stringexpr(Expression *expr) {
decrement();
print_depth();
fprintf(stderr, "leave stringexpr\n");
}
static void enter_nullexpr(Expression* expr) {
print_depth();
fprintf(stderr, "enter nullexpr\n");
increment();
}
static void leave_nullexpr(Expression* expr) {
decrement();
print_depth();
fprintf(stderr, "leave nullexpr\n");
}
static void enter_boolean_expression(Expression* expr) {
print_depth();
fprintf(stderr, "enter booleanexpr = %d\n", expr->u.boolean_value);
increment();
}
static void leave_boolean_expression(Expression* expr) {
decrement();
print_depth();
fprintf(stderr, "leave booleanexpr\n");
}
static void enter_identifier_expression(Expression* expr) {
print_depth();
fprintf(stderr, "enter identifierexpr = %s\n", expr->u.identifier);
increment();
}
static void leave_identifier_expression(Expression* expr) {
decrement();
print_depth();
fprintf(stderr, "leave identifierexpr\n");
}
static void enter_functioncall_expression(Expression* expr) {
print_depth();
fprintf(stderr, "enter functioncall_expr = %s\n", expr->u.function_call_expression.identifier);
increment();
}
static void leave_functioncall_expression(Expression* expr) {
decrement();
print_depth();
fprintf(stderr, "leave functioncall_expr\n");
}
static void enter_minus_expression(Expression* expr) {
print_depth();
fprintf(stderr, "enter minusexpr: -\n");
increment();
}
static void leave_minus_expression(Expression* expr) {
decrement();
print_depth();
fprintf(stderr, "leave minusexpr\n");
}
static void enter_gt_expression(Expression* expr) {
print_depth();
fprintf(stderr, "enter gt expr: >\n");
increment();
}
static void leave_gt_expression(Expression* expr) {
decrement();
print_depth();
fprintf(stderr, "leave gt expr\n");
}
static void enter_ge_expression(Expression* expr) {
print_depth();
fprintf(stderr, "enter ge expr : >=\n");
increment();
}
static void leave_ge_expression(Expression* expr) {
decrement();
print_depth();
fprintf(stderr, "leave ge expr\n");
}
static void enter_lt_expression(Expression* expr) {
print_depth();
fprintf(stderr, "enter lt expr: <\n");
increment();
}
static void leave_lt_expression(Expression* expr) {
decrement();
print_depth();
fprintf(stderr, "leave lt expr\n");
}
static void enter_le_expression(Expression* expr) {
print_depth();
fprintf(stderr, "enter le expr: <=\n");
increment();
}
static void leave_le_expression(Expression* expr) {
decrement();
print_depth();
fprintf(stderr, "leave le expr\n");
}
static void enter_eq_expression(Expression* expr) {
print_depth();
fprintf(stderr, "enter eq expr: ==\n");
increment();
}
static void leave_eq_expression(Expression* expr) {
decrement();
print_depth();
fprintf(stderr, "leave eq expr\n");
}
static void enter_ne_expression(Expression* expr) {
print_depth();
fprintf(stderr, "enter ne expr: !=\n");
increment();
}
static void leave_ne_expression(Expression* expr) {
decrement();
print_depth();
fprintf(stderr, "leave ne expr\n");
}
static void enter_logical_and_expression(Expression* expr) {
print_depth();
fprintf(stderr, "enter logical and expr: &&\n");
increment();
}
static void leave_logical_and_expression(Expression* expr) {
decrement();
print_depth();
fprintf(stderr, "leave logical and expr\n");
}
static void enter_logical_or_expression(Expression* expr) {
print_depth();
fprintf(stderr, "enter logical or expr: ||\n");
increment();
}
static void leave_logical_or_expression(Expression* expr) {
decrement();
print_depth();
fprintf(stderr, "leave logical or expr\n");
}
static void enter_assignment_expression(Expression* expr) {
print_depth();
fprintf(stderr, "enter assignment expr : \n");
increment();
}
static void leave_assignment_expression(Expression* expr) {
decrement();
print_depth();
fprintf(stderr, "leave assignment expr\n");
}
static void enter_index_expression(Expression* expr) {
print_depth();
fprintf(stderr, "enter index expr : \n");
increment();
}
static void leave_index_expression(Expression* expr) {
decrement();
print_depth();
fprintf(stderr, "leave index expr\n");
}
static void enter_increment_expression(Expression* expr) {
print_depth();
fprintf(stderr, "enter increment expr : \n");
increment();
}
static void leave_increment_expression(Expression* expr) {
decrement();
print_depth();
fprintf(stderr, "leave increment expr : \n");
}
static void enter_decrement_expression(Expression* expr) {
print_depth();
fprintf(stderr, "enter decrement expr : \n");
increment();
}
static void leave_decrement_expression(Expression* expr) {
decrement();
print_depth();
fprintf(stderr, "leave decrement expr : \n");
}
static void enter_methodcall_expression(Expression* expr) {
print_depth();
fprintf(stderr, "enter methodcall expr : \n");
increment();
}
static void leave_methodcall_expression(Expression* expr) {
decrement();
print_depth();
fprintf(stderr, "leave methodcall expr : \n");
}
static void enter_array_expression(Expression* expr) {
print_depth();
fprintf(stderr, "enter array expr : \n");
increment();
}
static void leave_array_expression(Expression* expr) {
decrement();
print_depth();
fprintf(stderr, "leave array expr : \n");
}
/* Statement */
static void enter_expression_statement(Statement* stmt) {
print_depth();
fprintf(stderr, "enter expression stmt\n");
increment();
}
static void leave_expression_statement(Statement* stmt) {
decrement();
print_depth();
fprintf(stderr, "leave expression stmt\n");
}
static void enter_global_statement(Statement* stmt) {
print_depth();
fprintf(stderr, "enter global statement\n");
increment();
}
static void leave_global_statement(Statement* stmt) {
decrement();
print_depth();
fprintf(stderr, "leave global statement\n");
}
static void enter_return_statement(Statement* stmt) {
print_depth();
fprintf(stderr, "enter return statement\n");
increment();
}
static void leave_return_statement(Statement* stmt) {
decrement();
print_depth();
fprintf(stderr, "leave return statement\n");
}
static void enter_break_statement(Statement* stmt) {
print_depth();
fprintf(stderr, "enter break statement\n");
increment();
}
static void leave_break_statement(Statement* stmt) {
decrement();
print_depth();
fprintf(stderr, "leave break statement\n");
}
static void enter_continue_statement(Statement* stmt) {
print_depth();
fprintf(stderr, "enter continue statement\n");
increment();
}
static void leave_continue_statement(Statement* stmt) {
decrement();
print_depth();
fprintf(stderr, "leave continue statement\n");
}
static void enter_while_statement(Statement* stmt) {
print_depth();
fprintf(stderr, "enter while statement\n");
increment();
}
static void leave_while_statement(Statement* stmt) {
decrement();
print_depth();
fprintf(stderr, "leave while statement\n");
}
static void enter_for_statement(Statement* stmt) {
print_depth();
fprintf(stderr, "enter for statement\n");
increment();
}
static void leave_for_statement(Statement* stmt) {
decrement();
print_depth();
fprintf(stderr, "leave for statement\n");
}
static void enter_if_statement(Statement* stmt) {
print_depth();
fprintf(stderr, "enter if statement\n");
increment();
}
static void leave_if_statement(Statement* stmt) {
decrement();
print_depth();
fprintf(stderr, "leave if statement\n");
}
/* Function */
static void enter_mas_func(FunctionDefinition* func) {
print_depth();
fprintf(stderr, "enter mas func : %s\n", func->name);
increment();
}
static void leave_mas_func(FunctionDefinition* func) {
decrement();
print_depth();
fprintf(stderr, "leave mas func\n");
}
static void enter_native_func(FunctionDefinition* func) {
print_depth();
fprintf(stderr, "enter native func : %s\n", func->name);
increment();
}
static void leave_native_func(FunctionDefinition* func) {
decrement();
print_depth();
fprintf(stderr, "leave native func\n");
}
Visitor* create_visitor() {
visit_expr* enter_list;
visit_expr* leave_list;
visit_stmt* enter_stmt_list;
visit_stmt* leave_stmt_list;
visit_func* enter_func_list;
visit_func* leave_func_list;
Visitor* visitor = (Visitor*)malloc(sizeof(Visitor));
/* Expression list creation */
enter_list = (visit_expr*)malloc(sizeof(visit_expr) * EXPRESSION_TYPE_COUNT_PLUS_1);
leave_list = (visit_expr*)malloc(sizeof(visit_expr) * EXPRESSION_TYPE_COUNT_PLUS_1);
/* Statement list creation */
enter_stmt_list = (visit_stmt*)malloc(sizeof(visit_stmt) * STATEMENT_TYPE_COUNT_PLUS_1);
leave_stmt_list = (visit_stmt*)malloc(sizeof(visit_stmt) * STATEMENT_TYPE_COUNT_PLUS_1);
/* Function list creation */
enter_func_list = (visit_func*)malloc(sizeof(visit_func) * FUNCTION_TYPE_PLUS1);
leave_func_list = (visit_func*)malloc(sizeof(visit_func) * FUNCTION_TYPE_PLUS1);
enter_list[MUL_EXPRESSION] = enter_mulexpr;
enter_list[DIV_EXPRESSION] = enter_divexpr;
enter_list[MOD_EXPRESSION] = enter_modexpr;
enter_list[ADD_EXPRESSION] = enter_addexpr;
enter_list[SUB_EXPRESSION] = enter_subepr;
enter_list[INT_EXPRESSION] = enter_intexpr;
enter_list[DOUBLE_EXPRESSION] = enter_doubleexpr;
enter_list[STRING_EXPRESSION] = enter_stringexpr;
enter_list[NULL_EXPRESSION] = enter_nullexpr;
enter_list[BOOLEAN_EXPRESSION] = enter_boolean_expression;
enter_list[IDENTIFIER_EXPRESSION] = enter_identifier_expression;
enter_list[FUNCTION_CALL_EXPRESSION] = enter_functioncall_expression;
enter_list[MINUS_EXPRESSION] = enter_minus_expression;
enter_list[GT_EXPRESSION] = enter_gt_expression;
enter_list[GE_EXPRESSION] = enter_ge_expression;
enter_list[LT_EXPRESSION] = enter_lt_expression;
enter_list[LE_EXPRESSION] = enter_le_expression;
enter_list[EQ_EXPRESSION] = enter_eq_expression;
enter_list[NE_EXPRESSION] = enter_ne_expression;
enter_list[LOGICAL_AND_EXPRESSION] = enter_logical_and_expression;
enter_list[LOGICAL_OR_EXPRESSION] = enter_logical_or_expression;
enter_list[ASSIGN_EXPRESSION] = enter_assignment_expression;
enter_list[INDEX_EXPRESSION] = enter_index_expression;
enter_list[INCREMENT_EXPRESSION] = enter_increment_expression;
enter_list[DECREMENT_EXPRESSION] = enter_decrement_expression;
enter_list[METHOD_CALL_EXPRESSION] = enter_methodcall_expression;
enter_list[ARRAY_EXPRESSION] = enter_array_expression;
leave_list[MUL_EXPRESSION] = leave_mulexpr;
leave_list[DIV_EXPRESSION] = leave_divexpr;
leave_list[MOD_EXPRESSION] = leave_modexpr;
leave_list[ADD_EXPRESSION] = leave_addexpr;
leave_list[SUB_EXPRESSION] = leave_subexpr;
leave_list[INT_EXPRESSION] = leave_intexpr;
leave_list[DOUBLE_EXPRESSION] = leave_doubleexpr;
leave_list[STRING_EXPRESSION] = leave_stringexpr;
leave_list[NULL_EXPRESSION] = leave_nullexpr;
leave_list[BOOLEAN_EXPRESSION] = leave_boolean_expression;
leave_list[IDENTIFIER_EXPRESSION] = leave_identifier_expression;
leave_list[FUNCTION_CALL_EXPRESSION] = leave_functioncall_expression;
leave_list[MINUS_EXPRESSION] = leave_minus_expression;
leave_list[GT_EXPRESSION] = leave_gt_expression;
leave_list[GE_EXPRESSION] = leave_ge_expression;
leave_list[LT_EXPRESSION] = leave_lt_expression;
leave_list[LE_EXPRESSION] = leave_le_expression;
leave_list[EQ_EXPRESSION] = leave_eq_expression;
leave_list[NE_EXPRESSION] = leave_ne_expression;
leave_list[LOGICAL_AND_EXPRESSION] = leave_logical_and_expression;
leave_list[LOGICAL_OR_EXPRESSION] = leave_logical_or_expression;
leave_list[ASSIGN_EXPRESSION] = leave_assignment_expression;
leave_list[INDEX_EXPRESSION] = leave_index_expression;
leave_list[INCREMENT_EXPRESSION] = leave_increment_expression;
leave_list[DECREMENT_EXPRESSION] = leave_decrement_expression;
leave_list[METHOD_CALL_EXPRESSION] = leave_methodcall_expression;
leave_list[ARRAY_EXPRESSION] = leave_array_expression;
/* Statement */
enter_stmt_list[EXPRESSION_STATEMENT] = enter_expression_statement;
enter_stmt_list[GLOBAL_STATEMENT] = enter_global_statement;
enter_stmt_list[RETURN_STATEMENT] = enter_return_statement;
enter_stmt_list[BREAK_STATEMENT] = enter_break_statement;
enter_stmt_list[CONTINUE_STATEMENT] = enter_continue_statement;
enter_stmt_list[WHILE_STATEMENT] = enter_while_statement;
enter_stmt_list[FOR_STATEMENT] = enter_for_statement;
enter_stmt_list[IF_STATEMENT] = enter_if_statement;
leave_stmt_list[EXPRESSION_STATEMENT] = leave_expression_statement;
leave_stmt_list[GLOBAL_STATEMENT] = leave_global_statement;
leave_stmt_list[RETURN_STATEMENT] = leave_return_statement;
leave_stmt_list[BREAK_STATEMENT] = leave_break_statement;
leave_stmt_list[CONTINUE_STATEMENT] = leave_continue_statement;
leave_stmt_list[WHILE_STATEMENT] = leave_while_statement;
leave_stmt_list[FOR_STATEMENT] = leave_for_statement;
leave_stmt_list[IF_STATEMENT] = leave_if_statement;
/* Function */
enter_func_list[MAS_FUNCTION] = enter_mas_func;
enter_func_list[NATIVE_FUNCTION] = enter_native_func;
leave_func_list[MAS_FUNCTION] = leave_mas_func;
leave_func_list[NATIVE_FUNCTION] = leave_native_func;
visitor->enter_list = enter_list;
visitor->leave_list = leave_list;
visitor->enter_stmt_list = enter_stmt_list;
visitor->leave_stmt_list = leave_stmt_list;
visitor->enter_func_list = enter_func_list;
visitor->leave_func_list = leave_func_list;
return visitor;
}
/*
int main(void) {
Visitor* visitor = create_visitor();
fprintf(stderr, "visitor = %p\n", visitor);
visitor->enter_list[ADD_EXPRESSION](NULL);
return 0;
}
*/