// File: /workspace/github_cpp_makefile_star2_O1/AshOlogn[P]ash-language-interpreter/src/parser.cpp
// Function: handleMemberAccess(AbstractExpressionNode*)
AbstractExpressionNode* handleMemberAccess(AbstractExpressionNode* head) {

  if(head->evalType != STRING_T && head->evalType != ARRAY_T) {
      
    //first write message
    const char* arrayAccessErrorMessage = "A value must be of array or string type to be indexed";
    throw StaticTypeError(head->startLine+1, peek()->line+1, getCodeLineBlock(head->startLine, peek()->line), arrayAccessErrorMessage);
  }
  
  Token* leftBracketToken = consume(); //consume [

  //if next value is colon, this is a slice with implicit start = 0
  if(peek()->type == COLON) {

    Token* colonToken = consume(); //consume :
    
    //start = 0
    ParseData startIndex;
    startIndex.type = INT32_T;
    startIndex.value.integer = 0;

    //if next value is ], implicit end = -1
    if(peek()->type == RIGHT_BRACKET) {

      Token* rightBracketToken = consume(); //consume ]
      ParseData endIndex;
      endIndex.type = INT32_T;
      endIndex.value.integer = (int32_t) -1;
      
      return new ArrayAccessNode(head, new LiteralNode(startIndex, leftBracketToken->line+1), 
                                  new LiteralNode(endIndex, rightBracketToken->line+1), getCodeLineBlock(head->startLine-1, rightBracketToken->line), rightBracketToken->line+1);

    } else {

      //defined end index
      AbstractExpressionNode* end = evalExpression();
      
      //make sure that the expression evaluates to an integer
      if(!typecheckMemberAccessExpression(end->evalType)) {
        throw StaticTypeError(head->startLine+1, end->endLine+1, getCodeLineBlock(head->startLine, end->endLine), "array indexing", end->evalType);
      }

      if(peek()->type != RIGHT_BRACKET) {
        
        //!!! If [ does not have a corresponding ], throw an error
        throw ParseSyntaxError(leftBracketToken->line+1, codeLines->at(leftBracketToken->line), "[", "Array access operation must end with ']'");
        
      } else {
  
        Token* rightBracketToken = consume(); //consume ]
        return new ArrayAccessNode(head, new LiteralNode(startIndex, leftBracketToken->line+1), end, getCodeLineBlock(head->startLine-1, rightBracketToken->line), rightBracketToken->line+1);
      }
    }

  } else {

    //defined start index
    AbstractExpressionNode* start = evalExpression();
    
    //make sure that the expression evaluates to an integer
    if(!typecheckMemberAccessExpression(start->evalType)) {
      throw StaticTypeError(head->startLine+1, start->endLine+1, getCodeLineBlock(head->startLine, start->endLine), "array indexing", start->evalType);
    }

    //accessing a slice with explicit start
    if(peek()->type == COLON) {
  
      consume(); //consume :

      //end = -1 implied
      if(peek()->type == RIGHT_BRACKET) {

        Token* rightBracketToken = consume(); //consume ]
        ParseData endIndex;
        endIndex.type = INT32_T;
        endIndex.value.integer = (int32_t) -1;
    
        return new ArrayAccessNode(head, start, new LiteralNode(endIndex, rightBracketToken->line+1), getCodeLineBlock(head->startLine-1, rightBracketToken->line), rightBracketToken->line+1);

      } else {

        //defined end index
        AbstractExpressionNode* end = evalExpression();
        
        //make sure that the expression evaluates to an integer
        if(!typecheckMemberAccessExpression(end->evalType)) {
          throw StaticTypeError(head->startLine+1, end->endLine+1, getCodeLineBlock(head->startLine, end->endLine), "array indexing", end->evalType);            
        }
          
        if(peek()->type != RIGHT_BRACKET) {
          
          //If [ does not have a corresponding ], throw an error
          throw ParseSyntaxError(leftBracketToken->line+1, codeLines->at(leftBracketToken->line), "[", "Array access operation must end with ']'");

        } else {
          Token* rightBracketToken = consume(); //consume ]
          return new ArrayAccessNode(head, start, end, getCodeLineBlock(head->startLine-1, rightBracketToken->line), rightBracketToken->line+1);
        }
      }

    } else {
      
      //accessing single value
      if(peek()->type != RIGHT_BRACKET) {
        
        //!!! If [ does not have a corresponding ], throw an error
        throw ParseSyntaxError(leftBracketToken->line+1, codeLines->at(leftBracketToken->line), "[", "Array access operation must end with ']'");

      } else {
        Token* rightBracketToken = consume(); //consume ]
        return new ArrayAccessNode(head, start, getCodeLineBlock(head->startLine-1, rightBracketToken->line), rightBracketToken->line+1);
      }
    }
  }

  return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movl 0x10(%rdi), %eax
addl $-0xb, %eax
cmpl $0x2, %eax
jae 0x1b6bf
callq 0x1789c
movq %rax, %r12
callq 0x178eb
cmpl $0x1e, (%rax)
jne 0x1b4c6
callq 0x1789c
callq 0x178eb
cmpl $0x3, (%rax)
jne 0x1b569
callq 0x1789c
movq %rax, %r13
movl $0x40, %edi
callq 0x4210
movq %rax, %r14
movl $0x28, %edi
callq 0x4210
movq %rax, %r15
movl 0x10(%r12), %ecx
incl %ecx
movq %rax, %rdi
movl $0x2, %esi
xorl %edx, %edx
callq 0x969e
movl $0x28, %edi
callq 0x4210
movq %rax, %r12
movl 0x10(%r13), %ecx
incl %ecx
movq %rax, %rdi
movl $0x2, %esi
movq $-0x1, %rdx
callq 0x969e
movl 0x8(%rbx), %edi
decl %edi
movl 0x10(%r13), %esi
callq 0x17961
movl 0x10(%r13), %r9d
incl %r9d
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %r12, %rcx
movq %rax, %r8
callq 0x9190
jmp 0x1b6ad
callq 0x1ce9f
movq %rax, %r15
movl 0x10(%rax), %edi
callq 0x16068
testb %al, %al
je 0x1b72a
callq 0x178eb
cmpl $0x1e, (%rax)
jne 0x1b5fa
callq 0x1789c
callq 0x178eb
cmpl $0x3, (%rax)
jne 0x1b647
callq 0x1789c
movq %rax, %r13
movl $0x40, %edi
callq 0x4210
movq %rax, %r14
movl $0x28, %edi
callq 0x4210
movq %rax, %r12
movl 0x10(%r13), %ecx
incl %ecx
movq %rax, %rdi
movl $0x2, %esi
movq $-0x1, %rdx
callq 0x969e
movl 0x8(%rbx), %edi
decl %edi
movl 0x10(%r13), %esi
callq 0x17961
movl 0x10(%r13), %r9d
incl %r9d
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %r12, %rcx
movq %rax, %r8
callq 0x9190
jmp 0x1b6ad
callq 0x1ce9f
movq %rax, %r13
movl 0x10(%rax), %edi
callq 0x16068
testb %al, %al
je 0x1b787
movq %r13, -0x30(%rbp)
callq 0x178eb
cmpl $0x3, (%rax)
jne 0x1b7b5
callq 0x1789c
movq %rax, %r15
movl $0x40, %edi
callq 0x4210
movq %rax, %r14
movl $0x28, %edi
callq 0x4210
movq %rax, %r13
movl 0x10(%r12), %ecx
incl %ecx
movq %rax, %rdi
movl $0x2, %esi
xorl %edx, %edx
callq 0x969e
movl 0x8(%rbx), %edi
decl %edi
movl 0x10(%r15), %esi
callq 0x17961
movl 0x10(%r15), %r9d
incl %r9d
movq %r14, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq -0x30(%rbp), %rcx
movq %rax, %r8
callq 0x9190
jmp 0x1b6ad
callq 0x178eb
cmpl $0x3, (%rax)
jne 0x1b7db
callq 0x1789c
movq %rax, %r12
movl $0x40, %edi
callq 0x4210
movq %rax, %r14
movl 0x8(%rbx), %edi
decl %edi
movl 0x10(%r12), %esi
callq 0x17961
movl 0x10(%r12), %r8d
incl %r8d
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %rax, %rcx
callq 0x914a
jmp 0x1b6ad
callq 0x1ce9f
movq %rax, %r13
movl 0x10(%rax), %edi
callq 0x16068
testb %al, %al
je 0x1b801
callq 0x178eb
cmpl $0x3, (%rax)
jne 0x1b86a
callq 0x1789c
movq %rax, %r12
movl $0x40, %edi
callq 0x4210
movq %rax, %r14
movl 0x8(%rbx), %edi
decl %edi
movl 0x10(%r12), %esi
callq 0x17961
movl 0x10(%r12), %r9d
incl %r9d
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %r13, %rcx
movq %rax, %r8
callq 0x9190
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x38, %edi
callq 0x4110
movq %rax, %r14
movl 0x8(%rbx), %r15d
callq 0x178eb
movl 0x10(%rax), %r12d
movl 0x8(%rbx), %ebx
callq 0x178eb
movl 0x10(%rax), %esi
movl %ebx, %edi
callq 0x17961
incl %r12d
incl %r15d
leaq 0xff18(%rip), %rcx      # 0x2b610
addq $0x10, %rcx
movq %rcx, (%r14)
movw $0x100, 0x8(%r14)       # imm = 0x100
movq %rax, 0x10(%r14)
movq $0x0, 0x18(%r14)
leaq 0x7c48(%rip), %rax      # 0x23361
movq %rax, 0x20(%r14)
movl %r15d, 0x28(%r14)
movl %r12d, 0x2c(%r14)
jmp 0x1b861
movl $0x38, %edi
callq 0x4110
movq %rax, %r14
movl 0x8(%rbx), %r12d
movl 0xc(%r15), %ebx
movl %r12d, %edi
movl %ebx, %esi
callq 0x17961
incl %ebx
incl %r12d
movl 0x10(%r15), %ecx
leaq 0xfeb7(%rip), %rdx      # 0x2b610
addq $0x10, %rdx
movq %rdx, (%r14)
movw $0x0, 0x8(%r14)
movq %rax, 0x10(%r14)
leaq 0x7c25(%rip), %rax      # 0x23397
movq %rax, 0x18(%r14)
movq $0x0, 0x20(%r14)
movl %r12d, 0x28(%r14)
jmp 0x1b859
movl $0x38, %edi
callq 0x4110
movq %rax, %r14
movl 0x8(%rbx), %r15d
movq %r13, %r12
movl 0xc(%r13), %ebx
movl %r15d, %edi
movl %ebx, %esi
callq 0x17961
incl %ebx
incl %r15d
movl 0x10(%r12), %ecx
jmp 0x1b829
movl $0x28, %edi
callq 0x4110
movq %rax, %r14
movl 0x10(%r12), %ebx
movq 0x10aba(%rip), %rdi     # 0x2c288
movq %rbx, %rsi
callq 0x1d630
jmp 0x1b88b
movl $0x28, %edi
callq 0x4110
movq %rax, %r14
movl 0x10(%r12), %ebx
movq 0x10a94(%rip), %rdi     # 0x2c288
movq %rbx, %rsi
callq 0x1d630
jmp 0x1b88b
movl $0x38, %edi
callq 0x4110
movq %rax, %r14
movl 0x8(%rbx), %r15d
movl 0xc(%r13), %ebx
movl %r15d, %edi
movl %ebx, %esi
callq 0x17961
incl %ebx
incl %r15d
movl 0x10(%r13), %ecx
leaq 0xfde0(%rip), %rdx      # 0x2b610
addq $0x10, %rdx
movq %rdx, (%r14)
movw $0x0, 0x8(%r14)
movq %rax, 0x10(%r14)
leaq 0x7b4e(%rip), %rax      # 0x23397
movq %rax, 0x18(%r14)
movq $0x0, 0x20(%r14)
movl %r15d, 0x28(%r14)
movl %ebx, 0x2c(%r14)
movl %ecx, 0x30(%r14)
leaq 0xfdd0(%rip), %rsi      # 0x2b638
jmp 0x1b8c7
movl $0x28, %edi
callq 0x4110
movq %rax, %r14
movl 0x10(%r12), %ebx
movq 0x10a05(%rip), %rdi     # 0x2c288
movq %rbx, %rsi
callq 0x1d630
incl %ebx
movq (%rax), %rax
leaq 0xfd39(%rip), %rcx      # 0x2b5d0
addq $0x10, %rcx
movq %rcx, (%r14)
movq %rax, 0x8(%r14)
leaq 0x38a4(%rip), %rax      # 0x1f14d
movq %rax, 0x10(%r14)
leaq 0x7af2(%rip), %rax      # 0x233a6
movq %rax, 0x18(%r14)
movl %ebx, 0x20(%r14)
movl %ebx, 0x24(%r14)
leaq 0xfd31(%rip), %rsi      # 0x2b5f8
movq 0x1070a(%rip), %rdx     # 0x2bfd8
movq %r14, %rdi
callq 0x4350
jmp 0x1b909
jmp 0x1b909
jmp 0x1b8ec
jmp 0x1b909
jmp 0x1b909
jmp 0x1b909
jmp 0x1b916
movq %rax, %rbx
movq %r13, %rdi
jmp 0x1b8fa
movq %rax, %rbx
movq %r12, %rdi
jmp 0x1b8fa
movq %rax, %rbx
movq %r15, %rdi
callq 0x41e0
jmp 0x1b919
jmp 0x1b909
jmp 0x1b916
jmp 0x1b916
jmp 0x1b916
movq %rax, %rbx
movq %r14, %rdi
callq 0x4190
jmp 0x1b921
movq %rax, %rbx
movq %r14, %rdi
callq 0x41e0
movq %rbx, %rdi
callq 0x4380