version 1.4, 1998/04/25 18:47:19 |
version 1.5, 1999/09/14 08:26:10 |
|
|
|
|
#include <sys/cdefs.h> |
#include <sys/cdefs.h> |
#include <stdio.h> |
#include <stdio.h> |
|
#include "mdef.h" |
|
|
/* |
/* |
* expression evaluator: performs a standard recursive |
* expression evaluator: performs a standard recursive |
|
|
* Bob Harper |
* Bob Harper |
*/ |
*/ |
|
|
#define TRUE 1 |
|
#define FALSE 0 |
|
#define EOS (char) 0 |
|
#define EQL 0 |
#define EQL 0 |
#define NEQ 1 |
#define NEQ 1 |
#define LSS 2 |
#define LSS 2 |
|
|
#define GEQ 5 |
#define GEQ 5 |
#define OCTAL 8 |
#define OCTAL 8 |
#define DECIMAL 10 |
#define DECIMAL 10 |
|
#define HEX 16 |
|
|
static char *nxtch; /* Parser scan pointer */ |
static char *nxtch; /* Parser scan pointer */ |
|
|
|
|
register int rval, c, base; |
register int rval, c, base; |
int ndig; |
int ndig; |
|
|
base = ((c = skipws()) == '0') ? OCTAL : DECIMAL; |
|
rval = 0; |
rval = 0; |
ndig = 0; |
ndig = 0; |
while (c >= '0' && c <= (base == OCTAL ? '7' : '9')) { |
c = skipws(); |
rval *= base; |
if (c == '0') { |
rval += (c - '0'); |
c = skipws(); |
|
if (c == 'x' || c == 'X') { |
|
base = HEX; |
|
c = skipws(); |
|
} else { |
|
base = OCTAL; |
|
ndig++; |
|
} |
|
} else |
|
base = DECIMAL; |
|
for(;;) { |
|
switch(c) { |
|
case '8': case '9': |
|
if (base != OCTAL) |
|
goto bad_digit; |
|
/*FALLTHRU*/ |
|
case '0': case '1': case '2': case '3': |
|
case '4': case '5': case '6': case '7': |
|
rval *= base; |
|
rval += c - '0'; |
|
break; |
|
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': |
|
c = tolower(c); |
|
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': |
|
if (base == HEX) { |
|
rval *= base; |
|
rval += c - 'a' + 10; |
|
break; |
|
} |
|
/*FALLTHRU*/ |
|
default: |
|
goto bad_digit; |
|
} |
c = getch(); |
c = getch(); |
ndig++; |
ndig++; |
} |
} |
|
bad_digit: |
ungetch(); |
ungetch(); |
|
|
if (ndig == 0) |
if (ndig == 0) |