KCC - Kayte C Compiler 1.10.0
A C compiler implementation with preprocessor, lexer, parser, and code generator
Loading...
Searching...
No Matches
symbol_table.c
1#include "symbol_table.h"
2#include "kcc.h"
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6
7SymbolTable *symbol_table_create(void) {
8 SymbolTable *table = malloc(sizeof(SymbolTable));
9 if (!table) {
10 error_fatal("Memory allocation failed for symbol table");
11 return NULL;
12 }
13
14 table->table = calloc(SYMBOL_TABLE_SIZE, sizeof(Symbol*));
15 if (!table->table) {
16 free(table);
17 error_fatal("Memory allocation failed for symbol table array");
18 return NULL;
19 }
20
21 table->current_scope = 0;
22 return table;
23}
24
25void symbol_table_destroy(SymbolTable *table) {
26 if (!table) return;
27
28 for (int i = 0; i < SYMBOL_TABLE_SIZE; i++) {
29 Symbol *symbol = table->table[i];
30 while (symbol) {
31 Symbol *next = symbol->next;
32 free(symbol->name);
33 free(symbol);
34 symbol = next;
35 }
36 }
37 free(table->table);
38 free(table);
39}
40
41void symbol_table_enter_scope(SymbolTable *table) {
42 if (table) {
43 table->current_scope++;
44 }
45}
46
47void symbol_table_exit_scope(SymbolTable *table) {
48 if (!table) return;
49
50 // Remove all symbols in the current scope
51 for (int i = 0; i < SYMBOL_TABLE_SIZE; i++) {
52 Symbol **current = &table->table[i];
53 while (*current) {
54 if ((*current)->scope_level == table->current_scope) {
55 Symbol *to_remove = *current;
56 *current = (*current)->next;
57 free(to_remove->name);
58 free(to_remove);
59 } else {
60 current = &(*current)->next;
61 }
62 }
63 }
64
65 if (table->current_scope > 0) {
66 table->current_scope--;
67 }
68}
69
70unsigned int symbol_table_hash(const char *name) {
71 if (!name) return 0;
72
73 unsigned int hash = 0;
74 for (int i = 0; name[i] != '\0'; i++) {
75 hash = hash * 31 + name[i];
76 }
77 return hash % SYMBOL_TABLE_SIZE;
78}
79
80bool symbol_table_insert(SymbolTable *table, const char *name, SymbolType symbol_type, DataType data_type) {
81 if (!table || !name) return false;
82
83 // Check if symbol already exists in current scope
84 if (symbol_table_lookup_current_scope(table, name)) {
85 return false; // Symbol already exists in current scope
86 }
87
88 unsigned int index = symbol_table_hash(name);
89
90 Symbol *symbol = malloc(sizeof(Symbol));
91 if (!symbol) {
92 error_fatal("Memory allocation failed for symbol");
93 return false;
94 }
95
96 symbol->name = safe_strdup(name);
97 if (!symbol->name) {
98 free(symbol);
99 return false;
100 }
101
102 symbol->symbol_type = symbol_type;
103 symbol->data_type = data_type;
104 symbol->scope_level = table->current_scope;
105 symbol->next = table->table[index];
106
107 table->table[index] = symbol;
108
109 return true;
110}
111
112Symbol *symbol_table_lookup(SymbolTable *table, const char *name) {
113 if (!table || !name) return NULL;
114
115 unsigned int index = symbol_table_hash(name);
116 Symbol *symbol = table->table[index];
117
118 while (symbol) {
119 if (strcmp(symbol->name, name) == 0) {
120 return symbol;
121 }
122 symbol = symbol->next;
123 }
124
125 return NULL;
126}
127
128Symbol *symbol_table_lookup_current_scope(SymbolTable *table, const char *name) {
129 if (!table || !name) return NULL;
130
131 unsigned int index = symbol_table_hash(name);
132 Symbol *symbol = table->table[index];
133
134 while (symbol) {
135 if (strcmp(symbol->name, name) == 0 && symbol->scope_level == table->current_scope) {
136 return symbol;
137 }
138 symbol = symbol->next;
139 }
140
141 return NULL;
142}
143
144void symbol_table_print(SymbolTable *table) {
145 if (!table) {
146 printf("Symbol table is NULL\n");
147 return;
148 }
149
150 printf("=== Symbol Table ===\n");
151 printf("Current scope: %d\n", table->current_scope);
152
153 for (int i = 0; i < SYMBOL_TABLE_SIZE; i++) {
154 Symbol *symbol = table->table[i];
155 while (symbol) {
156 const char *symbol_type_str;
157 switch (symbol->symbol_type) {
158 case SYMBOL_VARIABLE: symbol_type_str = "VARIABLE"; break;
159 case SYMBOL_FUNCTION: symbol_type_str = "FUNCTION"; break;
160 case SYMBOL_PARAMETER: symbol_type_str = "PARAMETER"; break;
161 default: symbol_type_str = "UNKNOWN"; break;
162 }
163
164 printf(" %s: %s %s (scope %d)\n",
165 symbol->name,
166 data_type_to_string(symbol->data_type),
167 symbol_type_str,
168 symbol->scope_level);
169
170 symbol = symbol->next;
171 }
172 }
173 printf("=== End Symbol Table ===\n");
174}
DataType
Data types supported by the compiler.
Definition types.h:193