// File: /workspace/github_c_makefile_star2_O3/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 0x30f2(%rip), %r12      # 0x77e8
movq 0x68db(%rip), %rbp      # 0xafd8
leaq 0x31ab(%rip), %r13      # 0x78af
movl (%r15), %edi
leal -0x6(%rdi), %eax
cmpl $0x5, %eax
ja 0x490e
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
leaq 0x3178(%rip), %rdi      # 0x789b
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 0x46db
movq 0x18(%r15), %r15
jmp 0x4704
movq (%rbp), %rcx
movl $0x10, %esi
movl $0x1, %edx
movq %r13, %rdi
callq 0x11c0
movq 0x10(%r15), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x46db
cmpl $0x0, 0x8(%r15)
jne 0x48ef
movq 0x18(%r15), %r15
testq %r15, %r15
jne 0x4704
jmp 0x48bc
movq (%rbp), %rcx
leaq 0x3174(%rip), %rdi      # 0x7917
movl $0x9, %esi
movl $0x1, %edx
callq 0x11c0
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 0x43d5
movq (%rbp), %rcx
testq %rax, %rax
je 0x48cb
leaq 0x3141(%rip), %rdi      # 0x792e
movl $0x8, %esi
jmp 0x48d7
movq 0x8(%r15), %rsi
leaq 0x30ff(%rip), %rdi      # 0x7901
xorl %eax, %eax
callq 0x10b0
movq (%rbx), %rdi
callq 0x5fca
movq %rax, %r14
movl $0x0, (%rax)
movl $0x8, 0x8(%rax)
movq (%rbx), %rdi
callq 0x5fca
movl $0x1, (%rax)
movq %r14, 0x8(%rax)
movq %rax, 0x28(%r15)
jmp 0x48bc
movq (%rbp), %rcx
leaq 0x30f0(%rip), %rdi      # 0x7937
movl $0xf, %esi
movl $0x1, %edx
callq 0x11c0
movq 0x8(%r15), %r15
movq %r15, %rdi
callq 0x2f0b
testq %rax, %rax
je 0x48bc
xorl %r12d, %r12d
movq %r15, %rdi
movq %r12, %rsi
callq 0x2ef6
movq (%rax), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x46db
incq %r12
movq %r15, %rdi
callq 0x2f0b
cmpq %rax, %r12
jb 0x486a
jmp 0x48bc
movl 0x8(%r15), %esi
leaq 0x304e(%rip), %rdi      # 0x78ee
xorl %eax, %eax
callq 0x10b0
movq (%rbx), %rdi
callq 0x5fca
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 0x304f(%rip), %rdi      # 0x7921
movl $0xc, %esi
movl $0x1, %edx
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x11c0
leaq 0x2f48(%rip), %rdi      # 0x783e
leaq 0x2f0f(%rip), %rsi      # 0x780c
leaq 0x2fbc(%rip), %rcx      # 0x78c0
movl $0xe4, %edx
callq 0x10d0
callq 0x134f
nop