// File: /workspace/github_cpp_makefile_star2_O2/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 0xf4d2
movl 0x4(%r15), %r12d
movl %ebx, %r13d
sarl $0x1f, %r13d
andl %r12d, %r13d
addl %ebx, %r13d
js 0xf510
cmpl %r12d, %r13d
jge 0xf510
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 0xf501
movq %rdx, %rdi
callq 0x152e4
movq %rax, %rdx
pushq $0xb
popq %rax
jmp 0xf501
movq %r15, %rdi
callq 0x40c0
movq %rax, %r12
movl %ebx, %r13d
sarl $0x1f, %r13d
andl %r12d, %r13d
addl %ebx, %r13d
js 0xf53c
cmpl %r12d, %r13d
jge 0xf53c
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
pushq $0x28
popq %rdi
callq 0x4110
movq 0x8(%r14), %rcx
movq 0x30(%r14), %rdx
leaq 0x1b1a9(%rip), %rsi     # 0x2a6d0
addq $0x10, %rsi
movq %rsi, (%rax)
movq %rdx, 0x8(%rax)
movb $0x1, 0x10(%rax)
movl %r12d, 0x14(%rax)
jmp 0xf566
pushq $0x28
popq %rdi
callq 0x4110
movq 0x8(%r14), %rcx
movq 0x30(%r14), %rdx
leaq 0x1b17d(%rip), %rsi     # 0x2a6d0
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 0x1b184(%rip), %rsi     # 0x2a6f8
movq 0x1ba5d(%rip), %rdx     # 0x2afd8
movq %rax, %rdi
callq 0x4310
nop