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
)
94 if (self
== NULL
|| self
->visited
)
101 ast_node_unset_visited(self
);
102 visit
= visitor
->visit_program
;
105 visit
= visitor
->visit_programdecl
;
108 visit
= visitor
->visit_vardecl_list
;
111 visit
= visitor
->visit_vardecl
;
114 visit
= visitor
->visit_identifier_list
;
117 visit
= visitor
->visit_procfunc_list
;
120 visit
= visitor
->visit_procedure
;
123 visit
= visitor
->visit_function
;
126 visit
= visitor
->visit_param_list
;
129 visit
= visitor
->visit_parameter
;
132 visit
= visitor
->visit_statement_list
;
135 visit
= visitor
->visit_printint_stmt
;
138 visit
= visitor
->visit_printchar_stmt
;
141 visit
= visitor
->visit_printbool_stmt
;
144 visit
= visitor
->visit_printline_stmt
;
146 case ASSIGNMENT_STMT
:
147 visit
= visitor
->visit_assignment_stmt
;
150 visit
= visitor
->visit_if_stmt
;
153 visit
= visitor
->visit_while_stmt
;
156 visit
= visitor
->visit_for_stmt
;
159 visit
= visitor
->visit_rel_expr
;
162 visit
= visitor
->visit_add_expr
;
165 visit
= visitor
->visit_mul_expr
;
168 visit
= visitor
->visit_notfactor
;
171 visit
= visitor
->visit_call
;
174 visit
= visitor
->visit_callparam_list
;
177 visit
= visitor
->visit_identifier
;
182 visit
= visitor
->visit_literal
;
187 visit
= visitor
->visit_add_op
;
192 visit
= visitor
->visit_mul_op
;
200 visit
= visitor
->visit_rel_op
;
203 visit
= visitor
->visit_not_op
;
210 visit(visitor
, self
);
214 ast_node_accept_children(struct AstNode
*self
, Visitor
*visitor
)
216 struct AstNode
*temp
;
217 for (temp
= self
; temp
!= NULL
; temp
= temp
->sibling
)
218 ast_node_accept(temp
, visitor
);