-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.c
349 lines (270 loc) · 9.99 KB
/
ast.c
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include "ast.h"
/* constantes pour l'affichage des noms */
static const char *noms_primitifs[NB_NAT_PRIMITIFS] = {
"carre",
"triangle"
};
static const char *noms_operations[NB_OPERATIONS] = {
"ROT",
"JUXT",
"SUPER"
};
/*---------------------------------------------------------------------------*/
/* FONCTIONS PORTEES PAR LES NOEUDS */
/* fonctions suivant les signatures des champs afficher et evaluer */
/* definies dans ast.h, adaptees aux differentes natures de noeud d'un ast */
/*---------------------------------------------------------------------------*/
/*----------- Affichage */
/* Fonctions "specifiques" locales (static) d'affichage d'un noeud
selon sa nature. Declarees ici, definies plus bas */
static void afficher_valeur(struct noeud_ast *ast);
static void afficher_unaire(struct noeud_ast *ast);
static void afficher_binaire(struct noeud_ast *ast);
/*----------- Evaluation */
/* Fonctions "specifiques" locales (static) d'evaluation d'un noeud
selon son type. Declarees ici, definies plus bas */
static struct patchwork *evaluer_valeur(struct noeud_ast *ast);
static struct patchwork *evaluer_unaire(struct noeud_ast *ast);
static struct patchwork *evaluer_binaire(struct noeud_ast *ast);
/*----------- Creation des patchworks */
/* Types des pointeurs sur les fonctions de creation des patchworks.
* Les signatures different selon les noeuds.
* Les fonctions specifiques sont definies ds le module patchwork.o */
typedef struct patchwork *(*creer_patchwork_valeur_fct)
(const enum nature_primitif);
typedef struct patchwork *(*creer_patchwork_unaire_fct)
(const struct patchwork *);
typedef struct patchwork *(*creer_patchwork_binaire_fct)
(const struct patchwork *,
const struct patchwork *);
/*---------------------------------------------------------------------------*/
/* STRUCTURES DES NOEUDS */
/*---------------------------------------------------------------------------*/
enum nature_noeud {
VALEUR, /* feuille */
OPERATION, /* noeud interne */
NB_NAT_NOEUDS /* sentinelle */
};
enum arite_operation {
UNAIRE,
BINAIRE,
NB_ARITES_OPERATIONS /* sentinelle */
};
struct operation_unaire {
struct noeud_ast *operande;
creer_patchwork_unaire_fct creer_patchwork;
};
struct operation_binaire {
struct noeud_ast *operande_gauche;
struct noeud_ast *operande_droit;
creer_patchwork_binaire_fct creer_patchwork;
};
struct operation {
enum arite_operation arite;
union {
struct operation_unaire oper_un;
struct operation_binaire oper_bin;
} u;
};
struct valeur {
enum nature_primitif nature;
creer_patchwork_valeur_fct creer_patchwork;
};
struct noeud_ast_data {
const char *nom;
// nature du noeud: VALEUR ou OPERATION
enum nature_noeud nature;
// selon la nature, les donnees representant le noeud
union {
struct valeur val; // si nature == VALEUR
struct operation oper; // si nature == OPERATION
} u;
};
/*----------- Fonctions de vérification */
static void erreur(const char *msg);
/*---------------------------------------------------------------------------*/
/* AFFICHAGE */
/*---------------------------------------------------------------------------*/
/* Definitions (locale) des fonctions "specifiques"
* Pas de verif sur le type d'ast, ces fonctions ont ete "branchees" sur
* les noeud de type adequat lors de leur construction */
static void afficher_valeur(struct noeud_ast *ast)
{
if (ast != NULL && ast->data != NULL) {
printf("%s", ast->data->nom);
} else {
printf("%s", "null");
}
}
static void afficher_unaire(struct noeud_ast *ast)
{
if (ast != NULL) {
struct noeud_ast *op = ast->data->u.oper.u.oper_un.operande;
printf("%s[", ast->data->nom);
(*(op->afficher))(op);
printf("]");
} else {
printf("%s", "null");
}
}
static void afficher_binaire(struct noeud_ast *ast)
{
if (ast != NULL) {
struct noeud_ast *opg = ast->data->u.oper.u.oper_bin.operande_gauche;
struct noeud_ast *opd = ast->data->u.oper.u.oper_bin.operande_droit;
printf("%s[", ast->data->nom);
(*(opg->afficher))(opg);
printf(", ");
(*(opd->afficher))(opd);
printf("]");
} else {
printf("%s", "null");
}
}
/*---------------------------------------------------------------------------*/
/* FONCTIONS D'EVALUATION */
/*---------------------------------------------------------------------------*/
/* Definitions (locale) des fonctions "specifiques"
* Pas de verif sur le type d'ast, ces fonctions ont ete "branchees" sur
* les noeud de type adequat lors de leur construction */
static struct patchwork *evaluer_valeur(struct noeud_ast *ast)
{
if (ast != NULL && ast->data != NULL) {
return ast->data->u.val.creer_patchwork(ast->data->u.val.nature);
} else {
erreur("ERREUR. Problème d'évaluation de valeur.");
return NULL;
}
}
static struct patchwork *evaluer_unaire(struct noeud_ast *ast)
{
if (ast != NULL && ast->data != NULL) {
struct noeud_ast *op = ast->data->u.oper.u.oper_un.operande;
struct patchwork *base = (*(op->evaluer))(op);
struct patchwork *res = ast->data->u.oper.u.oper_un.creer_patchwork(base);
liberer_patchwork(base);
return res;
} else {
erreur("ERREUR. Problème d'évaluation de valeur.");
return NULL;
}
}
static struct patchwork *evaluer_binaire(struct noeud_ast *ast)
{
if (ast != NULL && ast->data != NULL) {
struct noeud_ast *op_g = ast->data->u.oper.u.oper_bin.operande_gauche;
struct noeud_ast *op_d = ast->data->u.oper.u.oper_bin.operande_droit;
struct patchwork *base_g = (*(op_g->evaluer))(op_g);
struct patchwork *base_d = (*(op_d->evaluer))(op_d);
struct patchwork *res = ast->data->u.oper.u.oper_bin.creer_patchwork(base_g, base_d);
liberer_patchwork(base_g);
liberer_patchwork(base_d);
return res;
} else {
erreur("ERREUR. Problème d'évaluation de valeur.");
return NULL;
}
}
/*---------------------------------------------------------------------------*/
/* CREATION DES NOEUDS */
/*---------------------------------------------------------------------------*/
// C'est a la creation des noeuds que les "branchements" vers les fonctions
// adequats sont realises
struct noeud_ast *creer_valeur(const enum nature_primitif nat_prim)
{
if ((int) nat_prim < 0 || (int) nat_prim >= NB_NAT_PRIMITIFS)
erreur("ERREUR. Valeur du primitif inexistante.");
struct noeud_ast *noeud = malloc(sizeof(struct noeud_ast));
struct noeud_ast_data *data = malloc(sizeof(struct noeud_ast_data));
// Initialisation du contenu du "noeud_ast" et branchements
noeud->data = data;
noeud->evaluer = &evaluer_valeur;
noeud->afficher = &afficher_valeur;
// Initialisation du contenu du "noeud_ast_data"
data->nom = noms_primitifs[nat_prim];
data->nature = VALEUR;
data->u.val.nature = nat_prim;
data->u.val.creer_patchwork = &creer_primitif;
return noeud;
}
struct noeud_ast *creer_unaire(const enum nature_operation nat_oper,
struct noeud_ast *opde)
{
if ((int) nat_oper < 0 || (int) nat_oper >= NB_OPERATIONS)
erreur("ERREUR. Opération unaire inexistante.");
struct noeud_ast *noeud = malloc(sizeof(struct noeud_ast));
struct noeud_ast_data *data = malloc(sizeof(struct noeud_ast_data));
// Initialisation du contenu du "noeud_ast" et branchements
noeud->data = data;
noeud->evaluer = &evaluer_unaire;
noeud->afficher = &afficher_unaire;
// Initialisation du contenu du "noeud_ast_data"
data->nom = noms_operations[nat_oper];
data->nature = OPERATION;
data->u.oper.arite = UNAIRE;
data->u.oper.u.oper_un.operande = opde;
// INFO. Fonctionne tant que la seule opération unaire est "ROTATION".
// Si cela change, il faudra différencier les cas (cf. binaire).
data->u.oper.u.oper_un.creer_patchwork = &creer_rotation;
return noeud;
}
struct noeud_ast *creer_binaire(const enum nature_operation nat_oper,
struct noeud_ast *opde_g,
struct noeud_ast *opde_d)
{
if ((int) nat_oper < 0 || (int) nat_oper >= NB_OPERATIONS)
erreur("ERREUR. Opération binaire inexistante.");
struct noeud_ast *noeud = malloc(sizeof(struct noeud_ast));
struct noeud_ast_data *data = malloc(sizeof(struct noeud_ast_data));
// Initialisation du contenu du "noeud_ast" et branchements
noeud->data = data;
noeud->evaluer = &evaluer_binaire;
noeud->afficher = &afficher_binaire;
// Initialisation du contenu du "noeud_ast_data"
data->nom = noms_operations[nat_oper];
data->nature = OPERATION;
data->u.oper.arite = BINAIRE;
data->u.oper.u.oper_bin.operande_gauche = opde_g;
data->u.oper.u.oper_bin.operande_droit = opde_d;
switch (nat_oper) {
case JUXTAPOSITION:
data->u.oper.u.oper_bin.creer_patchwork = &creer_juxtaposition;
break;
case SUPERPOSITION:
data->u.oper.u.oper_bin.creer_patchwork = &creer_superposition;
break;
default:
exit(EXIT_FAILURE);
}
return noeud;
}
/*---------------------------------------------------------------------------*/
/* LIBERATION DES NOEUDS */
/*---------------------------------------------------------------------------*/
// Ici pas de fonction specifique portee par chaque noeud
// Comment faire? Comparer les deux modeles!
// On libère l'arbre syntaxique abstrait selon un parcours postfixe.
void liberer_expression(struct noeud_ast *res)
{
if (res != NULL) {
if (res->data->nature == OPERATION) {
switch (res->data->u.oper.arite) {
case UNAIRE:
liberer_expression(res->data->u.oper.u.oper_un.operande);
break;
case BINAIRE:
liberer_expression(res->data->u.oper.u.oper_bin.operande_gauche);
liberer_expression(res->data->u.oper.u.oper_bin.operande_droit);
break;
default:
break;
}
}
free(res->data);
free(res);
}
}
void erreur(const char *msg) {
printf("%s", msg);
exit(EXIT_FAILURE);
}