/* Symbol Table */ /* The Translators - Spring 2025 */ #pragma once #include #include #include #include #include #include #define SIZE_INT 4 #define SIZE_ADDR 8 #define SIZE_CHAR 1 #define SIZE_BOOL 1 struct TableNode; typedef struct TFList TFList; typedef struct CGNode CGNode; typedef struct Constant_Stack { struct TableNode *theType; void *theValue; struct Constant_Stack *next; bool isConst; } Constant_Stack; typedef struct { int size; } primitive_info; typedef struct { int numofdimensions; struct TableNode *typeofarray; } array_info; typedef struct { int numofelements; struct SymbolTable *recordScope; int total_size; int *offsets; } record_info; typedef struct { int startlinenumber; bool regularoras; } function_declaration_info; typedef struct { struct TableNode *parameter; struct TableNode *returntype; } function_type_info; typedef union { primitive_info *PrimAdInfo; array_info *ArrayAdInfo; record_info *RecAdInfo; function_declaration_info *FunDecAdInfo; function_type_info *FunTypeAdInfo; } AdInfo; typedef struct ListOfTable { struct SymbolTable *table; struct ListOfTable *next; } ListOfTable; typedef struct TableNode { struct TableNode *theType; int tag; char *theName; AdInfo *additionalinfo; struct TableNode *next; } TableNode; typedef struct SymbolTable { TableNode *entries; struct SymbolTable *Parent_Scope; struct ListOfTable *Children_Scope; int Line_Number; int Column_Number; } SymbolTable; typedef enum { TYPE_STRING = 1, TYPE_ARRAY_TYPE = 2, TYPE_RECORD_TYPE = 3, TYPE_FUNCTION_DECLARATION = 4, TYPE_FUNCTION_TYPE = 5, TYPE_PRIMITIVE = 6, TYPE_ALL_ELSE = 7, TYPE_UNDEFINED = 8, TYPE_RECORD = 9, TYPE_ARRAY = 10, TYPE_SYSTEM_DEFINED = 11, TYPE_PRIMITIVE_TYPE = 12 } types; void printdebug_impl(char *file, int line, const char *format, ...); #define printdebug(format, ...) \ printdebug_impl(__FILE__, __LINE__, format, ##__VA_ARGS__) char *temp_var_gen(); Constant_Stack *Push(TableNode *type, void *value, bool isConst); Constant_Stack *Pop(); Constant_Stack *Print_Stack(); AdInfo *CreatePrimitiveInfo(int size); int getPrimSize(TableNode *definition); AdInfo *CreateArrayInfo(int dim, TableNode *type); int getNumArrDim(TableNode *definition); TableNode *getArrType(TableNode *definition); AdInfo *CreateRecordInfo(int length, SymbolTable *recordScope); int getRecTotal(TableNode *node); TableNode *setRecOffsetInfo(SymbolTable *scope, TableNode *node); int *getRecOffsets(TableNode *node); int getRecLength(TableNode *definition); SymbolTable *getRecList(TableNode *definition); TableNode *setRecSize(TableNode *tn, int n); int getRecSize(SymbolTable *tn); AdInfo *CreateFunctionDeclarationInfo(int line, bool asorregular); int getStartLine(TableNode *definition); TableNode *setStartLine(TableNode *tn, int start); bool getAsKeyword(TableNode *definition); TableNode *setAsKeyword(TableNode *tn, bool as); AdInfo *CreateFunctionTypeInfo(TableNode *parameter, TableNode *returntype); TableNode *getParameter(TableNode *definition); TableNode *getReturn(TableNode *definition); SymbolTable *CreateScope(SymbolTable *ParentScope, int Line, int Column); SymbolTable *init(SymbolTable *start); TableNode *populateTypeAndInfo(TableNode *tn, TableNode *type, AdInfo *info); AdInfo *getAdInfo(TableNode *tn); int getAdInfoType(TableNode *tn); TableNode *CreateEntry(SymbolTable *table, int tag, TableNode *typeOf, char *id, AdInfo *ad); TableNode *getTypeEntry(TableNode *tn); char *getType(TableNode *tn); char *getName(TableNode *tn); int getLine(SymbolTable *st); int getColumn(SymbolTable *st); TableNode *addName(TableNode *tn, char *str); SymbolTable *setLineNumber(SymbolTable *st, int line); SymbolTable *setColumnNumber(SymbolTable *st, int column); TableNode *table_lookup(SymbolTable *table, char *x); TableNode *look_up(SymbolTable *table, char *x); void printline(FILE *file_ptr, bool b); void st_fprint(FILE *file_ptr, char *label1, int label2, int label3, char *label4, char *label5); void print_symbol_table(SymbolTable *table, FILE *file_ptr); SymbolTable *getAncestor(SymbolTable *table); SymbolTable *removeEntry(SymbolTable *scope, char *search); bool typeCheck(char *firstID, char *secondID); SymbolTable *getParent(SymbolTable *st); ListOfTable *getChildren(SymbolTable *st); SymbolTable *getFirstChild(ListOfTable *lt); ListOfTable *getRestOfChildren(ListOfTable *lt); TableNode *getFirstEntry(SymbolTable *st); TableNode *getNextEntry(TableNode *tn); TableNode *printTableNode(TableNode *tn); extern int yylex(void); extern char *yytext; extern int yyleng; extern int yychar; extern SymbolTable *cur; extern int line_number; extern int column_number; extern FILE *yyin; extern bool DEBUG; extern int temp2_count; extern TableNode *funprime; extern TableNode *arrayprim; extern TableNode *integ; extern TableNode *addr; extern TableNode *chara; extern TableNode *stri; extern TableNode *boo; extern TableNode *recprime; extern TableNode *funtypeprime; extern TableNode *undefined; extern Constant_Stack *head; extern char *COLOR_RED; extern char *COLOR_GREEN; extern char *COLOR_ORANGE; extern char *COLOR_BLUE; extern char *COLOR_PURPLE; extern char *COLOR_CYAN; extern char *COLOR_LIGHTGRAY; extern char *COLOR_DARKGRAY; extern char *COLOR_LIGHTRED; extern char *COLOR_LIGHTGREEN; extern char *COLOR_YELLOW; extern char *COLOR_LIGHTBLUE; extern char *COLOR_LIGHTPURPLE; extern char *COLOR_LIGHTCYAN; extern char *COLOR_WHITE;