59 lines
1.5 KiB
C
59 lines
1.5 KiB
C
//Defining a symbol table
|
|
//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
|
|
//T
|
|
#include "symbol_table.h"
|
|
|
|
typedef struct ListOfTable{
|
|
struct SymbolTable* table;
|
|
struct ListOfTable* prev;
|
|
struct ListOfTable* next;
|
|
int Line_Number;
|
|
int Column_Number;
|
|
}ListOfTable;
|
|
|
|
typedef union Value{
|
|
int* value_of_int;
|
|
void* value_of_pointer;
|
|
bool* value_of_bool;
|
|
char* value_of_char;
|
|
}Value;
|
|
|
|
typedef struct TableNode{
|
|
string* theType;
|
|
string* 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;
|
|
}SymbolTable;
|
|
|
|
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;
|
|
table.Children_Scope = NULL;
|
|
table.entries = NULL;
|
|
return table;
|
|
}
|
|
|
|
TableNode* CreateEntry(SymbolTable* table, string typeOf, string id, Value value, int StringLength){
|
|
if(table.entries == NULL){
|
|
TableNode* newEntry = (TableNode*)malloc(sizeof(SymbolTable));
|
|
newEntry.theType = typeOf;
|
|
newEntry.theName = id;
|
|
newEntry.StrLength = StringLength;
|
|
} else{
|
|
|
|
|
|
|
|
|
|
|