// File: /workspace/github_c_makefile_star2_O2/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 0x20(%rdi), %rdi
callq 0x1b80
movq %rax, %r14
movq 0x28(%rbx), %r15
movq %rbx, -0x38(%rbp)
movq 0x30(%rbx), %rdi
callq 0x12b9
movq %rax, %r12
movl (%r14), %eax
cmpl $0x15, %eax
je 0x2546
cmpl $0x5, %eax
jne 0x25a3
movq 0x20(%r14), %rdi
callq 0x1090
xorl %ebx, %ebx
testl %eax, %eax
cmovlel %ebx, %eax
movq %rax, -0x30(%rbp)
xorl %eax, %eax
cmpq %rbx, -0x30(%rbp)
je 0x25b1
movq 0x20(%r14), %rax
movsbl (%rax,%rbx), %r13d
movl %r13d, %edi
callq 0x4238
pushq $0x26
popq %rdi
movq %rax, %rsi
callq 0x42a0
movq %rax, %rdi
movl %r13d, %esi
callq 0x136c
movq %rax, %r13
movq 0x8(%r15), %rsi
movq %r12, %rdi
callq 0x3ffb
movq %r15, %rdi
pushq $0x28
popq %rsi
movq %r13, %rdx
callq 0x16a8
movq %r12, %rdi
movq %rax, %rsi
callq 0x3fee
movq -0x38(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x1b80
incq %rbx
jmp 0x24db
movq 0x28(%r14), %rax
movl (%rax), %eax
xorl %ebx, %ebx
testl %eax, %eax
cmovlel %ebx, %eax
movq %rax, -0x30(%rbp)
xorl %eax, %eax
cmpq %rbx, -0x30(%rbp)
je 0x25b1
movq 0x28(%r14), %rax
movq 0x8(%rax), %rax
movq (%rax,%rbx,8), %r13
movq 0x8(%r15), %rsi
movq %r12, %rdi
callq 0x3ffb
movl 0x20(%r14), %esi
movq %r15, %rdi
movq %r13, %rdx
callq 0x16a8
movq %r12, %rdi
movq %rax, %rsi
callq 0x3fee
movq -0x38(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x1b80
incq %rbx
jmp 0x2559
leaq 0x2bdc(%rip), %rdi      # 0x5186
callq 0x1ad9
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq