// File: /workspace/github_cpp_makefile_star2_O2/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 0x1a2a6
movq %rax, %rbx
leaq 0x10751(%rip), %r14     # 0x2ab28
leaq 0x106ea(%rip), %rdx     # 0x2aac8
movq %rax, %rdi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x4200
movq %rax, %r15
leaq 0x106a3(%rip), %rdx     # 0x2aa98
movq %rbx, %rdi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x4200
movq %rax, %r13
movq %r15, %rax
orq %r13, %rax
je 0x1a758
callq 0x154d4
movl (%rax), %edi
callq 0x5ada
testb %al, %al
je 0x1a758
testq %r15, %r15
je 0x1a46b
movq 0x10e47(%rip), %r12     # 0x2b278
leaq 0x20(%r15), %rsi
leaq -0x98(%rbp), %r14
movq %r14, %rdi
movq %rsi, -0x30(%rbp)
callq 0x4290
movq %r12, %rdi
movq %r14, %rsi
callq 0x14422
movl %eax, %r12d
leaq -0x98(%rbp), %rdi
callq 0x40e0
testb %r12b, %r12b
je 0x1a7d5
movl 0x10(%rbx), %r14d
callq 0x154a8
movq %rax, %r12
callq 0x1a3b7
movq %rax, -0x30(%rbp)
movl (%r12), %eax
addl $-0x1f, %eax
cmpl $0x9, %eax
movl %r14d, -0x34(%rbp)
ja 0x1a4c0
leaq 0x6b51(%rip), %rcx      # 0x20fe8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
pushq $0xf
jmp 0x1a4c2
pushq $0x10
jmp 0x1a4c2
pushq $0xd
jmp 0x1a4c2
pushq $0x1c
jmp 0x1a4c2
pushq $0x1d
jmp 0x1a4c2
pushq $0xb
jmp 0x1a4c2
pushq $0xc
jmp 0x1a4c2
pushq $0x1e
jmp 0x1a4c2
pushq $0x2b
popq %r14
movl %r14d, %edi
callq 0x7264
movq -0x30(%rbp), %rcx
movl 0x10(%rcx), %edx
movl %r14d, %edi
testb %al, %al
je 0x1a51b
movl -0x34(%rbp), %esi
callq 0x13fba
testb %al, %al
jne 0x1a52b
movl 0x8(%rbx), %r12d
pushq $0x38
popq %rdi
callq 0x4110
movq %rax, %rbx
movq -0x30(%rbp), %rax
movl 0xc(%rax), %r13d
leal -0x1(%r12), %edi
leal -0x1(%r13), %esi
callq 0x15504
movq %rax, %r15
movl %r14d, %edi
callq 0x7335
jmp 0x1a85f
movl -0x34(%rbp), %esi
callq 0x140a5
testb %al, %al
je 0x1a82f
movl (%r12), %eax
addl $-0x20, %eax
cmpl $0x8, %eax
ja 0x1a6b3
leaq 0x6ace(%rip), %rcx      # 0x21010
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
movl -0x34(%rbp), %r14d
jmpq *%rax
pushq $0x30
popq %rdi
callq 0x41f0
movq %rax, %r12
pushq $0xf
popq %rsi
movq %rax, %rdi
movq %rbx, %rdx
movq -0x30(%rbp), %rcx
callq 0x7bfe
jmp 0x1a6bb
pushq $0x30
popq %rdi
callq 0x41f0
movq %rax, %r12
cmpl $0x9, %r14d
jne 0x1a677
pushq $0x1c
popq %rsi
movq %r12, %rdi
movq %rbx, %rdx
movq -0x30(%rbp), %rcx
callq 0x7c5c
jmp 0x1a6bb
pushq $0x30
popq %rdi
callq 0x41f0
movq %rax, %r12
pushq $0x10
popq %rsi
movq %rax, %rdi
movq %rbx, %rdx
movq -0x30(%rbp), %rcx
callq 0x7bfe
jmp 0x1a6bb
pushq $0x30
popq %rdi
callq 0x41f0
movq %rax, %r12
cmpl $0x9, %r14d
jne 0x1a68b
pushq $0x1d
popq %rsi
movq %r12, %rdi
movq %rbx, %rdx
movq -0x30(%rbp), %rcx
callq 0x7c5c
jmp 0x1a6bb
pushq $0x30
popq %rdi
callq 0x41f0
movq %rax, %r12
cmpl $0x9, %r14d
jne 0x1a69f
pushq $0x1e
popq %rsi
movq %r12, %rdi
movq %rbx, %rdx
movq -0x30(%rbp), %rcx
callq 0x7c5c
jmp 0x1a6bb
pushq $0x30
popq %rdi
callq 0x41f0
movq %rax, %r12
pushq $0xb
popq %rsi
movq %rax, %rdi
movq %rbx, %rdx
movq -0x30(%rbp), %rcx
callq 0x7bfe
jmp 0x1a6bb
pushq $0x30
popq %rdi
callq 0x41f0
movq %rax, %r12
pushq $0xc
popq %rsi
movq %rax, %rdi
movq %rbx, %rdx
movq -0x30(%rbp), %rcx
callq 0x7bfe
jmp 0x1a6bb
pushq $0x30
popq %rdi
callq 0x41f0
movq %rax, %r12
pushq $0xd
popq %rsi
movq %rax, %rdi
movq %rbx, %rdx
movq -0x30(%rbp), %rcx
callq 0x7bfe
jmp 0x1a6bb
pushq $0x19
popq %rsi
movq %r12, %rdi
movq %rbx, %rdx
movq -0x30(%rbp), %rcx
callq 0x7c5c
jmp 0x1a6bb
pushq $0x1a
popq %rsi
movq %r12, %rdi
movq %rbx, %rdx
movq -0x30(%rbp), %rcx
callq 0x7c5c
jmp 0x1a6bb
pushq $0x1b
popq %rsi
movq %r12, %rdi
movq %rbx, %rdx
movq -0x30(%rbp), %rcx
callq 0x7c5c
jmp 0x1a6bb
movl -0x34(%rbp), %r14d
movq -0x30(%rbp), %r12
movl 0x10(%r12), %edi
movl %r14d, %esi
callq 0x13ec3
testb %al, %al
je 0x1a76a
testq %r15, %r15
je 0x1a718
pushq $0x50
popq %rdi
callq 0x41f0
movq %rax, %rbx
movq %r15, %rsi
addq $0x20, %rsi
leaq -0x58(%rbp), %rdi
callq 0x4290
movq 0x10b81(%rip), %r8      # 0x2b278
movl 0x8(%r15), %r9d
leaq -0x58(%rbp), %rsi
movq %rbx, %rdi
movl %r14d, %edx
movq %r12, %rcx
callq 0x78b4
leaq -0x58(%rbp), %rdi
callq 0x40e0
jmp 0x1a758
pushq $0x40
popq %rdi
callq 0x41f0
movq %rax, %rbx
movq 0x18(%r13), %r14
movq 0x20(%r13), %r15
movl 0x8(%r13), %edi
decl %edi
movl 0xc(%r12), %esi
decl %esi
callq 0x15504
movq 0x10b34(%rip), %r9      # 0x2b278
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
movq %rax, %r8
callq 0x7966
movq %rbx, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x30
popq %rdi
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 0x15504
movl 0x10(%r13), %ecx
leaq 0xfef9(%rip), %rdx      # 0x2a690
addq $0x10, %rdx
movq %rdx, (%r15)
movl %ecx, 0x8(%r15)
movl -0x34(%rbp), %ecx
movl %ecx, 0xc(%r15)
movq %rax, 0x10(%r15)
andq $0x0, 0x18(%r15)
movb $0x0, 0x20(%r15)
movl %ebx, 0x24(%r15)
movl %r14d, 0x28(%r15)
leaq 0xfef2(%rip), %rsi      # 0x2a6b8
movq 0x1080b(%rip), %rdx     # 0x2afd8
movq %r15, %rdi
callq 0x4310
pushq $0x38
popq %rdi
callq 0x4110
movq %rax, %rbx
movl 0x8(%r15), %r12d
leaq -0x78(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x4290
movl 0x8(%r15), %edi
decl %edi
movb $0x1, %r15b
movl %edi, %esi
callq 0x15504
leaq -0x78(%rbp), %rdx
movq %rbx, %rdi
movl %r12d, %esi
movq %rax, %rcx
xorl %r8d, %r8d
callq 0x1aad4
xorl %r15d, %r15d
leaq 0xfe58(%rip), %rsi      # 0x2a678
leaq -0x152ab(%rip), %rdx    # 0x557c
movq %rbx, %rdi
callq 0x4310
movl 0x8(%rbx), %r12d
pushq $0x38
popq %rdi
callq 0x4110
movq %rax, %rbx
movq -0x30(%rbp), %rax
movl 0xc(%rax), %r13d
leal -0x1(%r12), %edi
leal -0x1(%r13), %esi
callq 0x15504
movq %rax, %r15
movl %r14d, %edi
callq 0x7335
movq -0x30(%rbp), %rcx
movl 0x10(%rcx), %ecx
leaq 0xfda3(%rip), %rdx      # 0x2a610
addq $0x10, %rdx
movq %rdx, (%rbx)
movw $0x1, 0x8(%rbx)
movq %r15, 0x10(%rbx)
movq %rax, 0x18(%rbx)
andq $0x0, 0x20(%rbx)
movl %r12d, 0x28(%rbx)
movl %r13d, 0x2c(%rbx)
movl -0x34(%rbp), %eax
movl %eax, 0x30(%rbx)
movl %ecx, 0x34(%rbx)
leaq 0xfd99(%rip), %rsi      # 0x2a638
movq 0x10732(%rip), %rdx     # 0x2afd8
movq %rbx, %rdi
callq 0x4310
jmp 0x1a8c2
jmp 0x1a8c2
jmp 0x1a8c2
jmp 0x1a8c2
jmp 0x1a8c2
jmp 0x1a8c2
jmp 0x1a8c2
jmp 0x1a8c2
jmp 0x1a8c2
jmp 0x1a8c2
movq %rax, %r14
movq %r12, %rdi
jmp 0x1a91d
movq %rax, %r14
leaq -0x78(%rbp), %rdi
callq 0x40e0
testb %r15b, %r15b
jne 0x1a8e4
jmp 0x1a922
jmp 0x1a8e1
jmp 0x1a8e1
movq %rax, %r14
movq %rbx, %rdi
jmp 0x1a8ef
movq %rax, %r14
movq %r15, %rdi
callq 0x4180
jmp 0x1a922
movq %rax, %r14
leaq -0x58(%rbp), %rdi
callq 0x40e0
jmp 0x1a91a
jmp 0x1a917
movq %rax, %r14
leaq -0x98(%rbp), %rdi
callq 0x40e0
jmp 0x1a922
movq %rax, %r14
movq %rbx, %rdi
callq 0x41d0
movq %r14, %rdi
callq 0x4340