4 #include "graphprinter_visitor.h"
6 static void _print_arrow(struct AstNode
*node
);
7 static void _print_symbol_table(struct AstNode
*node
);
8 static void _print_symbols(Symbol
*symbol
);
13 Visitor
*visitor
= (Visitor
*) malloc (sizeof(Visitor
));
15 visitor
->visit_program
= &graphprinter_visit_program
;
16 visitor
->visit_programdecl
= &graphprinter_visit_programdecl
;
17 visitor
->visit_vardecl_list
= &graphprinter_visit_vardecl_list
;
18 visitor
->visit_vardecl
= &graphprinter_visit_simplenode
;
19 visitor
->visit_identifier_list
= &graphprinter_visit_identifier_list
;
20 visitor
->visit_procfunc_list
= &graphprinter_visit_procfunc_list
;
21 visitor
->visit_procedure
= &graphprinter_visit_procfunc
;
22 visitor
->visit_function
= &graphprinter_visit_procfunc
;
23 visitor
->visit_param_list
= &graphprinter_visit_param_list
;
24 visitor
->visit_parameter
= &graphprinter_visit_parameter
;
25 visitor
->visit_statement_list
= &graphprinter_visit_statement_list
;
26 visitor
->visit_printint_stmt
= &graphprinter_visit_simplenode
;
27 visitor
->visit_printchar_stmt
= &graphprinter_visit_simplenode
;
28 visitor
->visit_printbool_stmt
= &graphprinter_visit_simplenode
;
29 visitor
->visit_printline_stmt
= &graphprinter_visit_simplenode
;
30 visitor
->visit_assignment_stmt
= &graphprinter_visit_simplenode
;
31 visitor
->visit_if_stmt
= &graphprinter_visit_simplenode
;
32 visitor
->visit_while_stmt
= &graphprinter_visit_simplenode
;
33 visitor
->visit_for_stmt
= &graphprinter_visit_simplenode
;
34 visitor
->visit_rel_expr
= &graphprinter_visit_binary_expr
;
35 visitor
->visit_add_expr
= &graphprinter_visit_binary_expr
;
36 visitor
->visit_mul_expr
= &graphprinter_visit_binary_expr
;
37 visitor
->visit_notfactor
= &graphprinter_visit_simplenode
;
38 visitor
->visit_call
= &graphprinter_visit_call
;
39 visitor
->visit_callparam_list
= &graphprinter_visit_callparam_list
;
40 visitor
->visit_callparam
= &graphprinter_visit_callparam
;
41 visitor
->visit_identifier
= &graphprinter_visit_identifier
;
42 visitor
->visit_literal
= &graphprinter_visit_literal
;
43 visitor
->visit_add_op
= NULL
;
44 visitor
->visit_mul_op
= NULL
;
45 visitor
->visit_rel_op
= NULL
;
46 visitor
->visit_not_op
= NULL
;
52 graphprinter_visit_program(struct _Visitor
*visitor
, struct AstNode
*node
)
54 printf("/* toypasc AST graph. */\n");
55 printf("digraph {\n");
57 printf("\tremincross=true;\n");
58 printf("\tordering=out;\n");
59 printf("\tcompound=true;\n");
60 printf("\tranksep=1.0;\n");
61 printf("\tnode [fontsize=11,fontname=Courier];\n");
62 printf("\tedge [color="COLOR_EDGE_GROUP
"];\n\n");
64 printf("\tnode_%x [label=\"%s\",fontsize=16,fontname=Courier,",
66 printf("style=filled,color=black,fillcolor="COLOR_FILL_GLOBAL
"];\n");
68 _print_symbol_table(node
);
70 ast_node_accept_children(node
->children
, visitor
);
76 graphprinter_visit_simplenode (struct _Visitor
*visitor
, struct AstNode
*node
)
79 printf("\tnode_%x [label=\"%s\\n[line: %d]\",style=filled,",
80 node
, node
->name
, node
->linenum
);
81 printf("fillcolor="COLOR_FILL_COMMON
",color=%s];\n",
82 (node
->type
== ERROR
) ? COLOR_EDGE_ERROR
: COLOR_FILL_COMMON
);
83 ast_node_accept_children(node
->children
, visitor
);
87 graphprinter_visit_programdecl(struct _Visitor
*visitor
, struct AstNode
*node
)
90 printf("\tnode_%x [label=\"%s\\n[line: %d]\",style=filled,",
91 node
, node
->name
, node
->linenum
);
92 printf("color="COLOR_EDGE_GROUP
",fillcolor="COLOR_FILL_COMMON
"];\n");
93 ast_node_accept_children(node
->children
, visitor
);
97 graphprinter_visit_vardecl_list (struct _Visitor
*visitor
, struct AstNode
*node
)
100 printf("\tnode_%x [label=\"%s\",style=filled,", node
, node
->name
);
101 printf("color="COLOR_EDGE_GROUP
",fillcolor="COLOR_FILL_COMMON
"];\n");
102 printf("\nsubgraph cluster_%x {\n\tstyle=dotted;\n", node
);
103 ast_node_accept_children(node
->children
, visitor
);
108 graphprinter_visit_identifier_list (struct _Visitor
*visitor
, struct AstNode
*node
)
111 printf("\tnode_%x [label=\"%s\",style=filled,", node
, node
->name
);
112 printf("color="COLOR_EDGE_GROUP
",fillcolor="COLOR_FILL_COMMON
"];\n");
113 printf("\nsubgraph cluster_%x {\n\tstyle=dotted;\n", node
);
114 ast_node_accept_children(node
->children
, visitor
);
119 graphprinter_visit_procfunc_list (struct _Visitor
*visitor
, struct AstNode
*node
)
122 printf("\tnode_%x [label=\"%s\",style=filled,", node
, node
->name
);
123 printf("color="COLOR_EDGE_GROUP
",fillcolor="COLOR_FILL_COMMON
"];\n");
124 ast_node_accept_children(node
->children
, visitor
);
128 graphprinter_visit_procfunc (struct _Visitor
*visitor
, struct AstNode
*node
)
131 printf("\tnode_%x [label=\"%s\\n<%s>\\n[line: %d]\",style=filled,",
132 node
, node
->name
, type_get_lexeme(node
->type
), node
->linenum
);
133 printf("color=blue,fillcolor="COLOR_EDGE_FUNCT
"];\n");
134 printf("\nsubgraph cluster_%x {\n\tstyle=dotted;\n", node
);
136 _print_symbol_table(node
);
137 ast_node_accept_children(node
->children
, visitor
);
142 graphprinter_visit_param_list (struct _Visitor
*visitor
, struct AstNode
*node
)
145 printf("\tnode_%x [label=\"%s\",style=filled,", node
, node
->name
);
146 printf("color="COLOR_EDGE_GROUP
",fillcolor="COLOR_FILL_COMMON
"];\n");
147 printf("\nsubgraph cluster_%x {\n\tstyle=dotted;\n", node
);
148 ast_node_accept_children(node
->children
, visitor
);
153 graphprinter_visit_parameter (struct _Visitor
*visitor
, struct AstNode
*node
)
156 printf("\tnode_%x [label=\"%s\",style=filled,", node
, node
->name
);
157 printf("fillcolor="COLOR_FILL_COMMON
",color=%s];\n",
158 (node
->type
== ERROR
) ? COLOR_EDGE_ERROR
: COLOR_FILL_COMMON
);
159 ast_node_accept_children(node
->children
, visitor
);
163 graphprinter_visit_statement_list (struct _Visitor
*visitor
, struct AstNode
*node
)
166 printf("\tnode_%x [label=\"%s\",style=filled,", node
, node
->name
);
167 printf("color="COLOR_EDGE_GROUP
",fillcolor="COLOR_FILL_COMMON
"];\n");
168 printf("\nsubgraph cluster_%x {\n\tstyle=dotted;\n", node
);
169 ast_node_accept_children(node
->children
, visitor
);
174 graphprinter_visit_binary_expr (struct _Visitor
*visitor
, struct AstNode
*node
)
177 printf("\tnode_%x [label=\"%s\\n'%s'\",style=filled,",
178 node
, node
->name
, node
->children
->sibling
->name
);
179 printf("fillcolor="COLOR_FILL_COMMON
",color=%s];\n",
180 (node
->type
== ERROR
) ? COLOR_EDGE_ERROR
: COLOR_FILL_COMMON
);
181 ast_node_accept_children(node
->children
, visitor
);
185 graphprinter_visit_call (struct _Visitor
*visitor
, struct AstNode
*node
)
187 struct AstNode
*ident
= node
->children
;
188 struct AstNode
*plist
= ident
->sibling
;
191 printf("\tnode_%x [label=\"%s\\n[line: %d]\",style=filled,",
192 node
, node
->name
, node
->linenum
);
193 printf("fillcolor="COLOR_FILL_COMMON
",color=%s];\n",
194 (node
->type
== ERROR
) ? COLOR_EDGE_ERROR
: COLOR_FILL_COMMON
);
195 printf("\nsubgraph cluster_%x {\n\tstyle=dotted;\n", node
);
196 ast_node_accept(ident
, visitor
);
197 ast_node_accept(plist
, visitor
);
202 graphprinter_visit_callparam_list (struct _Visitor
*visitor
, struct AstNode
*node
)
207 printf("\tnode_%x [label=\"%s\\n<", node
, node
->name
);
209 for (i
= 0; i
< node
->symbol
->params
; i
++) {
210 printf("%s", type_get_lexeme(node
->symbol
->param_types
[i
]));
211 if (i
+ 1 < node
->symbol
->params
)
216 printf(">\",style=filled,fillcolor="COLOR_FILL_COMMON
",color=%s];\n",
217 (node
->type
== ERROR
) ? COLOR_EDGE_ERROR
: COLOR_EDGE_GROUP
);
219 ast_node_accept_children(node
->children
, visitor
);
223 graphprinter_visit_callparam (struct _Visitor
*visitor
, struct AstNode
*node
)
226 printf("\tnode_%x [label=\"%s\\n<%s>\",style=filled,",
227 node
, node
->name
, type_get_lexeme(node
->type
));
228 printf("fillcolor="COLOR_FILL_COMMON
",color=%s];\n",
229 (node
->type
== ERROR
) ? COLOR_EDGE_ERROR
: COLOR_FILL_COMMON
);
230 ast_node_accept_children(node
->children
, visitor
);
234 graphprinter_visit_identifier (struct _Visitor
*visitor
, struct AstNode
*node
)
238 printf("\tnode_%x [label=\"", node
);
240 if (node
->symbol
->decl_linenum
== 0)
241 printf("UNDECLARED\\n");
243 printf("%s\\n'%s'\\n<%s>\",style=filled,color=",
244 node
->name
, node
->symbol
->name
, type_get_lexeme(node
->type
));
246 if (node
->symbol
->decl_linenum
== 0)
247 printf(COLOR_EDGE_ERROR
);
248 else if (node
->symbol
->is_global
)
249 printf(COLOR_FILL_GLOBAL
);
251 printf(COLOR_FILL_LOCAL
);
253 printf(",fillcolor=");
255 if (node
->symbol
->is_global
)
256 printf(COLOR_FILL_GLOBAL
);
258 printf(COLOR_FILL_LOCAL
);
264 graphprinter_visit_literal (struct _Visitor
*visitor
, struct AstNode
*node
)
266 printf("\tnode_%x -> literal_%x;\n", node
->parent
, node
);
267 printf("\tliteral_%x [label=\"", node
);
268 value_print(stdout
, &node
->value
, node
->type
);
269 printf("\\n<%s>\",style=filled,color="COLOR_FILL_LITERAL
"];\n",
270 node
->name
, type_get_lexeme(node
->type
));
271 ast_node_accept_children(node
->children
, visitor
);
274 // Helper functions ----------------------------------------------------------
277 _print_arrow(struct AstNode
*node
)
279 printf("\tnode_%x -> node_%x [label=\"%d\",",
280 node
->parent
, node
, ast_node_get_child_counter(node
->parent
));
281 printf("fontsize=11,fontname=Courier];\n");
285 _print_symbol_table(struct AstNode
*node
)
287 if (node
->symbol
->next
== NULL
)
290 printf("\tnode_%x -> symbol_%x [lhead=cluster_symtab_%x,color=",
291 node
, node
->symbol
->next
, node
);
292 if (node
->parent
== NULL
)
297 printf("\n\tsubgraph cluster_symtab_%x {\n", node
);
299 if (node
->parent
== NULL
)
300 printf("\t\tcolor=black;\n");
302 printf("\t\tcolor=blue;\n");
304 printf("\t\tstyle=filled;\n\t\tfillcolor="COLOR_FILL_GLOBAL
";\n\t\tfontname=Courier;\n");
305 printf("\t\tnode [style=filled,color=white,fillcolor="COLOR_FILL_SYMBOL
"];\n");
307 _print_symbols(node
->symbol
->next
);
313 _print_symbols(Symbol
*symbol
)
318 if (symbol
->name
!= NULL
) {
319 printf("\t\tsymbol_%x [shape=record,label=\"{", symbol
);
320 printf("Symbol|Address: 0x%x\\l|lexeme: %s\\l|", symbol
, symbol
->name
);
321 printf("type: %s\\l}\"", type_get_lexeme(symbol
->type
));
322 printf(",style=filled,color=white,fillcolor="COLOR_FILL_SYMBOL
"];\n");
324 if (symbol
->next
!= NULL
)
325 printf("\tsymbol_%x -> symbol_%x;\n", symbol
, symbol
->next
);
329 _print_symbols(symbol
->next
);