summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbiouman <biouman>2001-05-01 11:27:52 +0000
committerbiouman <biouman>2001-05-01 11:27:52 +0000
commit655f3b2663b9909656b896cf47ad949b4e9644a8 (patch)
treea9e775e6ea01fbf2140176499d4936a099946e32
parent6b8f02426a4dd9358c7b35d34268db358fd113c3 (diff)
*** empty log message ***
-rw-r--r--include/pile.h2
-rw-r--r--lib/exceptions.c35
-rw-r--r--lib/hash.c2
-rw-r--r--lib/parser.c20
-rw-r--r--lib/pile.c35
-rw-r--r--lib/polynom.c2
-rw-r--r--lib/scalaires.c2
-rw-r--r--src/Polynom.c6
8 files changed, 73 insertions, 31 deletions
diff --git a/include/pile.h b/include/pile.h
index 6130357..d2e3176 100644
--- a/include/pile.h
+++ b/include/pile.h
@@ -27,6 +27,8 @@ void push_pile_string(char *st);
pile_elem pop_pile(unsigned int count);
+void flush_pile(void);
+
char *affichage_level_1(void);
int is_mute(char *st);
diff --git a/lib/exceptions.c b/lib/exceptions.c
index 0fc363e..e59013b 100644
--- a/lib/exceptions.c
+++ b/lib/exceptions.c
@@ -13,6 +13,7 @@
#define _(x) x
#endif
#include "exceptions.h"
+#include "pile.h"
char *contexts[128];
int clevel = 0;
@@ -23,7 +24,7 @@ char *Estrdup(char *o)
if (o) {
if (!(r = strdup(o))) {
- exception(1, _("Out of memory."));
+ exception(2, _("Out of memory."));
}
} else {
return NULL;
@@ -37,7 +38,7 @@ void *Emalloc(size_t s)
if (s) {
if (!(r = malloc(s))) {
- exception(1, _("Out of memory."));
+ exception(2, _("Out of memory."));
}
} else {
return NULL;
@@ -48,7 +49,7 @@ void *Emalloc(size_t s)
void pushcontext(char *c)
{
if (clevel == 128) {
- exception(1, _("Too much error contexts during pushcontext()."));
+ exception(2, _("Too much error contexts during pushcontext()."));
}
contexts[clevel++] = Estrdup(c);
#ifdef DEBUG
@@ -59,7 +60,7 @@ void pushcontext(char *c)
void popcontext(void)
{
if (clevel == 0) {
- exception(1, _("Error context empty, but popcontext() called."));
+ exception(2, _("Error context empty, but popcontext() called."));
}
free(contexts[--clevel]);
}
@@ -71,14 +72,24 @@ void flushcontext(void)
}
}
-void exception(int level, char *msg)
+/* 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)
{
int i;
-
- fprintf(stderr, "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);
- exit(level);
+ switch (level) {
+ case 1:
+ fprintf(stderr, " Error description: %s\n", msg);
+ flush_pile();
+ break;
+ default:
+ fprintf(stderr, "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);
+ exit(1);
+ break;
+ }
}
diff --git a/lib/hash.c b/lib/hash.c
index 4658aed..f1cc597 100644
--- a/lib/hash.c
+++ b/lib/hash.c
@@ -24,7 +24,7 @@ static int FonctionHachage(char *clef)
unsigned int i;
if (!clef) {
- exception(1, _("Internal error into hashing"));
+ exception(2, _("Internal error into hashing"));
}
for (i = 0; i < strlen(CHAINEHACHAGE); i++) {
diff --git a/lib/parser.c b/lib/parser.c
index 60e3dca..6d608b4 100644
--- a/lib/parser.c
+++ b/lib/parser.c
@@ -96,7 +96,7 @@ 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"));
+ exception(2, _("Too many nested operators in expression.\n"));
}
@@ -127,7 +127,7 @@ 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"));
+ exception(2, _("Too many nested functions calls in expression.\n"));
}
/* Cette fonction lit un "mot" sur la chaine line et renvoit le nouveau pointeur */
@@ -204,9 +204,9 @@ void parse_line(char *line)
}
if (get_pri(buffer[0]) == -1) {
if (got_unary) {
- exception(-1, _("Invalid unary operator"));
+ exception(1, _("Invalid unary operator"));
} else {
- exception(-1, _("Invalid binary operator"));
+ exception(1, _("Invalid binary operator"));
}
}
while (get_pri(get_last_op()) >= get_pri(buffer[0])
@@ -232,7 +232,7 @@ void parse_line(char *line)
while (pileop_pos) {
op = pop_op();
if (op == '(')
- exception(-1, _("Parse error: too much left parenthesis"));
+ exception(1, _("Parse error: too much left parenthesis"));
act_pile(get_func(op));
}
popcontext();
@@ -242,12 +242,12 @@ void parse_line(char *line)
/* Fin de parenthese (Appel de fonction ou expression mathématique) */
while (1) {
if (!pileop_pos)
- exception(-1, _("Parse error: too much right parenthesis"));
+ exception(1, _("Parse error: too much right parenthesis"));
op = pop_op();
if (((op & 127) == '('))
break;
if (((op & 127) == '['))
- exception(-1, _("Parse error: enclosure mismatch"));
+ exception(1, _("Parse error: enclosure mismatch"));
act_pile(get_func(op));
}
if (op == '(') {
@@ -261,12 +261,12 @@ void parse_line(char *line)
/* Fin d'opérateur de décalage */
while (1) {
if (!pileop_pos)
- exception(-1, _("Parse error: too much right parenthesis"));
+ exception(1, _("Parse error: too much right parenthesis"));
op = pop_op();
if (((op & 127) == '['))
break;
if (((op & 127) == '('))
- exception(-1, _("Parse error: enclosure mismatch"));
+ exception(1, _("Parse error: enclosure mismatch"));
act_pile(get_func(op));
}
act_pile(get_func(op));
@@ -285,7 +285,7 @@ void parse_line(char *line)
if (!get_last_call())
increment_call();
} else if (buffer[0]) {
- exception(-1, _("Invalid character"));
+ exception(1, _("Invalid character"));
}
popcontext();
}
diff --git a/lib/pile.c b/lib/pile.c
index 5b98ef4..a7d8d47 100644
--- a/lib/pile.c
+++ b/lib/pile.c
@@ -73,7 +73,7 @@ void push_pile_poly(polynome poly)
pile[pile_ptr].poly = poly;
pile_ptr++;
} else {
- exception(1, _("push_pile_poly: Stack Overflow"));
+ exception(2, _("push_pile_poly: Stack Overflow"));
}
}
@@ -85,7 +85,7 @@ void push_pile_int(int val)
pile[pile_ptr].val = val;
pile_ptr++;
} else {
- exception(1, _("push_pile_int: Stack Overflow"));
+ exception(2, _("push_pile_int: Stack Overflow"));
}
@@ -98,7 +98,7 @@ void push_pile_string(char *st)
pile[pile_ptr].label = Estrdup(st);
pile_ptr++;
} else {
- exception(1, _("push_pile_string: Stack Overflow"));
+ exception(2, _("push_pile_string: Stack Overflow"));
}
@@ -112,11 +112,38 @@ pile_elem pop_pile(unsigned int count)
pile_ptr -= count;
} else {
sprintf(buf, _("pop_pile: Can't pop %u elements"), count);
- exception(1, buf);
+ exception(2, buf);
}
return pile[pile_ptr];
}
+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;
+ }
+ }
+ }
+ pile_ptr=0;
+}
+
+
+
+
+
+
+
char *affichage_level_1(void)
{
char *result = NULL;
diff --git a/lib/polynom.c b/lib/polynom.c
index a50d0cb..241c2da 100644
--- a/lib/polynom.c
+++ b/lib/polynom.c
@@ -288,7 +288,7 @@ char *ply_affichage(polynome poly)
if (count < BUFSIZ)
strcat(buf, temp);
else
- exception(1, _("ply_affichage: strcat error, not enoug space in buffer"));
+ exception(2, _("ply_affichage: strcat error, not enoug space in buffer"));
poly = poly->suiv;
}
return Estrdup(buf);
diff --git a/lib/scalaires.c b/lib/scalaires.c
index a58b627..ab97084 100644
--- a/lib/scalaires.c
+++ b/lib/scalaires.c
@@ -109,7 +109,7 @@ rationnel rat_multiplication(rationnel rat1, rationnel rat2)
rationnel rat_division(rationnel rat1, rationnel rat2)
{ /* division */
if (!rat2.num)
- exception(1,_("division by zero"));
+ exception(2,_("division by zero"));
return rat_constr(rat1.num * rat2.denom, rat1.denom * rat2.num);
}
diff --git a/src/Polynom.c b/src/Polynom.c
index 2db428f..6121f7d 100644
--- a/src/Polynom.c
+++ b/src/Polynom.c
@@ -44,9 +44,11 @@ int main(void)
parse_line("soja;");
parse_line("soja^3;");
//parse_line("quake4@%*)+vo;i");
- //fprintf(stderr, "soja");
- AfficheTableau(variables);
printf("Resultat: %s\n", affichage_level_1());
+ flush_pile();
+ parse_line("0x52;");
+ affichage_pile();
+ AfficheTableau(variables);
return 0;