aboutsummaryrefslogtreecommitdiff
path: root/src/symbol_table.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/symbol_table.c')
-rw-r--r--src/symbol_table.c42
1 files changed, 19 insertions, 23 deletions
diff --git a/src/symbol_table.c b/src/symbol_table.c
index cff675a..b522929 100644
--- a/src/symbol_table.c
+++ b/src/symbol_table.c
@@ -7,6 +7,7 @@
7#include "generator.h" 7#include "generator.h"
8 8
9extern int lineno; /* from lexical analyser */ 9extern int lineno; /* from lexical analyser */
10void yyerror(char *);
10 11
11SymbolTable glo_symbol_table = {{{{0}, 0}}, MAXSYMBOLS, 0}; 12SymbolTable glo_symbol_table = {{{{0}, 0}}, MAXSYMBOLS, 0};
12SymbolTable loc_symbol_table = {{{{0}, 0}}, MAXSYMBOLS, 0}; 13SymbolTable loc_symbol_table = {{{{0}, 0}}, MAXSYMBOLS, 0};
@@ -57,12 +58,14 @@ int fun_lookup(const char name[], int nb_param) {
57 int count; 58 int count;
58 59
59 for (count = 0; count < fun_table.size; count++) { 60 for (count = 0; count < fun_table.size; count++) {
60 if (!strcmp(fun_table.entries[count].name, name) && (fun_table.entries[count].nb_parameters == nb_param)) { 61 if (!strcmp(fun_table.entries[count].name, name) &&
62 (fun_table.entries[count].nb_parameters == nb_param)) {
61 return fun_table.entries[count].return_type; 63 return fun_table.entries[count].return_type;
62 } 64 }
63 } 65 }
64 fprintf(stderr, "No definition of the function %s (or wrong number of parameters) near line %d\n", name, 66 fprintf(stderr, "No definition of the function %s (or wrong number of "
65 lineno); 67 "parameters) near line %d\n",
68 name, lineno);
66 return -1; 69 return -1;
67} 70}
68 71
@@ -82,17 +85,14 @@ static void glo_add(const char name[], int type, bool read_only) {
82 } 85 }
83 strcpy(glo_symbol_table.entries[glo_symbol_table.size - 1].name, name); 86 strcpy(glo_symbol_table.entries[glo_symbol_table.size - 1].name, name);
84 glo_symbol_table.entries[glo_symbol_table.size - 1].type = type; 87 glo_symbol_table.entries[glo_symbol_table.size - 1].type = type;
85 glo_symbol_table.entries[glo_symbol_table.size - 1].addr = (glo_symbol_table.size - 1) * 8; 88 glo_symbol_table.entries[glo_symbol_table.size - 1].addr =
89 (glo_symbol_table.size - 1) * 8;
86 glo_symbol_table.entries[glo_symbol_table.size - 1].read_only = read_only; 90 glo_symbol_table.entries[glo_symbol_table.size - 1].read_only = read_only;
87} 91}
88 92
89void glo_addVar(const char name[], int type) { 93void glo_addVar(const char name[], int type) { glo_add(name, type, false); }
90 glo_add(name, type, false);
91}
92 94
93void glo_addConst(const char name[]) { 95void glo_addConst(const char name[]) { glo_add(name, INT, true); }
94 glo_add(name, INT, true);
95}
96 96
97// Verifies that the variable exists and returns the type 97// Verifies that the variable exists and returns the type
98int glo_lookup(const char name[]) { 98int glo_lookup(const char name[]) {
@@ -149,17 +149,14 @@ static void loc_add(const char name[], int type, bool read_only) {
149 } 149 }
150 strcpy(loc_symbol_table.entries[loc_symbol_table.size - 1].name, name); 150 strcpy(loc_symbol_table.entries[loc_symbol_table.size - 1].name, name);
151 loc_symbol_table.entries[loc_symbol_table.size - 1].type = type; 151 loc_symbol_table.entries[loc_symbol_table.size - 1].type = type;
152 loc_symbol_table.entries[loc_symbol_table.size - 1].addr = (loc_symbol_table.size - 1) * 8 + 8; 152 loc_symbol_table.entries[loc_symbol_table.size - 1].addr =
153 (loc_symbol_table.size - 1) * 8 + 8;
153 loc_symbol_table.entries[loc_symbol_table.size - 1].read_only = read_only; 154 loc_symbol_table.entries[loc_symbol_table.size - 1].read_only = read_only;
154} 155}
155 156
156void loc_addVar(const char name[], int type) { 157void loc_addVar(const char name[], int type) { loc_add(name, type, false); }
157 loc_add(name, type, false);
158}
159 158
160void loc_addConst(const char name[]) { 159void loc_addConst(const char name[]) { loc_add(name, INT, true); }
161 loc_add(name, INT, true);
162}
163 160
164int loc_lookup(const char name[]) { 161int loc_lookup(const char name[]) {
165 int count; 162 int count;
@@ -205,9 +202,7 @@ void loc_display_table() {
205 fprintf(output, "\n"); 202 fprintf(output, "\n");
206} 203}
207 204
208void loc_clean_table() { 205void loc_clean_table() { loc_symbol_table.size = 0; }
209 loc_symbol_table.size = 0;
210}
211 206
212static char *string_of_type(int type) { 207static char *string_of_type(int type) {
213 switch (type) { 208 switch (type) {
@@ -230,11 +225,12 @@ void check_expected_type(Type type_to_check, Type type_expected) {
230 string_of_type(type_expected), string_of_type(type_to_check), 225 string_of_type(type_expected), string_of_type(type_to_check),
231 lineno); 226 lineno);
232} 227}
233void check_expected_types(Type type_to_check, Type type_expected1, Type type_expected2) { 228void check_expected_types(Type type_to_check, Type type_expected1,
229 Type type_expected2) {
234 if (type_to_check != type_expected1 && type_to_check != type_expected2) 230 if (type_to_check != type_expected1 && type_to_check != type_expected2)
235 fprintf(stderr, "Expected type : %s OR %s-> Got type : %s (near line %d)\n", 231 fprintf(stderr, "Expected type : %s OR %s-> Got type : %s (near line %d)\n",
236 string_of_type(type_expected1), string_of_type(type_expected2), string_of_type(type_to_check), 232 string_of_type(type_expected1), string_of_type(type_expected2),
237 lineno); 233 string_of_type(type_to_check), lineno);
238} 234}
239 235
240/* returns false if symbol can't be found too */ 236/* returns false if symbol can't be found too */