// File: /workspace/github_c_makefile_star2_O1/yutopp[P]cc/analyzer.c
// Function: analyze_expr
Type* analyze_expr(Analyzer* a, Node* node, Env* env) {
    switch(node->kind) {
    case NODE_EXPR_BIN:
    {
        printf("LOG: expr binary = ");
        token_fprint_buf(stdout, node->value.expr_bin.op);
        printf("\n");

        analyze_expr(a, node->value.expr_bin.lhs, env);
        analyze_expr(a, node->value.expr_bin.rhs, env);

        break;
    }

    case NODE_EXPR_POSTFIX:
    {
        fprintf(DEBUGOUT,"LOG: postfix = \n");

        analyze_expr(a, node->value.expr_postfix.lhs, env);

        switch(node->value.expr_postfix.kind) {
        case NODE_EXPR_POSTFIX_KIND_FUNC_CALL:
            if (node->value.expr_postfix.rhs) {
                analyze_expr(a, node->value.expr_postfix.rhs, env);
            }
            break;

        default:
            assert(0); // TODO: error handling
        }

        break;
    }

    case NODE_LIT_INT:
    {
        printf("LOG: lit int = %d\n", node->value.lit_int.v);

        Type* ty = type_arena_malloc(a->arena);
        ty->kind = TYPE_KIND_INT;
        ty->value.int_.bits = -1;

        return ty;
    }

    case NODE_LIT_STRING:
    {
        printf("LOG: lit string = %d\n", node->value.lit_string.v);

        Type* ty_inner = type_arena_malloc(a->arena);
        ty_inner->kind = TYPE_KIND_INT;
        ty_inner->value.int_.bits = 8;

        Type* ty = type_arena_malloc(a->arena);
        ty->kind = TYPE_KIND_PTR;
        ty->value.ptr.inner = ty_inner;

        node->ty = ty;

        return ty;
    }

    case NODE_ID:
    {
        fprintf(DEBUGOUT, "LOG: id =");
        token_fprint_buf(DEBUGOUT, node->value.id.tok);
        fprintf(DEBUGOUT, "\n");

        Env* found = env_lookup(env, node->value.id.tok);
        if (found == NULL) {
            fprintf(DEBUGOUT, "! NOT FOUND\n");
            return NULL;
        }
        fprintf(DEBUGOUT, "! FOUND\n");

        return NULL; // TODO: fix
    }

    case NODE_ARGS_LIST:
    {
        fprintf(DEBUGOUT, "LOG: args list\n");

        Vector* args = node->value.args_list.args;
        for(size_t i=0; i<vector_len(args); ++i) {
            Node** n = (Node**)vector_at(args, i);
            analyze_expr(a, *n, env); // TODO: error handling
        }

        return NULL; // TODO: fix
    }

    default:
        // TODO: error handling...
        fprintf(stderr, "Unknown kind: %d", node->kind);
        assert(0);
        break;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x2daf(%rip), %r12      # 0x77d8
movq 0x65a8(%rip), %rbp      # 0xafd8
leaq 0x2e68(%rip), %r13      # 0x789f
movl (%r15), %edi
leal -0x6(%rdi), %eax
cmpl $0x5, %eax
ja 0x4c41
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
leaq 0x2e35(%rip), %rdi      # 0x788b
xorl %eax, %eax
callq 0x10b0
movq (%rbp), %rdi
movq 0x8(%r15), %rsi
callq 0x1c61
movl $0xa, %edi
callq 0x1050
movq 0x10(%r15), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x4a0e
movq 0x18(%r15), %r15
jmp 0x4a37
movq (%rbp), %rcx
movl $0x10, %esi
movl $0x1, %edx
movq %r13, %rdi
callq 0x11b0
movq 0x10(%r15), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x4a0e
cmpl $0x0, 0x8(%r15)
jne 0x4c22
movq 0x18(%r15), %r15
testq %r15, %r15
jne 0x4a37
jmp 0x4bef
movq (%rbp), %rcx
leaq 0x2e31(%rip), %rdi      # 0x7907
movl $0x9, %esi
movl $0x1, %edx
callq 0x11b0
movq (%rbp), %rdi
movq 0x8(%r15), %rsi
callq 0x1c61
movq (%rbp), %rsi
movl $0xa, %edi
callq 0x10f0
movq 0x8(%r15), %rsi
movq %r14, %rdi
callq 0x4705
movq (%rbp), %rcx
testq %rax, %rax
je 0x4bfe
leaq 0x2dfe(%rip), %rdi      # 0x791e
movl $0x8, %esi
jmp 0x4c0a
movq 0x8(%r15), %rsi
leaq 0x2dbc(%rip), %rdi      # 0x78f1
xorl %eax, %eax
callq 0x10b0
movq (%rbx), %rdi
callq 0x62d6
movq %rax, %r14
movl $0x0, (%rax)
movl $0x8, 0x8(%rax)
movq (%rbx), %rdi
callq 0x62d6
movl $0x1, (%rax)
movq %r14, 0x8(%rax)
movq %rax, 0x28(%r15)
jmp 0x4bef
movq (%rbp), %rcx
leaq 0x2dad(%rip), %rdi      # 0x7927
movl $0xf, %esi
movl $0x1, %edx
callq 0x11b0
movq 0x8(%r15), %r15
movq %r15, %rdi
callq 0x322b
testq %rax, %rax
je 0x4bef
xorl %r12d, %r12d
movq %r15, %rdi
movq %r12, %rsi
callq 0x3216
movq (%rax), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x4a0e
incq %r12
movq %r15, %rdi
callq 0x322b
cmpq %rax, %r12
jb 0x4b9d
jmp 0x4bef
movl 0x8(%r15), %esi
leaq 0x2d0b(%rip), %rdi      # 0x78de
xorl %eax, %eax
callq 0x10b0
movq (%rbx), %rdi
callq 0x62d6
movl $0x0, (%rax)
movl $0xffffffff, 0x8(%rax)  # imm = 0xFFFFFFFF
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2d0c(%rip), %rdi      # 0x7911
movl $0xc, %esi
movl $0x1, %edx
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x11b0
leaq 0x2c05(%rip), %rdi      # 0x782e
leaq 0x2bcc(%rip), %rsi      # 0x77fc
leaq 0x2c79(%rip), %rcx      # 0x78b0
movl $0xe4, %edx
callq 0x10d0
callq 0x133f
nop