Files
compiler-the-translators/print_symbol_table.c
2025-02-21 14:30:14 -05:00

99 lines
3.8 KiB
C

#include <stdlib.h>
#include "symbol_table.h"
#include "symbol_table.c"
#include <string.h>
void print_symbol_table(SymbolTable *table, FILE *file_ptr){
if(table->Parent_Scope == NULL){
fprintf(file_ptr, "%-17s: %-6s : %-6s : %-21s: %-28s\n", "NAME", "SCOPE", "PARENT", "TYPE", "Extra annotation");
}
TableNode * entrie = table->entries;
fprintf(file_ptr, "-----------------:--------:--------:----------------------:-----------------------------\n");
int parant_scope = 0;
int current_scope = 0;
if(table->Parent_Scope != NULL){
parant_scope = table->Parent_Scope->Line_Number*1000 + table->Parent_Scope->Column_Number;
current_scope = table->Line_Number*1000 + table->Column_Number;
} else {
current_scope = 1001;
}
for(; entrie != NULL; entrie = entrie->next){
if (parant_scope == 0){
fprintf(file_ptr, "%-17s: %06d : : %-21s: %-28s\n",
entrie->theName, current_scope,
entrie->theType, "Extra annotation");
} else {
fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n",
entrie->theName, current_scope, parant_scope,
entrie->theType, "Extra annotation");
}
}
if (table->Children_Scope != NULL){
ListOfTable* node = table->Children_Scope;
for(; node != NULL; node = node->next){
print_symbol_table(node->table, file_ptr);
}
}
if (table->Parent_Scope == NULL) {
fprintf(file_ptr, "-----------------:--------:--------:----------------------:-----------------------------\n");
}
}
/*
int main(void){
char *prim = strdup("primitive");
char *inte = strdup("integer");
SymbolTable * parant = CreateScope(NULL, 1,1);
char *boole = strdup("Boolean");
char *chare = strdup("character");
char *str = strdup("string");
char *arg = strdup("arg");
// Value* v = calloc(1, sizeof(Value));
CreateEntry(parant, prim, boole);
CreateEntry(parant, prim, chare);
CreateEntry(parant, prim, inte);
CreateEntry(parant, &"1 -> character", str);
CreateEntry(parant, &"integer -> integer", &"int2int");
CreateEntry(parant, &"string -> integer", &"string2int");
CreateEntry(parant, &"int2int", &"square");
CreateEntry(parant, &"string2int", &"entry");
SymbolTable * child = CreateScope(parant, 14,14);
CreateEntry(child, inte, &"x");
SymbolTable * second = CreateScope(parant, 21,15);
CreateEntry(second, str, arg);
CreateEntry(second, inte, &"input");
CreateEntry(second, inte, &"expected");
CreateEntry(second, inte, &"actual");
CreateEntry(second, &"$_undefined_type", &"result");
SymbolTable * third = CreateScope(second, 33,44);
CreateEntry(third, &"BOO", arg);
CreateEntry(third, &"YAZOO", &"input");
TableNode *ret = table_lookup(third, "arg");
printf("%s == %s\n", ret->theName, "arg");
ret = table_lookup(third, "hello");
printf("This should be nil %p != %s\n", ret, "BOO");
ret = look_up(second, "input");
printf("%s == %s\n", ret->theName, "input");
ret = look_up(second, "square");
printf("%s == %s\n", ret->theName, "square");
ret = look_up(second, "spuare");
printf("This should be nil %p == %s\n", ret, "square");
print_symbol_table(parant, stderr);
free(inte);
free(boole);
free(prim);
free(str);
free(chare);
free(arg);
return 0;
}
*/