Adicionando codegen_visitor.h e c
[toypasc.git] / graphprinter_visitor.c
blob6a8a21a455168cbaa76a20919e6ee0fd18708c9a
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
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);
10 Visitor *
11 graphprinter_new()
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;
42 void
43 graphprinter_visit_program(struct AstNode *node)
45 struct AstNode *temp;
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);
63 void
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");
72 void
73 graphprinter_visit_programdecl(struct AstNode *node)
75 is_symboldecl = TRUE;
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");
81 void
82 graphprinter_visit_vardecl_list (struct AstNode *node)
84 is_symboldecl = TRUE;
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);
91 void
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);
100 void
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");
108 void
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);
120 void
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);
130 void
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);
140 void
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");
150 void
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);
159 void
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);
174 if (is_symboldecl)
175 printf(" [color=\"#00FF00\"]");
177 printf(";\n");
180 void
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");
189 void
190 graphprinter_close_group ()
192 printf("}\n");
195 static void
196 _print_graph(struct AstNode *self)
198 int i;
199 bool is_program;
200 bool is_cluster;
201 bool is_funcproc;
202 struct AstNode *temp;
204 if (self == NULL)
205 return;
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);
215 if (is_cluster) {
216 if (is_program)
217 printf("filled,color=orange,fillcolor=\"#FFEEEE\"];\n");
218 else if (is_funcproc)
219 printf("filled,color=blue,fillcolor=\"#EEEEFF\"];\n");
220 else
221 printf("filled,color=\"#22DDAA\",fillcolor=\"#EEFFEE\"];\n");
222 printf("subgraph cluster_%x {\n\tstyle=dotted;\n", self, self->name);
223 } else
224 printf("filled,color=\"#EEFFEE\"];\n");
226 if (is_funcproc) {
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);
231 printf("\t}\n");
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;
240 } else {
241 if (self->symbol != NULL) {
242 printf("\tnode_%x -> symbol_%x [color=lightgray,headport=n];\n",
243 self, self->symbol);
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);
255 if (is_cluster)
256 printf("}\n");
258 _print_graph(self->sibling);
261 static void
262 _print_symbol_table(struct AstNode *node)
264 if (node->symbol->next == NULL)
265 return;
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");
271 else
272 printf("blue];\n");
274 printf("\tsubgraph cluster_symtab_%x {\n\tstyle=filled;\n", node);
276 if (node->parent == NULL)
277 printf("\tcolor=orange;\n");
278 else
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);
286 printf("\t}\n");
289 static void
290 _print_symbols(Symbol *symbol)
292 if (symbol == NULL)
293 return;
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);