Exemplo n.º 1
0
/*
Purpose: Installs a new entry (VID record) in the symbol table.
Authors: Kwok Hong Kelvin Chan, Kyle Hinskens
Version: 13.11.14
Called functions: st_lookup(), b_setmark(), b_getsize(), strlen(), b_addc(), printf(),
				b_get_r_flag(), b_get_chmemloc(), b_getmark(), st_incoffset()
Parameters:
			STD sym_table The symbol table
			char *lexeme The lexeme
			int line The line number
Return value: int The offset of the lexeme or -1 on error
*/
int st_install(STD sym_table, char *lexeme, int line) {
	int offset; /* lookup() return value*/
	unsigned int i; /* loop counter for strlen */
	int j; /* loop counter */
	unsigned short buffer_offset = 0; /* buffer_offset, used for reorganize plex on the symbol table */

	offset = st_lookup(sym_table, lexeme);

	/* check if the symbol table is full */
	if (sym_table.st_offset == sym_table.st_size) return R_FAIL_1;

	/* lexeme is not found from the symbol table */ 
	if (offset == -1) {
		b_setmark(sym_table.plsBD, b_getsize(sym_table.plsBD)); /* set mark on the Buffer at the addc_offset index */
		/* store lexeme into the symbol table Buffer */
		for (i = 0; i < strlen(lexeme) + 1; ++i) {
			/* check if b_addc() executed successfully, just in case! */
			if (b_addc(sym_table.plsBD, lexeme[i]) == NULL) {
				return R_FAIL_2;
			}

			/* Buffer reallocation, reorganize plex on the symbol table */
			if (b_get_r_flag(sym_table.plsBD)) {
				/* traverse the symbol table, and reassign the correct memory address from the new Buffer */
				 for (j = 0; j < sym_table.st_offset; ++j) {
					sym_table.pstvr[j].plex = b_get_chmemloc(sym_table.plsBD, buffer_offset); /* get the character pointer from the buffer_offset */
					buffer_offset += (unsigned short)strlen(b_get_chmemloc(sym_table.plsBD, buffer_offset)) + 1; /* increment buffer_offset by the strlen from current string  */
				}
			}
		}

		sym_table.pstvr[sym_table.st_offset].plex = b_get_chmemloc(sym_table.plsBD, b_getmark(sym_table.plsBD)); /* set plex as the beginning of the lexeme from the Buffer */
		sym_table.pstvr[sym_table.st_offset].o_line = line; /* set line number */
		sym_table.pstvr[sym_table.st_offset].status_field = DEFAULT; /* set default status field */

		/* variable of type string */ 
		if (lexeme[strlen(lexeme) -1] == '#') {
			sym_table.pstvr[sym_table.st_offset].status_field |= STRING_MASK; /* set status field as string by performing a OR operation with the STRING_MASK */
			sym_table.pstvr[sym_table.st_offset].status_field |= UPDATE_MASK; /* Set update flag */
			sym_table.pstvr[sym_table.st_offset].i_value.str_offset = -1; /* set i_value */
		}
		/* variable of type int */ 
		else if (lexeme[0] == 'i' || lexeme[0] == 'o' || lexeme[0] == 'd' || lexeme[0] == 'n') {
			sym_table.pstvr[sym_table.st_offset].status_field |= INT_MASK; /* set status field as int by performing a OR operation with the INT_MASK*/
			sym_table.pstvr[sym_table.st_offset].i_value.int_val = 0; /* set i_value */
		}
		/* variable of type float */ 
		else {
			sym_table.pstvr[sym_table.st_offset].status_field |= FLOAT_MASK; /* set status field as float by performing a OR operation with the FLOAT_MASK*/
			sym_table.pstvr[sym_table.st_offset].i_value.fpl_val = 0.0f; /* set i_value */
		}

		st_incoffset(); /* increment global symbol table st_offset field */
		return sym_table.st_offset; /* The function returns the current offset of that entry from the beginning of the array of STVR (the array pointed by pstvr). */
	}

	return offset;
}
Exemplo n.º 2
0
/*******************************************************************************
* Purpose:			Add a new entry into the symbol table array of STVR at the 
*					next available element. 
* Author:			Skye Turriff
* History:			Version 1, 19 November 2015
* Called functions:	st_lookup(), b_setmark(), b_size(), b_addc(), b_rflag(),
*					st_incoffset()
* Parameters:		STD sym_table struct with valid st_size
*					char* lexeme pointer to VID name to be added to table
*					char type of VID
*					int line of first occurence of VID
* Return value:		int offset into STVR array where VID record is installed, 
*					-2 on bad parameters, or -1 if symbol table is full.
* Algorithm:		Check parameters, return -2 if bad. If lexeme already in
*					symbol table, return the offset. If symbol table is full,
*					return -1. Set plex for new STVR to point to the next space
*					available in CA. Add each character in the VID lexeme to 
*					this location and make a C-type string. Once the new lexeme
*					has been added, if at any time the location of the CA was 
*					moved, re-calculate plex for each STVR in the STVR array. 
*					Finally, initialize remaining members for new STVR, and
*					increment st_offset of global sym_table. Return offset of
*					new STVR in STVR array.
*******************************************************************************/
int st_install(STD sym_table, char* lexeme, char type, int line) {
	int offset;		/* Offset into array of STVR */
	char r_flag;	/* Memory reallocation flag for lexeme storage buffer */
	char* tplex;	/* Used to iterate through lexeme storage buffer */
	int i;			/* Loop counter for iteration through STVR array */

	/* Check for valid symbol table */
	if (sym_table.st_size == 0) 
		return ERR_FAIL2;	

	/* Check if lexeme already exists */
	if ((offset = st_lookup(sym_table, lexeme)) != ERR_FAIL1) 
		return offset;

	/* Ensure there is room for a new record */
	if (sym_table.st_offset >= sym_table.st_size) 
		return ERR_FAIL1;

	/* Set plex for new VID record */
	sym_table.pstvr[sym_table.st_offset].plex =
		b_setmark(sym_table.plsBD, b_size(sym_table.plsBD));

	/* Install new entry into STVR array, make C-type string */
	r_flag = 0;
	for (; *lexeme; lexeme++) {
		b_addc(sym_table.plsBD, *lexeme);
		if (b_rflag(sym_table.plsBD)) 
			r_flag = 1;
	}
	b_addc(sym_table.plsBD, '\0');
	if (b_rflag(sym_table.plsBD)) 
		r_flag = 1;

	/* If r_flag set, use tplex to interate through lexeme storage 
	until '\0'. Then set plex of the current STVR to tplex + 1 */
	if (r_flag) {
		sym_table.pstvr[0].plex = b_setmark(sym_table.plsBD, 0);
		tplex = sym_table.pstvr[0].plex;
		for (i = 1; i <= sym_table.st_offset; i++) {
			while (*tplex) { tplex++;  continue; }
			sym_table.pstvr[i].plex = ++tplex;
		}
	}

	/* Record source line number */
	sym_table.pstvr[sym_table.st_offset].o_line = line;

	/* Initialize status_field and i_value */
	sym_table.pstvr[sym_table.st_offset].status_field &= DEFAULTZ;
	sym_table.pstvr[sym_table.st_offset].status_field |= DEFAULT;
	if (type == 'I') { /* integer */
		sym_table.pstvr[sym_table.st_offset].status_field |= DT_INT;
		sym_table.pstvr[sym_table.st_offset].i_value.int_val = 0;
	}
	else if (type == 'F') {	/* float */
		sym_table.pstvr[sym_table.st_offset].status_field |= DT_FPL;
		sym_table.pstvr[sym_table.st_offset].i_value.fpl_val = 0.0F;
	}
	else { /* string */
		sym_table.pstvr[sym_table.st_offset].status_field |= DT_STR;
		sym_table.pstvr[sym_table.st_offset].status_field |= SET_FLG;
		sym_table.pstvr[sym_table.st_offset].i_value.str_offset = -1;
	}

	st_incoffset();	/* Increment offset into STVR array of global sym_table */

	return sym_table.st_offset;
}
Exemplo n.º 3
0
/********************************************************************
Function name:      st_install()
Purpose:            Install a new entry in the symbol table
Author:             Warren Rainey
History/Versions:   1.0
Called functions:   st_lookup(), b_setmark, b_get_r_flag(), b_addc(), b_get_chloc(), st_incoffset()
In parameters:      STD, char *, char, int
Return Value:       Int
Algorithm:			- Check if symbol table is valid
					- This function installs a new entry (VID record) in the symbol table.  
					First, It calls the st_lookup() function to search for the lexeme (variable name) in the symbol table. 
					- Add new record to symbol table and increment st_offest.
**********************************************************************/  
int st_install(STD sym_table, char *lexeme, char type, int line){
	int location = 0;
	int i = 0;
	int j = 0;
	STVR new_rec;
	
	/*intf("1lexeme: %s\n", lexeme);Test*/
	if(!sym_table.st_size){
		return R_FAIL_2;
	}

	location = st_lookup(sym_table,lexeme);

	if(location >= 0){/*lexeme found in symbol table*/  
		return location;
	}

	if(sym_table.st_offset == sym_table.st_size){
		return TABLE_FULL;
	}

	/*printf("2lexeme:%s\n", lexeme);Test*/
	/*sym_table.pstvr[sym_table.st_offset].plex = b_setmark(sym_table.plsBD,b_size(sym_table.plsBD));/*sym_table.st_offset*/
	new_rec.plex = b_setmark(sym_table.plsBD,b_size(sym_table.plsBD));/*sym_table.st_offset*/
	/*plex = b_setmark(sym_table.plsBD, getOffset(plsBD);
	b_setmark(sym_table.plsBD, 0); /*set to addc_offset*/
		for(i = 0; lexeme[i] != '\0'; i++){ /*add lexeme to lexeme storage*/
			b_addc(sym_table.plsBD, lexeme[i]);
			/*if plsBD has moves as result of realloc of buffer*/
			/*if( b_rflag(sym_table.plsBD)){/*Remove as it may creat p
				
				while(j < sym_table.st_offset){ 
					sym_table.pstvr[j].plex = temp;
					temp += strlen(temp) + 1; /* move temp to end of lexeme in buffer*
					j++;
				}
			}*/
		}
		b_addc(sym_table.plsBD, '\0');
		
	new_rec.status_field = DEFAULT;
	new_rec.o_line = line;
	
	
	/*Finding out if int, string or float*/
	if(type == 'I'){/*Is int*/
		new_rec.i_value.int_val = 0;
		new_rec.status_field |= SET_INTEGER;
	}else if(type == 'S'){/*Is string*/
		new_rec.i_value.str_offset = -1;
		new_rec.status_field |= SET_STRING;
		new_rec.status_field |= CHK_LSB;
	}else{/*Is float*/
		new_rec.i_value.fpl_val = 0.0f;
		new_rec.status_field |= SET_FLOAT;
	}
	/*printf("New plex: %s", new_rec.plex);Test*/
	sym_table.pstvr[sym_table.st_offset] = new_rec; /*add new record to table*/	
	st_incoffset();		
	return sym_table.st_offset;
}
Exemplo n.º 4
0
/**********************************************************************************************************
Purpose:				Add a new STVR into the symbol table
Author:					Thom Palmer
History/Versions:		10.18.13
Called functions:		macro-chk_sym_tbl(), st_lookup(), b_get_chmemloc(), b_getsize(), strlen(), b_addc(),
						b_get_r_flag(),st_incoffset()
Parameters:				STD sym_table,char *lexeme
Return value:			Offset of the STVR if it already exists -1 on failure.  
Algorithm:				Check for a valid symbol table, call st_lookup to find if the lexeme is already
						in the symbol table, if it is return the offset where it was found, otherwise
						point the current plex to the current size of the buffer, initialize o_line &
						status_field, store the lexeme in the buffer using a call to b_addc, 
						if a reallocation occurs at any point adjust all of the plex pointers, determine
						the default datatype of the lexeme and set it, increment the offset and return
						STD.st_offset
						* Assume that size_t is that same size as an int *
**********************************************************************************************************/
int st_install(STD sym_table, char *lexeme, int line)
{
	int vid_offset;							/* Stores the offset where lexeme is found */
	unsigned int i;									/* Used and an iterator*/
	short offset = 0;						/* Used to store the offset from the buffer head */
	int rFlag = 0;							/* Flag indicating if a reallocation occured in the buffer */

	/*Check for valid symbol table*/
	chk_sym_tbl(sym_table);	
	/* Call st_lookup to search for the lexeme in the table */
	vid_offset = st_lookup(sym_table, lexeme);
	/* If the lexeme was found. Return the offset where it was found */
	if(vid_offset != LEX_NOT_FND )
	{
		return vid_offset;
	}
	/* Check if symbol table is full */
	if(sym_table.st_size == sym_table.st_offset)
	{
		return SYM_TBL_FULL; 
	}
	/* Set the plex point using a call to b_get_chmemloc. This is the only buffer function that returns a pointer */
	sym_table.pstvr[sym_table.st_offset].plex  = b_get_chmemloc(sym_table.plsBD, b_getsize(sym_table.plsBD));
	/* Set o_line to the current line number */
	sym_table.pstvr[sym_table.st_offset].o_line = line;
	/* Clear the status filed berfore setting the default value*/
	sym_table.pstvr[sym_table.st_offset].status_field &= DEFAULTZ;
	/* Set the status field to default values using the default bitmask */
	sym_table.pstvr[sym_table.st_offset].status_field |= SETDFLT; 
	
	/* Iterate through the lexeme and store it as a c-type string within the symbol table buffer */ 
	for( i=0;i<=strlen(lexeme);i++)
	{
		/* Store each character including the string terminator */ 
		if(!b_addc(sym_table.plsBD, lexeme[i]))
		{
			/* Returning -1 on b_addc failure. Allows program to handle failure in this function correctly */
			return B_ADDC_FAIL; 
		}
		/* Increment rFlag if it is set*/
		if(b_get_r_flag(sym_table.plsBD))
		{			
			++rFlag;			
		}			
	}
	/*  According to language specs the VID is a string if its last character is a # */
	if(lexeme[strlen(lexeme) -1] == '#')
	{
		/* Set the data type field to a string using a bitmask and set i_value to -1 */
		sym_table.pstvr[sym_table.st_offset].status_field |= SET012_111; 
		sym_table.pstvr[sym_table.st_offset].i_value.str_offset = I_VAL_STR;
	}
	else
	{		
		switch(lexeme[0])
		{
		/*  According to language specs the  type of an VID is Integer if it's first character is i, o, d, or n */
		case 'd': case 'i': case 'n': case 'o':
			/* Set the data type field to an int using a bitmask and set i_value to 0 */
			sym_table.pstvr[sym_table.st_offset].status_field |= SET12_01; 
			sym_table.pstvr[sym_table.st_offset].i_value.int_val = I_VAL_INT;
			break;

		/* According to the language specs the default type for a VID is float */
		default:
			/* Set the data type field to a float using a bitmask and set i_value to 0 */
			sym_table.pstvr[sym_table.st_offset].status_field |= SET12_10; 
			sym_table.pstvr[sym_table.st_offset].i_value.fpl_val = I_VAL_FLT;
			break;
		}
	}
	/* If the rflag is set we will need to handle reallocating plex pointers */
	if(rFlag)
	{
		/* Iterate though the array of STVRs */
		/* Cast to unsigned because the offset is and index value which cannot be negative*/
		for(i=0; i<= (unsigned)sym_table.st_offset; i++)
		{							
			/* Correct the plex pointers of each STVR using the lengths of the previous lexemes cumulatively */
			sym_table.pstvr[i].plex  = b_get_chmemloc(sym_table.plsBD, offset);
			/* Casting to a short ebcause the plex is stored in the buffer which could never be larget that SHRT_MAX*/
			offset += (short)strlen(sym_table.pstvr[i].plex)+1;
		}	
	}

	/* Increment the current st_offset and return st_offset */
	st_incoffset();
	return sym_table.st_offset;
}