Refactor expr.c
This commit is contained in:
parent
f6552e3669
commit
abdd0671f7
102
expr.c
102
expr.c
|
@ -23,17 +23,16 @@ static Val match(Val, Val);
|
||||||
static void num(Val);
|
static void num(Val);
|
||||||
static int valcmp(Val, Val);
|
static int valcmp(Val, Val);
|
||||||
static char *valstr(Val, char*, size_t);
|
static char *valstr(Val, char*, size_t);
|
||||||
static int yylex(void);
|
static int lex(char *);
|
||||||
static int yyparse(int);
|
static int parse(char **, int);
|
||||||
|
|
||||||
static char **args;
|
|
||||||
static size_t intlen;
|
static size_t intlen;
|
||||||
static Val yylval;
|
static Val lastval;
|
||||||
|
|
||||||
static void
|
static void
|
||||||
ezero(intmax_t n)
|
ezero(intmax_t n)
|
||||||
{
|
{
|
||||||
if(n == 0)
|
if (n == 0)
|
||||||
enprintf(2, "division by zero\n");
|
enprintf(2, "division by zero\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -159,17 +158,14 @@ valstr(Val val, char *buf, size_t bufsiz)
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
yylex(void)
|
lex(char *p)
|
||||||
{
|
{
|
||||||
intmax_t d;
|
intmax_t d;
|
||||||
char *q, *p, *ops = "|&=><+-*/%():";
|
char *q, *ops = "|&=><+-*/%():";
|
||||||
|
|
||||||
if (!(p = *args++))
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
d = strtoimax(p, &q, 10);
|
d = strtoimax(p, &q, 10);
|
||||||
if (*p && !*q) {
|
if (*p && !*q) {
|
||||||
yylval = (Val){ NULL, d };
|
lastval = (Val){ NULL, d };
|
||||||
return VAL;
|
return VAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -183,17 +179,18 @@ yylex(void)
|
||||||
if (strcmp(p, "!=") == 0)
|
if (strcmp(p, "!=") == 0)
|
||||||
return NE;
|
return NE;
|
||||||
|
|
||||||
yylval = (Val){ p, 0 };
|
lastval = (Val){ p, 0 };
|
||||||
return VAL;
|
return VAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
yyparse(int argc)
|
parse(char **expr, int exprlen)
|
||||||
{
|
{
|
||||||
Val vals[argc], *vtop = vals;
|
Val val[exprlen], *valp = val;
|
||||||
int ops [argc], *otop = ops;
|
int op[exprlen], *opp = op;
|
||||||
int type, last = 0;
|
int i, type, lasttype = 0;
|
||||||
char prec[] = {
|
char prec[] = {
|
||||||
|
[ 0 ] = 0, [VAL] = 0,
|
||||||
['|'] = 1,
|
['|'] = 1,
|
||||||
['&'] = 2,
|
['&'] = 2,
|
||||||
['='] = 3, ['>'] = 3, [GE] = 3, ['<'] = 3, [LE] = 3, [NE] = 3,
|
['='] = 3, ['>'] = 3, [GE] = 3, ['<'] = 3, [LE] = 3, [NE] = 3,
|
||||||
|
@ -202,55 +199,70 @@ yyparse(int argc)
|
||||||
[':'] = 6,
|
[':'] = 6,
|
||||||
};
|
};
|
||||||
|
|
||||||
while((type = yylex())) {
|
for (i = 0; i < exprlen; i++) {
|
||||||
|
type = lex(expr[i]);
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case VAL: *vtop++ = yylval; break;
|
case VAL:
|
||||||
case '(': *otop++ = '(' ; break;
|
*valp++ = lastval;
|
||||||
|
break;
|
||||||
|
case '(':
|
||||||
|
*opp++ = '(';
|
||||||
|
break;
|
||||||
case ')':
|
case ')':
|
||||||
if (last == '(')
|
if (lasttype == '(')
|
||||||
enprintf(2, "syntax error: empty ( )\n");
|
enprintf(2, "syntax error: empty ( )\n");
|
||||||
while(otop > ops && otop[-1] != '(')
|
while (opp > op && opp[-1] != '(')
|
||||||
doop(ops, &otop, vals, &vtop);
|
doop(op, &opp, val, &valp);
|
||||||
if (otop == ops)
|
if (opp == op)
|
||||||
enprintf(2, "syntax error: extra )\n");
|
enprintf(2, "syntax error: extra )\n");
|
||||||
otop--;
|
opp--;
|
||||||
break;
|
break;
|
||||||
default :
|
default :
|
||||||
if (prec[last])
|
if (prec[lasttype])
|
||||||
enprintf(2, "syntax error: extra operator\n");
|
enprintf(2, "syntax error: extra operator\n");
|
||||||
while (otop > ops && prec[otop[-1]] >= prec[type])
|
while (opp > op && prec[opp[-1]] >= prec[type])
|
||||||
doop(ops, &otop, vals, &vtop);
|
doop(op, &opp, val, &valp);
|
||||||
*otop++ = type;
|
*opp++ = type;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
last = type;
|
lasttype = type;
|
||||||
}
|
}
|
||||||
while(otop > ops)
|
while (opp > op)
|
||||||
doop(ops, &otop, vals, &vtop);
|
doop(op, &opp, val, &valp);
|
||||||
|
|
||||||
if (vtop == vals)
|
if (valp == val)
|
||||||
enprintf(2, "syntax error: missing expression\n");
|
enprintf(2, "syntax error: missing expression\n");
|
||||||
if (vtop - vals > 1)
|
if (valp - val > 1)
|
||||||
enprintf(2, "syntax error: extra expression\n");
|
enprintf(2, "syntax error: extra expression\n");
|
||||||
|
|
||||||
vtop--;
|
valp--;
|
||||||
if (vtop->s)
|
if (valp->s)
|
||||||
printf("%s\n", vtop->s);
|
printf("%s\n", valp->s);
|
||||||
else
|
else
|
||||||
printf("%"PRIdMAX"\n", vtop->n);
|
printf("%"PRIdMAX"\n", valp->n);
|
||||||
|
|
||||||
return (vtop->s && *vtop->s) || vtop->n;
|
return (valp->s && *valp->s) || valp->n;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
usage(void)
|
||||||
|
{
|
||||||
|
eprintf("usage: %s EXPRESSION\n", argv0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char **argv)
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
if (!(intlen = snprintf(NULL, 0, "%"PRIdMAX, INTMAX_MIN) + 1))
|
intmax_t n = INTMAX_MIN;
|
||||||
enprintf(3, "failed to get max digits\n");
|
|
||||||
|
|
||||||
args = argv + 1;
|
/* Get the maximum number of digits (+ sign) */
|
||||||
if (*args && !strcmp("--", *args))
|
for (intlen = (n < 0); n; n /= 10, ++intlen);
|
||||||
++args;
|
|
||||||
|
|
||||||
return !yyparse(argc);
|
ARGBEGIN {
|
||||||
|
default:
|
||||||
|
usage();
|
||||||
|
} ARGEND;
|
||||||
|
|
||||||
|
return !parse(argv, argc);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user