added a remove entry function

This commit is contained in:
Partho Bhattacharya
2025-03-14 19:58:53 -04:00
parent 8a1477c04d
commit ead70170c0
4 changed files with 153 additions and 24 deletions

View File

@ -17,11 +17,18 @@
extern int line_number;
extern int column_number;
extern FILE * yyin;
extern TableNode* funprime;
extern TableNode* arrayprim;
extern TableNode* integ;
extern TableNode* addr;
extern TableNode* chara;
extern TableNode* stri;
extern TableNode* boo;
%}
//%define api.location.type {location_t}
%locations
%union {
//int integ;
int integ;
char * words;
}
@ -226,8 +233,9 @@ constant:
;
types:
T_STRING {printf("string of T_STRING in types is %s\n",$<words>1);} {$$ = $<words>1;}
| T_INTEGER {printf("string of T_INTEGER in types is %s\n",$<words>1);} {$$ = $<words>1;}
// Commented out T_String below
// T_STRING {printf("string of T_STRING in types is %s\n",$<words>1);} {$$ = $<words>1;}
T_INTEGER {printf("string of T_INTEGER in types is %s\n",$<words>1);} {$$ = $<words>1;}
| T_ADDRESS {printf("string of T_ADDRESS in types is %s\n",$<words>1);} {$$ = $<words>1;}
| T_CHARACTER {printf("string of T_CHARACTER in types is %s\n",$<words>1);} {$$ = $<words>1;}
| T_BOOLEAN {printf("string of T_BOOLEAN in types is %s\n",$<words>1);} {$$ = $<words>1;}

View File

@ -36,10 +36,10 @@ SCHAR \\n|\\t|\\\"|[^\"\n\\]
%%
"integer" {if(DEBUG) {printf( "T_INTEGER: %s (%d)\n", yytext, T_INTEGER);} else {if(tok_flag != NULL){print_tok(T_INTEGER);}incr(line_number,column_number,T_INTEGER);yylval.words = strdup("type");return T_INTEGER;}}
"address" {if(DEBUG) {printf( "T_ADDRESS: %s (%d)\n", yytext, T_ADDRESS);} else {if(tok_flag != NULL){print_tok(T_ADDRESS);}incr(line_number,column_number,T_ADDRESS);yylval.words = strdup("type");return T_ADDRESS;}}
"Boolean" {if(DEBUG) {printf( "T_BOOLEAN: %s (%d)\n", yytext, T_BOOLEAN);} else {if(tok_flag != NULL){print_tok(T_INTEGER);}incr(line_number,column_number,T_INTEGER);yylval.words = strdup("type");return T_BOOLEAN;}}
"character" {if(DEBUG) {printf( "T_CHARACTER: %s (%d)\n", yytext, T_CHARACTER);} else {if(tok_flag != NULL){print_tok(T_CHARACTER);}incr(line_number,column_number,T_CHARACTER);yylval.words = strdup("type");return T_CHARACTER;}}
"integer" {if(DEBUG) {printf( "T_INTEGER: %s (%d)\n", yytext, T_INTEGER);} else {if(tok_flag != NULL){print_tok(T_INTEGER);}incr(line_number,column_number,T_INTEGER);yylval.words = strdup(yytext);return T_INTEGER;}}
"address" {if(DEBUG) {printf( "T_ADDRESS: %s (%d)\n", yytext, T_ADDRESS);} else {if(tok_flag != NULL){print_tok(T_ADDRESS);}incr(line_number,column_number,T_ADDRESS);yylval.words = strdup(yytext);return T_ADDRESS;}}
"Boolean" {if(DEBUG) {printf( "T_BOOLEAN: %s (%d)\n", yytext, T_BOOLEAN);} else {if(tok_flag != NULL){print_tok(T_INTEGER);}incr(line_number,column_number,T_INTEGER);yylval.words = strdup(yytext);return T_BOOLEAN;}}
"character" {if(DEBUG) {printf( "T_CHARACTER: %s (%d)\n", yytext, T_CHARACTER);} else {if(tok_flag != NULL){print_tok(T_CHARACTER);}incr(line_number,column_number,T_CHARACTER);yylval.words = strdup(yytext);return T_CHARACTER;}}
"while" {if(DEBUG) {printf( "WHILE: %s (%d)\n", yytext, WHILE);} else {if(tok_flag != NULL){print_tok(WHILE);}incr(line_number,column_number,WHILE);return WHILE;}}
"if" {if(DEBUG) {printf( "IF: %s (%d)\n", yytext, IF);} else {if(tok_flag != NULL){print_tok(IF);}incr(line_number,column_number,IF);return IF;}}

