X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Flocale%2Fpleval.c;h=d60058da6c3711a3ceaed85f89afbb2f442f1fb5;hb=e3bc22f1eff87b8f029a6ab31f1a269d69e4b053;hp=47aefc340ec7689d0c9d362ff16749fcd5f4643b;hpb=73d2a3bfda462eebe8291eb788ef8be567a9add8;p=musl diff --git a/src/locale/pleval.c b/src/locale/pleval.c index 47aefc34..d60058da 100644 --- a/src/locale/pleval.c +++ b/src/locale/pleval.c @@ -11,20 +11,21 @@ And = Eq | And '&&' Eq Eq = Rel | Eq '==' Rel | Eq '!=' Rel Rel = Add | Rel '<=' Add | Rel '>=' Add | Rel '<' Add | Rel '>' Add Add = Mul | Add '+' Mul | Add '-' Mul -Mul = Term | Mul '*' Term | Mul '/' Term | Mul '%' Term -Term = '(' Expr ')' | '!' Term | decimal | 'n' +Mul = Prim | Mul '*' Prim | Mul '/' Prim | Mul '%' Prim +Prim = '(' Expr ')' | '!' Prim | decimal | 'n' internals: recursive descent expression evaluator with stack depth limit. -eval* functions return the value of the subexpression and set -the current string pointer to the next non-space char. +for binary operators an operator-precedence parser is used. +eval* functions store the result of the parsed subexpression +and return a pointer to the next non-space character. */ struct st { - const char *s; + unsigned long r; unsigned long n; - int err; + int op; }; static const char *skipspace(const char *s) @@ -33,165 +34,124 @@ static const char *skipspace(const char *s) return s; } -static unsigned long evalconst(struct st *st) -{ - char *e; - unsigned long n; - n = strtoul(st->s, &e, 10); - if (!isdigit(*st->s) || e == st->s || n == -1) - st->err = 1; - st->s = skipspace(e); - return n; -} +static const char *evalexpr(struct st *st, const char *s, int d); -static unsigned long evalexpr(struct st *st, int d); - -static unsigned long evalterm(struct st *st, int d) +static const char *evalprim(struct st *st, const char *s, int d) { - unsigned long a; - if (d <= 0) { - st->err = 1; - return 0; - } - st->s = skipspace(st->s); - if (*st->s == '!') { - st->s++; - return !evalterm(st, d-1); - } - if (*st->s == '(') { - st->s++; - a = evalexpr(st, d-1); - if (*st->s != ')') { - st->err = 1; - return 0; - } - st->s = skipspace(st->s + 1); - return a; - } - if (*st->s == 'n') { - st->s = skipspace(st->s + 1); - return st->n; + char *e; + if (--d < 0) return ""; + s = skipspace(s); + if (isdigit(*s)) { + st->r = strtoul(s, &e, 10); + if (e == s || st->r == -1) return ""; + return skipspace(e); } - return evalconst(st); -} - -static unsigned long evalmul(struct st *st, int d) -{ - unsigned long b, a = evalterm(st, d-1); - int op; - for (;;) { - op = *st->s; - if (op != '*' && op != '/' && op != '%') - return a; - st->s++; - b = evalterm(st, d-1); - if (op == '*') { - a *= b; - } else if (!b) { - st->err = 1; - return 0; - } else if (op == '%') { - a %= b; - } else { - a /= b; - } + if (*s == 'n') { + st->r = st->n; + return skipspace(s+1); } -} - -static unsigned long evaladd(struct st *st, int d) -{ - unsigned long a = 0; - int add = 1; - for (;;) { - a += (add?1:-1) * evalmul(st, d-1); - if (*st->s != '+' && *st->s != '-') - return a; - add = *st->s == '+'; - st->s++; + if (*s == '(') { + s = evalexpr(st, s+1, d); + if (*s != ')') return ""; + return skipspace(s+1); } -} - -static unsigned long evalrel(struct st *st, int d) -{ - unsigned long b, a = evaladd(st, d-1); - int less, eq; - for (;;) { - if (*st->s != '<' && *st->s != '>') - return a; - less = st->s[0] == '<'; - eq = st->s[1] == '='; - st->s += 1 + eq; - b = evaladd(st, d-1); - a = (less ? a < b : a > b) || (eq && a == b); + if (*s == '!') { + s = evalprim(st, s+1, d); + st->r = !st->r; + return s; } + return ""; } -static unsigned long evaleq(struct st *st, int d) +static int binop(struct st *st, int op, unsigned long left) { - unsigned long a = evalrel(st, d-1); - int neg; - for (;;) { - if ((st->s[0] != '=' && st->s[0] != '!') || st->s[1] != '=') - return a; - neg = st->s[0] == '!'; - st->s += 2; - a = evalrel(st, d-1) == a; - a ^= neg; + unsigned long a = left, b = st->r; + switch (op) { + case 0: st->r = a||b; return 0; + case 1: st->r = a&&b; return 0; + case 2: st->r = a==b; return 0; + case 3: st->r = a!=b; return 0; + case 4: st->r = a>=b; return 0; + case 5: st->r = a<=b; return 0; + case 6: st->r = a>b; return 0; + case 7: st->r = ar = a+b; return 0; + case 9: st->r = a-b; return 0; + case 10: st->r = a*b; return 0; + case 11: if (b) {st->r = a%b; return 0;} return 1; + case 12: if (b) {st->r = a/b; return 0;} return 1; } + return 1; } -static unsigned long evaland(struct st *st, int d) +static const char *parseop(struct st *st, const char *s) { - unsigned long a = evaleq(st, d-1); - for (;;) { - if (st->s[0] != '&' || st->s[1] != '&') - return a; - st->s += 2; - a = evaleq(st, d-1) && a; - } + static const char opch[11] = "|&=!><+-*%/"; + static const char opch2[6] = "|&===="; + int i; + for (i=0; i<11; i++) + if (*s == opch[i]) { + /* note: >,< are accepted with or without = */ + if (i<6 && s[1] == opch2[i]) { + st->op = i; + return s+2; + } + if (i>=4) { + st->op = i+2; + return s+1; + } + break; + } + st->op = 13; + return s; } -static unsigned long evalor(struct st *st, int d) +static const char *evalbinop(struct st *st, const char *s, int minprec, int d) { - unsigned long a = evaland(st, d-1); + static const char prec[14] = {1,2,3,3,4,4,4,4,5,5,6,6,6,0}; + unsigned long left; + int op; + d--; + s = evalprim(st, s, d); + s = parseop(st, s); for (;;) { - if (st->s[0] != '|' || st->s[1] != '|') - return a; - st->s += 2; - a = evaland(st, d-1) || a; + /* + st->r (left hand side value) and st->op are now set, + get the right hand side or back out if op has low prec, + if op was missing then prec[op]==0 + */ + op = st->op; + if (prec[op] <= minprec) + return s; + left = st->r; + s = evalbinop(st, s, prec[op], d); + if (binop(st, op, left)) + return ""; } } -static unsigned long evalexpr(struct st *st, int d) +static const char *evalexpr(struct st *st, const char *s, int d) { - unsigned long a1, a2, a3; - if (d <= 0) { - st->err = 1; - return 0; - } - a1 = evalor(st, d-1); - if (*st->s != '?') - return a1; - st->s++; - a2 = evalexpr(st, d-1); - if (*st->s != ':') { - st->err = 1; - return 0; - } - st->s++; - a3 = evalexpr(st, d-1); - return a1 ? a2 : a3; + unsigned long a, b; + if (--d < 0) + return ""; + s = evalbinop(st, s, 0, d); + if (*s != '?') + return s; + a = st->r; + s = evalexpr(st, s+1, d); + if (*s != ':') + return ""; + b = st->r; + s = evalexpr(st, s+1, d); + st->r = a ? b : st->r; + return s; } unsigned long __pleval(const char *s, unsigned long n) { - unsigned long a; struct st st; - st.s = s; st.n = n; - st.err = 0; - a = evalexpr(&st, 100); - if (st.err || *st.s != ';') - return -1; - return a; + s = evalexpr(&st, s, 100); + return *s == ';' ? st.r : -1; }