// File: /workspace/github_c_makefile_star2_O1/anuwu[P]ERPLAG-Compiler/typeChecker.c
// Function: validateVar
tokenID validateVar (moduleST *baseModule , astNode *varASTNode, varST **searchedVar)
{
	int isValidVarIndex ;
	varST *locSearchedVar ;

	locSearchedVar = searchVar (baseModule, varASTNode->tok->lexeme) ;

	if (searchedVar != NULL)
		*searchedVar = locSearchedVar ;

	if (locSearchedVar == NULL)
	{
		errSemantic () ;
		#ifdef _WIN64
			printf ("In module \"%s\" at line %d, variable \"%s\" is undeclared\n", getParentModuleName (baseModule), varASTNode->tok->lineNumber, varASTNode->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 ", variable " ANSI_BOLD ANSI_RED "%s" ANSI_RESET " is undeclared\n", getParentModuleName (baseModule), varASTNode->tok->lineNumber, varASTNode->tok->lexeme) ;
		#endif
		realBase->semanticError = 1 ;

		if (varASTNode->child != NULL)
			validVarIndex (baseModule, varASTNode->child) ;

		return (tokenID)0 ;
	}
	else if (locSearchedVar->datatype != TK_ARRAY)
	{
		if (varASTNode->child != NULL)
		{
			errSemantic () ;
			#ifdef _WIN64
				printf ("In module \"%s\" at line %d, primitive \"%s\" variable \"%s\" cannot be indexed\n", getParentModuleName (baseModule), varASTNode->child->tok->lineNumber, typeIDToString (locSearchedVar->datatype), locSearchedVar->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 %s" ANSI_RESET " variable " ANSI_BOLD ANSI_RED "%s" ANSI_RESET " cannot be indexed\n", getParentModuleName (baseModule), varASTNode->child->tok->lineNumber, typeIDToString (locSearchedVar->datatype), locSearchedVar->lexeme) ;
			#endif
			realBase->semanticError = 1 ;

			validVarIndex (baseModule, varASTNode->child) ;
		}

		return locSearchedVar->datatype ;
	}
	else if (varASTNode->child != NULL)		// It is array and has an index
	{
		isValidVarIndex = validVarIndex(baseModule, varASTNode->child) ;	// 0 if invalid static/dyanmic, TK_NUM if valid static, TK_ID if valid dynamic

		if (!isValidVarIndex)
			realBase->semanticError = 1 ;
		else if (isVarStaticArr (locSearchedVar) && isValidVarIndex == TK_NUM)		// Static array
		{
			if (!validStaticArrStaticIndex (baseModule, locSearchedVar, varASTNode->child))
				realBase->semanticError = 1 ;
		}
		
		return locSearchedVar->arrayIndices->type ;
	}
	else
		return TK_ARRAY ;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %rbx
movq 0x8(%rsi), %rax
movq 0x8(%rax), %rsi
callq 0xc430
movq %rax, %r15
testq %r12, %r12
je 0xea45
movq %r15, (%r12)
testq %r15, %r15
je 0xeab6
cmpl $0x3b, 0x8(%r15)
movq 0x30(%r14), %rsi
jne 0xeb0b
testq %rsi, %rsi
je 0xeb77
movq %rbx, %rdi
callq 0xe8d3
testl %eax, %eax
je 0xea96
movl %eax, %r12d
movq %r15, %rdi
callq 0xe88a
cmpl $0x71, %r12d
jne 0xeaaa
testl %eax, %eax
je 0xeaaa
movq 0x30(%r14), %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe967
testl %eax, %eax
jne 0xeaaa
leaq 0x2358b(%rip), %rax     # 0x32028
movq (%rax), %rax
movl $0x1, 0xd8(%rax)
movq 0x10(%r15), %rax
movl (%rax), %r15d
jmp 0xeb7d
xorl %r15d, %r15d
xorl %eax, %eax
callq 0xf205
movq %rbx, %rdi
callq 0xcb84
movq 0x8(%r14), %rcx
movl 0x10(%rcx), %edx
movq 0x8(%rcx), %rcx
leaq 0x4d6b(%rip), %rdi      # 0x13845
movq %rax, %rsi
xorl %eax, %eax
callq 0x10b0
leaq 0x2353d(%rip), %rax     # 0x32028
movq (%rax), %rax
movl $0x1, 0xd8(%rax)
movq 0x30(%r14), %rsi
testq %rsi, %rsi
je 0xeb7d
movq %rbx, %rdi
callq 0xe8d3
jmp 0xeb7d
testq %rsi, %rsi
je 0xeb71
xorl %eax, %eax
callq 0xf205
movq %rbx, %rdi
callq 0xcb84
movq %rax, %r12
movq 0x30(%r14), %rax
movq 0x8(%rax), %rax
movl 0x10(%rax), %r13d
movl 0x8(%r15), %edi
callq 0xd3bf
movq (%r15), %r8
leaq 0x5684(%rip), %rdi      # 0x141c5
movq %r12, %rsi
movl %r13d, %edx
movq %rax, %rcx
xorl %eax, %eax
callq 0x10b0
leaq 0x234d0(%rip), %rax     # 0x32028
movq (%rax), %rax
movl $0x1, 0xd8(%rax)
movq 0x30(%r14), %rsi
movq %rbx, %rdi
callq 0xe8d3
movl 0x8(%r15), %r15d
jmp 0xeb7d
movl $0x3b, %r15d
movl %r15d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq