save point for tweaking symbol table--note won't be currently working

This commit is contained in:
Partho Bhattacharya
2025-03-14 04:24:03 -04:00
parent 3db6191969
commit f0e03b2724
2 changed files with 120 additions and 3 deletions

View File

@ -8,6 +8,71 @@
#include <stdlib.h> #include <stdlib.h>
char * typey = "type"; char * typey = "type";
char * funy = "function"; char * funy = "function";
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
TYPE_STRING,
//Array can be multidimensional. Information should be stored here
TYPE_ARRAY,
//Record is user defined types
TYPE_RECORD,
//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)
TYPE_FUNCTION_TYPE,
//The Type being pointed to by the first 4 above that only stores the size
TYPE_PRIMITIVE
} types;
/* put in symbol_table.h
typedef struct{
int size;
}primitive_info;
typedef struct{
int length;
char* location;
}string_info;
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 a multidimensional array of that size
int* arr;
}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
int numofelements;
TableNode* listoftypes;
}record_info;
typedef struct{
int startlinenumber;
bool regularoras;
}function_declaration_info;
typedef struct{
TableNode* parameter;
TableNode* returntype;
}function_type_info;
typedef union {
PrimAdInfo* primitive_info;
ArrayAdInfo* array_info;
RecAdInfo* record_info;
StringAdInfo* string_info;
FunDecAdInfo* func_dec_info;
FunTypeAdInfo* func_type_info;
}AdInfo;
*/
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));
table->Line_Number = Line; table->Line_Number = Line;
@ -35,10 +100,22 @@ 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
SymbolTable* init(SymbolTable* start){
if(start->Parent_Scope == NULL){
printf("Cannot initialize a scope that is not the parent scope\n");
return NULL;
}
TableNode* table1 = (TableNode*)malloc(sizeof(SymbolTable));
table->Line_Number = Line;
table->Column_Number = Column;
table->Parent_Scope = ParentScope;
table->Children_Scope = NULL;
table->entries = NULL;
TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id) { TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id) {
if(table ==NULL /*|| table->Parent_Scope == NULL*/){ if(table ==NULL){
printf("Null reference to table given for create entry or given top level scope which is invalid\n"); printf("Null reference to table");
return NULL; return NULL;
} }
/*TableNode* topDef = (table_lookup(getAncestor(table),typeOf)); /*TableNode* topDef = (table_lookup(getAncestor(table),typeOf));

View File

@ -3,6 +3,44 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
typedef struct{
int size;
}primitive_info;
typedef struct{
int length;
char* location;
}string_info;
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;
}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 > int numofelements;
TableNode* listoftypes;
}record_info;
typedef struct{
int startlinenumber;
bool regularoras;
}function_declaration_info;
typedef struct{
TableNode* parameter;
TableNode* returntype;
}function_type_info;
typedef union {
PrimAdInfo* primitive_info;
ArrayAdInfo* array_info;
RecAdInfo* record_info;
StringAdInfo* string_info;
FunDecAdInfo* func_dec_info;
FunTypeAdInfo* func_type_info;
}AdInfo;
typedef struct ListOfTable { typedef struct ListOfTable {
struct SymbolTable* table; struct SymbolTable* table;
// struct ListOfTable* prev; // struct ListOfTable* prev;
@ -10,8 +48,10 @@ typedef struct ListOfTable {
} ListOfTable; } ListOfTable;
typedef struct TableNode { typedef struct TableNode {
char* theType; //reference to the type entry that this is
TableNode* theType;
char* theName; char* theName;
AdInfo* additionalinfo;
struct TableNode* next; struct TableNode* next;
} TableNode; } TableNode;