// File: /workspace/github_c_makefile_star2_O1/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 0xee3f
cmpq $0x0, 0x38(%rsi)
jne 0xef3f
movl (%rcx), %eax
addl $-0x57, %eax
cmpl $0x2, %eax
jae 0xef3f
jmp 0xef42
cmpl $0x40, (%rcx)
jne 0xef36
leaq -0x30(%rbp), %rdx
movq %r14, %rdi
movq %rax, %rsi
callq 0xea15
movl %eax, %r15d
movq 0x30(%rbx), %rax
movq 0x38(%rax), %rsi
leaq -0x38(%rbp), %rdx
movq %r14, %rdi
callq 0xea15
cmpl $0x3b, %r15d
jne 0xf025
cmpl $0x3b, %eax
jne 0xf025
movq -0x30(%rbp), %rdi
movq 0x10(%rdi), %r12
movl (%r12), %eax
movq -0x38(%rbp), %r15
movq 0x10(%r15), %r13
cmpl (%r13), %eax
jne 0xf079
callq 0xe88a
testl %eax, %eax
je 0xf0c2
movq %r15, %rdi
callq 0xe88a
testl %eax, %eax
je 0xf0c2
movq 0x8(%r12), %rax
movq 0x8(%rax), %rdi
callq 0x1140
movl %eax, %r15d
movq 0x8(%r13), %rax
movq 0x8(%rax), %rdi
callq 0x1140
cmpl %eax, %r15d
jne 0xef06
movq 0x10(%r13), %rax
movq 0x8(%rax), %rdi
callq 0x1140
movl %eax, %r15d
movq 0x10(%r12), %rax
movq 0x8(%rax), %rdi
callq 0x1140
cmpl %r15d, %eax
je 0xf0c2
xorl %eax, %eax
callq 0xf205
movq %r14, %rdi
callq 0xcb84
movq 0x8(%rbx), %rcx
movl 0x10(%rcx), %edx
movq -0x30(%rbp), %rcx
movq (%rcx), %rcx
movq -0x38(%rbp), %rsi
movq (%rsi), %r8
leaq 0x54be(%rip), %rdi      # 0x143ef
jmp 0xf0a4
testq %rsi, %rsi
jne 0xee21
movq %rcx, %rsi
movq %r14, %rdi
callq 0xebd3
movl %eax, %r15d
movq 0x30(%rbx), %rsi
leaq -0x30(%rbp), %rdx
movq %r14, %rdi
callq 0xea15
testl %eax, %eax
je 0xf0c2
movl %eax, %r12d
movq -0x30(%rbp), %rsi
movq 0x30(%rbx), %rdx
movq %r14, %rdi
callq 0xd430
cmpl $0x3b, %r12d
jne 0xefb6
xorl %eax, %eax
callq 0xf205
movq %r14, %rdi
callq 0xcb84
movq 0x8(%rbx), %rcx
movq 0x30(%rbx), %rsi
movl 0x10(%rcx), %edx
movq 0x8(%rsi), %rcx
movq 0x8(%rcx), %rcx
leaq 0x5729(%rip), %rdi      # 0x146d0
movq %rax, %rsi
xorl %eax, %eax
callq 0x10b0
jmp 0xf0ae
testl %r15d, %r15d
sete %al
cmpl %r15d, %r12d
sete %cl
orb %al, %cl
jne 0xf0c2
xorl %eax, %eax
callq 0xf205
movq %r14, %rdi
callq 0xcb84
movq %rax, %r14
movq 0x8(%rbx), %rax
movl 0x10(%rax), %r13d
movl %r15d, %edi
callq 0xd3bf
movq %rax, %r15
movq 0x30(%rbx), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rbx
movl %r12d, %edi
callq 0xd3bf
leaq 0x5753(%rip), %rdi      # 0x1475d
movq %r14, %rsi
movl %r13d, %edx
movq %r15, %rcx
movq %rbx, %r8
movq %rax, %r9
xorl %eax, %eax
callq 0x10b0
jmp 0xf0ae
testl %r15d, %r15d
je 0xf0c2
testl %eax, %eax
je 0xf0c2
cmpl $0x3b, %r15d
je 0xf0d1
cmpl $0x3b, %eax
jne 0xf0d1
xorl %eax, %eax
callq 0xf205
movq %r14, %rdi
callq 0xcb84
movq 0x8(%rbx), %rcx
movl 0x10(%rcx), %edx
movq -0x38(%rbp), %rcx
movq (%rcx), %rcx
movq -0x30(%rbp), %rsi
movq (%rsi), %r8
leaq 0x54a5(%rip), %rdi      # 0x14519
jmp 0xf139
xorl %eax, %eax
callq 0xf205
movq %r14, %rdi
callq 0xcb84
movq 0x8(%rbx), %rcx
movl 0x10(%rcx), %edx
movq -0x30(%rbp), %rcx
movq (%rcx), %rcx
movq -0x38(%rbp), %rsi
movq (%rsi), %r8
leaq 0x53e6(%rip), %rdi      # 0x1448a
movq %rax, %rsi
xorl %eax, %eax
callq 0x10b0
leaq 0x22f73(%rip), %rax     # 0x32028
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 0xf109
cmpl $0x3b, %eax
je 0xf109
xorl %eax, %eax
callq 0xf205
movq %r14, %rdi
callq 0xcb84
movq 0x8(%rbx), %rcx
movl 0x10(%rcx), %edx
movq -0x38(%rbp), %rcx
movq (%rcx), %rcx
movq -0x30(%rbp), %rsi
movq (%rsi), %r8
leaq 0x54b3(%rip), %rdi      # 0x145ba
jmp 0xf139
cmpl %eax, %r15d
je 0xf157
xorl %eax, %eax
callq 0xf205
movq %r14, %rdi
callq 0xcb84
movq 0x8(%rbx), %rcx
movl 0x10(%rcx), %edx
movq -0x38(%rbp), %rcx
movq (%rcx), %rcx
movq -0x30(%rbp), %rsi
movq (%rsi), %r8
leaq 0x5522(%rip), %rdi      # 0x1465b
movq %rax, %rsi
xorl %eax, %eax
callq 0x10b0
leaq 0x22ede(%rip), %rax     # 0x32028
movq (%rax), %rax
movl $0x1, 0xd8(%rax)
movq -0x30(%rbp), %rsi
movq 0x30(%rbx), %rdx
movq %r14, %rdi
callq 0xd430
jmp 0xf0c2