5 #include "typecheck_visitor.h"
8 ast_node_new(const char* name
, int kind
, int type
,
9 int linenum
, Symbol
*symbol
)
14 node
= (struct AstNode
*) malloc (sizeof(struct AstNode
));
17 node
->name
= strdup(name
);
23 node
->linenum
= linenum
;
24 node
->symbol
= symbol
;
25 node
->visited
= FALSE
;
27 node
->children
= NULL
;
34 ast_node_destroy(struct AstNode
*self
)
37 ast_node_destroy(self
->children
);
38 ast_node_destroy(self
->sibling
);
44 ast_node_unset_visited(struct AstNode
*self
)
49 ast_node_unset_visited(self
->children
);
50 ast_node_unset_visited(self
->sibling
);
51 self
->visited
= FALSE
;
55 ast_node_add_child(struct AstNode
*self
, struct AstNode
*child
)
62 if (self
->children
== NULL
) {
64 self
->children
= child
;
66 ast_node_add_sibling(self
->children
, child
);
68 for (temp
= child
; temp
!= NULL
; temp
= temp
->sibling
)
73 ast_node_add_sibling(struct AstNode
*self
, struct AstNode
*sibling
)
80 if (self
->sibling
== NULL
) {
81 self
->sibling
= sibling
;
83 for (temp
= self
->sibling
; temp
->sibling
!= NULL
; temp
= temp
->sibling
)
85 temp
->sibling
= sibling
;
90 ast_node_accept(struct AstNode
*self
, Visitor
*visitor
)
93 bool opened_group
= FALSE
;
97 ast_node_unset_visited(self
);
98 visitor
->visit_program(self
);
102 visitor
->visit_programdecl(self
);
105 visitor
->visit_vardecl_list(self
);
109 visitor
->visit_vardecl(self
);
112 visitor
->visit_identifier_list(self
);
116 visitor
->visit_procfunc_list(self
);
119 visitor
->visit_procedure(self
);
123 visitor
->visit_function(self
);
127 visitor
->visit_param_list(self
);
131 visitor
->visit_parameter(self
);
134 visitor
->visit_statement_list(self
);
141 visitor
->visit_print_stmt(self
);
143 case ASSIGNMENT_STMT
:
144 visitor
->visit_assignment_stmt(self
);
147 visitor
->visit_if_stmt(self
);
150 visitor
->visit_while_stmt(self
);
153 visitor
->visit_for_stmt(self
);
156 visitor
->visit_rel_expr(self
);
159 visitor
->visit_add_expr(self
);
162 visitor
->visit_mul_expr(self
);
165 visitor
->visit_notfactor(self
);
168 visitor
->visit_call(self
);
171 visitor
->visit_callparam_list(self
);
175 visitor
->visit_identifier(self
);
180 visitor
->visit_literal(self
);
184 for (temp
= self
->children
; temp
!= NULL
; temp
= temp
->sibling
)
185 ast_node_accept(temp
, visitor
);
188 if (visitor
->close_group
!= NULL
)
189 visitor
->close_group();