added a simple test of symbol table and entry creation

This commit is contained in:
Partho Bhattacharya
2025-02-20 19:07:21 -05:00
parent b21d7039a2
commit f2a0fbd1bc
3 changed files with 118 additions and 64 deletions

View File

@ -2,84 +2,104 @@
//Using a Linked List Structure. Head of linked List points to parent scope (if one exists) //Using a Linked List Structure. Head of linked List points to parent scope (if one exists)
//Tail of Linked List points to a Linked List of all the child scopes //Tail of Linked List points to a Linked List of all the child scopes
//T //T
#include "symbol_table.h" //*#include "symbol_table.h"
/*
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct ListOfTable{ typedef struct ListOfTable{
struct SymbolTable* table; struct SymbolTable* table;
//struct ListOfTable* prev; //struct ListOfTable* prev;
struct ListOfTable* next; struct ListOfTable* next;
}ListOfTable; }ListOfTable;
typedef union Value{ typedef union Value{
int* value_of_int; int* value_of_int;
void* value_of_pointer; void* value_of_pointer;
bool* value_of_bool; bool* value_of_bool;
char* value_of_char; char* value_of_char;
}Value; }Value;
typedef struct TableNode{ typedef struct TableNode{
string* theType; char* theType;
string* theName; char* theName;
Value* value; Value* value;
struct TableNode* next; struct TableNode* next;
//this next value is an int for string types to tell you how far to traverse a buffer for the string //this next value is an int for string types to tell you how far to traverse a buffer for the string
int StrLength; int StrLength;
}TableNode; }TableNode;
typedef struct SymbolTable{ typedef struct SymbolTable{
TableNode* entries; TableNode* entries;
struct SymbolTable* Parent_Scope; struct SymbolTable* Parent_Scope;
struct ListOfTable* Children_Scope; struct ListOfTable* Children_Scope;
int Line_Number; int Line_Number;
int Column_Number; int Column_Number;
}SymbolTable; }SymbolTable;
*/
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;
table->Column_Number = Column; table->Column_Number = Column;
table->Parent_Scope = ParentScope; table->Parent_Scope = ParentScope;
table->Children_Scope = NULL; table->Children_Scope = NULL;
table->entries = NULL; table->entries = NULL;
if(ParentScope->Children_scope == NULL){ if(ParentScope != NULL){
ListOfTable* newEntry = (ListOfTable*)malloc(sizeof(ListOfTable)); if(ParentScope->Children_Scope == NULL){
newEntry->next = NULL; ListOfTable* newEntry = (ListOfTable*)malloc(sizeof(ListOfTable));
//newEntry->prev = NULL; newEntry->next = NULL;
newEntry->table = table; //newEntry->prev = NULL;
ParentScope->Children_scope = newEntry; newEntry->table = table;
} else{ ParentScope->Children_Scope = newEntry;
ListOfTable* newEntry = (ListOfTable*)malloc(sizeof(ListOfTable)); } else{
//newEntry->prev = NULL; ListOfTable* newEntry = (ListOfTable*)malloc(sizeof(ListOfTable));
newEntry->table= table; //newEntry->prev = NULL;
ListOfTable* oldEntry = ParentScope->Children_scope; newEntry->table= table;
ParentScope->Children_scope = newEntry; ListOfTable* oldEntry = ParentScope->Children_Scope;
newEntry->next = oldEntry; ParentScope->Children_Scope = newEntry;
newEntry->next = oldEntry;
} }
return table; }
} return table;
}
return table;
}
TableNode* CreateEntry(SymbolTable* table, string typeOf, string id, Value value, int StringLength){
if(table->entries == NULL){
TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode));
newEntry->theType = typeOf;
newEntry->theName = id;
newEntry->StrLength = StringLength;
return newEntry;
} else{
TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode));
newEntry->theType = typeOf;
newEntry->theName = id;
newEntry->StrLength = StringLength;
TableNode* oldEntry = table->entries;
table->entries = newEntry;
newEntry->next = oldEntry;
return newEntry;
}
}
TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id, Value* value, int StringLength){
if(table->entries == NULL){
TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode));
newEntry->theType = typeOf;
newEntry->theName = id;
newEntry->value = value;
newEntry->StrLength = StringLength;
return newEntry;
} else{
TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode));
newEntry->theType = typeOf;
newEntry->theName = id;
newEntry->value = value;
newEntry->StrLength = StringLength;
TableNode* oldEntry = table->entries;
table->entries = newEntry;
newEntry->next = oldEntry;
return newEntry;
}
}
//uncomment the below main function along with the headers above for a simple standalone test of table and entry creation
/*
int main(){
char* String = "STRING";
char* X = "X";
Value* ofX = (Value*)malloc(sizeof(Value));
ofX->value_of_char = X;
SymbolTable* Second = CreateScope(NULL, 2,2);
printf("Line number is %d, Column number of scope is %d\n",Second->Line_Number,Second->Column_Number);
TableNode* First_Entry = CreateEntry(Second,String,X,ofX,-1);
printf("The value of the first entry is %s\n",First_Entry->value->value_of_char);
return 0;
}
*/

View File

@ -0,0 +1,34 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct ListOfTable{
struct SymbolTable* table;
//struct ListOfTable* prev;
struct ListOfTable* next;
}ListOfTable;
typedef union Value{
int* value_of_int;
void* value_of_pointer;
bool* value_of_bool;
char* value_of_char;
}Value;
typedef struct TableNode{
char* theType;
char* theName;
Value* value;
struct TableNode* next;
//this next value is an int for string types to tell you how far to traverse a buffer for the string
int StrLength;
}TableNode;
typedef struct SymbolTable{
TableNode* entries;
struct SymbolTable* Parent_Scope;
struct ListOfTable* Children_Scope;
int Line_Number;
int Column_Number;
}SymbolTable;