mirror of git://git.suckless.org/sbase
283 lines
7.2 KiB
C
283 lines
7.2 KiB
C
/* See LICENSE file for copyright and license details. */
|
|
#include <inttypes.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#include "utf.h"
|
|
#include "util.h"
|
|
|
|
/* token types for lexing/parsing
|
|
* single character operators represent themselves */
|
|
enum {
|
|
VAL = CHAR_MAX + 1, GE, LE, NE
|
|
};
|
|
|
|
struct val {
|
|
char *s; /* iff s is NULL, val is an integer */
|
|
intmax_t n;
|
|
};
|
|
|
|
static size_t intlen;
|
|
|
|
static void
|
|
enan(struct val v)
|
|
{
|
|
if (v.s)
|
|
enprintf(2, "syntax error: expected integer got `%s'\n", v.s);
|
|
}
|
|
|
|
static void
|
|
ezero(intmax_t n)
|
|
{
|
|
if (n == 0)
|
|
enprintf(2, "division by zero\n");
|
|
}
|
|
|
|
static char *
|
|
valstr(struct val val, char *buf, size_t bufsiz)
|
|
{
|
|
if (val.s)
|
|
return val.s;
|
|
snprintf(buf, bufsiz, "%"PRIdMAX, val.n);
|
|
return buf;
|
|
}
|
|
|
|
static int
|
|
valcmp(struct val a, struct val b)
|
|
{
|
|
char buf1[intlen], buf2[intlen];
|
|
char *astr = valstr(a, buf1, sizeof(buf1));
|
|
char *bstr = valstr(b, buf2, sizeof(buf2));
|
|
|
|
if (!a.s && !b.s)
|
|
return (a.n > b.n) - (a.n < b.n);
|
|
return strcmp(astr, bstr);
|
|
}
|
|
|
|
/* match vstr against BRE vregx (treat both values as strings)
|
|
* if there is at least one subexpression \(...\)
|
|
* then return the text matched by it \1 (empty string for no match)
|
|
* else return number of characters matched (0 for no match)
|
|
*/
|
|
static struct val
|
|
match(struct val vstr, struct val vregx)
|
|
{
|
|
regex_t re;
|
|
regmatch_t matches[2];
|
|
intmax_t d;
|
|
char *s, *p, buf1[intlen], buf2[intlen];
|
|
char *str = valstr(vstr, buf1, sizeof(buf1));
|
|
char *regx = valstr(vregx, buf2, sizeof(buf2));;
|
|
char anchreg[strlen(regx) + 2];
|
|
|
|
/* expr(1p) "all patterns are anchored to the beginning of the string" */
|
|
snprintf(anchreg, sizeof(anchreg), "^%s", regx);
|
|
enregcomp(3, &re, anchreg, 0);
|
|
|
|
if (regexec(&re, str, 2, matches, 0)) {
|
|
regfree(&re);
|
|
return (struct val){ (re.re_nsub ? "" : NULL), 0 };
|
|
}
|
|
|
|
if (re.re_nsub) {
|
|
regfree(&re);
|
|
s = str + matches[1].rm_so;
|
|
p = str + matches[1].rm_eo;
|
|
|
|
*p = '\0';
|
|
d = strtoimax(s, &p, 10);
|
|
if (*s && !*p) /* string matched by subexpression is an integer */
|
|
return (struct val){ NULL, d };
|
|
|
|
/* FIXME? string is never free()d, worth fixing?
|
|
* need to allocate as it could be in buf1 instead of vstr.s */
|
|
return (struct val){ enstrdup(3, s), 0 };
|
|
}
|
|
regfree(&re);
|
|
str += matches[0].rm_so;
|
|
return (struct val){ NULL, utfnlen(str, matches[0].rm_eo - matches[0].rm_so) };
|
|
}
|
|
|
|
/* ops points to a stack of operators, opp points to one past the last op
|
|
* vals points to a stack of values , valp points to one past the last val
|
|
* guaranteed that opp != ops
|
|
* ops is unused here, but still included for parity with vals
|
|
* pop operator, pop two values, apply operator, push result
|
|
*/
|
|
static void
|
|
doop(int *ops, int **opp, struct val *vals, struct val **valp)
|
|
{
|
|
struct val ret, a, b;
|
|
int op;
|
|
|
|
/* For an operation, we need a valid operator
|
|
* and two values on the stack */
|
|
if ((*opp)[-1] == '(')
|
|
enprintf(2, "syntax error: extra (\n");
|
|
if (*valp - vals < 2)
|
|
enprintf(2, "syntax error: missing expression or extra operator\n");
|
|
|
|
a = (*valp)[-2];
|
|
b = (*valp)[-1];
|
|
op = (*opp)[-1];
|
|
|
|
switch (op) {
|
|
case '|':
|
|
if ( a.s && *a.s) ret = (struct val){ a.s , 0 };
|
|
else if (!a.s && a.n) ret = (struct val){ NULL, a.n };
|
|
else if ( b.s && *b.s) ret = (struct val){ b.s , 0 };
|
|
else ret = (struct val){ NULL, b.n };
|
|
break;
|
|
case '&':
|
|
if (((a.s && *a.s) || a.n) && ((b.s && *b.s) || b.n))
|
|
ret = a;
|
|
else
|
|
ret = (struct val){ NULL, 0 };
|
|
break;
|
|
|
|
case '=': ret = (struct val){ NULL, valcmp(a, b) == 0 }; break;
|
|
case '>': ret = (struct val){ NULL, valcmp(a, b) > 0 }; break;
|
|
case GE : ret = (struct val){ NULL, valcmp(a, b) >= 0 }; break;
|
|
case '<': ret = (struct val){ NULL, valcmp(a, b) < 0 }; break;
|
|
case LE : ret = (struct val){ NULL, valcmp(a, b) <= 0 }; break;
|
|
case NE : ret = (struct val){ NULL, valcmp(a, b) != 0 }; break;
|
|
|
|
case '+': enan(a); enan(b); ret = (struct val){ NULL, a.n + b.n }; break;
|
|
case '-': enan(a); enan(b); ret = (struct val){ NULL, a.n - b.n }; break;
|
|
case '*': enan(a); enan(b); ret = (struct val){ NULL, a.n * b.n }; break;
|
|
case '/': enan(a); enan(b); ezero(b.n); ret = (struct val){ NULL, a.n / b.n }; break;
|
|
case '%': enan(a); enan(b); ezero(b.n); ret = (struct val){ NULL, a.n % b.n }; break;
|
|
|
|
case ':': ret = match(a, b); break;
|
|
}
|
|
|
|
(*valp)[-2] = ret;
|
|
(*opp)--;
|
|
(*valp)--;
|
|
}
|
|
|
|
/* retrn the type of the next token, s
|
|
* if it is a value, place the value in v for use by parser
|
|
*/
|
|
static int
|
|
lex(char *s, struct val *v)
|
|
{
|
|
intmax_t d;
|
|
char *p, *ops = "|&=><+-*/%():";
|
|
|
|
/* clean integer */
|
|
d = strtoimax(s, &p, 10);
|
|
if (*s && !*p) {
|
|
*v = (struct val){ NULL, d };
|
|
return VAL;
|
|
}
|
|
|
|
/* one-char operand */
|
|
if (*s && !s[1] && strchr(ops, *s))
|
|
return *s;
|
|
|
|
/* two-char operand */
|
|
if (!strcmp(s, ">=")) return GE;
|
|
if (!strcmp(s, "<=")) return LE;
|
|
if (!strcmp(s, "!=")) return NE;
|
|
|
|
/* nothing matched, treat as string */
|
|
*v = (struct val){ s, 0 };
|
|
return VAL;
|
|
}
|
|
|
|
/* using shunting-yard to convert from infix to rpn
|
|
* https://en.wikipedia.org/wiki/Shunting-yard_algorithm
|
|
* instead of creating rpn output to evaluate later, evaluate it immediately as
|
|
* it is created
|
|
* vals is the value stack, valp points to one past last value on the stack
|
|
* ops is the operator stack, opp points to one past last op on the stack
|
|
*/
|
|
static int
|
|
parse(char *expr[], int exprlen)
|
|
{
|
|
struct val vals[exprlen], *valp = vals, v;
|
|
int ops[exprlen], *opp = ops;
|
|
int i, type, lasttype = 0;
|
|
char prec[] = { /* precedence of operators */
|
|
['|'] = 1,
|
|
['&'] = 2,
|
|
['='] = 3, ['>'] = 3, [GE] = 3, ['<'] = 3, [LE] = 3, [NE] = 3,
|
|
['+'] = 4, ['-'] = 4,
|
|
['*'] = 5, ['/'] = 5, ['%'] = 5,
|
|
[':'] = 6,
|
|
};
|
|
|
|
for (i = 0; i < exprlen; i++) {
|
|
switch ((type = lex(expr[i], &v))) {
|
|
case VAL:
|
|
*valp++ = v;
|
|
break;
|
|
case '(':
|
|
*opp++ = '(';
|
|
break;
|
|
case ')':
|
|
if (lasttype == '(')
|
|
enprintf(2, "syntax error: empty ( )\n");
|
|
while (opp > ops && opp[-1] != '(')
|
|
doop(ops, &opp, vals, &valp);
|
|
if (opp == ops)
|
|
enprintf(2, "syntax error: extra )\n");
|
|
opp--;
|
|
break;
|
|
default: /* operator */
|
|
if (prec[lasttype])
|
|
enprintf(2, "syntax error: extra operator\n");
|
|
while (opp > ops && prec[opp[-1]] >= prec[type])
|
|
doop(ops, &opp, vals, &valp);
|
|
*opp++ = type;
|
|
break;
|
|
}
|
|
lasttype = type;
|
|
}
|
|
while (opp > ops)
|
|
doop(ops, &opp, vals, &valp);
|
|
|
|
if (valp == vals)
|
|
enprintf(2, "syntax error: missing expression\n");
|
|
if (--valp != vals)
|
|
enprintf(2, "syntax error: extra expression\n");
|
|
|
|
if (valp->s)
|
|
printf("%s\n", valp->s);
|
|
else
|
|
printf("%"PRIdMAX"\n", valp->n);
|
|
|
|
return (valp->s && *valp->s) || valp->n;
|
|
}
|
|
|
|
/* the only way to get usage() is if the user didn't supply -- and expression
|
|
* begins with a -
|
|
* expr(1p): "... the conforming application must employ the -- construct ...
|
|
* if there is any chance the first operand might be a negative integer (or any
|
|
* string with a leading minus"
|
|
*/
|
|
static void
|
|
usage(void)
|
|
{
|
|
enprintf(3, "usage: %s [--] expression\n"
|
|
"note : the -- is mandatory if expression begins with a -\n", argv0);
|
|
}
|
|
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
intmax_t n = INTMAX_MIN;
|
|
|
|
/* Get the maximum number of digits (+ sign) */
|
|
for (intlen = (n < 0); n; n /= 10, ++intlen)
|
|
;
|
|
|
|
ARGBEGIN {
|
|
default:
|
|
usage();
|
|
} ARGEND;
|
|
|
|
return !parse(argv, argc);
|
|
}
|