// File: /workspace/github_c_makefile_star2_O1/samsonjs[P]lake/src/eval.c
// Function: eval
LakeVal *eval(LakeCtx *ctx, Env *env, LakeVal *expr)
{
    LakeVal *result;
    LakeList *list;

    switch (expr->type)
    {

    /* self evaluating types */
    case TYPE_BOOL:
    case TYPE_INT:
    case TYPE_STR:
        result = expr;
        break;

    case TYPE_SYM:
        result = env_get(env, (void *)SYM(expr));
        if (!result)
        {
            ERR("undefined variable: %s", sym_repr(SYM(expr)));
        }
        break;

    case TYPE_DLIST:
        ERR("malformed function call");
        result = NULL;
        break;

    case TYPE_COMM:
        result = NULL;
        break;

    case TYPE_LIST:
        list = LIST(expr);

        if (LIST_N(list) == 0)
        {
            result = expr;
        }
        else
        {
            if (is_special_form(ctx, list))
            {
                result = eval_special_form(ctx, env, list);
            }
            else
            {
                LakeVal *fn = eval(ctx, env, LIST_VAL(list, 0));
                if (!fn)
                {
                    return NULL;
                }
                LakeList *args = list_make_with_capacity(LIST_N(list) - 1);
                int i;
                LakeVal *v;
                for (i = 1; i < LIST_N(list); ++i)
                {
                    v = eval(ctx, env, LIST_VAL(list, i));
                    if (v != NULL)
                    {
                        list_append(args, v);
                    }
                    else
                    {
                        list_free(args);
                        result = NULL;
                        goto done;
                    }
                }
                result = apply(ctx, fn, args);
            }
        }
        break;

    default:
        ERR("unrecognized value, type %d, size %zu bytes", expr->type,
            expr->size);
        DIE("we don't eval that around here!");
    }

done:
    return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %rbx
movl (%rdx), %eax
decl %eax
cmpl $0x8, %eax
ja 0x2c1c
movq %rsi, %r14
leaq 0x3540(%rip), %rcx      # 0x6010
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %rbx, %rax
jmp 0x2c0d
cmpq $0x0, 0x18(%rbx)
je 0x2ad9
movq %rdi, %r15
movq %rbx, %rsi
callq 0x2a2b
testl %eax, %eax
je 0x2b5d
movq 0x20(%rbx), %rax
movq (%rax), %r12
movq 0x10(%r15), %rdi
movq 0x18(%r12), %rsi
callq 0x3172
testq %rax, %rax
je 0x2c03
movq %rbx, %rdi
movq %rax, %rbx
callq 0x3b73
movq %r15, %rdi
movq %r14, %rsi
movq %rax, %rdx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *%rax
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2347
testq %rax, %rax
jne 0x2c0d
movq %rbx, %rdi
callq 0x1375
jmp 0x2c0b
cmpq $0x0, 0x18(%rbx)
je 0x2b6d
movq 0x20(%rbx), %rax
movq (%rax), %rdx
jmp 0x2b6f
xorl %edx, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x2aab
testq %rax, %rax
je 0x2bdb
movq %rax, %r12
movq 0x18(%rbx), %rdi
decq %rdi
callq 0x396e
movq %rax, %r13
cmpq $0x2, 0x18(%rbx)
jb 0x2bc9
movl $0x1, %ebp
movq 0x20(%rbx), %rax
movq (%rax,%rbp,8), %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x2aab
testq %rax, %rax
je 0x2be9
movq %r13, %rdi
movq %rax, %rsi
callq 0x3ac7
incq %rbp
cmpq %rbp, 0x18(%rbx)
ja 0x2b9d
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x2c24
xorl %ecx, %ecx
jmp 0x2bf8
movl $0x1, %ecx
jmp 0x2bf8
callq 0x132b
jmp 0x2c0b
movq %r13, %rdi
callq 0x3920
movl $0xa, %ecx
xorl %eax, %eax
testl %ecx, %ecx
je 0x2c0d
cmpl $0xa, %ecx
je 0x2c0d
jmp 0x2c0b
movq %r12, %rdi
callq 0x12d8
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x13c8