// File: /workspace/github_cpp_makefile_star2_O1/AshOlogn[P]ash-language-interpreter/src/parser.cpp
// Function: evalAssignment()
AbstractExpressionNode* evalAssignment() {
  
  AbstractExpressionNode* head = evalLogicOr();
  VariableNode* var = dynamic_cast<VariableNode*>(head);
	ArrayAccessNode* arrVar = dynamic_cast<ArrayAccessNode*>(head);
  
  if((var || arrVar) && isAssignmentOperatorTokenType(peek()->type)) {
  
    //if just variable, make sure it is already declared in some scope
		if(var && !symbolTable->isDeclared(var->variable)) {
      throw StaticVariableScopeError(var->startLine, var->variable, getCodeLineBlock(var->startLine-1, var->startLine-1), false);
		}
    
    //get variable's type
    ParseDataType type = head->evalType;
    
    Token* assignmentToken = consume();
    AbstractExpressionNode* next = evalAssignment();
		
		//make sure the assignment operation is even valid
		if(assignmentToken->type != EQ) {

			ParseOperatorType opType;
			switch(assignmentToken->type) {
				case ADD_EQ: opType = ADD_OP; break;
				case SUBTRACT_EQ: opType = SUBTRACT_OP; break;
				case EXPONENT_EQ: opType = EXPONENT_OP; break;
				case MULTIPLY_EQ: opType = MULTIPLY_OP; break;
				case DIVIDE_EQ: opType = DIVIDE_OP; break;
				case AND_EQ: opType = AND_OP; break;
				case XOR_EQ: opType = XOR_OP; break;
				case OR_EQ: opType = OR_OP; break;
				default: opType = INVALID_OP;
			}

			if(isArithmeticParseOperatorType(opType)) {

				if(!typecheckArithmeticExpression(opType, type, next->evalType)) {
					uint32_t startLine = head->startLine;
					throw StaticTypeError(startLine, next->endLine, getCodeLineBlock(startLine-1, next->endLine-1), 
																toStringParseOperatorType(opType), type, next->evalType); 
				}

			} else {

				if(!typecheckBitLogicalExpression(opType, type, next->evalType)) {
					uint32_t startLine = head->startLine;
					throw StaticTypeError(startLine, next->endLine, getCodeLineBlock(startLine-1, next->endLine-1), 
																toStringParseOperatorType(opType), type, next->evalType); 
				}
			}
		}
    
    switch(assignmentToken->type) {
      case EQ: break;
      case ADD_EQ: next = new ArithmeticOperatorNode(ADD_OP, head, next); break; 
      case SUBTRACT_EQ: next = new ArithmeticOperatorNode(SUBTRACT_OP, head, next); break;
      case EXPONENT_EQ: next = new ArithmeticOperatorNode(EXPONENT_OP, head, next); break;
      case MULTIPLY_EQ: next = new ArithmeticOperatorNode(MULTIPLY_OP, head, next); break;
      case DIVIDE_EQ: next = new ArithmeticOperatorNode(DIVIDE_OP, head, next); break;
      case AND_EQ: {
        if(type == BOOL_T)
          next = new BitLogicalOperatorNode(AND_OP, head, next);
        else 
          next = new BitLogicalOperatorNode(BIT_AND_OP, head, next);
        break;
      }
      
      case XOR_EQ: {
        if(type == BOOL_T)
          next = new BitLogicalOperatorNode(XOR_OP, head, next);
        else 
          next = new BitLogicalOperatorNode(BIT_XOR_OP, head, next);
        break;
      }
      
      case OR_EQ: {
        if(type == BOOL_T)
          next = new BitLogicalOperatorNode(OR_OP, head, next);
        else 
          next = new BitLogicalOperatorNode(BIT_OR_OP, head, next);
        break;
      }
    }
    
    //now make sure that implicit type cast is valid
    if(!typecheckImplicitCastExpression(next->evalType, type)) {
      throw StaticCastError(head->startLine, next->endLine, getCodeLineBlock(head->startLine-1, next->endLine-1), next->evalType, type, false);
    }
    
		if(var) {
			return new AssignmentExpressionNode(var->variable, type, next, symbolTable, var->startLine);
		} else {
			return new ArrayAssignmentExpressionNode(arrVar->array, arrVar->start, next, getCodeLineBlock(arrVar->startLine-1, next->endLine-1), symbolTable);
		}
    
  } else {
    return head;
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
callq 0x1cd8b
movq %rax, %rbx
leaq 0xec69(%rip), %r14      # 0x2bb28
leaq 0xec02(%rip), %rdx      # 0x2bac8
movq %rax, %rdi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x4230
movq %rax, %r15
leaq 0xebbb(%rip), %rdx      # 0x2ba98
movq %rbx, %rdi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x4230
movq %rax, %r13
movq %r15, %rax
orq %r13, %rax
je 0x1d2bd
callq 0x178eb
movl (%rax), %edi
callq 0x643b
testb %al, %al
je 0x1d2bd
leaq -0x48(%rbp), %rax
testq %r15, %r15
je 0x1cf50
movq 0xf373(%rip), %r14      # 0x2c290
leaq -0x58(%rbp), %r12
movq %rax, (%r12)
movq 0x20(%r15), %rsi
movq 0x28(%r15), %rdx
addq %rsi, %rdx
movq %r12, %rdi
callq 0x8ac2
movq %r14, %rdi
movq %r12, %rsi
callq 0x16646
movl %eax, %r14d
xorb $0x1, %r14b
leaq -0x48(%rbp), %rax
jmp 0x1cf53
xorl %r14d, %r14d
testq %r15, %r15
je 0x1cf66
movq -0x58(%rbp), %rdi
cmpq %rax, %rdi
je 0x1cf66
callq 0x41e0
testb %r14b, %r14b
jne 0x1d2cf
movl 0x10(%rbx), %eax
movl %eax, -0x34(%rbp)
callq 0x1789c
movq %rax, %r12
callq 0x1ce9f
movq %rax, -0x30(%rbp)
movl (%r12), %eax
addl $-0x1f, %eax
cmpl $0x9, %eax
ja 0x1cfe2
leaq 0x605b(%rip), %rcx      # 0x22ff4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0xf, %r14d
jmp 0x1cfe8
movl $0x10, %r14d
jmp 0x1cfe8
movl $0xd, %r14d
jmp 0x1cfe8
movl $0x1c, %r14d
jmp 0x1cfe8
movl $0x1d, %r14d
jmp 0x1cfe8
movl $0xb, %r14d
jmp 0x1cfe8
movl $0xc, %r14d
jmp 0x1cfe8
movl $0x1e, %r14d
jmp 0x1cfe8
movl $0x2b, %r14d
movl %r14d, %edi
callq 0x8202
movq -0x30(%rbp), %rcx
movl 0x10(%rcx), %edx
movl %r14d, %edi
testb %al, %al
je 0x1d041
movl -0x34(%rbp), %esi
callq 0x16176
testb %al, %al
jne 0x1d051
movl 0x8(%rbx), %r12d
movl $0x38, %edi
callq 0x4110
movq %rax, %rbx
movq -0x30(%rbp), %rax
movl 0xc(%rax), %r13d
leal -0x1(%r12), %edi
leal -0x1(%r13), %esi
callq 0x17961
movq %rax, %r15
movl %r14d, %edi
callq 0x82cf
jmp 0x1d3e6
movl -0x34(%rbp), %esi
callq 0x16261
testb %al, %al
je 0x1d3b4
movl (%r12), %eax
addl $-0x20, %eax
cmpl $0x8, %eax
ja 0x1d1ff
leaq 0x5fb4(%rip), %rcx      # 0x2301c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
movl -0x34(%rbp), %r14d
jmpq *%rax
movl $0x30, %edi
callq 0x4210
movq %rax, %r12
movq %rax, %rdi
movl $0xf, %esi
movq %rbx, %rdx
movq -0x30(%rbp), %rcx
callq 0x8f24
jmp 0x1d207
movl $0x30, %edi
callq 0x4210
movq %rax, %r12
cmpl $0x9, %r14d
jne 0x1d1bd
movq %r12, %rdi
movl $0x1c, %esi
movq %rbx, %rdx
movq -0x30(%rbp), %rcx
callq 0x8f82
jmp 0x1d207
movl $0x30, %edi
callq 0x4210
movq %rax, %r12
movq %rax, %rdi
movl $0x10, %esi
movq %rbx, %rdx
movq -0x30(%rbp), %rcx
callq 0x8f24
jmp 0x1d207
movl $0x30, %edi
callq 0x4210
movq %rax, %r12
cmpl $0x9, %r14d
jne 0x1d1d3
movq %r12, %rdi
movl $0x1d, %esi
movq %rbx, %rdx
movq -0x30(%rbp), %rcx
callq 0x8f82
jmp 0x1d207
movl $0x30, %edi
callq 0x4210
movq %rax, %r12
cmpl $0x9, %r14d
jne 0x1d1e9
movq %r12, %rdi
movl $0x1e, %esi
movq %rbx, %rdx
movq -0x30(%rbp), %rcx
callq 0x8f82
jmp 0x1d207
movl $0x30, %edi
callq 0x4210
movq %rax, %r12
movq %rax, %rdi
movl $0xb, %esi
movq %rbx, %rdx
movq -0x30(%rbp), %rcx
callq 0x8f24
jmp 0x1d207
movl $0x30, %edi
callq 0x4210
movq %rax, %r12
movq %rax, %rdi
movl $0xc, %esi
movq %rbx, %rdx
movq -0x30(%rbp), %rcx
callq 0x8f24
jmp 0x1d207
movl $0x30, %edi
callq 0x4210
movq %rax, %r12
movq %rax, %rdi
movl $0xd, %esi
movq %rbx, %rdx
movq -0x30(%rbp), %rcx
callq 0x8f24
jmp 0x1d207
movq %r12, %rdi
movl $0x19, %esi
movq %rbx, %rdx
movq -0x30(%rbp), %rcx
callq 0x8f82
jmp 0x1d207
movq %r12, %rdi
movl $0x1a, %esi
movq %rbx, %rdx
movq -0x30(%rbp), %rcx
callq 0x8f82
jmp 0x1d207
movq %r12, %rdi
movl $0x1b, %esi
movq %rbx, %rdx
movq -0x30(%rbp), %rcx
callq 0x8f82
jmp 0x1d207
movl -0x34(%rbp), %r14d
movq -0x30(%rbp), %r12
movl 0x10(%r12), %edi
movl %r14d, %esi
callq 0x1607f
testb %al, %al
je 0x1d344
testq %r15, %r15
je 0x1d27b
movq %r12, %r13
movl $0x50, %edi
callq 0x4210
movq %rax, %rbx
leaq -0x68(%rbp), %r12
movq %r12, -0x10(%r12)
movq 0x20(%r15), %rsi
movq 0x28(%r15), %rdx
addq %rsi, %rdx
leaq -0x78(%rbp), %rdi
callq 0x8ac2
movq 0xf03b(%rip), %r8       # 0x2c290
movl 0x8(%r15), %r9d
leaq -0x78(%rbp), %rsi
movq %rbx, %rdi
movl %r14d, %edx
movq %r13, %rcx
callq 0x8b5c
movq -0x78(%rbp), %rdi
cmpq %r12, %rdi
je 0x1d2bd
callq 0x41e0
jmp 0x1d2bd
movl $0x40, %edi
callq 0x4210
movq %rax, %rbx
movq 0x18(%r13), %r14
movq 0x20(%r13), %r15
movl 0x8(%r13), %edi
decl %edi
movl 0xc(%r12), %esi
decl %esi
callq 0x17961
movq 0xefe7(%rip), %r9       # 0x2c290
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
movq %rax, %r8
callq 0x8c22
movq %rbx, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x38, %edi
callq 0x4110
movq %rax, %rbx
movl 0x8(%r15), %r14d
leaq -0x88(%rbp), %r12
movq %r12, -0x10(%r12)
movq 0x20(%r15), %rsi
movq 0x28(%r15), %rdx
addq %rsi, %rdx
leaq -0x98(%rbp), %rdi
callq 0x8ac2
movl 0x8(%r15), %edi
decl %edi
movb $0x1, %r15b
movl %edi, %esi
callq 0x17961
leaq -0x98(%rbp), %rdx
movq %rbx, %rdi
movl %r14d, %esi
movq %rax, %rcx
xorl %r8d, %r8d
callq 0x1d65a
xorl %r15d, %r15d
leaq 0xe343(%rip), %rsi      # 0x2b678
leaq -0x17756(%rip), %rdx    # 0x5be6
movq %rbx, %rdi
callq 0x4350
movl $0x30, %edi
callq 0x4110
movq %rax, %r15
movl 0x8(%rbx), %ebx
movq %r12, %r13
movl 0xc(%r12), %r14d
leal -0x1(%rbx), %edi
leal -0x1(%r14), %esi
callq 0x17961
movl 0x10(%r13), %ecx
leaq 0xe31d(%rip), %rdx      # 0x2b690
addq $0x10, %rdx
movq %rdx, (%r15)
movl %ecx, 0x8(%r15)
movl -0x34(%rbp), %ecx
movl %ecx, 0xc(%r15)
movq %rax, 0x10(%r15)
movq $0x0, 0x18(%r15)
movb $0x0, 0x20(%r15)
movl %ebx, 0x24(%r15)
movl %r14d, 0x28(%r15)
leaq 0xe313(%rip), %rsi      # 0x2b6b8
movq 0xec2c(%rip), %rdx      # 0x2bfd8
movq %r15, %rdi
callq 0x4350
movl 0x8(%rbx), %r12d
movl $0x38, %edi
callq 0x4110
movq %rax, %rbx
movq -0x30(%rbp), %rax
movl 0xc(%rax), %r13d
leal -0x1(%r12), %edi
leal -0x1(%r13), %esi
callq 0x17961
movq %rax, %r15
movl %r14d, %edi
callq 0x82cf
movq -0x30(%rbp), %rcx
movl 0x10(%rcx), %ecx
leaq 0xe21c(%rip), %rdx      # 0x2b610
addq $0x10, %rdx
movq %rdx, (%rbx)
movw $0x1, 0x8(%rbx)
movq %r15, 0x10(%rbx)
movq %rax, 0x18(%rbx)
movq $0x0, 0x20(%rbx)
movl %r12d, 0x28(%rbx)
movl %r13d, 0x2c(%rbx)
movl -0x34(%rbp), %eax
movl %eax, 0x30(%rbx)
movl %ecx, 0x34(%rbx)
leaq 0xe20f(%rip), %rsi      # 0x2b638
movq 0xeba8(%rip), %rdx      # 0x2bfd8
movq %rbx, %rdi
callq 0x4350
jmp 0x1d44c
jmp 0x1d44c
jmp 0x1d44c
jmp 0x1d44c
jmp 0x1d44c
jmp 0x1d44c
jmp 0x1d44c
jmp 0x1d44c
jmp 0x1d44c
jmp 0x1d44c
movq %rax, %r14
movq %r12, %rdi
jmp 0x1d4bb
jmp 0x1d456
movq %rax, %r14
jmp 0x1d484
movq %rax, %r14
movq %r15, %rdi
jmp 0x1d487
movq %rax, %r14
movq -0x98(%rbp), %rdi
cmpq %r12, %rdi
je 0x1d47f
callq 0x41e0
jmp 0x1d47f
movq %rax, %r14
movb $0x1, %r15b
testb %r15b, %r15b
je 0x1d4c0
movq %rbx, %rdi
callq 0x4190
jmp 0x1d4c0
movq %rax, %r14
movq -0x78(%rbp), %rdi
cmpq %r12, %rdi
je 0x1d4b8
callq 0x41e0
jmp 0x1d4b8
jmp 0x1d4b5
movq %rax, %r14
movq -0x58(%rbp), %rdi
leaq -0x48(%rbp), %rax
cmpq %rax, %rdi
jne 0x1d4bb
jmp 0x1d4c0
movq %rax, %r14
movq %rbx, %rdi
callq 0x41e0
movq %r14, %rdi
callq 0x4380