// File: /workspace/github_c_makefile_star2_O3/sebbekarlsson[P]cola/src/interpret.c
// Function: interpret_visit_foreach
ast_node* interpret_visit_foreach(ast_node_foreach* node) {
    ast_node* final_result = (void*)0;
    ast_node* iterable = interpret_visit(node->iterable);
    token* value_name_tok = node->value_name;
    int counter = 0;
    int size = 0;


    scope* target_scope = (scope*)ast_node_get_scope((ast_node*)node->body);

    // we currently only support strings...
    // no other iterables exists yet! :)
    //
    // TODO: support more types
    if (iterable->type == AST_TYPE_STRING) {
        ast_node_string* iterable_string = (ast_node_string*) iterable;
        size = strlen(iterable_string->value);


        while (counter < size) {
            char current_char = iterable_string->value[counter];
            ast_node_char* current_item_char = init_ast_node_char(init_token(_CHAR, char_to_string(current_char)), current_char);

            // this is quite ugly, it deletes the definition and
            // sets the definition for every iteration in the loop.
            //
            // TODO: Make this prettier
            delete_variable_definition(target_scope, value_name_tok->value);
            ast_node_variable_definition* var_def = init_ast_node_variable_definition(
                value_name_tok,
                _DATA_TYPE_CHAR,
                (ast_node*) current_item_char        
            );
            save_variable_definition(target_scope, var_def);

            final_result = interpret_visit((ast_node*) node->body);
            counter++;
        }
    } else if (iterable->type == AST_TYPE_VECTOR) {
        ast_node_vector* iterable_vector = (ast_node_vector*) iterable;
        size = iterable_vector->vector->size;

        while (counter < size) {
            ast_node* current_node = iterable_vector->vector->items[counter];

            // this is quite ugly, it deletes the definition and
            // sets the definition for every iteration in the loop.
            //
            // TODO: Make this prettier
            delete_variable_definition(target_scope, value_name_tok->value);
            ast_node_variable_definition* var_def = init_ast_node_variable_definition(
                value_name_tok,
                iterable_vector->data_type,
                current_node 
            );
            save_variable_definition(target_scope, var_def);

            final_result = interpret_visit((ast_node*) node->body);
            counter++;
        }

    } else {
        error_in_interpreter("Does not know how to iterate data_type");
    }

    return final_result;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movq %rdi, -0x38(%rbp)
movq 0x20(%rdi), %rdi
callq 0x1c40
movq %rax, %r14
movq 0x28(%rbx), %r15
movq 0x30(%rbx), %rdi
callq 0x12b2
movq %rax, %r12
movl (%r14), %eax
cmpl $0x15, %eax
je 0x26ff
cmpl $0x5, %eax
jne 0x2767
movq 0x20(%r14), %rdi
callq 0x1090
movq %rax, -0x30(%rbp)
testl %eax, %eax
jle 0x2773
movq -0x30(%rbp), %rax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, -0x30(%rbp)
xorl %ebx, %ebx
movq 0x20(%r14), %rax
movsbl (%rax,%rbx), %r13d
movl %r13d, %edi
callq 0x4850
movl $0x26, %edi
movq %rax, %rsi
callq 0x48cc
movq %rax, %rdi
movl %r13d, %esi
callq 0x1370
movq %rax, %r13
movq 0x8(%r15), %rsi
movq %r12, %rdi
callq 0x45f7
movq %r15, %rdi
movl $0x28, %esi
movq %r13, %rdx
callq 0x16ec
movq %r12, %rdi
movq %rax, %rsi
callq 0x45ea
movq -0x38(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x1c40
incq %rbx
cmpq %rbx, -0x30(%rbp)
jne 0x2694
jmp 0x2775
movq 0x28(%r14), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
testl %eax, %eax
jle 0x2773
movq -0x30(%rbp), %rax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, -0x30(%rbp)
xorl %ebx, %ebx
movq 0x28(%r14), %rax
movq 0x8(%rax), %rax
movq (%rax,%rbx,8), %r13
movq 0x8(%r15), %rsi
movq %r12, %rdi
callq 0x45f7
movl 0x20(%r14), %esi
movq %r15, %rdi
movq %r13, %rdx
callq 0x16ec
movq %r12, %rdi
movq %rax, %rsi
callq 0x45ea
movq -0x38(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x1c40
incq %rbx
cmpq %rbx, -0x30(%rbp)
jne 0x271d
jmp 0x2775
leaq 0x2a18(%rip), %rdi      # 0x5186
callq 0x1b85
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq