// File: /workspace/github_c_makefile_star2_O2/anuwu[P]ERPLAG-Compiler/typeChecker.c
// Function: assignmentTypeCheck
void assignmentTypeCheck (moduleST *baseModule, astNode *assignopASTNode)
{
	varST *searchedVarLeft, *searchedVarRight ;
	tokenID typeLeft, typeRight ;

	if (assignopASTNode->child->next->child == NULL && assignopASTNode->child->next->id == TK_ID) // Simple assignment with RHS also as ID
	{	
		typeLeft = validateVar (baseModule, assignopASTNode->child, &searchedVarLeft) ;
		typeRight = validateVar (baseModule, assignopASTNode->child->next, &searchedVarRight) ;

		if (typeLeft == TK_ARRAY && typeRight == TK_ARRAY) 	// Array assignment
		{
			if (searchedVarLeft->arrayIndices->type == searchedVarRight->arrayIndices->type)
			{
				if (isVarStaticArr (searchedVarLeft) && isVarStaticArr (searchedVarRight))
				{
					int leftLim1, rightLim1, leftLim2, rightLim2 ;	
					leftLim1 = atoi (searchedVarLeft->arrayIndices->tokLeft->lexeme) ;
					rightLim1 = atoi (searchedVarLeft->arrayIndices->tokRight->lexeme) ;
					leftLim2 = atoi (searchedVarRight->arrayIndices->tokLeft->lexeme) ;
					rightLim2 = atoi (searchedVarRight->arrayIndices->tokRight->lexeme) ;

					if (leftLim1 != leftLim2 || rightLim1 != rightLim2)
					{
						errSemantic () ;
						#ifdef _WIN64
							printf ("In module \"%s\" at line %d, array variables \"%s\" and \"%s\" do not have matching bounds for assignment\n", getParentModuleName (baseModule), assignopASTNode->tok->lineNumber, searchedVarLeft->lexeme, searchedVarRight->lexeme) ;
						#endif
						#if defined __linux__ || defined __MACH__
							printf ("In module " ANSI_BOLD "%s" ANSI_RESET " at line " ANSI_BOLD ANSI_CYAN "%d" ANSI_RESET ", " ANSI_BOLD ANSI_BLACK "array" ANSI_RESET " variables " ANSI_BOLD ANSI_RED "%s" ANSI_RESET " and " ANSI_BOLD ANSI_RED "%s" ANSI_RESET " do not have matching bounds for assignment\n", getParentModuleName (baseModule), assignopASTNode->tok->lineNumber, searchedVarLeft->lexeme, searchedVarRight->lexeme) ;
						#endif
						realBase->semanticError = 1 ;
					}
				}
			}
			else
			{
				errSemantic () ;
				#ifdef _WIN64
					printf ("In module \"%s\" at line %d, array variables \"%s\" and \"%s\" do not have the same base type\n", getParentModuleName (baseModule), assignopASTNode->tok->lineNumber, searchedVarLeft->lexeme, searchedVarRight->lexeme) ;
				#endif
				#if defined __linux__ || defined __MACH__
					printf ("In module " ANSI_BOLD "%s" ANSI_RESET " at line " ANSI_BOLD ANSI_CYAN "%d" ANSI_RESET ", " ANSI_BOLD ANSI_BLACK "array" ANSI_RESET " variables " ANSI_BOLD ANSI_RED "%s" ANSI_RESET " and " ANSI_BOLD ANSI_RED "%s" ANSI_RESET " do not have the same base type\n", getParentModuleName (baseModule), assignopASTNode->tok->lineNumber, searchedVarLeft->lexeme, searchedVarRight->lexeme) ;
				#endif
				realBase->semanticError = 1 ;
			}
		}
		else if (typeLeft != 0 && typeRight != 0)
		{
			if (typeLeft != TK_ARRAY && typeRight == TK_ARRAY)
			{
				errSemantic () ;
				#ifdef _WIN64
					printf ("In module \"%s\" at line %d, array variable \"%s\" cannot be assigned to primitive variable \"%s\"\n", getParentModuleName (baseModule), assignopASTNode->tok->lineNumber, searchedVarRight->lexeme, searchedVarLeft->lexeme) ;
				#endif
				#if defined __linux__ || defined __MACH__
					printf ("In module " ANSI_BOLD "%s" ANSI_RESET " at line " ANSI_BOLD ANSI_CYAN "%d" ANSI_RESET ", " ANSI_BOLD ANSI_BLACK "array" ANSI_RESET " variable " ANSI_BOLD ANSI_RED "%s" ANSI_RESET " cannot be assigned to " ANSI_BOLD ANSI_BLACK "primitive" ANSI_RESET " variable " ANSI_BOLD ANSI_RED "%s" ANSI_RESET "\n", getParentModuleName (baseModule), assignopASTNode->tok->lineNumber, searchedVarRight->lexeme, searchedVarLeft->lexeme) ;
				#endif
				realBase->semanticError = 1 ;
			}
			else if (typeRight != TK_ARRAY && typeLeft == TK_ARRAY)
			{
				errSemantic () ;
				#ifdef _WIN64
					printf ("In module \"%s\" at line %d, primitive variable \"%s\" cannot be assigned to array variable \"%s\"\n", getParentModuleName (baseModule), assignopASTNode->tok->lineNumber, searchedVarRight->lexeme, searchedVarLeft->lexeme) ;
				#endif
				#if defined __linux__ || defined __MACH__
					printf ("In module " ANSI_BOLD "%s" ANSI_RESET " at line " ANSI_BOLD ANSI_CYAN "%d" ANSI_RESET ", " ANSI_BOLD ANSI_BLACK "primitive" ANSI_RESET " variable " ANSI_BOLD ANSI_RED "%s" ANSI_RESET " cannot be assigned to " ANSI_BOLD ANSI_BLACK "array" ANSI_RESET " variable " ANSI_BOLD ANSI_RED "%s" ANSI_RESET "\n", getParentModuleName (baseModule), assignopASTNode->tok->lineNumber, searchedVarRight->lexeme, searchedVarLeft->lexeme) ;
				#endif
				realBase->semanticError = 1 ;

			}
			else if (typeLeft != typeRight)
			{
				errSemantic () ;
				#ifdef _WIN64
					printf ("In module \"%s\" at line %d, assignment of \"%s\" to \"%s\" are in type conflict\n", getParentModuleName (baseModule), assignopASTNode->tok->lineNumber, searchedVarRight->lexeme, searchedVarLeft->lexeme) ;
				#endif
				#if defined __linux__ || defined __MACH__
					printf ("In module " ANSI_BOLD "%s" ANSI_RESET " at line " ANSI_BOLD ANSI_CYAN "%d" ANSI_RESET ", assignment of " ANSI_BOLD ANSI_RED "%s" ANSI_RESET " to " ANSI_BOLD ANSI_RED "%s" ANSI_RESET " are in type conflict\n", getParentModuleName (baseModule), assignopASTNode->tok->lineNumber, searchedVarRight->lexeme, searchedVarLeft->lexeme) ;
				#endif
				realBase->semanticError = 1 ;
			}

			tinkerVar (baseModule, searchedVarLeft, assignopASTNode->child) ;
		}

		return ;
	}
	else if (assignopASTNode->child->next->child != NULL && assignopASTNode->child->next->child->next == NULL && (assignopASTNode->child->next->id == TK_PLUS || assignopASTNode->child->next->id == TK_MINUS)) // <U>
		typeRight = getExpressionType (baseModule, assignopASTNode->child->next->child) ;
	else	// expression_new
		typeRight = getExpressionType (baseModule, assignopASTNode->child->next) ;

	/* ------------------------------------------------------------------------------------------------------------------*/

	typeLeft = validateVar (baseModule, assignopASTNode->child, &searchedVarLeft) ;
	if (!typeLeft)
		return ;
	else
	{ 
		tinkerVar (baseModule, searchedVarLeft, assignopASTNode->child) ;
		if (typeLeft == TK_ARRAY)
		{
			errSemantic () ;
			#ifdef _WIN64
				printf ("In module \"%s\" at line %d, the assignment of an expression to array variable \"%s\" cannot be made\n", getParentModuleName (baseModule), assignopASTNode->tok->lineNumber, assignopASTNode->child->tok->lexeme) ;
			#endif
			#if defined __linux__ || defined __MACH__
				printf ("In module " ANSI_BOLD "%s" ANSI_RESET " at line " ANSI_BOLD ANSI_CYAN "%d" ANSI_RESET ", the assignment of an expression to " ANSI_BOLD ANSI_BLACK "array" ANSI_RESET " variable " ANSI_BOLD ANSI_RED "%s" ANSI_RESET " cannot be made\n", getParentModuleName (baseModule), assignopASTNode->tok->lineNumber, assignopASTNode->child->tok->lexeme) ;
			#endif
			realBase->semanticError = 1 ;
			return ;
		}
	}

	if (!typeRight)
		return ;
	else if (typeLeft != typeRight)
	{
		errSemantic () ;
		#ifdef _WIN64
			printf ("In module \"%s\" at line %d, assignment of expression of type \"%s\" to the variable \"%s\" of type \"%s\" cannot be made\n", getParentModuleName (baseModule), assignopASTNode->tok->lineNumber, typeIDToString(typeRight), assignopASTNode->child->tok->lexeme, typeIDToString(typeLeft)) ;
		#endif
		#if defined __linux__ || defined __MACH__
			printf ("In module " ANSI_BOLD "%s" ANSI_RESET " at line " ANSI_BOLD ANSI_CYAN "%d" ANSI_RESET ", assignment of expression of type " ANSI_BOLD ANSI_GREEN "%s" ANSI_RESET " to the variable " ANSI_BOLD ANSI_RED "%s" ANSI_RESET " of type " ANSI_BOLD ANSI_GREEN "%s" ANSI_RESET " cannot be made\n", getParentModuleName (baseModule), assignopASTNode->tok->lineNumber, typeIDToString(typeRight), assignopASTNode->child->tok->lexeme, typeIDToString(typeLeft)) ;
		#endif
		realBase->semanticError = 1 ;
		return ;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movq 0x30(%rsi), %rax
movq 0x38(%rax), %rcx
movq 0x30(%rcx), %rsi
testq %rsi, %rsi
je 0xd2d5
cmpq $0x0, 0x38(%rsi)
jne 0xd3cc
movl (%rcx), %eax
addl $-0x57, %eax
cmpl $0x2, %eax
jae 0xd3cc
jmp 0xd3cf
cmpl $0x40, (%rcx)
jne 0xd3cc
leaq -0x30(%rbp), %rdx
movq %r14, %rdi
movq %rax, %rsi
callq 0xced0
movl %eax, %r15d
movq 0x30(%rbx), %rax
movq 0x38(%rax), %rsi
leaq -0x38(%rbp), %rdx
movq %r14, %rdi
callq 0xced0
cmpl $0x3b, %r15d
jne 0xd4b2
cmpl $0x3b, %eax
jne 0xd4b2
movq -0x30(%rbp), %rdi
movq 0x10(%rdi), %r12
movl (%r12), %eax
movq -0x38(%rbp), %r15
movq 0x10(%r15), %r13
cmpl (%r13), %eax
jne 0xd506
callq 0xcd66
testl %eax, %eax
je 0xd54f
movq %r15, %rdi
callq 0xcd66
testl %eax, %eax
je 0xd54f
movq 0x8(%r12), %rax
movq 0x8(%rax), %rdi
callq 0x1130
movl %eax, %r15d
movq 0x8(%r13), %rax
movq 0x8(%rax), %rdi
callq 0x1130
cmpl %eax, %r15d
jne 0xd39c
movq 0x10(%r13), %rax
movq 0x8(%rax), %rdi
callq 0x1130
movl %eax, %r15d
movq 0x10(%r12), %rax
movq 0x8(%rax), %rdi
callq 0x1130
cmpl %r15d, %eax
je 0xd54f
xorl %eax, %eax
callq 0xd671
movq %r14, %rdi
callq 0xb395
movq 0x8(%rbx), %rcx
movl 0x10(%rcx), %edx
movq -0x30(%rbp), %rcx
movq (%rcx), %rcx
movq -0x38(%rbp), %rsi
movq (%rsi), %r8
leaq 0x5038(%rip), %rdi      # 0x123ff
jmp 0xd531
movq %rcx, %rsi
movq %r14, %rdi
callq 0xd08f
movl %eax, %r15d
movq 0x30(%rbx), %rsi
leaq -0x30(%rbp), %rdx
movq %r14, %rdi
callq 0xced0
testl %eax, %eax
je 0xd54f
movl %eax, %r12d
movq -0x30(%rbp), %rsi
movq 0x30(%rbx), %rdx
movq %r14, %rdi
callq 0xbbf1
cmpl $0x3b, %r12d
jne 0xd443
xorl %eax, %eax
callq 0xd671
movq %r14, %rdi
callq 0xb395
movq 0x8(%rbx), %rcx
movq 0x30(%rbx), %rsi
movl 0x10(%rcx), %edx
movq 0x8(%rsi), %rcx
movq 0x8(%rcx), %rcx
leaq 0x52ac(%rip), %rdi      # 0x126e0
movq %rax, %rsi
xorl %eax, %eax
callq 0x10b0
jmp 0xd53b
testl %r15d, %r15d
sete %al
cmpl %r15d, %r12d
sete %cl
orb %al, %cl
jne 0xd54f
xorl %eax, %eax
callq 0xd671
movq %r14, %rdi
callq 0xb395
movq %rax, %r14
movq 0x8(%rbx), %rax
movl 0x10(%rax), %r13d
movl %r15d, %edi
callq 0xbb77
movq %rax, %r15
movq 0x30(%rbx), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rbx
movl %r12d, %edi
callq 0xbb77
leaq 0x52d6(%rip), %rdi      # 0x1276d
movq %r14, %rsi
movl %r13d, %edx
movq %r15, %rcx
movq %rbx, %r8
movq %rax, %r9
xorl %eax, %eax
callq 0x10b0
jmp 0xd53b
testl %r15d, %r15d
je 0xd54f
testl %eax, %eax
je 0xd54f
cmpl $0x3b, %r15d
je 0xd55e
cmpl $0x3b, %eax
jne 0xd55e
xorl %eax, %eax
callq 0xd671
movq %r14, %rdi
callq 0xb395
movq 0x8(%rbx), %rcx
movl 0x10(%rcx), %edx
movq -0x38(%rbp), %rcx
movq (%rcx), %rcx
movq -0x30(%rbp), %rsi
movq (%rsi), %r8
leaq 0x5028(%rip), %rdi      # 0x12529
jmp 0xd5c6
xorl %eax, %eax
callq 0xd671
movq %r14, %rdi
callq 0xb395
movq 0x8(%rbx), %rcx
movl 0x10(%rcx), %edx
movq -0x30(%rbp), %rcx
movq (%rcx), %rcx
movq -0x38(%rbp), %rsi
movq (%rsi), %r8
leaq 0x4f69(%rip), %rdi      # 0x1249a
movq %rax, %rsi
xorl %eax, %eax
callq 0x10b0
leaq 0x22ad6(%rip), %rax     # 0x30018
movq (%rax), %rax
movl $0x1, 0xd8(%rax)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x3b, %r15d
jne 0xd596
cmpl $0x3b, %eax
je 0xd596
xorl %eax, %eax
callq 0xd671
movq %r14, %rdi
callq 0xb395
movq 0x8(%rbx), %rcx
movl 0x10(%rcx), %edx
movq -0x38(%rbp), %rcx
movq (%rcx), %rcx
movq -0x30(%rbp), %rsi
movq (%rsi), %r8
leaq 0x5036(%rip), %rdi      # 0x125ca
jmp 0xd5c6
cmpl %eax, %r15d
je 0xd5e4
xorl %eax, %eax
callq 0xd671
movq %r14, %rdi
callq 0xb395
movq 0x8(%rbx), %rcx
movl 0x10(%rcx), %edx
movq -0x38(%rbp), %rcx
movq (%rcx), %rcx
movq -0x30(%rbp), %rsi
movq (%rsi), %r8
leaq 0x50a5(%rip), %rdi      # 0x1266b
movq %rax, %rsi
xorl %eax, %eax
callq 0x10b0
leaq 0x22a41(%rip), %rax     # 0x30018
movq (%rax), %rax
movl $0x1, 0xd8(%rax)
movq -0x30(%rbp), %rsi
movq 0x30(%rbx), %rdx
movq %r14, %rdi
callq 0xbbf1
jmp 0xd54f
nopl (%rax)