--- rpl/src/simplification.c 2019/11/05 15:37:51 1.70 +++ rpl/src/simplification.c 2019/11/06 17:31:47 1.71 @@ -218,15 +218,22 @@ ordonnancement_branches(const void *a1, if (((**_a1).feuille != NULL) && ((**_a2).feuille != NULL)) { + // Si les types sont identiques, on ne change rien. + if ((*(*(**_a1).feuille).donnee).type == (*(*(**_a2).feuille).donnee).type) { return(0); } - if (((*(*(**_a1).feuille).donnee).type == INT) || + // On rejette les nombres à la fin. + + if ((((*(*(**_a1).feuille).donnee).type == INT) || ((*(*(**_a1).feuille).donnee).type == REL) || - ((*(*(**_a1).feuille).donnee).type == CPL)) + ((*(*(**_a1).feuille).donnee).type == CPL)) && + ((((*(*(**_a2).feuille).donnee).type != INT) && + ((*(*(**_a2).feuille).donnee).type != REL) && + ((*(*(**_a2).feuille).donnee).type != CPL)))) { return(1); } @@ -248,6 +255,8 @@ simplification_arbre(struct_processus *s integer8 j; integer8 nouveaux_elements; + struct_arbre *s_branche; + struct_objet *s_objet; if ((*(*(*s_arbre).feuille).donnee).type != FCT) @@ -258,6 +267,86 @@ simplification_arbre(struct_processus *s return; } + // Transformation des soustractions que l'on remplace par + // une addition de l'opposé. Si l'on a une soustraction, + // on greffe donc une instruction NEG dans l'arbre. + // Note : à cet instant, l'instruction '-' ne peut avoir que deux + // opérandes. + + if (strcmp((*((struct_fonction *) (*(*((*s_arbre).feuille)).donnee).objet)) + .nom_fonction, "-") == 0) + { + if ((*s_arbre).nombre_branches != 2) + { + (*s_etat_processus).erreur_execution = d_ex_simplification; + return; + } + + liberation(s_etat_processus, (*((*s_arbre).feuille)).donnee); + + if (((*((*s_arbre).feuille)).donnee = allocation(s_etat_processus, + FCT)) == NULL) + { + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + if (((*((struct_fonction *) (*(*((*s_arbre).feuille)).donnee).objet)) + .nom_fonction = malloc(2 * sizeof(unsigned char))) == NULL) + { + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + strcpy((*((struct_fonction *) (*(*((*s_arbre).feuille)).donnee).objet)) + .nom_fonction, "+"); + (*((struct_fonction *) (*(*((*s_arbre).feuille)).donnee).objet)) + .nombre_arguments = 1; + + if ((s_branche = malloc(sizeof(struct_arbre))) == NULL) + { + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + if (((*s_branche).branches = malloc(sizeof(struct_arbre *))) == NULL) + { + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + if (((*s_branche).feuille = allocation_maillon(s_etat_processus)) + == NULL) + { + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + (*(*s_branche).feuille).suivant = NULL; + + if (((*(*s_branche).feuille).donnee = allocation(s_etat_processus, FCT)) + == NULL) + { + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + if (((*((struct_fonction *) (*(*(*s_branche).feuille).donnee).objet)) + .nom_fonction = malloc(4 * sizeof(unsigned char))) == NULL) + { + (*s_etat_processus).erreur_systeme = d_es_allocation_memoire; + return; + } + + strcpy((*((struct_fonction *) (*(*(*s_branche).feuille).donnee).objet)) + .nom_fonction, "NEG"); + (*((struct_fonction *) (*(*(*s_branche).feuille).donnee).objet)) + .nombre_arguments = 1; + (*s_branche).branches[0] = (*s_arbre).branches[1]; + (*s_branche).nombre_branches = 1; + (*s_arbre).branches[1] = s_branche; + } + // La feuille est une fonction, on peut envisager la simplification // de l'arbre. Pour cela, on descend d'un niveau pour greffer // de nouvelles branches. @@ -274,6 +363,9 @@ simplification_arbre(struct_processus *s if (strcmp((*((struct_fonction *) (*s_objet).objet)) .nom_fonction, "-") == 0) { + simplification_arbre(s_etat_processus, + (*s_arbre).branches[i]); + s_objet = (*((*((*s_arbre).branches[i])).feuille)).donnee; } if (strcmp((*((struct_fonction *) (*s_objet).objet)) @@ -555,8 +647,6 @@ simplification(struct_processus *s_etat_ */ affichage_arbre(s_etat_processus, s_arbre, 0); - -//# ifdef EXPERIMENTAL_CODE simplification_arbre(s_etat_processus, s_arbre); affichage_arbre(s_etat_processus, s_arbre, 0); @@ -564,7 +654,6 @@ simplification(struct_processus *s_etat_ { return(NULL); } -//# endif /* * Transcription de l'arbre q-aire simplifié en expression algébrique.