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.c91
1 files changed, 73 insertions, 18 deletions
diff --git a/src/symbol_table.c b/src/symbol_table.c
index 6dc3e64..d03f858 100644
--- a/src/symbol_table.c
+++ b/src/symbol_table.c
@@ -10,6 +10,61 @@ extern int lineno; /* from lexical analyser */
10 10
11SymbolTable glo_symbol_table = {{{{0}, 0}}, MAXSYMBOLS, 0}; 11SymbolTable glo_symbol_table = {{{{0}, 0}}, MAXSYMBOLS, 0};
12SymbolTable loc_symbol_table = {{{{0}, 0}}, MAXSYMBOLS, 0}; 12SymbolTable loc_symbol_table = {{{{0}, 0}}, MAXSYMBOLS, 0};
13FunctionTable fun_table = {{{{0}, 0}}, MAXFUNCTIONS, 0};
14
15void fun_add(const char name[], int rt_type, int nb_par) {
16 int count;
17 for (count = 0; count < fun_table.size; count++) {
18 if (!strcmp(fun_table.entries[count].name, name)) {
19 fprintf(stderr,
20 "semantic error, redefinition of function %s near line %d\n",
21 name, lineno);
22 return;
23 }
24 }
25 if (++fun_table.size > fun_table.maxsize) {
26 fprintf(stderr, "too many functions near line %d\n", lineno);
27 exit(1);
28 }
29 strcpy(fun_table.entries[fun_table.size - 1].name, name);
30 fun_table.entries[fun_table.size - 1].return_type = rt_type;
31 fun_table.entries[fun_table.size - 1].nb_parameters = nb_par;
32}
33
34void fun_display_table() {
35 int count;
36 for (count = 0; count < fun_table.size; count++) {
37 if (fun_table.entries[count].return_type == INT)
38 fprintf(output,
39 ";%s, type de retour : entier, nombre de parametres : %d \n",
40 fun_table.entries[count].name,
41 fun_table.entries[count].nb_parameters);
42 else if (fun_table.entries[count].return_type == CHAR)
43 fprintf(output,
44 ";%s, type de retour : caractere, nombre de parametres : %d \n",
45 fun_table.entries[count].name,
46 fun_table.entries[count].nb_parameters);
47 else
48 fprintf(output,
49 ";%s, type de retour : void, nombre de parametres : %d \n",
50 fun_table.entries[count].name,
51 fun_table.entries[count].nb_parameters);
52 }
53 fprintf(output, "\n");
54}
55
56int fun_lookup(const char name[], int nb_param) {
57 int count;
58
59 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 return fun_table.entries[count].return_type;
62 }
63 }
64 fprintf(stderr, "No definition of the function %s (or wrong number of parameters) near line %d\n", name,
65 lineno);
66 return -1;
67}
13 68
14void glo_addVar(const char name[], int type) { 69void glo_addVar(const char name[], int type) {
15 int count; 70 int count;
@@ -28,7 +83,7 @@ void glo_addVar(const char name[], int type) {
28 strcpy(glo_symbol_table.entries[glo_symbol_table.size - 1].name, name); 83 strcpy(glo_symbol_table.entries[glo_symbol_table.size - 1].name, name);
29 glo_symbol_table.entries[glo_symbol_table.size - 1].type = type; 84 glo_symbol_table.entries[glo_symbol_table.size - 1].type = type;
30 glo_symbol_table.entries[glo_symbol_table.size - 1].addr = 85 glo_symbol_table.entries[glo_symbol_table.size - 1].addr =
31 (glo_symbol_table.size - 1) * 8; 86 (glo_symbol_table.size - 1) * 8;
32} 87}
33 88
34// Verifies that the variable exists and returns the type 89// Verifies that the variable exists and returns the type
@@ -52,8 +107,6 @@ int glo_get_addr(const char name[]) {
52 return glo_symbol_table.entries[count].addr; 107 return glo_symbol_table.entries[count].addr;
53 } 108 }
54 } 109 }
55 fprintf(stderr, "No definition of the variable %s near line %d\n", name,
56 lineno);
57 return -1; 110 return -1;
58} 111}
59 112
@@ -62,12 +115,12 @@ void glo_display_table() {
62 for (count = 0; count < glo_symbol_table.size; count++) { 115 for (count = 0; count < glo_symbol_table.size; count++) {
63 if (glo_symbol_table.entries[count].type == INT) 116 if (glo_symbol_table.entries[count].type == INT)
64 fprintf(output, ";entier: %s, pos: %d \n", 117 fprintf(output, ";entier: %s, pos: %d \n",
65 glo_symbol_table.entries[count].name, 118 glo_symbol_table.entries[count].name,
66 glo_symbol_table.entries[count].addr); 119 glo_symbol_table.entries[count].addr);
67 else 120 else
68 fprintf(output, ";caractere: %s, pos: %d \n", 121 fprintf(output, ";caractere: %s, pos: %d \n",
69 glo_symbol_table.entries[count].name, 122 glo_symbol_table.entries[count].name,
70 glo_symbol_table.entries[count].addr); 123 glo_symbol_table.entries[count].addr);
71 } 124 }
72 fprintf(output, "\n"); 125 fprintf(output, "\n");
73} 126}
@@ -89,7 +142,7 @@ void loc_addVar(const char name[], int type) {
89 strcpy(loc_symbol_table.entries[loc_symbol_table.size - 1].name, name); 142 strcpy(loc_symbol_table.entries[loc_symbol_table.size - 1].name, name);
90 loc_symbol_table.entries[loc_symbol_table.size - 1].type = type; 143 loc_symbol_table.entries[loc_symbol_table.size - 1].type = type;
91 loc_symbol_table.entries[loc_symbol_table.size - 1].addr = 144 loc_symbol_table.entries[loc_symbol_table.size - 1].addr =
92 (loc_symbol_table.size - 1) * 8; 145 (loc_symbol_table.size - 1) * 8 + 8;
93} 146}
94 147
95int loc_lookup(const char name[]) { 148int loc_lookup(const char name[]) {
@@ -100,7 +153,7 @@ int loc_lookup(const char name[]) {
100 return loc_symbol_table.entries[count].type; 153 return loc_symbol_table.entries[count].type;
101 } 154 }
102 } 155 }
103 //Check in global table 156 // Check in global table
104 for (count = 0; count < glo_symbol_table.size; count++) { 157 for (count = 0; count < glo_symbol_table.size; count++) {
105 if (!strcmp(glo_symbol_table.entries[count].name, name)) { 158 if (!strcmp(glo_symbol_table.entries[count].name, name)) {
106 return glo_symbol_table.entries[count].type; 159 return glo_symbol_table.entries[count].type;
@@ -128,12 +181,12 @@ void loc_display_table() {
128 for (count = 0; count < loc_symbol_table.size; count++) { 181 for (count = 0; count < loc_symbol_table.size; count++) {
129 if (loc_symbol_table.entries[count].type == INT) 182 if (loc_symbol_table.entries[count].type == INT)
130 fprintf(output, ";entier: %s, pos: %d \n", 183 fprintf(output, ";entier: %s, pos: %d \n",
131 loc_symbol_table.entries[count].name, 184 loc_symbol_table.entries[count].name,
132 loc_symbol_table.entries[count].addr); 185 loc_symbol_table.entries[count].addr);
133 else 186 else
134 fprintf(output, ";caractere: %s, pos: %d \n", 187 fprintf(output, ";caractere: %s, pos: %d \n",
135 loc_symbol_table.entries[count].name, 188 loc_symbol_table.entries[count].name,
136 loc_symbol_table.entries[count].addr); 189 loc_symbol_table.entries[count].addr);
137 } 190 }
138 fprintf(output, "\n"); 191 fprintf(output, "\n");
139} 192}
@@ -148,16 +201,18 @@ void loc_clean_table() {
148 201
149static char *string_of_type(int type) { 202static char *string_of_type(int type) {
150 switch (type) { 203 switch (type) {
151 case INT: return "INT"; 204 case INT:
152 case CHAR: return "CHAR"; 205 return "INT";
153 default: return "UNEXPECTED"; 206 case CHAR:
207 return "CHAR";
208 default:
209 return "UNEXPECTED";
154 } 210 }
155} 211}
156 212
157void check_expected_type(int type_to_check, int type_expected) { 213void check_expected_type(int type_to_check, int type_expected) {
158 if (type_to_check != type_expected) 214 if (type_to_check != type_expected)
159 fprintf(stderr, "Expected type : %s -> Got type : %s (near line %d)\n", 215 fprintf(stderr, "Expected type : %s -> Got type : %s (near line %d)\n",
160 string_of_type(type_to_check), 216 string_of_type(type_to_check), string_of_type(type_to_check),
161 string_of_type(type_to_check),
162 lineno); 217 lineno);
163} 218}