// File: /workspace/github_cpp_makefile_star2_O3/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 0x1b685
callq 0x177cc
movq %rax, %r12
callq 0x1781b
cmpl $0x1e, (%rax)
jne 0x1b48c
callq 0x177cc
callq 0x1781b
cmpl $0x3, (%rax)
jne 0x1b52f
callq 0x177cc
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 0x9542
movl $0x28, %edi
callq 0x4210
movq %rax, %r12
movl 0x10(%r13), %ecx
incl %ecx
movq %rax, %rdi
movl $0x2, %esi
movq $-0x1, %rdx
callq 0x9542
movl 0x8(%rbx), %edi
decl %edi
movl 0x10(%r13), %esi
callq 0x17891
movl 0x10(%r13), %r9d
incl %r9d
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %r12, %rcx
movq %rax, %r8
callq 0x9048
jmp 0x1b673
callq 0x1ce62
movq %rax, %r15
movl 0x10(%rax), %edi
callq 0x15fe0
testb %al, %al
je 0x1b6f0
callq 0x1781b
cmpl $0x1e, (%rax)
jne 0x1b5c0
callq 0x177cc
callq 0x1781b
cmpl $0x3, (%rax)
jne 0x1b60d
callq 0x177cc
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 0x9542
movl 0x8(%rbx), %edi
decl %edi
movl 0x10(%r13), %esi
callq 0x17891
movl 0x10(%r13), %r9d
incl %r9d
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %r12, %rcx
movq %rax, %r8
callq 0x9048
jmp 0x1b673
callq 0x1ce62
movq %rax, %r13
movl 0x10(%rax), %edi
callq 0x15fe0
testb %al, %al
je 0x1b74d
movq %r13, -0x30(%rbp)
callq 0x1781b
cmpl $0x3, (%rax)
jne 0x1b77b
callq 0x177cc
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 0x9542
movl 0x8(%rbx), %edi
decl %edi
movl 0x10(%r15), %esi
callq 0x17891
movl 0x10(%r15), %r9d
incl %r9d
movq %r14, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq -0x30(%rbp), %rcx
movq %rax, %r8
callq 0x9048
jmp 0x1b673
callq 0x1781b
cmpl $0x3, (%rax)
jne 0x1b7a1
callq 0x177cc
movq %rax, %r12
movl $0x40, %edi
callq 0x4210
movq %rax, %r14
movl 0x8(%rbx), %edi
decl %edi
movl 0x10(%r12), %esi
callq 0x17891
movl 0x10(%r12), %r8d
incl %r8d
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %rax, %rcx
callq 0x9006
jmp 0x1b673
callq 0x1ce62
movq %rax, %r13
movl 0x10(%rax), %edi
callq 0x15fe0
testb %al, %al
je 0x1b7c7
callq 0x1781b
cmpl $0x3, (%rax)
jne 0x1b830
callq 0x177cc
movq %rax, %r12
movl $0x40, %edi
callq 0x4210
movq %rax, %r14
movl 0x8(%rbx), %edi
decl %edi
movl 0x10(%r12), %esi
callq 0x17891
movl 0x10(%r12), %r9d
incl %r9d
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %r13, %rcx
movq %rax, %r8
callq 0x9048
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 0x1781b
movl 0x10(%rax), %r12d
movl 0x8(%rbx), %ebx
callq 0x1781b
movl 0x10(%rax), %esi
movl %ebx, %edi
callq 0x17891
incl %r12d
incl %r15d
leaq 0xff52(%rip), %rcx      # 0x2b610
addq $0x10, %rcx
movq %rcx, (%r14)
movw $0x100, 0x8(%r14)       # imm = 0x100
movq %rax, 0x10(%r14)
movq $0x0, 0x18(%r14)
leaq 0x7c76(%rip), %rax      # 0x23355
movq %rax, 0x20(%r14)
movl %r15d, 0x28(%r14)
movl %r12d, 0x2c(%r14)
jmp 0x1b827
movl $0x38, %edi
callq 0x4110
movq %rax, %r14
movl 0x8(%rbx), %r12d
movl 0xc(%r15), %ebx
movl %r12d, %edi
movl %ebx, %esi
callq 0x17891
incl %ebx
incl %r12d
movl 0x10(%r15), %ecx
leaq 0xfef1(%rip), %rdx      # 0x2b610
addq $0x10, %rdx
movq %rdx, (%r14)
movw $0x0, 0x8(%r14)
movq %rax, 0x10(%r14)
leaq 0x7c53(%rip), %rax      # 0x2338b
movq %rax, 0x18(%r14)
movq $0x0, 0x20(%r14)
movl %r12d, 0x28(%r14)
jmp 0x1b81f
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 0x17891
incl %ebx
incl %r15d
movl 0x10(%r12), %ecx
jmp 0x1b7ef
movl $0x28, %edi
callq 0x4110
movq %rax, %r14
movl 0x10(%r12), %ebx
movq 0x10af4(%rip), %rdi     # 0x2c288
movq %rbx, %rsi
callq 0x1d5e0
jmp 0x1b851
movl $0x28, %edi
callq 0x4110
movq %rax, %r14
movl 0x10(%r12), %ebx
movq 0x10ace(%rip), %rdi     # 0x2c288
movq %rbx, %rsi
callq 0x1d5e0
jmp 0x1b851
movl $0x38, %edi
callq 0x4110
movq %rax, %r14
movl 0x8(%rbx), %r15d
movl 0xc(%r13), %ebx
movl %r15d, %edi
movl %ebx, %esi
callq 0x17891
incl %ebx
incl %r15d
movl 0x10(%r13), %ecx
leaq 0xfe1a(%rip), %rdx      # 0x2b610
addq $0x10, %rdx
movq %rdx, (%r14)
movw $0x0, 0x8(%r14)
movq %rax, 0x10(%r14)
leaq 0x7b7c(%rip), %rax      # 0x2338b
movq %rax, 0x18(%r14)
movq $0x0, 0x20(%r14)
movl %r15d, 0x28(%r14)
movl %ebx, 0x2c(%r14)
movl %ecx, 0x30(%r14)
leaq 0xfe0a(%rip), %rsi      # 0x2b638
jmp 0x1b88d
movl $0x28, %edi
callq 0x4110
movq %rax, %r14
movl 0x10(%r12), %ebx
movq 0x10a3f(%rip), %rdi     # 0x2c288
movq %rbx, %rsi
callq 0x1d5e0
incl %ebx
movq (%rax), %rax
leaq 0xfd73(%rip), %rcx      # 0x2b5d0
addq $0x10, %rcx
movq %rcx, (%r14)
movq %rax, 0x8(%r14)
leaq 0x38be(%rip), %rax      # 0x1f12d
movq %rax, 0x10(%r14)
leaq 0x7b20(%rip), %rax      # 0x2339a
movq %rax, 0x18(%r14)
movl %ebx, 0x20(%r14)
movl %ebx, 0x24(%r14)
leaq 0xfd6b(%rip), %rsi      # 0x2b5f8
movq 0x10744(%rip), %rdx     # 0x2bfd8
movq %r14, %rdi
callq 0x4350
jmp 0x1b8cf
jmp 0x1b8cf
jmp 0x1b8b2
jmp 0x1b8cf
jmp 0x1b8cf
jmp 0x1b8cf
jmp 0x1b8dc
movq %rax, %rbx
movq %r13, %rdi
jmp 0x1b8c0
movq %rax, %rbx
movq %r12, %rdi
jmp 0x1b8c0
movq %rax, %rbx
movq %r15, %rdi
callq 0x41e0
jmp 0x1b8df
jmp 0x1b8cf
jmp 0x1b8dc
jmp 0x1b8dc
jmp 0x1b8dc
movq %rax, %rbx
movq %r14, %rdi
callq 0x4190
jmp 0x1b8e7
movq %rax, %rbx
movq %r14, %rdi
callq 0x41e0
movq %rbx, %rdi
callq 0x4380