From 892950d2c402691cc12e58b556944c19b4013601 Mon Sep 17 00:00:00 2001 From: biouman Date: Sat, 28 Apr 2001 00:53:35 +0000 Subject: *** empty log message *** --- Makefile | 2 +- hash.h | 4 - interface.c | 234 ----------------------------------------------- interface.h | 22 ----- main.c | 2 +- parser.c | 295 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ parser.h | 25 ++++++ pile.c | 12 +-- polynom.c | 4 +- 9 files changed, 330 insertions(+), 270 deletions(-) delete mode 100644 interface.c delete mode 100644 interface.h create mode 100644 parser.c create mode 100644 parser.h diff --git a/Makefile b/Makefile index 718e4b7..da10b70 100644 --- a/Makefile +++ b/Makefile @@ -7,4 +7,4 @@ CC=gcc LDFLAGS=-lm CFLAGS=-Wall -O3 -polynom: exceptions.o hash.o parse.o main.o numbers.o pile.o polynom.o scalaires.o +polynom: exceptions.o hash.o parser.o main.o numbers.o pile.o polynom.o scalaires.o diff --git a/hash.h b/hash.h index a7948a1..4f40659 100644 --- a/hash.h +++ b/hash.h @@ -3,11 +3,7 @@ #define TAILLECHAINEHACHAGE (26*2+1) -#ifdef HAVE_CONFIG_H typedef void *_TypeVariable; -#else -typedef int _TypeVariable; -#endif typedef struct { char *NomVar; diff --git a/interface.c b/interface.c deleted file mode 100644 index 419e624..0000000 --- a/interface.c +++ /dev/null @@ -1,234 +0,0 @@ -/* - * - * Interpreteur de ligne de commande - * - */ - -#include -#include -#ifdef HAVE_CONFIG_H -#include "config.h" -#else -#define _(x) x -#endif -#include "interface.h" -#include "exceptions.h" -#include "pile.h" - - - -typedef unsigned char op_t; - -typedef struct operator_t { - op_t op; - int pri, func; -} operator_t; - -op_t pile_operators[PILEOP_MAX]; -int pile_nestedcall[PILECALL_MAX]; - -int pileop_pos = 0, pilecall_pos = 0; - -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} -}; - -operator_t get_op(op_t op) -{ - int i; - - for (i = 0; operators[i].op != 255; i++) { - if (operators[i].op == op) - return operators[i]; - } - - return operators[i]; -} - -int get_pri(op_t op) -{ - return get_op(op).pri; -} - -int get_func(op_t op) -{ - return get_op(op).func; -} - -op_t get_last_op(void) -{ - if (pileop_pos) - return pile_operators[pileop_pos - 1]; - else - return -1; -} - -op_t pop_op(void) -{ - if (pileop_pos) - return pile_operators[--pileop_pos]; - return -1; -} - -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")); - -} - -int pop_call(void) -{ - if (pilecall_pos) - fprintf(stderr, "Dépilement du dernier appel de fonction: %i\n", - pile_nestedcall[pilecall_pos - 1]); - return pile_nestedcall[--pilecall_pos]; - return -1; -} - -void increment_call(void) -{ - if (pilecall_pos) { - fprintf(stderr, "Incrément du dernier appel de fonction: %i\n", - pile_nestedcall[pilecall_pos - 1]); - if (pile_nestedcall[pilecall_pos - 1] != -1) - pile_nestedcall[pilecall_pos - 1]++; - } -} - -int get_last_call(void) -{ - if (pilecall_pos) - return pile_nestedcall[pilecall_pos - 1]; - return -1; -} - -void push_call(int call) -{ - fprintf(stderr, "Empilement d'un appel de fonction: %i\n", call); - if (pilecall_pos != PILECALL_MAX) - pile_nestedcall[pilecall_pos++] = call; - else - exception(-1, _("Too many nested functions calls in expression.\n")); -} - -char *getword(char *line, char *p) -{ - char o; - - do { - if (*(line) != ' ') { - o = *(p++) = *line; - fprintf(stderr, "getword(): a trouvé le caractère '%c'\n", o); - } - line++; - } while ((*line) && (*line != ')') && (*line != ';') && (get_func(*line) == -1) - && (get_func(o) == -1)); - *p = '\0'; - return line; -} - -int parse_line(char *line) -{ - char buffer[BUFSIZ]; - op_t op; - int got_unary = 128, nbrargs; - - fprintf(stderr, "Analyse de: \"%s\"...\n\n", line); - while (*line) { - line = getword(line, buffer); - fprintf(stderr, "parse_line(): getword a trouvé le mot \"%s\"\n", buffer); - if (get_func(buffer[0]) != -1) { - fprintf(stderr, "Il s'agit d'un opérateur.\n"); - buffer[0] += got_unary; - if (got_unary) { - fprintf(stderr, "Opérateur de type unaire\n"); - } - if (get_pri(buffer[0]) == -1) { - if (got_unary) { - exception(-1, _("Invalid unary operator")); - } else { - exception(-1, _("Invalid binary operator")); - } - } - fprintf(stderr, "Priorité du dernier op: %i, priorité actu: %i\n", - get_pri(get_last_op()), get_pri(buffer[0])); - while (get_pri(get_last_op()) >= get_pri(buffer[0]) - && ((get_last_op() & 127) != '(')) { - fprintf(stderr, - "Il y a un opérateur plus important sur la file.\n"); - act_pile(get_func(pop_op())); - got_unary = 0; - fprintf(stderr, "Priorité du dernier op: %i, priorité actu: %i\n", - get_pri(get_last_op()), get_pri(buffer[0])); - } - fprintf(stderr, "Mise sur la file\n"); - 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] == ')')) { - fprintf(stderr, "Caractère spécial\n"); - switch (buffer[0]) { - case ';': - fprintf(stderr, "Vidage complet de la file (fin de commande).\n"); - while (pileop_pos) { - op = pop_op(); - if (op == '(') - exception(-1, - _ - ("Parse error: too much left parenthesis")); - act_pile(get_func(op)); - } - break; - case ')': - fprintf(stderr, "Vidage de la file jusqu'à '(' car ')' trouvée.\n"); - while (1) { - if (!pileop_pos) - exception(-1, - _ - ("Parse error: too much right parenthesis")); - op = pop_op(); - if (((op & 127) == '(')) - break; - act_pile(get_func(op)); - } - 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] == '_')) { - fprintf(stderr, "Symbole.\n"); - push_pile(buffer); - got_unary = 0; - if (!get_last_call()) - increment_call(); - } else if (buffer[0]) { - exception(-1, _("Invalid character")); - } - } -} diff --git a/interface.h b/interface.h deleted file mode 100644 index 546eddd..0000000 --- a/interface.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef __INTERFACE_H__ -#define __INTERFACE_H__ - -#define PILEOP_MAX 50 -#define PILECALL_MAX 50 - -enum { - OP_NEST, - OP_PLUS, - OP_MOINS, - OP_EXP, - OP_PLUS_UNARY, - OP_MOINS_UNARY, - OP_DIV, - OP_MOD, - OP_MUL, - OP_ASSIGN, - OP_LPAREN, - OP_FUNC_CALL -}; - -#endif diff --git a/main.c b/main.c index 5a66e1a..8305ad4 100644 --- a/main.c +++ b/main.c @@ -7,7 +7,7 @@ #include #include "main.h" #include "hash.h" -#include "parse.h" +#include "parser.h" #include "polynom.h" #include "pile.h" #ifdef HAVE_CONFIG_H diff --git a/parser.c b/parser.c new file mode 100644 index 0000000..cd0a03b --- /dev/null +++ b/parser.c @@ -0,0 +1,295 @@ +/* + * + * Interpreteur de ligne de commande + * + */ +#include +#include +#ifdef HAVE_CONFIG_H +#include "config.h" +#else +#define _(x) x +#endif +#include "exceptions.h" +#include "parser.h" +#include "pile.h" + + +typedef unsigned char op_t; + +typedef struct operator_t { + op_t op; + int pri, func; +} operator_t; + +static op_t pile_operators[PILEOP_MAX]; +static int pile_nestedcall[PILECALL_MAX]; + +static int pileop_pos = 0, pilecall_pos = 0; + +/* La liste des opérateurs 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} +}; + + + + +/* Fonction interne: convertit un operateur en sa structure */ + +static operator_t get_op(op_t op) +{ + int i; + + for (i = 0; operators[i].op != 255; i++) { + if (operators[i].op == op) + return operators[i]; + } + + return operators[i]; +} + +/* Fonctions internes de lectures sur la structure */ + +static int get_pri(op_t op) +{ + return get_op(op).pri; +} + +static int get_func(op_t op) +{ + return get_op(op).func; +} + +/* Focntions internes d'empilement / dépilement */ + +static op_t get_last_op(void) +{ + 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; +} + +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")); + +} + +static int pop_call(void) +{ + 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]++; + } +} + +static int get_last_call(void) +{ + 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")); +} + +/* 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; + + 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; + } + } + } + line++; + } + while (((*line) && (*line != ')') && (*line != ']') + && (*line != ';') && (get_func(*line) == -1) + && (get_func(o) == -1)) || (instring)); + *p = '\0'; + return line; +} + +/* Cette fonction va parcourire une chaine afin d'appeler les fonction push_pule() et act_pile() */ + +void parse_line(char *line) +{ + char buffer[BUFSIZ], imm[BUFSIZ], *d = line; + op_t op; + int got_unary = 128, nbrargs; + + sprintf(buffer, "Read line '%s'", line); + pushcontext(buffer); + + while (*line) { + 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 lut 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) != '(') + && ((get_last_op() & 127) != '['))) { + 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] == ')') + || (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) { + op = pop_op(); + if (op == '(') + exception(-1, _("Parse error: too much left parenthesis")); + act_pile(get_func(op)); + } + popcontext(); + popcontext(); + return; + case ')': + /* Fin de parenthese (Appel de fonction ou expression mathématique) */ + while (1) { + if (!pileop_pos) + exception(-1, _("Parse error: too much right parenthesis")); + op = pop_op(); + if (((op & 127) == '(')) + break; + if (((op & 127) == '[')) + exception(-1, _("Parse error: enclosure mismatch")); + act_pile(get_func(op)); + } + if (op == '(') { + nbrargs = pop_call(); + push_pile_int(nbrargs); + act_pile(get_func(op)); + } + got_unary = 0; + break; + case ']': + /* Fin d'opérateur de décalage */ + while (1) { + if (!pileop_pos) + exception(-1, _("Parse error: too much right parenthesis")); + op = pop_op(); + if (((op & 127) == '[')) + break; + if (((op & 127) == '(')) + exception(-1, _("Parse error: enclosure mismatch")); + act_pile(get_func(op)); + } + 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(); +} + diff --git a/parser.h b/parser.h new file mode 100644 index 0000000..1e7c0fd --- /dev/null +++ b/parser.h @@ -0,0 +1,25 @@ +#ifndef __PARSER_H__ +#define __PARSER_H__ + +#define PILEOP_MAX 50 +#define PILECALL_MAX 50 + +enum { + OP_NEST, + OP_PLUS, + OP_MOINS, + OP_EXP, + OP_PLUS_UNARY, + OP_MOINS_UNARY, + OP_DIV, + OP_MOD, + OP_MUL, + OP_ASSIGN, + OP_LPAREN, + OP_FUNC_CALL +}; + + +void parse_line(char *); + +#endif diff --git a/pile.c b/pile.c index fe305a1..9f4bead 100644 --- a/pile.c +++ b/pile.c @@ -3,12 +3,12 @@ * Gestion de la pile des operandes * */ - +#include #include "pile.h" #include "exceptions.h" #include "numbers.h" #include "main.h" -#include "parse.h" +#include "parser.h" #ifdef HAVE_CONFIG_H #include "config.h" #else @@ -22,11 +22,11 @@ unsigned int pile_ptr = 0; void push_pile(char *st) { - int valid1, valid2, valid3, valid4; + int valid1, valid2, valid3, valid4=0; int i_number; double d_number; polynome poly; - char *buf; + i_number = char_to_number(st, &valid1); d_number = char_to_double(st, &valid2); @@ -98,7 +98,7 @@ pile_elem pop_pile(unsigned int count) char *affichage_level_1(void) { - char *result; + char *result=NULL; if (!pile_ptr) { switch (pile[pile_ptr - 1].type) { @@ -110,11 +110,11 @@ char *affichage_level_1(void) break; case T_INT: result = (char *) Emalloc(11 * sizeof(char)); - sprintf(result, "%10d", pile[pile_ptr - 1].val); break; } } + return result; } int is_mute(char *st) diff --git a/polynom.c b/polynom.c index 26a11c8..f1c19a2 100644 --- a/polynom.c +++ b/polynom.c @@ -218,7 +218,7 @@ polynome ply_multiplication(polynome poly1, polynome poly2) polynome ply_division(polynome poly1, polynome poly2) { /* division de deux polynomes */ - polynome result; + polynome result=NULL; return result; @@ -227,7 +227,7 @@ polynome ply_division(polynome poly1, polynome poly2) polynome ply_modulo(polynome poly1, polynome poly2) { /* reste de la division de deux polynomes */ - polynome result; + polynome result=NULL; return result; } -- cgit v1.2.3