added a remove entry function
This commit is contained in:
@ -17,11 +17,18 @@
|
|||||||
extern int line_number;
|
extern int line_number;
|
||||||
extern int column_number;
|
extern int column_number;
|
||||||
extern FILE * yyin;
|
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}
|
//%define api.location.type {location_t}
|
||||||
%locations
|
%locations
|
||||||
%union {
|
%union {
|
||||||
//int integ;
|
int integ;
|
||||||
char * words;
|
char * words;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -225,9 +232,10 @@ constant:
|
|||||||
| C_FALSE {$$ = $<words>1;}
|
| C_FALSE {$$ = $<words>1;}
|
||||||
;
|
;
|
||||||
|
|
||||||
types:
|
types:
|
||||||
T_STRING {printf("string of T_STRING in types is %s\n",$<words>1);} {$$ = $<words>1;}
|
// Commented out T_String below
|
||||||
| T_INTEGER {printf("string of T_INTEGER in types is %s\n",$<words>1);} {$$ = $<words>1;}
|
// 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_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_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;}
|
| T_BOOLEAN {printf("string of T_BOOLEAN in types is %s\n",$<words>1);} {$$ = $<words>1;}
|
||||||
|
@ -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;}}
|
"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("type");return T_ADDRESS;}}
|
"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("type");return T_BOOLEAN;}}
|
"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("type");return T_CHARACTER;}}
|
"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;}}
|
"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;}}
|
"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;}}
|
||||||
|
@ -4,6 +4,11 @@
|
|||||||
#include "runner.h"
|
#include "runner.h"
|
||||||
extern TableNode* funprime;
|
extern TableNode* funprime;
|
||||||
extern TableNode* arrayprim;
|
extern TableNode* arrayprim;
|
||||||
|
extern TableNode* integ;
|
||||||
|
extern TableNode* addr;
|
||||||
|
extern TableNode* chara;
|
||||||
|
extern TableNode* stri;
|
||||||
|
extern TableNode* boo;
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
if (argc == 1) {
|
if (argc == 1) {
|
||||||
|
@ -11,6 +11,17 @@ char * funy = "function";
|
|||||||
TableNode* funprime;
|
TableNode* funprime;
|
||||||
TableNode* arrayprim;
|
TableNode* arrayprim;
|
||||||
extern SymbolTable* cur;
|
extern SymbolTable* cur;
|
||||||
|
TableNode* integ;
|
||||||
|
TableNode* addr;
|
||||||
|
TableNode* chara;
|
||||||
|
TableNode* stri;
|
||||||
|
TableNode* boo;
|
||||||
|
TableNode* recprime;
|
||||||
|
TableNode* funprime;
|
||||||
|
TableNode* arrayprime;
|
||||||
|
TableNode* funtypeprime;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
//First 4 below are primitive types that are all encapsulated in primitive type
|
//First 4 below are primitive types that are all encapsulated in primitive type
|
||||||
@ -86,6 +97,13 @@ AdInfo* CreatePrimitiveInfo(int size){
|
|||||||
return info;
|
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
|
//probably don't need the below structure since can create from an array
|
||||||
/*string_info* CreateStringInfo(int length, char* loc){
|
/*string_info* CreateStringInfo(int length, char* loc){
|
||||||
string_info* stringy = (string_info*)malloc(sizeof(string_info));
|
string_info* stringy = (string_info*)malloc(sizeof(string_info));
|
||||||
@ -94,6 +112,7 @@ AdInfo* CreatePrimitiveInfo(int size){
|
|||||||
return stringy;
|
return stringy;
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
AdInfo* CreateArrayInfo(int dim, /*int* sizes,*/ TableNode* type){
|
AdInfo* CreateArrayInfo(int dim, /*int* sizes,*/ TableNode* type){
|
||||||
AdInfo* info = (AdInfo*)malloc(sizeof(AdInfo));
|
AdInfo* info = (AdInfo*)malloc(sizeof(AdInfo));
|
||||||
info->ArrayAdInfo = (array_info*)malloc(sizeof(array_info));
|
info->ArrayAdInfo = (array_info*)malloc(sizeof(array_info));
|
||||||
@ -104,6 +123,20 @@ AdInfo* CreateArrayInfo(int dim, /*int* sizes,*/ TableNode* type){
|
|||||||
return info;
|
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* CreateRecordInfo(int length, TableNode* typesarray){
|
||||||
AdInfo* info = (AdInfo*)malloc(sizeof(AdInfo));
|
AdInfo* info = (AdInfo*)malloc(sizeof(AdInfo));
|
||||||
info->RecAdInfo = (record_info*)malloc(sizeof(record_info));
|
info->RecAdInfo = (record_info*)malloc(sizeof(record_info));
|
||||||
@ -112,6 +145,20 @@ AdInfo* CreateRecordInfo(int length, TableNode* typesarray){
|
|||||||
return info;
|
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
|
//below function takes a bool to see if parameter should be decomposed or not
|
||||||
AdInfo* CreateFunctionDeclarationInfo(int line, bool asorregular){
|
AdInfo* CreateFunctionDeclarationInfo(int line, bool asorregular){
|
||||||
AdInfo* info = (AdInfo*)malloc(sizeof(AdInfo));
|
AdInfo* info = (AdInfo*)malloc(sizeof(AdInfo));
|
||||||
@ -121,6 +168,20 @@ AdInfo* CreateFunctionDeclarationInfo(int line, bool asorregular){
|
|||||||
return info;
|
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* CreateFunctionTypeInfo(TableNode* parameter, TableNode* returntype){
|
||||||
AdInfo* info = (AdInfo*)malloc(sizeof(AdInfo));
|
AdInfo* info = (AdInfo*)malloc(sizeof(AdInfo));
|
||||||
info->FunTypeAdInfo = (function_type_info*)malloc(sizeof(function_type_info));
|
info->FunTypeAdInfo = (function_type_info*)malloc(sizeof(function_type_info));
|
||||||
@ -129,7 +190,19 @@ AdInfo* CreateFunctionTypeInfo(TableNode* parameter, TableNode* returntype){
|
|||||||
return info;
|
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");
|
printf("Cannot initialize a scope that is not the parent scope\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
TableNode* integ = (TableNode*)malloc(sizeof(SymbolTable));
|
integ = (TableNode*)malloc(sizeof(TableNode));
|
||||||
TableNode* addr = (TableNode*)malloc(sizeof(SymbolTable));
|
addr = (TableNode*)malloc(sizeof(TableNode));
|
||||||
TableNode* chara = (TableNode*)malloc(sizeof(SymbolTable));
|
chara = (TableNode*)malloc(sizeof(TableNode));
|
||||||
TableNode* stri = (TableNode*)malloc(sizeof(SymbolTable));
|
stri = (TableNode*)malloc(sizeof(TableNode));
|
||||||
TableNode* boo = (TableNode*)malloc(sizeof(SymbolTable));
|
boo = (TableNode*)malloc(sizeof(TableNode));
|
||||||
//TableNode* arr = (TableNode*)malloc(sizeof(SymbolTable));
|
//TableNode* arr = (TableNode*)malloc(sizeof(SymbolTable));
|
||||||
start->entries = integ;
|
start->entries = integ;
|
||||||
integ->next = addr;
|
integ->next = addr;
|
||||||
@ -183,31 +256,44 @@ SymbolTable* init(SymbolTable* start){
|
|||||||
addr->theName= "address";
|
addr->theName= "address";
|
||||||
chara->theName= "character";
|
chara->theName= "character";
|
||||||
boo->theName= "Boolean";
|
boo->theName= "Boolean";
|
||||||
stri->theName= "string";
|
stri->theName= "string primitive";
|
||||||
//arr->theName= "array"
|
//arr->theName= "array"
|
||||||
|
|
||||||
//root TableNode that all are pointing to but not in table
|
//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->theName= "primitive";
|
||||||
prime->theType=NULL;
|
prime->theType=NULL;
|
||||||
prime->additionalinfo = NULL;
|
prime->additionalinfo = NULL;
|
||||||
prime->next = 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
|
//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));
|
arrayprim = (TableNode*)malloc(sizeof(TableNode));
|
||||||
prime->theName= "array";
|
arrayprim->theName= "array";
|
||||||
prime->theType=NULL;
|
arrayprim->theType=NULL;
|
||||||
prime->additionalinfo = NULL;
|
arrayprim->additionalinfo = NULL;
|
||||||
prime->next = NULL;
|
prime->next = NULL;
|
||||||
|
|
||||||
//funprime = CreateEntry(NULL,NULL,strdup("function primitive"),NULL);
|
//funprime = CreateEntry(NULL,NULL,strdup("function primitive"),NULL);
|
||||||
|
|
||||||
//similar workaround to arrays above
|
//similar workaround to arrays above
|
||||||
TableNode* funprime = (TableNode*)malloc(sizeof(SymbolTable));
|
funprime = (TableNode*)malloc(sizeof(TableNode));
|
||||||
prime->theName= "primitive function";
|
funprime->theName= "primitive function";
|
||||||
prime->theType=NULL;
|
funprime->theType=NULL;
|
||||||
prime->additionalinfo = NULL;
|
funprime->additionalinfo = NULL;
|
||||||
prime->next = 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;
|
integ->theType=prime;
|
||||||
addr->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){
|
bool typeCheck(char* firstID, char* secondID){
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user