Merge pull request #31 from UB-CSE443/Sprint2-Partho_Symbol_Tabke-FE-t#NoTask
updated get entry function to get string pointers instead of creating…
This commit is contained in:
@ -75,7 +75,7 @@ fprintf(tok_flag, "%d %d %3d \"%s\"\n", line_number, column_number,tok, yytext);
|
||||
}
|
||||
int run(FILE *alpha) {
|
||||
int token;
|
||||
cur = CreateScope(NULL, 1, 1);
|
||||
top = cur = CreateScope(NULL, 1, 1);
|
||||
|
||||
// If file is not found
|
||||
if (alpha == NULL) {
|
||||
|
@ -6,7 +6,8 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
char * typey = "type";
|
||||
char * funy = "function";
|
||||
SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column) {
|
||||
SymbolTable* table = (SymbolTable*)malloc(sizeof(SymbolTable));
|
||||
table->Line_Number = Line;
|
||||
@ -33,9 +34,20 @@ SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column) {
|
||||
return table;
|
||||
}
|
||||
|
||||
//create entry just for things below top level scope
|
||||
TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id) {
|
||||
|
||||
if(table ==NULL || table->Parent_Scope == NULL){
|
||||
printf("Null reference to table given for create entry or given top level scope which is invalid\n");
|
||||
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;
|
||||
}
|
||||
TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode));
|
||||
newEntry->theType = typeOf;
|
||||
newEntry->theType = topDef->theName;
|
||||
newEntry->theName = id;
|
||||
if (table->entries == NULL) {
|
||||
table->entries = newEntry;
|
||||
@ -47,6 +59,41 @@ TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id) {
|
||||
return newEntry;
|
||||
}
|
||||
}
|
||||
|
||||
//we use false for type defs and true for functions for parameter of typeOf
|
||||
TableNode* Define(SymbolTable* table, bool typeOf, char* id) {
|
||||
if(table ==NULL || table->Parent_Scope != NULL){
|
||||
printf("No valid table given for header defs\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode));
|
||||
|
||||
//possible issues with referencing text instead of heap
|
||||
if(typeOf == 0){
|
||||
newEntry->theType = typey;
|
||||
}
|
||||
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;
|
||||
}
|
||||
newEntry->theName = id;
|
||||
if (table->entries == NULL) {
|
||||
table->entries = newEntry;
|
||||
return newEntry;
|
||||
} else {
|
||||
TableNode* oldEntry = table->entries;
|
||||
table->entries = newEntry;
|
||||
newEntry->next = oldEntry;
|
||||
return newEntry;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TableNode* table_lookup(SymbolTable* table, char* x) {
|
||||
TableNode* entrie = table->entries;
|
||||
for (; entrie != NULL; entrie = entrie->next) {
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include "library.alpha"
|
||||
|
||||
entry(arg) := {
|
||||
[int : x]
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user