// File: /workspace/github_c_makefile_star2_O2/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 $0x118, %rsp            # imm = 0x118
movq %rdx, 0x8(%rsp)
movq %rsi, %r12
movq %rdi, (%rsp)
leaq 0x28(%r12), %r13
movl 0x28(%r12), %eax
addl $-0x3, %eax
cmpl $0xb, %eax
ja 0x21e5
leaq 0x2475(%rip), %rcx      # 0x4368
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x8(%rsp), %rax
movl $0x1, 0x28(%rax)
movq 0x8(%r12), %r12
jmp 0x1ed6
movq (%rsp), %rdi
pushq $0x1
popq %rsi
pushq $0x2
popq %rdx
leaq 0x26fb(%rip), %rcx      # 0x461b
callq 0x15c8
movq 0x30(%r12), %rbx
movq %rbx, %rdi
callq 0x1080
movq %rax, %rbp
pushq $0x1
popq %rdi
pushq $0x38
popq %rsi
callq 0x1130
movq %rax, %r15
movups (%r12), %xmm0
movups 0x10(%r12), %xmm1
movups 0x20(%r12), %xmm2
movups %xmm0, (%rax)
movups %xmm1, 0x10(%rax)
movups %xmm2, 0x20(%rax)
movq 0x30(%r12), %rax
movq %rax, 0x30(%r15)
movq 0x8(%rsp), %r14
testq %r14, %r14
je 0x20a7
movq 0x8(%r14), %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x293c
testq %rax, %rax
jne 0x2048
movq 0x10(%r14), %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x293c
testq %rax, %rax
jne 0x2048
movq 0x18(%r14), %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x293c
testq %rax, %rax
jne 0x20b4
movq (%r14), %r14
jmp 0x1f6d
movq 0x8(%r12), %rsi
movq (%rsp), %rdi
movq 0x8(%rsp), %rdx
callq 0x21fa
movl 0x20(%r12), %ebx
leaq 0x23f6(%rip), %rcx      # 0x43d8
cmpq %rcx, %rax
je 0x2052
movl %ebx, %ebp
movq %r12, %r14
movl %ebx, %eax
cmpl %eax, %ebp
jne 0x209f
movq 0x8(%r14), %r12
testq %r12, %r12
jne 0x2012
pushq $0x1
popq %rdi
leaq 0x2602(%rip), %rsi      # 0x460b
callq 0x15f9
movq 0x8(%r14), %r12
movl 0x20(%r12), %ecx
cmpl %ebx, %ecx
movl %ebp, %eax
cmoval %ecx, %eax
cmpl $0x7, 0x28(%r12)
cmovnel %ebp, %eax
movl %ecx, %ebp
movq %r12, %r14
cmpl %eax, %ecx
jae 0x1fee
jmp 0x1ed6
movq 0x8(%rsp), %rax
andl $0x0, 0x28(%rax)
movq 0x8(%r12), %r12
jmp 0x1ed6
movl $0x1, 0x28(%r15)
jmp 0x20c3
movl %ebx, %ecx
leaq 0x256f(%rip), %r14      # 0x45ca
cmpl %ebx, %ecx
jne 0x2081
movq 0x8(%r12), %rax
testq %rax, %rax
jne 0x2079
pushq $0x1
popq %rdi
movq %r14, %rsi
callq 0x15f9
movq 0x8(%r12), %rax
movl 0x20(%rax), %ecx
movq %rax, %r12
jmp 0x205b
seta %al
movzbl %al, %esi
movq (%rsp), %rdi
pushq $0x1
popq %rdx
leaq 0x2557(%rip), %rcx      # 0x45ec
callq 0x15c8
jmp 0x1ed6
movq %r14, %r12
jmp 0x1ed6
movq %r15, %rdi
callq 0x1030
xorl %r15d, %r15d
jmp 0x20c3
movl 0x28(%rax), %ecx
movl %ecx, 0x28(%r15)
movq 0x30(%rax), %rax
movq %rax, 0x30(%r15)
movq 0x8(%r12), %rsi
testq %rsi, %rsi
je 0x2110
cmpl $0x4, 0x28(%rsi)
jne 0x2110
movq (%rsp), %rdi
movq 0x8(%rsp), %rbx
movq %rbx, %rdx
callq 0x1eb9
movq %rax, %r13
movq 0x30(%r12), %rsi
movq %rbx, %rdi
movq %rax, %rdx
callq 0x247c
movq %r12, %rdi
callq 0x24d8
movq %rax, %r12
testq %rax, %rax
jne 0x1ed6
jmp 0x21e5
movq (%r12), %rax
testq %rax, %rax
je 0x2159
cmpl $0x7, 0x28(%rax)
jne 0x2159
movl (%r13), %eax
cmpl $0xe, %eax
ja 0x2159
movl $0x500e, %ecx           # imm = 0x500E
btl %eax, %ecx
jae 0x2159
movq (%rsp), %rdi
movq %r12, %rsi
movq 0x8(%rsp), %rdx
callq 0x21fa
movq %r12, %rdi
callq 0x24d8
movq %rax, %r12
testq %rax, %rax
jne 0x1ed6
jmp 0x219c
testq %r15, %r15
jne 0x219c
leaq 0x10(%rsp), %rbx
movl $0x100, %edx            # imm = 0x100
movq %rbx, %rdi
xorl %esi, %esi
callq 0x10d0
movq 0x30(%r12), %rcx
leaq 0x2349(%rip), %rdx      # 0x44c7
movl $0x100, %esi            # imm = 0x100
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10b0
pushq $0x2
popq %rsi
movq (%rsp), %rdi
movq %rbx, %rdx
callq 0x15d8
addq $0x28, %r15
movq %r15, %r13
jmp 0x21e5
movq (%rsp), %rdi
movq %r12, %rsi
jmp 0x21b7
movq 0x8(%r12), %rsi
movq (%rsp), %rdi
movq 0x8(%rsp), %rdx
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x21fa
leaq 0x22e4(%rip), %rdx      # 0x44ba
pushq $0x2
popq %rsi
movq (%rsp), %rdi
callq 0x15d8
xorl %r13d, %r13d
movq %r13, %rax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq