added reference to function scopes

This commit is contained in:
Partho
2025-05-03 14:16:31 -04:00
parent 5a47e40227
commit 6b239b0724
3 changed files with 86 additions and 12 deletions

View File

@ -456,12 +456,13 @@ int getRecSize(SymbolTable *tn) {
// 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));
AdInfo *CreateFunctionDeclarationInfo(int line, bool asorregular, SymbolTable *scope) {
AdInfo *info = (AdInfo *)calloc(1,sizeof(AdInfo));
info->FunDecAdInfo = (function_declaration_info *)malloc(
sizeof(function_declaration_info));
info->FunDecAdInfo->startlinenumber = line;
info->FunDecAdInfo->regularoras = asorregular;
info->FunDecAdInfo->scope = scope;
return info;
}
// gets the line at which the function was first defined. (Can be used to print
@ -530,6 +531,68 @@ bool getAsKeyword(TableNode *definition) {
return definition->additionalinfo->FunDecAdInfo->regularoras;
}
SymbolTable *getFunScope(TableNode *definition) {
if (definition == NULL) {
printdebug(
"passed a NULL entry to getScope "
"function. Invalid.");
return NULL;
}
if (getAdInfoType(definition) != TYPE_FUNCTION_DECLARATION) {
printdebug(
"passed in invalid entry type to getScope "
"function. Invalid.");
return NULL;
}
if (definition->additionalinfo == NULL) {
printdebug(
"node has NULL additionalinfo. Invalid.");
return NULL;
}
if(definition->additionalinfo->FunDecAdInfo == NULL) {
printdebug(
"node has NULL additionalinfo. Invalid.");
return NULL;
}
if(definition->additionalinfo->FunDecAdInfo->scope == NULL) {
printdebug(
"node has no scope initialized.");
return NULL;
}
return definition->additionalinfo->FunDecAdInfo->scope;
}
TableNode *setFunScope(TableNode *tn, SymbolTable *scope) {
if (tn == NULL) {
printdebug(
"passing in a NULL entry to setFunScope. "
"invalid");
return undefined;
}
if (getAdInfoType(tn) != TYPE_FUNCTION_DECLARATION) {
printdebug(
"passing in an invalid entry to setFunScope. "
"invalid");
return undefined;
}
if(tn->additionalinfo == NULL) {
printdebug(
"node has NULL additionalinfo. Invalid.");
return undefined;
}
if(tn->additionalinfo->FunDecAdInfo == NULL) {
printdebug(
"node has NULL additionalinfo. Invalid.");
return undefined;
}
if(scope == NULL) {
printdebug(
"passed in an empty scope.");
return undefined;
}
tn->additionalinfo->FunDecAdInfo->scope = scope;
return tn;
}
TableNode *setAsKeyword(TableNode *tn, bool as) {
if (tn == NULL) {
printdebug(
@ -787,10 +850,10 @@ SymbolTable *init(SymbolTable *start) {
chara->additionalinfo = CreatePrimitiveInfo(SIZE_CHAR);
stri->additionalinfo = CreateArrayInfo(1, chara);
boo->additionalinfo = CreatePrimitiveInfo(SIZE_BOOL);
reserve->additionalinfo = CreateFunctionDeclarationInfo(0, false);
reserve->additionalinfo = CreateFunctionDeclarationInfo(0, false,NULL);
reservetype->additionalinfo = CreateFunctionTypeInfo(integ, addr);
releasetype->additionalinfo = CreateFunctionTypeInfo(addr, integ);
release->additionalinfo = CreateFunctionDeclarationInfo(0, false);
release->additionalinfo = CreateFunctionDeclarationInfo(0, false,NULL);
integ->tag = TYPE_PRIMITIVE_TYPE; // explicitly set the type for integ
addr->tag = TYPE_PRIMITIVE_TYPE; // explicitly set the type for addr
@ -1308,10 +1371,16 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) {
if (getAdInfoType(entry) == TYPE_FUNCTION_DECLARATION) {
char *functiontype = (char *)malloc(100);
sprintf(functiontype, " %s", getName(getTypeEntry(entry)));
char* functionScope = (char *)malloc(100);
if(getLine(getFunScope(entry)) < 1){
sprintf(functionScope, " Function not defined before runtime");
}else{
sprintf(functionScope, " Function Definition that starts at line %d",getLine(getFunScope(entry)));
}
if (parentScopeNum == 0) {
st_fprint(file_ptr, getName(entry), currentScopeNum, -100, functiontype, " Function Definition");
st_fprint(file_ptr, getName(entry), currentScopeNum, -100, functiontype, functionScope);
} else {
st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, functiontype, " Function Definition");
st_fprint(file_ptr, getName(entry), currentScopeNum, parentScopeNum, functiontype, functionScope);
}
}