-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableSymbole.c
More file actions
210 lines (179 loc) · 5.25 KB
/
Copy pathTableSymbole.c
File metadata and controls
210 lines (179 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "TableSymbole.h"
/********************************************************************************/
int isIntOrfloat(int x){
int result = 0 ;
if (x == 1 || x == 2){
result = 1 ;
}
return result ;
}
int isInt(int x){
int result = 0 ;
if (x == 1){
result = 1 ;
}
return result ;
}
int isFlt(int x){
int result = 0 ;
if (x == 2){
result = 1 ;
}
return result ;
}
// création de la table des symboles
SymbolTable* createSymbolTable() {
SymbolTable* table = (SymbolTable*)malloc(sizeof(SymbolTable));
if (table != NULL) {
table->head = NULL;
table->nbSymbol = 0;
}
return table;
}
// affichage table
void afficher(SymbolTable* table) {
Symbol* current = table->head;
printf("\nAffichage table des symboles :\n");
while (current != NULL) {
// Print symbol information
printf("Nom: %s, Type: %d, Value: %s\n", current->nom, current->type, current->value);
// Print attributes
for (int i = 0; i < 20; i++) {
if (current->attr[i].nom != NULL) {
printf(" Attribute: %s, Value: %s, Type: %s\n", current->attr[i].nom,
current->attr[i].value, current->attr[i].type);
}
}
current = current->next;
}
}
// création d'un symbole
Symbol* createSymbol(char* nom, int type, char* value) {
Symbol* symbol = (Symbol*)malloc(sizeof(Symbol));
if (symbol != NULL) {
symbol->nom = strdup(nom);
symbol->type = type;
symbol->value = strdup(value);
symbol->next = NULL;
for (int i = 0; i < 20; i++) {
symbol->attr[i].nom = NULL;
symbol->attr[i].value = NULL;
symbol->attr[i].type = NULL;
}
}
return symbol;
}
// modify symbole
void SetTypeSymbol(Symbol* symbole, int type) {
if (symbole != NULL) {
symbole->type = type;
}
}
void SetNomSymbol(Symbol* symbole, char* nom) {
if (symbole != NULL) {
free(symbole->nom);
symbole->nom = strdup(nom);
}
}
void SetValueSymbol(Symbol* symbole, char* value) {
if (symbole != NULL) {
free(symbole->value);
symbole->value = strdup(value);
}
}
// insertion symbole
void insertSymbol(SymbolTable* table, Symbol* newSymbol) {
if (table != NULL && newSymbol != NULL) {
newSymbol->next = table->head;
table->head = newSymbol;
table->nbSymbol++;
}
}
// recherche
Symbol* symbolSearchNom(SymbolTable* table, char* symbolName) {
Symbol* current = table->head;
while (current != NULL) {
if (strcmp(current->nom, symbolName) == 0) {
return current;
}
current = current->next;
}
return NULL;
}
// supprimer symbole
void deleteSymbol(SymbolTable* table, Symbol* symbolToDelete) {
if (table != NULL && symbolToDelete != NULL) {
Symbol* current = table->head;
Symbol* previous = NULL;
while (current != NULL && current != symbolToDelete) {
previous = current;
current = current->next;
}
if (current != NULL) {
if (previous != NULL) {
previous->next = current->next;
} else {
table->head = current->next;
}
// Free memory
free(current->nom);
free(current->value);
free(current);
table->nbSymbol--;
}
}
}
// Pour les attributs
Attribute* createAttribute(char* attributeName, char* attributeValue, char* attributeType) {
Attribute* attribute = (Attribute*)malloc(sizeof(Attribute));
if (attribute != NULL) {
attribute->nom = strdup(attributeName);
attribute->value = strdup(attributeValue);
attribute->type = strdup(attributeType);
}
return attribute;
}
void insertAttribute(Symbol* symbol, Attribute* newAttribute) {
if (symbol != NULL && newAttribute != NULL) {
int i = 0;
while (i < 20 && symbol->attr[i].nom != NULL) {
i++;
}
if (i < 20) {
symbol->attr[i].nom = strdup(newAttribute->nom);
symbol->attr[i].value = strdup(newAttribute->value);
symbol->attr[i].type = strdup(newAttribute->type);
}
}
}
Attribute* attributeSearchNom(Symbol* symbol, char* attributeName) {
if (symbol != NULL) {
int i = 0;
while (i < 20 && symbol->attr[i].nom != NULL) {
if (strcmp(symbol->attr[i].nom, attributeName) == 0) {
return &(symbol->attr[i]);
}
i++;
}
}
return NULL;
}
void deleteAttribute(Symbol* symbol, Attribute* attributeToDelete) {
if (symbol != NULL && attributeToDelete != NULL) {
int i = 0;
while (i < 20 && symbol->attr[i].nom != NULL) {
if (&(symbol->attr[i]) == attributeToDelete) {
// Free memory
free(symbol->attr[i].nom);
free(symbol->attr[i].value);
free(symbol->attr[i].type);
// Remove attribute
symbol->attr[i].nom = NULL;
symbol->attr[i].value = NULL;
symbol->attr[i].type = NULL;
return;
}
i++;
}
}
}