2014-11-16 07:57:40 -05:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2014-11-13 18:07:15 -05:00
|
|
|
#include <inttypes.h>
|
2014-11-16 09:19:50 -05:00
|
|
|
#include <stdio.h>
|
2014-09-29 08:43:41 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-11-16 09:19:50 -05:00
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
#include "util.h"
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
enum {
|
|
|
|
VAL = CHAR_MAX + 1, GE, LE, NE
|
2014-09-29 08:43:41 -04:00
|
|
|
};
|
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
typedef struct {
|
2014-11-16 09:19:50 -05:00
|
|
|
char *s;
|
2014-11-13 18:07:15 -05:00
|
|
|
intmax_t n;
|
|
|
|
} Val;
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-17 09:39:57 -05:00
|
|
|
static void enan(Val);
|
|
|
|
static void ezero(intmax_t);
|
2014-11-13 18:07:15 -05:00
|
|
|
static void doop(int*, int**, Val*, Val**);
|
|
|
|
static Val match(Val, Val);
|
|
|
|
static int valcmp(Val, Val);
|
2014-11-17 09:39:57 -05:00
|
|
|
static char *valstr(Val, char *, size_t);
|
2014-11-16 14:29:29 -05:00
|
|
|
static int lex(char *);
|
|
|
|
static int parse(char **, int);
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
static size_t intlen;
|
2014-11-16 14:29:29 -05:00
|
|
|
static Val lastval;
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-17 09:39:57 -05:00
|
|
|
static void
|
|
|
|
enan(Val v)
|
|
|
|
{
|
|
|
|
if (v.s)
|
|
|
|
enprintf(2, "syntax error: expected integer got `%s'\n", v.s);
|
|
|
|
}
|
|
|
|
|
2014-11-16 09:19:50 -05:00
|
|
|
static void
|
|
|
|
ezero(intmax_t n)
|
|
|
|
{
|
2014-11-16 14:29:29 -05:00
|
|
|
if (n == 0)
|
2014-11-16 09:19:50 -05:00
|
|
|
enprintf(2, "division by zero\n");
|
|
|
|
}
|
|
|
|
|
2014-10-16 07:29:02 -04:00
|
|
|
static void
|
2014-11-17 09:39:57 -05:00
|
|
|
doop(int *op, int **opp, Val *val, Val **valp)
|
2014-09-29 08:43:41 -04:00
|
|
|
{
|
2014-11-13 18:07:15 -05:00
|
|
|
Val ret, a, b;
|
2014-11-17 09:39:57 -05:00
|
|
|
int o;
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-17 09:39:57 -05:00
|
|
|
/* For an operation, we need a valid operator
|
|
|
|
* and two values on the stack */
|
|
|
|
if ((*opp)[-1] == '(')
|
2014-11-13 18:07:15 -05:00
|
|
|
enprintf(2, "syntax error: extra (\n");
|
2014-11-17 09:39:57 -05:00
|
|
|
if (*valp - val < 2)
|
2014-11-13 18:07:15 -05:00
|
|
|
enprintf(2, "syntax error: missing expression or extra operator\n");
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-17 09:39:57 -05:00
|
|
|
a = (*valp)[-2];
|
|
|
|
b = (*valp)[-1];
|
|
|
|
o = (*opp)[-1];
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-17 09:39:57 -05:00
|
|
|
switch (o) {
|
2014-11-16 09:19:50 -05:00
|
|
|
case '|':
|
2014-11-17 09:39:57 -05:00
|
|
|
if (a.s && *a.s) {
|
2014-11-16 09:19:50 -05:00
|
|
|
ret = (Val){ a.s, 0 };
|
2014-11-17 09:39:57 -05:00
|
|
|
} else if (!a.s && a.n) {
|
2014-11-16 09:19:50 -05:00
|
|
|
ret = (Val){ NULL, a.n };
|
2014-11-17 09:39:57 -05:00
|
|
|
} else if (b.s && *b.s) {
|
2014-11-16 09:19:50 -05:00
|
|
|
ret = (Val){ b.s, 0 };
|
2014-11-17 09:39:57 -05:00
|
|
|
} else {
|
2014-11-16 09:19:50 -05:00
|
|
|
ret = (Val){ NULL, b.n };
|
2014-11-17 09:39:57 -05:00
|
|
|
}
|
2014-11-16 09:19:50 -05:00
|
|
|
break;
|
|
|
|
case '&':
|
|
|
|
if (((a.s && *a.s) || a.n) &&
|
2014-11-17 09:39:57 -05:00
|
|
|
((b.s && *b.s) || b.n)) {
|
2014-11-16 09:19:50 -05:00
|
|
|
ret = a;
|
2014-11-17 09:39:57 -05:00
|
|
|
} else {
|
2014-11-16 09:19:50 -05:00
|
|
|
ret = (Val){ NULL, 0 };
|
2014-11-17 09:39:57 -05:00
|
|
|
}
|
2014-11-16 09:19:50 -05:00
|
|
|
break;
|
|
|
|
case '=': ret = (Val){ NULL, valcmp(a, b) == 0 }; break;
|
|
|
|
case '>': ret = (Val){ NULL, valcmp(a, b) > 0 }; break;
|
2015-02-20 07:45:57 -05:00
|
|
|
case GE: ret = (Val){ NULL, valcmp(a, b) >= 0 }; break;
|
2014-11-16 09:19:50 -05:00
|
|
|
case '<': ret = (Val){ NULL, valcmp(a, b) < 0 }; break;
|
2015-02-20 07:45:57 -05:00
|
|
|
case LE: ret = (Val){ NULL, valcmp(a, b) <= 0 }; break;
|
|
|
|
case NE: ret = (Val){ NULL, valcmp(a, b) != 0 }; break;
|
2014-11-16 09:19:50 -05:00
|
|
|
|
2014-11-17 09:39:57 -05:00
|
|
|
case '+': enan(a); enan(b); ret = (Val){ NULL, a.n + b.n }; break;
|
|
|
|
case '-': enan(a); enan(b); ret = (Val){ NULL, a.n - b.n }; break;
|
|
|
|
case '*': enan(a); enan(b); ret = (Val){ NULL, a.n * b.n }; break;
|
|
|
|
case '/': enan(a); enan(b); ezero(b.n); ret = (Val){ NULL, a.n / b.n }; break;
|
|
|
|
case '%': enan(a); enan(b); ezero(b.n); ret = (Val){ NULL, a.n % b.n }; break;
|
2014-11-16 09:19:50 -05:00
|
|
|
|
|
|
|
case ':': ret = match(a, b); break;
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|
|
|
|
|
2014-11-17 09:39:57 -05:00
|
|
|
(*valp)[-2] = ret;
|
|
|
|
(*opp)--;
|
|
|
|
(*valp)--;
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
static Val
|
|
|
|
match(Val vstr, Val vregx)
|
2014-09-29 08:43:41 -04:00
|
|
|
{
|
2014-11-16 09:19:50 -05:00
|
|
|
intmax_t d;
|
2014-11-17 09:39:57 -05:00
|
|
|
char *anchreg, *ret, *p;
|
|
|
|
char buf1[intlen], buf2[intlen], *str, *regx;
|
2014-11-16 09:19:50 -05:00
|
|
|
regoff_t len;
|
2014-11-17 09:39:57 -05:00
|
|
|
regex_t re;
|
2014-11-13 18:07:15 -05:00
|
|
|
regmatch_t matches[2];
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-17 09:39:57 -05:00
|
|
|
str = valstr(vstr, buf1, sizeof(buf1));
|
|
|
|
regx = valstr(vregx, buf2, sizeof(buf2));
|
|
|
|
|
2015-02-10 20:08:17 -05:00
|
|
|
anchreg = enmalloc(3, strlen(regx) + 2);
|
2014-11-17 09:39:57 -05:00
|
|
|
snprintf(anchreg, strlen(regx) + 2, "^%s", regx);
|
|
|
|
|
2014-11-16 09:19:50 -05:00
|
|
|
enregcomp(3, &re, anchreg, 0);
|
2014-11-17 09:39:57 -05:00
|
|
|
free(anchreg);
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-17 09:39:57 -05:00
|
|
|
if (regexec(&re, str, 2, matches, 0)) {
|
|
|
|
regfree(&re);
|
2014-11-13 18:07:15 -05:00
|
|
|
return (Val){ (re.re_nsub ? "" : NULL), 0 };
|
2014-11-17 09:39:57 -05:00
|
|
|
}
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-16 09:19:50 -05:00
|
|
|
if (re.re_nsub) {
|
2014-11-17 09:39:57 -05:00
|
|
|
regfree(&re);
|
2014-11-16 09:19:50 -05:00
|
|
|
len = matches[1].rm_eo - matches[1].rm_so + 1;
|
2015-02-10 20:08:17 -05:00
|
|
|
ret = enmalloc(3, len);
|
2014-11-13 18:07:15 -05:00
|
|
|
strlcpy(ret, str + matches[1].rm_so, len);
|
2014-11-16 09:43:10 -05:00
|
|
|
d = strtoimax(ret, &p, 10);
|
2014-11-17 09:39:57 -05:00
|
|
|
if (*ret && !*p) {
|
|
|
|
free(ret);
|
2014-11-13 18:07:15 -05:00
|
|
|
return (Val){ NULL, d };
|
2014-11-17 09:39:57 -05:00
|
|
|
}
|
2014-11-13 18:07:15 -05:00
|
|
|
return (Val){ ret, 0 };
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|
2014-11-17 09:39:57 -05:00
|
|
|
regfree(&re);
|
2014-11-13 18:07:15 -05:00
|
|
|
return (Val){ NULL, matches[0].rm_eo - matches[0].rm_so };
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|
|
|
|
|
2014-11-17 09:39:57 -05:00
|
|
|
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
static int
|
|
|
|
valcmp(Val a, Val b)
|
2014-09-29 08:43:41 -04:00
|
|
|
{
|
2014-11-17 09:39:57 -05:00
|
|
|
char buf1[intlen], buf2[intlen], *astr, *bstr;
|
|
|
|
|
|
|
|
astr = valstr(a, buf1, sizeof(buf1));
|
|
|
|
bstr = valstr(b, buf2, sizeof(buf2));
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-17 09:39:57 -05:00
|
|
|
return strcmp(astr, bstr);
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
static char *
|
2014-11-16 09:19:50 -05:00
|
|
|
valstr(Val val, char *buf, size_t bufsiz)
|
2014-09-29 08:43:41 -04:00
|
|
|
{
|
2014-11-17 09:39:57 -05:00
|
|
|
if (val.s)
|
|
|
|
return val.s;
|
|
|
|
snprintf(buf, bufsiz, "%"PRIdMAX, val.n);
|
|
|
|
return buf;
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
static int
|
2014-11-16 14:29:29 -05:00
|
|
|
lex(char *p)
|
2014-09-29 08:43:41 -04:00
|
|
|
{
|
2014-11-13 18:07:15 -05:00
|
|
|
intmax_t d;
|
2014-11-16 14:29:29 -05:00
|
|
|
char *q, *ops = "|&=><+-*/%():";
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-17 09:39:57 -05:00
|
|
|
/* clean integer */
|
2014-11-13 18:07:15 -05:00
|
|
|
d = strtoimax(p, &q, 10);
|
2014-11-16 09:19:50 -05:00
|
|
|
if (*p && !*q) {
|
2014-11-16 14:29:29 -05:00
|
|
|
lastval = (Val){ NULL, d };
|
2014-11-13 18:07:15 -05:00
|
|
|
return VAL;
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|
|
|
|
|
2014-11-17 09:39:57 -05:00
|
|
|
/* one-char operand */
|
2015-01-25 10:49:46 -05:00
|
|
|
if (*p && !*(p + 1) && strchr(ops, *p))
|
2014-11-13 18:07:15 -05:00
|
|
|
return *p;
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-17 09:39:57 -05:00
|
|
|
/* two-char operand */
|
2015-01-25 10:49:46 -05:00
|
|
|
if (*p && *(p + 1) == '=' && !*(p + 2)) {
|
2014-11-17 09:39:57 -05:00
|
|
|
switch (*p) {
|
|
|
|
case '>':
|
|
|
|
return GE;
|
|
|
|
case '<':
|
|
|
|
return LE;
|
|
|
|
case '!':
|
|
|
|
return NE;
|
|
|
|
}
|
|
|
|
}
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-17 09:39:57 -05:00
|
|
|
/* nothing matched, treat as string */
|
2014-11-16 14:29:29 -05:00
|
|
|
lastval = (Val){ p, 0 };
|
2014-11-13 18:07:15 -05:00
|
|
|
return VAL;
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
static int
|
2014-11-16 14:29:29 -05:00
|
|
|
parse(char **expr, int exprlen)
|
2014-11-13 18:07:15 -05:00
|
|
|
{
|
2014-11-16 14:29:29 -05:00
|
|
|
Val val[exprlen], *valp = val;
|
|
|
|
int op[exprlen], *opp = op;
|
|
|
|
int i, type, lasttype = 0;
|
2014-11-13 18:07:15 -05:00
|
|
|
char prec[] = {
|
2014-11-16 14:29:29 -05:00
|
|
|
[ 0 ] = 0, [VAL] = 0,
|
2014-11-13 18:07:15 -05:00
|
|
|
['|'] = 1,
|
|
|
|
['&'] = 2,
|
|
|
|
['='] = 3, ['>'] = 3, [GE] = 3, ['<'] = 3, [LE] = 3, [NE] = 3,
|
|
|
|
['+'] = 4, ['-'] = 4,
|
|
|
|
['*'] = 5, ['/'] = 5, ['%'] = 5,
|
|
|
|
[':'] = 6,
|
|
|
|
};
|
|
|
|
|
2014-11-16 14:29:29 -05:00
|
|
|
for (i = 0; i < exprlen; i++) {
|
|
|
|
type = lex(expr[i]);
|
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
switch (type) {
|
2014-11-16 14:29:29 -05:00
|
|
|
case VAL:
|
|
|
|
*valp++ = lastval;
|
|
|
|
break;
|
|
|
|
case '(':
|
|
|
|
*opp++ = '(';
|
|
|
|
break;
|
2014-11-16 09:19:50 -05:00
|
|
|
case ')':
|
2014-11-16 14:29:29 -05:00
|
|
|
if (lasttype == '(')
|
2014-11-16 09:19:50 -05:00
|
|
|
enprintf(2, "syntax error: empty ( )\n");
|
2014-11-16 14:29:29 -05:00
|
|
|
while (opp > op && opp[-1] != '(')
|
|
|
|
doop(op, &opp, val, &valp);
|
|
|
|
if (opp == op)
|
2014-11-16 09:19:50 -05:00
|
|
|
enprintf(2, "syntax error: extra )\n");
|
2014-11-16 14:29:29 -05:00
|
|
|
opp--;
|
2014-11-16 09:19:50 -05:00
|
|
|
break;
|
|
|
|
default :
|
2014-11-16 14:29:29 -05:00
|
|
|
if (prec[lasttype])
|
2014-11-16 09:19:50 -05:00
|
|
|
enprintf(2, "syntax error: extra operator\n");
|
2014-11-16 14:29:29 -05:00
|
|
|
while (opp > op && prec[opp[-1]] >= prec[type])
|
|
|
|
doop(op, &opp, val, &valp);
|
|
|
|
*opp++ = type;
|
2014-11-16 09:19:50 -05:00
|
|
|
break;
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|
2014-11-16 14:29:29 -05:00
|
|
|
lasttype = type;
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|
2014-11-16 14:29:29 -05:00
|
|
|
while (opp > op)
|
|
|
|
doop(op, &opp, val, &valp);
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-16 14:29:29 -05:00
|
|
|
if (valp == val)
|
2014-11-13 18:07:15 -05:00
|
|
|
enprintf(2, "syntax error: missing expression\n");
|
2014-11-16 14:29:29 -05:00
|
|
|
if (valp - val > 1)
|
2014-11-13 18:07:15 -05:00
|
|
|
enprintf(2, "syntax error: extra expression\n");
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-16 14:29:29 -05:00
|
|
|
valp--;
|
|
|
|
if (valp->s)
|
|
|
|
printf("%s\n", valp->s);
|
2014-11-16 09:19:50 -05:00
|
|
|
else
|
2014-11-16 14:29:29 -05:00
|
|
|
printf("%"PRIdMAX"\n", valp->n);
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-16 14:29:29 -05:00
|
|
|
return (valp->s && *valp->s) || valp->n;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-01-25 13:31:25 -05:00
|
|
|
eprintf("usage: %s expression\n", argv0);
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-11-16 14:29:29 -05:00
|
|
|
main(int argc, char *argv[])
|
2014-09-29 08:43:41 -04:00
|
|
|
{
|
2014-11-16 14:29:29 -05:00
|
|
|
intmax_t n = INTMAX_MIN;
|
|
|
|
|
|
|
|
/* Get the maximum number of digits (+ sign) */
|
|
|
|
for (intlen = (n < 0); n; n /= 10, ++intlen);
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-16 14:29:29 -05:00
|
|
|
ARGBEGIN {
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-16 14:29:29 -05:00
|
|
|
return !parse(argv, argc);
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|