6 //static void _ast_node_print_graph(struct AstNode *node);
7 //static int graph_code;
10 ast_node_new(const char* name
, int kind
, int type
,
11 int linenum
, Symbol
*symbol
)
16 node
= (struct AstNode
*) malloc (sizeof(struct AstNode
));
19 node
->name
= strdup(name
);
25 node
->linenum
= linenum
;
26 node
->symbol
= symbol
;
28 for (i
= 0; i
< AST_CHILDREN_NUM
; i
++)
29 node
->children
[i
] = NULL
;
36 ast_node_destroy(struct AstNode
*node
)
43 for (i
= 0; i
< AST_CHILDREN_NUM
; i
++)
44 ast_node_destroy(node
->children
[i
]);
45 ast_node_destroy(node
->next
);
51 ast_node_print(struct AstNode
*node
)
58 printf("(AstNode) NULL\n");
62 printf("(AstNode) %x : %s\n", node
, node
->name
);
63 printf("kind: %d\n", node
->kind
);
64 printf("type: %d\n", node
->type
);
66 value_print(&node
->value
, node
->type
);
67 printf("\nlinenum: %d\n", node
->linenum
);
68 printf("symbol: %x\n", node
->symbol
);
70 if (node
->next
!= NULL
)
71 printf("Sibling: %x\n", node
->next
);
74 for (i
= 0; i
< AST_CHILDREN_NUM
; i
++)
75 go
= go
|| (node
->children
[i
] != NULL
);
79 for (i
= 0; i
< AST_CHILDREN_NUM
; i
++) {
80 temp
= node
->children
[i
];
81 printf("\t(AstNode) %x", temp
);
83 printf(" : %s", temp
->name
);
91 for (i
= 0; i
< AST_CHILDREN_NUM
; i
++) {
92 if (node
->children
[i
] != NULL
)
93 ast_node_print(node
->children
[i
]);
95 if (node
->next
!= NULL
)
96 ast_node_print(node
->next
);
101 ast_node_print_graph(struct AstNode *node)
104 printf("/* toypasc AST graph. *\n");
105 printf("digraph {\n");
106 _ast_node_print_graph(node);
111 _ast_node_print_graph(struct AstNode *node)
121 printf("\t%s_%d [label=%s]\n", node->name, code, node->name);
122 for (i = 0; i < AST_CHILDREN_NUM; i++) {
123 if (node->children[i] != NULL)
124 printf("\t%s_%d -> %s_%d\n", node->name, code,
125 node->children[i]->name,
127 _ast_node_print_graph(node->children[i]);
130 if (node->next != NULL)
131 printf("\t%s_%d -> %s_%d\n", node->next->name, code,
132 node->children[i]->name,
134 _ast_node_print_graph(node->next);