// File: /workspace/github_c_makefile_star2_O3/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 0x2c01
movq %rsi, %r14
leaq 0x354e(%rip), %rcx      # 0x6010
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %rbx, %rax
jmp 0x2bf2
cmpq $0x0, 0x18(%rbx)
je 0x2acb
movq %rdi, %r15
movq %rbx, %rsi
callq 0x2a1d
testl %eax, %eax
je 0x2b4f
movq 0x20(%rbx), %rax
movq (%rax), %r12
movq 0x10(%r15), %rdi
movq 0x18(%r12), %rsi
callq 0x339d
testq %rax, %rax
je 0x2be8
movq %rbx, %rdi
movq %rax, %rbx
callq 0x3b07
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 0x2329
testq %rax, %rax
jne 0x2bf2
movq %rbx, %rdi
callq 0x1375
jmp 0x2bf0
cmpq $0x0, 0x18(%rbx)
je 0x2b5f
movq 0x20(%rbx), %rax
movq (%rax), %rdx
jmp 0x2b61
xorl %edx, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x2a9d
testq %rax, %rax
je 0x2bf0
movq %rax, %r12
movq 0x18(%rbx), %rdi
decq %rdi
callq 0x3926
movq %rax, %r13
cmpq $0x2, 0x18(%rbx)
jb 0x2bbb
movl $0x1, %ebp
movq 0x20(%rbx), %rax
movq (%rax,%rbp,8), %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x2a9d
testq %rax, %rax
je 0x2bde
movq %r13, %rdi
movq %rax, %rsi
callq 0x3a63
incq %rbp
cmpq %rbp, 0x18(%rbx)
ja 0x2b8f
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2c09
callq 0x132b
jmp 0x2bf0
movq %r13, %rdi
callq 0x38d8
jmp 0x2bf0
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