From 6e5f6775d16e9730ccf1edbecf14da52d0ef134a Mon Sep 17 00:00:00 2001 From: Pixel Date: Thu, 3 May 2001 00:20:43 +0000 Subject: Indentation --- lib/exceptions.c | 115 +++++---- lib/fonctions.c | 289 +++++++++++---------- lib/hash.c | 211 ++++++++-------- lib/interface.c | 327 +++++++++++++----------- lib/numbers.c | 148 +++++------ lib/parser.c | 388 ++++++++++++++-------------- lib/pile.c | 759 +++++++++++++++++++++++++++++-------------------------- lib/polynom.c | 629 ++++++++++++++++++++++++--------------------- lib/scalaires.c | 249 ++++++++++-------- lib/terminal.c | 10 +- 10 files changed, 1665 insertions(+), 1460 deletions(-) (limited to 'lib') diff --git a/lib/exceptions.c b/lib/exceptions.c index b19df9b..b4a08a4 100644 --- a/lib/exceptions.c +++ b/lib/exceptions.c @@ -20,88 +20,95 @@ char *contexts[128]; int clevel = 0; int global_error = 0; -/* Les fonctions strdup et malloc sont réécrites ici afin de simplifier la vie en cas d'erreur. */ +/* + * Les fonctions strdup et malloc sont reecrites ici afin de simplifier la vie en cas d'erreur. + */ char *Estrdup(char *o) { - char *r; + char *r; - if (o) { - if (!(r = strdup(o))) { - exception(2, _("Out of memory.")); - } - } else { - return NULL; + if (o) { + if (!(r = strdup(o))) { + exception(2, _("Out of memory.")); } - return r; + } else { + return NULL; + } + return r; } void *Emalloc(size_t s) { - void *r; + void *r; - if (s) { - if (!(r = malloc(s))) { - exception(2, _("Out of memory.")); - } - } else { - return NULL; + if (s) { + if (!(r = malloc(s))) { + exception(2, _("Out of memory.")); } - return r; + } else { + return NULL; + } + return r; } -/* Les routines de manipulation de la pile de contexte d'erreurs */ +/* + * Les routines de manipulation de la pile de contexte d'erreurs + */ void pushcontext(char *c) { - if (clevel == 128) { - exception(2, _("Too much error contexts during pushcontext().")); - } - contexts[clevel++] = Estrdup(c); + if (clevel == 128) { + exception(2, _("Too much error contexts during pushcontext().")); + } + contexts[clevel++] = Estrdup(c); #ifdef DEBUG - fprintf(stderr, "%s\n", c); + fprintf(stderr, "%s\n", c); #endif } void popcontext(void) { - if (clevel == 0) { - exception(2, _("Error context empty, but popcontext() called.")); - } - free(contexts[--clevel]); + if (clevel == 0) { + exception(2, _("Error context empty, but popcontext() called.")); + } + free(contexts[--clevel]); } void flushcontext(void) { - while (clevel) { - popcontext(); - } + while (clevel) { + popcontext(); + } } -/* exception avec level=1 pour une erreur non fatale avec vidage de la pile sinon -pour toutre valeur de level erreur avec terminaison du programme */ +/* + * exception avec level=1 pour une erreur non fatale avec vidage de la pile sinon pour toutre + * valeur de level erreur avec terminaison du programme + */ -void exception(int level, char *msg) +void exception(int level, char *msg) { - int i; - switch (level) { - case 1: - fprintf(stderr, _("Non-fatal error detected. Showing context.\n")); - for (i = 0; i < clevel; i++) { - fprintf(stderr, " (%i) - %s\n", i, contexts[i]); - } - fprintf(stderr, _(" Error description: %s\n"), msg); - flush_pile(); - global_error = 1; - break; - default: - fprintf(stderr, _("Fatal error detected. Showing context.\n")); - for (i = 0; i < clevel; i++) { - fprintf(stderr, " (%i) - %s\n", i, contexts[i]); - } - fprintf(stderr, _(" Error description: %s\n"), msg); - clearterm(); - exit(1); - break; - } + int i; + + switch (level) { + case 1: + fprintf(stderr, _("Non-fatal error detected. Showing context.\n")); + for (i = 0; i < clevel; i++) { + fprintf(stderr, " (%i) - %s\n", i, contexts[i]); + } + fprintf(stderr, _(" Error description: %s\n"), msg); + flush_pile(); + global_error = 1; + break; + default: + fprintf(stderr, _("Fatal error detected. Showing context.\n")); + for (i = 0; i < clevel; i++) { + fprintf(stderr, " (%i) - %s\n", i, contexts[i]); + } + fprintf(stderr, _(" Error description: %s\n"), msg); + clearterm(); + exit(1); + break; + } } diff --git a/lib/fonctions.c b/lib/fonctions.c index 2d25eea..674b2e8 100644 --- a/lib/fonctions.c +++ b/lib/fonctions.c @@ -3,7 +3,7 @@ * fonctions avancees sur les polynomes * */ - + #include "fonctions.h" #include "pile.h" #include "hash.h" @@ -19,196 +19,211 @@ #define _(x) x #endif -/* Nous allons utiliser des pointeurs sur des fonctions. Voici donc les typedefs pour notre structure... */ -typedef void (*func_name)(polynome arg1, polynome arg2, polynome arg3); +/* + * Nous allons utiliser des pointeurs sur des fonctions. Voici donc les typedefs pour notre + * structure... + */ +typedef void (*func_name) (polynome arg1, polynome arg2, polynome arg3); typedef struct func_t { - func_name func; - char *nom; - int arite; + func_name func; + char *nom; + int arite; } func_t; -/* ... et la structure elle-même */ - +/* + * ... et la structure elle-meme + */ + static func_t func_table[] = { - { deriv, "deriv", 1, }, - { derivn, "derivn", 2 }, - { integ, "int", 1 }, - { printvars, "printvars", 0 }, - { help, "help", 0 }, - { setdisplay, "setdisplay", 1 }, - { reinit, "reinit", 0 }, - { exit_call, "exit", 0 }, - { setsmartprint, "setsmartprint", 1 }, - { NULL, NULL, -1 } + {deriv, "deriv", 1,}, + {derivn, "derivn", 2}, + {integ, "int", 1}, + {printvars, "printvars", 0}, + {help, "help", 0}, + {setdisplay, "setdisplay", 1}, + {reinit, "reinit", 0}, + {exit_call, "exit", 0}, + {setsmartprint, "setsmartprint", 1}, + {NULL, NULL, -1} }; - -/* On cherche simplement la routine a appeler parmi la table des fonctions */ +/* + * On cherche simplement la routine a appeler parmi la table des fonctions + */ void appel_fonction(char *nom, int arite, polynome p1, polynome p2, polynome p3) { - int i=0; - int trouve = 0; - - while ((func_table[i].nom)&&(!trouve)) { - if(!strcmp(func_table[i].nom, nom)) - trouve = 1; + int i = 0; + int trouve = 0; + + while ((func_table[i].nom) && (!trouve)) { + if (!strcmp(func_table[i].nom, nom)) + trouve = 1; i++; - } - if (trouve) { - if (func_table[i-1].arite==arite) { - (*func_table[i-1].func)(p1, p2, p3); - } else { - exception(1,_("appel_fonction: incorrect arg number")); - } + } + if (trouve) { + if (func_table[i - 1].arite == arite) { + (*func_table[i - 1].func) (p1, p2, p3); } else { - exception(1,_("appel_fonction: non-existent function")); + exception(1, _("appel_fonction: incorrect arg number")); } + } else { + exception(1, _("appel_fonction: non-existent function")); + } } - -/* Fonction de dérivation - rajoute le résultat sur la pile. */ +/* + * Fonction de derivation - rajoute le resultat sur la pile. + */ void deriv(polynome p1, polynome p2, polynome p3) { - polynome resultat = NULL, temp = NULL, t; - - while (p1) { - if (p1->degre) { - t=ply_constr(rat_constr((p1->coef.num)*(p1->degre), p1->coef.denom), (p1->degre-1)); - if (t) { - if (resultat) { - temp->suiv = t; - temp = t; - } else { - resultat = t; - temp = t; - } - } + polynome resultat = NULL, temp = NULL, t; + + while (p1) { + if (p1->degre) { + t = + ply_constr(rat_constr((p1->coef.num) * (p1->degre), p1->coef.denom), + (p1->degre - 1)); + if (t) { + if (resultat) { + temp->suiv = t; + temp = t; + } else { + resultat = t; + temp = t; } - p1=p1->suiv; + } } - push_pile_poly(resultat); + p1 = p1->suiv; + } + push_pile_poly(resultat); } - -/* Fonction paresseuse */ +/* + * Fonction paresseuse + */ void derivn(polynome p1, polynome p2, polynome p3) -{ pile_elem temp; - int i; - - if (p1) { - if ((p1->degre==0) && (p1->coef.num>0) && (p1->coef.denom==1)) { - push_pile_poly(NULL); - for(i=0; icoef.num; i++) { - temp = pop_pile(1); - ply_destruct(temp.poly); - deriv(temp.poly, NULL, NULL); - } - } else { - exception(1,_("derivn: invalid 2nd arg")); - } +{ + pile_elem temp; + int i; + + if (p1) { + if ((p1->degre == 0) && (p1->coef.num > 0) && (p1->coef.denom == 1)) { + push_pile_poly(NULL); + for (i = 0; i < p1->coef.num; i++) { + temp = pop_pile(1); + ply_destruct(temp.poly); + deriv(temp.poly, NULL, NULL); + } } else { - exception(1,_("derivn: invalid 2nd arg")); - } + exception(1, _("derivn: invalid 2nd arg")); + } + } else { + exception(1, _("derivn: invalid 2nd arg")); + } } - -/* Intégration d'un polynome */ +/* + * Integration d'un polynome + */ void integ(polynome p1, polynome p2, polynome p3) { - polynome resultat = NULL, temp = NULL, t; - - - while (p1) { - t=ply_constr(rat_constr((p1->coef.num), (p1->coef.denom)*(p1->degre+1)), (p1->degre+1)); - if (t) { - if (resultat) { - temp->suiv = t; - temp = t; - } else { - resultat = t; - temp = t; - } - } - p1=p1->suiv; + polynome resultat = NULL, temp = NULL, t; + + while (p1) { + t = + ply_constr(rat_constr((p1->coef.num), (p1->coef.denom) * (p1->degre + 1)), + (p1->degre + 1)); + if (t) { + if (resultat) { + temp->suiv = t; + temp = t; + } else { + resultat = t; + temp = t; + } } - push_pile_poly(resultat); + p1 = p1->suiv; + } + push_pile_poly(resultat); } -/* Quelques fonctions explicites... */ +/* + * Quelques fonctions explicites... + */ void printvars(polynome p1, polynome p2, polynome p3) { - AfficheTableau(variables); + AfficheTableau(variables); } void help(polynome p1, polynome p2, polynome p3) { - printf(_("Available functions:\n" - ". deriv(p); first derivative of p\n" - ". derivn(p, n); nth derivative of p\n" - ". integ(p); primitive of p\n" - ". printvars(); print all variables\n" - ". help(); this help message\n" - ". setdisplay(n); set integer display format\n" - "\tn=1: DECIMAL, n=2: HEXA\n" - "\tn=3: OCTAL, n=4: FLOAT\n" - ". smartprint(bool); toggle smart print of polynoms\n" - ". reinit(); clear all variables\n" - ". exit(); end program\n")); + printf(_("Available functions:\n" + ". deriv(p); first derivative of p\n" + ". derivn(p, n); nth derivative of p\n" + ". integ(p); primitive of p\n" + ". printvars(); print all variables\n" + ". help(); this help message\n" + ". setdisplay(n); set integer display format\n" + "\tn=1: DECIMAL, n=2: HEXA\n" + "\tn=3: OCTAL, n=4: FLOAT\n" + ". smartprint(bool); toggle smart print of polynoms\n" + ". reinit(); clear all variables\n" ". exit(); end program\n")); } void setdisplay(polynome p1, polynome p2, polynome p3) { - if (p1) { - if ((!p1->degre)) { - switch (p1->coef.num) { - case 1: - display=DEC; - break; - case 2: - display=HEX; - break; - case 3: - display=OCT; - break; - case 4: - display=FLT; - break; - default: - exception(1, _("setdisplay: invalid arg")); - } - } else { - exception(1, _("setdisplay: invalid arg")); - } - } else { + if (p1) { + if ((!p1->degre)) { + switch (p1->coef.num) { + case 1: + display = DEC; + break; + case 2: + display = HEX; + break; + case 3: + display = OCT; + break; + case 4: + display = FLT; + break; + default: exception(1, _("setdisplay: invalid arg")); - } + } + } else { + exception(1, _("setdisplay: invalid arg")); + } + } else { + exception(1, _("setdisplay: invalid arg")); + } } void reinit(polynome p1, polynome p2, polynome p3) { - DetruitTab(&variables); - Initialise(&variables); + DetruitTab(&variables); + Initialise(&variables); } void exit_call(polynome p1, polynome p2, polynome p3) { - quit = 1; + quit = 1; } -void setsmartprint(polynome p1, polynome p2, polynome p3) { - if (p1) { - if ((!p1->degre)) { - smartprint = p1->coef.num; - } else { - exception(1, _("setsmartprint: invalid arg")); - } +void setsmartprint(polynome p1, polynome p2, polynome p3) +{ + if (p1) { + if ((!p1->degre)) { + smartprint = p1->coef.num; } else { - smartprint = 0; - } + exception(1, _("setsmartprint: invalid arg")); + } + } else { + smartprint = 0; + } } diff --git a/lib/hash.c b/lib/hash.c index 5009611..70459e1 100644 --- a/lib/hash.c +++ b/lib/hash.c @@ -16,177 +16,180 @@ #define _(x) x #endif -/* L'ensemble de ce code source nous sert à implémenter une table de hachage qui nous sera utile - pour le stockage des variables du programme. C'est juste histoire d'aller un peu plus vite qu'une - simple liste chaînée. */ +/* + * L'ensemble de ce code source nous sert a implementer une table de hachage qui nous sera utile + * pour le stockage des variables du programme. C'est juste histoire d'aller un peu plus vite + * qu'une simple liste chainee. + */ static char *CHAINEHACHAGE = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"; static int FonctionHachage(char *clef) { - unsigned int i; + unsigned int i; - if (!clef) { - exception(2, _("Internal error into hashing")); - } + if (!clef) { + exception(2, _("Internal error into hashing")); + } - for (i = 0; i < strlen(CHAINEHACHAGE); i++) { - if (clef[0] == CHAINEHACHAGE[i]) { - return (i); - } + for (i = 0; i < strlen(CHAINEHACHAGE); i++) { + if (clef[0] == CHAINEHACHAGE[i]) { + return (i); } - return strlen(CHAINEHACHAGE); + } + return strlen(CHAINEHACHAGE); } _Element CreerElement(char *Nom, _TypeVariable Var) { - _Element e; + _Element e; - e.NomVar = Estrdup(Nom); + e.NomVar = Estrdup(Nom); - e.Variable = Var; - return (e); + e.Variable = Var; + return (e); } static _ListeChaine InserTete(_ListeChaine l, _Element e) { - _ListeChaine aux; - unsigned int i; - - aux = (_ListeChaine) Emalloc(sizeof(struct _LstChn)); - aux->Elem.NomVar = (char *) Emalloc(sizeof(char) * (strlen(e.NomVar) + 1)); - - for (i = 0; i <= strlen(e.NomVar); i++) { - aux->Elem.NomVar[i] = e.NomVar[i]; - } - aux->Elem.Variable = e.Variable; - aux->Suivant = l; - return (aux); + _ListeChaine aux; + unsigned int i; + + aux = (_ListeChaine) Emalloc(sizeof(struct _LstChn)); + aux->Elem.NomVar = (char *) Emalloc(sizeof(char) * (strlen(e.NomVar) + 1)); + + for (i = 0; i <= strlen(e.NomVar); i++) { + aux->Elem.NomVar[i] = e.NomVar[i]; + } + aux->Elem.Variable = e.Variable; + aux->Suivant = l; + return (aux); } static int EgaliteChaine(char *ch1, char *ch2) { - unsigned int i; - - if (strlen(ch1) != strlen(ch2)) { - return (0); - } - for (i = 0; i < strlen(ch1); i++) { - if (ch1[i] != ch2[i]) { - return (0); - } + unsigned int i; + + if (strlen(ch1) != strlen(ch2)) { + return (0); + } + for (i = 0; i < strlen(ch1); i++) { + if (ch1[i] != ch2[i]) { + return (0); } - return (1); - /* return (1-strcmp(ch1,ch2)); */ + } + return (1); + /* + * return (1-strcmp(ch1,ch2)); + */ } static void Supprimer(_ListeChaine * l, char *Nom) { - _ListeChaine l_aux = NULL; - - if ((*l) != NULL) { - if (EgaliteChaine((*l)->Elem.NomVar, Nom)) { - l_aux = *l; - *l = (*l)->Suivant; - free(l_aux->Elem.NomVar); - free(l_aux); - } else { - Supprimer(&((*l)->Suivant), Nom); - } + _ListeChaine l_aux = NULL; + + if ((*l) != NULL) { + if (EgaliteChaine((*l)->Elem.NomVar, Nom)) { + l_aux = *l; + *l = (*l)->Suivant; + free(l_aux->Elem.NomVar); + free(l_aux); + } else { + Supprimer(&((*l)->Suivant), Nom); } + } } static void Detruit(_ListeChaine * l) { - _ListeChaine l_aux = NULL; - - while (*l) { - l_aux = (*l)->Suivant; - free((*l)->Elem.NomVar); - free(*l); - *l = l_aux; - } + _ListeChaine l_aux = NULL; + + while (*l) { + l_aux = (*l)->Suivant; + free((*l)->Elem.NomVar); + free(*l); + *l = l_aux; + } } char SupprimerDansTab(_TableauVariable * t, char *Nom) { - int index = FonctionHachage(Nom); - - if (0 <= index && index <= strlen(CHAINEHACHAGE)) { - Supprimer(&((*t)[index]), Nom); - } else { - return (0); - } - return (1); + int index = FonctionHachage(Nom); + + if (0 <= index && index <= strlen(CHAINEHACHAGE)) { + Supprimer(&((*t)[index]), Nom); + } else { + return (0); + } + return (1); } char InsererVarDansTab(_TableauVariable * t, _Element e) { - int index = FonctionHachage(e.NomVar); - - if (0 <= index && index <= strlen(CHAINEHACHAGE)) { - (*t)[index] = InserTete((*t)[index], e); - } else { - return (0); - } - return (1); + int index = FonctionHachage(e.NomVar); + + if (0 <= index && index <= strlen(CHAINEHACHAGE)) { + (*t)[index] = InserTete((*t)[index], e); + } else { + return (0); + } + return (1); } static _TypeVariable NomVarToVarListe(char *Nom, _ListeChaine l, char *trouve) { - *trouve = 0; - while (l != NULL) { - if (EgaliteChaine(Nom, (l->Elem).NomVar)) { - *trouve = 1; - return (l->Elem.Variable); - } - l = l->Suivant; + *trouve = 0; + while (l != NULL) { + if (EgaliteChaine(Nom, (l->Elem).NomVar)) { + *trouve = 1; + return (l->Elem.Variable); } - return (NULL); + l = l->Suivant; + } + return (NULL); } _TypeVariable NomVarToVar(char *Nom, _TableauVariable t, char *trouve) { - return (NomVarToVarListe(Nom, t[FonctionHachage(Nom)], trouve)); + return (NomVarToVarListe(Nom, t[FonctionHachage(Nom)], trouve)); } void AfficheListe(_ListeChaine l) { - while (l != NULL) { - fprintf(stderr, "%s: %s\n", l->Elem.NomVar, ply_affichage(l->Elem.Variable)); - l = l->Suivant; - } + while (l != NULL) { + fprintf(stderr, "%s: %s\n", l->Elem.NomVar, ply_affichage(l->Elem.Variable)); + l = l->Suivant; + } } void AfficheTableau(_TableauVariable t) { - unsigned int i; + unsigned int i; - for (i = 0; i < TAILLECHAINEHACHAGE; i++) { - AfficheListe(t[i]); - } + for (i = 0; i < TAILLECHAINEHACHAGE; i++) { + AfficheListe(t[i]); + } } int Initialise(_TableauVariable * t) { - unsigned int i; - + unsigned int i; - (*t) = (_TableauVariable) Emalloc(sizeof(_ListeChaine) * (strlen(CHAINEHACHAGE) + 1)); - for (i = 0; i <= strlen(CHAINEHACHAGE); i++) { - (*t)[i] = NULL; - } - return (i); + (*t) = (_TableauVariable) Emalloc(sizeof(_ListeChaine) * (strlen(CHAINEHACHAGE) + 1)); + for (i = 0; i <= strlen(CHAINEHACHAGE); i++) { + (*t)[i] = NULL; + } + return (i); } void DetruitTab(_TableauVariable * t) { - int i; + int i; - for (i = 0; i <= strlen(CHAINEHACHAGE); i++) { - Detruit(&((*t)[i])); - } + for (i = 0; i <= strlen(CHAINEHACHAGE); i++) { + Detruit(&((*t)[i])); + } - free(*t); - *t = NULL; + free(*t); + *t = NULL; } diff --git a/lib/interface.c b/lib/interface.c index 3a3c6fc..0ba058d 100644 --- a/lib/interface.c +++ b/lib/interface.c @@ -10,163 +10,198 @@ int quit; -char * ancien = NULL; +char *ancien = NULL; -/* Ces deux fonctions nous servent à insérer ou supprimer un caractère dans une chaîne. Utile pour l'utilisation des -touches backspace, suppr et insert */ +/* + * Ces deux fonctions nous servent a inserer ou supprimer un caractere dans une chaine. Utile pour + * l'utilisation des touches backspace, suppr et insert + */ -void supprime(char * ch) { - for (; *ch; ch++) { - *ch = *(ch + 1); - } +void supprime(char *ch) +{ + for (; *ch; ch++) { + *ch = *(ch + 1); + } } -void inserer(char * ch, char c) { - int i; - - ch[strlen(ch) + 1] = '\0'; - - for (i = strlen(ch) - 1; i != -1; i--) { - *(ch + i + 1) = *(ch + i); - } - *ch = c; +void inserer(char *ch, char c) +{ + int i; + + ch[strlen(ch) + 1] = '\0'; + + for (i = strlen(ch) - 1; i != -1; i--) { + *(ch + i + 1) = *(ch + i); + } + *ch = c; } -/* La boucle de l'interface. Même si elle a l'air longue, elle ne fait que tester les différents cas -en fonction des touches enfoncées. */ +/* + * La boucle de l'interface. Meme si elle a l'air longue, elle ne fait que tester les differents + * cas en fonction des touches enfoncees. + */ + +void ifloop(void) +{ + int cread, i, insert = 0; + int gotesc = 0; + char buffer[BUFSIZ] = ""; + int position = 0; + + printf("> "); -void ifloop(void) { - int cread, i, insert = 0; - int gotesc = 0; - char buffer[BUFSIZ] = ""; - int position = 0; - - printf("> "); + quit = 0; + while (!quit) { + cread = fgetc(input); - quit = 0; - while (!quit) { + if (gotesc) { + switch (cread) { + case 91: cread = fgetc(input); - - if (gotesc) { - switch (cread) { - case 91: - cread = fgetc(input); - switch (cread) { - case 49: /* Home */ - position = 0; - break; - case 50: /* Insert */ - insert ^= 1; - break; - case 52: /* End */ - printf("%s", &(buffer[position])); - position = strlen(buffer); - break; - case 67: /* Droite */ - if (buffer[position]) { - printf("%c", buffer[position++]); - } - break; - case 68: /* Gauche */ - if (position) { - printf("\010"); - position--; - } - break; - default: - gotesc = 0; - break; - } - break; - case 79: - cread = fgetc(input); - switch (cread) { - case 82: /* F3 */ - for (i = 0; i < position; i++) { - printf("\010"); - } - strcpy(buffer, ancien); - position = strlen(buffer); - printf("%s", buffer); - break; - default: - gotesc = 0; - break; + switch (cread) { + case 49: /* + * Home + */ + position = 0; + break; + case 50: /* + * Insert + */ + insert ^= 1; + break; + case 52: /* + * End + */ + printf("%s", &(buffer[position])); + position = strlen(buffer); + break; + case 67: /* + * Droite + */ + if (buffer[position]) { + printf("%c", buffer[position++]); + } + break; + case 68: /* + * Gauche + */ + if (position) { + printf("\010"); + position--; + } + break; + default: + gotesc = 0; + break; + } + break; + case 79: + cread = fgetc(input); + switch (cread) { + case 82: /* + * F3 + */ + for (i = 0; i < position; i++) { + printf("\010"); + } + strcpy(buffer, ancien); + position = strlen(buffer); + printf("%s", buffer); + break; + default: + gotesc = 0; + break; - } - break; - default: - gotesc = 0; - break; - } - if (gotesc) { - cread = fgetc(input); - gotesc = 0; - } } - switch(cread) { - case 3: /* CTRL-C */ - printf(_("*CTRL-C*\n")); - quit = 1; - break; - case 8: /* backspace */ - if (position) { - supprime(&(buffer[--position])); - printf("\010%s ", &(buffer[position])); - for (i = 0; i <= strlen(&(buffer[position])); i++) { - printf("\010"); - } - } - break; - case 10: /* Entrée */ - printf("\n"); - clearterm(); - if (ancien) free(ancien); - ancien = Estrdup(buffer); - parse_line(buffer); - if (quit) break; - initterm(); - position = 0; - buffer[0] = 0; - printf("\n"); - while (has_resultat()) { - printf(" . %s\n", pop_resultat()); - } - printf("\n> "); - global_error = 0; - break; - case 27: /* ESC */ - gotesc = 1; - break; - case 126: /* ~ */ - break; - case 127: /* Suppr */ - if (buffer[position]) { - supprime(&(buffer[position])); - } - printf("%s ", &(buffer[position])); - for (i = 0; i <= strlen(&(buffer[position])); i++) { - printf("\010"); - } - break; - default: /* Tout caractère autre, on le rajoute à la position en cours */ - if (insert) { - inserer(&(buffer[position]), cread); - printf("%s", &(buffer[position])); - position++; - for (i = 0; i < strlen(&(buffer[position])); i++) { - printf("\010"); - } - } else { - printf("%c", cread); - if (buffer[position]) { - buffer[position++] = cread; - } else { - buffer[position++] = cread; - buffer[position] = 0; - } - } - break; + break; + default: + gotesc = 0; + break; + } + if (gotesc) { + cread = fgetc(input); + gotesc = 0; + } + } + switch (cread) { + case 3: /* + * CTRL-C + */ + printf(_("*CTRL-C*\n")); + quit = 1; + break; + case 8: /* + * backspace + */ + if (position) { + supprime(&(buffer[--position])); + printf("\010%s ", &(buffer[position])); + for (i = 0; i <= strlen(&(buffer[position])); i++) { + printf("\010"); + } + } + break; + case 10: /* + * Entree + */ + printf("\n"); + clearterm(); + if (ancien) + free(ancien); + ancien = Estrdup(buffer); + parse_line(buffer); + if (quit) + break; + initterm(); + position = 0; + buffer[0] = 0; + printf("\n"); + while (has_resultat()) { + printf(" . %s\n", pop_resultat()); + } + printf("\n> "); + global_error = 0; + break; + case 27: /* + * ESC + */ + gotesc = 1; + break; + case 126: /* + * ~ + */ + break; + case 127: /* + * Suppr + */ + if (buffer[position]) { + supprime(&(buffer[position])); + } + printf("%s ", &(buffer[position])); + for (i = 0; i <= strlen(&(buffer[position])); i++) { + printf("\010"); + } + break; + default: /* + * Tout caractere autre, on le rajoute a la position en cours + */ + if (insert) { + inserer(&(buffer[position]), cread); + printf("%s", &(buffer[position])); + position++; + for (i = 0; i < strlen(&(buffer[position])); i++) { + printf("\010"); + } + } else { + printf("%c", cread); + if (buffer[position]) { + buffer[position++] = cread; + } else { + buffer[position++] = cread; + buffer[position] = 0; } + } + break; } + } } diff --git a/lib/numbers.c b/lib/numbers.c index 9c2814f..9dd306c 100644 --- a/lib/numbers.c +++ b/lib/numbers.c @@ -4,95 +4,97 @@ * */ - #include "numbers.h" #include "scalaires.h" -/* Cette fonction lit un nombre. Elle va chercher absolument à traduire la chaîne passée en argument en un nombre. Si -ce nombre n'est pas valide, alors l'int valid est mis à faux. Cette fonction reconnait les nombres en décimaux, les nombres -en octal préfixés avec 0 et les nombres en hexadécimal préfixés avec 0x. -*/ +/* + * Cette fonction lit un nombre. Elle va chercher absolument a traduire la chaine passee en + * argument en un nombre. Si ce nombre n'est pas valide, alors l'int valid est mis a faux. Cette + * fonction reconnait les nombres en decimaux, les nombres en octal prefixes avec 0 et les nombres + * en hexadecimal prefixes avec 0x. + */ int char_to_number(char *st, int *valid) { - int whattype = 0, result = 0; + int whattype = 0, result = 0; - *valid = 0; + *valid = 0; - if (*st == '0') { - st++; - if (*st == 'x') { - whattype = 1; - st++; - } else if (*st) { - whattype = 2; - } else { - *valid = 1; - return 0; - } + if (*st == '0') { + st++; + if (*st == 'x') { + whattype = 1; + st++; + } else if (*st) { + whattype = 2; + } else { + *valid = 1; + return 0; } + } - while (*st) { - switch (whattype) { - case 0: - if ((*st < '0') || (*st > '9')) { - return 0; - } - result *= 10; - result += *st - '0'; - break; - case 1: - if (((*st < '0') || (*st > '9')) - && ((*st < 'A') || (*st > 'F')) - && ((*st < 'a') || (*st > 'f'))) { - return 0; - } - result *= 16; - if ((*st >= '0') && (*st <= '9')) { - result += *st - '0'; - } else if ((*st >= 'A') && (*st <= 'F')) { - result += *st - 'A' + 10; - } else { - result += *st - 'a' + 10; - } - break; - case 2: - if ((*st < '0') || (*st > '7')) { - return 0; - } - result *= 8; - result += *st - '0'; - break; - } - st++; + while (*st) { + switch (whattype) { + case 0: + if ((*st < '0') || (*st > '9')) { + return 0; + } + result *= 10; + result += *st - '0'; + break; + case 1: + if (((*st < '0') || (*st > '9')) + && ((*st < 'A') || (*st > 'F')) + && ((*st < 'a') || (*st > 'f'))) { + return 0; + } + result *= 16; + if ((*st >= '0') && (*st <= '9')) { + result += *st - '0'; + } else if ((*st >= 'A') && (*st <= 'F')) { + result += *st - 'A' + 10; + } else { + result += *st - 'a' + 10; + } + break; + case 2: + if ((*st < '0') || (*st > '7')) { + return 0; + } + result *= 8; + result += *st - '0'; + break; } + st++; + } - *valid = 1; - return result; + *valid = 1; + return result; } - rationnel char_to_rat(char *st, int *valid) -{ /* cette fonction tente de traduire une chaine en rationnel */ - int dotnum = 0, deci = 1, temp = 0; +{ /* + * cette fonction tente de traduire une chaine en rationnel + */ + int dotnum = 0, deci = 1, temp = 0; - while (*st) { - if (*st == '.') { - dotnum++; - } else { - if ((*st < '0') || (*st > '9') || (dotnum > 1)) { - *valid = 0; - return rat_constr_zero(); - } else { - temp *= 10; - temp += *st - '0'; - if (dotnum == 1) - deci *= 10; - } - } - st++; + while (*st) { + if (*st == '.') { + dotnum++; + } else { + if ((*st < '0') || (*st > '9') || (dotnum > 1)) { + *valid = 0; + return rat_constr_zero(); + } else { + temp *= 10; + temp += *st - '0'; + if (dotnum == 1) + deci *= 10; + } } + st++; + } - *valid = 1; - return rat_constr(temp, deci); + *valid = 1; + return rat_constr(temp, deci); } diff --git a/lib/parser.c b/lib/parser.c index 74a7b71..caa1cc4 100644 --- a/lib/parser.c +++ b/lib/parser.c @@ -15,12 +15,11 @@ #include "parser.h" #include "pile.h" - typedef unsigned char op_t; typedef struct operator_t { - op_t op; - int pri, func; + op_t op; + int pri, func; } operator_t; static op_t pile_operators[PILEOP_MAX]; @@ -28,251 +27,274 @@ static int pile_nestedcall[PILECALL_MAX]; static int pileop_pos = 0, pilecall_pos = 0; -/* La liste des opérateurs reconnus par le parser */ +/* + * La liste des operateurs reconnus par le parser + */ static operator_t operators[] = { - {',', 0, OP_NEST}, - {'+', 2, OP_PLUS}, - {'-', 2, OP_MOINS}, - {'*', 3, OP_MUL}, - {'/', 3, OP_DIV}, - {'%', 3, OP_MOD}, - {'^', 4, OP_EXP}, - {'+' + 128, 5, OP_PLUS_UNARY}, - {'-' + 128, 5, OP_MOINS_UNARY}, - {'=', 1, OP_ASSIGN}, - {'(', 6, OP_FUNC_CALL}, - {'(' + 128, 6, OP_LPAREN}, - {255, -1, -1} + {',', 0, OP_NEST}, + {'+', 2, OP_PLUS}, + {'-', 2, OP_MOINS}, + {'*', 3, OP_MUL}, + {'/', 3, OP_DIV}, + {'%', 3, OP_MOD}, + {'^', 4, OP_EXP}, + {'+' + 128, 5, OP_PLUS_UNARY}, + {'-' + 128, 5, OP_MOINS_UNARY}, + {'=', 1, OP_ASSIGN}, + {'(', 6, OP_FUNC_CALL}, + {'(' + 128, 6, OP_LPAREN}, + {255, -1, -1} }; -/* Fonction interne: convertit un operateur en sa structure */ +/* + * Fonction interne: convertit un operateur en sa structure + */ static operator_t get_op(op_t op) { - int i; + int i; - for (i = 0; operators[i].op != 255; i++) { - if (operators[i].op == op) - return operators[i]; - } + for (i = 0; operators[i].op != 255; i++) { + if (operators[i].op == op) + return operators[i]; + } - return operators[i]; + return operators[i]; } -/* Fonctions internes de lectures sur la structure */ +/* + * Fonctions internes de lectures sur la structure + */ static int get_pri(op_t op) { - return get_op(op).pri; + return get_op(op).pri; } static int get_func(op_t op) { - return get_op(op).func; + return get_op(op).func; } -/* Focntions internes d'empilement / dépilement */ +/* + * Focntions internes d'empilement / depilement + */ static op_t get_last_op(void) { - if (pileop_pos) - return pile_operators[pileop_pos - 1]; - else - return -1; + if (pileop_pos) + return pile_operators[pileop_pos - 1]; + else + return -1; } static op_t pop_op(void) { - if (pileop_pos) - return pile_operators[--pileop_pos]; - return -1; + if (pileop_pos) + return pile_operators[--pileop_pos]; + return -1; } static void push_op(op_t op) { - if (pileop_pos != PILEOP_MAX) - pile_operators[pileop_pos++] = op; - else - exception(1, _("Too many nested operators in expression.\n")); + if (pileop_pos != PILEOP_MAX) + pile_operators[pileop_pos++] = op; + else + exception(1, _("Too many nested operators in expression.\n")); } static int pop_call(void) { - if (pilecall_pos) - return pile_nestedcall[--pilecall_pos]; - return -1; + if (pilecall_pos) + return pile_nestedcall[--pilecall_pos]; + return -1; } static void increment_call(void) { - if (pilecall_pos) { - if (pile_nestedcall[pilecall_pos - 1] != -1) - pile_nestedcall[pilecall_pos - 1]++; - } + if (pilecall_pos) { + if (pile_nestedcall[pilecall_pos - 1] != -1) + pile_nestedcall[pilecall_pos - 1]++; + } } static int get_last_call(void) { - if (pilecall_pos) - return pile_nestedcall[pilecall_pos - 1]; - return -1; + if (pilecall_pos) + return pile_nestedcall[pilecall_pos - 1]; + return -1; } static void push_call(int call) { - if (pilecall_pos != PILECALL_MAX) - pile_nestedcall[pilecall_pos++] = call; - else - exception(1, _("Too many nested functions calls in expression.\n")); + if (pilecall_pos != PILECALL_MAX) + pile_nestedcall[pilecall_pos++] = call; + else + exception(1, _("Too many nested functions calls in expression.\n")); } -/* Cette fonction lit un "mot" sur la chaine line et renvoit le nouveau pointeur */ +/* + * Cette fonction lit un "mot" sur la chaine line et renvoit le nouveau pointeur + */ static char *getword(char *line, char *p) { - char o = 0, *d = line, instring = 0, gotbslash = 0; + char o = 0, *d = line, instring = 0, gotbslash = 0; - do { - if (instring) { - o = *(p++) = *line; - if (!gotbslash) { - switch (instring) { - case 1: - if (*line == '\'') { - instring = 0; - } - break; - case 2: - if (*line == '"') { - instring = 0; - } - break; - } - if (*line == '\\') - gotbslash = 1; - } else { - gotbslash = 0; - } - } else { - if (*(line) == '\'') { - o = *(p++) = *line; - instring = 1; - } else if (*(line) == '"') { - o = *(p++) = *line; - instring = 2; - } else { - if (*(line) != ' ' && *(line) != '\t') { - o = *(p++) = *line; - } else if (d != line) { - *p = '\0'; - return line; - } - } + do { + if (instring) { + o = *(p++) = *line; + if (!gotbslash) { + switch (instring) { + case 1: + if (*line == '\'') { + instring = 0; + } + break; + case 2: + if (*line == '"') { + instring = 0; + } + break; } - line++; + if (*line == '\\') + gotbslash = 1; + } else { + gotbslash = 0; + } + } else { + if (*(line) == '\'') { + o = *(p++) = *line; + instring = 1; + } else if (*(line) == '"') { + o = *(p++) = *line; + instring = 2; + } else { + if (*(line) != ' ' && *(line) != '\t') { + o = *(p++) = *line; + } else if (d != line) { + *p = '\0'; + return line; + } + } } - while (((*line) && (*line != ')') && (*line != ';') && (get_func(*line) == -1) - && (get_func(o) == -1)) || (instring)); - *p = '\0'; - return line; + line++; + } + while (((*line) && (*line != ')') && (*line != ';') && (get_func(*line) == -1) + && (get_func(o) == -1)) || (instring)); + *p = '\0'; + return line; } -/* Cette fonction va parcourir une chaine afin d'appeler les fonction push_pile() et act_pile() */ +/* + * Cette fonction va parcourir une chaine afin d'appeler les fonction push_pile() et act_pile() + */ void parse_line(char *line) { - char buffer[BUFSIZ], imm[BUFSIZ], *d = line; - op_t op; - static int got_unary = 128, nbrargs; + char buffer[BUFSIZ], imm[BUFSIZ], *d = line; + op_t op; + static int got_unary = 128, nbrargs; - sprintf(buffer, "Read line '%s'", line); - pushcontext(buffer); + sprintf(buffer, "Read line '%s'", line); + pushcontext(buffer); - while ((*line) && !(global_error)) { - line = getword(line, buffer); - sprintf(imm, "Analysing word '%s' at position %i", buffer, line - d); - pushcontext(imm); - if (get_func(buffer[0]) != -1) { - /* Le mot lu est un operateur, on agit sur la pile */ - buffer[0] += got_unary; - if (got_unary) { - } - if (get_pri(buffer[0]) == -1) { - if (got_unary) { - exception(1, _("Invalid unary operator")); - } else { - exception(1, _("Invalid binary operator")); - } - } - while (((get_pri(get_last_op()) >= get_pri(buffer[0])) - && ((get_last_op() & 127) != '(')) && !global_error) { - act_pile(get_func(pop_op())); - got_unary = 0; - } - if (buffer[0] == '(') { - push_call(0); - } - if (buffer[0] == ',') { - increment_call(); - } else - push_op(buffer[0]); - got_unary = 128; - } else if ((buffer[0] == ';') || (buffer[0] == ')')) { - /* Le mot lut est un opérateur spécial, on vide la pile */ - switch (buffer[0]) { - case ';': - /* Equivalent a fin de ligne */ - while ((pileop_pos) && !global_error) { - op = pop_op(); - if (op == '(') - exception(1, _("Parse error: too much left parenthesis")); - act_pile(get_func(op)); - } - got_unary = 128; - move_to_resultat_pile(); - break; - case ')': - /* Fin de parenthese (Appel de fonction ou expression mathématique) */ - while (!global_error) { - if (!pileop_pos) - exception(1, _("Parse error: too much right parenthesis")); - op = pop_op(); - if (((op & 127) == '(')) - break; - act_pile(get_func(op)); - } - if (global_error) break; - if (op == '(') { - nbrargs = pop_call(); - push_pile_int(nbrargs); - act_pile(get_func(op)); - } - got_unary = 0; - break; - } - } else if (((buffer[0] >= 'A') && (buffer[0] <= 'Z')) - || ((buffer[0] >= 'a') && (buffer[0] <= 'z')) - || ((buffer[0] >= '0') && (buffer[0] <= '9')) - || (buffer[0] == '_') || (buffer[0] == '"') - || (buffer[0] == '\'') || (buffer[0] == '.') - || (buffer[0] == '#') || (buffer[0] == '?')) { - /* Dans tous les autres cas, on a reçu un symbole, on le pose sur la pile */ - push_pile(buffer); - got_unary = 0; - if (!get_last_call()) - increment_call(); - } else if (buffer[0]) { - exception(1, _("Invalid character")); + while ((*line) && !(global_error)) { + line = getword(line, buffer); + sprintf(imm, "Analysing word '%s' at position %i", buffer, line - d); + pushcontext(imm); + if (get_func(buffer[0]) != -1) { + /* + * Le mot lu est un operateur, on agit sur la pile + */ + buffer[0] += got_unary; + if (got_unary) { + } + if (get_pri(buffer[0]) == -1) { + if (got_unary) { + exception(1, _("Invalid unary operator")); + } else { + exception(1, _("Invalid binary operator")); + } + } + while (((get_pri(get_last_op()) >= get_pri(buffer[0])) + && ((get_last_op() & 127) != '(')) && !global_error) { + act_pile(get_func(pop_op())); + got_unary = 0; + } + if (buffer[0] == '(') { + push_call(0); + } + if (buffer[0] == ',') { + increment_call(); + } else + push_op(buffer[0]); + got_unary = 128; + } else if ((buffer[0] == ';') || (buffer[0] == ')')) { + /* + * Le mot lut est un operateur special, on vide la pile + */ + switch (buffer[0]) { + case ';': + /* + * Equivalent a fin de ligne + */ + while ((pileop_pos) && !global_error) { + op = pop_op(); + if (op == '(') + exception(1, _("Parse error: too much left parenthesis")); + act_pile(get_func(op)); } - popcontext(); - } - popcontext(); - if (global_error) { got_unary = 128; - while (pileop_pos) { - pop_op(); + move_to_resultat_pile(); + break; + case ')': + /* + * Fin de parenthese (Appel de fonction ou expression mathematique) + */ + while (!global_error) { + if (!pileop_pos) + exception(1, _("Parse error: too much right parenthesis")); + op = pop_op(); + if (((op & 127) == '(')) + break; + act_pile(get_func(op)); } + if (global_error) + break; + if (op == '(') { + nbrargs = pop_call(); + push_pile_int(nbrargs); + act_pile(get_func(op)); + } + got_unary = 0; + break; + } + } else if (((buffer[0] >= 'A') && (buffer[0] <= 'Z')) + || ((buffer[0] >= 'a') && (buffer[0] <= 'z')) + || ((buffer[0] >= '0') && (buffer[0] <= '9')) + || (buffer[0] == '_') || (buffer[0] == '"') + || (buffer[0] == '\'') || (buffer[0] == '.') + || (buffer[0] == '#') || (buffer[0] == '?')) { + /* + * Dans tous les autres cas, on a reçu un symbole, on le pose sur la pile + */ + push_pile(buffer); + got_unary = 0; + if (!get_last_call()) + increment_call(); + } else if (buffer[0]) { + exception(1, _("Invalid character")); + } + popcontext(); + } + popcontext(); + if (global_error) { + got_unary = 128; + while (pileop_pos) { + pop_op(); } + } } diff --git a/lib/pile.c b/lib/pile.c index 2920139..6298b92 100644 --- a/lib/pile.c +++ b/lib/pile.c @@ -18,461 +18,498 @@ pile_elem result_pile[PILE_MAX]; unsigned int pile_ptr = 0; unsigned int result_pile_ptr = 0; -/* Fonctions basiques sur la pile d'opérandes. Elle seront appelées par le parser. */ +/* + * Fonctions basiques sur la pile d'operandes. Elle seront appelees par le parser. + */ -/* La fonction push_pile est générique. Le parser lui transmet une chaîne de caractères, et cette -fonction va tenter de la convertir en ce qui lui semble le mieux pour empiler */ +/* + * La fonction push_pile est generique. Le parser lui transmet une chaine de caracteres, et cette + * fonction va tenter de la convertir en ce qui lui semble le mieux pour empiler + */ void push_pile(char *st) { - int valid1, valid2, valid3; - char valid4 = 0; - int i_number; - rationnel r_number; - polynome poly; - char buf[128]; - - sprintf(buf, _("Calling push_pile(%s)"), st); - pushcontext(buf); - i_number = char_to_number(st, &valid1); - r_number = char_to_rat(st, &valid2); - valid3 = is_mute(st); - poly = (polynome) NomVarToVar(st, variables, &valid4); - if (valid1) { /* il s'agit d'un entier */ - pushcontext(_("it's an integer")); - push_pile_poly(ply_constr(rat_constr(i_number, 1), 0)); - popcontext(); - } else if (valid2) { /* il s'agit d'un flottant */ - pushcontext(_("it's a float")); - push_pile_poly(ply_constr(r_number, 0)); - popcontext(); - } else if (valid3) { /* il s'agit de x */ - pushcontext(_("it's X")); - push_pile_poly(ply_constr(rat_constr(1, 1), 1)); - popcontext(); - } else if (valid4) { /* il s'agit d'une variable */ - pushcontext(_("it's a variable")); - push_pile_poly(ply_copy(poly)); - popcontext(); - } else { /* il s'agit d'un nom */ - pushcontext(_("it's a name")); - if (*st == '\'') { - st++; - st[strlen(st) - 1] = 0; - } - push_pile_string(Estrdup(st)); - popcontext(); + int valid1, valid2, valid3; + char valid4 = 0; + int i_number; + rationnel r_number; + polynome poly; + char buf[128]; + + sprintf(buf, _("Calling push_pile(%s)"), st); + pushcontext(buf); + i_number = char_to_number(st, &valid1); + r_number = char_to_rat(st, &valid2); + valid3 = is_mute(st); + poly = (polynome) NomVarToVar(st, variables, &valid4); + if (valid1) { /* + * il s'agit d'un entier + */ + pushcontext(_("it's an integer")); + push_pile_poly(ply_constr(rat_constr(i_number, 1), 0)); + popcontext(); + } else if (valid2) { /* + * il s'agit d'un flottant + */ + pushcontext(_("it's a float")); + push_pile_poly(ply_constr(r_number, 0)); + popcontext(); + } else if (valid3) { /* + * il s'agit de x + */ + pushcontext(_("it's X")); + push_pile_poly(ply_constr(rat_constr(1, 1), 1)); + popcontext(); + } else if (valid4) { /* + * il s'agit d'une variable + */ + pushcontext(_("it's a variable")); + push_pile_poly(ply_copy(poly)); + popcontext(); + } else { /* + * il s'agit d'un nom + */ + pushcontext(_("it's a name")); + if (*st == '\'') { + st++; + st[strlen(st) - 1] = 0; } + push_pile_string(Estrdup(st)); popcontext(); + } + popcontext(); #ifdef DEBUG - fprintf(stderr, "exiting push_pile\n"); + fprintf(stderr, "exiting push_pile\n"); #endif } -/* Fonctions "atomiques" appelées par push_pile() */ +/* + * Fonctions "atomiques" appelees par push_pile() + */ void push_pile_poly(polynome poly) { - if (pile_ptr != PILE_MAX) { - pile[pile_ptr].type = T_POLY; - pile[pile_ptr].poly = poly; - pile_ptr++; - } else { - exception(2, _("push_pile_poly: Stack Overflow")); - } + if (pile_ptr != PILE_MAX) { + pile[pile_ptr].type = T_POLY; + pile[pile_ptr].poly = poly; + pile_ptr++; + } else { + exception(2, _("push_pile_poly: Stack Overflow")); + } } void push_pile_int(int val) { - if (pile_ptr != PILE_MAX) { - pile[pile_ptr].type = T_INT; - pile[pile_ptr].val = val; - pile_ptr++; - } else { - exception(2, _("push_pile_int: Stack Overflow")); - } - + if (pile_ptr != PILE_MAX) { + pile[pile_ptr].type = T_INT; + pile[pile_ptr].val = val; + pile_ptr++; + } else { + exception(2, _("push_pile_int: Stack Overflow")); + } } void push_pile_string(char *st) { - if (pile_ptr != PILE_MAX) { - pile[pile_ptr].type = T_STRING; - pile[pile_ptr].label = Estrdup(st); - pile_ptr++; - } else { - exception(2, _("push_pile_string: Stack Overflow")); - } - + if (pile_ptr != PILE_MAX) { + pile[pile_ptr].type = T_STRING; + pile[pile_ptr].label = Estrdup(st); + pile_ptr++; + } else { + exception(2, _("push_pile_string: Stack Overflow")); + } } -/* Dépilement */ +/* + * Depilement + */ pile_elem pop_pile(unsigned int count) { - char buf[50]; - - if (((int) (pile_ptr) - (int) count) >= 0) { - pile_ptr -= count; - } else { - sprintf(buf, _("pop_pile: Can't pop %u elements"), count); - exception(2, buf); - } - return pile[pile_ptr]; + char buf[50]; + + if (((int) (pile_ptr) - (int) count) >= 0) { + pile_ptr -= count; + } else { + sprintf(buf, _("pop_pile: Can't pop %u elements"), count); + exception(2, buf); + } + return pile[pile_ptr]; } -/* Vidage (appelée en cas d'erreur) */ +/* + * Vidage (appelee en cas d'erreur) + */ void flush_pile(void) { - int i; - - if (pile_ptr) { - for (i = 0; i <= pile_ptr - 1; i++) { - switch (pile[i].type) { - case T_INT: - break; - case T_STRING: - free(pile[i].label); - break; - case T_POLY: - ply_destruct(pile[i].poly); - break; - } - } + int i; + + if (pile_ptr) { + for (i = 0; i <= pile_ptr - 1; i++) { + switch (pile[i].type) { + case T_INT: + break; + case T_STRING: + free(pile[i].label); + break; + case T_POLY: + ply_destruct(pile[i].poly); + break; + } } - pile_ptr=0; + } + pile_ptr = 0; } - - -/* Fonctions basiques sur la pile de resultats. Grâce à cette pile résultat, nous pouvons passer un résultat -à l'interface, dès que le parser aura terminé son travail. */ +/* + * Fonctions basiques sur la pile de resultats. Grace a cette pile resultat, nous pouvons passer un + * resultat a l'interface, des que le parser aura termine son travail. + */ void move_to_resultat_pile(void) -{ pile_elem temp; - char valid; - polynome t; - - pushcontext(_("move_to_resultat_pile()")); - if (pile_ptr) { - temp=pop_pile(1); - if (result_pile_ptr!=PILE_MAX) { - if (temp.type==T_POLY) { - result_pile[result_pile_ptr]=temp; - result_pile_ptr++; - t = NomVarToVar("ANS", variables, &valid); - if (valid) { - ply_destruct(t); - SupprimerDansTab(&variables, "ANS"); - } - InsererVarDansTab(&variables, CreerElement("ANS", (void *) (ply_copy(temp.poly)))); - } else { - exception(1, _("move_to_resultat_pile: invalid argument type")); - } - } else { - exception(2, _("move_to_resultat_pile: Stack Overflow")); +{ + pile_elem temp; + char valid; + polynome t; + + pushcontext(_("move_to_resultat_pile()")); + if (pile_ptr) { + temp = pop_pile(1); + if (result_pile_ptr != PILE_MAX) { + if (temp.type == T_POLY) { + result_pile[result_pile_ptr] = temp; + result_pile_ptr++; + t = NomVarToVar("ANS", variables, &valid); + if (valid) { + ply_destruct(t); + SupprimerDansTab(&variables, "ANS"); } + InsererVarDansTab(&variables, CreerElement("ANS", (void *) (ply_copy(temp.poly)))); + } else { + exception(1, _("move_to_resultat_pile: invalid argument type")); + } + } else { + exception(2, _("move_to_resultat_pile: Stack Overflow")); } - popcontext(); + } + popcontext(); } -char * pop_resultat(void) -{ static char result[BUFSIZ]; - - pushcontext(_("pop_resultat()")); - if (result_pile_ptr) { - result_pile_ptr--; - sprintf(result,"%s", ply_affichage(result_pile[result_pile_ptr].poly)); - } else { - exception(2, _("move_to_resultat_pile: empty stack")); - } - popcontext(); - return result; +char *pop_resultat(void) +{ + static char result[BUFSIZ]; + + pushcontext(_("pop_resultat()")); + if (result_pile_ptr) { + result_pile_ptr--; + sprintf(result, "%s", ply_affichage(result_pile[result_pile_ptr].poly)); + } else { + exception(2, _("move_to_resultat_pile: empty stack")); + } + popcontext(); + return result; } polynome return_last(int *valid) { - if (!result_pile_ptr) { - *valid=0; - return NULL; - } else { - *valid=1; - return result_pile[result_pile_ptr--].poly; - } + if (!result_pile_ptr) { + *valid = 0; + return NULL; + } else { + *valid = 1; + return result_pile[result_pile_ptr--].poly; + } } - -int has_resultat(void) { - return (result_pile_ptr ? 1 : 0); + +int has_resultat(void) +{ + return (result_pile_ptr ? 1 : 0); } -/* Fonctions avancées sur la pile d'opérandes. Nous allons surtout manipuler les opérandes de la pile, à l'aide -des opérateurs que le parser nous enverra. */ +/* + * Fonctions avancees sur la pile d'operandes. Nous allons surtout manipuler les operandes de la + * pile, a l'aide des operateurs que le parser nous enverra. + */ char *affichage_level_1(void) { - char *result = NULL; - - if (pile_ptr) { - switch (pile[pile_ptr - 1].type) { - case T_POLY: - result = ply_affichage(pile[pile_ptr - 1].poly); - break; - case T_STRING: - result = pile[pile_ptr - 1].label; - break; - case T_INT: - result = (char *) Emalloc(11 * sizeof(char)); - sprintf(result, "%10d", pile[pile_ptr - 1].val); - break; - } + char *result = NULL; + + if (pile_ptr) { + switch (pile[pile_ptr - 1].type) { + case T_POLY: + result = ply_affichage(pile[pile_ptr - 1].poly); + break; + case T_STRING: + result = pile[pile_ptr - 1].label; + break; + case T_INT: + result = (char *) Emalloc(11 * sizeof(char)); + + sprintf(result, "%10d", pile[pile_ptr - 1].val); + break; } - return result; + } + return result; } int is_mute(char *st) { - return !(strcmp(st, mute)); + return !(strcmp(st, mute)); } -/* Cette fonction est appelée par le parser, et est donc un énorme switch qui va tester l'opérateur passé en paramètre */ +/* + * Cette fonction est appelee par le parser, et est donc un enorme switch qui va tester l'operateur + * passe en parametre + */ void act_pile(int func) { - pile_elem operande1, operande2; - pile_elem operande[3]; - char buf[50]; - char valid; - int i, err=0; - - sprintf(buf, _("Calling act_pile(%i)"), func); - pushcontext(buf); + pile_elem operande1, operande2; + pile_elem operande[3]; + char buf[50]; + char valid; + int i, err = 0; + + sprintf(buf, _("Calling act_pile(%i)"), func); + pushcontext(buf); #ifdef DEBUG - affichage_pile(); + affichage_pile(); #endif - switch (func) { - case OP_PLUS: + switch (func) { + case OP_PLUS: #ifdef DEBUG - printf("----- OP_PLUS\n"); + printf("----- OP_PLUS\n"); #endif - operande1 = pop_pile(1); - operande2 = pop_pile(1); - if ((operande1.type == T_POLY) && (operande2.type == T_POLY)) { - push_pile_poly(ply_addition(operande1.poly, operande2.poly)); - if (operande1.poly) - ply_destruct(operande1.poly); - if (operande2.poly) - ply_destruct(operande2.poly); - } else { - exception(1, _("act_pile: OP_PLUS invalid arguments")); - } - break; - case OP_MOINS: + operande1 = pop_pile(1); + operande2 = pop_pile(1); + if ((operande1.type == T_POLY) && (operande2.type == T_POLY)) { + push_pile_poly(ply_addition(operande1.poly, operande2.poly)); + if (operande1.poly) + ply_destruct(operande1.poly); + if (operande2.poly) + ply_destruct(operande2.poly); + } else { + exception(1, _("act_pile: OP_PLUS invalid arguments")); + } + break; + case OP_MOINS: #ifdef DEBUG - printf("----- OP_MOINS\n"); + printf("----- OP_MOINS\n"); #endif - operande2 = pop_pile(1); - operande1 = pop_pile(1); - if ((operande1.type == T_POLY) && (operande2.type == T_POLY)) { - push_pile_poly(ply_soustraction(operande1.poly, operande2.poly)); - if (operande1.poly) - ply_destruct(operande1.poly); - if (operande2.poly) - ply_destruct(operande2.poly); - } else { - exception(1, _("act_pile: OP_MOINS invalid arguments")); - } - break; - case OP_MUL: + operande2 = pop_pile(1); + operande1 = pop_pile(1); + if ((operande1.type == T_POLY) && (operande2.type == T_POLY)) { + push_pile_poly(ply_soustraction(operande1.poly, operande2.poly)); + if (operande1.poly) + ply_destruct(operande1.poly); + if (operande2.poly) + ply_destruct(operande2.poly); + } else { + exception(1, _("act_pile: OP_MOINS invalid arguments")); + } + break; + case OP_MUL: #ifdef DEBUG - printf("----- OP_MUL\n"); + printf("----- OP_MUL\n"); #endif - operande1 = pop_pile(1); - operande2 = pop_pile(1); - if ((operande1.type == T_POLY) && (operande2.type == T_POLY)) { - push_pile_poly(ply_multiplication(operande1.poly, operande2.poly)); - if (operande1.poly) - ply_destruct(operande1.poly); - if (operande2.poly) - ply_destruct(operande2.poly); - } else { - exception(1, _("act_pile: OP_MUL invalid arguments")); - } - break; - case OP_DIV: + operande1 = pop_pile(1); + operande2 = pop_pile(1); + if ((operande1.type == T_POLY) && (operande2.type == T_POLY)) { + push_pile_poly(ply_multiplication(operande1.poly, operande2.poly)); + if (operande1.poly) + ply_destruct(operande1.poly); + if (operande2.poly) + ply_destruct(operande2.poly); + } else { + exception(1, _("act_pile: OP_MUL invalid arguments")); + } + break; + case OP_DIV: #ifdef DEBUG - printf("----- OP_DIV\n"); + printf("----- OP_DIV\n"); #endif - operande2 = pop_pile(1); - operande1 = pop_pile(1); - if ((operande1.type == T_POLY) && (operande2.type == T_POLY)) { - push_pile_poly(ply_division(operande1.poly, operande2.poly)); - if (operande1.poly) - ply_destruct(operande1.poly); - if (operande2.poly) - ply_destruct(operande2.poly); - } else { - exception(1, _("act_pile: OP_DIV invalid arguments")); - } - break; - case OP_MOD: + operande2 = pop_pile(1); + operande1 = pop_pile(1); + if ((operande1.type == T_POLY) && (operande2.type == T_POLY)) { + push_pile_poly(ply_division(operande1.poly, operande2.poly)); + if (operande1.poly) + ply_destruct(operande1.poly); + if (operande2.poly) + ply_destruct(operande2.poly); + } else { + exception(1, _("act_pile: OP_DIV invalid arguments")); + } + break; + case OP_MOD: #ifdef DEBUG - printf("----- OP_MOD\n"); + printf("----- OP_MOD\n"); #endif - operande2 = pop_pile(1); - operande1 = pop_pile(1); - if ((operande1.type == T_POLY) && (operande2.type == T_POLY)) { - push_pile_poly(ply_modulo(operande1.poly, operande2.poly)); - if (operande1.poly) - ply_destruct(operande1.poly); - if (operande2.poly) - ply_destruct(operande2.poly); - } else { - exception(1, _("act_pile: OP_MOD invalid arguments")); - } - break; - case OP_EXP: + operande2 = pop_pile(1); + operande1 = pop_pile(1); + if ((operande1.type == T_POLY) && (operande2.type == T_POLY)) { + push_pile_poly(ply_modulo(operande1.poly, operande2.poly)); + if (operande1.poly) + ply_destruct(operande1.poly); + if (operande2.poly) + ply_destruct(operande2.poly); + } else { + exception(1, _("act_pile: OP_MOD invalid arguments")); + } + break; + case OP_EXP: #ifdef DEBUG - printf("----- OP_EXP\n"); + printf("----- OP_EXP\n"); #endif - operande1 = pop_pile(1); - operande2 = pop_pile(1); - if ((operande1.type == T_POLY) && (operande2.type == T_POLY)) { - if (operande1.poly) { - if ((operande1.poly->coef.denom == 1) - && (operande1.poly->coef.num >= 0)) { - push_pile_poly(ply_exposant(operande2.poly, operande1.poly->coef.num)); - if (operande2.poly) - ply_destruct(operande2.poly); - ply_destruct(operande1.poly); - } else { - exception(1, _("act_pile: OP_EXP invalid power")); - } - } else { - push_pile_poly(ply_constr(rat_constr(1,1),0)); - if (operande2.poly) - ply_destruct(operande2.poly); - } + operande1 = pop_pile(1); + operande2 = pop_pile(1); + if ((operande1.type == T_POLY) && (operande2.type == T_POLY)) { + if (operande1.poly) { + if ((operande1.poly->coef.denom == 1) + && (operande1.poly->coef.num >= 0)) { + push_pile_poly(ply_exposant(operande2.poly, operande1.poly->coef.num)); + if (operande2.poly) + ply_destruct(operande2.poly); + ply_destruct(operande1.poly); } else { - exception(1, _("act_pile: OP_EXP invalid arguments")); + exception(1, _("act_pile: OP_EXP invalid power")); } - break; - case OP_ASSIGN: + } else { + push_pile_poly(ply_constr(rat_constr(1, 1), 0)); + if (operande2.poly) + ply_destruct(operande2.poly); + } + } else { + exception(1, _("act_pile: OP_EXP invalid arguments")); + } + break; + case OP_ASSIGN: #ifdef DEBUG - printf("----- OP_ASSIGN\n"); + printf("----- OP_ASSIGN\n"); #endif - operande1 = pop_pile(1); - operande2 = pop_pile(1); - if ((operande1.type == T_POLY) && (operande2.type == T_STRING)) { - if (operande2.label) { - NomVarToVar(operande2.label, variables, &valid); - if (valid) - SupprimerDansTab(&variables,operande2.label); - InsererVarDansTab(&variables, CreerElement(operande2.label, (void *) operande1.poly)); - push_pile(operande2.label); - free(operande2.label); - } else { - exception(1, _("act_pile: OP_ASSIGN empty string")); - } - } else { - exception(1, _("act_pile: OP_ASSIGN invalid arguments")); - } - break; - case OP_PLUS_UNARY: + operande1 = pop_pile(1); + operande2 = pop_pile(1); + if ((operande1.type == T_POLY) && (operande2.type == T_STRING)) { + if (operande2.label) { + NomVarToVar(operande2.label, variables, &valid); + if (valid) + SupprimerDansTab(&variables, operande2.label); + InsererVarDansTab(&variables, + CreerElement(operande2.label, (void *) operande1.poly)); + push_pile(operande2.label); + free(operande2.label); + } else { + exception(1, _("act_pile: OP_ASSIGN empty string")); + } + } else { + exception(1, _("act_pile: OP_ASSIGN invalid arguments")); + } + break; + case OP_PLUS_UNARY: #ifdef DEBUG - printf("----- OP_PLUS_UNARY\n"); + printf("----- OP_PLUS_UNARY\n"); #endif - break; - case OP_MOINS_UNARY: + break; + case OP_MOINS_UNARY: #ifdef DEBUG - printf("----- OP_MOINS_UNARY\n"); + printf("----- OP_MOINS_UNARY\n"); #endif - operande1 = pop_pile(1); - if (operande1.type == T_POLY) { - push_pile_poly(ply_soustraction(ply_constr(rat_constr_zero(), 0), operande1.poly)); - if (operande1.poly) - ply_destruct(operande1.poly); - } else { - exception(1, _("act_pile: OP_MOINS_UNARY invalid argument")); - } - break; - case OP_FUNC_CALL: + operande1 = pop_pile(1); + if (operande1.type == T_POLY) { + push_pile_poly(ply_soustraction(ply_constr(rat_constr_zero(), 0), operande1.poly)); + if (operande1.poly) + ply_destruct(operande1.poly); + } else { + exception(1, _("act_pile: OP_MOINS_UNARY invalid argument")); + } + break; + case OP_FUNC_CALL: #ifdef DEBUG - printf("----- OP_FUNC_CALL\n"); + printf("----- OP_FUNC_CALL\n"); #endif - for (i=0;i<3;i++) - operande[i].poly=NULL; - operande1 = pop_pile(1); - if (operande1.type == T_INT) { - for(i=0;idegre == 0) { - push_pile_poly(ply_constr(ply_valuation(operande2.poly, operande[0].poly->coef), 0)); - if (operande[0].poly) - ply_destruct(operande[0].poly); - ply_destruct(operande2.poly); - } else { - exception(1, _("act_pile: OP_FUNC_CALL incorrect value for 2nd arg")); - } - } else { - exception(1, _("act_pile: OP_FUNC_CALL arg2 is an empty polynom")); - } - break; - case T_STRING: - appel_fonction(operande2.label,operande1.val,operande[0].poly, operande[1].poly, operande[2].poly); - break; - } - + for (i = 0; i < 3; i++) + operande[i].poly = NULL; + operande1 = pop_pile(1); + if (operande1.type == T_INT) { + for (i = 0; i < operande1.val; i++) { + operande[i] = pop_pile(1); + if (operande[i].type != T_POLY) + err = 1; + } + if (err == 1) { + exception(1, _("act_pile: OP_FUNC_CALL incorrect argument type")); + return; + } + operande2 = pop_pile(1); /* + * on recupere le polynome a evaluer ou le nom de la + * fonction a executer + */ + switch (operande2.type) { + case T_INT: + exception(2, _("act_pile: OP_FUNC_CALL invalid operand type")); + break; + case T_POLY: + if (operande2.poly) { + if (operande1.val != 1) { + exception(1, + _ + ("act_pile: OP_FUNC_CALL need only one argument for a polynom evaluation")); + } + if (operande[0].poly->degre == 0) { + push_pile_poly(ply_constr + (ply_valuation(operande2.poly, operande[0].poly->coef), 0)); + if (operande[0].poly) + ply_destruct(operande[0].poly); + ply_destruct(operande2.poly); + } else { + exception(1, _("act_pile: OP_FUNC_CALL incorrect value for 2nd arg")); + } } else { - exception(1, _("act_pile: OP_FUNC_CALL incorrect argument number")); + exception(1, _("act_pile: OP_FUNC_CALL arg2 is an empty polynom")); } break; - default: - exception(1, _("act_pile: Unknown operator")); + case T_STRING: + appel_fonction(operande2.label, operande1.val, operande[0].poly, operande[1].poly, + operande[2].poly); + break; + } + + } else { + exception(1, _("act_pile: OP_FUNC_CALL incorrect argument number")); } - popcontext(); + break; + default: + exception(1, _("act_pile: Unknown operator")); + } + popcontext(); } -/* Fonction de déboguage uniquement */ +/* + * Fonction de deboguage uniquement + */ void affichage_pile(void) { - int i; - - printf(_("\t-- Printing Stack\n")); - if (pile_ptr) { - for (i = 0; i <= pile_ptr - 1; i++) { - switch (pile[i].type) { - case T_INT: - printf("\t\t%d:I: %d\n", i, pile[i].val); - break; - case T_STRING: - printf("\t\t%d:S: %s\n", i, pile[i].label); - break; - case T_POLY: - printf("\t\t%d:P: %s\n", i, ply_affichage(pile[i].poly)); - break; - } - } + int i; + + printf(_("\t-- Printing Stack\n")); + if (pile_ptr) { + for (i = 0; i <= pile_ptr - 1; i++) { + switch (pile[i].type) { + case T_INT: + printf("\t\t%d:I: %d\n", i, pile[i].val); + break; + case T_STRING: + printf("\t\t%d:S: %s\n", i, pile[i].label); + break; + case T_POLY: + printf("\t\t%d:P: %s\n", i, ply_affichage(pile[i].poly)); + break; + } } - printf(_("\t-- End Printing Stack\n")); + } + printf(_("\t-- End Printing Stack\n")); } diff --git a/lib/polynom.c b/lib/polynom.c index e2e88d3..931f292 100644 --- a/lib/polynom.c +++ b/lib/polynom.c @@ -19,374 +19,421 @@ int smartprint = 1; -/* Les fonctions de base pour la construction et la destruction de polynomes */ +/* + * Les fonctions de base pour la construction et la destruction de polynomes + */ polynome ply_constr(rationnel coef, int degre) -{ /* constructeur monome */ - polynome temp; +{ /* + * constructeur monome + */ + polynome temp; - if (!coef.num) - return NULL; + if (!coef.num) + return NULL; - temp = (monome *) Emalloc(sizeof(monome)); + temp = (monome *) Emalloc(sizeof(monome)); - temp->coef = coef; - temp->degre = degre; - temp->suiv = NULL; + temp->coef = coef; + temp->degre = degre; + temp->suiv = NULL; - return temp; + return temp; } polynome ply_vide(void) -{ /* cree un polynome */ +{ /* + * cree un polynome + */ - return NULL; + return NULL; } void ply_destruct(polynome poly) -{ /* destructeur */ - if (poly) { - ply_destruct(poly->suiv); - free(poly); - } +{ /* + * destructeur + */ + if (poly) { + ply_destruct(poly->suiv); + free(poly); + } } - polynome ply_copy(polynome poly) -{ /* recopie */ - polynome result = NULL, temp = NULL, t; - - while (poly) { - t = ply_constr(poly->coef, poly->degre); - if (result) { - temp->suiv = t; - temp = t; - } else { - result = t; - temp = t; - } - poly = poly->suiv; +{ /* + * recopie + */ + polynome result = NULL, temp = NULL, t; + + while (poly) { + t = ply_constr(poly->coef, poly->degre); + if (result) { + temp->suiv = t; + temp = t; + } else { + result = t; + temp = t; } - return result; + poly = poly->suiv; + } + return result; } - -/* Voici toutes les fonctions de manipulation de polynôme. La fonction d'addition est la plus importante -puisqu'elle nous sert à construire un polynome entier à partir de monomes. Sa structure est comparable à celle -de l'opération 'fusion' dans le cas du tri fusion */ +/* + * Voici toutes les fonctions de manipulation de polynome. La fonction d'addition est la plus + * importante puisqu'elle nous sert a construire un polynome entier a partir de monomes. Sa + * structure est comparable a celle de l'operation 'fusion' dans le cas du tri fusion + */ polynome ply_addition(polynome poly1, polynome poly2) -{ /* addition de deux polynomes */ - polynome resultat = NULL, temp = NULL, t; - rationnel newrat; - int degre; - - while (poly1 && poly2) { - if (poly1->degre > poly2->degre) { - t = ply_constr(poly1->coef, poly1->degre); - poly1 = poly1->suiv; - } else if (poly1->degre < poly2->degre) { - t = ply_constr(poly2->coef, poly2->degre); - poly2 = poly2->suiv; - } else { - newrat = rat_addition(poly1->coef, poly2->coef); - degre = poly1->degre; - t = ply_constr(newrat, degre); - poly1 = poly1->suiv; - poly2 = poly2->suiv; - } - if (t) { - if (resultat) { - temp->suiv = t; - temp = t; - } else { - resultat = t; - temp = t; - } - } +{ /* + * addition de deux polynomes + */ + polynome resultat = NULL, temp = NULL, t; + rationnel newrat; + int degre; + + while (poly1 && poly2) { + if (poly1->degre > poly2->degre) { + t = ply_constr(poly1->coef, poly1->degre); + poly1 = poly1->suiv; + } else if (poly1->degre < poly2->degre) { + t = ply_constr(poly2->coef, poly2->degre); + poly2 = poly2->suiv; + } else { + newrat = rat_addition(poly1->coef, poly2->coef); + degre = poly1->degre; + t = ply_constr(newrat, degre); + poly1 = poly1->suiv; + poly2 = poly2->suiv; } - - while (poly1) { - t = ply_constr(poly1->coef, poly1->degre); - if (resultat) { - temp->suiv = t; - temp = t; - } else { - resultat = t; - temp = t; - } - poly1 = poly1->suiv; + if (t) { + if (resultat) { + temp->suiv = t; + temp = t; + } else { + resultat = t; + temp = t; + } } + } - while (poly2) { - t = ply_constr(poly2->coef, poly2->degre); - if (resultat) { - temp->suiv = t; - temp = t; - } else { - resultat = t; - temp = t; - } - poly2 = poly2->suiv; + while (poly1) { + t = ply_constr(poly1->coef, poly1->degre); + if (resultat) { + temp->suiv = t; + temp = t; + } else { + resultat = t; + temp = t; + } + poly1 = poly1->suiv; + } + + while (poly2) { + t = ply_constr(poly2->coef, poly2->degre); + if (resultat) { + temp->suiv = t; + temp = t; + } else { + resultat = t; + temp = t; } + poly2 = poly2->suiv; + } - return resultat; + return resultat; } -/* Nous avons choisi de réécrire cette fonction entièrement, plutôt que de bricoler à partir de la fonction addition */ +/* + * Nous avons choisi de reecrire cette fonction entierement, plutot que de bricoler a partir de la + * fonction addition + */ polynome ply_soustraction(polynome poly1, polynome poly2) -{ /* soustraction de deux polynomes */ - - polynome resultat = NULL, temp = NULL, t; - rationnel newrat; - int degre; - - while (poly1 && poly2) { - if (poly1->degre > poly2->degre) { - t = ply_constr(poly1->coef, poly1->degre); - poly1 = poly1->suiv; - } else if (poly1->degre < poly2->degre) { - t = ply_constr(rat_moinsunaire(poly2->coef), poly2->degre); - poly2 = poly2->suiv; - } else { - newrat = rat_soustraction(poly1->coef, poly2->coef); - degre = poly1->degre; - t = ply_constr(newrat, degre); - poly1 = poly1->suiv; - poly2 = poly2->suiv; - } - if (t) { - if (resultat) { - temp->suiv = t; - temp = t; - } else { - resultat = t; - temp = t; - } - } +{ /* + * soustraction de deux polynomes + */ + + polynome resultat = NULL, temp = NULL, t; + rationnel newrat; + int degre; + + while (poly1 && poly2) { + if (poly1->degre > poly2->degre) { + t = ply_constr(poly1->coef, poly1->degre); + poly1 = poly1->suiv; + } else if (poly1->degre < poly2->degre) { + t = ply_constr(rat_moinsunaire(poly2->coef), poly2->degre); + poly2 = poly2->suiv; + } else { + newrat = rat_soustraction(poly1->coef, poly2->coef); + degre = poly1->degre; + t = ply_constr(newrat, degre); + poly1 = poly1->suiv; + poly2 = poly2->suiv; } - - while (poly1) { - t = ply_constr(poly1->coef, poly1->degre); - if (resultat) { - temp->suiv = t; - temp = t; - } else { - resultat = t; - temp = t; - } - poly1 = poly1->suiv; + if (t) { + if (resultat) { + temp->suiv = t; + temp = t; + } else { + resultat = t; + temp = t; + } } + } - while (poly2) { - t = ply_constr(rat_moinsunaire(poly2->coef), poly2->degre); - if (resultat) { - temp->suiv = t; - temp = t; - } else { - resultat = t; - temp = t; - } - poly2 = poly2->suiv; + while (poly1) { + t = ply_constr(poly1->coef, poly1->degre); + if (resultat) { + temp->suiv = t; + temp = t; + } else { + resultat = t; + temp = t; } + poly1 = poly1->suiv; + } + + while (poly2) { + t = ply_constr(rat_moinsunaire(poly2->coef), poly2->degre); + if (resultat) { + temp->suiv = t; + temp = t; + } else { + resultat = t; + temp = t; + } + poly2 = poly2->suiv; + } - return resultat; + return resultat; } -/* La fonction multiplication se base sur la fonction addition, suivant les mathématiques classiques */ +/* + * La fonction multiplication se base sur la fonction addition, suivant les mathematiques + * classiques + */ polynome ply_multiplication(polynome poly1, polynome poly2) -{ /* multiplication de deux polynomes */ - polynome temp = NULL, t, resultat = NULL, r, tempresult = NULL, poly2init; - - poly2init = poly2; - while (poly1) { - poly2 = poly2init; - while (poly2) { - t = ply_constr(rat_multiplication(poly1->coef, poly2->coef), poly1->degre + poly2->degre); - if (t) { - if (tempresult) { - temp->suiv = t; - temp = t; - } else { - tempresult = t; - temp = t; - } - } - poly2 = poly2->suiv; +{ /* + * multiplication de deux polynomes + */ + polynome temp = NULL, t, resultat = NULL, r, tempresult = NULL, poly2init; + + poly2init = poly2; + while (poly1) { + poly2 = poly2init; + while (poly2) { + t = + ply_constr(rat_multiplication(poly1->coef, poly2->coef), + poly1->degre + poly2->degre); + if (t) { + if (tempresult) { + temp->suiv = t; + temp = t; + } else { + tempresult = t; + temp = t; } - poly1 = poly1->suiv; - r = ply_addition(tempresult, resultat); - ply_destruct(resultat); - resultat = r; - ply_destruct(tempresult); - tempresult = NULL; + } + poly2 = poly2->suiv; } - - return resultat; + poly1 = poly1->suiv; + r = ply_addition(tempresult, resultat); + ply_destruct(resultat); + resultat = r; + ply_destruct(tempresult); + tempresult = NULL; + } + + return resultat; } - -/* L'algorithme pour la division et le modulo est le même. Seul le return change. En effet, pour faire le calcul, nous -utilisons la méthode simple vue en cours de mathématiques, qui consiste à poser la division sur une feuille et à effectuer -une série de multiplications élémentaires et de soustractions. Nous obtenons donc le quotient ET le reste. */ +/* + * L'algorithme pour la division et le modulo est le meme. Seul le return change. En effet, pour + * faire le calcul, nous utilisons la methode simple vue en cours de mathematiques, qui consiste a + * poser la division sur une feuille et a effectuer une serie de multiplications elementaires et de + * soustractions. Nous obtenons donc le quotient ET le reste. + */ polynome ply_division(polynome dividende, polynome diviseur) -{ /* division de deux polynomes */ - polynome interdividende = ply_copy(dividende), interdiviseur = NULL, inter = NULL, reste = dividende, resultat = NULL, r = NULL; - int b = 0; +{ /* + * division de deux polynomes + */ + polynome interdividende = ply_copy(dividende), interdiviseur = NULL, inter = NULL, reste = + dividende, resultat = NULL, r = NULL; + int b = 0; #ifdef DEBUG - printf("On divise %s", ply_affichage(dividende)); - printf("par %s\n", ply_affichage(diviseur)); - printf("interdividende degre = %u\n", interdividende->degre); - printf("diviseur degre = %u\n", diviseur->degre); + printf("On divise %s", ply_affichage(dividende)); + printf("par %s\n", ply_affichage(diviseur)); + printf("interdividende degre = %u\n", interdividende->degre); + printf("diviseur degre = %u\n", diviseur->degre); #endif - while ((interdividende) && (interdividende->degre >= diviseur->degre)) { - b = 1; - inter = ply_constr(rat_division(interdividende->coef, diviseur->coef), interdividende->degre - diviseur->degre); + while ((interdividende) && (interdividende->degre >= diviseur->degre)) { + b = 1; + inter = + ply_constr(rat_division(interdividende->coef, diviseur->coef), + interdividende->degre - diviseur->degre); #ifdef DEBUG - printf("On trouve qu'il nous faut soustraire %s fois\n", ply_affichage(inter)); + printf("On trouve qu'il nous faut soustraire %s fois\n", ply_affichage(inter)); #endif - r = ply_addition(resultat, inter); + r = ply_addition(resultat, inter); #ifdef DEBUG - printf("Resultat intermédiaire %s\n", ply_affichage(r)); + printf("Resultat intermediaire %s\n", ply_affichage(r)); #endif - interdiviseur = ply_multiplication(diviseur, inter); + interdiviseur = ply_multiplication(diviseur, inter); #ifdef DEBUG - printf("On soustrait donc %s\n", ply_affichage(interdiviseur)); + printf("On soustrait donc %s\n", ply_affichage(interdiviseur)); #endif - reste = ply_soustraction(interdividende, interdiviseur); + reste = ply_soustraction(interdividende, interdiviseur); #ifdef DEBUG - printf("Reste intermédiaire %s\n", ply_affichage(reste)); + printf("Reste intermediaire %s\n", ply_affichage(reste)); #endif - ply_destruct(resultat); - ply_destruct(inter); - ply_destruct(interdividende); - ply_destruct(interdiviseur); - resultat = r; - interdividende = reste; - } - - if (!b) ply_destruct(interdividende); - - return resultat; + ply_destruct(resultat); + ply_destruct(inter); + ply_destruct(interdividende); + ply_destruct(interdiviseur); + resultat = r; + interdividende = reste; + } + + if (!b) + ply_destruct(interdividende); + + return resultat; } polynome ply_modulo(polynome dividende, polynome diviseur) -{ /* reste de la division de deux polynomes */ - polynome interdividende = ply_copy(dividende), interdiviseur = NULL, inter = NULL, reste = dividende, resultat = NULL, r = NULL; - int b = 0; +{ /* + * reste de la division de deux polynomes + */ + polynome interdividende = ply_copy(dividende), interdiviseur = NULL, inter = NULL, reste = + dividende, resultat = NULL, r = NULL; + int b = 0; #ifdef DEBUG - printf("On divise %s", ply_affichage(dividende)); - printf("par %s\n", ply_affichage(diviseur)); - printf("interdividende degre = %u\n", interdividende->degre); - printf("diviseur degre = %u\n", diviseur->degre); + printf("On divise %s", ply_affichage(dividende)); + printf("par %s\n", ply_affichage(diviseur)); + printf("interdividende degre = %u\n", interdividende->degre); + printf("diviseur degre = %u\n", diviseur->degre); #endif - while ((interdividende) && (interdividende->degre >= diviseur->degre)) { - b = 1; - inter = ply_constr(rat_division(interdividende->coef, diviseur->coef), interdividende->degre - diviseur->degre); + while ((interdividende) && (interdividende->degre >= diviseur->degre)) { + b = 1; + inter = + ply_constr(rat_division(interdividende->coef, diviseur->coef), + interdividende->degre - diviseur->degre); #ifdef DEBUG - printf("On trouve qu'il nous faut soustraire %s fois\n", ply_affichage(inter)); + printf("On trouve qu'il nous faut soustraire %s fois\n", ply_affichage(inter)); #endif - r = ply_addition(resultat, inter); + r = ply_addition(resultat, inter); #ifdef DEBUG - printf("Resultat intermédiaire %s\n", ply_affichage(r)); + printf("Resultat intermediaire %s\n", ply_affichage(r)); #endif - interdiviseur = ply_multiplication(diviseur, inter); + interdiviseur = ply_multiplication(diviseur, inter); #ifdef DEBUG - printf("On soustrait donc %s\n", ply_affichage(interdiviseur)); + printf("On soustrait donc %s\n", ply_affichage(interdiviseur)); #endif - reste = ply_soustraction(interdividende, interdiviseur); + reste = ply_soustraction(interdividende, interdiviseur); #ifdef DEBUG - printf("Reste intermédiaire %s\n", ply_affichage(reste)); + printf("Reste intermediaire %s\n", ply_affichage(reste)); #endif - ply_destruct(resultat); - ply_destruct(inter); - ply_destruct(interdividende); - ply_destruct(interdiviseur); - resultat = r; - interdividende = reste; - } - - if (!b) ply_destruct(interdividende); - - return b ? reste : ply_copy(reste); + ply_destruct(resultat); + ply_destruct(inter); + ply_destruct(interdividende); + ply_destruct(interdiviseur); + resultat = r; + interdividende = reste; + } + + if (!b) + ply_destruct(interdividende); + + return b ? reste : ply_copy(reste); } polynome ply_exposant(polynome poly, unsigned int exp) -{ /* exponentiation d'un polynome */ - int i; - polynome result, temp; - - if (poly) { - result = ply_constr(rat_constr(1, 1), 0); - for (i = 0; i < exp; i++) { - temp = ply_multiplication(result, poly); - ply_destruct(result); - result = temp; - } - } else { - result = NULL; +{ /* + * exponentiation d'un polynome + */ + int i; + polynome result, temp; + + if (poly) { + result = ply_constr(rat_constr(1, 1), 0); + for (i = 0; i < exp; i++) { + temp = ply_multiplication(result, poly); + ply_destruct(result); + result = temp; } - return result; + } else { + result = NULL; + } + return result; } rationnel ply_valuation(polynome poly, rationnel point) -{ /* évaluation d'un polynome en un point */ - rationnel result = rat_constr_zero(); - - while (poly) { - result = rat_addition(result, rat_multiplication(poly->coef, rat_pow(point, poly->degre))); - poly = poly->suiv; - } - return result; +{ /* + * evaluation d'un polynome en un point + */ + rationnel result = rat_constr_zero(); + + while (poly) { + result = rat_addition(result, rat_multiplication(poly->coef, rat_pow(point, poly->degre))); + poly = poly->suiv; + } + return result; } char *ply_affichage(polynome poly) -{ /* routine d'affichage d'un polynome */ - static char buf[BUFSIZ]; - char temp[BUFSIZ]; - char * rat; - int count = 0; - int first = 1; - - buf[0] = '\0'; - if (!poly) { - sprintf(buf, "%s ", rat_to_string(rat_constr(0,1), 1)); - } else { - while (poly) { - if (poly->degre != 0) { - if (smartprint) { - rat = (rat_to_double(poly->coef) == 1) ? (first ? "" : "+ ") : rat_to_double(poly->coef) == -1 ? (first ? "-" : "- ") : rat_to_string(poly->coef, first); - switch(poly->degre) { - case 1: - sprintf(temp, "%s%s ", rat, mute); - break; - case 2: - sprintf(temp, "%s%s%c ", rat, mute, 178); - break; - case 3: - sprintf(temp, "%s%s%c ", rat, mute, 179); - break; - default: - sprintf(temp, "%s%s^%u ", rat, mute, poly->degre); - break; - } - } else { - sprintf(temp, "%s*%s^%u ", rat_to_string(poly->coef, first), mute, poly->degre); - } - } else { - sprintf(temp, "%s ", rat_to_string(poly->coef, first)); - } - count += strlen(temp); - if (count < BUFSIZ) - strcat(buf, temp); - else - exception(2, _("ply_affichage: strcat error, not enough space in buffer")); - poly = poly->suiv; - first = 0; +{ /* + * routine d'affichage d'un polynome + */ + static char buf[BUFSIZ]; + char temp[BUFSIZ]; + char *rat; + int count = 0; + int first = 1; + + buf[0] = '\0'; + if (!poly) { + sprintf(buf, "%s ", rat_to_string(rat_constr(0, 1), 1)); + } else { + while (poly) { + if (poly->degre != 0) { + if (smartprint) { + rat = + (rat_to_double(poly->coef) == + 1) ? (first ? "" : "+ ") : rat_to_double(poly->coef) == + -1 ? (first ? "-" : "- ") : rat_to_string(poly->coef, first); + switch (poly->degre) { + case 1: + sprintf(temp, "%s%s ", rat, mute); + break; + case 2: + sprintf(temp, "%s%s%c ", rat, mute, 178); + break; + case 3: + sprintf(temp, "%s%s%c ", rat, mute, 179); + break; + default: + sprintf(temp, "%s%s^%u ", rat, mute, poly->degre); + break; + } + } else { + sprintf(temp, "%s*%s^%u ", rat_to_string(poly->coef, first), mute, poly->degre); } - } - return buf; + } else { + sprintf(temp, "%s ", rat_to_string(poly->coef, first)); + } + count += strlen(temp); + if (count < BUFSIZ) + strcat(buf, temp); + else + exception(2, _("ply_affichage: strcat error, not enough space in buffer")); + poly = poly->suiv; + first = 0; + } + } + return buf; } diff --git a/lib/scalaires.c b/lib/scalaires.c index ee44304..857faab 100644 --- a/lib/scalaires.c +++ b/lib/scalaires.c @@ -13,171 +13,206 @@ #define _(x) x #endif -/* Cette précision est utilisée dans le cas où nous devons convertir un double en rationnel */ +/* + * Cette precision est utilisee dans le cas ou nous devons convertir un double en rationnel + */ #define PRECISION 1E6 typedisplay display; -/* Fonction interne uniquement */ +/* + * Fonction interne uniquement + */ static unsigned long long pgcd(unsigned long long a, unsigned long long b) { - if (a < b) - return pgcd(b, a); - if (!b) - return a; - return pgcd(b, a % b); + if (a < b) + return pgcd(b, a); + if (!b) + return a; + return pgcd(b, a % b); } -/* Les quelques fonctions utiles à tout le monde... Juste pour éviter de taper du code en n exemplaire */ +/* + * Les quelques fonctions utiles a tout le monde... Juste pour eviter de taper du code en n + * exemplaire + */ rationnel rat_constr_zero(void) -{ /* renvoie 0 */ - rationnel temp; - - temp.num = 0; - temp.denom = 1; - return temp; +{ /* + * renvoie 0 + */ + rationnel temp; + + temp.num = 0; + temp.denom = 1; + return temp; } - rationnel rat_constr(unsigned long long num, unsigned long long denom) -{ /* cree une fraction */ - rationnel temp; - int sgnnum = 1, sgndenom = 1; - - if (num < 0) { - sgnnum = -1; - num = -num; - } - - if (denom < 0) { - sgndenom = -1; - denom = -denom; - } - if (!num) { - temp.num = 0; - temp.denom = 1; - } else if (denom) { - temp.num = sgnnum * sgndenom * num / pgcd(num, denom); - temp.denom = denom / pgcd(num, denom); - } else { - exception(2, _("rat_constr: division by zero")); - } - return temp; +{ /* + * cree une fraction + */ + rationnel temp; + int sgnnum = 1, sgndenom = 1; + + if (num < 0) { + sgnnum = -1; + num = -num; + } + + if (denom < 0) { + sgndenom = -1; + denom = -denom; + } + if (!num) { + temp.num = 0; + temp.denom = 1; + } else if (denom) { + temp.num = sgnnum * sgndenom * num / pgcd(num, denom); + temp.denom = denom / pgcd(num, denom); + } else { + exception(2, _("rat_constr: division by zero")); + } + return temp; } -/* Cette fonction est là pour faire beau. En fait, aucune routine ne l'utilise. Elle est écrite "au cas où" */ +/* + * Cette fonction est la pour faire beau. En fait, aucune routine ne l'utilise. Elle est ecrite "au + * cas ou" + */ rationnel rat_constr_from_double(double flt) -{ /* cree une fraction a partir d un double */ - return rat_constr(floor(flt * PRECISION), PRECISION); +{ /* + * cree une fraction a partir d un double + */ + return rat_constr(floor(flt * PRECISION), PRECISION); } void rat_destruct(rationnel rat) -{ /* destructeur */ +{ /* + * destructeur + */ } double rat_to_double(rationnel rat) -{ /* obtention du double correspondant a un rationnel */ - return ((double) rat.num / (double) rat.denom); +{ /* + * obtention du double correspondant a un rationnel + */ + return ((double) rat.num / (double) rat.denom); } rationnel rat_addition(rationnel rat1, rationnel rat2) -{ /* addition */ +{ /* + * addition + */ - return rat_constr(rat1.num * rat2.denom + rat2.num * rat1.denom, rat1.denom * rat2.denom); + return rat_constr(rat1.num * rat2.denom + rat2.num * rat1.denom, rat1.denom * rat2.denom); } rationnel rat_soustraction(rationnel rat1, rationnel rat2) -{ /* soustraction */ +{ /* + * soustraction + */ - return rat_constr(rat1.num * rat2.denom - rat2.num * rat1.denom, rat1.denom * rat2.denom); + return rat_constr(rat1.num * rat2.denom - rat2.num * rat1.denom, rat1.denom * rat2.denom); } rationnel rat_moinsunaire(rationnel rat1) -{ /* moins unaire */ +{ /* + * moins unaire + */ - return rat_constr(-rat1.num, rat1.denom); + return rat_constr(-rat1.num, rat1.denom); } rationnel rat_multiplication(rationnel rat1, rationnel rat2) -{ /* multiplication */ +{ /* + * multiplication + */ - return rat_constr(rat1.num * rat2.num, rat1.denom * rat2.denom); + return rat_constr(rat1.num * rat2.num, rat1.denom * rat2.denom); } rationnel rat_division(rationnel rat1, rationnel rat2) -{ /* division */ - if (!rat2.num) - exception(2,_("division by zero")); - return rat_constr(rat1.num * rat2.denom, rat1.denom * rat2.num); - +{ /* + * division + */ + if (!rat2.num) + exception(2, _("division by zero")); + return rat_constr(rat1.num * rat2.denom, rat1.denom * rat2.num); + } -/* On choisit délibérément de faire une puissance 'lente' pour éviter de faire des overflow -trop facilement */ +/* + * On choisit deliberement de faire une puissance 'lente' pour eviter de faire des overflow trop + * facilement + */ rationnel rat_pow(rationnel rat, unsigned int p) -{ /* puissance */ - for (; p; p--) { - rat = rat_multiplication(rat, rat); - } - return rat; +{ /* + * puissance + */ + for (; p; p--) { + rat = rat_multiplication(rat, rat); + } + return rat; } -/* On convertit un rationnel en une chaîne. Le booléen first indique si on le rationnel devra être placé en début de chaîne (pour -les signes) */ +/* + * On convertit un rationnel en une chaine. Le booleen first indique si on le rationnel devra etre + * place en debut de chaine (pour les signes) + */ char *rat_to_string(rationnel rat, int first) { - static char resultat[128]; - char temp[64]; - - resultat[0] = '\0'; - - if (rat.num<0) { - rat.num=-rat.num; - if (first) { - strcat(resultat,"-"); - } else { - strcat(resultat,"- "); - } + static char resultat[128]; + char temp[64]; + + resultat[0] = '\0'; + + if (rat.num < 0) { + rat.num = -rat.num; + if (first) { + strcat(resultat, "-"); } else { - if (!first) { - strcat(resultat,"+ "); - } + strcat(resultat, "- "); } - - switch(display) { - case DEC: - if (rat.denom==1) - sprintf(temp,"%qd", rat.num); - else - sprintf(temp,"%qd/%qd", rat.num, rat.denom); - break; - case HEX: - if (rat.denom==1) - sprintf(temp,"0x%qx", rat.num); - else - sprintf(temp,"0x%qx/0x%qx", rat.num, rat.denom); - break; - case OCT: - if (rat.denom==1) - sprintf(temp,"0%qo", rat.num); - else - sprintf(temp,"0%qo/0%qo", rat.num, rat.denom); - break; - case FLT: - sprintf(temp,"%g", rat_to_double(rat)); - break; + } else { + if (!first) { + strcat(resultat, "+ "); } - - strcat(resultat, temp); - return resultat; + } + + switch (display) { + case DEC: + if (rat.denom == 1) + sprintf(temp, "%qd", rat.num); + else + sprintf(temp, "%qd/%qd", rat.num, rat.denom); + break; + case HEX: + if (rat.denom == 1) + sprintf(temp, "0x%qx", rat.num); + else + sprintf(temp, "0x%qx/0x%qx", rat.num, rat.denom); + break; + case OCT: + if (rat.denom == 1) + sprintf(temp, "0%qo", rat.num); + else + sprintf(temp, "0%qo/0%qo", rat.num, rat.denom); + break; + case FLT: + sprintf(temp, "%g", rat_to_double(rat)); + break; + } + + strcat(resultat, temp); + return resultat; } diff --git a/lib/terminal.c b/lib/terminal.c index 2b13242..7d0784c 100644 --- a/lib/terminal.c +++ b/lib/terminal.c @@ -6,10 +6,12 @@ #include "config.h" #include "exceptions.h" -/* Quelques fonctions globales pour initialiser le terminal et en faire quelque chose -qui ressemble aux fonctions DOS. En clair, l'interface n'a pas besoin d'attendre que l'utilisateur -tape ENTREE pour lire une touche. De plus, dans ce mode, il n'y a pas d'écho à l'écran. Donc -l'interface peut refaire ce qu'elle veut derrière */ +/* + * Quelques fonctions globales pour initialiser le terminal et en faire quelque chose qui ressemble + * aux fonctions DOS. En clair, l'interface n'a pas besoin d'attendre que l'utilisateur tape ENTREE + * pour lire une touche. De plus, dans ce mode, il n'y a pas d'echo a l'ecran. Donc l'interface + * peut refaire ce qu'elle veut derriere + */ FILE *input; struct termios initial_settings, new_settings; -- cgit v1.2.3