website

Website contents
git clone git://git.reagancfischer.dev/website.git
Log | Files | Refs

ast.lit (62256B)


      1 @code_type c .c
      2 @comment_type /* %s */
      3 @compiler lit -t ast.lit && gcc -Wall -Wextra -Wstrict-aliasing=3 -Wwrite-strings -Wvla -Wcast-align=strict -Wstrict-prototypes -Wstringop-overflow=4  -fanalyzer -c ast.c -g -O0 -o ast.o
      4 
      5 @title AST code
      6 @add_css ./style.css
      7 
      8 @s
      9 This is the AST code. There's no important logic here, this is just accessors and constructors for the AST nodes
     10 
     11 @s The AST Interface - Node Types
     12 
     13 --- Node Types
     14 typedef struct ast_node ast_node_t;
     15 
     16 typedef enum {
     17     // Expression nodes
     18     AST_INTU32,
     19     AST_INTU64,
     20     AST_INTS32,
     21     AST_INTS64,
     22     AST_FLOAT,
     23     AST_DOUBLE,
     24     AST_STRING,
     25     AST_CHAR,
     26     AST_VAR,
     27     AST_SUBSCRIPT,
     28     AST_DIRECT_COMPONENT_SELECTION,
     29     AST_INDIRECT_COMPONENT_SELECTION,
     30     AST_FUNCTION_CALL,
     31     AST_POSTFIX_INC,
     32     AST_POSTFIX_DEC,
     33     AST_UNARY_PLUS,
     34     AST_UNARY_MINUS,
     35     AST_LOGICAL_NOT,
     36     AST_BITWISE_NOT,
     37     AST_ADDRESS_OF,
     38     AST_INDIRECTION,
     39     AST_SIZEOF,
     40     AST_PRE_INC,
     41     AST_PRE_DEC,
     42     AST_CAST,
     43     AST_MUL,
     44     AST_DIV,
     45     AST_MOD,
     46     AST_ADD,
     47     AST_SUB,
     48     AST_LEFT_SHIFT,
     49     AST_RIGHT_SHIFT,
     50     AST_LESS_THAN,
     51     AST_GREATER_THAN,
     52     AST_LESS_THAN_OR_EQUAL,
     53     AST_GREATER_THAN_OR_EQUAL,
     54     AST_EQUAL,
     55     AST_NOT_EQUAL,
     56     AST_BITWISE_AND,
     57     AST_BITWISE_XOR,
     58     AST_BITWISE_OR,
     59     AST_LOGICAL_AND,
     60     AST_LOGICAL_OR,
     61     AST_CONDITIONAL,
     62     AST_ASSIGN,
     63     AST_ADD_ASSIGN,
     64     AST_SUB_ASSIGN,
     65     AST_MUL_ASSIGN,
     66     AST_DIV_ASSIGN,
     67     AST_MOD_ASSIGN,
     68     AST_LEFT_SHIFT_ASSIGN,
     69     AST_RIGHT_SHIFT_ASSIGN,
     70     AST_AND_ASSIGN,
     71     AST_XOR_ASSIGN,
     72     AST_OR_ASSIGN,
     73     AST_COMMA,
     74     // Statement nodes
     75     AST_EXPRESSION_STATEMENT,
     76     AST_LABEL_STATEMENT,
     77     AST_CASE_STATEMENT,
     78     AST_DEFAULT_STATEMENT,
     79     AST_COMPOUND_STATEMENT,
     80     AST_IF_STATEMENT,
     81     AST_WHILE_STATEMENT,
     82     AST_DO_WHILE_STATEMENT,
     83     AST_FOR_STATEMENT,
     84     AST_GOTO_STATEMENT,
     85     AST_CONTINUE_STATEMENT,
     86     AST_BREAK_STATEMENT,
     87     AST_RETURN_STATEMENT,
     88     AST_SWITCH_STATEMENT,
     89     AST_NULL_STATEMENT,
     90     // Declaration nodes
     91     AST_VAR_DECL,
     92     AST_FUN_DECL,
     93     AST_PARAM,
     94     AST_FUN_DEF,
     95     AST_STRUCT_DECL,
     96     AST_UNION_DECL,
     97     AST_ENUM_DECL,
     98     AST_TYPEDEF,
     99     AST_TYPE,
    100     AST_MEMBER,
    101     AST_ENUM_MEMBER,
    102     // Translation unit
    103     AST_TRANSLATION_UNIT
    104 } ast_node_type_t;
    105 ---
    106 
    107 @s The AST Interface - Creating Nodes
    108 These functions create nodes for the AST.
    109 
    110 The order of functions here partly follows the order of the grammar rules from the bottom up, I.E primary -> postfix -> unary -> multiplicative -> additive -> relational -> equality -> assignment -> expression -> statement -> declaration.
    111 --- Node Creation (Expression Nodes)
    112 // Expression nodes (primary)
    113 ast_node_t *ast_create_ints32_node(int32_t value, int line, int column); // 1
    114 ast_node_t *ast_create_ints64_node(int64_t value, int line, int column); // 1L
    115 ast_node_t *ast_create_intu32_node(uint32_t value, int line, int column); // 1u
    116 ast_node_t *ast_create_intu64_node(uint64_t value, int line, int column); // 1ul
    117 ast_node_t *ast_create_float_node(float value, int line, int column); // 1.0f
    118 ast_node_t *ast_create_double_node(double value, int line, int column); // 1.0
    119 ast_node_t *ast_create_string_node(char *value, int line, int column); // "string"
    120 ast_node_t *ast_create_char_node(char value, int line, int column); // 'c'
    121 ast_node_t *ast_create_var_node(char *value, int line, int column); // Variable
    122 // Nothing needed for paren as the child can just be placed in the parent node directly
    123 // Expression nodes (postfix)
    124 ast_node_t *ast_create_subscript_node(ast_node_t *expr, ast_node_t *subscript, int line, int column); // expr[subscript]
    125 ast_node_t *ast_create_direct_component_selection_node(ast_node_t *expr, char *component, int line, int column); // .component
    126 ast_node_t *ast_create_indirect_component_selection_node(ast_node_t *expr, char *component, int line, int column); // ->component
    127 ast_node_t *ast_create_function_call_node(ast_node_t *expr, ast_node_t **args, size_t num_args, int line, int column); // expr(arg1, arg2, ...)
    128 ast_node_t  *ast_create_postfix_inc_node(ast_node_t *expr, int line, int column); // expr++
    129 ast_node_t  *ast_create_postfix_dec_node(ast_node_t *expr, int line, int column); // expr--
    130 // Expression nodes (unary)
    131 ast_node_t *ast_create_unary_plus_node(ast_node_t *expr, int line, int column); // +expr
    132 ast_node_t *ast_create_unary_minus_node(ast_node_t *expr, int line, int column); // -expr
    133 ast_node_t *ast_create_logical_not_node(ast_node_t *expr, int line, int column); // !expr
    134 ast_node_t *ast_create_bitwise_not_node(ast_node_t *expr, int line, int column); // ~expr
    135 ast_node_t *ast_create_address_of_node(ast_node_t *expr, int line, int column); // &expr
    136 ast_node_t *ast_create_indirection_node(ast_node_t *expr, int line, int column); // *expr
    137 ast_node_t *ast_create_sizeof_node(ast_node_t *expr, int line, int column); // sizeof(expr)
    138 ast_node_t *ast_create_pre_inc_node(ast_node_t *expr, int line, int column); // ++expr
    139 ast_node_t *ast_create_pre_dec_node(ast_node_t *expr, int line, int column); // --expr
    140 // Expression nodes (cast)
    141 ast_node_t *ast_create_cast_node(ast_node_t *type, ast_node_t *expr, int line, int column); // (type)expr
    142 // Expression nodes (multiplicative)
    143 ast_node_t *ast_create_mul_node(ast_node_t *left, ast_node_t *right, int line, int column); // left * right
    144 ast_node_t *ast_create_div_node(ast_node_t *left, ast_node_t *right, int line, int column); // left / right
    145 ast_node_t *ast_create_mod_node(ast_node_t *left, ast_node_t *right, int line, int column); // left % right
    146 // Expression nodes (additive)
    147 ast_node_t *ast_create_add_node(ast_node_t *left, ast_node_t *right, int line, int column); // left + right
    148 ast_node_t *ast_create_sub_node(ast_node_t *left, ast_node_t *right, int line, int column); // left - right
    149 // Expression nodes (shift)
    150 ast_node_t *ast_create_left_shift_node(ast_node_t *left, ast_node_t *right, int line, int column); // left << right
    151 ast_node_t *ast_create_right_shift_node(ast_node_t *left, ast_node_t *right, int line, int column); // left >> right
    152 // Expression nodes (relational)
    153 ast_node_t *ast_create_less_than_node(ast_node_t *left, ast_node_t *right, int line, int column); // left < right
    154 ast_node_t *ast_create_greater_than_node(ast_node_t *left, ast_node_t *right, int line, int column); // left > right
    155 ast_node_t *ast_create_less_than_or_equal_node(ast_node_t *left, ast_node_t *right, int line, int column); // left <= right
    156 ast_node_t *ast_create_greater_than_or_equal_node(ast_node_t *left, ast_node_t *right, int line, int column); // left >= right
    157 // Expression nodes (equality)
    158 ast_node_t *ast_create_equal_node(ast_node_t *left, ast_node_t *right, int line, int column); // left == right
    159 ast_node_t *ast_create_not_equal_node(ast_node_t *left, ast_node_t *right, int line, int column); // left != right
    160 // Expression nodes (bitwise and)
    161 ast_node_t *ast_create_bitwise_and_node(ast_node_t *left, ast_node_t *right, int line, int column); // left & right
    162 // Expression nodes (bitwise xor)
    163 ast_node_t *ast_create_bitwise_xor_node(ast_node_t *left, ast_node_t *right, int line, int column); // left ^ right
    164 // Expression nodes (bitwise or)
    165 ast_node_t *ast_create_bitwise_or_node(ast_node_t *left, ast_node_t *right, int line, int column); // left | right
    166 // Expression nodes (logical and)
    167 ast_node_t *ast_create_logical_and_node(ast_node_t *left, ast_node_t *right, int line, int column); // left && right
    168 // Expression nodes (logical or)
    169 ast_node_t *ast_create_logical_or_node(ast_node_t *left, ast_node_t *right, int line, int column); // left || right
    170 // Expression nodes (conditional)
    171 ast_node_t *ast_create_conditional_node(ast_node_t *condition, ast_node_t *true_expr, ast_node_t *false_expr, int line, int column); // condition ? true_expr : false_expr
    172 // Expression nodes (assignment)
    173 ast_node_t *ast_create_assign_node(ast_node_t *left, ast_node_t *right, int line, int column); // left = right
    174 ast_node_t *ast_create_add_assign_node(ast_node_t *left, ast_node_t *right, int line, int column); // left += right
    175 ast_node_t *ast_create_sub_assign_node(ast_node_t *left, ast_node_t *right, int line, int column); // left -= right
    176 ast_node_t *ast_create_mul_assign_node(ast_node_t *left, ast_node_t *right, int line, int column); // left *= right
    177 ast_node_t *ast_create_div_assign_node(ast_node_t *left, ast_node_t *right, int line, int column); // left /= right
    178 ast_node_t *ast_create_mod_assign_node(ast_node_t *left, ast_node_t *right, int line, int column); // left %= right
    179 ast_node_t *ast_create_left_shift_assign_node(ast_node_t *left, ast_node_t *right, int line, int column); // left <<= right
    180 ast_node_t *ast_create_right_shift_assign_node(ast_node_t *left, ast_node_t *right, int line, int column); // left >>= right
    181 ast_node_t *ast_create_and_assign_node(ast_node_t *left, ast_node_t *right, int line, int column); // left &= right
    182 ast_node_t *ast_create_xor_assign_node(ast_node_t *left, ast_node_t *right, int line, int column); // left ^= right
    183 ast_node_t *ast_create_or_assign_node(ast_node_t *left, ast_node_t *right, int line, int column); // left |= right
    184 // Expression nodes (comma)
    185 ast_node_t *ast_create_comma_node(ast_node_t *left, ast_node_t *right, int line, int column); // left, right
    186 ---
    187 
    188 @s
    189 Here we define functions for creating statement nodes.
    190 --- Node Creation (Statement Nodes)
    191 // Statement nodes (expression)
    192 ast_node_t *ast_create_expression_statement_node(ast_node_t *expr); // expr;
    193 // Statement nodes (label)
    194 ast_node_t *ast_create_label_statement_node(char *label, int line, int column); // label:
    195 ast_node_t *ast_create_case_statement_node(ast_node_t *expr, int line, int column); // case expr:
    196 ast_node_t *ast_create_default_statement_node(int line, int column); // default:
    197 // Statement nodes (compound)
    198 ast_node_t *ast_create_compound_statement_node(ast_node_t **decls_and_stmts, size_t num_decls_and_stmts, int line, int column); // { decls_and_stmts }
    199 // Statement nodes (conditional)
    200 ast_node_t *ast_create_if_statement_node(ast_node_t *condition, ast_node_t *true_stmt, ast_node_t *false_stmt, int line, int column); // if (condition) true_stmt else false_stmt
    201 // Statement nodes (iteration)
    202 ast_node_t *ast_create_while_statement_node(ast_node_t *condition, ast_node_t *stmt, int line, int column); // while (condition) stmt
    203 ast_node_t *ast_create_do_while_statement_node(ast_node_t *stmt, ast_node_t *condition, int line, int column); // do stmt while (condition)
    204 ast_node_t *ast_create_for_statement_node(ast_node_t *init, ast_node_t *condition, ast_node_t *update, ast_node_t *stmt, int line, int column); // for (init; condition; update) stmt
    205 // Statement nodes (jump)
    206 ast_node_t *ast_create_goto_statement_node(char *label, int line, int column); // goto label;
    207 ast_node_t *ast_create_continue_statement_node(int line, int column); // continue;
    208 ast_node_t *ast_create_break_statement_node(int line, int column); // break;
    209 ast_node_t *ast_create_return_statement_node(ast_node_t *expr, int line, int column); // return expr;
    210 // Statement nodes (multiway branch)
    211 ast_node_t *ast_create_switch_statement_node(ast_node_t *expr, ast_node_t *stmt, int line, int column); // switch (expr) stmt
    212 // Statement nodes (null)
    213 ast_node_t *ast_create_null_statement_node(int line, int column); // ;
    214 ---
    215 
    216 @s
    217 Here we define functions for creating declaration nodes. Types are passed as a raw list of tokens, which will be used in the semantic analysis phase to build a type. In this way, we're kind of offsetting type parsing.
    218 
    219 An example call for a declaration would be `ast_node_t *type = ast_create_type_node((token_t *[]){ptr to TOK_VOLATILE, ptr to TOK_CONST, ptr to TOK_INT}, 3);`, `ast_node_t *decl = ast_create_var_decl_node(type, "x");`.
    220 
    221 A special AST node is created for parameters, struct/union members, and enum members. The enum member node can hold a value, but the others are just for the type.
    222 A special AST node also exists for types.
    223 --- Node Creation (Declaration Nodes)
    224 // Declaration nodes (simple)
    225 ast_node_t *ast_create_var_decl_node(ast_node_t *type, char *id, int line, int column); // type id
    226 ast_node_t *ast_create_fun_decl_node(ast_node_t *type, char *id, ast_node_t **params, size_t num_params, int line, int column); // type id(params)
    227 // Declaration nodes (parameter)
    228 ast_node_t *ast_create_param_node(ast_node_t *type, char *id, int line, int column); // type id
    229 // Declaration nodes (function)
    230 ast_node_t *ast_create_fun_def_node(ast_node_t *type, char *id, ast_node_t **params, size_t num_params, ast_node_t *stmt, int line, int column); // type id(params) stmt
    231 // Declaration nodes (struct) 
    232 ast_node_t *ast_create_struct_decl_node(char *id, ast_node_t **members, size_t num_members, int line, int column); // struct id { members } (reuses param node for members)
    233 // Declaration nodes (union)
    234 ast_node_t *ast_create_union_decl_node(char *id, ast_node_t **members, size_t num_members, int line, int column); // union id { members } (reuses param node for members)
    235 // Declaration nodes (enum)
    236 ast_node_t *ast_create_enum_decl_node(char *id, ast_node_t **enums, size_t num_enums, int line, int column); // enum id { enums }
    237 // Declaration nodes (Typedef)
    238 ast_node_t *ast_create_typedef_node(ast_node_t *type, char *id, int line, int column); // typedef type id
    239 // Types/members
    240 ast_node_t *ast_create_type_node(token_t *type[], size_t num_type, int line, int column); // type
    241 ast_node_t *ast_create_member_node(ast_node_t *type, char *id, int line, int column); // type id
    242 ast_node_t *ast_create_enum_member_node(char *id, ast_node_t *value, int line, int column); // id = value
    243 ---
    244 
    245 @s The AST Interface - Translation Unit
    246 The translation unit is the root of the AST. It's a list of nodes, which can be declarations or statements. This is the only function that will be called from the parser.
    247 --- Node Creation (Translation Unit)
    248 ast_node_t *ast_create_translation_unit(ast_node_t **nodes, size_t num_nodes, int line, int column);
    249 ---
    250 @s The AST Interface - Destroying Nodes
    251 A single function is defined to destroy an AST node and all its children.
    252 --- Node Destruction
    253 void ast_destroy_node(ast_node_t *node);
    254 ---
    255 
    256 @s The AST Interface - Getting Node Information
    257 These functions get information from the AST nodes. They will do on-the-fly type checking to ensure the node is the correct type, so we need to have separate functions for each type of node.
    258 
    259 @s
    260 --- Node Information (General)
    261 ast_node_type_t ast_get_node_type(ast_node_t *node);
    262 int ast_get_node_line(ast_node_t *node);
    263 int ast_get_node_column(ast_node_t *node);
    264 ---
    265 
    266 @s
    267 --- Node Information (Expression Nodes)
    268 // Expression nodes (primary)
    269 int64_t ast_get_int_value(ast_node_t *node);
    270 double ast_get_double_value(ast_node_t *node);
    271 char *ast_get_string_value(ast_node_t *node);
    272 char ast_get_char_value(ast_node_t *node);
    273 char *ast_get_var_value(ast_node_t *node);
    274 // Expression nodes (postfix)
    275 ast_node_t *ast_get_subscript_expr(ast_node_t *node);
    276 ast_node_t *ast_get_subscript_idx(ast_node_t *node);
    277 char *ast_get_component(ast_node_t *node);
    278 ast_node_t *ast_get_function_call_expr(ast_node_t *node);
    279 ast_node_t **ast_get_function_call_args(ast_node_t *node, size_t *num_args);
    280 ast_node_t *ast_get_postfix_expr(ast_node_t *node);
    281 // Expression nodes (unary)
    282 ast_node_t *ast_get_unary_expr(ast_node_t *node);
    283 // Expression nodes (cast)
    284 ast_node_t *ast_get_cast_type(ast_node_t *node);
    285 ast_node_t *ast_get_cast_expr(ast_node_t *node);
    286 // Expression nodes (binary)
    287 ast_node_t *ast_get_left_expr(ast_node_t *node);
    288 ast_node_t *ast_get_right_expr(ast_node_t *node);
    289 // Expression nodes (conditional)
    290 ast_node_t *ast_get_conditional_condition(ast_node_t *node);
    291 ast_node_t *ast_get_conditional_true_expr(ast_node_t *node);
    292 ast_node_t *ast_get_conditional_false_expr(ast_node_t *node);
    293 ---
    294 
    295 @s
    296 --- Node Information (Statement Nodes)
    297 // Statement nodes (expression)
    298 ast_node_t *ast_get_expression_statement_expr(ast_node_t *node);
    299 // Statement nodes (label)
    300 char *ast_get_label_statement_label(ast_node_t *node); // label
    301 ast_node_t *ast_get_case_statement_expr(ast_node_t *node); // expr
    302 // Statement nodes (compound)
    303 ast_node_t **ast_get_compound_statement_contents(ast_node_t *node, size_t *num_contents); // decls_and_stmts
    304 // Statement nodes (conditional)
    305 ast_node_t *ast_get_if_statement_condition(ast_node_t *node); // condition
    306 ast_node_t *ast_get_if_statement_true_stmt(ast_node_t *node); // true_stmt
    307 ast_node_t *ast_get_if_statement_false_stmt(ast_node_t *node); // false_stmt
    308 // Statement nodes (iteration)
    309 ast_node_t *ast_get_iteration_statement_condition(ast_node_t *node); // condition
    310 ast_node_t *ast_get_iteration_statement_stmt(ast_node_t *node); // stmt
    311 ast_node_t *ast_get_for_statement_init(ast_node_t *node); // init
    312 ast_node_t *ast_get_for_statement_update(ast_node_t *node); // update
    313 // Statement nodes (jump)
    314 char *ast_get_goto_statement_label(ast_node_t *node); // label
    315 // Statement nodes (multiway branch)
    316 ast_node_t *ast_get_switch_statement_expr(ast_node_t *node); // expr
    317 ast_node_t *ast_get_switch_statement_stmt(ast_node_t *node); // stmt
    318 ---
    319 
    320 @s
    321 --- Node Information (Declaration Nodes)
    322 // Declaration nodes (simple)
    323 ast_node_t *ast_get_var_decl_type(ast_node_t *node);
    324 char *ast_get_var_decl_id(ast_node_t *node);
    325 ast_node_t *ast_get_fun_decl_type(ast_node_t *node);
    326 char *ast_get_fun_decl_id(ast_node_t *node);
    327 ast_node_t **ast_get_fun_decl_params(ast_node_t *node, size_t *num_params);
    328 // Declaration nodes (parameter)
    329 ast_node_t *ast_get_param_type(ast_node_t *node);
    330 char *ast_get_param_id(ast_node_t *node);
    331 // Declaration nodes (function)
    332 ast_node_t *ast_get_fun_def_type(ast_node_t *node);
    333 char *ast_get_fun_def_id(ast_node_t *node);
    334 ast_node_t **ast_get_fun_def_params(ast_node_t *node, size_t *num_params);
    335 ast_node_t *ast_get_fun_def_stmt(ast_node_t *node);
    336 // Declaration nodes (struct)
    337 char *ast_get_struct_decl_id(ast_node_t *node);
    338 ast_node_t **ast_get_struct_decl_members(ast_node_t *node, size_t *num_members);
    339 // Declaration nodes (union)
    340 char *ast_get_union_decl_id(ast_node_t *node);
    341 ast_node_t **ast_get_union_decl_members(ast_node_t *node, size_t *num_members);
    342 // Declaration nodes (enum)
    343 char *ast_get_enum_decl_id(ast_node_t *node);
    344 ast_node_t **ast_get_enum_decl_enums(ast_node_t *node, size_t *num_enums);
    345 // Declaration nodes (Typedef)
    346 ast_node_t *ast_get_typedef_type(ast_node_t *node);
    347 char *ast_get_typedef_id(ast_node_t *node);
    348 // Types/members
    349 token_t **ast_get_type(ast_node_t *node, size_t *num_type);
    350 ast_node_t *ast_get_member_type(ast_node_t *node);
    351 char *ast_get_member_id(ast_node_t *node);
    352 ast_node_t *ast_get_enum_member_expr(ast_node_t *node);
    353 ---
    354 
    355 @s
    356 
    357 --- Node Information (Translation Unit)
    358 ast_node_t **ast_get_translation_unit(ast_node_t *node, size_t *num_nodes);
    359 ---
    360 
    361 @s
    362 Having seperate calls for each type makes it very difficult to mess up the tree structure in the parser.
    363 
    364 @s 
    365 
    366 --- ast.h
    367 #ifndef AST_H
    368 #define AST_H
    369 #include "token.h"
    370 #include <stddef.h>
    371 #include <stdint.h>
    372 @{Node Types}
    373 @{Node Creation (Expression Nodes)}
    374 @{Node Creation (Statement Nodes)}
    375 @{Node Creation (Declaration Nodes)}
    376 @{Node Creation (Translation Unit)}
    377 @{Node Destruction}
    378 @{Node Information (General)}
    379 @{Node Information (Expression Nodes)}
    380 @{Node Information (Statement Nodes)}
    381 @{Node Information (Declaration Nodes)}
    382 @{Node Information (Translation Unit)}
    383 #endif
    384 ---
    385 
    386 --- Implementation - Common Node Structure
    387 #define AST_MAGIC 0x4153544E4F44ULL // "ASTNOD"
    388 
    389 struct ast_node {
    390   long magic;
    391   int line;
    392   int column;
    393   int hasdata;
    394   short kind;
    395   long opt_data[0];
    396 };
    397 
    398 static void* ast_get_data(ast_node_t *node)
    399 {
    400   assert(node->magic == AST_MAGIC);
    401   assert(node->hasdata);
    402   return node->opt_data;
    403 }
    404 
    405 ast_node_type_t ast_get_node_type(ast_node_t *node)
    406 {
    407   assert(node->magic == AST_MAGIC);
    408   return node->kind;
    409 }
    410 
    411 int ast_get_node_line(ast_node_t *node)
    412 {
    413   assert(node->magic == AST_MAGIC);
    414   return node->line;
    415 }
    416 
    417 int ast_get_node_column(ast_node_t *node)
    418 {
    419   assert(node->magic == AST_MAGIC);
    420   return node->column;
    421 }
    422 
    423 static ast_node_t *ast_create_node(ast_node_type_t kind, int line, int column, size_t data_size)
    424 {
    425   ast_node_t *node = malloc(sizeof(ast_node_t) + data_size);
    426   node->magic = AST_MAGIC;
    427   node->line = line;
    428   node->column = column;
    429   node->hasdata = data_size > 0;
    430   node->kind = kind;
    431   return node;
    432 }
    433 ---
    434 
    435 
    436 
    437 @s
    438 --- Implementation - Nodes (Primary)
    439 struct ast_int_node {
    440   int64_t value;
    441 };
    442 
    443 ast_node_t *ast_create_intu32_node(uint32_t value, int line, int column)
    444 {
    445   ast_node_t *node =
    446       ast_create_node(AST_INTU32, line, column, sizeof(struct ast_int_node));
    447   struct ast_int_node *data = ast_get_data(node);
    448   data->value = value;
    449   return node;
    450 }
    451 
    452 ast_node_t *ast_create_ints32_node(int32_t value, int line, int column)
    453 {
    454   ast_node_t *node =
    455       ast_create_node(AST_INTS32, line, column, sizeof(struct ast_int_node));
    456   struct ast_int_node *data = ast_get_data(node);
    457   data->value = value;
    458   return node;
    459 }
    460 
    461 ast_node_t *ast_create_intu64_node(uint64_t value, int line, int column)
    462 {
    463   ast_node_t *node =
    464       ast_create_node(AST_INTU64, line, column, sizeof(struct ast_int_node));
    465   struct ast_int_node *data = ast_get_data(node);
    466   data->value = value;
    467   return node;
    468 }
    469 
    470 ast_node_t *ast_create_ints64_node(int64_t value, int line, int column)
    471 {
    472   ast_node_t *node =
    473       ast_create_node(AST_INTS64, line, column, sizeof(struct ast_int_node));
    474   struct ast_int_node *data = ast_get_data(node);
    475   data->value = value;
    476   return node;
    477 }
    478 
    479 int64_t ast_get_int_value(ast_node_t *node) {
    480   assert(ast_get_node_type(node) == AST_INTU32 || ast_get_node_type(node) == AST_INTS32 || ast_get_node_type(node) == AST_INTU64 || ast_get_node_type(node) == AST_INTS64);
    481   struct ast_int_node *data = ast_get_data(node);
    482   return data->value;
    483 }
    484 
    485 struct ast_double_node {
    486   double value;
    487 };
    488 
    489 struct ast_float_node {
    490     float value;
    491 };
    492 
    493 ast_node_t *ast_create_float_node(float value, int line, int column)
    494 {
    495   ast_node_t *node = ast_create_node(AST_FLOAT, line, column, sizeof(struct ast_float_node));
    496   struct ast_float_node *data = ast_get_data(node);
    497   data->value = value;
    498   return node;
    499 }
    500 
    501 float ast_get_float_value(ast_node_t *node)
    502 {
    503   assert(ast_get_node_type(node) == AST_FLOAT);
    504   struct ast_float_node *data = ast_get_data(node);
    505   return data->value;
    506 }
    507 
    508 ast_node_t *ast_create_double_node(double value, int line, int column)
    509 {
    510   ast_node_t *node = ast_create_node(AST_DOUBLE, line, column, sizeof(struct ast_double_node));
    511   struct ast_double_node *data = ast_get_data(node);
    512   data->value = value;
    513   return node;
    514 }
    515 
    516 double ast_get_double_value(ast_node_t *node)
    517 {
    518   assert(ast_get_node_type(node) == AST_DOUBLE);
    519   struct ast_double_node *data = ast_get_data(node);
    520   return data->value;
    521 }
    522 
    523 struct ast_string_node {
    524   char *value;
    525 };
    526 
    527 ast_node_t *ast_create_string_node(char *value, int line, int column)
    528 {
    529   ast_node_t *node = ast_create_node(AST_STRING, line, column, sizeof(struct ast_string_node));
    530   struct ast_string_node *data = ast_get_data(node);
    531   data->value = value; // We don't duplicate here because the string is in the shared string table
    532   return node;
    533 }
    534 
    535 char *ast_get_string_value(ast_node_t *node)
    536 {
    537   assert(ast_get_node_type(node) == AST_STRING);
    538   struct ast_string_node *data = ast_get_data(node);
    539   return data->value;
    540 }
    541 
    542 struct ast_char_node {
    543   char value;
    544 };
    545 
    546 ast_node_t *ast_create_char_node(char value, int line, int column)
    547 {
    548   ast_node_t *node = ast_create_node(AST_CHAR, line, column, sizeof(struct ast_char_node));
    549   struct ast_char_node *data = ast_get_data(node);
    550   data->value = value;
    551   return node;
    552 }
    553 
    554 char ast_get_char_value(ast_node_t *node)
    555 {
    556   assert(ast_get_node_type(node) == AST_CHAR);
    557   struct ast_char_node *data = ast_get_data(node);
    558   return data->value;
    559 }
    560 
    561 ast_node_t *ast_create_var_node(char *value, int line, int column)
    562 {
    563   ast_node_t *node = ast_create_node(AST_VAR, line, column, sizeof(struct ast_string_node));
    564   struct ast_string_node *data = ast_get_data(node);
    565   data->value = value; // We don't duplicate here because the string is in the shared string table
    566   return node;
    567 }
    568 
    569 char *ast_get_var_value(ast_node_t *node)
    570 {
    571   assert(ast_get_node_type(node) == AST_VAR);
    572   struct ast_string_node *data = ast_get_data(node);
    573   return data->value;
    574 }
    575 ---
    576 
    577 @s
    578 
    579 --- Implementation - Nodes (Postfix)
    580 struct ast_subscript_node {
    581   ast_node_t *expr;
    582   ast_node_t *subscript;
    583 };
    584 
    585 ast_node_t *ast_create_subscript_node(ast_node_t *expr, ast_node_t *subscript, int line, int column)
    586 {
    587   ast_node_t *node = ast_create_node(AST_SUBSCRIPT, line, column, sizeof(struct ast_subscript_node));
    588   struct ast_subscript_node *data = ast_get_data(node);
    589   data->expr = expr;
    590   data->subscript = subscript;
    591   return node;
    592 }
    593 
    594 ast_node_t *ast_get_subscript_expr(ast_node_t *node)
    595 {
    596   assert(ast_get_node_type(node) == AST_SUBSCRIPT);
    597   struct ast_subscript_node *data = ast_get_data(node);
    598   return data->expr;
    599 }
    600 
    601 ast_node_t *ast_get_subscript_idx(ast_node_t *node)
    602 {
    603   assert(ast_get_node_type(node) == AST_SUBSCRIPT);
    604   struct ast_subscript_node *data = ast_get_data(node);
    605   return data->subscript;
    606 }
    607 
    608 struct ast_component_selection_node {
    609   ast_node_t *expr;
    610   char *component;
    611 };
    612 
    613 ast_node_t *ast_create_direct_component_selection_node(ast_node_t *expr, char *component, int line, int column)
    614 {
    615   ast_node_t *node = ast_create_node(AST_DIRECT_COMPONENT_SELECTION, line, column, sizeof(struct ast_component_selection_node));
    616   struct ast_component_selection_node *data = ast_get_data(node);
    617   data->expr = expr;
    618   data->component = component;
    619   return node;
    620 }
    621 
    622 char *ast_get_component(ast_node_t *node)
    623 {
    624   assert(ast_get_node_type(node) == AST_DIRECT_COMPONENT_SELECTION);
    625   struct ast_component_selection_node *data = ast_get_data(node);
    626   return data->component;
    627 }
    628 
    629 ast_node_t *ast_get_component_expr(ast_node_t *node)
    630 {
    631   assert(ast_get_node_type(node) == AST_DIRECT_COMPONENT_SELECTION);
    632   struct ast_component_selection_node *data = ast_get_data(node);
    633   return data->expr;
    634 }
    635 
    636 ast_node_t *ast_create_indirect_component_selection_node(ast_node_t *expr, char *component, int line, int column)
    637 {
    638   ast_node_t *node = ast_create_node(AST_INDIRECT_COMPONENT_SELECTION, line, column, sizeof(struct ast_component_selection_node));
    639   struct ast_component_selection_node *data = ast_get_data(node);
    640   data->expr = expr;
    641   data->component = component;
    642   return node;
    643 }
    644 
    645 struct ast_function_call_node {
    646   ast_node_t *expr;
    647   ast_node_t **args;
    648   size_t num_args;
    649 };
    650 
    651 ast_node_t *ast_create_function_call_node(ast_node_t *expr, ast_node_t **args, size_t num_args, int line, int column)
    652 {
    653   ast_node_t *node = ast_create_node(AST_FUNCTION_CALL, line, column, sizeof(struct ast_function_call_node));
    654   struct ast_function_call_node *data = ast_get_data(node);
    655   data->expr = expr;
    656   data->args = args;
    657   data->num_args = num_args;
    658   return node;
    659 }
    660 
    661 ast_node_t *ast_get_function_call_expr(ast_node_t *node)
    662 {
    663   assert(ast_get_node_type(node) == AST_FUNCTION_CALL);
    664   struct ast_function_call_node *data = ast_get_data(node);
    665   return data->expr;
    666 }
    667 
    668 ast_node_t **ast_get_function_call_args(ast_node_t *node, size_t *num_args)
    669 {
    670   assert(ast_get_node_type(node) == AST_FUNCTION_CALL);
    671   struct ast_function_call_node *data = ast_get_data(node);
    672   *num_args = data->num_args;
    673   return data->args;
    674 }
    675 
    676 ast_node_t *ast_create_postfix_inc_node(ast_node_t *expr, int line, int column)
    677 {
    678   ast_node_t *node = ast_create_node(AST_POSTFIX_INC, line, column, sizeof(ast_node_t *));
    679   ast_node_t **data = ast_get_data(node);
    680   *data = expr;
    681   return node;
    682 }
    683 
    684 ast_node_t *ast_create_postfix_dec_node(ast_node_t *expr, int line, int column)
    685 {
    686   ast_node_t *node = ast_create_node(AST_POSTFIX_DEC, line, column, sizeof(ast_node_t *));
    687   ast_node_t **data = ast_get_data(node);
    688   *data = expr;
    689   return node;
    690 }
    691 
    692 ast_node_t *ast_get_postfix_expr(ast_node_t *node)
    693 {
    694   assert(ast_get_node_type(node) == AST_POSTFIX_INC || ast_get_node_type(node) == AST_POSTFIX_DEC);
    695   ast_node_t **data = ast_get_data(node);
    696   return *data;
    697 }
    698 ---
    699 
    700 @s
    701 
    702 --- Implementation - Nodes (Unary/Cast)
    703 
    704 #define AST_CREATE_UNARY_NODE(NODE_TYPE, NODE_NAME) \
    705 ast_node_t *ast_create_##NODE_NAME##_node(ast_node_t *expr, int line, int column) \
    706 { \
    707   ast_node_t *node = ast_create_node(NODE_TYPE, line, column, sizeof(ast_node_t *)); \
    708   ast_node_t **data = ast_get_data(node); \
    709   *data = expr; \
    710   return node; \
    711 }
    712 
    713 AST_CREATE_UNARY_NODE(AST_UNARY_PLUS, unary_plus)
    714 AST_CREATE_UNARY_NODE(AST_UNARY_MINUS, unary_minus)
    715 AST_CREATE_UNARY_NODE(AST_LOGICAL_NOT, logical_not)
    716 AST_CREATE_UNARY_NODE(AST_BITWISE_NOT, bitwise_not)
    717 AST_CREATE_UNARY_NODE(AST_ADDRESS_OF, address_of)
    718 AST_CREATE_UNARY_NODE(AST_INDIRECTION, indirection)
    719 AST_CREATE_UNARY_NODE(AST_SIZEOF, sizeof)
    720 AST_CREATE_UNARY_NODE(AST_PRE_INC, pre_inc)
    721 AST_CREATE_UNARY_NODE(AST_PRE_DEC, pre_dec)
    722 
    723 ast_node_t *ast_create_cast_node(ast_node_t *type, ast_node_t *expr, int line, int column)
    724 {
    725   ast_node_t *node = ast_create_node(AST_CAST, line, column, 2 * sizeof(ast_node_t *));
    726   ast_node_t **data = ast_get_data(node);
    727   data[0] = type;
    728   data[1] = expr;
    729   return node;
    730 }
    731 
    732 ast_node_t *ast_get_unary_expr(ast_node_t *node)
    733 {
    734   assert(ast_get_node_type(node) == AST_UNARY_PLUS || ast_get_node_type(node) == AST_UNARY_MINUS || ast_get_node_type(node) == AST_LOGICAL_NOT || ast_get_node_type(node) == AST_BITWISE_NOT || ast_get_node_type(node) == AST_ADDRESS_OF || ast_get_node_type(node) == AST_INDIRECTION || ast_get_node_type(node) == AST_SIZEOF || ast_get_node_type(node) == AST_PRE_INC || ast_get_node_type(node) == AST_PRE_DEC || ast_get_node_type(node) == AST_CAST);
    735   ast_node_t **data = ast_get_data(node);
    736   return data[0];
    737 }
    738 
    739 ast_node_t *ast_get_cast_type(ast_node_t *node)
    740 {
    741   assert(ast_get_node_type(node) == AST_CAST);
    742   ast_node_t **data = ast_get_data(node);
    743   return data[0];
    744 }
    745 
    746 ast_node_t *ast_get_cast_expr(ast_node_t *node)
    747 {
    748   assert(ast_get_node_type(node) == AST_CAST);
    749   ast_node_t **data = ast_get_data(node);
    750   return data[1];
    751 }
    752 
    753 ---
    754 
    755 @s
    756 
    757 --- Implementation - Nodes (Binary/Assign/Comma)
    758 struct ast_binary_node {
    759   ast_node_t *left;
    760   ast_node_t *right;
    761 };
    762 
    763 // Macro to generate binary AST node creation functions
    764 #define AST_CREATE_BINARY_NODE(NODE_TYPE, NODE_NAME) \
    765 ast_node_t *ast_create_##NODE_NAME##_node(ast_node_t *left, ast_node_t *right, int line, int column) \
    766 { \
    767   ast_node_t *node = ast_create_node(NODE_TYPE, line, column, sizeof(struct ast_binary_node)); \
    768   struct ast_binary_node *data = ast_get_data(node); \
    769   data->left = left; \
    770   data->right = right; \
    771   return node; \
    772 }
    773 
    774 AST_CREATE_BINARY_NODE(AST_MUL, mul)
    775 AST_CREATE_BINARY_NODE(AST_DIV, div)
    776 AST_CREATE_BINARY_NODE(AST_MOD, mod)
    777 AST_CREATE_BINARY_NODE(AST_ADD, add)
    778 AST_CREATE_BINARY_NODE(AST_SUB, sub)
    779 AST_CREATE_BINARY_NODE(AST_LEFT_SHIFT, left_shift)
    780 AST_CREATE_BINARY_NODE(AST_RIGHT_SHIFT, right_shift)
    781 AST_CREATE_BINARY_NODE(AST_LESS_THAN, less_than)
    782 AST_CREATE_BINARY_NODE(AST_GREATER_THAN, greater_than)
    783 AST_CREATE_BINARY_NODE(AST_LESS_THAN_OR_EQUAL, less_than_or_equal)
    784 AST_CREATE_BINARY_NODE(AST_GREATER_THAN_OR_EQUAL, greater_than_or_equal)
    785 AST_CREATE_BINARY_NODE(AST_EQUAL, equal)
    786 AST_CREATE_BINARY_NODE(AST_NOT_EQUAL, not_equal)
    787 AST_CREATE_BINARY_NODE(AST_BITWISE_AND, bitwise_and)
    788 AST_CREATE_BINARY_NODE(AST_BITWISE_XOR, bitwise_xor)
    789 AST_CREATE_BINARY_NODE(AST_BITWISE_OR, bitwise_or)
    790 AST_CREATE_BINARY_NODE(AST_LOGICAL_AND, logical_and)
    791 AST_CREATE_BINARY_NODE(AST_LOGICAL_OR, logical_or)
    792 AST_CREATE_BINARY_NODE(AST_ASSIGN, assign)
    793 AST_CREATE_BINARY_NODE(AST_ADD_ASSIGN, add_assign)
    794 AST_CREATE_BINARY_NODE(AST_SUB_ASSIGN, sub_assign)
    795 AST_CREATE_BINARY_NODE(AST_MUL_ASSIGN, mul_assign)
    796 AST_CREATE_BINARY_NODE(AST_DIV_ASSIGN, div_assign)
    797 AST_CREATE_BINARY_NODE(AST_MOD_ASSIGN, mod_assign)
    798 AST_CREATE_BINARY_NODE(AST_LEFT_SHIFT_ASSIGN, left_shift_assign)
    799 AST_CREATE_BINARY_NODE(AST_RIGHT_SHIFT_ASSIGN, right_shift_assign)
    800 AST_CREATE_BINARY_NODE(AST_AND_ASSIGN, bitwise_and_assign)
    801 AST_CREATE_BINARY_NODE(AST_XOR_ASSIGN, bitwise_xor_assign)
    802 AST_CREATE_BINARY_NODE(AST_OR_ASSIGN, bitwise_or_assign)
    803 AST_CREATE_BINARY_NODE(AST_COMMA, comma)
    804 
    805 ast_node_t *ast_get_left_expr(ast_node_t *node)
    806 {
    807   assert(ast_get_node_type(node) == AST_MUL || ast_get_node_type(node) == AST_DIV || ast_get_node_type(node) == AST_MOD
    808   || ast_get_node_type(node) == AST_ADD || ast_get_node_type(node) == AST_SUB || ast_get_node_type(node) == AST_LEFT_SHIFT 
    809   || ast_get_node_type(node) == AST_RIGHT_SHIFT || ast_get_node_type(node) == AST_LESS_THAN || ast_get_node_type(node) == AST_GREATER_THAN 
    810   || ast_get_node_type(node) == AST_LESS_THAN_OR_EQUAL || ast_get_node_type(node) == AST_GREATER_THAN_OR_EQUAL 
    811   || ast_get_node_type(node) == AST_EQUAL || ast_get_node_type(node) == AST_NOT_EQUAL 
    812   || ast_get_node_type(node) == AST_BITWISE_AND || ast_get_node_type(node) == AST_BITWISE_XOR || ast_get_node_type(node) == AST_BITWISE_OR 
    813   || ast_get_node_type(node) == AST_LOGICAL_AND || ast_get_node_type(node) == AST_LOGICAL_OR || ast_get_node_type(node) == AST_ASSIGN 
    814   || ast_get_node_type(node) == AST_ADD_ASSIGN || ast_get_node_type(node) == AST_SUB_ASSIGN || ast_get_node_type(node) == AST_MUL_ASSIGN || ast_get_node_type(node) == AST_DIV_ASSIGN 
    815   || ast_get_node_type(node) == AST_MOD_ASSIGN || ast_get_node_type(node) == AST_LEFT_SHIFT_ASSIGN || ast_get_node_type(node) == AST_RIGHT_SHIFT_ASSIGN || ast_get_node_type(node) == AST_AND_ASSIGN 
    816   || ast_get_node_type(node) == AST_XOR_ASSIGN || ast_get_node_type(node) == AST_OR_ASSIGN || ast_get_node_type(node) == AST_COMMA);
    817   struct ast_binary_node *data = ast_get_data(node);
    818   return data->left;
    819 }
    820 
    821 ast_node_t *ast_get_right_expr(ast_node_t *node)
    822 {
    823   assert(ast_get_node_type(node) == AST_MUL || ast_get_node_type(node) == AST_DIV || ast_get_node_type(node) == AST_MOD
    824   || ast_get_node_type(node) == AST_ADD || ast_get_node_type(node) == AST_SUB || ast_get_node_type(node) == AST_LEFT_SHIFT 
    825   || ast_get_node_type(node) == AST_RIGHT_SHIFT || ast_get_node_type(node) == AST_LESS_THAN || ast_get_node_type(node) == AST_GREATER_THAN 
    826   || ast_get_node_type(node) == AST_LESS_THAN_OR_EQUAL || ast_get_node_type(node) == AST_GREATER_THAN_OR_EQUAL 
    827   || ast_get_node_type(node) == AST_EQUAL || ast_get_node_type(node) == AST_NOT_EQUAL 
    828   || ast_get_node_type(node) == AST_BITWISE_AND || ast_get_node_type(node) == AST_BITWISE_XOR || ast_get_node_type(node) == AST_BITWISE_OR 
    829   || ast_get_node_type(node) == AST_LOGICAL_AND || ast_get_node_type(node) == AST_LOGICAL_OR || ast_get_node_type(node) == AST_ASSIGN 
    830   || ast_get_node_type(node) == AST_ADD_ASSIGN || ast_get_node_type(node) == AST_SUB_ASSIGN || ast_get_node_type(node) == AST_MUL_ASSIGN || ast_get_node_type(node) == AST_DIV_ASSIGN 
    831   || ast_get_node_type(node) == AST_MOD_ASSIGN || ast_get_node_type(node) == AST_LEFT_SHIFT_ASSIGN || ast_get_node_type(node) == AST_RIGHT_SHIFT_ASSIGN || ast_get_node_type(node) == AST_AND_ASSIGN 
    832   || ast_get_node_type(node) == AST_XOR_ASSIGN || ast_get_node_type(node) == AST_OR_ASSIGN || ast_get_node_type(node) == AST_COMMA);
    833   struct ast_binary_node *data = ast_get_data(node);
    834   return data->right;
    835 }
    836 
    837 ---
    838 
    839 @s
    840 
    841 --- Implementation - Nodes (Conditional)
    842 struct ast_conditional_node {
    843   ast_node_t *condition;
    844   ast_node_t *true_expr;
    845   ast_node_t *false_expr;
    846 };
    847 
    848 ast_node_t *ast_create_conditional_node(ast_node_t *condition, ast_node_t *true_expr, ast_node_t *false_expr, int line, int column)
    849 {
    850   ast_node_t *node = ast_create_node(AST_CONDITIONAL, line, column, sizeof(struct ast_conditional_node));
    851   struct ast_conditional_node *data = ast_get_data(node);
    852   data->condition = condition;
    853   data->true_expr = true_expr;
    854   data->false_expr = false_expr;
    855   return node;
    856 }
    857 
    858 ast_node_t *ast_get_conditional_condition(ast_node_t *node)
    859 {
    860   assert(ast_get_node_type(node) == AST_CONDITIONAL);
    861   struct ast_conditional_node *data = ast_get_data(node);
    862   return data->condition;
    863 }
    864 
    865 ast_node_t *ast_get_conditional_true_expr(ast_node_t *node)
    866 {
    867   assert(ast_get_node_type(node) == AST_CONDITIONAL);
    868   struct ast_conditional_node *data = ast_get_data(node);
    869   return data->true_expr;
    870 }
    871 
    872 ast_node_t *ast_get_conditional_false_expr(ast_node_t *node)
    873 {
    874   assert(ast_get_node_type(node) == AST_CONDITIONAL);
    875   struct ast_conditional_node *data = ast_get_data(node);
    876   return data->false_expr;
    877 }
    878 
    879 ---
    880 
    881 @s
    882 
    883 --- Implementation - Nodes (Expression Statement Nodes)
    884 struct ast_expression_statement_node {
    885   ast_node_t *expr;
    886 };
    887 
    888 ast_node_t *ast_create_expression_statement_node(ast_node_t *expr)
    889 {
    890   ast_node_t *node = ast_create_node(AST_EXPRESSION_STATEMENT, expr->line, expr->column, sizeof(struct ast_expression_statement_node));
    891   struct ast_expression_statement_node *data = ast_get_data(node);
    892   data->expr = expr;
    893   return node;
    894 }
    895 
    896 ast_node_t *ast_get_expression_statement_expr(ast_node_t *node)
    897 {
    898   assert(ast_get_node_type(node) == AST_EXPRESSION_STATEMENT);
    899   struct ast_expression_statement_node *data = ast_get_data(node);
    900   return data->expr;
    901 }
    902 
    903 ---
    904 
    905 @s
    906 
    907 --- Implementation - Nodes (Label/Case Statement Nodes)
    908 struct ast_label_statement_node {
    909   char *label;
    910 };
    911 
    912 ast_node_t *ast_create_label_statement_node(char *label, int line, int col)
    913 {
    914   ast_node_t *node = ast_create_node(AST_LABEL_STATEMENT, line, col, sizeof(struct ast_label_statement_node));
    915   struct ast_label_statement_node *data = ast_get_data(node);
    916   data->label = label;
    917   return node;
    918 }
    919 
    920 char *ast_get_label_statement_label(ast_node_t *node)
    921 {
    922   assert(ast_get_node_type(node) == AST_LABEL_STATEMENT);
    923   struct ast_label_statement_node *data = ast_get_data(node);
    924   return data->label;
    925 }
    926 
    927 ast_node_t *ast_create_case_statement_node(ast_node_t *expr, int line, int col)
    928 {
    929   ast_node_t *node = ast_create_node(AST_CASE_STATEMENT, line, col, sizeof(ast_node_t *));
    930   ast_node_t **data = ast_get_data(node);
    931   *data = expr;
    932   return node;
    933 }
    934 
    935 ast_node_t *ast_get_case_statement_expr(ast_node_t *node)
    936 {
    937   assert(ast_get_node_type(node) == AST_CASE_STATEMENT);
    938   ast_node_t **data = ast_get_data(node);
    939   return *data;
    940 }
    941 
    942 ast_node_t *ast_create_default_statement_node(int line, int col)
    943 {
    944   return ast_create_node(AST_DEFAULT_STATEMENT, line, col, 0);
    945 }
    946 ---
    947 
    948 @s
    949 
    950 --- Implementation - Nodes (Compound Statement Nodes)
    951 struct ast_compound_statement_node {
    952   ast_node_t **decls_and_stmts;
    953   size_t num_decls_and_stmts;
    954 };
    955 
    956 ast_node_t *ast_create_compound_statement_node(ast_node_t **decls_and_stmts, size_t num_decls_and_stmts, int line, int col)
    957 {
    958   ast_node_t *node = ast_create_node(AST_COMPOUND_STATEMENT, line, col, sizeof(struct ast_compound_statement_node));
    959   struct ast_compound_statement_node *data = ast_get_data(node);
    960   data->decls_and_stmts = decls_and_stmts;
    961   data->num_decls_and_stmts = num_decls_and_stmts;
    962   return node;
    963 }
    964 
    965 ast_node_t **ast_get_compound_statement_contents(ast_node_t *node, size_t *num_contents)
    966 {
    967   assert(ast_get_node_type(node) == AST_COMPOUND_STATEMENT);
    968   struct ast_compound_statement_node *data = ast_get_data(node);
    969   *num_contents = data->num_decls_and_stmts;
    970   return data->decls_and_stmts;
    971 }
    972 
    973 ---
    974 
    975 @s
    976 
    977 --- Implementation - Nodes (Conditional Statement Nodes)
    978 struct ast_if_statement_node {
    979   ast_node_t *condition;
    980   ast_node_t *true_stmt;
    981   ast_node_t *false_stmt;
    982 };
    983 
    984 ast_node_t *ast_create_if_statement_node(ast_node_t *condition, ast_node_t *true_stmt, ast_node_t *false_stmt, int line, int col)
    985 {
    986   ast_node_t *node = ast_create_node(AST_IF_STATEMENT, line, col, sizeof(struct ast_if_statement_node));
    987   struct ast_if_statement_node *data = ast_get_data(node);
    988   data->condition = condition;
    989   data->true_stmt = true_stmt;
    990   data->false_stmt = false_stmt;
    991   return node;
    992 }
    993 
    994 ast_node_t *ast_get_if_statement_condition(ast_node_t *node)
    995 {
    996   assert(ast_get_node_type(node) == AST_IF_STATEMENT);
    997   struct ast_if_statement_node *data = ast_get_data(node);
    998   return data->condition;
    999 }
   1000 
   1001 ast_node_t *ast_get_if_statement_true_stmt(ast_node_t *node)
   1002 {
   1003   assert(ast_get_node_type(node) == AST_IF_STATEMENT);
   1004   struct ast_if_statement_node *data = ast_get_data(node);
   1005   return data->true_stmt;
   1006 }
   1007 
   1008 ast_node_t *ast_get_if_statement_false_stmt(ast_node_t *node)
   1009 {
   1010   assert(ast_get_node_type(node) == AST_IF_STATEMENT);
   1011   struct ast_if_statement_node *data = ast_get_data(node);
   1012   return data->false_stmt;
   1013 }
   1014 
   1015 ---
   1016 
   1017 @s
   1018 
   1019 --- Implementation - Nodes (Iteration Statement Nodes)
   1020 
   1021 struct ast_iteration_statement_node {
   1022   ast_node_t *condition;
   1023   ast_node_t *stmt;
   1024 };
   1025 
   1026 ast_node_t *ast_create_while_statement_node(ast_node_t *condition, ast_node_t *stmt, int line, int col)
   1027 {
   1028   ast_node_t *node = ast_create_node(AST_WHILE_STATEMENT, line, col, sizeof(struct ast_iteration_statement_node));
   1029   struct ast_iteration_statement_node *data = ast_get_data(node);
   1030   data->condition = condition;
   1031   data->stmt = stmt;
   1032   return node;
   1033 }
   1034 
   1035 ast_node_t *ast_create_do_while_statement_node(ast_node_t *condition, ast_node_t *stmt, int line, int col)
   1036 {
   1037   ast_node_t *node = ast_create_node(AST_DO_WHILE_STATEMENT, line, col, sizeof(struct ast_iteration_statement_node));
   1038   struct ast_iteration_statement_node *data = ast_get_data(node);
   1039   data->condition = condition;
   1040   data->stmt = stmt;
   1041   return node;
   1042 }
   1043 
   1044 // Shares the first two members with ast_iteration_statement_node
   1045 struct ast_for_statement_node {  
   1046   ast_node_t *condition;  
   1047   ast_node_t *stmt;
   1048   ast_node_t *init;
   1049   ast_node_t *update;
   1050 
   1051 };
   1052 
   1053 ast_node_t *ast_create_for_statement_node(ast_node_t *init, ast_node_t *condition, ast_node_t *update, ast_node_t *stmt, int line, int col)
   1054 {
   1055   ast_node_t *node = ast_create_node(AST_FOR_STATEMENT, line, col, sizeof(struct ast_for_statement_node));
   1056   struct ast_for_statement_node *data = ast_get_data(node);
   1057   data->init = init;
   1058   data->condition = condition;
   1059   data->update = update;
   1060   data->stmt = stmt;
   1061   return node;
   1062 }
   1063 
   1064 ast_node_t *ast_get_iteration_statement_condition(ast_node_t *node)
   1065 {
   1066   assert(ast_get_node_type(node) == AST_WHILE_STATEMENT || ast_get_node_type(node) == AST_DO_WHILE_STATEMENT || ast_get_node_type(node) == AST_FOR_STATEMENT);
   1067   struct ast_iteration_statement_node *data = ast_get_data(node);
   1068   return data->condition;
   1069 }
   1070 
   1071 ast_node_t *ast_get_iteration_statement_stmt(ast_node_t *node)
   1072 {
   1073   assert(ast_get_node_type(node) == AST_WHILE_STATEMENT || ast_get_node_type(node) == AST_DO_WHILE_STATEMENT || ast_get_node_type(node) == AST_FOR_STATEMENT);
   1074   struct ast_iteration_statement_node *data = ast_get_data(node);
   1075   return data->stmt;
   1076 }
   1077 
   1078 ast_node_t *ast_get_for_statement_init(ast_node_t *node)
   1079 {
   1080   assert(ast_get_node_type(node) == AST_FOR_STATEMENT);
   1081   struct ast_for_statement_node *data = ast_get_data(node);
   1082   return data->init;
   1083 }
   1084 
   1085 ast_node_t *ast_get_for_statement_update(ast_node_t *node)
   1086 {
   1087   assert(ast_get_node_type(node) == AST_FOR_STATEMENT);
   1088   struct ast_for_statement_node *data = ast_get_data(node);
   1089   return data->update;
   1090 }
   1091 ---
   1092 
   1093 @s
   1094 
   1095 --- Implementation - Nodes (Jump Statement Nodes)
   1096 struct ast_goto_statement_node {
   1097   char *label;
   1098 };
   1099 
   1100 ast_node_t *ast_create_goto_statement_node(char *label, int line, int col)
   1101 {
   1102   ast_node_t *node = ast_create_node(AST_GOTO_STATEMENT, line, col, sizeof(struct ast_goto_statement_node));
   1103   struct ast_goto_statement_node *data = ast_get_data(node);
   1104   data->label = label;
   1105   return node;
   1106 }
   1107 
   1108 char *ast_get_goto_statement_label(ast_node_t *node)
   1109 {
   1110   assert(ast_get_node_type(node) == AST_GOTO_STATEMENT);
   1111   struct ast_goto_statement_node *data = ast_get_data(node);
   1112   return data->label;
   1113 }
   1114 
   1115 ast_node_t *ast_create_continue_statement_node(int line, int col)
   1116 {
   1117   return ast_create_node(AST_CONTINUE_STATEMENT, line, col, 0);
   1118 }
   1119 
   1120 ast_node_t *ast_create_break_statement_node(int line, int col)
   1121 {
   1122   return ast_create_node(AST_BREAK_STATEMENT, line, col, 0);
   1123 }
   1124 
   1125 ast_node_t *ast_create_return_statement_node(ast_node_t *expr, int line, int col)
   1126 {
   1127   ast_node_t *node = ast_create_node(AST_RETURN_STATEMENT, line, col, sizeof(ast_node_t *));
   1128   ast_node_t **data = ast_get_data(node);
   1129   *data = expr;
   1130   return node;
   1131 }
   1132 
   1133 ast_node_t *ast_get_return_statement_expr(ast_node_t *node)
   1134 {
   1135   assert(ast_get_node_type(node) == AST_RETURN_STATEMENT);
   1136   ast_node_t **data = ast_get_data(node);
   1137   return *data;
   1138 }
   1139 
   1140 ---
   1141 
   1142 @s
   1143 
   1144 --- Implementation - Nodes (Switch Statement Nodes)
   1145 
   1146 struct ast_switch_statement_node {
   1147   ast_node_t *expr;
   1148   ast_node_t *stmt;
   1149 };
   1150 
   1151 ast_node_t *ast_create_switch_statement_node(ast_node_t *expr, ast_node_t *stmt, int line, int col)
   1152 {
   1153   ast_node_t *node = ast_create_node(AST_SWITCH_STATEMENT, line, col, sizeof(struct ast_switch_statement_node));
   1154   struct ast_switch_statement_node *data = ast_get_data(node);
   1155   data->expr = expr;
   1156   data->stmt = stmt;
   1157   return node;
   1158 }
   1159 
   1160 ast_node_t *ast_get_switch_statement_expr(ast_node_t *node)
   1161 {
   1162   assert(ast_get_node_type(node) == AST_SWITCH_STATEMENT);
   1163   struct ast_switch_statement_node *data = ast_get_data(node);
   1164   return data->expr;
   1165 }
   1166 
   1167 ast_node_t *ast_get_switch_statement_stmt(ast_node_t *node)
   1168 {
   1169   assert(ast_get_node_type(node) == AST_SWITCH_STATEMENT);
   1170   struct ast_switch_statement_node *data = ast_get_data(node);
   1171   return data->stmt;
   1172 }
   1173 ---
   1174 
   1175 --- Implementation - Nodes (Simple Declaration Nodes)
   1176 struct var_decl_data {
   1177   ast_node_t *type;
   1178   char *id;
   1179 };
   1180 
   1181 ast_node_t *ast_create_var_decl_node(ast_node_t *type, char *id, int line, int col)
   1182 {
   1183   ast_node_t *node = ast_create_node(AST_VAR_DECL, line, col, sizeof(struct var_decl_data));
   1184     struct var_decl_data *data = ast_get_data(node);
   1185   data->type = type;
   1186   data->id = id;
   1187   return (ast_node_t *)data;
   1188 }
   1189 
   1190 ast_node_t *ast_get_var_decl_type(ast_node_t *node)
   1191 {
   1192   struct var_decl_data *data = ast_get_data(node);
   1193   return data->type;
   1194 }
   1195 
   1196 char *ast_get_var_decl_id(ast_node_t *node)
   1197 {
   1198   struct var_decl_data *data = ast_get_data(node);
   1199   return data->id;
   1200 }
   1201 
   1202 struct fun_decl_data {
   1203   ast_node_t *type;
   1204   char *id;
   1205     ast_node_t **params;
   1206   size_t num_params;
   1207 };
   1208 
   1209 ast_node_t *ast_create_fun_decl_node(ast_node_t *type, char *id, ast_node_t **params, size_t num_params, int line, int col)
   1210 {
   1211   ast_node_t *node = ast_create_node(AST_FUN_DECL, line, col, sizeof(struct fun_decl_data));
   1212     struct fun_decl_data *data = ast_get_data(node);
   1213   data->type = type;
   1214   data->id = id;
   1215   data->params = params;
   1216   data->num_params = num_params;
   1217   return (ast_node_t *)data;
   1218 }
   1219 
   1220 ast_node_t *ast_get_fun_decl_type(ast_node_t *node)
   1221 {
   1222   struct fun_decl_data *data = ast_get_data(node);
   1223   return data->type;
   1224 }
   1225 
   1226 char *ast_get_fun_decl_id(ast_node_t *node)
   1227 {
   1228   struct fun_decl_data *data = ast_get_data(node);
   1229   return data->id;
   1230 }
   1231 
   1232 ast_node_t **ast_get_fun_decl_params(ast_node_t *node, size_t *num_params)
   1233 {
   1234   struct fun_decl_data *data = ast_get_data(node);
   1235   *num_params = data->num_params;
   1236   return data->params;
   1237 }
   1238 
   1239 ---
   1240 
   1241 @s
   1242 
   1243 --- Implementation - Nodes (Parameter Declaration Nodes)
   1244 struct param_data {
   1245   ast_node_t *type;
   1246   char *id;
   1247 };
   1248 
   1249 ast_node_t *ast_create_param_node(ast_node_t *type, char *id, int line, int col)
   1250 {
   1251   ast_node_t *node = ast_create_node(AST_PARAM, line, col, sizeof(struct param_data));
   1252     struct param_data *data = ast_get_data(node);
   1253   data->type = type;
   1254   data->id = id;
   1255   return node;
   1256 }
   1257 
   1258 ast_node_t *ast_get_param_type(ast_node_t *node)
   1259 {
   1260   struct param_data *data = ast_get_data(node);
   1261   return data->type;
   1262 }
   1263 
   1264 char *ast_get_param_id(ast_node_t *node)
   1265 {
   1266   struct param_data *data = ast_get_data(node);
   1267   return data->id;
   1268 }
   1269 
   1270 ---
   1271 
   1272 @s
   1273 
   1274 --- Implementation - Nodes (Function Definition Nodes)
   1275 
   1276 struct fun_def_data {
   1277   ast_node_t *type;
   1278   char *id;
   1279   ast_node_t **params;
   1280   size_t num_params;
   1281   ast_node_t *stmt;
   1282 };
   1283 
   1284 ast_node_t *ast_create_fun_def_node(ast_node_t *type, char *id, ast_node_t **params, size_t num_params, ast_node_t *stmt, int line, int col)
   1285 {
   1286   ast_node_t *node = ast_create_node(AST_FUN_DEF, line, col, sizeof(struct fun_def_data));
   1287     struct fun_def_data *data = ast_get_data(node);
   1288   data->type = type;
   1289   data->id = id;
   1290   data->params = params;
   1291   data->num_params = num_params;
   1292   data->stmt = stmt;
   1293   return node;
   1294 }
   1295 
   1296 ast_node_t *ast_get_fun_def_type(ast_node_t *node)
   1297 {
   1298   struct fun_def_data *data = ast_get_data(node);
   1299   return data->type;
   1300 }
   1301 
   1302 char *ast_get_fun_def_id(ast_node_t *node)
   1303 {
   1304   struct fun_def_data *data = ast_get_data(node);
   1305   return data->id;
   1306 }
   1307 
   1308 ast_node_t **ast_get_fun_def_params(ast_node_t *node, size_t *num_params)
   1309 {
   1310   struct fun_def_data *data = ast_get_data(node);
   1311   *num_params = data->num_params;
   1312   return data->params;
   1313 }
   1314 
   1315 ast_node_t *ast_get_fun_def_stmt(ast_node_t *node)
   1316 {
   1317   struct fun_def_data *data = ast_get_data(node);
   1318   return data->stmt;
   1319 }
   1320 
   1321 ---
   1322 
   1323 @s
   1324 
   1325 --- Implementation - Nodes (Struct Declaration Nodes)
   1326 struct struct_decl_data {
   1327   char *id;
   1328   ast_node_t **members;
   1329   size_t num_members;
   1330 };
   1331 
   1332 ast_node_t *ast_create_struct_decl_node(char *id, ast_node_t **members, size_t num_members, int line, int col)
   1333 {
   1334   ast_node_t *node = ast_create_node(AST_STRUCT_DECL, line, col, sizeof(struct struct_decl_data));
   1335   struct struct_decl_data *data = ast_get_data(node);   
   1336   data->id = id;
   1337   data->members = members;
   1338   data->num_members = num_members;
   1339   return node;
   1340 }
   1341 
   1342 char *ast_get_struct_decl_id(ast_node_t *node)
   1343 {
   1344   struct struct_decl_data *data = ast_get_data(node);
   1345   return data->id;
   1346 }
   1347 
   1348 ast_node_t **ast_get_struct_decl_members(ast_node_t *node, size_t *num_members)
   1349 {
   1350   struct struct_decl_data *data = ast_get_data(node);
   1351   *num_members = data->num_members;
   1352   return data->members;
   1353 }
   1354 ---
   1355 
   1356 @s
   1357 
   1358 --- Implementation - Nodes (Union Declaration Nodes)
   1359 struct union_decl_data {
   1360   char *id;
   1361   ast_node_t **members;
   1362   size_t num_members;
   1363 };
   1364 
   1365 ast_node_t *ast_create_union_decl_node(char *id, ast_node_t **members, size_t num_members, int line, int col)
   1366 {
   1367   ast_node_t *node = ast_create_node(AST_UNION_DECL, line, col, sizeof(struct union_decl_data));
   1368     struct union_decl_data *data = ast_get_data(node);
   1369   data->id = id;
   1370   data->members = members;
   1371   data->num_members = num_members;
   1372   return node;
   1373 }
   1374 
   1375 char *ast_get_union_decl_id(ast_node_t *node)
   1376 {
   1377   struct union_decl_data *data = ast_get_data(node);
   1378   return data->id;
   1379 }
   1380 
   1381 ast_node_t **ast_get_union_decl_members(ast_node_t *node, size_t *num_members)
   1382 {
   1383   struct union_decl_data *data = ast_get_data(node);
   1384   *num_members = data->num_members;
   1385   return data->members;
   1386 }
   1387 
   1388 ---
   1389 
   1390 @s
   1391 
   1392 --- Implementation - Nodes (Enum Declaration Nodes)
   1393 struct enum_decl_data {
   1394   char *id;
   1395   ast_node_t **enums;
   1396   size_t num_enums;
   1397 };
   1398 
   1399 ast_node_t *ast_create_enum_decl_node(char *id, ast_node_t **enums, size_t num_enums, int line, int col)
   1400 {
   1401   ast_node_t *node = ast_create_node(AST_ENUM_DECL, line, col, sizeof(struct enum_decl_data));
   1402     struct enum_decl_data *data = ast_get_data(node);
   1403   data->id = id;
   1404   data->enums = enums;
   1405   data->num_enums = num_enums;
   1406   return node;
   1407 }
   1408 
   1409 char *ast_get_enum_decl_id(ast_node_t *node)
   1410 {
   1411   struct enum_decl_data *data = ast_get_data(node);
   1412   return data->id;
   1413 }
   1414 
   1415 ast_node_t **ast_get_enum_decl_enums(ast_node_t *node, size_t *num_enums)
   1416 {
   1417   struct enum_decl_data *data = ast_get_data(node);
   1418   *num_enums = data->num_enums;
   1419   return data->enums;
   1420 }
   1421 
   1422 ---
   1423 
   1424 @s
   1425 
   1426 --- Implementation - Nodes (Typedef Declaration Nodes)
   1427 struct typedef_data {
   1428   ast_node_t *type;
   1429   char *id;
   1430 };
   1431 
   1432 ast_node_t *ast_create_typedef_node(ast_node_t *type, char *id, int line, int col)
   1433 {
   1434   ast_node_t *node = ast_create_node(AST_TYPEDEF, line, col, sizeof(struct typedef_data));
   1435     struct typedef_data *data = ast_get_data(node);
   1436   data->type = type;
   1437   data->id = id;
   1438   return node;
   1439 }
   1440 
   1441 ast_node_t *ast_get_typedef_type(ast_node_t *node)
   1442 {
   1443   struct typedef_data *data = ast_get_data(node);
   1444   return data->type;
   1445 }
   1446 
   1447 char *ast_get_typedef_id(ast_node_t *node)
   1448 {
   1449   struct typedef_data *data = ast_get_data(node);
   1450   return data->id;
   1451 }
   1452 
   1453 ---
   1454 
   1455 @s
   1456 
   1457 --- Implementation - Nodes (Types/Members)
   1458 struct type_data {
   1459   token_t **type;
   1460   size_t num_type;
   1461 };
   1462 
   1463 ast_node_t *ast_create_type_node(token_t *type[], size_t num_type, int line, int col)
   1464 {
   1465   ast_node_t *node = ast_create_node(AST_TYPE, line, col, sizeof(struct type_data));
   1466     struct type_data *data = ast_get_data(node);
   1467   data->type = type;
   1468   data->num_type = num_type;
   1469   return node;
   1470 }
   1471 
   1472 token_t **ast_get_type(ast_node_t *node, size_t *num_type)
   1473 {
   1474   struct type_data *data = ast_get_data(node);
   1475   *num_type = data->num_type;
   1476   return data->type;
   1477 }
   1478 
   1479 struct member_data {
   1480   ast_node_t *type;
   1481   char *id;
   1482 };
   1483 
   1484 ast_node_t *ast_create_member_node(ast_node_t *type, char *id, int line, int col)
   1485 {
   1486   ast_node_t *node = ast_create_node(AST_MEMBER, line, col, sizeof(struct member_data));
   1487     struct member_data *data = ast_get_data(node);
   1488   data->type = type;
   1489   data->id = id;
   1490   return node;
   1491 }
   1492 
   1493 ast_node_t *ast_get_member_type(ast_node_t *node)
   1494 {
   1495   struct member_data *data = ast_get_data(node);
   1496   return data->type;
   1497 }
   1498 
   1499 char *ast_get_member_id(ast_node_t *node)
   1500 {
   1501   struct member_data *data = ast_get_data(node);
   1502   return data->id;
   1503 }
   1504 
   1505 struct enum_member_data {
   1506   char *id;
   1507   ast_node_t *value;
   1508 };
   1509 
   1510 ast_node_t *ast_create_enum_member_node(char *id, ast_node_t *value, int line, int col)
   1511 {
   1512   ast_node_t *node = ast_create_node(AST_ENUM_MEMBER, line, col, sizeof(struct enum_member_data));
   1513     struct enum_member_data *data = ast_get_data(node);
   1514   data->id = id;
   1515   data->value = value;
   1516   return node;
   1517 }
   1518 
   1519 char *ast_get_enum_member_id(ast_node_t *node)
   1520 {
   1521   struct enum_member_data *data = ast_get_data(node);
   1522   return data->id;
   1523 }
   1524 
   1525 ast_node_t *ast_get_enum_member_expr(ast_node_t *node)
   1526 {
   1527   struct enum_member_data *data = ast_get_data(node);
   1528   return data->value;
   1529 }
   1530 
   1531 ---
   1532 
   1533 @s
   1534 
   1535 --- Implementation - Nodes (Translation Unit Nodes)
   1536 struct translation_unit_data {
   1537   ast_node_t **items;
   1538   size_t num_items;
   1539 };
   1540 
   1541 ast_node_t *ast_create_translation_unit_node(ast_node_t **items, size_t num_items, int line, int col)
   1542 {
   1543   ast_node_t *node = ast_create_node(AST_TRANSLATION_UNIT, line, col, sizeof(struct translation_unit_data));
   1544     struct translation_unit_data *data = ast_get_data(node);
   1545   data->items = items;
   1546   data->num_items = num_items;
   1547   return node;
   1548 }
   1549 
   1550 ast_node_t **ast_get_translation_unit_items(ast_node_t *node, size_t *num_items)
   1551 {
   1552   struct translation_unit_data *data = ast_get_data(node);
   1553   *num_items = data->num_items;
   1554   return data->items;
   1555 }
   1556 ---
   1557 
   1558 @s
   1559 Freeing the AST is going to be pretty painful. We need to free all the nodes, and all the data associated with them. We'll start by freeing the data associated with each node, and then free the node itself. We'll do this recursively, so that we can free all the nodes in the tree.
   1560 --- Implementation - Nodes (Freeing)
   1561 void ast_free_node(ast_node_t *node) {
   1562   if (!node) {
   1563     return;
   1564   }
   1565   switch (ast_get_node_type(node)) {
   1566   case AST_INTU32:
   1567   case AST_INTU64:
   1568   case AST_INTS32:
   1569   case AST_INTS64:
   1570   case AST_FLOAT:
   1571   case AST_DOUBLE:
   1572   case AST_STRING:
   1573   case AST_CHAR:
   1574   case AST_VAR:
   1575     break;
   1576   case AST_SUBSCRIPT:
   1577     ast_free_node(ast_get_subscript_expr(node));
   1578     ast_free_node(ast_get_subscript_idx(node));
   1579     free(node);
   1580     break;
   1581   case AST_DIRECT_COMPONENT_SELECTION:
   1582   case AST_INDIRECT_COMPONENT_SELECTION:
   1583     ast_free_node(ast_get_component_expr(node));
   1584     free(node);
   1585     break;
   1586   case AST_FUNCTION_CALL: {
   1587     size_t num_args;
   1588     ast_node_t **args = ast_get_function_call_args(node, &num_args);
   1589     for (size_t i = 0; i < num_args; i++) {
   1590       ast_free_node(args[i]);
   1591     }
   1592     free(args);
   1593     ast_free_node(ast_get_function_call_expr(node));
   1594     free(node);
   1595     break;
   1596   }
   1597   case AST_POSTFIX_INC:
   1598   case AST_POSTFIX_DEC: {
   1599     ast_free_node(ast_get_postfix_expr(node));
   1600     free(node);
   1601     break;
   1602   }
   1603   case AST_UNARY_PLUS:
   1604   case AST_UNARY_MINUS:
   1605   case AST_LOGICAL_NOT:
   1606   case AST_BITWISE_NOT:
   1607   case AST_ADDRESS_OF:
   1608   case AST_INDIRECTION:
   1609   case AST_SIZEOF:
   1610   case AST_PRE_INC:
   1611   case AST_PRE_DEC: {
   1612     ast_free_node(ast_get_unary_expr(node));
   1613     free(node);
   1614     break;
   1615   }
   1616   case AST_CAST: {
   1617     ast_free_node(ast_get_cast_type(node));
   1618     ast_free_node(ast_get_cast_expr(node));
   1619     free(node);
   1620     break;
   1621   }
   1622   case AST_MUL:
   1623   case AST_DIV:
   1624   case AST_MOD:
   1625   case AST_ADD:
   1626   case AST_SUB:
   1627   case AST_LEFT_SHIFT:
   1628   case AST_RIGHT_SHIFT:
   1629   case AST_LESS_THAN:
   1630   case AST_GREATER_THAN:
   1631   case AST_LESS_THAN_OR_EQUAL:
   1632   case AST_GREATER_THAN_OR_EQUAL:
   1633   case AST_EQUAL:
   1634   case AST_NOT_EQUAL:
   1635   case AST_BITWISE_AND:
   1636   case AST_BITWISE_XOR:
   1637   case AST_BITWISE_OR:
   1638   case AST_LOGICAL_AND:
   1639   case AST_LOGICAL_OR:
   1640   case AST_COMMA:
   1641   case AST_ASSIGN:
   1642   case AST_ADD_ASSIGN:
   1643   case AST_SUB_ASSIGN:
   1644   case AST_MUL_ASSIGN:
   1645   case AST_DIV_ASSIGN:
   1646   case AST_MOD_ASSIGN:
   1647   case AST_LEFT_SHIFT_ASSIGN:
   1648   case AST_RIGHT_SHIFT_ASSIGN:
   1649   case AST_AND_ASSIGN:
   1650   case AST_XOR_ASSIGN:
   1651   case AST_OR_ASSIGN:
   1652  {
   1653     ast_free_node(ast_get_left_expr(node));
   1654     ast_free_node(ast_get_right_expr(node));
   1655     free(node);
   1656     break;
   1657   }
   1658   case AST_CONDITIONAL: {
   1659     ast_free_node(ast_get_conditional_condition(node));
   1660     ast_free_node(ast_get_conditional_true_expr(node));
   1661     ast_free_node(ast_get_conditional_false_expr(node));
   1662     free(node);
   1663     break;
   1664   }
   1665   case AST_EXPRESSION_STATEMENT:
   1666     ast_free_node(ast_get_expression_statement_expr(node));
   1667     free(node);
   1668     break;
   1669   case AST_LABEL_STATEMENT:
   1670     free(node);
   1671     break;
   1672   case AST_CASE_STATEMENT:
   1673     ast_free_node(ast_get_case_statement_expr(node));
   1674     free(node);
   1675     break;
   1676   case AST_DEFAULT_STATEMENT:
   1677     free(node);
   1678     break;
   1679   case AST_COMPOUND_STATEMENT:
   1680     {
   1681       size_t size = 0;
   1682       ast_node_t **comp_stmts = ast_get_compound_statement_contents(node, &size);
   1683       for(size_t i = 0; i < size; i++)
   1684       {
   1685         ast_free_node(comp_stmts[i]);
   1686       }
   1687       free(comp_stmts);
   1688       free(node);
   1689       break;
   1690     }
   1691   case AST_IF_STATEMENT:
   1692   {
   1693     ast_free_node(ast_get_if_statement_condition(node));
   1694     ast_free_node(ast_get_if_statement_true_stmt(node));
   1695     ast_free_node(ast_get_if_statement_false_stmt(node));
   1696     free(node);
   1697     break;
   1698   }
   1699   case AST_WHILE_STATEMENT:
   1700   case AST_DO_WHILE_STATEMENT:
   1701   {
   1702     ast_free_node(ast_get_iteration_statement_condition(node));
   1703     ast_free_node(ast_get_iteration_statement_stmt(node));
   1704     free(node);
   1705     break;
   1706   }
   1707   case AST_FOR_STATEMENT:
   1708   {
   1709     ast_free_node(ast_get_for_statement_init(node));
   1710     ast_free_node(ast_get_iteration_statement_condition(node));
   1711     ast_free_node(ast_get_for_statement_update(node));
   1712     ast_free_node(ast_get_iteration_statement_stmt(node));
   1713     free(node);
   1714     break;
   1715   }
   1716   case AST_GOTO_STATEMENT:
   1717     free(node);
   1718     break;
   1719   case AST_CONTINUE_STATEMENT:
   1720   case AST_BREAK_STATEMENT:
   1721     free(node);
   1722     break;
   1723   case AST_RETURN_STATEMENT:
   1724     ast_free_node(ast_get_return_statement_expr(node));
   1725     free(node);
   1726     break;
   1727   case AST_SWITCH_STATEMENT:
   1728   {
   1729     ast_free_node(ast_get_switch_statement_expr(node));
   1730     ast_free_node(ast_get_switch_statement_stmt(node));
   1731     free(node);
   1732     break;
   1733   }
   1734   case AST_NULL_STATEMENT:
   1735     free(node);
   1736     break;
   1737   case AST_VAR_DECL:
   1738   {
   1739     ast_node_t *type = ast_get_var_decl_type(node);
   1740     ast_free_node(type);
   1741     free(node);
   1742     break;
   1743   }
   1744   case AST_FUN_DECL:
   1745   {
   1746     ast_node_t *type = ast_get_fun_decl_type(node);
   1747     ast_free_node(type);
   1748     size_t num_params;
   1749     ast_node_t **params = ast_get_fun_decl_params(node, &num_params);
   1750     for(size_t i = 0; i < num_params; i++)
   1751     {
   1752       ast_free_node(params[i]);
   1753     }
   1754     free(params);
   1755     free(node);
   1756     break;
   1757   }
   1758   case AST_PARAM:
   1759   {
   1760     ast_node_t *type = ast_get_param_type(node);
   1761     ast_free_node(type);
   1762     free(node);
   1763     break;
   1764   }
   1765   case AST_FUN_DEF:
   1766   {
   1767     ast_node_t *type = ast_get_fun_def_type(node);
   1768     ast_free_node(type);
   1769     size_t num_params;
   1770     ast_node_t **params = ast_get_fun_def_params(node, &num_params);
   1771     for(size_t i = 0; i < num_params; i++)
   1772     {
   1773       ast_free_node(params[i]);
   1774     }
   1775     free(params);
   1776     ast_free_node(ast_get_fun_def_stmt(node));
   1777     free(node);
   1778     break;
   1779   }
   1780   case AST_STRUCT_DECL:
   1781   {
   1782     size_t num_members;
   1783     ast_node_t **members = ast_get_struct_decl_members(node, &num_members);
   1784     for(size_t i = 0; i < num_members; i++)
   1785     {
   1786       ast_free_node(members[i]);
   1787     }
   1788     free(members);
   1789     free(node);
   1790     break;
   1791   }
   1792   case AST_UNION_DECL:
   1793   {
   1794     size_t num_members;
   1795     ast_node_t **members = ast_get_union_decl_members(node, &num_members);
   1796     for(size_t i = 0; i < num_members; i++)
   1797     {
   1798       ast_free_node(members[i]);
   1799     }
   1800     free(members);
   1801     free(node);
   1802     break;
   1803   }
   1804   case AST_ENUM_DECL:
   1805   {
   1806     size_t num_enums;
   1807     ast_node_t **enums = ast_get_enum_decl_enums(node, &num_enums);
   1808     for(size_t i = 0; i < num_enums; i++)
   1809     {
   1810       ast_free_node(enums[i]);
   1811     }
   1812     free(enums);
   1813     free(node);
   1814     break;
   1815   }
   1816   case AST_TYPEDEF:
   1817   {
   1818     ast_node_t *type = ast_get_typedef_type(node);
   1819     ast_free_node(type);
   1820     free(node);
   1821     break;
   1822   }
   1823   case AST_TYPE:
   1824   {
   1825     token_t **type;
   1826     size_t num_type;
   1827     type = ast_get_type(node, &num_type);
   1828     for(size_t i = 0; i < num_type; i++)
   1829     {
   1830       free(type[i]);
   1831     }
   1832     free(type);
   1833     free(node);
   1834     break;
   1835   }
   1836   case AST_MEMBER:
   1837   {
   1838     ast_node_t *type = ast_get_member_type(node);
   1839     ast_free_node(type);
   1840     free(node);
   1841     break;
   1842   }
   1843   case AST_ENUM_MEMBER:
   1844   {
   1845     ast_node_t *value = ast_get_enum_member_expr(node);
   1846     ast_free_node(value);
   1847     free(node);
   1848     break;
   1849   }
   1850   case AST_TRANSLATION_UNIT:
   1851   {
   1852     size_t num_items;
   1853     ast_node_t **items = ast_get_translation_unit_items(node, &num_items);
   1854     for(size_t i = 0; i < num_items; i++)
   1855     {
   1856       ast_free_node(items[i]);
   1857     }
   1858     free(items);
   1859     free(node);
   1860     break;
   1861   }
   1862   default:
   1863     assert(0);
   1864   }
   1865 
   1866 }
   1867 ---
   1868 
   1869 @s
   1870 --- ast.c
   1871 #include <stdlib.h>
   1872 #include <assert.h>
   1873 #include "ast.h"
   1874 @{Implementation - Common Node Structure}
   1875 @{Implementation - Nodes (Primary)}
   1876 @{Implementation - Nodes (Postfix)}
   1877 @{Implementation - Nodes (Unary/Cast)}
   1878 @{Implementation - Nodes (Binary/Assign/Comma)}
   1879 @{Implementation - Nodes (Conditional)}
   1880 @{Implementation - Nodes (Expression Statement Nodes)}
   1881 @{Implementation - Nodes (Label/Case Statement Nodes)}
   1882 @{Implementation - Nodes (Compound Statement Nodes)}
   1883 @{Implementation - Nodes (Conditional Statement Nodes)}
   1884 @{Implementation - Nodes (Iteration Statement Nodes)}
   1885 @{Implementation - Nodes (Jump Statement Nodes)}
   1886 @{Implementation - Nodes (Switch Statement Nodes)}
   1887 @{Implementation - Nodes (Simple Declaration Nodes)}
   1888 @{Implementation - Nodes (Parameter Declaration Nodes)}
   1889 @{Implementation - Nodes (Function Definition Nodes)}
   1890 @{Implementation - Nodes (Struct Declaration Nodes)}
   1891 @{Implementation - Nodes (Union Declaration Nodes)}
   1892 @{Implementation - Nodes (Enum Declaration Nodes)}
   1893 @{Implementation - Nodes (Typedef Declaration Nodes)}
   1894 @{Implementation - Nodes (Types/Members)}
   1895 @{Implementation - Nodes (Translation Unit Nodes)}
   1896 @{Implementation - Nodes (Freeing)}
   1897 ---