4 #include "graphprinter_visitor.h"
6 static bool is_symboldecl
= FALSE
;
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_simplenode
;
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_simplenode
;
39 visitor
->visit_callparam_list
= &graphprinter_visit_callparam_list
;
40 visitor
->visit_identifier
= &graphprinter_visit_identifier
;
41 visitor
->visit_literal
= &graphprinter_visit_literal
;
42 visitor
->visit_add_op
= NULL
;
43 visitor
->visit_mul_op
= NULL
;
44 visitor
->visit_rel_op
= NULL
;
45 visitor
->visit_not_op
= NULL
;
51 graphprinter_visit_program(struct _Visitor
*visitor
, struct AstNode
*node
)
53 printf("/* toypasc AST graph. */\n");
54 printf("digraph {\n");
56 printf("\tremincross=true;\n");
57 printf("\tordering=out;\n");
58 printf("\tcompound=true;\n");
59 printf("\tranksep=1.0;\n");
60 printf("\tnode [fontsize=11,fontname=Courier];\n");
61 printf("\tedge [color=\"#22DDAA\"];\n\n");
63 printf("\tnode_%x [label=\"%s\",style=", node
, node
->name
);
64 printf("filled,color=orange,fillcolor=\"#FFEEEE\"];\n");
66 _print_symbol_table(node
);
68 ast_node_accept_children(node
->children
, visitor
);
73 graphprinter_visit_simplenode (struct _Visitor
*visitor
, struct AstNode
*node
)
75 printf("\tnode_%x -> node_%x;\n", node
->parent
, node
);
76 printf("\tnode_%x [label=\"%s\",style=", node
, node
->name
);
77 printf("filled,fillcolor=\"#EEFFEE\",color=\"#%s\"];\n",
78 (node
->type
== ERROR
) ? "FF0000" : "EEFFEE");
79 ast_node_accept_children(node
->children
, visitor
);
83 graphprinter_visit_programdecl(struct _Visitor
*visitor
, struct AstNode
*node
)
86 printf("\tnode_%x -> node_%x;\n", node
->parent
, node
);
87 printf("\tnode_%x [label=\"%s\",style=", node
, node
->name
);
88 printf("filled,color=\"#22DDAA\",fillcolor=\"#EEFFEE\"];\n");
89 ast_node_accept_children(node
->children
, visitor
);
93 graphprinter_visit_vardecl_list (struct _Visitor
*visitor
, struct AstNode
*node
)
96 printf("\tnode_%x -> node_%x;\n", node
->parent
, node
);
97 printf("\tnode_%x [label=\"%s\",style=", node
, node
->name
);
98 printf("filled,color=\"#22DDAA\",fillcolor=\"#EEFFEE\"];\n");
99 printf("\nsubgraph cluster_%x {\n\tstyle=dotted;\n", node
);
101 ast_node_accept_children(node
->children
, visitor
);
106 graphprinter_visit_identifier_list (struct _Visitor
*visitor
, struct AstNode
*node
)
108 printf("\tnode_%x -> node_%x;\n", node
->parent
, node
);
109 printf("\tnode_%x [label=\"%s\",style=", node
, node
->name
);
110 printf("filled,color=\"#22DDAA\",fillcolor=\"#EEFFEE\"];\n");
111 printf("\nsubgraph cluster_%x {\n\tstyle=dotted;\n", node
);
113 ast_node_accept_children(node
->children
, visitor
);
118 graphprinter_visit_procfunc_list (struct _Visitor
*visitor
, struct AstNode
*node
)
120 printf("\tnode_%x -> node_%x;\n", node
->parent
, node
);
121 printf("\tnode_%x [label=\"%s\",style=", node
, node
->name
);
122 printf("filled,color=\"#22DDAA\",fillcolor=\"#EEFFEE\"];\n");
123 ast_node_accept_children(node
->children
, visitor
);
127 graphprinter_visit_procfunc (struct _Visitor
*visitor
, struct AstNode
*node
)
129 is_symboldecl
= TRUE
;
130 printf("\tnode_%x -> node_%x;\n", node
->parent
, node
);
131 printf("\tnode_%x [label=\"%s\\n'%s'\",style=",
132 node
, node
->name
, node
->children
->symbol
->name
);
133 printf("filled,color=blue,fillcolor=\"#EEEEFF\"];\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
)
144 is_symboldecl
= TRUE
;
145 printf("\tnode_%x -> node_%x;\n", node
->parent
, node
);
146 printf("\tnode_%x [label=\"%s\",style=", node
, node
->name
);
147 printf("filled,color=\"#22DDAA\",fillcolor=\"#EEFFEE\"];\n");
148 printf("\nsubgraph cluster_%x {\n\tstyle=dotted;\n", node
);
150 ast_node_accept_children(node
->children
, visitor
);
155 graphprinter_visit_statement_list (struct _Visitor
*visitor
, struct AstNode
*node
)
157 is_symboldecl
= FALSE
;
158 printf("\tnode_%x -> node_%x;\n", node
->parent
, node
);
159 printf("\tnode_%x [label=\"%s\",style=", node
, node
->name
);
160 printf("filled,color=\"#22DDAA\",fillcolor=\"#EEFFEE\"];\n");
161 printf("\nsubgraph cluster_%x {\n\tstyle=dotted;\n", node
);
163 if (node
->parent
->kind
== PROGRAM
)
164 printf("\tcolor=\"red\";\n");
166 ast_node_accept_children(node
->children
, visitor
);
172 graphprinter_visit_binary_expr (struct _Visitor
*visitor
, struct AstNode
*node
)
174 printf("\tnode_%x -> node_%x;\n", node
->parent
, node
);
175 printf("\tnode_%x [label=\"%s\\n(%s)\",style=",
176 node
, node
->name
, node
->children
->sibling
->name
);
177 printf("filled,fillcolor=\"#EEFFEE\",color=\"#%s\"];\n",
178 (node
->type
== ERROR
) ? "FF0000" : "EEFFEE");
179 ast_node_accept_children(node
->children
, visitor
);
183 graphprinter_visit_callparam_list (struct _Visitor
*visitor
, struct AstNode
*node
)
185 printf("\tnode_%x -> node_%x;\n", node
->parent
, node
);
186 printf("\tnode_%x [label=\"%s\",style=", node
, node
->name
);
187 printf("filled,color=\"#22DDAA\",fillcolor=\"#EEFFEE\"];\n");
188 printf("\nsubgraph cluster_%x {\n\tstyle=dotted;\n", node
);
190 ast_node_accept_children(node
->children
, visitor
);
195 graphprinter_visit_identifier (struct _Visitor
*visitor
, struct AstNode
*node
)
197 printf("\tnode_%x -> node_%x;\n", node
->parent
, node
);
198 printf("\tnode_%x [label=\"%s\",style=", node
, node
->name
);
199 printf("filled,color=\"#EEFFEE\"];\n");
201 if (node
->symbol
->decl_linenum
== 0) {
202 printf("\tsymbol_%x [label=\"'%s'\\nundeclared\",",
203 node
->symbol
, node
->symbol
->name
);
204 printf("color=red,fillcolor=\"#FFEEEE\",style=filled];\n");
207 printf("\tnode_%x -> symbol_%x", node
, node
->symbol
);
210 printf(" [color=\"#00FF00\"]");
213 ast_node_accept_children(node
->children
, visitor
);
217 graphprinter_visit_literal (struct _Visitor
*visitor
, struct AstNode
*node
)
219 printf("\tnode_%x -> literal_%x;\n", node
->parent
, node
);
220 printf("\tliteral_%x [label=\"", node
);
221 value_print(&node
->value
, node
->type
);
222 printf("\",style=filled,color=\"#FFFFCC\"];\n");
223 ast_node_accept_children(node
->children
, visitor
);
227 _print_graph(struct AstNode
*self
)
233 struct AstNode
*temp
;
238 is_program
= self
->kind
== PROGRAM
;
239 is_funcproc
= self
->kind
== PROCEDURE
|| self
->kind
== FUNCTION
;
240 is_cluster
= is_program
|| is_funcproc
||
241 self
->kind
== PROGRAM_DECL
||
242 strstr(self
->name
, "List");
244 printf("\tnode_%x [label=\"%s\",style=", self
, self
->name
);
248 printf("filled,color=orange,fillcolor=\"#FFEEEE\"];\n");
249 else if (is_funcproc
)
250 printf("filled,color=blue,fillcolor=\"#EEEEFF\"];\n");
252 printf("filled,color=\"#22DDAA\",fillcolor=\"#EEFFEE\"];\n");
253 printf("\nsubgraph cluster_%x {\n\tstyle=dotted;\n", self
, self
->name
);
255 printf("filled,color=\"#EEFFEE\"];\n");
258 printf("\n\tsubgraph cluster_symtab_0x%x {\t\tstyle=filled;\n", self
->symbol
);
259 printf("\t\tcolor=\"#EFEFEF\";\n\t\tfontname=Courier;\n");
260 printf("\t\tnode [style=filled,color=white,fillcolor=\"#CCFF99\"];\n");
261 //_ast_node_print_graph_symbol_table(self->symbol);
265 if (self
->children
!= NULL
) {
266 temp
= self
->children
;
267 while (temp
!= NULL
) {
268 printf("\tnode_%x -> node_%x;\n", self
, temp
);
269 temp
= temp
->sibling
;
272 if (self
->symbol
!= NULL
) {
273 printf("\tnode_%x -> symbol_%x [color=lightgray,headport=n];\n",
275 } else if (strstr(self
->name
, "Literal")) {
276 printf("\tliteral_%x [label=\"", self
);
277 value_print(&self
->value
, self
->type
);
278 printf("\",style=filled,color=\"#FFFFCC\"];\n");
279 printf("\tnode_%x -> literal_%x;\n", self
, self
);
284 _print_graph(self
->children
);
289 _print_graph(self
->sibling
);
293 _print_symbol_table(struct AstNode
*node
)
295 if (node
->symbol
->next
== NULL
)
298 printf("\tnode_%x -> symbol_%x [lhead=cluster_symtab_%x,color=",
299 node
, node
->symbol
->next
, node
);
300 if (node
->parent
== NULL
)
301 printf("orange];\n");
305 printf("\n\tsubgraph cluster_symtab_%x {\n\t\tstyle=filled;\n", node
);
307 if (node
->parent
== NULL
)
308 printf("\t\tcolor=orange;\n");
310 printf("\t\tcolor=blue;\n");
312 printf("\t\tstyle=filled;\n\t\tfillcolor=\"#EFEFEF\";\n\t\tfontname=Courier;\n");
313 printf("\t\tnode [style=filled,color=white,fillcolor=\"#CCFF99\"];\n");
315 _print_symbols(node
->symbol
->next
);
321 _print_symbols(Symbol
*symbol
)
326 if (symbol
->name
!= NULL
) {
327 printf("\t\tsymbol_%x [shape=record,label=\"{", symbol
);
328 printf("Symbol|Address: 0x%x\\l|lexeme: %s\\l|", symbol
, symbol
->name
);
329 printf("type: %s\\l}\"", type_get_lexeme(symbol
->type
));
330 printf(",style=filled,color=white,fillcolor=\"#CCFF99\"];\n");
333 _print_symbols(symbol
->next
);