// File: /workspace/github_c_makefile_star2_O2/sebbekarlsson[P]cola/src/interpret.c
// Function: interpret_visit_component
ast_node* interpret_visit_component(ast_node_component* node) {
    save_component((scope*)ast_node_get_scope((ast_node*)node), node);
    
    if (node->deps->size == 0) {
        free(node->deps);
    } else {
        for (int i = 0; i < node->deps->size; i++) {
            ast_node_component* component = get_component(
                (scope*)ast_node_get_scope((ast_node*)node),
                ((token*)node->deps->items[i])->value
            );

            if (!component) {
                char msg[32];
                sprintf(msg, "could not find definition for: %s", ((token*)node->deps->items[i])->value);
                error_in_interpreter(msg);
            }

            interpret_visit((ast_node*)component->body);
            scope* tmp_scope = (scope*)ast_node_get_scope((ast_node*) component->body);

            for (int ii = 0; ii < tmp_scope->functions->size; ii++) {
                save_function_definition(
                    (scope*)ast_node_get_scope((ast_node*)node->body),
                    (ast_node_function_definition*)tmp_scope->functions->items[ii]
                );   
            }

            // look for yields function
            ast_node_function_definition* yields_definition = get_function_definition(
                tmp_scope,
                "yields"
            );

            if (yields_definition) {
                // lets create a variable definition in the currently visited
                // component and assign whatever the yields method returns
                // to it.
                ast_node_variable_definition* var_definition = init_ast_node_variable_definition(
                    (token*)node->deps->items[i],
                    yields_definition->data_type,
                    interpret_visit((ast_node*)yields_definition->body)
                );
                ast_node_set_scope(
                    (ast_node*) var_definition,
                    (struct scope*)tmp_scope // should use same scope as the-
                    // - component it came from
                );

                // save whatever the yields method returns as a variable
                // in the currently visited component.
                save_variable_definition(
                    (scope*)ast_node_get_scope((ast_node*)node->body),
                    var_definition 
                );
            }
        }
    }

    // check if main component
    if (strcmp(node->name->value, "main") == 0)
        interpret_visit((ast_node*)node->body);


    // look for run method
    ast_node_function_definition* run_definition = get_function_definition(
        (scope*)ast_node_get_scope((ast_node*)node->body),
        "run"
    );

    if (run_definition)
        interpret_visit((ast_node*)run_definition->body);

    return (ast_node*) node;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
callq 0x12b9
movq %rax, %rdi
movq %rbx, %rsi
callq 0x4077
movq 0x30(%rbx), %rdi
movq (%rdi), %rax
testq %rax, %rax
je 0x271f
xorl %r12d, %r12d
cmpq %r12, %rax
jbe 0x2724
movq %rbx, %rdi
callq 0x12b9
movq 0x30(%rbx), %rcx
movq 0x8(%rcx), %rcx
movq (%rcx,%r12,8), %rcx
movq 0x8(%rcx), %rsi
movq %rax, %rdi
callq 0x4142
movq %rax, %r13
testq %rax, %rax
jne 0x2670
movq 0x30(%rbx), %rax
movq 0x8(%rax), %rax
movq (%rax,%r12,8), %rax
movq 0x8(%rax), %rdx
leaq -0x50(%rbp), %r14
movq %r14, %rdi
leaq 0x2bc1(%rip), %rsi      # 0x5222
xorl %eax, %eax
callq 0x1150
movq %r14, %rdi
callq 0x1ad9
movq 0x28(%r13), %rdi
callq 0x1b80
movq 0x28(%r13), %rdi
callq 0x12b9
movq %rax, %r13
xorl %r14d, %r14d
movq 0x8(%r13), %rax
cmpq %r14, (%rax)
jbe 0x26b3
movq 0x28(%rbx), %rdi
callq 0x12b9
movq 0x8(%r13), %rcx
movq 0x8(%rcx), %rcx
movq (%rcx,%r14,8), %rsi
movq %rax, %rdi
callq 0x4069
incq %r14
jmp 0x2688
movq %r13, %rdi
leaq 0x2b87(%rip), %rsi      # 0x5244
callq 0x40da
testq %rax, %rax
je 0x2710
movq 0x30(%rbx), %rcx
movq 0x8(%rcx), %rcx
movq (%rcx,%r12,8), %r14
movl 0x20(%rax), %r15d
movq 0x30(%rax), %rdi
callq 0x1b80
movq %r14, %rdi
movl %r15d, %esi
movq %rax, %rdx
callq 0x16a8
movq %rax, %r14
movq %rax, %rdi
movq %r13, %rsi
callq 0x12d2
movq 0x28(%rbx), %rdi
callq 0x12b9
movq %rax, %rdi
movq %r14, %rsi
callq 0x3fee
incq %r12
movq 0x30(%rbx), %rax
movq (%rax), %rax
jmp 0x2612
callq 0x1030
movq 0x20(%rbx), %rax
movq 0x8(%rax), %rdi
leaq 0x2b18(%rip), %rsi      # 0x524b
callq 0x10c0
testl %eax, %eax
jne 0x2745
movq 0x28(%rbx), %rdi
callq 0x1b80
movq 0x28(%rbx), %rdi
callq 0x12b9
leaq 0x2afb(%rip), %rsi      # 0x5250
movq %rax, %rdi
callq 0x40da
testq %rax, %rax
je 0x276b
movq 0x30(%rax), %rdi
callq 0x1b80
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq