added comments to symbol table.c for clarity. Note- we need to add scope reference to record instead of array reference. Additionally, in print symbol table.c we need to update for if an entry is a function, it should print out parameter->return type

This commit is contained in:
Partho Bhattacharya
2025-03-24 11:59:53 -04:00
parent 78b3539de6
commit d817ceaf7d

View File

@ -86,7 +86,7 @@ typedef union {
FunTypeAdInfo* func_type_info;
}AdInfo;
*/
//primitive additional info only stores the size of that type
AdInfo* CreatePrimitiveInfo(int size){
AdInfo* info = (AdInfo*)malloc(sizeof(AdInfo));
@ -95,6 +95,7 @@ AdInfo* CreatePrimitiveInfo(int size){
return info;
}
//only gets the size of a primitive type
int getPrimSize(TableNode* definition){
if(strcmp(getType(definition),"primitive")!=0){
printf("not checking the size of a primitive -- invalid op\n");
@ -111,6 +112,8 @@ int getPrimSize(TableNode* definition){
}
*/
//Only information stored in array info is the number of dimensions and the type stored in the array
//per professor, the actual size of the array is calculated at runtime so bounds checking only needs to be done then
AdInfo* CreateArrayInfo(int dim, /*int* sizes,*/ TableNode* type){
AdInfo* info = (AdInfo*)malloc(sizeof(AdInfo));
info->ArrayAdInfo = (array_info*)malloc(sizeof(array_info));
@ -120,14 +123,14 @@ AdInfo* CreateArrayInfo(int dim, /*int* sizes,*/ TableNode* type){
//int* dimensionsizes = loc;
return info;
}
//This gets the number of dimensions from array info
int getNumArrDim(TableNode* definition){
if(strcmp(getType(definition),"array")!=0){
printf("not checking the dim of an array -- invalid op\n");
return 0;}
return definition->additionalinfo->ArrayAdInfo->numofdimensions;
}
//This gets the type stored in an array from arrtype. It returns a reference to the entry of that type
TableNode* getArrType(TableNode* definition){
if(strcmp(getType(definition),"array")!=0){
printf("not checking the type of an array -- invalid op\n");
@ -135,21 +138,25 @@ TableNode* getArrType(TableNode* definition){
return definition->additionalinfo->ArrayAdInfo->typeofarray;
}
//Record type currently stores the number of elements as well as the types, in order, of what make up that type in an array.
//Unfortunately this second part should probably instead be replaced by a reference to a scope in which those elements are found.
AdInfo* CreateRecordInfo(int length, TableNode* typesarray){
AdInfo* info = (AdInfo*)malloc(sizeof(AdInfo));
info->RecAdInfo = (record_info*)malloc(sizeof(record_info));
info->RecAdInfo->numofelements=length;
//replace below with reference to a scope, not an array
info->RecAdInfo->listoftypes = typesarray;
return info;
}
//This gets the number of elements that make up a record.
//Perhaps this may not be needed since we need to iterate over all elements anyways.
int getRecLength(TableNode* definition){
if(strcmp(getType(definition),"record")!=0){
printf("not checking the length of an record -- invalid op\n");
return 0;}
return definition->additionalinfo->RecAdInfo->numofelements;
}
//This gets the array. Needs to up be updated to get the scope instead
TableNode* getRecList(TableNode* definition){
if(strcmp(getType(definition),"record")!=0){
printf("not checking the list of types of a record -- invalid op\n");
@ -158,6 +165,9 @@ TableNode* getRecList(TableNode* definition){
}
//below function takes a bool to see if parameter should be decomposed or not
//note that functions only take one input and have one output
//using "as" the input record can be decomposed to give the illusion of multiple inputs
//Below function also has the line number where the function is first defined
AdInfo* CreateFunctionDeclarationInfo(int line, bool asorregular){
AdInfo* info = (AdInfo*)malloc(sizeof(AdInfo));
info->FunDecAdInfo = (function_declaration_info*)malloc(sizeof(function_declaration_info));
@ -165,21 +175,21 @@ AdInfo* CreateFunctionDeclarationInfo(int line, bool asorregular){
info->FunDecAdInfo->regularoras = asorregular;
return info;
}
//gets the line at which the function was first defined. (Can be used to print out in table if needed)
int getStartLine(TableNode* definition){
if(strcmp(getType(definition),"function primitive")!=0){
printf("not checking the start line of a function -- invalid op\n");
return 0;}
return definition->additionalinfo->FunDecAdInfo->startlinenumber;
}
//checks if "as" keyword was used for function definition. Either 0 or 1 for not used or used.
bool getAsKeyword(TableNode* definition){
if(strcmp(getType(definition),"function primitive")!=0){
printf("not checking if a function is called with as or not -- invalid op\n");
return NULL;}
return definition->additionalinfo->FunDecAdInfo->regularoras;
}
//stores the type of a function (parameter type and return type)
AdInfo* CreateFunctionTypeInfo(TableNode* parameter, TableNode* returntype){
AdInfo* info = (AdInfo*)malloc(sizeof(AdInfo));
info->FunTypeAdInfo = (function_type_info*)malloc(sizeof(function_type_info));
@ -187,14 +197,14 @@ AdInfo* CreateFunctionTypeInfo(TableNode* parameter, TableNode* returntype){
info->FunTypeAdInfo->returntype = returntype;
return info;
}
//returns parameter type of a function
TableNode* getParameter(TableNode* definition){
if(strcmp(getType(definition),"function type primitive")!=0){
printf("not checking the parameter of a function -- invalid op\n");
return NULL;}
return definition->additionalinfo->FunTypeAdInfo->parameter;
}
//returns return type of a function
TableNode* getReturn(TableNode* definition){
if(strcmp(getType(definition),"function type primitive")!=0){
printf("not checking the return of a function -- invalid op\n");
@ -203,7 +213,7 @@ TableNode* getReturn(TableNode* definition){
}
//creates a new scope (not the top scope though)
SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column) {
SymbolTable* table = (SymbolTable*)malloc(sizeof(SymbolTable));
table->Line_Number = Line;
@ -231,6 +241,7 @@ SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column) {
}
//create entry just for things below top level scope
//This function defines the integer, address, character, and bool primitive types
SymbolTable* init(SymbolTable* start){
if(start->Parent_Scope != NULL){
printf("Cannot initialize a scope that is not the parent scope\n");
@ -258,13 +269,18 @@ SymbolTable* init(SymbolTable* start){
//arr->theName= "array"
//root TableNode that all are pointing to but not in table
//This is only to solve the issue that all entries must have a name and a type
//and the type must point to an actual table entry
//Again, this primitive table entry isn't in the top scope. It is outside the top scope and is only there
//to facilitate the fact that these are primitive
TableNode* prime = (TableNode*)malloc(sizeof(TableNode));
prime->theName= "primitive";
prime->theType=NULL;
prime->additionalinfo = NULL;
prime->next = NULL;
//note sur exatly how to get array types to look right so using a dummy Table Node below and updating the print symbol table function to access the additional information to print for array types, similar to function types
//not sure exatly how to get array types to look right so using a dummy Table Node below and updating the print symbol table function to access the additional information to print for array types, similar to function types
//when printing symbol table, if array is seen
arrayprim = (TableNode*)malloc(sizeof(TableNode));
arrayprim->theName= "array";
arrayprim->theType=NULL;
@ -301,6 +317,9 @@ SymbolTable* init(SymbolTable* start){
//arr->theType=arrayprim;
//filling in all the values for the additional info for initial types
//These numbers below for create primitive specifically are supposed to be the size
//of these primitive types. We can change these if needed to not be hard coded numbers
//as a reminder, stri below is defined as a one dimensional array of characters
integ->additionalinfo = CreatePrimitiveInfo(4);
addr->additionalinfo = CreatePrimitiveInfo(8);
chara->additionalinfo = CreatePrimitiveInfo(1);