// File: /workspace/github_c_makefile_star2_O0/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
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq $0x0, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x2110
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x12b0
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x5, (%rax)
jne 0x3161
movq -0x18(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x1070
movl %eax, -0x28(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x315c
movq -0x38(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x24(%rbp), %rcx
movb (%rax,%rcx), %al
movb %al, -0x39(%rbp)
movsbl -0x39(%rbp), %edi
callq 0x6130
movq %rax, %rsi
movl $0x26, %edi
callq 0x61f0
movq %rax, %rdi
movsbl -0x39(%rbp), %esi
callq 0x13f0
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x5d30
movq -0x20(%rbp), %rdi
movq -0x48(%rbp), %rdx
movl $0x28, %esi
callq 0x1990
movq %rax, -0x50(%rbp)
movq -0x30(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x5d00
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x2110
movq %rax, -0x10(%rbp)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x30c5
jmp 0x3206
movq -0x18(%rbp), %rax
cmpl $0x15, (%rax)
jne 0x31f8
movq -0x18(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq 0x28(%rax), %rax
movq (%rax), %rax
movl %eax, -0x28(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x31f6
movq -0x58(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x8(%rax), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x60(%rbp)
movq -0x30(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x5d30
movq -0x20(%rbp), %rdi
movq -0x58(%rbp), %rax
movl 0x20(%rax), %esi
movq -0x60(%rbp), %rdx
callq 0x1990
movq %rax, -0x68(%rbp)
movq -0x30(%rbp), %rdi
movq -0x68(%rbp), %rsi
callq 0x5d00
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x2110
movq %rax, -0x10(%rbp)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x3184
jmp 0x3204
leaq 0x3f0f(%rip), %rdi      # 0x710e
callq 0x2040
jmp 0x3206
movq -0x10(%rbp), %rax
addq $0x70, %rsp
popq %rbp
retq