// File: /workspace/github_cpp_makefile_star2_O2/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 0x18cc6
callq 0x154a8
movq %rax, %r12
callq 0x154d4
cmpl $0x1e, (%rax)
jne 0x18ade
callq 0x154a8
callq 0x154d4
cmpl $0x3, (%rax)
jne 0x18b7a
callq 0x154a8
movq %rax, %r15
pushq $0x40
popq %rdi
callq 0x41f0
movq %rax, %r14
pushq $0x28
popq %rdi
callq 0x41f0
movq %rax, %r13
movl 0x10(%r12), %ecx
incl %ecx
pushq $0x2
popq %rsi
movq %rax, %rdi
xorl %edx, %edx
callq 0x8252
pushq $0x28
popq %rdi
callq 0x41f0
movq %rax, %r12
movl 0x10(%r15), %ecx
incl %ecx
pushq $0x2
popq %rsi
pushq $-0x1
popq %rdx
movq %rax, %rdi
callq 0x8252
movl 0x8(%rbx), %edi
decl %edi
movl 0x10(%r15), %esi
callq 0x15504
movl 0x10(%r15), %r9d
incl %r9d
movq %r14, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq %r12, %rcx
movq %rax, %r8
callq 0x7e3e
jmp 0x18cb4
callq 0x1a3b7
movq %rax, %r15
movl 0x10(%rax), %edi
callq 0x13eac
testb %al, %al
je 0x18d2c
callq 0x154d4
cmpl $0x1e, (%rax)
jne 0x18c05
callq 0x154a8
callq 0x154d4
cmpl $0x3, (%rax)
jne 0x18c50
callq 0x154a8
movq %rax, %r12
pushq $0x40
popq %rdi
callq 0x41f0
movq %rax, %r14
pushq $0x28
popq %rdi
callq 0x41f0
movq %rax, %r13
movl 0x10(%r12), %ecx
incl %ecx
pushq $0x2
popq %rsi
pushq $-0x1
popq %rdx
movq %rax, %rdi
callq 0x8252
movl 0x8(%rbx), %edi
decl %edi
movl 0x10(%r12), %esi
callq 0x15504
movl 0x10(%r12), %r9d
incl %r9d
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %r13, %rcx
movq %rax, %r8
callq 0x7e3e
jmp 0x18cb4
callq 0x1a3b7
movq %rax, %r13
movl 0x10(%rax), %edi
callq 0x13eac
testb %al, %al
je 0x18d83
movq %r13, -0x30(%rbp)
callq 0x154d4
cmpl $0x3, (%rax)
jne 0x18daa
callq 0x154a8
movq %rax, %r13
pushq $0x40
popq %rdi
callq 0x41f0
movq %rax, %r14
pushq $0x28
popq %rdi
callq 0x41f0
movq %rax, %r15
movl 0x10(%r12), %ecx
incl %ecx
pushq $0x2
popq %rsi
movq %rax, %rdi
xorl %edx, %edx
callq 0x8252
movl 0x8(%rbx), %edi
decl %edi
movl 0x10(%r13), %esi
callq 0x15504
movl 0x10(%r13), %r9d
incl %r9d
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq -0x30(%rbp), %rcx
movq %rax, %r8
callq 0x7e3e
jmp 0x18cb4
callq 0x154d4
cmpl $0x3, (%rax)
jne 0x18dce
callq 0x154a8
movq %rax, %r12
pushq $0x40
popq %rdi
callq 0x41f0
movq %rax, %r14
movl 0x8(%rbx), %edi
decl %edi
movl 0x10(%r12), %esi
callq 0x15504
movl 0x10(%r12), %r8d
incl %r8d
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %rax, %rcx
callq 0x7dfc
jmp 0x18cb4
callq 0x1a3b7
movq %rax, %r13
movl 0x10(%rax), %edi
callq 0x13eac
testb %al, %al
je 0x18df2
callq 0x154d4
cmpl $0x3, (%rax)
jne 0x18e55
callq 0x154a8
movq %rax, %r12
pushq $0x40
popq %rdi
callq 0x41f0
movq %rax, %r14
movl 0x8(%rbx), %edi
decl %edi
movl 0x10(%r12), %esi
callq 0x15504
movl 0x10(%r12), %r9d
incl %r9d
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %r13, %rcx
movq %rax, %r8
callq 0x7e3e
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x38
popq %rdi
callq 0x4110
movq %rax, %r14
movl 0x8(%rbx), %r15d
callq 0x154d4
movl 0x10(%rax), %r12d
movl 0x8(%rbx), %ebx
callq 0x154d4
movl 0x10(%rax), %esi
movl %ebx, %edi
callq 0x15504
incl %r12d
incl %r15d
leaq 0x11913(%rip), %rcx     # 0x2a610
addq $0x10, %rcx
movq %rcx, (%r14)
movw $0x100, 0x8(%r14)       # imm = 0x100
movq %rax, 0x10(%r14)
andq $0x0, 0x18(%r14)
leaq 0x863a(%rip), %rax      # 0x21355
movq %rax, 0x20(%r14)
movl %r15d, 0x28(%r14)
movl %r12d, 0x2c(%r14)
jmp 0x18e4c
pushq $0x38
popq %rdi
callq 0x4110
movq %rax, %r14
movl 0x8(%rbx), %r12d
movl 0xc(%r15), %ebx
movl %r12d, %edi
movl %ebx, %esi
callq 0x15504
movl 0x10(%r15), %ecx
leaq 0x118bc(%rip), %rdx     # 0x2a610
addq $0x10, %rdx
movq %rdx, (%r14)
andw $0x0, 0x8(%r14)
incl %ebx
movq %rax, 0x10(%r14)
leaq 0x861d(%rip), %rax      # 0x2138b
movq %rax, 0x18(%r14)
andq $0x0, 0x20(%r14)
incl %r12d
movl %r12d, 0x28(%r14)
jmp 0x18e44
pushq $0x38
popq %rdi
callq 0x4110
movq %rax, %r14
movl 0x8(%rbx), %r15d
movq %r13, %r12
movl 0xc(%r13), %ebx
movl %r15d, %edi
movl %ebx, %esi
callq 0x15504
movl 0x10(%r12), %ecx
jmp 0x18e13
pushq $0x28
popq %rdi
callq 0x4110
movq %rax, %r14
movl 0x10(%r12), %ebx
movq 0x124af(%rip), %rdi     # 0x2b270
movq %rbx, %rsi
callq 0x6f18
jmp 0x18e74
pushq $0x28
popq %rdi
callq 0x4110
movq %rax, %r14
movl 0x10(%r12), %ebx
movq 0x1248b(%rip), %rdi     # 0x2b270
movq %rbx, %rsi
callq 0x6f18
jmp 0x18e74
pushq $0x38
popq %rdi
callq 0x4110
movq %rax, %r14
movl 0x8(%rbx), %r15d
movl 0xc(%r13), %ebx
movl %r15d, %edi
movl %ebx, %esi
callq 0x15504
movl 0x10(%r13), %ecx
leaq 0x117f6(%rip), %rdx     # 0x2a610
addq $0x10, %rdx
movq %rdx, (%r14)
andw $0x0, 0x8(%r14)
incl %ebx
movq %rax, 0x10(%r14)
leaq 0x8557(%rip), %rax      # 0x2138b
movq %rax, 0x18(%r14)
andq $0x0, 0x20(%r14)
incl %r15d
movl %r15d, 0x28(%r14)
movl %ebx, 0x2c(%r14)
movl %ecx, 0x30(%r14)
leaq 0x117e5(%rip), %rsi     # 0x2a638
jmp 0x18eb0
pushq $0x28
popq %rdi
callq 0x4110
movq %rax, %r14
movl 0x10(%r12), %ebx
movq 0x12404(%rip), %rdi     # 0x2b270
movq %rbx, %rsi
callq 0x6f18
incl %ebx
movq (%rax), %rax
leaq 0x11750(%rip), %rcx     # 0x2a5d0
addq $0x10, %rcx
movq %rcx, (%r14)
movq %rax, 0x8(%r14)
leaq 0x429b(%rip), %rax      # 0x1d12d
movq %rax, 0x10(%r14)
leaq 0x84fd(%rip), %rax      # 0x2139a
movq %rax, 0x18(%r14)
movl %ebx, 0x20(%r14)
movl %ebx, 0x24(%r14)
leaq 0x11748(%rip), %rsi     # 0x2a5f8
movq 0x12121(%rip), %rdx     # 0x2afd8
movq %r14, %rdi
callq 0x4310
jmp 0x18ef2
jmp 0x18ef2
jmp 0x18edd
jmp 0x18ef2
jmp 0x18ef2
jmp 0x18ef2
jmp 0x18eff
movq %rax, %rbx
movq %r15, %rdi
jmp 0x18ee3
movq %rax, %rbx
movq %r12, %rdi
jmp 0x18ee3
movq %rax, %rbx
movq %r13, %rdi
callq 0x41d0
jmp 0x18f02
jmp 0x18ef2
jmp 0x18eff
jmp 0x18eff
jmp 0x18eff
movq %rax, %rbx
movq %r14, %rdi
callq 0x4180
jmp 0x18f0a
movq %rax, %rbx
movq %r14, %rdi
callq 0x41d0
movq %rbx, %rdi
callq 0x4340