// File: /workspace/github_cpp_makefile_star2_O2/pranavmodx[P]modlang/src/evaluator.cpp
// Function: Evaluator::evalCallExpression(Object*, std::vector<Object*, std::allocator<Object*>>&)
Object *Evaluator::evalCallExpression(Object *fn, std::vector<Object *> &args)
{
	if (fn->type() != FUNCTION_OBJ && fn->type() != BUILTIN_OBJ)
		return new Error("error: not a function -> " + fn->type());

	if (fn->type() == BUILTIN_OBJ)
		return ((Builtin *)fn)->function(args);

	if (((Function *)fn)->parameters.size() != args.size())
		return new Error(
			"error: argument length (" + std::to_string(args.size()) +
			") not equal to parameter length (" + std::to_string(((Function *)fn)->parameters.size()) + ")");

	Environment *extendedEnv = extendFunctionEnv((Function *)fn, args, ((Function *)fn)->env);

	Object *evaluated = Eval(((Function *)fn)->body, extendedEnv);

	if (evaluated->type() == RETURN_VALUE_OBJ)
		return ((ReturnValue *)evaluated)->value;

	return evaluated;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0xe0, %rsp
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
movq (%rsi), %rax
movq %rsp, %r12
movq %r12, %rdi
callq *(%rax)
leaq 0x162e8(%rip), %rsi     # 0x2d1b8
movq %r12, %rdi
callq 0x10766
testb %al, %al
je 0x16f6a
movq (%r14), %rax
leaq 0x20(%rsp), %rdi
movq %r14, %rsi
callq *(%rax)
leaq 0x16404(%rip), %rsi     # 0x2d2f8
leaq 0x20(%rsp), %r12
movq %r12, %rdi
callq 0x10766
movl %eax, %ebp
movq %r12, %rdi
callq 0x5388
movq %rsp, %rdi
callq 0x5388
testb %bpl, %bpl
je 0x16f72
pushq $0x28
popq %rdi
callq 0x51d0
movq %rax, %rbx
movq (%r14), %rax
movq %rsp, %rdi
movq %r14, %rsi
callq *(%rax)
leaq 0x5c7d(%rip), %rsi      # 0x1cbb2
leaq 0xc0(%rsp), %rdi
movq %rsp, %rdx
callq 0xd257
leaq 0xc0(%rsp), %rsi
movq %rbx, %rdi
callq 0x12cc2
leaq 0xc0(%rsp), %rdi
callq 0x5388
movq %rsp, %rdi
jmp 0x170f3
movq %rsp, %rdi
callq 0x5388
movq (%r14), %rax
movq %rsp, %r12
movq %r12, %rdi
movq %r14, %rsi
callq *(%rax)
leaq 0x16371(%rip), %rsi     # 0x2d2f8
movq %r12, %rdi
callq 0xa7b4
movl %eax, %ebp
movq %r12, %rdi
callq 0x5388
testb %bpl, %bpl
je 0x16fad
movq %r15, %rdi
callq *0x8(%r14)
movq %rax, %rbx
jmp 0x170f8
movq 0x10(%r14), %rax
subq 0x8(%r14), %rax
movq 0x8(%r15), %r12
subq (%r15), %r12
cmpq %r12, %rax
jne 0x1701b
movq 0x28(%r14), %rcx
movq %r14, %rsi
movq %r15, %rdx
callq 0x1837e
movq 0x20(%r14), %rsi
movq %rbx, %rdi
movq %rax, %rdx
callq 0x15ad8
movq %rax, %rbx
movq (%rax), %rax
movq %rsp, %r14
movq %r14, %rdi
movq %rbx, %rsi
callq *(%rax)
leaq 0x162e1(%rip), %rsi     # 0x2d2d8
movq %r14, %rdi
callq 0xa7b4
movl %eax, %ebp
movq %r14, %rdi
callq 0x5388
testb %bpl, %bpl
je 0x170f8
movq 0x8(%rbx), %rbx
jmp 0x170f8
sarq $0x3, %r12
pushq $0x28
popq %rdi
callq 0x51d0
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
movq %r12, %rsi
callq 0x187d0
leaq 0x575b(%rip), %rsi      # 0x1c799
leaq 0x80(%rsp), %rdi
leaq 0x60(%rsp), %rdx
callq 0xd257
leaq 0x5b75(%rip), %rdx      # 0x1cbcc
leaq 0x20(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0xd12a
movq 0x10(%r14), %rsi
subq 0x8(%r14), %rsi
sarq $0x3, %rsi
leaq 0x40(%rsp), %rdi
callq 0x187d0
movq %rsp, %rdi
leaq 0x20(%rsp), %rsi
leaq 0x40(%rsp), %rdx
callq 0xd149
leaq 0x5a6e(%rip), %rdx      # 0x1cb06
leaq 0xa0(%rsp), %rdi
movq %rsp, %rsi
callq 0xd12a
leaq 0xa0(%rsp), %rsi
movq %rbx, %rdi
callq 0x12cc2
leaq 0xa0(%rsp), %rdi
callq 0x5388
movq %rsp, %rdi
callq 0x5388
leaq 0x40(%rsp), %rdi
callq 0x5388
leaq 0x20(%rsp), %rdi
callq 0x5388
leaq 0x80(%rsp), %rdi
callq 0x5388
leaq 0x60(%rsp), %rdi
callq 0x5388
movq %rbx, %rax
addq $0xe0, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
leaq 0xa0(%rsp), %rdi
callq 0x5388
jmp 0x17120
movq %rax, %r14
movq %rsp, %rdi
callq 0x5388
jmp 0x1712d
movq %rax, %r14
leaq 0x40(%rsp), %rdi
callq 0x5388
jmp 0x1713c
movq %rax, %r14
leaq 0x20(%rsp), %rdi
callq 0x5388
jmp 0x1714b
movq %rax, %r14
leaq 0x80(%rsp), %rdi
callq 0x5388
leaq 0x60(%rsp), %rdi
jmp 0x1717e
movq %rax, %r14
jmp 0x17158
jmp 0x17185
movq %rax, %r14
leaq 0xc0(%rsp), %rdi
callq 0x5388
jmp 0x1717b
movq %rax, %r14
movq %rsp, %rdi
callq 0x5388
jmp 0x17188
movq %rax, %r14
pushq $0x28
popq %rsi
movq %rbx, %rdi
callq 0x51e0
movq %r14, %rdi
callq 0x5330
movq %rax, %r14
movq %rsp, %rdi
callq 0x5388
jmp 0x17193