removed most seg faults

This commit is contained in:
Scarlett
2025-03-28 16:11:42 -04:00
parent cca01eb0b5
commit f217500b55
3 changed files with 63 additions and 53 deletions

View File

@ -6,6 +6,7 @@ CFLAGS :=
YACC := bison YACC := bison
TESTS-S1 := $(wildcard tests/sprint1/test/*.alpha) TESTS-S1 := $(wildcard tests/sprint1/test/*.alpha)
TESTS-S3 := $(wildcard tests/sprint3/test/*.alpha)
TESTS-S2 := $(wildcard tests/sprint2/test/*.alpha) TESTS-S2 := $(wildcard tests/sprint2/test/*.alpha)
compiler: clean runner compiler: clean runner
@ -32,13 +33,19 @@ runner: tmp/lex.yy.c tmp/runner.o tmp/symbol_table.o
debug: CFLAGS += -DDEBUG=1 debug: CFLAGS += -DDEBUG=1
debug: clean compiler debug: clean compiler
test: test-s1 test-s2 test: test-s1 test-s3 test-s2
test-s1: test-s1:
chmod +x ./check.sh chmod +x ./check.sh
$(foreach test, $(TESTS-S1), ./$(EXE) -tok $(test);) $(foreach test, $(TESTS-S1), ./$(EXE) -tok $(test);)
./check.sh ./check.sh
test-s3:
chmod +x ./check.sh
$(foreach test, $(TESTS-S3), ./$(EXE) -st $(test);)
./check.sh
test-s2: test-s2:
chmod +x ./check.sh chmod +x ./check.sh
$(foreach test, $(TESTS-S2), ./$(EXE) -st $(test);) $(foreach test, $(TESTS-S2), ./$(EXE) -st $(test);)

View File

@ -246,10 +246,10 @@ simple_statement:
; ;
assignable: assignable:
ID {$$ = $1; } ID {$$ = getType(look_up(cur,$1));}
| assignable ablock {$$ = getName(getReturn(look_up(cur, $1)));} | assignable ablock {$$ = getName(getReturn(look_up(cur, $1)));}
| assignable rec_op ID {if(undefined != (tn = table_lookup(getRecList(look_up(cur, $1)), $3))) | assignable rec_op ID {if(undefined != table_lookup(getRecList(look_up(cur, $1)), $3)){
{$$ = getType(tn);}} {$$ = getName(table_lookup(getRecList(look_up(cur, $1)), $3));}};}
; ;
rec_op : rec_op :

View File

@ -105,7 +105,7 @@ AdInfo *CreatePrimitiveInfo(int size) {
// only gets the size of a primitive type // only gets the size of a primitive type
int getPrimSize(TableNode *definition) { int getPrimSize(TableNode *definition) {
if (definition == NULL){ if (definition == NULL || definition == undefined){
printf("passed an NULL entry to getPrimSize function. Invalid.\n"); printf("passed an NULL entry to getPrimSize function. Invalid.\n");
return -1; return -1;
} }
@ -143,7 +143,7 @@ AdInfo *CreateArrayInfo(int dim, /*int* sizes,*/ TableNode *type) {
} }
// This gets the number of dimensions from array info // This gets the number of dimensions from array info
int getNumArrDim(TableNode *definition) { int getNumArrDim(TableNode *definition) {
if (definition == NULL){ if (definition == NULL|| definition == undefined){
printf("passed an NULL entry to getNumArrDim function. Invalid.\n"); printf("passed an NULL entry to getNumArrDim function. Invalid.\n");
return -1; return -1;
} }
@ -158,7 +158,7 @@ int getNumArrDim(TableNode *definition) {
TableNode *getArrType(TableNode *definition) { TableNode *getArrType(TableNode *definition) {
if (strcmp(getType(definition), "array") != 0) { if (strcmp(getType(definition), "array") != 0) {
printf("not checking the type of an array -- invalid op\n"); printf("not checking the type of an array -- invalid op\n");
return NULL; return undefined;
} }
return definition->additionalinfo->ArrayAdInfo->typeofarray; return definition->additionalinfo->ArrayAdInfo->typeofarray;
} }
@ -196,9 +196,9 @@ SymbolTable *getRecList(TableNode *definition) {
} }
TableNode* setRecSize(TableNode* tn, int n){ TableNode* setRecSize(TableNode* tn, int n){
if(tn == NULL){ if(tn == NULL||tn==undefined){
printf("passed in NULL entry for setRecSize. Invalid\n"); printf("passed in NULL entry for setRecSize. Invalid\n");
return NULL; return undefined;
} }
tn->additionalinfo->RecAdInfo->numofelements = n; tn->additionalinfo->RecAdInfo->numofelements = n;
return tn; return tn;
@ -215,12 +215,6 @@ int getRecSize(SymbolTable* tn){
} }
// below function takes a bool to see if parameter should be decomposed or not // below function takes a bool to see if parameter should be decomposed or not
assignable:
ID {$$ = $1; }
| assignable ablock {$$ = getName(getReturn(look_up(cur, $1)));}
| assignable rec_op ID {TableNode * tn; if(NULL != (tn = table_lookup(getRecList(look_up(cur, $1)), $3)))
{$$ = getType(tn);}
}
;// note that functions only take one input and have one output ;// note that functions only take one input and have one output
// using "as" the input record can be decomposed to give the illusion of // 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 // multiple inputs Below function also has the line number where the function is
@ -245,9 +239,9 @@ int getStartLine(TableNode *definition) {
} }
TableNode* setStartLine(TableNode* tn, int start){ TableNode* setStartLine(TableNode* tn, int start){
if(tn == NULL){ if(tn == NULL||tn==undefined){
printf("passing in a NULL entry to setStartLine. invalid\n"); printf("passing in a NULL or undefined entry to setStartLine. invalid\n");
return NULL; return undefined;
} }
tn->additionalinfo->FunDecAdInfo->startlinenumber = start; tn->additionalinfo->FunDecAdInfo->startlinenumber = start;
return tn; return tn;
@ -258,15 +252,15 @@ bool getAsKeyword(TableNode *definition) {
if (strcmp(getType(definition), "primitive function") != 0) { if (strcmp(getType(definition), "primitive function") != 0) {
printf("not checking if a function is called with as or not -- " printf("not checking if a function is called with as or not -- "
"invalid op\n"); "invalid op\n");
return NULL; return 0;
} }
return definition->additionalinfo->FunDecAdInfo->regularoras; return definition->additionalinfo->FunDecAdInfo->regularoras;
} }
TableNode* setAsKeyword(TableNode* tn, bool as){ TableNode* setAsKeyword(TableNode* tn, bool as){
if(tn == NULL){ if(tn == NULL||tn==undefined){
printf("passing in a NULL entry to setAsKeyword. invalid\n"); printf("passing in a NULL or undefined entry to setAsKeyword. invalid\n");
return NULL; return undefined;
} }
tn->additionalinfo->FunDecAdInfo->regularoras = as; tn->additionalinfo->FunDecAdInfo->regularoras = as;
return tn; return tn;
@ -286,7 +280,7 @@ TableNode *getParameter(TableNode *definition) {
if (strcmp(getType(definition), "primitive function type") != 0) { if (strcmp(getType(definition), "primitive function type") != 0) {
printf( printf(
"not checking the parameter of a function -- invalid op\n"); "not checking the parameter of a function -- invalid op\n");
return NULL; return undefined;
} }
return definition->additionalinfo->FunTypeAdInfo->parameter; return definition->additionalinfo->FunTypeAdInfo->parameter;
} }
@ -294,7 +288,7 @@ TableNode *getParameter(TableNode *definition) {
TableNode *getReturn(TableNode *definition) { TableNode *getReturn(TableNode *definition) {
if (strcmp(getType(definition), "primitive function type") != 0) { if (strcmp(getType(definition), "primitive function type") != 0) {
printf("not checking the return of a function -- invalid op\n"); printf("not checking the return of a function -- invalid op\n");
return NULL; return undefined;
} }
return definition->additionalinfo->FunTypeAdInfo->returntype; return definition->additionalinfo->FunTypeAdInfo->returntype;
} }
@ -444,17 +438,17 @@ TableNode* recprime;
TableNode* funtypeprime; TableNode* funtypeprime;
*/ */
TableNode* populateTypeAndInfo(TableNode* tn, TableNode* type, AdInfo* info){ TableNode* populateTypeAndInfo(TableNode* tn, TableNode* type, AdInfo* info){
if(tn == NULL){ if(tn == NULL||tn==undefined){
printf("passed in an invalid table node to modify (NULL).\n"); printf("passed in an invalid table node to modify (NULL or undefined).\n");
return NULL; return undefined;
} }
if(type == NULL){ if(type == NULL||type==undefined){
printf("passed in a NULL type reference to populate a table node. Invalid.\n"); printf("passed in a NULL or undefined type reference to populate a table node. Invalid.\n");
return NULL; return undefined;
} }
if(info == NULL){ if(info == NULL){
printf("passed in a NULL info reference to populate a table node. Invalid.\n"); printf("passed in a NULL info reference to populate a table node. Invalid.\n");
return NULL; return undefined;
} }
tn->theType = type; tn->theType = type;
tn->additionalinfo = info; tn->additionalinfo = info;
@ -463,12 +457,12 @@ TableNode* populateTypeAndInfo(TableNode* tn, TableNode* type, AdInfo* info){
} }
int getAdInfoType(TableNode* tn){ int getAdInfoType(TableNode* tn){
if(tn == NULL){ if(tn == NULL||tn==undefined){
printf("passing in NULL table entry. Invalid\n"); printf("passing in NULL or undefined table entry. Invalid\n");
return -1; return -1;
} }
if(tn->theType == NULL){ if(tn->theType == NULL|| tn->theType == undefined){
printf("Entry being passed in has a null reference for theType. Invalid.\n"); printf("Entry being passed in has a null or undefined reference for theType. Invalid.\n");
return -1; return -1;
} }
if(strcmp(getType(tn),getName(integ))==0){ if(strcmp(getType(tn),getName(integ))==0){
@ -508,7 +502,7 @@ TableNode *CreateEntry(SymbolTable *table, TableNode *typeOf, char *id,
if (table == NULL) { if (table == NULL) {
printf("Null reference to table"); printf("Null reference to table");
return NULL; return undefined;
} }
/* /*
TableNode* topDef = (table_lookup(getAncestor(table),typeOf)); TableNode* topDef = (table_lookup(getAncestor(table),typeOf));
@ -517,11 +511,11 @@ TableNode *CreateEntry(SymbolTable *table, TableNode *typeOf, char *id,
return NULL; return NULL;
} }
*/ */
if (typeOf == NULL) { if (typeOf == NULL||typeOf == undefined) {
printf("This is not pointing to a proper definition\n"); printf("This is not pointing to a proper definition (either NULL or undefined)\n");
return NULL; return undefined;
} }
TableNode *newEntry = (TableNode *)malloc(sizeof(TableNode)); TableNode *newEntry = (TableNode *)calloc(1,sizeof(TableNode));
newEntry->theType = typeOf /*topDef*/; newEntry->theType = typeOf /*topDef*/;
newEntry->theName = id; newEntry->theName = id;
newEntry->additionalinfo = ad; newEntry->additionalinfo = ad;
@ -537,23 +531,23 @@ TableNode *CreateEntry(SymbolTable *table, TableNode *typeOf, char *id,
} }
char *getType(TableNode *tn) { char *getType(TableNode *tn) {
if(tn == NULL){ if(tn == NULL||tn==undefined){
printf("passed a NULL table entry to getType\n"); printf("passed a NULL or undefined table entry to getType\n");
return getName(undefined); return getName(undefined);
} }
if(tn->theType == NULL){ if(tn->theType == NULL|| tn->theType == undefined){
printf("type of entry is currently NULL, undefined type \n"); printf("type of entry is currently NULL or undefined type \n");
return getName(undefined); return getName(undefined);
} }
return tn->theType->theName; } return tn->theType->theName; }
char *getName(TableNode *tn) { char *getName(TableNode *tn) {
if(tn == NULL){ if(tn == NULL||tn==undefined){
printf("passed a NULL table entry to getName\n"); //printf("passed a NULL or undefined table entry to getName\n");
return undefined->theName; return undefined->theName;
} }
if(tn->theName == NULL){ if(tn->theName == NULL){
printf("name of entry is currently NULL, undefined \n"); //printf("name of entry is currently NULL, undefined \n");
return undefined->theName; return undefined->theName;
} }
return tn->theName; return tn->theName;
@ -562,10 +556,20 @@ char *getName(TableNode *tn) {
int getLine(SymbolTable *st) { return st->Line_Number; } int getLine(SymbolTable *st) { return st->Line_Number; }
int getColumn(SymbolTable *st) { return st->Column_Number; } int getColumn(SymbolTable *st) { return st->Column_Number; }
TableNode* addName(TableNode *tn, char* str){ TableNode* addName(TableNode *tn, char* str){
if(tn == NULL){ if(tn == NULL||tn==undefined){
printf("passed a Null table node to the addName function. Invalid./n"); printf("passed a Null or undefined table node to the addName function. Invalid./n");
return tn; return undefined;
} }
if(tn->theName != NULL){
printf("Name doesn't look like it is empty before you change. Are you sure you need to update name?/n");
return undefined;
}
if(str == NULL){
printf("passed a NULL string to the addName function. Invalid./n");
return undefined;
}
tn->theName = str;
return tn;
} }
SymbolTable* setLineNumber(SymbolTable *st,int line){ SymbolTable* setLineNumber(SymbolTable *st,int line){
@ -647,7 +651,6 @@ TableNode *look_up(SymbolTable *table, char *x) {
void print_symbol_table(SymbolTable *table, FILE *file_ptr) { void print_symbol_table(SymbolTable *table, FILE *file_ptr) {
return;
if (table->Parent_Scope == NULL) { if (table->Parent_Scope == NULL) {
fprintf(file_ptr, "%-17s: %-6s : %-6s : %-21s: %-28s\n", "NAME", fprintf(file_ptr, "%-17s: %-6s : %-6s : %-21s: %-28s\n", "NAME",
"SCOPE", "PARENT", "TYPE", "Extra annotation"); "SCOPE", "PARENT", "TYPE", "Extra annotation");
@ -811,11 +814,11 @@ bool typeCheck(char *firstID, char *secondID) {
TableNode *entry1 = look_up(cur, firstID); TableNode *entry1 = look_up(cur, firstID);
TableNode *entry2 = look_up(cur, secondID); TableNode *entry2 = look_up(cur, secondID);
if (entry1 == NULL) { if (entry1 == NULL|| entry1 == undefined) {
printf("first type not defined\n"); printf("first type not defined\n");
return false; return false;
} }
if (entry2 == NULL) { if (entry2 == NULL|| entry2 == undefined) {
printf("second type not defined\n"); printf("second type not defined\n");
return false; return false;
} }