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