View File

@ -4,6 +4,11 @@
#include "runner.h"
extern TableNode* funprime;
extern TableNode* arrayprim;
extern TableNode* integ;
extern TableNode* addr;
extern TableNode* chara;
extern TableNode* stri;
extern TableNode* boo;
int main(int argc, char *argv[]) {
if (argc == 1) {

View File

@ -11,6 +11,17 @@ char * funy = "function";
TableNode* funprime;
TableNode* arrayprim;
extern SymbolTable* cur;
TableNode* integ;
TableNode* addr;
TableNode* chara;
TableNode* stri;
TableNode* boo;
TableNode* recprime;
TableNode* funprime;
TableNode* arrayprime;
TableNode* funtypeprime;
typedef enum {
//First 4 below are primitive types that are all encapsulated in primitive type
@ -86,6 +97,13 @@ AdInfo* CreatePrimitiveInfo(int size){
return info;
}
int getPrimSize(TableNode* definition){
if(strcmp(getType(definition),"primitive")!=0){
printf("not checking the size of a primitive -- invalid op\n");
return 0;}
return definition->additionalinfo->PrimAdInfo->size;
}
//probably don't need the below structure since can create from an array
/*string_info* CreateStringInfo(int length, char* loc){
string_info* stringy = (string_info*)malloc(sizeof(string_info));
@ -94,6 +112,7 @@ AdInfo* CreatePrimitiveInfo(int size){
return stringy;
}
*/
AdInfo* CreateArrayInfo(int dim, /*int* sizes,*/ TableNode* type){
AdInfo* info = (AdInfo*)malloc(sizeof(AdInfo));
info->ArrayAdInfo = (array_info*)malloc(sizeof(array_info));
@ -104,6 +123,20 @@ AdInfo* CreateArrayInfo(int dim, /*int* sizes,*/ TableNode* type){
return 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;
}
TableNode* getArrType(TableNode* definition){
if(strcmp(getType(definition),"array")!=0){
printf("not checking the type of an array -- invalid op\n");
return NULL;}
return definition->additionalinfo->ArrayAdInfo->typeofarray;
}
AdInfo* CreateRecordInfo(int length, TableNode* typesarray){
AdInfo* info = (AdInfo*)malloc(sizeof(AdInfo));
info->RecAdInfo = (record_info*)malloc(sizeof(record_info));
@ -112,6 +145,20 @@ AdInfo* CreateRecordInfo(int length, TableNode* typesarray){
return info;
}
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;
}
TableNode* getRecList(TableNode* definition){
if(strcmp(getType(definition),"record")!=0){
printf("not checking the list of types of a record -- invalid op\n");
return NULL;}
return definition->additionalinfo->RecAdInfo->listoftypes;
}
//below function takes a bool to see if parameter should be decomposed or not
AdInfo* CreateFunctionDeclarationInfo(int line, bool asorregular){
AdInfo* info = (AdInfo*)malloc(sizeof(AdInfo));
@ -121,6 +168,20 @@ AdInfo* CreateFunctionDeclarationInfo(int line, bool asorregular){
return info;
}
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;
}
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;
}
AdInfo* CreateFunctionTypeInfo(TableNode* parameter, TableNode* returntype){
AdInfo* info = (AdInfo*)malloc(sizeof(AdInfo));
info->FunTypeAdInfo = (function_type_info*)malloc(sizeof(function_type_info));
@ -129,7 +190,19 @@ AdInfo* CreateFunctionTypeInfo(TableNode* parameter, TableNode* returntype){
return info;
}
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;
}
TableNode* getReturn(TableNode* definition){
if(strcmp(getType(definition),"function type primitive")!=0){
printf("not checking the return of a function -- invalid op\n");
return NULL;}
return definition->additionalinfo->FunTypeAdInfo->returntype;
}
@ -165,11 +238,11 @@ SymbolTable* init(SymbolTable* start){
printf("Cannot initialize a scope that is not the parent scope\n");
return NULL;
}
TableNode* integ = (TableNode*)malloc(sizeof(SymbolTable));
TableNode* addr = (TableNode*)malloc(sizeof(SymbolTable));
TableNode* chara = (TableNode*)malloc(sizeof(SymbolTable));
TableNode* stri = (TableNode*)malloc(sizeof(SymbolTable));
TableNode* boo = (TableNode*)malloc(sizeof(SymbolTable));
integ = (TableNode*)malloc(sizeof(TableNode));
addr = (TableNode*)malloc(sizeof(TableNode));
chara = (TableNode*)malloc(sizeof(TableNode));
stri = (TableNode*)malloc(sizeof(TableNode));
boo = (TableNode*)malloc(sizeof(TableNode));
//TableNode* arr = (TableNode*)malloc(sizeof(SymbolTable));
start->entries = integ;
integ->next = addr;
@ -183,31 +256,44 @@ SymbolTable* init(SymbolTable* start){
addr->theName= "address";
chara->theName= "character";
boo->theName= "Boolean";
stri->theName= "string";
stri->theName= "string primitive";
//arr->theName= "array"
//root TableNode that all are pointing to but not in table
TableNode* prime = (TableNode*)malloc(sizeof(SymbolTable));
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
TableNode* arrayprim = (TableNode*)malloc(sizeof(SymbolTable));
prime->theName= "array";
prime->theType=NULL;
prime->additionalinfo = NULL;
arrayprim = (TableNode*)malloc(sizeof(TableNode));
arrayprim->theName= "array";
arrayprim->theType=NULL;
arrayprim->additionalinfo = NULL;
prime->next = NULL;
//funprime = CreateEntry(NULL,NULL,strdup("function primitive"),NULL);
//similar workaround to arrays above
TableNode* funprime = (TableNode*)malloc(sizeof(SymbolTable));
prime->theName= "primitive function";
prime->theType=NULL;
prime->additionalinfo = NULL;
prime->next = NULL;
funprime = (TableNode*)malloc(sizeof(TableNode));
funprime->theName= "primitive function";
funprime->theType=NULL;
funprime->additionalinfo = NULL;
funpprime->next = NULL;
//record
recprime = (TableNode*)malloc(sizeof(TableNode));
recprime->theName= "record";
recprime->theType=NULL;
recprime->additionalinfo = NULL;
recprime->next = NULL;
funtypeprime = (TableNode*)malloc(sizeof(TableNode));
funtypeprime->theName= "primitive function type";
funtypeprime->theType=NULL;
funtypeprime->additionalinfo = NULL;
funtypeprime->next = NULL;
integ->theType=prime;
addr->theType=prime;
@ -374,6 +460,36 @@ SymbolTable* getAncestor(SymbolTable* table) {
}
}
SymbolTable* removeEntry(SymbolTable* scope, char* search){
if(scope == NULL){
return NULL;
}
if(scope->entries == NULL){
return scope;
}
TableNode* prev = NULL;
TableNode* now = scope->entries;
while(now != NULL){
if(strcmp(getName(now),search)==0){
if(prev == NULL){
scope->entries = getNextEntry(now);
return scope;
} else{
prev->next = now->next;
return scope;
}
}
prev = now;
now = now->next;
}
return scope;
}
bool typeCheck(char* firstID, char* secondID){