// File: /workspace/github_c_makefile_star2_O3/qpfiffer[P]lair/src/eval.c
// Function: lair_env_eval
inline const struct _lair_type *_lair_env_eval(
		struct _lair_runtime *r,
		const struct _lair_ast *ast,
		struct _lair_env *env) {
	/* We have a goto here to avoid creating a new stack frame, when we really just
	 * want to call this function again.
	 */
	/* THIS WHOLE FUCKING THING NEEDS A FINITE STATE MACHINE */
	const struct _lair_ast *possible_new_atom = NULL;
start_eval:
	switch (ast->atom.type) {
		case LR_OPERATOR:
			return _lair_call_function(r, ast, env);
		case LR_CALL:
			return _lair_call_function(r, ast->next, env);
		case LR_IF:
			ast = _evalute_if_statement(r, ast, env);
			goto start_eval;
		case LR_DEDENT:
			throw_exception(r, ERR_RUNTIME, "PANIC DEDENT");
			return NULL;
		case LR_ATOM:
			possible_new_atom = _infer_atom_at_runtime(r, ast, env);
			if (ast->next != NULL && ast->next->atom.type == LR_RETURN) {
				/* Evaluate the RHS, get the value. */
				const struct _lair_type *ret_val = _lair_env_eval(r, ast->next, env);
				/* Now stick that value as a simple function under the name of
				 * whatever the AST's atom is.
				 */
				_lair_add_simple_function(env, ast->atom.value.str, ret_val);
				ast = _continue(ast);
				if (ast == NULL)
					return ret_val;
				goto start_eval;
			} else if (ast->prev != NULL && ast->prev->atom.type == LR_INDENT &&
					_is_callable(ast)) {
				ast = _call_and_continue(r, ast, env);
				if (ast == NULL)
					return &possible_new_atom->atom;
				goto start_eval;
			} else if (possible_new_atom == NULL) {
				char buf[256] = {0};
				snprintf(buf, sizeof(buf), "Atom is undefined: %s", ast->atom.value.str);
				throw_exception(r, ERR_RUNTIME, buf);
			}
			return &possible_new_atom->atom;
		case LR_INDENT:
			env->currently_returning = 0;
			ast = ast->next;
			goto start_eval;
		case LR_RETURN:
			env->currently_returning = 1;
			return _lair_env_eval(r, ast->next, env);
		default:
			return &ast->atom;
	}
	/* TODO: Expire anything in this scope. Probably. */

	return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %rsi, %r13
movq %rdi, %r12
movq %rdx, 0x8(%rsp)
leaq 0x18(%rdx), %rax
movq %rax, 0x10(%rsp)
movq %rdi, 0x18(%rsp)
leaq 0x28(%r13), %r15
movl 0x28(%r13), %eax
addl $-0x3, %eax
cmpl $0xb, %eax
ja 0x22f0
leaq 0x23b2(%rip), %rcx      # 0x4368
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x8(%rsp), %rax
movl $0x0, 0x28(%rax)
movq 0x8(%r13), %r13
jmp 0x1f9b
movq 0x8(%r13), %rsi
movq %r12, %rdi
movq 0x8(%rsp), %rdx
callq 0x2305
movl 0x20(%r13), %ebx
leaq 0x23eb(%rip), %rcx      # 0x43d8
cmpq %rcx, %rax
je 0x2101
movl %ebx, %ebp
movq %r13, %r14
movl %ebx, %eax
cmpl %eax, %ebp
jne 0x2145
movq 0x8(%r14), %r13
testq %r13, %r13
jne 0x2023
movl $0x1, %edi
leaq 0x25f1(%rip), %rsi      # 0x460b
callq 0x1653
movq 0x8(%r14), %r13
movl 0x20(%r13), %ecx
cmpl %ebx, %ecx
movl %ebp, %eax
cmoval %ecx, %eax
cmpl $0x7, 0x28(%r13)
cmovnel %ebp, %eax
movl %ecx, %ebp
movq %r13, %r14
cmpl %eax, %ecx
jae 0x1ffd
jmp 0x1f9b
movq %r12, %rdi
movl $0x1, %esi
movl $0x2, %edx
leaq 0x25c3(%rip), %rcx      # 0x461b
callq 0x1608
movq 0x30(%r13), %r12
movq %r12, %rdi
callq 0x1080
movq %rax, %rbx
movl $0x1, %edi
movl $0x38, %esi
callq 0x1130
movq %rax, %rbp
movups (%r13), %xmm0
movups 0x10(%r13), %xmm1
movups 0x20(%r13), %xmm2
movups %xmm0, (%rax)
movups %xmm1, 0x10(%rax)
movups %xmm2, 0x20(%rax)
movq 0x30(%r13), %rax
movq %rax, 0x30(%rbp)
cmpq $0x0, 0x8(%rsp)
je 0x20f5
movq 0x8(%rsp), %r14
movq 0x8(%r14), %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x2a16
testq %rax, %rax
jne 0x214d
movq 0x10(%r14), %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x2a16
testq %rax, %rax
jne 0x214d
movq 0x18(%r14), %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x2a16
testq %rax, %rax
jne 0x2156
movq (%r14), %r14
testq %r14, %r14
jne 0x20ad
movq %rbp, %rdi
callq 0x1030
xorl %ebp, %ebp
jmp 0x2164
leaq 0x24c2(%rip), %r15      # 0x45ca
movq 0x8(%r13), %r14
testq %r14, %r14
jne 0x2122
movl $0x1, %edi
movq %r15, %rsi
callq 0x1653
movq 0x8(%r13), %r14
movq %r14, %r13
cmpl %ebx, 0x20(%r14)
je 0x2108
seta %al
movzbl %al, %esi
movq %r12, %rdi
movl $0x1, %edx
leaq 0x24ac(%rip), %rcx      # 0x45ec
callq 0x1608
movq %r14, %r13
jmp 0x1f9b
movl $0x1, 0x28(%rbp)
jmp 0x2164
movl 0x28(%rax), %ecx
movl %ecx, 0x28(%rbp)
movq 0x30(%rax), %rax
movq %rax, 0x30(%rbp)
movq 0x8(%r13), %rsi
testq %rsi, %rsi
movq 0x18(%rsp), %r12
je 0x2200
cmpl $0x4, 0x28(%rsi)
jne 0x2200
movq %r12, %rdi
movq 0x8(%rsp), %rdx
callq 0x1f71
movq %rax, %r15
movq 0x30(%r13), %rbx
movl (%rax), %eax
movq 0x8(%r15), %rcx
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movl %eax, 0x48(%rsp)
movq %rcx, 0x50(%rsp)
movq %rbx, %rdi
callq 0x1080
movl $0x38, %r8d
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
movq %rax, %rdx
leaq 0x20(%rsp), %rcx
callq 0x2910
movl 0x28(%r13), %eax
cmpl $0x7, %eax
je 0x1f9b
movq 0x8(%r13), %r13
testq %r13, %r13
je 0x22f0
movl 0x28(%r13), %eax
cmpl $0x9, %eax
jne 0x21dc
jmp 0x1f9b
movq (%r13), %rax
testq %rax, %rax
je 0x2255
cmpl $0x7, 0x28(%rax)
jne 0x2255
movl (%r15), %eax
cmpl $0xe, %eax
ja 0x2255
movl $0x500e, %ecx           # imm = 0x500E
btl %eax, %ecx
jae 0x2255
movq %r12, %rdi
movq %r13, %rsi
movq 0x8(%rsp), %rdx
callq 0x2305
movl 0x28(%r13), %eax
cmpl $0x7, %eax
je 0x1f9b
movq 0x8(%r13), %r13
testq %r13, %r13
je 0x2298
movl 0x28(%r13), %eax
cmpl $0x9, %eax
jne 0x2235
jmp 0x1f9b
testq %rbp, %rbp
jne 0x2298
leaq 0x20(%rsp), %rbx
movl $0x100, %edx            # imm = 0x100
movq %rbx, %rdi
xorl %esi, %esi
callq 0x10d0
movq 0x30(%r13), %rcx
leaq 0x224e(%rip), %rdx      # 0x44c7
movl $0x100, %esi            # imm = 0x100
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10b0
movq %r12, %rdi
movl $0x2, %esi
movq %rbx, %rdx
callq 0x1630
addq $0x28, %rbp
movq %rbp, %r15
jmp 0x22f0
leaq 0x2212(%rip), %rdx      # 0x44ba
movq %r12, %rdi
movl $0x2, %esi
callq 0x1630
xorl %r15d, %r15d
jmp 0x22f0
movq %r12, %rdi
movq %r13, %rsi
jmp 0x22e3
movq 0x8(%rsp), %rdx
movl $0x1, 0x28(%rdx)
movq 0x8(%r13), %rsi
movq %r12, %rdi
callq 0x1f71
jmp 0x22ed
movq 0x8(%r13), %rsi
movq %r12, %rdi
movq 0x8(%rsp), %rdx
callq 0x2305
movq %rax, %r15
movq %r15, %rax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq