// File: AshOlogn[P]ash-language-interpreter/src/assignmenteval.cpp
// Function: evaluateArrayAssignmentExpression(ArrayAssignmentExpressionNode*)
ParseData evaluateArrayAssignmentExpression(ArrayAssignmentExpressionNode* node) {

	//extract instance fields
	SymbolTable* symbolTable = node->symbolTable;
	int32_t index = (int32_t) node->index->evaluate().value.integer;

	if(node->array->evalType == ARRAY_T) {

		Array* arr = (Array*) node->array->evaluate().value.allocated;
		int32_t length = (int32_t) arr->length;

		//calculate final index and throw exception if out of bounds
		int32_t finalIndex = (index < 0) ? index + length : index;
		if(finalIndex < 0 || finalIndex > length-1)
			throw OutOfBoundsException(true, length, index, node->context, node->startLine, node->endLine);

		ParseData value = node->value->evaluate();

		//assign array index
		arr->values[finalIndex] = value;

		//return a deep copy of assigned value
		ParseData retValue;
		retValue.type = value.type;

		if(value.type == STRING_T) {
			retValue.value.allocated = (void*) copyString((char*) value.value.allocated);
		} else if(value.type == DOUBLE_T) {
			retValue.value.floatingPoint = value.value.floatingPoint;
		} else {
			retValue.value.integer = value.value.integer;
		}

		return retValue;

	} else {

		char* str = (char*) node->array->evaluate().value.allocated;
		int32_t length = strlen(str);

		//calculate final index and throw exception if out of bounds
		int32_t finalIndex = (index < 0) ? index + length : index;
		if(finalIndex < 0 || finalIndex > length-1)
			throw OutOfBoundsException(false, length, index, node->context, node->startLine, node->endLine);

		ParseData value = node->value->evaluate();
		str[finalIndex] = (unsigned char) value.value.integer;

		return value;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rdi
movq (%rdi), %rax
callq *(%rax)
movl %eax, -0x38(%rbp)
movq %rdx, -0x30(%rbp)
movq -0x30(%rbp), %rax
movl %eax, -0x24(%rbp)
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
cmpl $0xc, 0x10(%rax)
jne 0x1d03d
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdi
movq (%rdi), %rax
callq *(%rax)
movl %eax, -0x50(%rbp)
movq %rdx, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x24(%rbp)
jge 0x1cf32
movl -0x24(%rbp), %eax
addl -0x54(%rbp), %eax
movl %eax, -0x9c(%rbp)
jmp 0x1cf3b
movl -0x24(%rbp), %eax
movl %eax, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
movl %eax, -0x58(%rbp)
cmpl $0x0, -0x58(%rbp)
jl 0x1cf57
movl -0x58(%rbp), %eax
movl -0x54(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jle 0x1cfcb
movl $0x28, %edi
callq 0x4130
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xa8(%rbp)
movl -0x54(%rbp), %edx
movl -0x24(%rbp), %ecx
movq -0x18(%rbp), %rax
movq 0x30(%rax), %r8
movl 0x8(%rax), %r9d
movl 0xc(%rax), %esi
movq %rsp, %rax
movl %esi, (%rax)
movl $0x1, %esi
callq 0x1d170
jmp 0x1cf94
movq -0xa8(%rbp), %rdi
leaq 0x36806(%rip), %rsi     # 0x537a8
leaq -0x15919(%rip), %rdx    # 0x7690
callq 0x43f0
movq -0xa8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
callq 0x41d0
jmp 0x1d162
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rdi
movq (%rdi), %rax
callq *(%rax)
movl %eax, -0x78(%rbp)
movq %rdx, -0x70(%rbp)
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x58(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq -0x78(%rbp), %rcx
movq %rcx, (%rax)
movq -0x70(%rbp), %rcx
movq %rcx, 0x8(%rax)
movl -0x78(%rbp), %eax
movl %eax, -0x10(%rbp)
cmpl $0xb, -0x78(%rbp)
jne 0x1d01c
movq -0x70(%rbp), %rdi
callq 0x2fb10
movq %rax, -0x8(%rbp)
jmp 0x1d038
cmpl $0xa, -0x78(%rbp)
jne 0x1d02e
movsd -0x70(%rbp), %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0x1d036
movq -0x70(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x1d038
jmp 0x1d152
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdi
movq (%rdi), %rax
callq *(%rax)
movl %eax, -0x90(%rbp)
movq %rdx, -0x88(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rdi
callq 0x4100
movl %eax, -0x94(%rbp)
cmpl $0x0, -0x24(%rbp)
jge 0x1d088
movl -0x24(%rbp), %eax
addl -0x94(%rbp), %eax
movl %eax, -0xac(%rbp)
jmp 0x1d091
movl -0x24(%rbp), %eax
movl %eax, -0xac(%rbp)
movl -0xac(%rbp), %eax
movl %eax, -0x98(%rbp)
cmpl $0x0, -0x98(%rbp)
jl 0x1d0b9
movl -0x98(%rbp), %eax
movl -0x94(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jle 0x1d12a
movl $0x28, %edi
callq 0x4130
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xb8(%rbp)
movl -0x94(%rbp), %edx
movl -0x24(%rbp), %ecx
movq -0x18(%rbp), %rax
movq 0x30(%rax), %r8
movl 0x8(%rax), %r9d
movl 0xc(%rax), %esi
movq %rsp, %rax
movl %esi, (%rax)
xorl %esi, %esi
callq 0x1d170
jmp 0x1d0f6
movq -0xb8(%rbp), %rdi
leaq 0x366a4(%rip), %rsi     # 0x537a8
leaq -0x15a7b(%rip), %rdx    # 0x7690
callq 0x43f0
movq -0xb8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
callq 0x41d0
jmp 0x1d162
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rdi
movq (%rdi), %rax
callq *(%rax)
movl %eax, -0x10(%rbp)
movq %rdx, -0x8(%rbp)
movq -0x8(%rbp), %rax
movb %al, %dl
movq -0x80(%rbp), %rax
movslq -0x98(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x10(%rbp), %eax
movq -0x8(%rbp), %rdx
addq $0xc0, %rsp
popq %rbp
retq
movq -0x60(%rbp), %rdi
callq 0x4420
nopl (%rax,%rax)