6 memset(node, 0,
sizeof(
ASTNode));
8 node->data_type = TYPE_UNKNOWN;
13ASTNode *ast_create_program(
void) {
14 ASTNode *node = ast_create_node(AST_PROGRAM);
16 node->data.program.declarations = NULL;
17 node->data.program.declaration_count = 0;
24 ASTNode *node = ast_create_node(AST_FUNCTION_DECLARATION);
26 node->data.function_decl.return_type = return_type;
27 node->data.function_decl.name = safe_strdup(name);
28 node->data.function_decl.parameters = params;
29 node->data.function_decl.parameter_count = 0;
30 node->data.function_decl.body = body;
37 ASTNode *node = ast_create_node(AST_VARIABLE_DECLARATION);
39 node->data.var_decl.var_type = type;
40 node->data.var_decl.name = safe_strdup(name);
41 node->data.var_decl.initializer = initializer;
48 ASTNode *node = ast_create_node(AST_PARAMETER);
50 node->data.parameter.param_type = type;
51 node->data.parameter.name = safe_strdup(name);
57ASTNode *ast_create_compound_stmt(
void) {
58 ASTNode *node = ast_create_node(AST_COMPOUND_STATEMENT);
60 node->data.compound_stmt.statements = NULL;
61 node->data.compound_stmt.statement_count = 0;
68 ASTNode *node = ast_create_node(AST_EXPRESSION_STATEMENT);
70 node->data.expression_stmt.expression = expr;
77 ASTNode *node = ast_create_node(AST_RETURN_STATEMENT);
79 node->data.return_stmt.expression = expr;
86 ASTNode *node = ast_create_node(AST_IF_STATEMENT);
88 node->data.if_stmt.condition = condition;
89 node->data.if_stmt.then_stmt = then_stmt;
90 node->data.if_stmt.else_stmt = else_stmt;
97 ASTNode *node = ast_create_node(AST_WHILE_STATEMENT);
99 node->data.while_stmt.condition = condition;
100 node->data.while_stmt.body = body;
107 ASTNode *node = ast_create_node(AST_FOR_STATEMENT);
109 node->data.for_stmt.init = init;
110 node->data.for_stmt.condition = condition;
111 node->data.for_stmt.update = update;
112 node->data.for_stmt.body = body;
118ASTNode *ast_create_break_stmt(
void) {
119 return ast_create_node(AST_BREAK_STATEMENT);
123ASTNode *ast_create_continue_stmt(
void) {
124 return ast_create_node(AST_CONTINUE_STATEMENT);
129 ASTNode *node = ast_create_node(AST_BINARY_OP);
131 node->data.binary_expr.operator = op;
132 node->data.binary_expr.left = left;
133 node->data.binary_expr.right = right;
140 ASTNode *node = ast_create_node(AST_UNARY_OP);
142 node->data.unary_expr.operator = op;
143 node->data.unary_expr.operand = operand;
149ASTNode *ast_create_call_expr(
const char *function_name) {
150 ASTNode *node = ast_create_node(AST_FUNCTION_CALL);
151 if (node && function_name) {
152 node->data.call_expr.function_name = safe_strdup(function_name);
153 node->data.call_expr.arguments = NULL;
154 node->data.call_expr.argument_count = 0;
160ASTNode *ast_create_identifier(
const char *name) {
161 ASTNode *node = ast_create_node(AST_IDENTIFIER);
163 node->data.identifier.name = safe_strdup(name);
169ASTNode *ast_create_number(
int value) {
170 ASTNode *node = ast_create_node(AST_NUMBER_LITERAL);
172 node->data.number.value = value;
178ASTNode *ast_create_string(
const char *value) {
179 ASTNode *node = ast_create_node(AST_STRING_LITERAL);
181 node->data.string.value = safe_strdup(value);
187ASTNode *ast_create_assignment(
const char *var_name,
ASTNode *value) {
188 ASTNode *node = ast_create_node(AST_ASSIGNMENT);
189 if (node && var_name) {
190 node->data.assignment.variable = safe_strdup(var_name);
191 node->data.assignment.value = value;
198 if (!program || !declaration)
return;
200 int new_count = program->data.program.declaration_count + 1;
201 ASTNode **new_declarations = realloc(program->data.program.declarations,
203 if (new_declarations) {
204 program->data.program.declarations = new_declarations;
205 program->data.program.declarations[program->data.program.declaration_count] = declaration;
206 program->data.program.declaration_count = new_count;
212 if (!compound || compound->type != AST_COMPOUND_STATEMENT || !statement)
return;
214 int new_count = compound->data.compound_stmt.statement_count + 1;
215 ASTNode **new_statements = realloc(compound->data.compound_stmt.statements,
217 if (new_statements) {
218 compound->data.compound_stmt.statements = new_statements;
219 compound->data.compound_stmt.statements[compound->data.compound_stmt.statement_count] = statement;
220 compound->data.compound_stmt.statement_count = new_count;
226 if (!call || call->type != AST_FUNCTION_CALL || !arg)
return;
228 int new_count = call->data.call_expr.argument_count + 1;
229 ASTNode **new_arguments = realloc(call->data.call_expr.arguments,
232 call->data.call_expr.arguments = new_arguments;
233 call->data.call_expr.arguments[call->data.call_expr.argument_count] = arg;
234 call->data.call_expr.argument_count = new_count;
239void ast_destroy(
ASTNode *node) {
245 #ifdef DEBUG_AST_DESTROY
246 printf(
"DEBUG: Destroying node with type %d\n", node->type);
250 switch (node->type) {
252 if (node->data.program.declarations) {
253 for (
int i = 0; i < node->data.program.declaration_count; i++) {
254 ast_destroy(node->data.program.declarations[i]);
256 free(node->data.program.declarations);
257 node->data.program.declarations = NULL;
261 case AST_FUNCTION_DECLARATION:
263 if (node->data.function_decl.name) {
264 free(node->data.function_decl.name);
265 node->data.function_decl.name = NULL;
268 if (node->data.function_decl.parameters) {
269 for (
int i = 0; i < node->data.function_decl.parameter_count; i++) {
270 ast_destroy(node->data.function_decl.parameters[i]);
272 free(node->data.function_decl.parameters);
273 node->data.function_decl.parameters = NULL;
276 if (node->data.function_decl.body) {
277 ast_destroy(node->data.function_decl.body);
278 node->data.function_decl.body = NULL;
282 case AST_VARIABLE_DECLARATION:
283 if (node->data.var_decl.name) {
284 free(node->data.var_decl.name);
285 node->data.var_decl.name = NULL;
287 if (node->data.var_decl.initializer) {
288 ast_destroy(node->data.var_decl.initializer);
289 node->data.var_decl.initializer = NULL;
294 if (node->data.parameter.name) {
295 free(node->data.parameter.name);
296 node->data.parameter.name = NULL;
300 case AST_COMPOUND_STATEMENT:
301 if (node->data.compound_stmt.statements) {
302 for (
int i = 0; i < node->data.compound_stmt.statement_count; i++) {
303 ast_destroy(node->data.compound_stmt.statements[i]);
305 free(node->data.compound_stmt.statements);
306 node->data.compound_stmt.statements = NULL;
310 case AST_EXPRESSION_STATEMENT:
311 if (node->data.expression_stmt.expression) {
312 ast_destroy(node->data.expression_stmt.expression);
313 node->data.expression_stmt.expression = NULL;
317 case AST_IF_STATEMENT:
318 if (node->data.if_stmt.condition) {
319 ast_destroy(node->data.if_stmt.condition);
320 node->data.if_stmt.condition = NULL;
322 if (node->data.if_stmt.then_stmt) {
323 ast_destroy(node->data.if_stmt.then_stmt);
324 node->data.if_stmt.then_stmt = NULL;
326 if (node->data.if_stmt.else_stmt) {
327 ast_destroy(node->data.if_stmt.else_stmt);
328 node->data.if_stmt.else_stmt = NULL;
332 case AST_WHILE_STATEMENT:
333 if (node->data.while_stmt.condition) {
334 ast_destroy(node->data.while_stmt.condition);
335 node->data.while_stmt.condition = NULL;
337 if (node->data.while_stmt.body) {
338 ast_destroy(node->data.while_stmt.body);
339 node->data.while_stmt.body = NULL;
343 case AST_FOR_STATEMENT:
344 if (node->data.for_stmt.init) {
345 ast_destroy(node->data.for_stmt.init);
346 node->data.for_stmt.init = NULL;
348 if (node->data.for_stmt.condition) {
349 ast_destroy(node->data.for_stmt.condition);
350 node->data.for_stmt.condition = NULL;
352 if (node->data.for_stmt.update) {
353 ast_destroy(node->data.for_stmt.update);
354 node->data.for_stmt.update = NULL;
356 if (node->data.for_stmt.body) {
357 ast_destroy(node->data.for_stmt.body);
358 node->data.for_stmt.body = NULL;
362 case AST_RETURN_STATEMENT:
363 if (node->data.return_stmt.expression) {
364 ast_destroy(node->data.return_stmt.expression);
365 node->data.return_stmt.expression = NULL;
370 if (node->data.binary_expr.left) {
371 ast_destroy(node->data.binary_expr.left);
372 node->data.binary_expr.left = NULL;
374 if (node->data.binary_expr.right) {
375 ast_destroy(node->data.binary_expr.right);
376 node->data.binary_expr.right = NULL;
381 if (node->data.unary_expr.operand) {
382 ast_destroy(node->data.unary_expr.operand);
383 node->data.unary_expr.operand = NULL;
388 if (node->data.assignment.variable) {
389 free(node->data.assignment.variable);
390 node->data.assignment.variable = NULL;
392 if (node->data.assignment.value) {
393 ast_destroy(node->data.assignment.value);
394 node->data.assignment.value = NULL;
398 case AST_FUNCTION_CALL:
399 if (node->data.call_expr.function_name) {
400 free(node->data.call_expr.function_name);
401 node->data.call_expr.function_name = NULL;
403 if (node->data.call_expr.arguments) {
404 for (
int i = 0; i < node->data.call_expr.argument_count; i++) {
405 ast_destroy(node->data.call_expr.arguments[i]);
407 free(node->data.call_expr.arguments);
408 node->data.call_expr.arguments = NULL;
413 if (node->data.identifier.name) {
414 free(node->data.identifier.name);
415 node->data.identifier.name = NULL;
419 case AST_STRING_LITERAL:
420 if (node->data.string.value) {
421 free(node->data.string.value);
422 node->data.string.value = NULL;
426 case AST_NUMBER_LITERAL:
427 case AST_CHAR_LITERAL:
428 case AST_BREAK_STATEMENT:
429 case AST_CONTINUE_STATEMENT:
435 #ifdef DEBUG_AST_DESTROY
436 printf(
"WARNING: Unknown node type %d in ast_destroy\n", node->type);
ASTNodeType
AST Node types.
TokenType
Token types for lexical analysis.
DataType
Data types supported by the compiler.