adding function to get adinfo type
This commit is contained in:
@ -123,7 +123,8 @@ definition:
|
|||||||
| TYPE ID COLON constant ARROW ID
|
| TYPE ID COLON constant ARROW ID
|
||||||
| function_declaration
|
| function_declaration
|
||||||
| TYPE ID COLON id_or_types ARROW id_or_types {
|
| TYPE ID COLON id_or_types ARROW id_or_types {
|
||||||
CreateEntry(cur,funtypeprime,$2,CreateFunctionTypeInfo(table_lookup(cur,$4),table_lookup(cur,$6)));}
|
CreateEntry(cur,funtypeprime,$2,CreateFunctionTypeInfo(table_lookup(cur,$4),table_lookup(cur,$6)));
|
||||||
|
}
|
||||||
| ID parameter ASSIGN sblock
|
| ID parameter ASSIGN sblock
|
||||||
;
|
;
|
||||||
|
|
||||||
@ -192,15 +193,12 @@ rec_op :
|
|||||||
DOT
|
DOT
|
||||||
|
|
||||||
expression:
|
expression:
|
||||||
|
|
||||||
constant {printf("constant expression\n");} {$$ = $<words>1;}
|
constant {printf("constant expression\n");} {$$ = $<words>1;}
|
||||||
| SUB_OR_NEG expression %prec UMINUS {printf("negative expression\n");if(strcmp($2,"integer") != 0)
|
| SUB_OR_NEG expression %prec UMINUS {printf("negative expression\n");if(strcmp($2,"integer") != 0)
|
||||||
{printf("cant negate something not an integer at line %d and column %d\n",@2.first_line,@2.first_column);
|
{printf("cant negate something not an integer at line %d and column %d\n",@2.first_line,@2.first_column);
|
||||||
$$=strdup("undefined");}else{$$=$2;}}
|
$$=strdup("undefined");}else{$$=$2;}}
|
||||||
|
|
||||||
| NOT expression {printf("not expression\n"); if(strcmp($2,"Boolean")==0){$$=$2;}else{$$=strdup("undefined");
|
| NOT expression {printf("not expression\n"); if(strcmp($2,"Boolean")==0){$$=$2;}else{$$=strdup("undefined");
|
||||||
printf("mismatch at line %d and column %d\n",@1.first_line,@1.first_column);}}
|
printf("mismatch at line %d and column %d\n",@1.first_line,@1.first_column);}}
|
||||||
|
|
||||||
| expression ADD expression {printf("add expression\n");}
|
| expression ADD expression {printf("add expression\n");}
|
||||||
| expression SUB_OR_NEG expression {printf("subtract expression\n");}
|
| expression SUB_OR_NEG expression {printf("subtract expression\n");}
|
||||||
| expression MUL expression {printf("multiply expression\n");}
|
| expression MUL expression {printf("multiply expression\n");}
|
||||||
@ -263,29 +261,3 @@ types:
|
|||||||
void yyerror(const char *err) {
|
void yyerror(const char *err) {
|
||||||
fprintf(stderr, "ERROR: %s at token %s at line number %d,column number %d\n", err,yytext,yylloc.first_line,yylloc.first_column);
|
fprintf(stderr, "ERROR: %s at token %s at line number %d,column number %d\n", err,yytext,yylloc.first_line,yylloc.first_column);
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
int main(int argc, char * argv[]) {
|
|
||||||
token_tracker = 1;
|
|
||||||
cur=CreateScope(NULL,1,1);
|
|
||||||
//int a;
|
|
||||||
FILE * fp;
|
|
||||||
if(argc > 1){
|
|
||||||
fp = fopen(argv[1], "r");
|
|
||||||
yyin = fp;
|
|
||||||
} else {
|
|
||||||
fp = stdin;
|
|
||||||
yyin = fp;
|
|
||||||
}
|
|
||||||
yyparse();
|
|
||||||
//while ((a = yyparse() != EOF){
|
|
||||||
// token_tracker++;
|
|
||||||
//printf("%d = a: yytext = %s: yychar = %d, token number: %d\n", a, yytext, yychar,token_tracker);
|
|
||||||
//if(yytext[0] == '\n'){
|
|
||||||
FILE* f = fdopen(1,"w");
|
|
||||||
print_symbol_table(getAncestor(cur),f);
|
|
||||||
fclose(f);
|
|
||||||
// break;
|
|
||||||
//}
|
|
||||||
//}
|
|
||||||
return 0;
|
|
||||||
} */
|
|
||||||
|
30
src/runner.c
30
src/runner.c
@ -29,13 +29,16 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
else {
|
else {
|
||||||
if (is_alpha_file(argv[argc - 1], strlen(argv[argc - 1])) != 0) {
|
if (is_alpha_file(argv[argc - 1], strlen(argv[argc - 1])) !=
|
||||||
|
0) {
|
||||||
fprintf(stderr, INVALID);
|
fprintf(stderr, INVALID);
|
||||||
return -1;
|
return -1;
|
||||||
} else {
|
} else {
|
||||||
for (int i = 1; i < argc - 1; i++) {
|
for (int i = 1; i < argc - 1; i++) {
|
||||||
if (check_flag(argv[i], argv[argc - 1]) != 0) {
|
if (check_flag(argv[i], argv[argc - 1]) != 0) {
|
||||||
fprintf(stderr, "INVALID FLAG(S): Use -help to view valid inputs \n");
|
fprintf(stderr,
|
||||||
|
"INVALID FLAG(S): Use -help to "
|
||||||
|
"view valid inputs \n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -78,7 +81,8 @@ void incr(int lnum,int cnum, int tok){
|
|||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
void print_tok(int tok) {
|
void print_tok(int tok) {
|
||||||
fprintf(tok_flag, "%d %d %3d \"%s\"\n", line_number, column_number,tok, yytext);
|
fprintf(tok_flag, "%d %d %3d \"%s\"\n", line_number, column_number, tok,
|
||||||
|
yytext);
|
||||||
}
|
}
|
||||||
int run(FILE *alpha) {
|
int run(FILE *alpha) {
|
||||||
int token;
|
int token;
|
||||||
@ -95,7 +99,8 @@ int run(FILE *alpha) {
|
|||||||
if (tok_flag != NULL) {
|
if (tok_flag != NULL) {
|
||||||
while (0 != (token = yylex())) {
|
while (0 != (token = yylex())) {
|
||||||
// if (tok_flag != NULL) {
|
// if (tok_flag != NULL) {
|
||||||
// fprintf(tok_flag, "%d %d %3d \"%s\"\n", line_number, column_number,
|
// fprintf(tok_flag, "%d %d %3d \"%s\"\n",
|
||||||
|
// line_number, column_number,
|
||||||
// token, yytext);
|
// token, yytext);
|
||||||
// }
|
// }
|
||||||
/*if (token == COMMENT) {
|
/*if (token == COMMENT) {
|
||||||
@ -110,9 +115,10 @@ int run(FILE *alpha) {
|
|||||||
}
|
}
|
||||||
if (token == 1999) {
|
if (token == 1999) {
|
||||||
printf(
|
printf(
|
||||||
"On line number %d and column number %d we have an invalid "
|
"On line number %d and column
|
||||||
"character:%s\n",
|
number %d we have an invalid " "character:%s\n",
|
||||||
line_number, column_number, yytext);
|
line_number, column_number,
|
||||||
|
yytext);
|
||||||
}
|
}
|
||||||
column_number += yyleng; */
|
column_number += yyleng; */
|
||||||
}
|
}
|
||||||
@ -172,7 +178,8 @@ int new_file(char *arg, char *alpha) {
|
|||||||
} else if (strcmp(arg, "-st") == 0) {
|
} else if (strcmp(arg, "-st") == 0) {
|
||||||
type_len = ST_LEN;
|
type_len = ST_LEN;
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "INVALID FLAG: Use -help to view valid inputs\n");
|
fprintf(stderr,
|
||||||
|
"INVALID FLAG: Use -help to view valid inputs\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -195,15 +202,14 @@ int new_file(char *arg, char *alpha) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int is_alpha_file(char *alpha, int file_len) {
|
int is_alpha_file(char *alpha, int file_len) {
|
||||||
if (strcmp(".alpha", alpha + sizeof(char) * (file_len - ALPHA_OFFSET)) != 0) {
|
if (strcmp(".alpha",
|
||||||
|
alpha + sizeof(char) * (file_len - ALPHA_OFFSET)) != 0) {
|
||||||
return -1; // not alpha file
|
return -1; // not alpha file
|
||||||
}
|
}
|
||||||
return 0; // is alpha file
|
return 0; // is alpha file
|
||||||
}
|
}
|
||||||
|
|
||||||
void enter_scope(int line, int column) {
|
void enter_scope(int line, int column) { cur = CreateScope(cur, line, column); }
|
||||||
cur = CreateScope(cur, line, column);
|
|
||||||
}
|
|
||||||
void exit_scope() {
|
void exit_scope() {
|
||||||
if (cur->Parent_Scope == NULL) {
|
if (cur->Parent_Scope == NULL) {
|
||||||
printf("Can't close top");
|
printf("Can't close top");
|
||||||
|
12
src/runner.h
12
src/runner.h
@ -5,10 +5,14 @@
|
|||||||
#define TOK_LEN 3
|
#define TOK_LEN 3
|
||||||
#define ST_LEN 2
|
#define ST_LEN 2
|
||||||
#define HELP \
|
#define HELP \
|
||||||
"HELP:\nHow to run the alpha compiler:\n./alpha [options] program\nValid " \
|
"HELP:\nHow to run the alpha compiler:\n./alpha [options] " \
|
||||||
"options:\n-tok output the token number, token, line number, and column " \
|
"program\nValid " \
|
||||||
"number for each of the tokens to the .tok file\n-st output the symbol " \
|
"options:\n-tok output the token number, token, line number, and " \
|
||||||
"table for the program to the .st file\n-help print this message and exit " \
|
"column " \
|
||||||
|
"number for each of the tokens to the .tok file\n-st output the " \
|
||||||
|
"symbol " \
|
||||||
|
"table for the program to the .st file\n-help print this message " \
|
||||||
|
"and exit " \
|
||||||
"the alpha compiler\n"
|
"the alpha compiler\n"
|
||||||
#define SET_FLAG 1 // Used to set flags for arg types
|
#define SET_FLAG 1 // Used to set flags for arg types
|
||||||
#define INVALID \
|
#define INVALID \
|
||||||
|
@ -19,10 +19,9 @@ TableNode* boo;
|
|||||||
TableNode *recprime;
|
TableNode *recprime;
|
||||||
TableNode *funtypeprime;
|
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
|
||||||
// TYPE_INTEGER,
|
// TYPE_INTEGER,
|
||||||
// TYPE_CHARACTER,
|
// TYPE_CHARACTER,
|
||||||
// TYPE_BOOLEAN,
|
// TYPE_BOOLEAN,
|
||||||
@ -37,11 +36,13 @@ typedef enum {
|
|||||||
TYPE_FUNCTION_DECLARATION,
|
TYPE_FUNCTION_DECLARATION,
|
||||||
// Declaring what type a particular function is with as
|
// Declaring what type a particular function is with as
|
||||||
// TYPE_AS_FUNCTION_DECLARATION,
|
// TYPE_AS_FUNCTION_DECLARATION,
|
||||||
//Declaring what type a function is (what the parameters and output are)
|
// Declaring what type a function is (what the parameters and output
|
||||||
|
// are)
|
||||||
TYPE_FUNCTION_TYPE,
|
TYPE_FUNCTION_TYPE,
|
||||||
//The Type being pointed to by the first 4 above that only stores the size
|
// The Type being pointed to by the first 4 above that only stores the
|
||||||
|
// size
|
||||||
TYPE_PRIMITIVE,
|
TYPE_PRIMITIVE,
|
||||||
//All else (NULL case likely)
|
//likely NULL
|
||||||
TYPE_ALL_ELSE
|
TYPE_ALL_ELSE
|
||||||
|
|
||||||
} types;
|
} types;
|
||||||
@ -60,13 +61,14 @@ typedef struct{
|
|||||||
|
|
||||||
typedef struct{
|
typedef struct{
|
||||||
int numofdimensions;
|
int numofdimensions;
|
||||||
//the above value tells you how long the below array is. For example if num of dimensions is 5, I can store 1,3,2,5,9 to define >
|
//the above value tells you how long the below array is. For example if num
|
||||||
int* sizesofdimensions;
|
of dimensions is 5, I can store 1,3,2,5,9 to define > int* sizesofdimensions;
|
||||||
TableNode* typeofarray;
|
TableNode* typeofarray;
|
||||||
}array_info;
|
}array_info;
|
||||||
|
|
||||||
typedef struct{
|
typedef struct{
|
||||||
//similar to above we define a record to hold the number of elements and an array of tablenodes (types) that it contains in the order specified by the user
|
//similar to above we define a record to hold the number of elements and an
|
||||||
|
array of tablenodes (types) that it contains in the order specified by the user
|
||||||
int numofelements;
|
int numofelements;
|
||||||
TableNode* listoftypes;
|
TableNode* listoftypes;
|
||||||
}record_info;
|
}record_info;
|
||||||
@ -103,7 +105,8 @@ AdInfo* CreatePrimitiveInfo(int size){
|
|||||||
int getPrimSize(TableNode *definition) {
|
int getPrimSize(TableNode *definition) {
|
||||||
if (strcmp(getType(definition), "primitive") != 0) {
|
if (strcmp(getType(definition), "primitive") != 0) {
|
||||||
printf("not checking the size of a primitive -- invalid op\n");
|
printf("not checking the size of a primitive -- invalid op\n");
|
||||||
return 0;}
|
return 0;
|
||||||
|
}
|
||||||
return definition->additionalinfo->PrimAdInfo->size;
|
return definition->additionalinfo->PrimAdInfo->size;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,8 +119,9 @@ int getPrimSize(TableNode* definition){
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
//Only information stored in array info is the number of dimensions and the type stored in the array
|
// Only information stored in array info is the number of dimensions and the
|
||||||
//per professor, the actual size of the array is calculated at runtime so bounds checking only needs to be done then
|
// 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 *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));
|
||||||
@ -131,19 +135,24 @@ AdInfo* CreateArrayInfo(int dim, /*int* sizes,*/ TableNode* type){
|
|||||||
int getNumArrDim(TableNode *definition) {
|
int getNumArrDim(TableNode *definition) {
|
||||||
if (strcmp(getType(definition), "array") != 0) {
|
if (strcmp(getType(definition), "array") != 0) {
|
||||||
printf("not checking the dim of an array -- invalid op\n");
|
printf("not checking the dim of an array -- invalid op\n");
|
||||||
return 0;}
|
return 0;
|
||||||
|
}
|
||||||
return definition->additionalinfo->ArrayAdInfo->numofdimensions;
|
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
|
// This gets the type stored in an array from arrtype. It returns a reference to
|
||||||
|
// the entry of that type
|
||||||
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 NULL;
|
||||||
|
}
|
||||||
return definition->additionalinfo->ArrayAdInfo->typeofarray;
|
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.
|
// Record type currently stores the number of elements as well as the types, in
|
||||||
//Unfortunately this second part should probably instead be replaced by a reference to a scope in which those elements are found.
|
// 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 *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));
|
||||||
@ -153,50 +162,63 @@ AdInfo* CreateRecordInfo(int length, TableNode* typesarray){
|
|||||||
return info;
|
return info;
|
||||||
}
|
}
|
||||||
// This gets the number of elements that make up a record.
|
// 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.
|
// Perhaps this may not be needed since we need to iterate over all elements
|
||||||
|
// anyways.
|
||||||
int getRecLength(TableNode *definition) {
|
int getRecLength(TableNode *definition) {
|
||||||
if (strcmp(getType(definition), "record") != 0) {
|
if (strcmp(getType(definition), "record") != 0) {
|
||||||
printf("not checking the length of an record -- invalid op\n");
|
printf("not checking the length of an record -- invalid op\n");
|
||||||
return 0;}
|
return 0;
|
||||||
|
}
|
||||||
return definition->additionalinfo->RecAdInfo->numofelements;
|
return definition->additionalinfo->RecAdInfo->numofelements;
|
||||||
}
|
}
|
||||||
// This gets the array. Needs to up be updated to get the scope instead
|
// This gets the array. Needs to up be updated to get the scope instead
|
||||||
TableNode *getRecList(TableNode *definition) {
|
TableNode *getRecList(TableNode *definition) {
|
||||||
if (strcmp(getType(definition), "record") != 0) {
|
if (strcmp(getType(definition), "record") != 0) {
|
||||||
printf("not checking the list of types of a record -- invalid op\n");
|
printf("not checking the list of types of a record -- invalid "
|
||||||
return NULL;}
|
"op\n");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
return definition->additionalinfo->RecAdInfo->listoftypes;
|
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
|
||||||
// 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 multiple inputs
|
// using "as" the input record can be decomposed to give the illusion of
|
||||||
//Below function also has the line number where the function is first defined
|
// multiple inputs Below function also has the line number where the function is
|
||||||
|
// first defined
|
||||||
AdInfo *CreateFunctionDeclarationInfo(int line, bool asorregular) {
|
AdInfo *CreateFunctionDeclarationInfo(int line, bool asorregular) {
|
||||||
AdInfo *info = (AdInfo *)malloc(sizeof(AdInfo));
|
AdInfo *info = (AdInfo *)malloc(sizeof(AdInfo));
|
||||||
info->FunDecAdInfo = (function_declaration_info*)malloc(sizeof(function_declaration_info));
|
info->FunDecAdInfo = (function_declaration_info *)malloc(
|
||||||
|
sizeof(function_declaration_info));
|
||||||
info->FunDecAdInfo->startlinenumber = line;
|
info->FunDecAdInfo->startlinenumber = line;
|
||||||
info->FunDecAdInfo->regularoras = asorregular;
|
info->FunDecAdInfo->regularoras = asorregular;
|
||||||
return info;
|
return info;
|
||||||
}
|
}
|
||||||
//gets the line at which the function was first defined. (Can be used to print out in table if needed)
|
// gets the line at which the function was first defined. (Can be used to print
|
||||||
|
// out in table if needed)
|
||||||
int getStartLine(TableNode *definition) {
|
int getStartLine(TableNode *definition) {
|
||||||
if (strcmp(getType(definition), "function primitive") != 0) {
|
if (strcmp(getType(definition), "function primitive") != 0) {
|
||||||
printf("not checking the start line of a function -- invalid op\n");
|
printf("not checking the start line of a function -- invalid "
|
||||||
return 0;}
|
"op\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
return definition->additionalinfo->FunDecAdInfo->startlinenumber;
|
return definition->additionalinfo->FunDecAdInfo->startlinenumber;
|
||||||
}
|
}
|
||||||
//checks if "as" keyword was used for function definition. Either 0 or 1 for not used or used.
|
// checks if "as" keyword was used for function definition. Either 0 or 1 for
|
||||||
|
// not used or used.
|
||||||
bool getAsKeyword(TableNode *definition) {
|
bool getAsKeyword(TableNode *definition) {
|
||||||
if (strcmp(getType(definition), "function primitive") != 0) {
|
if (strcmp(getType(definition), "function primitive") != 0) {
|
||||||
printf("not checking if a function is called with as or not -- invalid op\n");
|
printf("not checking if a function is called with as or not -- "
|
||||||
return NULL;}
|
"invalid op\n");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
return definition->additionalinfo->FunDecAdInfo->regularoras;
|
return definition->additionalinfo->FunDecAdInfo->regularoras;
|
||||||
}
|
}
|
||||||
// stores the type of a function (parameter type and return type)
|
// stores the type of a function (parameter type and return type)
|
||||||
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));
|
||||||
info->FunTypeAdInfo->parameter = parameter;
|
info->FunTypeAdInfo->parameter = parameter;
|
||||||
info->FunTypeAdInfo->returntype = returntype;
|
info->FunTypeAdInfo->returntype = returntype;
|
||||||
return info;
|
return info;
|
||||||
@ -204,19 +226,21 @@ AdInfo* CreateFunctionTypeInfo(TableNode* parameter, TableNode* returntype){
|
|||||||
// returns parameter type of a function
|
// returns parameter type of a function
|
||||||
TableNode *getParameter(TableNode *definition) {
|
TableNode *getParameter(TableNode *definition) {
|
||||||
if (strcmp(getType(definition), "function type primitive") != 0) {
|
if (strcmp(getType(definition), "function type primitive") != 0) {
|
||||||
printf("not checking the parameter of a function -- invalid op\n");
|
printf(
|
||||||
return NULL;}
|
"not checking the parameter of a function -- invalid op\n");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
return definition->additionalinfo->FunTypeAdInfo->parameter;
|
return definition->additionalinfo->FunTypeAdInfo->parameter;
|
||||||
}
|
}
|
||||||
// returns return type of a function
|
// returns return type of a function
|
||||||
TableNode *getReturn(TableNode *definition) {
|
TableNode *getReturn(TableNode *definition) {
|
||||||
if (strcmp(getType(definition), "function type primitive") != 0) {
|
if (strcmp(getType(definition), "function type primitive") != 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 NULL;
|
||||||
|
}
|
||||||
return definition->additionalinfo->FunTypeAdInfo->returntype;
|
return definition->additionalinfo->FunTypeAdInfo->returntype;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// creates a new scope (not the top scope though)
|
// creates a new scope (not the top scope though)
|
||||||
SymbolTable *CreateScope(SymbolTable *ParentScope, int Line, int Column) {
|
SymbolTable *CreateScope(SymbolTable *ParentScope, int Line, int Column) {
|
||||||
SymbolTable *table = (SymbolTable *)malloc(sizeof(SymbolTable));
|
SymbolTable *table = (SymbolTable *)malloc(sizeof(SymbolTable));
|
||||||
@ -227,13 +251,15 @@ SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column) {
|
|||||||
table->entries = NULL;
|
table->entries = NULL;
|
||||||
if (ParentScope != NULL) {
|
if (ParentScope != NULL) {
|
||||||
if (ParentScope->Children_Scope == NULL) {
|
if (ParentScope->Children_Scope == NULL) {
|
||||||
ListOfTable* newEntry = (ListOfTable*)malloc(sizeof(ListOfTable));
|
ListOfTable *newEntry =
|
||||||
|
(ListOfTable *)malloc(sizeof(ListOfTable));
|
||||||
newEntry->next = NULL;
|
newEntry->next = NULL;
|
||||||
// newEntry->prev = NULL;
|
// newEntry->prev = NULL;
|
||||||
newEntry->table = table;
|
newEntry->table = table;
|
||||||
ParentScope->Children_Scope = newEntry;
|
ParentScope->Children_Scope = newEntry;
|
||||||
} else {
|
} else {
|
||||||
ListOfTable* newEntry = (ListOfTable*)malloc(sizeof(ListOfTable));
|
ListOfTable *newEntry =
|
||||||
|
(ListOfTable *)malloc(sizeof(ListOfTable));
|
||||||
// newEntry->prev = NULL;
|
// newEntry->prev = NULL;
|
||||||
newEntry->table = table;
|
newEntry->table = table;
|
||||||
ListOfTable *oldEntry = ParentScope->Children_Scope;
|
ListOfTable *oldEntry = ParentScope->Children_Scope;
|
||||||
@ -245,10 +271,12 @@ SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// create entry just for things below top level scope
|
// create entry just for things below top level scope
|
||||||
//This function defines the integer, address, character, and bool primitive types
|
// This function defines the integer, address, character, and bool primitive
|
||||||
|
// types
|
||||||
SymbolTable *init(SymbolTable *start) {
|
SymbolTable *init(SymbolTable *start) {
|
||||||
if (start->Parent_Scope != NULL) {
|
if (start->Parent_Scope != NULL) {
|
||||||
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;
|
||||||
}
|
}
|
||||||
integ = (TableNode *)malloc(sizeof(TableNode));
|
integ = (TableNode *)malloc(sizeof(TableNode));
|
||||||
@ -273,18 +301,21 @@ SymbolTable* init(SymbolTable* start){
|
|||||||
// 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
|
||||||
//This is only to solve the issue that all entries must have a name and a type
|
// This is only to solve the issue that all entries must have a name and
|
||||||
//and the type must point to an actual table entry
|
// a type and the type must point to an actual table entry Again, this
|
||||||
//Again, this primitive table entry isn't in the top scope. It is outside the top scope and is only there
|
// primitive table entry isn't in the top scope. It is outside the top
|
||||||
//to facilitate the fact that these are primitive
|
// scope and is only there to facilitate the fact that these are
|
||||||
|
// primitive
|
||||||
TableNode *prime = (TableNode *)malloc(sizeof(TableNode));
|
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;
|
||||||
|
|
||||||
//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
|
// not sure exatly how to get array types to look right so using a dummy
|
||||||
//when printing symbol table, if array is seen
|
// 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 = (TableNode *)malloc(sizeof(TableNode));
|
||||||
arrayprim->theName = "array";
|
arrayprim->theName = "array";
|
||||||
arrayprim->theType = NULL;
|
arrayprim->theType = NULL;
|
||||||
@ -321,9 +352,10 @@ SymbolTable* init(SymbolTable* start){
|
|||||||
// arr->theType=arrayprim;
|
// arr->theType=arrayprim;
|
||||||
|
|
||||||
// filling in all the values for the additional info for initial types
|
// 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
|
// These numbers below for create primitive specifically are supposed to
|
||||||
//of these primitive types. We can change these if needed to not be hard coded numbers
|
// be the size of these primitive types. We can change these if needed
|
||||||
//as a reminder, stri below is defined as a one dimensional array of characters
|
// to not be hard coded numbers as a reminder, stri below is defined as
|
||||||
|
// a one dimensional array of characters
|
||||||
integ->additionalinfo = CreatePrimitiveInfo(4);
|
integ->additionalinfo = CreatePrimitiveInfo(4);
|
||||||
addr->additionalinfo = CreatePrimitiveInfo(8);
|
addr->additionalinfo = CreatePrimitiveInfo(8);
|
||||||
chara->additionalinfo = CreatePrimitiveInfo(1);
|
chara->additionalinfo = CreatePrimitiveInfo(1);
|
||||||
@ -338,8 +370,48 @@ SymbolTable* init(SymbolTable* start){
|
|||||||
|
|
||||||
return start;
|
return start;
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
TableNode* integ;
|
||||||
|
TableNode* addr;
|
||||||
|
TableNode* chara;
|
||||||
|
TableNode* stri;
|
||||||
|
TableNode* boo;
|
||||||
|
TableNode* recprime;
|
||||||
|
TableNode* funtypeprime;
|
||||||
|
*/
|
||||||
|
|
||||||
TableNode* CreateEntry(SymbolTable* table, TableNode* typeOf, char* id, AdInfo* ad) {
|
int getAdInfoType(TableNode* tn){
|
||||||
|
if(strcmp(getType(tn),getName(integ))==0){
|
||||||
|
return TYPE_PRIMITIVE;
|
||||||
|
}
|
||||||
|
if(strcmp(getType(tn),getName(addr))==0){
|
||||||
|
return TYPE_PRIMITIVE;
|
||||||
|
}
|
||||||
|
if(strcmp(getType(tn),getName(chara))==0){
|
||||||
|
return TYPE_PRIMITIVE;
|
||||||
|
}
|
||||||
|
if(strcmp(getType(tn),getName(stri))==0){
|
||||||
|
return TYPE_ARRAY;
|
||||||
|
}
|
||||||
|
if(strcmp(getType(tn),getName(boo))==0){
|
||||||
|
return TYPE_PRIMITIVE;
|
||||||
|
}
|
||||||
|
if(strcmp(getType(tn),getName(recprime))==0){
|
||||||
|
return TYPE_RECORD;
|
||||||
|
}
|
||||||
|
if(strcmp(getType(tn),getName(funtypeprime))==0){
|
||||||
|
return TYPE_FUNCTION_TYPE;
|
||||||
|
}
|
||||||
|
if(strcmp(getType(tn),getName(arrayprim))==0){
|
||||||
|
return TYPE_ARRAY;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
return TYPE_FUNCTION_DECLARATION;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TableNode *CreateEntry(SymbolTable *table, TableNode *typeOf, char *id,
|
||||||
|
AdInfo *ad) {
|
||||||
|
|
||||||
if (table == NULL) {
|
if (table == NULL) {
|
||||||
printf("Null reference to table");
|
printf("Null reference to table");
|
||||||
@ -370,49 +442,12 @@ if(typeOf == NULL){
|
|||||||
return newEntry;
|
return newEntry;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
TableNode* integ;
|
|
||||||
TableNode* addr;
|
|
||||||
TableNode* chara;
|
|
||||||
TableNode* stri;
|
|
||||||
TableNode* boo;
|
|
||||||
TableNode* recprime;
|
|
||||||
TableNode* funtypeprime;
|
|
||||||
*/
|
|
||||||
|
|
||||||
char *getType(TableNode *tn) { return tn->theType->theName; }
|
char *getType(TableNode *tn) { return tn->theType->theName; }
|
||||||
int getAdInfoType(TableNode* tn){
|
|
||||||
if(strcmp(getType(tn),getName(integ))==0){
|
|
||||||
return TYPE_PRIMITIVE;
|
|
||||||
}
|
|
||||||
if(strcmp(getType(tn),getName(addr))==0){
|
|
||||||
return TYPE_PRIMITIVE;
|
|
||||||
}
|
|
||||||
if(strcmp(getType(tn),getName(chara))==0){
|
|
||||||
return TYPE_PRIMITIVE;
|
|
||||||
}
|
|
||||||
if(strcmp(getType(tn),getName(stri))==0){
|
|
||||||
return TYPE_ARRAY;
|
|
||||||
}
|
|
||||||
if(strcmp(getType(tn),getName(boo))==0){
|
|
||||||
return TYPE_PRIMITIVE;
|
|
||||||
}
|
|
||||||
if(strcmp(getType(tn),getName(recprime))==0){
|
|
||||||
return TYPE_RECORD;
|
|
||||||
}
|
|
||||||
if(strcmp(getType(tn),getName(funtypeprime))==0){
|
|
||||||
return TYPE_FUNCTION_TYPE;
|
|
||||||
}
|
|
||||||
if(strcmp(getType(tn),getName(arrayprim))==0){
|
|
||||||
return TYPE_ARRAY;
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
return TYPE_FUNCTION_DECLARATION;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
char *getName(TableNode *tn) { return tn->theName; }
|
char *getName(TableNode *tn) { return tn->theName; }
|
||||||
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; }
|
||||||
|
|
||||||
/*
|
/*
|
||||||
//we use false for type defs and true for functions for parameter of typeOf
|
//we use false for type defs and true for functions for parameter of typeOf
|
||||||
TableNode* Define(SymbolTable* table, bool typeOf, char* id) {
|
TableNode* Define(SymbolTable* table, bool typeOf, char* id) {
|
||||||
@ -432,8 +467,8 @@ if (typeOf == 1){
|
|||||||
newEntry->theType = funy;
|
newEntry->theType = funy;
|
||||||
}
|
}
|
||||||
if(table_lookup(table,id) != NULL){
|
if(table_lookup(table,id) != NULL){
|
||||||
printf("already defined at the top level, can't define duplicate names\n");
|
printf("already defined at the top level, can't define duplicate
|
||||||
return NULL;
|
names\n"); return NULL;
|
||||||
}
|
}
|
||||||
newEntry->theName = id;
|
newEntry->theName = id;
|
||||||
if (table->entries == NULL) {
|
if (table->entries == NULL) {
|
||||||
@ -470,19 +505,20 @@ 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) {
|
||||||
if (table->Parent_Scope == NULL) {
|
if (table->Parent_Scope == NULL) {
|
||||||
fprintf(file_ptr, "%-17s: %-6s : %-6s : %-21s: %-28s\n", "NAME", "SCOPE",
|
fprintf(file_ptr, "%-17s: %-6s : %-6s : %-21s: %-28s\n", "NAME",
|
||||||
"PARENT", "TYPE", "Extra annotation");
|
"SCOPE", "PARENT", "TYPE", "Extra annotation");
|
||||||
}
|
}
|
||||||
TableNode *entrie = table->entries;
|
TableNode *entrie = table->entries;
|
||||||
fprintf(file_ptr,
|
fprintf(file_ptr, "-----------------:--------:--------:----------------"
|
||||||
"-----------------:--------:--------:----------------------:---------"
|
"------:---------"
|
||||||
"--------------------\n");
|
"--------------------\n");
|
||||||
int parant_scope = 0;
|
int parant_scope = 0;
|
||||||
int current_scope = 0;
|
int current_scope = 0;
|
||||||
if (table->Parent_Scope != NULL) {
|
if (table->Parent_Scope != NULL) {
|
||||||
parant_scope = table->Parent_Scope->Line_Number * 1000 +
|
parant_scope = table->Parent_Scope->Line_Number * 1000 +
|
||||||
table->Parent_Scope->Column_Number;
|
table->Parent_Scope->Column_Number;
|
||||||
current_scope = table->Line_Number * 1000 + table->Column_Number;
|
current_scope =
|
||||||
|
table->Line_Number * 1000 + table->Column_Number;
|
||||||
} else {
|
} else {
|
||||||
current_scope = 1001;
|
current_scope = 1001;
|
||||||
}
|
}
|
||||||
@ -492,14 +528,19 @@ void print_symbol_table(SymbolTable* table, FILE* file_ptr) {
|
|||||||
}
|
}
|
||||||
for (; entrie != NULL; entrie = entrie->next) {
|
for (; entrie != NULL; entrie = entrie->next) {
|
||||||
if (parant_scope == 0) {
|
if (parant_scope == 0) {
|
||||||
/*have to update*/ if(strcmp(entrie->theType->theName,"function primitive")|| strcmp(entrie->theType->theName,"array")){
|
/*have to update*/ if (strcmp(entrie->theType->theName,
|
||||||
|
"function primitive") ||
|
||||||
|
strcmp(entrie->theType->theName,
|
||||||
|
"array")) {
|
||||||
}
|
}
|
||||||
fprintf(file_ptr, "%-17s: %06d : : %-21s: %-28s\n",
|
fprintf(file_ptr,
|
||||||
entrie->theName, current_scope, entrie->theType->theName,
|
"%-17s: %06d : : %-21s: %-28s\n",
|
||||||
"Extra annotation");
|
entrie->theName, current_scope,
|
||||||
|
entrie->theType->theName, "Extra annotation");
|
||||||
} else {
|
} else {
|
||||||
fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n", entrie->theName,
|
fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n",
|
||||||
current_scope, parant_scope, entrie->theType->theName, "Extra annotation");
|
entrie->theName, current_scope, parant_scope,
|
||||||
|
entrie->theType->theName, "Extra annotation");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (table->Children_Scope != NULL) {
|
if (table->Children_Scope != NULL) {
|
||||||
@ -509,8 +550,8 @@ void print_symbol_table(SymbolTable* table, FILE* file_ptr) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (table->Parent_Scope == NULL) {
|
if (table->Parent_Scope == NULL) {
|
||||||
fprintf(file_ptr,
|
fprintf(file_ptr, "-----------------:--------:--------:--------"
|
||||||
"-----------------:--------:--------:----------------------:-------"
|
"--------------:-------"
|
||||||
"----------------------\n");
|
"----------------------\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -553,11 +594,8 @@ now = now->next;
|
|||||||
return scope;
|
return scope;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool typeCheck(char *firstID, char *secondID) {
|
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) {
|
||||||
@ -571,20 +609,28 @@ bool typeCheck(char* firstID, char* secondID){
|
|||||||
if (table_lookup(getAncestor(cur), getType(look_up(cur, firstID))) ==
|
if (table_lookup(getAncestor(cur), getType(look_up(cur, firstID))) ==
|
||||||
table_lookup(getAncestor(cur), getType(look_up(cur, secondID)))) {
|
table_lookup(getAncestor(cur), getType(look_up(cur, secondID)))) {
|
||||||
if (strcmp(getType(look_up(cur, firstID)), "array") == 0) {
|
if (strcmp(getType(look_up(cur, firstID)), "array") == 0) {
|
||||||
if(look_up(cur,firstID)->additionalinfo->ArrayAdInfo->numofdimensions ==
|
if (look_up(cur, firstID)
|
||||||
look_up(cur,secondID)->additionalinfo->ArrayAdInfo->numofdimensions &&
|
->additionalinfo->ArrayAdInfo
|
||||||
look_up(cur,firstID)->additionalinfo->ArrayAdInfo->typeofarray ==
|
->numofdimensions ==
|
||||||
look_up(cur,secondID)->additionalinfo->ArrayAdInfo->typeofarray){
|
look_up(cur, secondID)
|
||||||
return true;}
|
->additionalinfo->ArrayAdInfo
|
||||||
else{
|
->numofdimensions &&
|
||||||
return false;}
|
look_up(cur, firstID)
|
||||||
|
->additionalinfo->ArrayAdInfo
|
||||||
|
->typeofarray ==
|
||||||
|
look_up(cur, secondID)
|
||||||
|
->additionalinfo->ArrayAdInfo
|
||||||
|
->typeofarray) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
SymbolTable *getParent(SymbolTable *st) { return st->Parent_Scope; }
|
SymbolTable *getParent(SymbolTable *st) { return st->Parent_Scope; }
|
||||||
|
|
||||||
ListOfTable *getChildren(SymbolTable *st) { return st->Children_Scope; }
|
ListOfTable *getChildren(SymbolTable *st) { return st->Children_Scope; }
|
||||||
|
@ -19,15 +19,16 @@ typedef struct{
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int numofdimensions;
|
int numofdimensions;
|
||||||
//the above value tells you how long the below array is. For example if num of dimensions is 5, I can store 1,3,2,5,9 to define > int* arr;
|
// the above value tells you how long the below array is. For example if
|
||||||
//shouldn't need to store any values (like sizes of dimenions or the location
|
// num of dimensions is 5, I can store 1,3,2,5,9 to define > int*
|
||||||
//int* sizesofdimensions;
|
// arr; shouldn't need to store any values (like sizes of dimenions or
|
||||||
//do have to store type of array
|
// the location int* sizesofdimensions; do have to store type of array
|
||||||
struct TableNode *typeofarray;
|
struct TableNode *typeofarray;
|
||||||
} array_info;
|
} array_info;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
//similar to above we define a record to hold the number of elements and an array of tablenodes (types) that it contains in the >
|
// similar to above we define a record to hold the number of elements
|
||||||
|
// and an array of tablenodes (types) that it contains in the >
|
||||||
int numofelements;
|
int numofelements;
|
||||||
struct TableNode *listoftypes;
|
struct TableNode *listoftypes;
|
||||||
} record_info;
|
} record_info;
|
||||||
|
Reference in New Issue
Block a user