Ejemplo n.º 1
0
SymTableNodePtr enterSymTable (char* name, SymTableNodePtr* ptrToNodePtr) {

	//-------------------------------------
	// First, create the new symbol node...
	SymTableNodePtr newNode = (SymTableNodePtr)ABLSymbolMallocCallback(sizeof(SymTableNode));
	if (!newNode)
		ABL_Fatal(0, " ABL: Unable to AblSymTableHeap->malloc symbol ");

	newNode->name = (char*)ABLSymbolMallocCallback(strlen(name) + 1);
	if (!newNode->name)
		ABL_Fatal(0, " ABL: Unable to AblSymTableHeap->malloc symbol name ");
	strcpy(newNode->name, name);
	newNode->left = NULL;
	newNode->parent = NULL;
	newNode->right = NULL;
	newNode->next = NULL;
	newNode->info = NULL;
	newNode->defn.key = DFN_UNDEFINED;
	newNode->defn.info.data.varType = VAR_TYPE_NORMAL;
	newNode->defn.info.data.offset = 0;
	newNode->typePtr = NULL;
	newNode->level = (unsigned char)level;
	newNode->labelIndex = 0;

	//-------------------------------------
	// Find where to put this new symbol...
	SymTableNodePtr curNode = *ptrToNodePtr;
	SymTableNodePtr parentNode = NULL;
	while (curNode) {
		if (strcmp(name, curNode->name) < 0)
			ptrToNodePtr = &(curNode->left);
		else
			ptrToNodePtr = &(curNode->right);
		parentNode = curNode;
		curNode = *ptrToNodePtr;
	}
	newNode->parent = parentNode;
	*ptrToNodePtr = newNode;

	return(newNode);
}
Ejemplo n.º 2
0
TypePtr createType (void) {

	TypePtr newType = (TypePtr)ABLSymbolMallocCallback(sizeof(Type));
	if (!newType)
		ABL_Fatal(0, " ABL: Unable to AblStackHeap->malloc newType ");
	newType->numInstances = 1;
	newType->form = FRM_NONE;
	newType->size = 0;
	newType->typeIdPtr = NULL;

	return(newType);
}
Ejemplo n.º 3
0
TypePtr factor (void) {

	TypePtr thisType = NULL;
	switch (curToken) {
		case TKN_IDENTIFIER: {
			SymTableNodePtr IdPtr = NULL;
			searchAndFindAllSymTables(IdPtr);
			switch (IdPtr->defn.key) {
				case DFN_FUNCTION:
					crunchSymTableNodePtr(IdPtr);
					getToken();
					thisType = routineCall(IdPtr, 1);
					break;
				case DFN_CONST:
					crunchSymTableNodePtr(IdPtr);
					getToken();
					thisType = (TypePtr)(IdPtr->typePtr);
					break;
				default:
					thisType = (TypePtr)variable(IdPtr);
					break;
			}
			}
			break;
		case TKN_NUMBER: {
			SymTableNodePtr thisNode = searchSymTable(tokenString, SymTableDisplay[1]);
			if (!thisNode)
				thisNode = enterSymTable(tokenString, &SymTableDisplay[1]);
			if (curLiteral.type == LIT_INTEGER) {
				thisNode->typePtr = IntegerTypePtr;
				thisType = (TypePtr)(thisNode->typePtr);
				thisNode->defn.info.constant.value.integer = curLiteral.value.integer;
				}
			else {
				thisNode->typePtr = RealTypePtr;
				thisType = (TypePtr)(thisNode->typePtr);
				thisNode->defn.info.constant.value.real = curLiteral.value.real;
			}
			crunchSymTableNodePtr(thisNode);
			getToken();
			}
			break;
		case TKN_STRING: {
			long length = strlen(curLiteral.value.string);
			if (EnterStateSymbol) {
				SymTableNodePtr stateSymbol = searchSymTableForState(curLiteral.value.string, SymTableDisplay[1]);
				if (!stateSymbol)
					forwardState(curLiteral.value.string);
			}
			SymTableNodePtr thisNode = searchSymTableForString(tokenString, SymTableDisplay[1]);
			if (!thisNode)// {
				thisNode = enterSymTable(tokenString, &SymTableDisplay[1]);
				if (length == 1) {
					thisNode->defn.info.constant.value.character = curLiteral.value.string[0];
					thisType = CharTypePtr;
					}
				else {
					thisNode->typePtr = thisType = makeStringType(length);
					thisNode->info = (char*)ABLSymbolMallocCallback(length + 1);
					if (!thisNode->info)
						ABL_Fatal(0, " ABL: Unable to AblSymTableHeap->malloc string literal ");
					strcpy(thisNode->info, curLiteral.value.string);
				}
			//}
			crunchSymTableNodePtr(thisNode);

			getToken();
			}
			break;
		case TKN_NOT:
			getToken();
			thisType = factor();
			break;
		case TKN_LPAREN:
			getToken();
			thisType = expression();
			ifTokenGetElseError(TKN_RPAREN, ABL_ERR_SYNTAX_MISSING_RPAREN);
			break;
		default:
			syntaxError(ABL_ERR_SYNTAX_INVALID_EXPRESSION);
			thisType = &DummyType;
			break;
	}
	return(thisType);
}
Ejemplo n.º 4
0
void doConst (SymTableNodePtr constantIdPtr) {

	TokenCodeType sign = TKN_PLUS;
	bool sawSign = false;

	if ((curToken == TKN_PLUS) || (curToken == TKN_MINUS)) {
		sign = curToken;
		sawSign = true;
		getToken();
	}

	//----------------------------------
	// Numeric constant: real or integer
	if (curToken == TKN_NUMBER) {
		if (curLiteral.type == LIT_INTEGER) {
			if (sign == TKN_PLUS)
				constantIdPtr->defn.info.constant.value.integer = curLiteral.value.integer;
			else
				constantIdPtr->defn.info.constant.value.integer = -(curLiteral.value.integer);
			constantIdPtr->typePtr = setType(IntegerTypePtr);
			}
		else {
			if (sign == TKN_PLUS)
				constantIdPtr->defn.info.constant.value.real = curLiteral.value.real;
			else
				constantIdPtr->defn.info.constant.value.real = -(curLiteral.value.real);
			constantIdPtr->typePtr = setType(RealTypePtr);
		}
		}
	else if (curToken == TKN_IDENTIFIER) {
		SymTableNodePtr idPtr = NULL;
		searchAllSymTables(idPtr);

		if (!idPtr)
			syntaxError(ABL_ERR_SYNTAX_UNDEFINED_IDENTIFIER);
		else if (idPtr->defn.key != DFN_CONST)
			syntaxError(ABL_ERR_SYNTAX_NOT_A_CONSTANT_IDENTIFIER);
		else if (idPtr->typePtr == IntegerTypePtr) {
			if (sign == TKN_PLUS)
				constantIdPtr->defn.info.constant.value.integer = idPtr->defn.info.constant.value.integer;
			else
				constantIdPtr->defn.info.constant.value.integer = -(idPtr->defn.info.constant.value.integer);
			constantIdPtr->typePtr = setType(IntegerTypePtr);
			}
		else if (idPtr->typePtr == CharTypePtr) {
			if (sawSign)
				syntaxError(ABL_ERR_SYNTAX_INVALID_CONSTANT);
			constantIdPtr->defn.info.constant.value.character = idPtr->defn.info.constant.value.character;
			constantIdPtr->typePtr = setType(CharTypePtr);
			}
		else if (idPtr->typePtr == RealTypePtr) {
			if (sign == TKN_PLUS)
				constantIdPtr->defn.info.constant.value.real = idPtr->defn.info.constant.value.real;
			else
				constantIdPtr->defn.info.constant.value.real = -(idPtr->defn.info.constant.value.real);
			constantIdPtr->typePtr = setType(RealTypePtr);
			}
		else if (((Type*)(idPtr->typePtr))->form == FRM_ENUM) {
			if (sawSign)
				syntaxError(ABL_ERR_SYNTAX_INVALID_CONSTANT);
			constantIdPtr->defn.info.constant.value.integer = idPtr->defn.info.constant.value.integer;
			constantIdPtr->typePtr = setType(idPtr->typePtr);
			}
		else if (((TypePtr)(idPtr->typePtr))->form == FRM_ARRAY) {
			if (sawSign)
				syntaxError(ABL_ERR_SYNTAX_INVALID_CONSTANT);
			constantIdPtr->defn.info.constant.value.stringPtr = idPtr->defn.info.constant.value.stringPtr;
			constantIdPtr->typePtr = setType(idPtr->typePtr);
		}
		}
	else if (curToken == TKN_STRING) {
		if (sawSign)
			syntaxError(ABL_ERR_SYNTAX_INVALID_CONSTANT);
		if (strlen(curLiteral.value.string) == 1) {
			constantIdPtr->defn.info.constant.value.character = curLiteral.value.string[0];
			constantIdPtr->typePtr = setType(CharTypePtr);
			}
		else {
			long length = strlen(curLiteral.value.string);
			constantIdPtr->defn.info.constant.value.stringPtr = (char*)ABLSymbolMallocCallback(length + 1);
			if (!constantIdPtr->defn.info.constant.value.stringPtr)
				ABL_Fatal(0, " ABL: Unable to AblSymbolHeap->malloc array string constant ");
			strcpy(constantIdPtr->defn.info.constant.value.stringPtr, curLiteral.value.string);
			constantIdPtr->typePtr = makeStringType(length);
		}
		}
	else {
		constantIdPtr->typePtr = NULL;
		syntaxError(ABL_ERR_SYNTAX_INVALID_CONSTANT);
	}

	getToken();
}