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
*gp_visitor
= (Visitor
*) malloc (sizeof(Visitor
));
15 gp_visitor
->visit_program
= &graphprinter_visit_program
;
16 gp_visitor
->visit_programdecl
= &graphprinter_visit_programdecl
;
17 gp_visitor
->visit_vardecl_list
= &graphprinter_visit_vardecl_list
;
18 gp_visitor
->visit_vardecl
= &graphprinter_visit_simplenode
;
19 gp_visitor
->visit_identifier_list
= &graphprinter_visit_identifier_list
;
20 gp_visitor
->visit_procfunc_list
= &graphprinter_visit_procfunc_list
;
21 gp_visitor
->visit_procedure
= &graphprinter_visit_procfunc
;
22 gp_visitor
->visit_function
= &graphprinter_visit_procfunc
;
23 gp_visitor
->visit_param_list
= &graphprinter_visit_param_list
;
24 gp_visitor
->visit_parameter
= &graphprinter_visit_simplenode
;
25 gp_visitor
->visit_statement_list
= &graphprinter_visit_statement_list
;
26 gp_visitor
->visit_print_stmt
= &graphprinter_visit_simplenode
;
27 gp_visitor
->visit_assignment_stmt
= &graphprinter_visit_simplenode
;
28 gp_visitor
->visit_if_stmt
= &graphprinter_visit_simplenode
;
29 gp_visitor
->visit_while_stmt
= &graphprinter_visit_simplenode
;
30 gp_visitor
->visit_for_stmt
= &graphprinter_visit_simplenode
;
31 gp_visitor
->visit_rel_expr
= &graphprinter_visit_binary_expr
;
32 gp_visitor
->visit_add_expr
= &graphprinter_visit_binary_expr
;
33 gp_visitor
->visit_mul_expr
= &graphprinter_visit_binary_expr
;
34 gp_visitor
->visit_notfactor
= &graphprinter_visit_simplenode
;
35 gp_visitor
->visit_call
= &graphprinter_visit_simplenode
;
36 gp_visitor
->visit_callparam_list
= &graphprinter_visit_callparam_list
;
37 gp_visitor
->visit_identifier
= &graphprinter_visit_identifier
;
38 gp_visitor
->visit_literal
= &graphprinter_visit_literal
;
39 gp_visitor
->close_group
= &graphprinter_close_group
;
43 graphprinter_visit_program(struct AstNode
*node
)
47 printf("/* toypasc AST graph. */\n");
48 printf("digraph {\n");
50 printf("\tremincross=true;\n");
51 printf("\tordering=out;\n");
52 printf("\tcompound=true;\n");
53 printf("\tranksep=1.0;\n");
54 printf("\tnode [fontsize=11,fontname=Courier];\n");
55 printf("\tedge [color=\"#22DDAA\"];\n\n");
57 printf("\tnode_%x [label=\"%s\",style=", node
, node
->name
);
58 printf("filled,color=orange,fillcolor=\"#FFEEEE\"];\n");
60 _print_symbol_table(node
);
64 graphprinter_visit_simplenode (struct AstNode
*node
)
66 printf("\tnode_%x -> node_%x;\n", node
->parent
, node
);
67 printf("\tnode_%x [label=\"%s\",style=", node
, node
->name
);
68 printf("filled,fillcolor=\"#EEFFEE\",color=\"#%s\"];\n",
69 (node
->type
== ERROR
) ? "FF0000" : "EEFFEE");
73 graphprinter_visit_programdecl(struct AstNode
*node
)
76 printf("\tnode_%x -> node_%x;\n", node
->parent
, node
);
77 printf("\tnode_%x [label=\"%s\",style=", node
, node
->name
);
78 printf("filled,color=\"#22DDAA\",fillcolor=\"#EEFFEE\"];\n");
82 graphprinter_visit_vardecl_list (struct AstNode
*node
)
85 printf("\tnode_%x -> node_%x;\n", node
->parent
, node
);
86 printf("\tnode_%x [label=\"%s\",style=", node
, node
->name
);
87 printf("filled,color=\"#22DDAA\",fillcolor=\"#EEFFEE\"];\n");
88 printf("subgraph cluster_%x {\n\tstyle=dotted;\n", node
);
92 graphprinter_visit_identifier_list (struct AstNode
*node
)
94 printf("\tnode_%x -> node_%x;\n", node
->parent
, node
);
95 printf("\tnode_%x [label=\"%s\",style=", node
, node
->name
);
96 printf("filled,color=\"#22DDAA\",fillcolor=\"#EEFFEE\"];\n");
97 printf("subgraph cluster_%x {\n\tstyle=dotted;\n", node
);
101 graphprinter_visit_procfunc_list (struct AstNode
*node
)
103 printf("\tnode_%x -> node_%x;\n", node
->parent
, node
);
104 printf("\tnode_%x [label=\"%s\",style=", node
, node
->name
);
105 printf("filled,color=\"#22DDAA\",fillcolor=\"#EEFFEE\"];\n");
109 graphprinter_visit_procfunc (struct AstNode
*node
)
111 is_symboldecl
= TRUE
;
112 printf("\tnode_%x -> node_%x;\n", node
->parent
, node
);
113 printf("\tnode_%x [label=\"%s\\n'%s'\",style=",
114 node
, node
->name
, node
->children
->symbol
->name
);
115 printf("filled,color=blue,fillcolor=\"#EEEEFF\"];\n");
116 printf("subgraph cluster_%x {\n\tstyle=dotted;\n", node
);
117 _print_symbol_table(node
);
121 graphprinter_visit_param_list (struct AstNode
*node
)
123 is_symboldecl
= TRUE
;
124 printf("\tnode_%x -> node_%x;\n", node
->parent
, node
);
125 printf("\tnode_%x [label=\"%s\",style=", node
, node
->name
);
126 printf("filled,color=\"#22DDAA\",fillcolor=\"#EEFFEE\"];\n");
127 printf("subgraph cluster_%x {\n\tstyle=dotted;\n", node
);
131 graphprinter_visit_statement_list (struct AstNode
*node
)
133 is_symboldecl
= FALSE
;
134 printf("\tnode_%x -> node_%x;\n", node
->parent
, node
);
135 printf("\tnode_%x [label=\"%s\",style=", node
, node
->name
);
136 printf("filled,color=\"#22DDAA\",fillcolor=\"#EEFFEE\"];\n");
137 printf("subgraph cluster_%x {\n\tstyle=dotted;\n", node
);
141 graphprinter_visit_binary_expr (struct AstNode
*node
)
143 printf("\tnode_%x -> node_%x;\n", node
->parent
, node
);
144 printf("\tnode_%x [label=\"%s\\n(%s)\",style=",
145 node
, node
->name
, node
->children
->sibling
->name
);
146 printf("filled,fillcolor=\"#EEFFEE\",color=\"#%s\"];\n",
147 (node
->type
== ERROR
) ? "FF0000" : "EEFFEE");
151 graphprinter_visit_callparam_list (struct AstNode
*node
)
153 printf("\tnode_%x -> node_%x;\n", node
->parent
, node
);
154 printf("\tnode_%x [label=\"%s\",style=", node
, node
->name
);
155 printf("filled,color=\"#22DDAA\",fillcolor=\"#EEFFEE\"];\n");
156 printf("subgraph cluster_%x {\n\tstyle=dotted;\n", node
);
160 graphprinter_visit_identifier (struct AstNode
*node
)
162 printf("\tnode_%x -> node_%x;\n", node
->parent
, node
);
163 printf("\tnode_%x [label=\"%s\",style=", node
, node
->name
);
164 printf("filled,color=\"#EEFFEE\"];\n");
166 if (node
->symbol
->decl_linenum
== 0) {
167 printf("\tsymbol_%x [label=\"'%s'\\nundeclared\",",
168 node
->symbol
, node
->symbol
->name
);
169 printf("color=red,fillcolor=\"#FFEEEE\",style=filled];\n");
172 printf("\tnode_%x -> symbol_%x", node
, node
->symbol
);
175 printf(" [color=\"#00FF00\"]");
181 graphprinter_visit_literal (struct AstNode
*node
)
183 printf("\tnode_%x -> literal_%x;\n", node
->parent
, node
);
184 printf("\tliteral_%x [label=\"", node
);
185 value_print(&node
->value
, node
->type
);
186 printf("\",style=filled,color=\"#FFFFCC\"];\n");
190 graphprinter_close_group ()
196 _print_graph(struct AstNode
*self
)
202 struct AstNode
*temp
;
207 is_program
= self
->kind
== PROGRAM
;
208 is_funcproc
= self
->kind
== PROCEDURE
|| self
->kind
== FUNCTION
;
209 is_cluster
= is_program
|| is_funcproc
||
210 self
->kind
== PROGDECL
||
211 strstr(self
->name
, "List");
213 printf("\tnode_%x [label=\"%s\",style=", self
, self
->name
);
217 printf("filled,color=orange,fillcolor=\"#FFEEEE\"];\n");
218 else if (is_funcproc
)
219 printf("filled,color=blue,fillcolor=\"#EEEEFF\"];\n");
221 printf("filled,color=\"#22DDAA\",fillcolor=\"#EEFFEE\"];\n");
222 printf("subgraph cluster_%x {\n\tstyle=dotted;\n", self
, self
->name
);
224 printf("filled,color=\"#EEFFEE\"];\n");
227 printf("\tsubgraph cluster_symtab_0x%x {\tstyle=filled;\n", self
->symbol
);
228 printf("\tcolor=\"#EFEFEF\";\n\tfontname=Courier;\n");
229 printf("\tnode [style=filled,color=white,fillcolor=\"#CCFF99\"];\n");
230 //_ast_node_print_graph_symbol_table(self->symbol);
234 if (self
->children
!= NULL
) {
235 temp
= self
->children
;
236 while (temp
!= NULL
) {
237 printf("\tnode_%x -> node_%x;\n", self
, temp
);
238 temp
= temp
->sibling
;
241 if (self
->symbol
!= NULL
) {
242 printf("\tnode_%x -> symbol_%x [color=lightgray,headport=n];\n",
244 } else if (strstr(self
->name
, "Literal")) {
245 printf("\tliteral_%x [label=\"", self
);
246 value_print(&self
->value
, self
->type
);
247 printf("\",style=filled,color=\"#FFFFCC\"];\n");
248 printf("\tnode_%x -> literal_%x;\n", self
, self
);
253 _print_graph(self
->children
);
258 _print_graph(self
->sibling
);
262 _print_symbol_table(struct AstNode
*node
)
264 if (node
->symbol
->next
== NULL
)
267 printf("\tnode_%x -> symbol_%x [lhead=cluster_symtab_%x,color=",
268 node
, node
->symbol
->next
, node
);
269 if (node
->parent
== NULL
)
270 printf("orange];\n");
274 printf("\tsubgraph cluster_symtab_%x {\n\tstyle=filled;\n", node
);
276 if (node
->parent
== NULL
)
277 printf("\tcolor=orange;\n");
279 printf("\tcolor=blue;\n");
281 printf("\tstyle=filled;\n\tfillcolor=\"#EFEFEF\";\n\tfontname=Courier;\n");
282 printf("\tnode [style=filled,color=white,fillcolor=\"#CCFF99\"];\n");
284 _print_symbols(node
->symbol
->next
);
290 _print_symbols(Symbol
*symbol
)
295 if (symbol
->name
!= NULL
) {
296 printf("\t\tsymbol_%x [shape=record,label=\"{", symbol
);
297 printf("Symbol|Address: 0x%x\\l|lexeme: %s\\l|", symbol
, symbol
->name
);
298 printf("type: %s\\l}\"", type_get_lexeme(symbol
->type
));
299 printf(",style=filled,color=white,fillcolor=\"#CCFF99\"];\n");
302 _print_symbols(symbol
->next
);