33#include <string.h>
44#include "../error/error.h"
55
6- // Helper functions for common AST node creation patterns
7-
8- ASTNode * create_number_node (double value ) {
9- ASTNode * ast_node = malloc (sizeof (ASTNode ));
10- if (!ast_node ) {
11- report_error ("[AST] malloc failed for ASTNode" );
12- return NULL ;
13- }
14- ast_node -> type = AST_NUMBER ;
15- ast_node -> parent = NULL ;
16- ast_node -> number .value = value ;
17- return ast_node ;
18- }
19-
20- ASTNode * create_var_node (const char * name ) {
21- ASTNode * ast_node = malloc (sizeof (ASTNode ));
22- if (!ast_node ) {
23- report_error ("[AST] malloc failed for ASTNode" );
24- return NULL ;
25- }
26- ast_node -> type = AST_VAR ;
27- ast_node -> parent = NULL ;
28- ast_node -> var .name = strdup (name );
29- if (!ast_node -> var .name ) {
30- report_error ("[AST] strdup failed for variable name" );
31- free (ast_node );
32- return NULL ;
33- }
34- return ast_node ;
35- }
36-
37- ASTNode * create_binary_node (Token_Type op , ASTNode * left , ASTNode * right ) {
38- ASTNode * ast_node = malloc (sizeof (ASTNode ));
39- if (!ast_node ) {
40- report_error ("[AST] malloc failed for ASTNode" );
41- return NULL ;
42- }
43- ast_node -> type = AST_BINARY ;
44- ast_node -> parent = NULL ;
45- ast_node -> binary_expr .op = op ;
46- ast_node -> binary_expr .left = left ;
47- ast_node -> binary_expr .right = right ;
48- return ast_node ;
49- }
50-
51- ASTNode * create_unary_node (Token_Type op , ASTNode * operand ) {
52- ASTNode * ast_node = malloc (sizeof (ASTNode ));
53- if (!ast_node ) {
54- report_error ("[AST] malloc failed for ASTNode" );
55- return NULL ;
56- }
57- ast_node -> type = AST_UNARY ;
58- ast_node -> parent = NULL ;
59- ast_node -> unary_expr .op = op ;
60- ast_node -> unary_expr .operand = operand ;
61- return ast_node ;
62- }
63-
64- ASTNode * create_call_node (const char * name , ASTNode * * args , int arg_count ) {
65- ASTNode * ast_node = malloc (sizeof (ASTNode ));
66- if (!ast_node ) {
67- report_error ("[AST] malloc failed for ASTNode" );
68- return NULL ;
69- }
70- ast_node -> type = AST_CALL ;
71- ast_node -> parent = NULL ;
72- ast_node -> call_expr .name = strdup (name );
73- if (!ast_node -> call_expr .name ) {
74- report_error ("[AST] strdup failed for function name" );
75- free (ast_node );
76- return NULL ;
77- }
78- ast_node -> call_expr .args = args ;
79- ast_node -> call_expr .arg_count = arg_count ;
80- return ast_node ;
81- }
82-
83- ASTNode * create_array_literal_node (ASTNode * * elements , int count ) {
84- ASTNode * ast_node = malloc (sizeof (ASTNode ));
85- if (!ast_node ) {
86- report_error ("[AST] malloc failed for ASTNode" );
87- return NULL ;
88- }
89- ast_node -> type = AST_ARRAY_LITERAL ;
90- ast_node -> parent = NULL ;
91- ast_node -> array_literal .elements = elements ;
92- ast_node -> array_literal .count = count ;
93- return ast_node ;
94- }
95-
96- ASTNode * create_block_node (ASTNode * * statements , int count ) {
97- ASTNode * ast_node = malloc (sizeof (ASTNode ));
98- if (!ast_node ) {
99- report_error ("[AST] malloc failed for ASTNode" );
100- return NULL ;
101- }
102- ast_node -> type = AST_BLOCK ;
103- ast_node -> parent = NULL ;
104- ast_node -> block .statements = statements ;
105- ast_node -> block .count = count ;
106- return ast_node ;
107- }
6+ // Helper functions for common AST node creation patterns
0 commit comments