// File: AshOlogn[P]ash-language-interpreter/src/functioneval.cpp
// Function: evaluateFunctionExpression(FunctionExpressionNode*)
ParseData evaluateFunctionExpression(FunctionExpressionNode* node) {
	
	SymbolTable* symbolTable = node->symbolTable;

	//function information
	Function* function = node->function;
	uint32_t numArgs = node->numArgs;
	AbstractExpressionNode** args = node->arguments;
	char** argNames = function->argNames;
	vector<AbstractStatementNode*>* body = function->body;
	
	
	//evaluate parameters
	ParseData* arguments = (ParseData*) malloc(sizeof(ParseData) * numArgs);
	for(uint32_t i = 0; i < numArgs; i++) {
		arguments[i] = args[i]->evaluate();
	}

	//enter function scope
	symbolTable->enterNewScope();

	//declare parameters with computed arguments
	for(uint32_t i = 0; i < numArgs; i++) {
		symbolTable->declare(argNames[i], arguments[i]);
	}

	//execute statements in the body
	vector<AbstractStatementNode*>::iterator it;
	bool* returnFlag = node->isReturned;
	*returnFlag = false;
	for(it = body->begin(); !(*returnFlag) && it != body->end(); it++) {
		(*it)->execute();
	}

	//return value placed in correct address by return statement
	symbolTable->leaveScope();
	return *(node->returnValue);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, -0x2c(%rbp)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x48(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, %edi
shlq $0x4, %rdi
callq 0x4310
movq %rax, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0x292a5
movq -0x38(%rbp), %rax
movl -0x54(%rbp), %ecx
movq (%rax,%rcx,8), %rdi
movq (%rdi), %rax
callq *(%rax)
movl %eax, -0x68(%rbp)
movq %rdx, -0x60(%rbp)
movq -0x50(%rbp), %rax
movl -0x54(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq -0x68(%rbp), %rcx
movq %rcx, (%rax)
movq -0x60(%rbp), %rcx
movq %rcx, 0x8(%rax)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x2925e
movq -0x20(%rbp), %rdi
callq 0x2bd00
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0x293ae
movq -0x20(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0x40(%rbp), %rax
movl -0x6c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0xf8(%rbp)
leaq -0x91(%rbp), %rdi
movq %rdi, -0xf0(%rbp)
callq 0x4430
movq -0xf8(%rbp), %rsi
movq -0xf0(%rbp), %rdx
leaq -0x90(%rbp), %rdi
callq 0x4350
jmp 0x2930d
movq -0x100(%rbp), %rdi
movq -0x50(%rbp), %rax
movl -0x6c(%rbp), %ecx
shlq $0x4, %rcx
movups (%rax,%rcx), %xmm0
movaps %xmm0, -0xc0(%rbp)
movl -0xc0(%rbp), %edx
movq -0xb8(%rbp), %rcx
leaq -0x90(%rbp), %rsi
callq 0x2bda0
jmp 0x29345
leaq -0x90(%rbp), %rdi
callq 0x4170
leaq -0x91(%rbp), %rdi
callq 0x4290
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x292b5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
jmp 0x2939d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x4170
leaq -0x91(%rbp), %rdi
callq 0x4290
jmp 0x2948d
leaq -0xc8(%rbp), %rdi
callq 0x5300
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rax
movb $0x0, (%rax)
movq -0x48(%rbp), %rdi
callq 0x5320
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0xd0(%rbp), %rcx
xorl %eax, %eax
testb $0x1, (%rcx)
movb %al, -0x101(%rbp)
jne 0x2942e
movq -0x48(%rbp), %rdi
callq 0x5390
movq %rax, -0xe0(%rbp)
leaq -0xc8(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x5350
movb %al, -0x101(%rbp)
movb -0x101(%rbp), %al
testb $0x1, %al
jne 0x2943a
jmp 0x29465
leaq -0xc8(%rbp), %rdi
callq 0x53c0
movq (%rax), %rdi
movq (%rdi), %rax
callq *(%rax)
leaq -0xc8(%rbp), %rdi
xorl %esi, %esi
callq 0x53e0
movq %rax, -0xe8(%rbp)
jmp 0x293f1
movq -0x20(%rbp), %rdi
callq 0x2bd70
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rax
movups (%rax), %xmm0
movaps %xmm0, -0x10(%rbp)
movl -0x10(%rbp), %eax
movq -0x8(%rbp), %rdx
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
movq -0xa0(%rbp), %rdi
callq 0x4420
nopl (%rax)