// File: /workspace/github_c_makefile_star2_O2/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 0x1fd0
movq %rsi, %r14
leaq 0x423e(%rip), %rcx      # 0x6010
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %rbx, %r14
jmp 0x1f73
cmpq $0x0, 0x18(%rbx)
je 0x1ddb
movq %rdi, %r15
movq %rbx, %rsi
callq 0x1d2f
testl %eax, %eax
je 0x1ea2
movq 0x20(%rbx), %rax
movq (%rax), %r12
movq 0x10(%r15), %rdi
movq 0x18(%r12), %rsi
callq 0x28af
testq %rax, %rax
je 0x1f85
movq %rbx, %rdi
movq %rax, %rbx
callq 0x2e8a
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 0x164c
movq %rax, %r14
testq %rax, %rax
jne 0x1f73
movq 0x8198(%rip), %r12      # 0x9ff8
movq (%r12), %rcx
leaq 0x41f3(%rip), %rdi      # 0x605e
pushq $0x7
popq %rsi
pushq $0x1
popq %rdx
callq 0x11f0
movq (%r12), %r15
movq %rbx, %rdi
callq 0x4cbb
leaq 0x41dd(%rip), %rsi      # 0x6066
xorl %r14d, %r14d
movq %r15, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1170
movq (%r12), %rsi
jmp 0x1fc6
cmpq $0x0, 0x18(%rbx)
je 0x1eb2
movq 0x20(%rbx), %rax
movq (%rax), %rdx
jmp 0x1eb4
xorl %edx, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x1dad
testq %rax, %rax
je 0x1f70
movq %rax, %r12
movq 0x18(%rbx), %rdi
decq %rdi
callq 0x2d21
movq %rax, %r13
pushq $0x1
popq %rbp
cmpq %rbp, 0x18(%rbx)
jbe 0x1f0b
movq 0x20(%rbx), %rax
movq (%rax,%rbp,8), %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x1dad
testq %rax, %rax
je 0x1f27
movq %r13, %rdi
movq %rax, %rsi
callq 0x2e14
incq %rbp
jmp 0x1edd
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 0x2059
movq %r13, %rdi
callq 0x2cfc
jmp 0x1f70
movq 0x80c0(%rip), %r14      # 0x9ff8
movq (%r14), %rcx
leaq 0x411c(%rip), %rdi      # 0x605e
pushq $0x7
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
callq 0x11f0
movq (%r14), %rcx
leaq 0x4123(%rip), %rdi      # 0x607d
pushq $0x17
popq %rsi
movq %rbx, %rdx
callq 0x11f0
movq (%r14), %rsi
pushq $0xa
popq %rdi
callq 0x1100
xorl %r14d, %r14d
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x806c(%rip), %r15      # 0x9ff8
movq (%r15), %rcx
leaq 0x40c8(%rip), %rdi      # 0x605e
pushq $0x7
popq %rsi
pushq $0x1
popq %rdx
callq 0x11f0
movq (%r15), %rbx
movq %r12, %rdi
callq 0x4cbb
leaq 0x42ae(%rip), %rsi      # 0x6261
xorl %r14d, %r14d
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1170
movq (%r15), %rsi
pushq $0xa
popq %rdi
callq 0x1100
jmp 0x1f73
movq 0x8021(%rip), %r13      # 0x9ff8
movq (%r13), %rcx
leaq 0x407c(%rip), %r15      # 0x605e
pushq $0x7
popq %r12
pushq $0x1
popq %r14
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x11f0
movq (%r13), %rdi
movl (%rbx), %edx
movq 0x8(%rbx), %rcx
leaq 0x4312(%rip), %rsi      # 0x631b
xorl %eax, %eax
callq 0x1170
movq (%r13), %rsi
pushq $0xa
popq %rbx
movl %ebx, %edi
callq 0x1100
movq (%r13), %rcx
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x11f0
movq (%r13), %rcx
leaq 0x405a(%rip), %rdi      # 0x6095
pushq $0x1f
popq %rsi
movq %r14, %rdx
callq 0x11f0
movq (%r13), %rsi
movl %ebx, %edi
callq 0x1100
pushq $0x1
popq %rdi
callq 0x11e0