-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpretty_print.c
executable file
·150 lines (120 loc) · 5.32 KB
/
pretty_print.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
#include "pretty_print.h"
#include "list.h"
#include "type.h"
#include "utils.h"
#include <stdio.h>
static void pprint_(AST* ast, int indent);
static void pprint(AST* ast, int indent);
void pretty_print(AST* ast)
{
pprint(ast, 0);
}
static void pprint(AST* ast, int indent)
{
if (ast != NULL)
pprint_(ast, indent);
}
static void print_binexpr(int indent, char* rep, AST* ast)
{
indentation(indent);
printf("%s\n",rep);
pprint(ast->d.be.lhs_, indent+2);
pprint(ast->d.be.rhs_, indent+2);
}
static void pprint_(AST* ast, int indent)
{
switch(ast->kind_)
{
case K_NUM: indentation(indent);
printf("%d\n",ast->d.val_);
break;
case BE_LT: print_binexpr(indent, "<", ast);
break;
case BE_LTE: print_binexpr(indent, "<=", ast);
break;
case BE_GT: print_binexpr(indent, ">", ast);
break;
case BE_GTE: print_binexpr(indent, ">=", ast);
break;
case BE_EQ: print_binexpr(indent, "==", ast);
break;
case BE_NEQ: print_binexpr(indent, "!=", ast);
break;
case BE_ADD: print_binexpr(indent, "+", ast);
break;
case BE_SUB: print_binexpr(indent, "-", ast);
break;
case BE_MUL: print_binexpr(indent, "*", ast);
break;
case BE_DIV: print_binexpr(indent, "/", ast);
break;
case K_CALL: indentation(indent);
printf("callee: %s\n", ast->d.call.name_);
for (struct node* tmp = ast->d.call.args_->head; tmp != NULL; tmp = tmp->next)
pprint(tmp->data, indent+2);
break;
case E_ASSIGN: indentation(indent);
printf("=\n");
pprint(ast->d.assign.to_, indent+2);
pprint(ast->d.assign.expr_, indent+2);
break;
case ARRAY_ACCESS: indentation(indent);
printf("Array name: %s\n",ast->d.access.name_);
pprint(ast->d.access.expr_, indent+2);
break;
case VAR_NAME: indentation(indent);
printf("variable name: %s\n",ast->d.variable.name_);
break;
case RETURN: indentation(indent);
printf("Return statement\n");
pprint(ast->d.ret.expr_, indent+2);
break;
case WHILE: indentation(indent);
printf("While statement\n");
pprint(ast->d.while_loop.expr_, indent+2);
pprint(ast->d.while_loop.stmt_, indent+2);
break;
case IF_THEN: indentation(indent);
printf("If then statement\n");
pprint(ast->d.if_then.expr_, indent+2);
pprint(ast->d.if_then.if_stmt_, indent+2);
if(ast->d.if_then.else_stmt_ != NULL)
{
indentation(indent);
printf("else\n");
pprint(ast->d.if_then.else_stmt_, indent+2);
}
break;
case COMPOUND: indentation(indent);
printf("Declarations\n");
for (struct node* tmp = ast->d.compound.decls_->head; tmp != NULL; tmp = tmp->next)
pprint(tmp->data, indent+2);
indentation(indent);
printf("Statements\n");
for (struct node* tmp = ast->d.compound.stmts_->head; tmp != NULL; tmp = tmp->next)
pprint(tmp->data, indent+2);
break;
case PARAM: indentation(indent);
printf("%s %s%s\n",
ast->d.param.type_spec_ == TYPE_INT ? "int" : "void",
ast->d.param.name_,
ast->d.param.ref_ == 0 ? "" : " []");
break;
case FUNCTION: indentation(indent);
printf("%s %s\n",
ast->d.function.ret_type_ == TYPE_INT ? "int" : "void", ast->d.function.name_);
indentation(indent+2);
printf("Parameters\n");
for (struct node* tmp = ast->d.function.params_->head; tmp != NULL; tmp = tmp->next)
pprint(tmp->data, indent+2);
pprint(ast->d.function.body_,indent+2);
break;
case VAR_DECL: indentation(indent);
printf("%s %s %d %s\n",
ast->d.var_decl.type_spec_ == TYPE_INT ? "int" : "void",
ast->d.var_decl.name_,
ast->d.var_decl.ref_ == 0 ? 1 : ast->d.var_decl.size_,
ast->d.var_decl.ref_ == 0 ? "" : " []");
break;
}
}