src/parse.c
// Create a node to assign an initial value to a given local variable.
Create a node to assign an initial value to a given local variable.
Create a node to assign an initial value to a given local variable .
Node *lvar_init(Type *type, Node *node, InitVal *iv, Token *tok) {
    Node *initializer = new_node(ND_BLOCK, tok);
    initializer->stmts = vec_create();
    if (type->kind == TY_ARY) {
        for (int i = 0; i < iv->vals->len; i++) {
            Node *node_i = new_node_binop(ND_ADD, node, new_node_num(i, NULL), NULL);
            node_i = new_node_uniop(ND_DEREF, node_i, NULL);
            vec_push(initializer->stmts, lvar_init(type->base, node_i, vec_at(iv->vals, i), tok));
        }
        if (type->array_size == -1) {
            type->array_size = iv->vals->len;
            type->size = type->base->size * type->array_size;
        }
        for (int i = iv->vals->len; i < type->array_size; i++) {
            Node *node_i = new_node_binop(ND_ADD, node, new_node_num(i, NULL), NULL);
            node_i = new_node_uniop(ND_DEREF, node_i, NULL);
            InitVal *iv = calloc(1, sizeof(InitVal));
            iv->val = new_node_num(0, NULL);
            vec_push(initializer->stmts, lvar_init(type->base, node_i, iv, tok));
        }
    } else {
        Node *assign = new_node_binop(ND_ASSIGN, node, iv->val, NULL);
        vec_push(initializer->stmts, new_node_uniop(ND_EXPR_STMT, assign, NULL));
    }
    return initializer;
}
Node * lvar_init ( Type * type , Node * node , InitVal * iv , Token * tok ) { Node * initializer = new_node ( ND_BLOCK , tok ) ; initializer -> stmts = vec_create ( ) ; if ( type -> kind == TY_ARY ) { for ( int i = 0 ; i < iv -> vals -> len ; i ++ ) { Node * node_i = new_node_binop ( ND_ADD , node , new_node_num ( i , NULL ) , NULL ) ; node_i = new_node_uniop ( ND_DEREF , node_i , NULL ) ; vec_push ( initializer -> stmts , lvar_init ( type -> base , node_i , vec_at ( iv -> vals , i ) , tok ) ) ; } if ( type -> array_size == -1 ) { type -> array_size = iv -> vals -> len ; type -> size = type -> base -> size * type -> array_size ; } for ( int i = iv -> vals -> len ; i < type -> array_size ; i ++ ) { Node * node_i = new_node_binop ( ND_ADD , node , new_node_num ( i , NULL ) , NULL ) ; node_i = new_node_uniop ( ND_DEREF , node_i , NULL ) ; InitVal * iv = calloc ( 1 , sizeof ( InitVal ) ) ; iv -> val = new_node_num ( 0 , NULL ) ; vec_push ( initializer -> stmts , lvar_init ( type -> base , node_i , iv , tok ) ) ; } } else { Node * assign = new_node_binop ( ND_ASSIGN , node , iv -> val , NULL ) ; vec_push ( initializer -> stmts , new_node_uniop ( ND_EXPR_STMT , assign , NULL ) ) ; } return initializer ; }
Create a node to assign an initial value to a given local variable.
Create a node to assign an initial value to a given local variable .