// File: /workspace/github_c_makefile_star2_O3/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 0x12b2
movq %rax, %rdi
movq %rbx, %rsi
callq 0x4677
movq 0x30(%rbx), %rdi
cmpq $0x0, (%rdi)
je 0x28e3
xorl %r12d, %r12d
movq %rbx, %rdi
callq 0x12b2
movq 0x30(%rbx), %rcx
movq 0x8(%rcx), %rcx
movq (%rcx,%r12,8), %rcx
movq 0x8(%rcx), %rsi
movq %rax, %rdi
callq 0x474a
movq %rax, %r13
testq %rax, %rax
jne 0x2829
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 0x2a08(%rip), %rsi      # 0x5222
xorl %eax, %eax
callq 0x1150
movq %r14, %rdi
callq 0x1b85
movq 0x28(%r13), %rdi
callq 0x1c40
movq 0x28(%r13), %rdi
callq 0x12b2
movq %rax, %r13
movq 0x8(%rax), %rax
cmpq $0x0, (%rax)
je 0x2874
xorl %r14d, %r14d
movq 0x28(%rbx), %rdi
callq 0x12b2
movq 0x8(%r13), %rcx
movq 0x8(%rcx), %rcx
movq (%rcx,%r14,8), %rsi
movq %rax, %rdi
callq 0x4669
incq %r14
movq 0x8(%r13), %rax
cmpq %r14, (%rax)
ja 0x284b
movq %r13, %rdi
leaq 0x29c6(%rip), %rsi      # 0x5244
callq 0x46dd
testq %rax, %rax
je 0x28d1
movq 0x30(%rbx), %rcx
movq 0x8(%rcx), %rcx
movq (%rcx,%r12,8), %r14
movl 0x20(%rax), %r15d
movq 0x30(%rax), %rdi
callq 0x1c40
movq %r14, %rdi
movl %r15d, %esi
movq %rax, %rdx
callq 0x16ec
movq %rax, %r14
movq %rax, %rdi
movq %r13, %rsi
callq 0x12cb
movq 0x28(%rbx), %rdi
callq 0x12b2
movq %rax, %rdi
movq %r14, %rsi
callq 0x45ea
incq %r12
movq 0x30(%rbx), %rax
cmpq %r12, (%rax)
ja 0x27d4
jmp 0x28e8
callq 0x1030
movq 0x20(%rbx), %rax
movq 0x8(%rax), %rdi
leaq 0x2954(%rip), %rsi      # 0x524b
callq 0x10c0
testl %eax, %eax
jne 0x2909
movq 0x28(%rbx), %rdi
callq 0x1c40
movq 0x28(%rbx), %rdi
callq 0x12b2
leaq 0x2937(%rip), %rsi      # 0x5250
movq %rax, %rdi
callq 0x46dd
testq %rax, %rax
je 0x292f
movq 0x30(%rax), %rdi
callq 0x1c40
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq