// File: /workspace/github_c_makefile_star2_O1/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 %rdx, %r15
movq %rsi, %r14
movq %rdi, %r12
movq %rdi, 0x8(%rsp)
movq %rdx, 0x10(%rsp)
movq %rax, 0x18(%rsp)
leaq 0x28(%r14), %rbx
movl 0x28(%r14), %eax
leal -0x3(%rax), %ecx
cmpl $0xb, %ecx
ja 0x22c3
leaq 0x23bb(%rip), %rdx      # 0x4368
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movl $0x0, 0x28(%r15)
movq 0x8(%r14), %r14
jmp 0x1f92
movq 0x8(%r14), %rsi
movq %r12, %rdi
movq %r15, %rdx
callq 0x22d8
movl 0x20(%r14), %ebx
leaq 0x23fa(%rip), %rcx      # 0x43d8
cmpq %rcx, %rax
je 0x216c
movl %ebx, %ebp
cmpl %ebp, 0x20(%r14)
jne 0x1f92
cmpq $0x0, 0x8(%r14)
jne 0x2007
movl $0x1, %edi
leaq 0x2609(%rip), %rsi      # 0x460b
callq 0x164b
movq 0x8(%r14), %r14
cmpl $0x7, 0x28(%r14)
jne 0x201b
movl 0x20(%r14), %eax
cmpl %ebx, %eax
cmoval %eax, %ebp
cmpl %ebp, 0x20(%r14)
jae 0x1fe9
jmp 0x1f92
xorl %esi, %esi
cmpl $0xe, %eax
sete %sil
movq %r12, %rdi
movl $0x2, %edx
leaq 0x25dd(%rip), %rcx      # 0x461b
callq 0x1600
movq 0x30(%r14), %r12
movq %r12, %rdi
callq 0x1080
movq %rax, %rbp
movl $0x1, %edi
movl $0x38, %esi
callq 0x1130
movq %rax, %r13
movups (%r14), %xmm0
movups 0x10(%r14), %xmm1
movups 0x20(%r14), %xmm2
movups %xmm0, (%rax)
movups %xmm1, 0x10(%rax)
movups %xmm2, 0x20(%rax)
movq 0x30(%r14), %rax
movq %rax, 0x30(%r13)
testq %r15, %r15
je 0x20ee
movq 0x8(%r15), %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x2a5e
testq %rax, %rax
jne 0x20b2
movq 0x10(%r15), %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x2a5e
testq %rax, %rax
je 0x20c2
movl $0x1, 0x28(%r13)
xorl %eax, %eax
testb %al, %al
jne 0x2085
jmp 0x20f9
movq 0x18(%r15), %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x2a5e
testq %rax, %rax
je 0x20e7
movl 0x28(%rax), %ecx
movl %ecx, 0x28(%r13)
movq 0x30(%rax), %rax
movq %rax, 0x30(%r13)
jmp 0x20ba
movq (%r15), %r15
movb $0x1, %al
jmp 0x20bc
movq %r13, %rdi
callq 0x1030
xorl %r13d, %r13d
movq 0x8(%r14), %rsi
testq %rsi, %rsi
je 0x210c
cmpl $0x4, 0x28(%rsi)
je 0x21cd
movq (%r14), %rax
testq %rax, %rax
movq 0x8(%rsp), %r12
movq 0x10(%rsp), %r15
je 0x223a
cmpl $0x7, 0x28(%rax)
jne 0x223a
movl (%rbx), %eax
cmpl $0xe, %eax
ja 0x223a
movl $0x500e, %ecx           # imm = 0x500E
btl %eax, %ecx
jae 0x223a
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x22d8
cmpl $0x7, 0x28(%r14)
je 0x21bf
movq 0x8(%r14), %r14
testq %r14, %r14
je 0x21bc
cmpl $0x9, 0x28(%r14)
jne 0x2153
jmp 0x21bf
movl 0x20(%r14), %eax
cmpl %ebx, %eax
jne 0x219b
leaq 0x244f(%rip), %r13      # 0x45ca
cmpq $0x0, 0x8(%r14)
jne 0x218f
movl $0x1, %edi
movq %r13, %rsi
callq 0x164b
movq 0x8(%r14), %r14
movl 0x20(%r14), %eax
cmpl %ebx, %eax
je 0x217b
xorl %esi, %esi
cmpl %ebx, %eax
seta %sil
movq %r12, %rdi
movl $0x1, %edx
leaq 0x243a(%rip), %rcx      # 0x45ec
callq 0x1600
jmp 0x1f92
xorl %r14d, %r14d
testq %r14, %r14
jne 0x1f92
jmp 0x227d
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %r15
movq %r15, %rdx
callq 0x1f69
movq %rax, %r12
movq 0x30(%r14), %rsi
movq %r15, %rdi
movq %rax, %rdx
callq 0x25c9
movq 0x18(%rsp), %rax
cmpl $0x7, 0x28(%r14)
je 0x2212
movq 0x8(%r14), %r14
testq %r14, %r14
je 0x220f
cmpl $0x9, 0x28(%r14)
jne 0x21f6
jmp 0x2212
xorl %r14d, %r14d
testq %r14, %r14
cmoveq %r12, %rax
movq 0x8(%rsp), %r12
jne 0x1f8d
jmp 0x22c6
movl $0x1, 0x28(%r15)
movq 0x8(%r14), %r14
jmp 0x1f8d
testq %r13, %r13
jne 0x227d
leaq 0x20(%rsp), %r15
movl $0x100, %edx            # imm = 0x100
movq %r15, %rdi
xorl %esi, %esi
callq 0x10d0
movq 0x30(%r14), %rcx
leaq 0x2269(%rip), %rdx      # 0x44c7
movl $0x100, %esi            # imm = 0x100
movq %r15, %rdi
xorl %eax, %eax
callq 0x10b0
movq %r12, %rdi
movl $0x2, %esi
movq %r15, %rdx
callq 0x1628
addq $0x28, %r13
movq %r13, %rax
jmp 0x22c6
leaq 0x222d(%rip), %rdx      # 0x44ba
movq %r12, %rdi
movl $0x2, %esi
callq 0x1628
xorl %eax, %eax
jmp 0x22c6
movq %r12, %rdi
movq %r14, %rsi
jmp 0x22ad
movq 0x8(%r14), %rsi
movq %r12, %rdi
movq %r15, %rdx
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x22d8
movq %rbx, %rax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq