// File: AshOlogn[P]ash-language-interpreter/src/memberaccesseval.cpp
// Function: elementHelper(ArrayAccessNode*, ParseData, int)
ParseData elementHelper(ArrayAccessNode* node, ParseData arr, int32_t index) {
  
  ParseData d;
	ParseDataType type = arr.type;
	
	if(arr.type == STRING_T) {

		d.type = CHAR_T;
		char* str = (char*) arr.value.allocated;
		int32_t len = strlen(str);
		
		int32_t ind = index;
		if(ind < 0)
			ind += len+1;

		//if index is out-of-bounds, throw exception
		if(ind < 0 || ind > len-1) {
			throw OutOfBoundsException(false, len, index, copyString(node->context), node->startLine, node->endLine);
		}

		d.value.integer = str[ind];
		return d;
		
	} else if(type == ARRAY_T) {

		Array* array = (Array*) arr.value.allocated;
		int32_t len = array->length;
		ParseData* values = array->values;

		int32_t ind = index;
		if(ind < 0)
			ind += len; 
		
		//if index is out-of-bounds, throw exception
		if(ind < 0 || ind > len-1) {
			throw OutOfBoundsException(true, len, index, copyString(node->context), node->startLine, node->endLine);
		}

		ParseData val = values[ind];
		return val;
	}

  return d;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movl %esi, -0x20(%rbp)
movq %rdx, -0x18(%rbp)
movq %rdi, -0x28(%rbp)
movl %ecx, -0x2c(%rbp)
movl -0x20(%rbp), %eax
movl %eax, -0x44(%rbp)
cmpl $0xb, -0x20(%rbp)
jne 0x299bc
movl $0x8, -0x40(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
callq 0x4100
movl %eax, -0x54(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, -0x58(%rbp)
cmpl $0x0, -0x58(%rbp)
jge 0x298dc
movl -0x54(%rbp), %eax
addl $0x1, %eax
addl -0x58(%rbp), %eax
movl %eax, -0x58(%rbp)
cmpl $0x0, -0x58(%rbp)
jl 0x298f3
movl -0x58(%rbp), %eax
movl -0x54(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jle 0x29996
movl $0x28, %edi
callq 0x4130
movq %rax, -0xa0(%rbp)
movl -0x54(%rbp), %eax
movl %eax, -0x98(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, -0x94(%rbp)
movq -0x28(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x2fb10
movq %rax, -0x90(%rbp)
jmp 0x2992c
movq -0x90(%rbp), %r8
movl -0x94(%rbp), %ecx
movl -0x98(%rbp), %edx
movq -0xa0(%rbp), %rdi
movq -0x28(%rbp), %rax
movl 0x8(%rax), %r9d
movl 0xc(%rax), %esi
movq %rsp, %rax
movl %esi, (%rax)
xorl %esi, %esi
callq 0x1d170
jmp 0x2995f
movq -0xa0(%rbp), %rdi
leaq 0x29e3b(%rip), %rsi     # 0x537a8
leaq -0x222e4(%rip), %rdx    # 0x7690
callq 0x43f0
movq -0xa0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
callq 0x41d0
jmp 0x29b0a
movq -0x50(%rbp), %rax
movslq -0x58(%rbp), %rcx
movsbq (%rax,%rcx), %rax
movq %rax, -0x38(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x10(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x29afa
cmpl $0xc, -0x44(%rbp)
jne 0x29ae8
movq -0x18(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x74(%rbp)
movq -0x70(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x80(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, -0x84(%rbp)
cmpl $0x0, -0x84(%rbp)
jge 0x29a05
movl -0x74(%rbp), %eax
addl -0x84(%rbp), %eax
movl %eax, -0x84(%rbp)
cmpl $0x0, -0x84(%rbp)
jl 0x29a22
movl -0x84(%rbp), %eax
movl -0x74(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jle 0x29ac5
movl $0x28, %edi
callq 0x4130
movq %rax, -0xb8(%rbp)
movl -0x74(%rbp), %eax
movl %eax, -0xb0(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, -0xac(%rbp)
movq -0x28(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x2fb10
movq %rax, -0xa8(%rbp)
jmp 0x29a5b
movq -0xa8(%rbp), %r8
movl -0xac(%rbp), %ecx
movl -0xb0(%rbp), %edx
movq -0xb8(%rbp), %rdi
movq -0x28(%rbp), %rax
movl 0x8(%rax), %r9d
movl 0xc(%rax), %esi
movq %rsp, %rax
movl %esi, (%rax)
movl $0x1, %esi
callq 0x1d170
jmp 0x29a91
movq -0xb8(%rbp), %rdi
leaq 0x29d09(%rip), %rsi     # 0x537a8
leaq -0x22416(%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 0x29b0a
movq -0x80(%rbp), %rax
movslq -0x84(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, -0x10(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x8(%rbp)
jmp 0x29afa
jmp 0x29aea
movq -0x40(%rbp), %rax
movq %rax, -0x10(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x8(%rbp)
movl -0x10(%rbp), %eax
movq -0x8(%rbp), %rdx
addq $0xc0, %rsp
popq %rbp
retq
movq -0x60(%rbp), %rdi
callq 0x4420
nopw %cs:(%rax,%rax)