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 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;
}