adding function to get adinfo type
This commit is contained in:
@ -123,7 +123,8 @@ definition:
|
||||
| TYPE ID COLON constant ARROW ID
|
||||
| function_declaration
|
||||
| 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
|
||||
;
|
||||
|
||||
@ -192,15 +193,12 @@ rec_op :
|
||||
DOT
|
||||
|
||||
expression:
|
||||
|
||||
constant {printf("constant expression\n");} {$$ = $<words>1;}
|
||||
| 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);
|
||||
$$=strdup("undefined");}else{$$=$2;}}
|
||||
|
||||
| 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);}}
|
||||
|
||||
| expression ADD expression {printf("add expression\n");}
|
||||
| expression SUB_OR_NEG expression {printf("subtract expression\n");}
|
||||
| expression MUL expression {printf("multiply expression\n");}
|
||||
@ -216,8 +214,8 @@ expression:
|
||||
$$=strdup("Boolean");$$=strdup("undefined");}}
|
||||
| expression EQUAL_TO expression {printf("equals check expression\n");
|
||||
if(strcmp($1,$3)==0){$$=strdup("Boolean");}else if((strcmp($1,"array")==0||strcmp($1,"record")==0||
|
||||
strcmp($1,"function type primitive")==0) && (strcmp($3,"address")==0)){$$=strdup("Boolean");}
|
||||
else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column);$$=strdup("undefined");}}
|
||||
strcmp($1,"function type primitive")==0) && (strcmp($3,"address")==0)){$$=strdup("Boolean");}
|
||||
else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column);$$=strdup("undefined");}}
|
||||
| assignable {printf("assignable expression\n");$$=$1;}
|
||||
| L_PAREN expression R_PAREN {printf("paren expression\n");$$=$2;}
|
||||
| memOp assignable {$$ = strdup("address");}
|
||||
@ -263,29 +261,3 @@ types:
|
||||
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);
|
||||
}
|
||||
/*
|
||||
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;
|
||||
} */
|
||||
|
60
src/runner.c
60
src/runner.c
@ -2,13 +2,13 @@
|
||||
/* The Translators - Spring 2025 */
|
||||
|
||||
#include "runner.h"
|
||||
extern TableNode* funprime;
|
||||
extern TableNode* arrayprim;
|
||||
extern TableNode* integ;
|
||||
extern TableNode* addr;
|
||||
extern TableNode* chara;
|
||||
extern TableNode* stri;
|
||||
extern TableNode* boo;
|
||||
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) {
|
||||
@ -29,13 +29,16 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
|
||||
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);
|
||||
return -1;
|
||||
} else {
|
||||
for (int i = 1; i < argc - 1; i++) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
@ -64,8 +67,8 @@ int check_flag(char *arg, char *alpha) {
|
||||
}
|
||||
}
|
||||
|
||||
void incr(int lnum,int cnum, int tok){
|
||||
//if (tok == COMMENT) {
|
||||
void incr(int lnum, int cnum, int tok) {
|
||||
// if (tok == COMMENT) {
|
||||
for (int i = 0; i < yyleng; i++) {
|
||||
if (yytext[i] == '\n') {
|
||||
line_number++;
|
||||
@ -73,12 +76,13 @@ void incr(int lnum,int cnum, int tok){
|
||||
}
|
||||
column_number++;
|
||||
}
|
||||
// }else{
|
||||
// column_number += yyleng;
|
||||
// }
|
||||
// }else{
|
||||
// column_number += yyleng;
|
||||
// }
|
||||
}
|
||||
void print_tok(int tok){
|
||||
fprintf(tok_flag, "%d %d %3d \"%s\"\n", line_number, column_number,tok, yytext);
|
||||
void print_tok(int tok) {
|
||||
fprintf(tok_flag, "%d %d %3d \"%s\"\n", line_number, column_number, tok,
|
||||
yytext);
|
||||
}
|
||||
int run(FILE *alpha) {
|
||||
int token;
|
||||
@ -94,10 +98,11 @@ int run(FILE *alpha) {
|
||||
// TOK FLAG
|
||||
if (tok_flag != NULL) {
|
||||
while (0 != (token = yylex())) {
|
||||
//if (tok_flag != NULL) {
|
||||
// fprintf(tok_flag, "%d %d %3d \"%s\"\n", line_number, column_number,
|
||||
// if (tok_flag != NULL) {
|
||||
// fprintf(tok_flag, "%d %d %3d \"%s\"\n",
|
||||
// line_number, column_number,
|
||||
// token, yytext);
|
||||
//}
|
||||
// }
|
||||
/*if (token == COMMENT) {
|
||||
for (int i = 0; i < yyleng; i++) {
|
||||
if (yytext[i] == '\n') {
|
||||
@ -110,9 +115,10 @@ int run(FILE *alpha) {
|
||||
}
|
||||
if (token == 1999) {
|
||||
printf(
|
||||
"On line number %d and column number %d we have an invalid "
|
||||
"character:%s\n",
|
||||
line_number, column_number, yytext);
|
||||
"On line number %d and column
|
||||
number %d we have an invalid " "character:%s\n",
|
||||
line_number, column_number,
|
||||
yytext);
|
||||
}
|
||||
column_number += yyleng; */
|
||||
}
|
||||
@ -172,7 +178,8 @@ int new_file(char *arg, char *alpha) {
|
||||
} else if (strcmp(arg, "-st") == 0) {
|
||||
type_len = ST_LEN;
|
||||
} 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;
|
||||
}
|
||||
|
||||
@ -195,15 +202,14 @@ int new_file(char *arg, char *alpha) {
|
||||
}
|
||||
|
||||
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 0; // is alpha file
|
||||
}
|
||||
|
||||
void enter_scope(int line, int column) {
|
||||
cur = CreateScope(cur, line, column);
|
||||
}
|
||||
void enter_scope(int line, int column) { cur = CreateScope(cur, line, column); }
|
||||
void exit_scope() {
|
||||
if (cur->Parent_Scope == NULL) {
|
||||
printf("Can't close top");
|
||||
|
14
src/runner.h
14
src/runner.h
@ -5,10 +5,14 @@
|
||||
#define TOK_LEN 3
|
||||
#define ST_LEN 2
|
||||
#define HELP \
|
||||
"HELP:\nHow to run the alpha compiler:\n./alpha [options] program\nValid " \
|
||||
"options:\n-tok output the token number, token, line number, and 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 " \
|
||||
"HELP:\nHow to run the alpha compiler:\n./alpha [options] " \
|
||||
"program\nValid " \
|
||||
"options:\n-tok output the token number, token, line number, and " \
|
||||
"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"
|
||||
#define SET_FLAG 1 // Used to set flags for arg types
|
||||
#define INVALID \
|
||||
@ -22,7 +26,7 @@
|
||||
|
||||
#include "../tmp/flex.h"
|
||||
#include "symbol_table.h"
|
||||
//#include "typedefs.h"
|
||||
// #include "typedefs.h"
|
||||
#include "../tmp/grammar.tab.h"
|
||||
|
||||
extern int line_number, column_number;
|
||||
|
@ -6,42 +6,43 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
char * typey = "type";
|
||||
char * funy = "function";
|
||||
TableNode* funprime;
|
||||
TableNode* arrayprim;
|
||||
extern SymbolTable* cur;
|
||||
TableNode* integ;
|
||||
TableNode* addr;
|
||||
TableNode* chara;
|
||||
TableNode* stri;
|
||||
TableNode* boo;
|
||||
TableNode* recprime;
|
||||
TableNode* funtypeprime;
|
||||
|
||||
|
||||
char *typey = "type";
|
||||
char *funy = "function";
|
||||
TableNode *funprime;
|
||||
TableNode *arrayprim;
|
||||
extern SymbolTable *cur;
|
||||
TableNode *integ;
|
||||
TableNode *addr;
|
||||
TableNode *chara;
|
||||
TableNode *stri;
|
||||
TableNode *boo;
|
||||
TableNode *recprime;
|
||||
TableNode *funtypeprime;
|
||||
|
||||
typedef enum {
|
||||
//First 4 below are primitive types that are all encapsulated in primitive type
|
||||
//TYPE_INTEGER,
|
||||
//TYPE_CHARACTER,
|
||||
//TYPE_BOOLEAN,
|
||||
//TYPE_ADDRESS,
|
||||
//Type String is an array of char enclosed in double quotes per lexer
|
||||
// First 4 below are primitive types that are all encapsulated in
|
||||
// primitive type
|
||||
// TYPE_INTEGER,
|
||||
// TYPE_CHARACTER,
|
||||
// TYPE_BOOLEAN,
|
||||
// TYPE_ADDRESS,
|
||||
// Type String is an array of char enclosed in double quotes per lexer
|
||||
TYPE_STRING,
|
||||
//Array can be multidimensional. Information should be stored here
|
||||
// Array can be multidimensional. Information should be stored here
|
||||
TYPE_ARRAY,
|
||||
//Record is user defined types
|
||||
// Record is user defined types
|
||||
TYPE_RECORD,
|
||||
//Declaring what type a particular function is without as
|
||||
// Declaring what type a particular function is without as
|
||||
TYPE_FUNCTION_DECLARATION,
|
||||
//Declaring what type a particular function is with as
|
||||
//TYPE_AS_FUNCTION_DECLARATION,
|
||||
//Declaring what type a function is (what the parameters and output are)
|
||||
// Declaring what type a particular function is with as
|
||||
// TYPE_AS_FUNCTION_DECLARATION,
|
||||
// Declaring what type a function is (what the parameters and output
|
||||
// are)
|
||||
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,
|
||||
//All else (NULL case likely)
|
||||
//likely NULL
|
||||
TYPE_ALL_ELSE
|
||||
|
||||
} types;
|
||||
@ -60,13 +61,14 @@ typedef struct{
|
||||
|
||||
typedef struct{
|
||||
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* sizesofdimensions;
|
||||
//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* sizesofdimensions;
|
||||
TableNode* typeofarray;
|
||||
}array_info;
|
||||
|
||||
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;
|
||||
TableNode* listoftypes;
|
||||
}record_info;
|
||||
@ -90,24 +92,25 @@ typedef union {
|
||||
FunTypeAdInfo* func_type_info;
|
||||
}AdInfo;
|
||||
*/
|
||||
//primitive additional info only stores the size of that type
|
||||
AdInfo* CreatePrimitiveInfo(int size){
|
||||
// primitive additional info only stores the size of that type
|
||||
AdInfo *CreatePrimitiveInfo(int size) {
|
||||
|
||||
AdInfo* info = (AdInfo*)malloc(sizeof(AdInfo));
|
||||
info->PrimAdInfo = (primitive_info*)malloc(sizeof(primitive_info));
|
||||
info->PrimAdInfo->size=size;
|
||||
AdInfo *info = (AdInfo *)malloc(sizeof(AdInfo));
|
||||
info->PrimAdInfo = (primitive_info *)malloc(sizeof(primitive_info));
|
||||
info->PrimAdInfo->size = size;
|
||||
return info;
|
||||
}
|
||||
|
||||
//only gets the size of a primitive type
|
||||
int getPrimSize(TableNode* definition){
|
||||
if(strcmp(getType(definition),"primitive")!=0){
|
||||
// only gets the size of a primitive type
|
||||
int getPrimSize(TableNode *definition) {
|
||||
if (strcmp(getType(definition), "primitive") != 0) {
|
||||
printf("not checking the size of a primitive -- invalid op\n");
|
||||
return 0;}
|
||||
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* stringy = (string_info*)malloc(sizeof(string_info));
|
||||
stringy.length=length;
|
||||
@ -116,110 +119,131 @@ int getPrimSize(TableNode* definition){
|
||||
}
|
||||
*/
|
||||
|
||||
//Only information stored in array info is the number of dimensions and the 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* info = (AdInfo*)malloc(sizeof(AdInfo));
|
||||
info->ArrayAdInfo = (array_info*)malloc(sizeof(array_info));
|
||||
info->ArrayAdInfo->numofdimensions=dim;
|
||||
info->ArrayAdInfo->typeofarray=type;
|
||||
//avoiding storing any types like below
|
||||
//int* dimensionsizes = loc;
|
||||
// Only information stored in array info is the number of dimensions and the
|
||||
// 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 *info = (AdInfo *)malloc(sizeof(AdInfo));
|
||||
info->ArrayAdInfo = (array_info *)malloc(sizeof(array_info));
|
||||
info->ArrayAdInfo->numofdimensions = dim;
|
||||
info->ArrayAdInfo->typeofarray = type;
|
||||
// avoiding storing any types like below
|
||||
// int* dimensionsizes = loc;
|
||||
return info;
|
||||
}
|
||||
//This gets the number of dimensions from array info
|
||||
int getNumArrDim(TableNode* definition){
|
||||
if(strcmp(getType(definition),"array")!=0){
|
||||
// This gets the number of dimensions from array 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 0;
|
||||
}
|
||||
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
|
||||
TableNode* getArrType(TableNode* definition){
|
||||
if(strcmp(getType(definition),"array")!=0){
|
||||
// This gets the type stored in an array from arrtype. It returns a reference to
|
||||
// the entry of that type
|
||||
TableNode *getArrType(TableNode *definition) {
|
||||
if (strcmp(getType(definition), "array") != 0) {
|
||||
printf("not checking the type of an array -- invalid op\n");
|
||||
return NULL;}
|
||||
return NULL;
|
||||
}
|
||||
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.
|
||||
//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* info = (AdInfo*)malloc(sizeof(AdInfo));
|
||||
info->RecAdInfo = (record_info*)malloc(sizeof(record_info));
|
||||
info->RecAdInfo->numofelements=length;
|
||||
//replace below with reference to a scope, not an array
|
||||
// Record type currently stores the number of elements as well as the types, in
|
||||
// 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 *info = (AdInfo *)malloc(sizeof(AdInfo));
|
||||
info->RecAdInfo = (record_info *)malloc(sizeof(record_info));
|
||||
info->RecAdInfo->numofelements = length;
|
||||
// replace below with reference to a scope, not an array
|
||||
info->RecAdInfo->listoftypes = typesarray;
|
||||
return info;
|
||||
}
|
||||
//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.
|
||||
int getRecLength(TableNode* definition){
|
||||
if(strcmp(getType(definition),"record")!=0){
|
||||
// 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.
|
||||
int getRecLength(TableNode *definition) {
|
||||
if (strcmp(getType(definition), "record") != 0) {
|
||||
printf("not checking the length of an record -- invalid op\n");
|
||||
return 0;}
|
||||
return 0;
|
||||
}
|
||||
return definition->additionalinfo->RecAdInfo->numofelements;
|
||||
}
|
||||
//This gets the array. Needs to up be updated to get the scope instead
|
||||
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;}
|
||||
// This gets the array. Needs to up be updated to get the scope instead
|
||||
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
|
||||
//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
|
||||
//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));
|
||||
info->FunDecAdInfo = (function_declaration_info*)malloc(sizeof(function_declaration_info));
|
||||
info->FunDecAdInfo->startlinenumber=line;
|
||||
// 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
|
||||
// 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));
|
||||
info->FunDecAdInfo = (function_declaration_info *)malloc(
|
||||
sizeof(function_declaration_info));
|
||||
info->FunDecAdInfo->startlinenumber = line;
|
||||
info->FunDecAdInfo->regularoras = asorregular;
|
||||
return info;
|
||||
}
|
||||
//gets the line at which the function was first defined. (Can be used to print out in table if needed)
|
||||
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;}
|
||||
// gets the line at which the function was first defined. (Can be used to print
|
||||
// out in table if needed)
|
||||
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;
|
||||
}
|
||||
//checks if "as" keyword was used for function definition. Either 0 or 1 for not used or used.
|
||||
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;}
|
||||
// checks if "as" keyword was used for function definition. Either 0 or 1 for
|
||||
// not used or used.
|
||||
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;
|
||||
}
|
||||
//stores the type of a function (parameter type and return type)
|
||||
AdInfo* CreateFunctionTypeInfo(TableNode* parameter, TableNode* returntype){
|
||||
AdInfo* info = (AdInfo*)malloc(sizeof(AdInfo));
|
||||
info->FunTypeAdInfo = (function_type_info*)malloc(sizeof(function_type_info));
|
||||
info->FunTypeAdInfo->parameter=parameter;
|
||||
// stores the type of a function (parameter type and return type)
|
||||
AdInfo *CreateFunctionTypeInfo(TableNode *parameter, TableNode *returntype) {
|
||||
AdInfo *info = (AdInfo *)malloc(sizeof(AdInfo));
|
||||
info->FunTypeAdInfo =
|
||||
(function_type_info *)malloc(sizeof(function_type_info));
|
||||
info->FunTypeAdInfo->parameter = parameter;
|
||||
info->FunTypeAdInfo->returntype = returntype;
|
||||
return info;
|
||||
}
|
||||
//returns parameter type of a function
|
||||
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;}
|
||||
// returns parameter type of a function
|
||||
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;
|
||||
}
|
||||
//returns return type of a function
|
||||
TableNode* getReturn(TableNode* definition){
|
||||
if(strcmp(getType(definition),"function type primitive")!=0){
|
||||
// returns return type of a function
|
||||
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 NULL;
|
||||
}
|
||||
return definition->additionalinfo->FunTypeAdInfo->returntype;
|
||||
}
|
||||
|
||||
|
||||
//creates a new scope (not the top scope though)
|
||||
SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column) {
|
||||
SymbolTable* table = (SymbolTable*)malloc(sizeof(SymbolTable));
|
||||
// creates a new scope (not the top scope though)
|
||||
SymbolTable *CreateScope(SymbolTable *ParentScope, int Line, int Column) {
|
||||
SymbolTable *table = (SymbolTable *)malloc(sizeof(SymbolTable));
|
||||
table->Line_Number = Line;
|
||||
table->Column_Number = Column;
|
||||
table->Parent_Scope = ParentScope;
|
||||
@ -227,16 +251,18 @@ SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column) {
|
||||
table->entries = NULL;
|
||||
if (ParentScope != NULL) {
|
||||
if (ParentScope->Children_Scope == NULL) {
|
||||
ListOfTable* newEntry = (ListOfTable*)malloc(sizeof(ListOfTable));
|
||||
ListOfTable *newEntry =
|
||||
(ListOfTable *)malloc(sizeof(ListOfTable));
|
||||
newEntry->next = NULL;
|
||||
// newEntry->prev = NULL;
|
||||
newEntry->table = table;
|
||||
ParentScope->Children_Scope = newEntry;
|
||||
} else {
|
||||
ListOfTable* newEntry = (ListOfTable*)malloc(sizeof(ListOfTable));
|
||||
ListOfTable *newEntry =
|
||||
(ListOfTable *)malloc(sizeof(ListOfTable));
|
||||
// newEntry->prev = NULL;
|
||||
newEntry->table = table;
|
||||
ListOfTable* oldEntry = ParentScope->Children_Scope;
|
||||
ListOfTable *oldEntry = ParentScope->Children_Scope;
|
||||
ParentScope->Children_Scope = newEntry;
|
||||
newEntry->next = oldEntry;
|
||||
}
|
||||
@ -244,92 +270,98 @@ SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column) {
|
||||
return table;
|
||||
}
|
||||
|
||||
//create entry just for things below top level scope
|
||||
//This function defines the integer, address, character, and bool primitive types
|
||||
SymbolTable* init(SymbolTable* start){
|
||||
if(start->Parent_Scope != NULL){
|
||||
printf("Cannot initialize a scope that is not the parent scope\n");
|
||||
// create entry just for things below top level scope
|
||||
// This function defines the integer, address, character, and bool primitive
|
||||
// types
|
||||
SymbolTable *init(SymbolTable *start) {
|
||||
if (start->Parent_Scope != NULL) {
|
||||
printf(
|
||||
"Cannot initialize a scope that is not the parent scope\n");
|
||||
return NULL;
|
||||
}
|
||||
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));
|
||||
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;
|
||||
addr->next = chara;
|
||||
chara->next = stri;
|
||||
stri->next = boo;
|
||||
//boo->next = arr;
|
||||
// boo->next = arr;
|
||||
boo->next = NULL;
|
||||
|
||||
integ->theName= "integer";
|
||||
addr->theName= "address";
|
||||
chara->theName= "character";
|
||||
boo->theName= "Boolean";
|
||||
stri->theName= "string";
|
||||
//arr->theName= "array"
|
||||
integ->theName = "integer";
|
||||
addr->theName = "address";
|
||||
chara->theName = "character";
|
||||
boo->theName = "Boolean";
|
||||
stri->theName = "string";
|
||||
// arr->theName= "array"
|
||||
|
||||
//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
|
||||
//and the type must point to an actual table entry
|
||||
//Again, this primitive table entry isn't in the top scope. It is outside the top scope and is only there
|
||||
//to facilitate the fact that these are primitive
|
||||
TableNode* prime = (TableNode*)malloc(sizeof(TableNode));
|
||||
prime->theName= "primitive";
|
||||
prime->theType=NULL;
|
||||
// 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 and the type must point to an actual table entry Again, this
|
||||
// primitive table entry isn't in the top scope. It is outside the top
|
||||
// scope and is only there to facilitate the fact that these are
|
||||
// primitive
|
||||
TableNode *prime = (TableNode *)malloc(sizeof(TableNode));
|
||||
prime->theName = "primitive";
|
||||
prime->theType = NULL;
|
||||
prime->additionalinfo = 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
|
||||
//when printing symbol table, if array is seen
|
||||
arrayprim = (TableNode*)malloc(sizeof(TableNode));
|
||||
arrayprim->theName= "array";
|
||||
arrayprim->theType=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 when printing symbol table, if array is seen
|
||||
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);
|
||||
// funprime = CreateEntry(NULL,NULL,strdup("function primitive"),NULL);
|
||||
|
||||
//similar workaround to arrays above
|
||||
funprime = (TableNode*)malloc(sizeof(TableNode));
|
||||
funprime->theName= "primitive function";
|
||||
funprime->theType=NULL;
|
||||
// similar workaround to arrays above
|
||||
funprime = (TableNode *)malloc(sizeof(TableNode));
|
||||
funprime->theName = "primitive function";
|
||||
funprime->theType = NULL;
|
||||
funprime->additionalinfo = NULL;
|
||||
funprime->next = NULL;
|
||||
|
||||
//record
|
||||
recprime = (TableNode*)malloc(sizeof(TableNode));
|
||||
recprime->theName= "record";
|
||||
recprime->theType=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 = (TableNode *)malloc(sizeof(TableNode));
|
||||
funtypeprime->theName = "primitive function type";
|
||||
funtypeprime->theType = NULL;
|
||||
funtypeprime->additionalinfo = NULL;
|
||||
funtypeprime->next = NULL;
|
||||
|
||||
integ->theType=prime;
|
||||
addr->theType=prime;
|
||||
chara->theType=prime;
|
||||
stri->theType=arrayprim;
|
||||
boo->theType=prime;
|
||||
//arr->theType=arrayprim;
|
||||
integ->theType = prime;
|
||||
addr->theType = prime;
|
||||
chara->theType = prime;
|
||||
stri->theType = arrayprim;
|
||||
boo->theType = prime;
|
||||
// arr->theType=arrayprim;
|
||||
|
||||
//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
|
||||
//of these primitive types. We can change these if needed to not be hard coded numbers
|
||||
//as a reminder, stri below is defined as a one dimensional array of characters
|
||||
// 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 of these primitive types. We can change these if needed
|
||||
// to not be hard coded numbers as a reminder, stri below is defined as
|
||||
// a one dimensional array of characters
|
||||
integ->additionalinfo = CreatePrimitiveInfo(4);
|
||||
addr->additionalinfo = CreatePrimitiveInfo(8);
|
||||
chara->additionalinfo = CreatePrimitiveInfo(1);
|
||||
stri->additionalinfo = CreateArrayInfo(1,chara);
|
||||
stri->additionalinfo = CreateArrayInfo(1, chara);
|
||||
boo->additionalinfo = CreatePrimitiveInfo(1);
|
||||
//addr->additionalinfo = CreatePrimitiveInfo(8);
|
||||
// addr->additionalinfo = CreatePrimitiveInfo(8);
|
||||
|
||||
start->Line_Number = 1;
|
||||
start->Column_Number = 1;
|
||||
@ -338,38 +370,6 @@ SymbolTable* init(SymbolTable* start){
|
||||
|
||||
return start;
|
||||
}
|
||||
|
||||
TableNode* CreateEntry(SymbolTable* table, TableNode* typeOf, char* id, AdInfo* ad) {
|
||||
|
||||
if(table ==NULL){
|
||||
printf("Null reference to table");
|
||||
return NULL;
|
||||
}
|
||||
/*
|
||||
TableNode* topDef = (table_lookup(getAncestor(table),typeOf));
|
||||
if(topDef == NULL){
|
||||
printf("This type is not defined at the top level\n");
|
||||
return NULL;
|
||||
}
|
||||
*/
|
||||
if(typeOf == NULL){
|
||||
printf("This is not pointing to a proper definition\n");
|
||||
return NULL;
|
||||
}
|
||||
TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode));
|
||||
newEntry->theType = typeOf/*topDef*/;
|
||||
newEntry->theName = id;
|
||||
newEntry->additionalinfo = ad;
|
||||
if (table->entries == NULL) {
|
||||
table->entries = newEntry;
|
||||
return newEntry;
|
||||
} else {
|
||||
TableNode* oldEntry = table->entries;
|
||||
table->entries = newEntry;
|
||||
newEntry->next = oldEntry;
|
||||
return newEntry;
|
||||
}
|
||||
}
|
||||
/*
|
||||
TableNode* integ;
|
||||
TableNode* addr;
|
||||
@ -380,8 +380,7 @@ TableNode* recprime;
|
||||
TableNode* funtypeprime;
|
||||
*/
|
||||
|
||||
char* getType(TableNode* tn) { return tn->theType->theName; }
|
||||
int getAdInfoType(TableNode* tn){
|
||||
int getAdInfoType(TableNode* tn){
|
||||
if(strcmp(getType(tn),getName(integ))==0){
|
||||
return TYPE_PRIMITIVE;
|
||||
}
|
||||
@ -410,9 +409,45 @@ char* getType(TableNode* tn) { return tn->theType->theName; }
|
||||
return TYPE_FUNCTION_DECLARATION;
|
||||
}
|
||||
}
|
||||
char* getName(TableNode* tn) { return tn->theName; }
|
||||
int getLine(SymbolTable* st) { return st->Line_Number; }
|
||||
int getColumn(SymbolTable* st) { return st->Column_Number; }
|
||||
|
||||
TableNode *CreateEntry(SymbolTable *table, TableNode *typeOf, char *id,
|
||||
AdInfo *ad) {
|
||||
|
||||
if (table == NULL) {
|
||||
printf("Null reference to table");
|
||||
return NULL;
|
||||
}
|
||||
/*
|
||||
TableNode* topDef = (table_lookup(getAncestor(table),typeOf));
|
||||
if(topDef == NULL){
|
||||
printf("This type is not defined at the top level\n");
|
||||
return NULL;
|
||||
}
|
||||
*/
|
||||
if (typeOf == NULL) {
|
||||
printf("This is not pointing to a proper definition\n");
|
||||
return NULL;
|
||||
}
|
||||
TableNode *newEntry = (TableNode *)malloc(sizeof(TableNode));
|
||||
newEntry->theType = typeOf /*topDef*/;
|
||||
newEntry->theName = id;
|
||||
newEntry->additionalinfo = ad;
|
||||
if (table->entries == NULL) {
|
||||
table->entries = newEntry;
|
||||
return newEntry;
|
||||
} else {
|
||||
TableNode *oldEntry = table->entries;
|
||||
table->entries = newEntry;
|
||||
newEntry->next = oldEntry;
|
||||
return newEntry;
|
||||
}
|
||||
}
|
||||
|
||||
char *getType(TableNode *tn) { return tn->theType->theName; }
|
||||
char *getName(TableNode *tn) { return tn->theName; }
|
||||
int getLine(SymbolTable *st) { return st->Line_Number; }
|
||||
int getColumn(SymbolTable *st) { return st->Column_Number; }
|
||||
|
||||
/*
|
||||
//we use false for type defs and true for functions for parameter of typeOf
|
||||
TableNode* Define(SymbolTable* table, bool typeOf, char* id) {
|
||||
@ -432,8 +467,8 @@ if (typeOf == 1){
|
||||
newEntry->theType = funy;
|
||||
}
|
||||
if(table_lookup(table,id) != NULL){
|
||||
printf("already defined at the top level, can't define duplicate names\n");
|
||||
return NULL;
|
||||
printf("already defined at the top level, can't define duplicate
|
||||
names\n"); return NULL;
|
||||
}
|
||||
newEntry->theName = id;
|
||||
if (table->entries == NULL) {
|
||||
@ -448,8 +483,8 @@ if(table_lookup(table,id) != NULL){
|
||||
|
||||
}
|
||||
*/
|
||||
TableNode* table_lookup(SymbolTable* table, char* x) {
|
||||
TableNode* entrie = table->entries;
|
||||
TableNode *table_lookup(SymbolTable *table, char *x) {
|
||||
TableNode *entrie = table->entries;
|
||||
for (; entrie != NULL; entrie = entrie->next) {
|
||||
if (!strcmp(entrie->theName, x)) {
|
||||
return entrie;
|
||||
@ -457,65 +492,71 @@ TableNode* table_lookup(SymbolTable* table, char* x) {
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
TableNode* look_up(SymbolTable* table, char* x) {
|
||||
TableNode *look_up(SymbolTable *table, char *x) {
|
||||
if (table == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
TableNode* ret = table_lookup(table, x);
|
||||
TableNode *ret = table_lookup(table, x);
|
||||
if (ret != NULL) {
|
||||
return ret;
|
||||
}
|
||||
return look_up(table->Parent_Scope, x);
|
||||
}
|
||||
|
||||
void print_symbol_table(SymbolTable* table, FILE* file_ptr) {
|
||||
void print_symbol_table(SymbolTable *table, FILE *file_ptr) {
|
||||
if (table->Parent_Scope == NULL) {
|
||||
fprintf(file_ptr, "%-17s: %-6s : %-6s : %-21s: %-28s\n", "NAME", "SCOPE",
|
||||
"PARENT", "TYPE", "Extra annotation");
|
||||
fprintf(file_ptr, "%-17s: %-6s : %-6s : %-21s: %-28s\n", "NAME",
|
||||
"SCOPE", "PARENT", "TYPE", "Extra annotation");
|
||||
}
|
||||
TableNode* entrie = table->entries;
|
||||
fprintf(file_ptr,
|
||||
"-----------------:--------:--------:----------------------:---------"
|
||||
TableNode *entrie = table->entries;
|
||||
fprintf(file_ptr, "-----------------:--------:--------:----------------"
|
||||
"------:---------"
|
||||
"--------------------\n");
|
||||
int parant_scope = 0;
|
||||
int current_scope = 0;
|
||||
if (table->Parent_Scope != NULL) {
|
||||
parant_scope = table->Parent_Scope->Line_Number * 1000 +
|
||||
table->Parent_Scope->Column_Number;
|
||||
current_scope = table->Line_Number * 1000 + table->Column_Number;
|
||||
current_scope =
|
||||
table->Line_Number * 1000 + table->Column_Number;
|
||||
} else {
|
||||
current_scope = 1001;
|
||||
}
|
||||
if ( entrie == NULL ) {
|
||||
if (entrie == NULL) {
|
||||
fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n", "",
|
||||
current_scope, parant_scope, "", "Empty Scope");
|
||||
}
|
||||
for (; entrie != NULL; entrie = entrie->next) {
|
||||
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",
|
||||
entrie->theName, current_scope, entrie->theType->theName,
|
||||
"Extra annotation");
|
||||
fprintf(file_ptr,
|
||||
"%-17s: %06d : : %-21s: %-28s\n",
|
||||
entrie->theName, current_scope,
|
||||
entrie->theType->theName, "Extra annotation");
|
||||
} else {
|
||||
fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n", entrie->theName,
|
||||
current_scope, parant_scope, entrie->theType->theName, "Extra annotation");
|
||||
fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n",
|
||||
entrie->theName, current_scope, parant_scope,
|
||||
entrie->theType->theName, "Extra annotation");
|
||||
}
|
||||
}
|
||||
if (table->Children_Scope != NULL) {
|
||||
ListOfTable* node = table->Children_Scope;
|
||||
ListOfTable *node = table->Children_Scope;
|
||||
for (; node != NULL; node = node->next) {
|
||||
print_symbol_table(node->table, file_ptr);
|
||||
}
|
||||
}
|
||||
if (table->Parent_Scope == NULL) {
|
||||
fprintf(file_ptr,
|
||||
"-----------------:--------:--------:----------------------:-------"
|
||||
fprintf(file_ptr, "-----------------:--------:--------:--------"
|
||||
"--------------:-------"
|
||||
"----------------------\n");
|
||||
}
|
||||
}
|
||||
|
||||
SymbolTable* getAncestor(SymbolTable* table) {
|
||||
SymbolTable *getAncestor(SymbolTable *table) {
|
||||
if (table->Parent_Scope == NULL) {
|
||||
// if table has no parent, return itself
|
||||
return table;
|
||||
@ -525,73 +566,78 @@ SymbolTable* getAncestor(SymbolTable* table) {
|
||||
}
|
||||
}
|
||||
|
||||
SymbolTable* removeEntry(SymbolTable* scope, char* search){
|
||||
SymbolTable *removeEntry(SymbolTable *scope, char *search) {
|
||||
|
||||
if(scope == NULL){
|
||||
if (scope == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
if(scope->entries == NULL){
|
||||
}
|
||||
if (scope->entries == NULL) {
|
||||
return scope;
|
||||
}
|
||||
}
|
||||
|
||||
TableNode* prev = NULL;
|
||||
TableNode* now = scope->entries;
|
||||
TableNode *prev = NULL;
|
||||
TableNode *now = scope->entries;
|
||||
|
||||
while(now != NULL){
|
||||
if(strcmp(getName(now),search)==0){
|
||||
if(prev == NULL){
|
||||
while (now != NULL) {
|
||||
if (strcmp(getName(now), search) == 0) {
|
||||
if (prev == NULL) {
|
||||
scope->entries = getNextEntry(now);
|
||||
return scope;
|
||||
} else{
|
||||
} else {
|
||||
prev->next = now->next;
|
||||
return scope;
|
||||
}
|
||||
}
|
||||
prev = now;
|
||||
now = now->next;
|
||||
}
|
||||
return scope;
|
||||
}
|
||||
prev = now;
|
||||
now = now->next;
|
||||
}
|
||||
return scope;
|
||||
}
|
||||
|
||||
bool typeCheck(char *firstID, char *secondID) {
|
||||
|
||||
|
||||
bool typeCheck(char* firstID, char* secondID){
|
||||
|
||||
|
||||
TableNode* entry1 = look_up(cur,firstID);
|
||||
TableNode* entry2 = look_up(cur,secondID);
|
||||
if(entry1==NULL){
|
||||
TableNode *entry1 = look_up(cur, firstID);
|
||||
TableNode *entry2 = look_up(cur, secondID);
|
||||
if (entry1 == NULL) {
|
||||
printf("first type not defined\n");
|
||||
return false;
|
||||
}
|
||||
if(entry2==NULL){
|
||||
if (entry2 == NULL) {
|
||||
printf("second type not defined\n");
|
||||
return false;
|
||||
}
|
||||
if(table_lookup(getAncestor(cur),getType(look_up(cur,firstID))) ==
|
||||
table_lookup(getAncestor(cur),getType(look_up(cur,secondID)))){
|
||||
if(strcmp(getType(look_up(cur,firstID)),"array")==0){
|
||||
if(look_up(cur,firstID)->additionalinfo->ArrayAdInfo->numofdimensions ==
|
||||
look_up(cur,secondID)->additionalinfo->ArrayAdInfo->numofdimensions &&
|
||||
look_up(cur,firstID)->additionalinfo->ArrayAdInfo->typeofarray ==
|
||||
look_up(cur,secondID)->additionalinfo->ArrayAdInfo->typeofarray){
|
||||
return true;}
|
||||
else{
|
||||
return false;}
|
||||
if (table_lookup(getAncestor(cur), getType(look_up(cur, firstID))) ==
|
||||
table_lookup(getAncestor(cur), getType(look_up(cur, secondID)))) {
|
||||
if (strcmp(getType(look_up(cur, firstID)), "array") == 0) {
|
||||
if (look_up(cur, firstID)
|
||||
->additionalinfo->ArrayAdInfo
|
||||
->numofdimensions ==
|
||||
look_up(cur, secondID)
|
||||
->additionalinfo->ArrayAdInfo
|
||||
->numofdimensions &&
|
||||
look_up(cur, firstID)
|
||||
->additionalinfo->ArrayAdInfo
|
||||
->typeofarray ==
|
||||
look_up(cur, secondID)
|
||||
->additionalinfo->ArrayAdInfo
|
||||
->typeofarray) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
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; }
|
||||
SymbolTable* getFirstChild(ListOfTable* lt) { return lt->table; }
|
||||
ListOfTable* getRestOfChildren(ListOfTable* lt) { return lt->next; }
|
||||
TableNode* getFirstEntry(SymbolTable* st) { return st->entries; }
|
||||
TableNode* getNextEntry(TableNode* tn) { return tn->next; }
|
||||
ListOfTable *getChildren(SymbolTable *st) { return st->Children_Scope; }
|
||||
SymbolTable *getFirstChild(ListOfTable *lt) { return lt->table; }
|
||||
ListOfTable *getRestOfChildren(ListOfTable *lt) { return lt->next; }
|
||||
TableNode *getFirstEntry(SymbolTable *st) { return st->entries; }
|
||||
TableNode *getNextEntry(TableNode *tn) { return tn->next; }
|
||||
// uncomment the below main function along with the headers above for a simple
|
||||
// standalone test of table and entry creation
|
||||
|
||||
|
@ -5,9 +5,9 @@
|
||||
|
||||
struct TableNode;
|
||||
|
||||
typedef struct{
|
||||
typedef struct {
|
||||
int size;
|
||||
}primitive_info;
|
||||
} primitive_info;
|
||||
|
||||
/*This structure can be subsumed into the structure below (1-d array of chars)
|
||||
typedef struct{
|
||||
@ -17,86 +17,87 @@ typedef struct{
|
||||
}string_info;
|
||||
*/
|
||||
|
||||
typedef struct{
|
||||
typedef struct {
|
||||
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;
|
||||
//shouldn't need to store any values (like sizes of dimenions or the location
|
||||
//int* sizesofdimensions;
|
||||
//do have to store type of array
|
||||
struct TableNode* typeofarray;
|
||||
}array_info;
|
||||
// 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; shouldn't need to store any values (like sizes of dimenions or
|
||||
// the location int* sizesofdimensions; do have to store type of array
|
||||
struct TableNode *typeofarray;
|
||||
} array_info;
|
||||
|
||||
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 >
|
||||
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 >
|
||||
int numofelements;
|
||||
struct TableNode* listoftypes;
|
||||
}record_info;
|
||||
struct TableNode *listoftypes;
|
||||
} record_info;
|
||||
|
||||
typedef struct{
|
||||
typedef struct {
|
||||
int startlinenumber;
|
||||
bool regularoras;
|
||||
}function_declaration_info;
|
||||
} function_declaration_info;
|
||||
|
||||
typedef struct{
|
||||
struct TableNode* parameter;
|
||||
struct TableNode* returntype;
|
||||
}function_type_info;
|
||||
typedef struct {
|
||||
struct TableNode *parameter;
|
||||
struct TableNode *returntype;
|
||||
} function_type_info;
|
||||
|
||||
typedef union {
|
||||
primitive_info* PrimAdInfo;
|
||||
array_info* ArrayAdInfo;
|
||||
record_info* RecAdInfo;
|
||||
//string_info* StringAdInfo;
|
||||
function_declaration_info* FunDecAdInfo;
|
||||
function_type_info* FunTypeAdInfo;
|
||||
}AdInfo;
|
||||
primitive_info *PrimAdInfo;
|
||||
array_info *ArrayAdInfo;
|
||||
record_info *RecAdInfo;
|
||||
// string_info* StringAdInfo;
|
||||
function_declaration_info *FunDecAdInfo;
|
||||
function_type_info *FunTypeAdInfo;
|
||||
} AdInfo;
|
||||
|
||||
typedef struct ListOfTable {
|
||||
struct SymbolTable* table;
|
||||
struct SymbolTable *table;
|
||||
// struct ListOfTable* prev;
|
||||
struct ListOfTable* next;
|
||||
struct ListOfTable *next;
|
||||
} ListOfTable;
|
||||
|
||||
typedef struct TableNode {
|
||||
//reference to the type entry that this is
|
||||
struct TableNode* theType;
|
||||
char* theName;
|
||||
AdInfo* additionalinfo;
|
||||
struct TableNode* next;
|
||||
}TableNode;
|
||||
// reference to the type entry that this is
|
||||
struct TableNode *theType;
|
||||
char *theName;
|
||||
AdInfo *additionalinfo;
|
||||
struct TableNode *next;
|
||||
} TableNode;
|
||||
|
||||
typedef struct SymbolTable {
|
||||
TableNode* entries;
|
||||
struct SymbolTable* Parent_Scope;
|
||||
struct ListOfTable* Children_Scope;
|
||||
TableNode *entries;
|
||||
struct SymbolTable *Parent_Scope;
|
||||
struct ListOfTable *Children_Scope;
|
||||
int Line_Number;
|
||||
int Column_Number;
|
||||
} SymbolTable;
|
||||
|
||||
SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column);
|
||||
TableNode* table_lookup(SymbolTable* table, char* x);
|
||||
TableNode* look_up(SymbolTable* table, char* x);
|
||||
void print_symbol_table(SymbolTable* table, FILE* file_ptr);
|
||||
SymbolTable *CreateScope(SymbolTable *ParentScope, int Line, int Column);
|
||||
TableNode *table_lookup(SymbolTable *table, char *x);
|
||||
TableNode *look_up(SymbolTable *table, char *x);
|
||||
void print_symbol_table(SymbolTable *table, FILE *file_ptr);
|
||||
|
||||
SymbolTable* getAncestor(SymbolTable* table);
|
||||
SymbolTable* getParent(SymbolTable* st);
|
||||
ListOfTable* getChildren(SymbolTable* st);
|
||||
SymbolTable* getFirstChild(ListOfTable* lt);
|
||||
ListOfTable* getRestOfChildren(ListOfTable* lt);
|
||||
TableNode* getFirstEntry(SymbolTable* st);
|
||||
TableNode* getNextEntry(TableNode* tn);
|
||||
SymbolTable* init(SymbolTable* scope);
|
||||
int getPrimSize(TableNode* definition);
|
||||
int getNumArrDim(TableNode* definition);
|
||||
TableNode* getArrType(TableNode* definition);
|
||||
int getRecLength(TableNode* definition);
|
||||
TableNode* getRecList(TableNode* definition);
|
||||
int getStartLine(TableNode* definition);
|
||||
bool getAsKeyword(TableNode* definition);
|
||||
TableNode* getParameter(TableNode* definition);
|
||||
TableNode* getReturn(TableNode* definition);
|
||||
SymbolTable *getAncestor(SymbolTable *table);
|
||||
SymbolTable *getParent(SymbolTable *st);
|
||||
ListOfTable *getChildren(SymbolTable *st);
|
||||
SymbolTable *getFirstChild(ListOfTable *lt);
|
||||
ListOfTable *getRestOfChildren(ListOfTable *lt);
|
||||
TableNode *getFirstEntry(SymbolTable *st);
|
||||
TableNode *getNextEntry(TableNode *tn);
|
||||
SymbolTable *init(SymbolTable *scope);
|
||||
int getPrimSize(TableNode *definition);
|
||||
int getNumArrDim(TableNode *definition);
|
||||
TableNode *getArrType(TableNode *definition);
|
||||
int getRecLength(TableNode *definition);
|
||||
TableNode *getRecList(TableNode *definition);
|
||||
int getStartLine(TableNode *definition);
|
||||
bool getAsKeyword(TableNode *definition);
|
||||
TableNode *getParameter(TableNode *definition);
|
||||
TableNode *getReturn(TableNode *definition);
|
||||
|
||||
char* getType(TableNode* tn);
|
||||
char* getName(TableNode* tn);
|
||||
int getLine(SymbolTable* st);
|
||||
int getColumn(SymbolTable* st);
|
||||
char *getType(TableNode *tn);
|
||||
char *getName(TableNode *tn);
|
||||
int getLine(SymbolTable *st);
|
||||
int getColumn(SymbolTable *st);
|
||||
|
Reference in New Issue
Block a user