fix output format

This commit is contained in:
2025-12-02 14:35:38 +09:00
parent f1fd203233
commit 75407304f8

View File

@@ -178,6 +178,81 @@ BucketList st_lookup_from(char *name, Scope scope) {
return NULL; /* not found */
}
static void printEntry(FILE *listing, BucketList entry) {
if (entry == NULL) return;
fprintf(listing, "%-14s", entry->name);
switch (entry->symbolKind) {
case SymbolVar:
fprintf(listing, "|%-13s", "Var");
break;
case SymbolFunc:
fprintf(listing, "|%-13s", "Func");
break;
case SymbolParam:
fprintf(listing, "|%-13s", "Param");
break;
}
switch (entry->type) {
case Void:
fprintf(listing, "|%-11s", "void");
break;
case Integer:
fprintf(listing, "|%-11s", "int");
break;
case IntegerArray:
fprintf(listing, "|%-11s", "int[]");
break;
}
fprintf(listing, "|%-9d|", entry->memloc);
LineList t = entry->lines;
while (t != NULL) {
fprintf(listing, "%d ", t->lineno);
t = t->next;
}
fprintf(listing, "\n");
}
static void printScopeTable(FILE *listing, Scope scope) {
if (scope == NULL) return;
int count = 0;
for (int i = 0; i < SYMTAB_SIZE; ++i) {
BucketList l = scope->hashTable[i];
while (l != NULL) {
count++;
l = l->next;
}
}
if (count > 0) {
BucketList *entries = (BucketList *)malloc(sizeof(BucketList) * count);
int idx = 0;
for (int i = 0; i < SYMTAB_SIZE; ++i) {
BucketList l = scope->hashTable[i];
while (l != NULL) {
entries[idx++] = l;
l = l->next;
}
}
for (int i = 1; i < count; ++i) {
BucketList key = entries[i];
int j = i - 1;
while (j >= 0 && entries[j]->memloc > key->memloc) {
entries[j + 1] = entries[j];
--j;
}
entries[j + 1] = key;
}
for (int i = 0; i < count; ++i) {
printEntry(listing, entries[i]);
}
free(entries);
}
fprintf(listing, "\n");
}
void printScope(FILE *listing, Scope scope) {
if (scope == NULL) return;
fprintf(listing, "Scope Name: %s, Depth: %d\n", scope->name, scope->depth);
@@ -185,43 +260,7 @@ void printScope(FILE *listing, Scope scope) {
fprintf(listing, "Variable Name Symbol Kind Type Location Line Numbers\n");
fprintf(listing, "------------------------------------------------------------\n");
for (int i = 0; i < SYMTAB_SIZE; i++) {
BucketList l = scope->hashTable[i];
while (l != NULL) {
fprintf(listing, "%-14s", l->name);
switch (l->symbolKind) {
case SymbolVar:
fprintf(listing, "%-13s", "Variable");
break;
case SymbolFunc:
fprintf(listing, "%-13s", "Function");
break;
case SymbolParam:
fprintf(listing, "%-13s", "Parameter");
break;
}
switch (l->type) {
case Void:
fprintf(listing, "%-11s", "void");
break;
case Integer:
fprintf(listing, "%-11s", "int");
break;
case IntegerArray:
fprintf(listing, "%-11s", "int[]");
break;
}
fprintf(listing, "%-9d", l->memloc);
LineList t = l->lines;
while (t != NULL) {
fprintf(listing, "%d ", t->lineno);
t = t->next;
}
fprintf(listing, "\n");
l = l->next;
}
}
fprintf(listing, "\n");
printScopeTable(listing, scope);
}
static void printScopeRecursive(FILE *listing, Scope scope) {
@@ -244,6 +283,24 @@ void printScopeTree(FILE *listing) {
printScopeRecursive(listing, current_scope);
}
void printSymTab(FILE *listing) {
printScopeTree(listing);
static void printScopeTableRecursive(FILE *listing, Scope scope) {
if (scope == NULL) return;
printScopeTable(listing, scope);
Scope child = scope->child;
while (child != NULL) {
printScopeTableRecursive(listing, child);
child = child->next_sibling;
}
}
void printSymTab(FILE *listing) {
fprintf(listing, "Scope Table");
fprintf(listing, "------------------------------------------------------------\n");
fprintf(listing, "Variable Name Symbol Kind Type Location Line Numbers\n");
fprintf(listing, "------------------------------------------------------------\n");
Scope curr = scope_global;
printScopeTableRecursive(listing, curr);
}