From: short <> Date: Mon, 9 Jul 2001 23:04:21 +0000 (+0000) Subject: Improved version, originally named "ev2.c" X-Git-Tag: rh71~79 X-Git-Url: http://git.jankratochvil.net/?p=nethome.git;a=commitdiff_plain;h=620a419f5f5e2e8f5fe9888ba375006bc838083e Improved version, originally named "ev2.c" --- diff --git a/src/ev.c b/src/ev.c index f8f8d95..0ce6142 100644 --- a/src/ev.c +++ b/src/ev.c @@ -3,7 +3,11 @@ #include #include -#define BUFSIZE 100 +#define NDEBUG + +#define BUFSIZE 1024 + +#define bail(arg...) do { fprintf(stderr, ##arg); exit(EXIT_FAILURE); } while (0) char *arg; @@ -11,6 +15,23 @@ int istack[BUFSIZE]; char opstack[BUFSIZE]; int istackp,opstackp; +#ifdef NDEBUG +#define dump(x) +#else +static void dump(char *pref) +{ +int i; + fprintf(stderr,"%s: i[",pref); + for (i=0;i=BUFSIZE) bail("Too many operators (%d is max)!\n",BUFSIZE); opstack[opstackp++]=op; } static void process(void) { -char lastop=0,op; +char lastop,op; int curi; istackp=opstackp=0; - for (;;) switch ((op=lex(&curi))) { + for (lastop=0;;lastop=op) switch (op=lex(&curi)) { case 'n': - if (lastop=='n') { - fprintf(stderr,"Two successive numbers not permitted!\n"); - exit(EXIT_FAILURE); - } - assert(istackp=BUFSIZE) bail("Too many operators (%d is max)!\n",BUFSIZE); istack[istackp++]=curi; break; case '(': - if (lastop=='n') { - fprintf(stderr,"Open-bracket not permitted after number!\n"); - exit(EXIT_FAILURE); - } + if (lastop=='n') bail("Open-bracket not permitted after number!\n"); pushop(op); break; case '+': case '-': case '*': case '/': case ')': pushop(op); break; case 0: pushop(op); - if (istackp!=1||opstackp) { - fprintf(stderr,"Machine not in sane state on the end of expression, ints=%d,ops=%d!\n",istackp,opstackp); - exit(EXIT_FAILURE); - } + if (istackp!=1||opstackp) + bail("Machine not in sane state on the end of expression, ints=%d,ops=%d!\n",istackp,opstackp); printf("%d\n",istack[0]); return; default: assert(0);