// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x140, %rsp            # imm = 0x140
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x18(%rbp), %rax
movl 0x28(%rax), %eax
addl $-0x3, %eax
movl %eax, %ecx
movq %rcx, -0x138(%rbp)
subl $0xb, %eax
ja 0x2a24
movq -0x138(%rbp), %rax
leaq 0x3b2f(%rip), %rcx      # 0x6358
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x2a40
movq %rax, -0x8(%rbp)
jmp 0x2a30
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x20(%rbp), %rdx
callq 0x2a40
movq %rax, -0x8(%rbp)
jmp 0x2a30
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x2ce0
movq %rax, -0x18(%rbp)
jmp 0x27ff
movq -0x10(%rbp), %rdi
movl $0x2, %esi
leaq 0x3bd0(%rip), %rdx      # 0x6464
callq 0x1870
movq $0x0, -0x8(%rbp)
jmp 0x2a30
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x2e10
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x2928
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x4, 0x28(%rax)
jne 0x2928
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x20(%rbp), %rdx
callq 0x27e0
movq %rax, -0x30(%rbp)
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rsi
movq -0x30(%rbp), %rdx
callq 0x2f80
movq -0x18(%rbp), %rdi
callq 0x3000
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x2923
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x2a30
jmp 0x27ff
movq -0x18(%rbp), %rax
cmpq $0x0, (%rax)
je 0x297f
movq -0x18(%rbp), %rax
movq (%rax), %rax
cmpl $0x7, 0x28(%rax)
jne 0x297f
movq -0x18(%rbp), %rdi
callq 0x3040
cmpl $0x0, %eax
je 0x297f
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x3090
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x297a
movq -0x28(%rbp), %rax
addq $0x28, %rax
movq %rax, -0x8(%rbp)
jmp 0x2a30
jmp 0x27ff
cmpq $0x0, -0x28(%rbp)
jne 0x29d0
leaq -0x130(%rbp), %rdi
xorl %esi, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x10b0
leaq -0x130(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rcx
movl $0x100, %esi            # imm = 0x100
leaq 0x3abd(%rip), %rdx      # 0x6471
movb $0x0, %al
callq 0x1090
movq -0x10(%rbp), %rdi
leaq -0x130(%rbp), %rdx
movl $0x2, %esi
callq 0x1870
jmp 0x29d2
jmp 0x29d4
movq -0x28(%rbp), %rax
addq $0x28, %rax
movq %rax, -0x8(%rbp)
jmp 0x2a30
movq -0x20(%rbp), %rax
movl $0x0, 0x28(%rax)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x18(%rbp)
jmp 0x27ff
movq -0x20(%rbp), %rax
movl $0x1, 0x28(%rax)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x20(%rbp), %rdx
callq 0x27e0
movq %rax, -0x8(%rbp)
jmp 0x2a30
movq -0x18(%rbp), %rax
addq $0x28, %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
nopl (%rax)