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 run(FILE *alpha) {
|
||||||
int token;
|
int token;
|
||||||
cur = CreateScope(NULL, 1, 1);
|
top = cur = CreateScope(NULL, 1, 1);
|
||||||
|
|
||||||
// If file is not found
|
// If file is not found
|
||||||
if (alpha == NULL) {
|
if (alpha == NULL) {
|
||||||
|
@ -6,7 +6,8 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
char * typey = "type";
|
||||||
|
char * funy = "function";
|
||||||
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;
|
||||||
@ -33,20 +34,66 @@ SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column) {
|
|||||||
return table;
|
return table;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//create entry just for things below top level scope
|
||||||
TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id) {
|
TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id) {
|
||||||
TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode));
|
|
||||||
newEntry->theType = typeOf;
|
if(table ==NULL || table->Parent_Scope == NULL){
|
||||||
newEntry->theName = id;
|
printf("Null reference to table given for create entry or given top level scope which is invalid\n");
|
||||||
if (table->entries == NULL) {
|
return NULL;
|
||||||
table->entries = newEntry;
|
|
||||||
return newEntry;
|
|
||||||
} else {
|
|
||||||
TableNode* oldEntry = table->entries;
|
|
||||||
table->entries = newEntry;
|
|
||||||
newEntry->next = oldEntry;
|
|
||||||
return newEntry;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
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 = topDef->theName;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//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* table_lookup(SymbolTable* table, char* x) {
|
||||||
TableNode* entrie = table->entries;
|
TableNode* entrie = table->entries;
|
||||||
for (; entrie != NULL; entrie = entrie->next) {
|
for (; entrie != NULL; entrie = entrie->next) {
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
#include "library.alpha"
|
|
||||||
entry(arg) := {
|
entry(arg) := {
|
||||||
|
[int : x]
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user