// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %r14
movq 0x20(%rdi), %rdi
movq (%rdi), %rax
callq *(%rax)
movq %rdx, %rbx
movq 0x18(%r14), %rdi
movl 0x10(%rdi), %r12d
movq (%rdi), %rax
callq *(%rax)
movq %rdx, %r15
cmpl $0xc, %r12d
jne 0x10d6a
movl 0x4(%r15), %r12d
movl %ebx, %r13d
sarl $0x1f, %r13d
andl %r12d, %r13d
addl %ebx, %r13d
js 0x10da8
cmpl %r12d, %r13d
jge 0x10da8
movq 0x28(%r14), %rdi
movq (%rdi), %rax
callq *(%rax)
movq 0x8(%r15), %rcx
movl %r13d, %esi
shlq $0x4, %rsi
movl %eax, (%rcx,%rsi)
movq %rdx, 0x8(%rcx,%rsi)
cmpl $0xb, %eax
jne 0x10d99
movq %rdx, %rdi
callq 0x175e8
movq %rax, %rdx
movl $0xb, %eax
jmp 0x10d99
movq %r15, %rdi
callq 0x40e0
movq %rax, %r12
movl %ebx, %r13d
sarl $0x1f, %r13d
andl %r12d, %r13d
addl %ebx, %r13d
js 0x10dd6
cmpl %r12d, %r13d
jge 0x10dd6
movq 0x28(%r14), %rdi
movq (%rdi), %rax
callq *(%rax)
movl %r13d, %ecx
movb %dl, (%r15,%rcx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x28, %edi
callq 0x4110
movq 0x8(%r14), %rcx
movq 0x30(%r14), %rdx
leaq 0x1a90f(%rip), %rsi     # 0x2b6d0
addq $0x10, %rsi
movq %rsi, (%rax)
movq %rdx, 0x8(%rax)
movb $0x1, 0x10(%rax)
movl %r12d, 0x14(%rax)
jmp 0x10e02
movl $0x28, %edi
callq 0x4110
movq 0x8(%r14), %rcx
movq 0x30(%r14), %rdx
leaq 0x1a8e1(%rip), %rsi     # 0x2b6d0
addq $0x10, %rsi
movq %rsi, (%rax)
movq %rdx, 0x8(%rax)
movb $0x0, 0x10(%rax)
movl %r12d, 0x14(%rax)
movl %ebx, 0x18(%rax)
movq %rcx, 0x1c(%rax)
leaq 0x1a8e8(%rip), %rsi     # 0x2b6f8
movq 0x1b1c1(%rip), %rdx     # 0x2bfd8
movq %rax, %rdi
callq 0x4350
nop