aboutsummaryrefslogtreecommitdiff
path: root/src/symbol_table.c
diff options
context:
space:
mode:
authorpacien2018-05-07 01:33:55 +0200
committerpacien2018-05-07 01:33:55 +0200
commit15390a3b612562a8f1c995e968cd3b1943375ab9 (patch)
tree23fdd2a5b680bf18f3505199b11a2e5ed2786271 /src/symbol_table.c
parent49662d2162ef19a137faf17736fb7641d0b61452 (diff)
downloadtpc-compiler-15390a3b612562a8f1c995e968cd3b1943375ab9.tar.gz
Code cleanup and reformating
Diffstat (limited to 'src/symbol_table.c')
-rw-r--r--src/symbol_table.c163
1 files changed, 163 insertions, 0 deletions
diff --git a/src/symbol_table.c b/src/symbol_table.c
new file mode 100644
index 0000000..6f84ce4
--- /dev/null
+++ b/src/symbol_table.c
@@ -0,0 +1,163 @@
1/**
2 * UPEM / Compilation / Projet
3 * Pacien TRAN-GIRARD, Adam NAILI
4 */
5
6#include "symbol_table.h"
7
8extern int lineno; /* from lexical analyser */
9
10SymbolTable glo_symbol_table = {{{{0}, 0}}, MAXSYMBOLS, 0};
11SymbolTable loc_symbol_table = {{{{0}, 0}}, MAXSYMBOLS, 0};
12
13void glo_addVar(const char name[], int type) {
14 int count;
15 for (count = 0; count < glo_symbol_table.size; count++) {
16 if (!strcmp(glo_symbol_table.entries[count].name, name)) {
17 fprintf(stderr,
18 "semantic error, redefinition of variable %s near line %d\n",
19 name, lineno);
20 return;
21 }
22 }
23 if (++glo_symbol_table.size > glo_symbol_table.maxsize) {
24 fprintf(stderr, "too many variables near line %d\n", lineno);
25 exit(1);
26 }
27 strcpy(glo_symbol_table.entries[glo_symbol_table.size - 1].name, name);
28 glo_symbol_table.entries[glo_symbol_table.size - 1].type = type;
29 glo_symbol_table.entries[glo_symbol_table.size - 1].addr =
30 (glo_symbol_table.size - 1) * 8;
31}
32
33// Verifies that the variable exists and returns the type
34int glo_lookup(const char name[]) {
35 int count;
36
37 for (count = 0; count < glo_symbol_table.size; count++) {
38 if (!strcmp(glo_symbol_table.entries[count].name, name)) {
39 return glo_symbol_table.entries[count].type;
40 }
41 }
42 fprintf(stderr, "No definition of the variable %s near line %d\n", name,
43 lineno);
44 return -1;
45}
46
47int glo_get_addr(const char name[]) {
48 int count;
49
50 for (count = 0; count < glo_symbol_table.size; count++) {
51 if (!strcmp(glo_symbol_table.entries[count].name, name)) {
52 return glo_symbol_table.entries[count].addr;
53 }
54 }
55 fprintf(stderr, "No definition of the variable %s near line %d\n", name,
56 lineno);
57 return -1;
58}
59
60void glo_display_table() {
61 int count;
62 for (count = 0; count < glo_symbol_table.size; count++) {
63 if (glo_symbol_table.entries[count].type == INT)
64 printf(";entier: %s, pos: %d \n",
65 glo_symbol_table.entries[count].name,
66 glo_symbol_table.entries[count].addr);
67 else
68 printf(";caractere: %s, pos: %d \n",
69 glo_symbol_table.entries[count].name,
70 glo_symbol_table.entries[count].addr);
71 }
72 printf("\n");
73}
74
75void loc_addVar(const char name[], int type) {
76 int count;
77 for (count = 0; count < loc_symbol_table.size; count++) {
78 if (!strcmp(loc_symbol_table.entries[count].name, name)) {
79 fprintf(stderr,
80 "semantic error, redefinition of variable %s near line %d\n",
81 name, lineno);
82 return;
83 }
84 }
85 if (++loc_symbol_table.size > loc_symbol_table.maxsize) {
86 fprintf(stderr, "too many variables near line %d\n", lineno);
87 exit(1);
88 }
89 strcpy(loc_symbol_table.entries[loc_symbol_table.size - 1].name, name);
90 loc_symbol_table.entries[loc_symbol_table.size - 1].type = type;
91 loc_symbol_table.entries[loc_symbol_table.size - 1].addr =
92 (loc_symbol_table.size - 1) * 8;
93}
94
95int loc_lookup(const char name[]) {
96 int count;
97
98 for (count = 0; count < loc_symbol_table.size; count++) {
99 if (!strcmp(loc_symbol_table.entries[count].name, name)) {
100 return loc_symbol_table.entries[count].type;
101 }
102 }
103 //Check in global table
104 for (count = 0; count < glo_symbol_table.size; count++) {
105 if (!strcmp(glo_symbol_table.entries[count].name, name)) {
106 return glo_symbol_table.entries[count].type;
107 }
108 }
109 fprintf(stderr, "No definition of the variable %s near line %d\n", name,
110 lineno);
111 return -1;
112}
113
114int loc_get_addr(const char name[]) {
115 int count;
116
117 for (count = 0; count < loc_symbol_table.size; count++) {
118 if (!strcmp(loc_symbol_table.entries[count].name, name)) {
119 return loc_symbol_table.entries[count].addr;
120 }
121 }
122 fprintf(stderr, "No definition of the variable %s near line %d\n", name,
123 lineno);
124 return -1;
125}
126void loc_display_table() {
127 int count;
128 for (count = 0; count < loc_symbol_table.size; count++) {
129 if (loc_symbol_table.entries[count].type == INT)
130 printf(";entier: %s, pos: %d \n",
131 loc_symbol_table.entries[count].name,
132 loc_symbol_table.entries[count].addr);
133 else
134 printf(";caractere: %s, pos: %d \n",
135 loc_symbol_table.entries[count].name,
136 loc_symbol_table.entries[count].addr);
137 }
138 printf("\n");
139}
140
141void loc_clean_table() {
142 int i;
143 for (i = 0; i < loc_symbol_table.size; i++) {
144 printf("pop eax\n");
145 }
146 loc_symbol_table.size = 0;
147}
148
149static char *string_of_type(int type) {
150 switch (type) {
151 case INT: return "INT";
152 case CHAR: return "CHAR";
153 default: return "UNEXPECTED";
154 }
155}
156
157void check_expected_type(int type_to_check, int type_expected) {
158 if (type_to_check != type_expected)
159 fprintf(stderr, "Expected type : %s -> Got type : %s (near line %d)\n",
160 string_of_type(type_to_check),
161 string_of_type(type_to_check),
162 lineno);
163}