1N/A/***********************************************************************
1N/A* *
1N/A* This software is part of the ast package *
1N/A* Copyright (c) 1985-2011 AT&T Intellectual Property *
1N/A* and is licensed under the *
1N/A* Common Public License, Version 1.0 *
1N/A* by AT&T Intellectual Property *
1N/A* *
1N/A* A copy of the License is available at *
1N/A* http://www.opensource.org/licenses/cpl1.0.txt *
1N/A* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
1N/A* *
1N/A* Information and Software Systems Research *
1N/A* AT&T Research *
1N/A* Florham Park NJ *
1N/A* *
1N/A* Glenn Fowler <gsf@research.att.com> *
1N/A* David Korn <dgk@research.att.com> *
1N/A* Phong Vo <kpv@research.att.com> *
1N/A* *
1N/A***********************************************************************/
1N/A#pragma prototyped
1N/A/*
1N/A * G. S. Fowler
1N/A * D. G. Korn
1N/A * AT&T Bell Laboratories
1N/A *
1N/A * long integer arithmetic expression evaluator
1N/A * long constants may be represented as:
1N/A *
1N/A * 0ooo octal
1N/A * 0[xX]hhh hexadecimal
1N/A * ddd decimal
1N/A * n#ccc base n, 2 <= b <= 36
1N/A *
1N/A * NOTE: all operands are evaluated as both the parse
1N/A * and evaluation are done on the fly
1N/A */
1N/A
1N/A#include <ast.h>
1N/A#include <ctype.h>
1N/A
1N/A#define getchr(ex) (*(ex)->nextchr++)
1N/A#define peekchr(ex) (*(ex)->nextchr)
1N/A#define ungetchr(ex) ((ex)->nextchr--)
1N/A
1N/A#define error(ex,msg) return(seterror(ex,msg))
1N/A
1N/Atypedef struct /* expression handle */
1N/A{
1N/A char* nextchr; /* next expression char */
1N/A char* errchr; /* next char after error */
1N/A char* errmsg; /* error message text */
1N/A long (*convert)(const char*, char**, void*);
1N/A void* handle; /* user convert handle */
1N/A} Expr_t;
1N/A
1N/A/*
1N/A * set error message string
1N/A */
1N/A
1N/Astatic long
1N/Aseterror(register Expr_t* ex, char* msg)
1N/A{
1N/A if (!ex->errmsg) ex->errmsg = msg;
1N/A ex->errchr = ex->nextchr;
1N/A ex->nextchr = "";
1N/A return(0);
1N/A}
1N/A
1N/A/*
1N/A * evaluate a subexpression with precedence
1N/A */
1N/A
1N/Astatic long
1N/Aexpr(register Expr_t* ex, register int precedence)
1N/A{
1N/A register int c;
1N/A register long n;
1N/A register long x;
1N/A char* pos;
1N/A int operand = 1;
1N/A
1N/A while (c = getchr(ex), isspace(c));
1N/A switch (c)
1N/A {
1N/A case 0:
1N/A ungetchr(ex);
1N/A if (!precedence) return(0);
1N/A error(ex, "more tokens expected");
1N/A case '-':
1N/A n = -expr(ex, 13);
1N/A break;
1N/A case '+':
1N/A n = expr(ex, 13);
1N/A break;
1N/A case '!':
1N/A n = !expr(ex, 13);
1N/A break;
1N/A case '~':
1N/A n = ~expr(ex, 13);
1N/A break;
1N/A default:
1N/A ungetchr(ex);
1N/A n = 0;
1N/A operand = 0;
1N/A break;
1N/A }
1N/A for (;;)
1N/A {
1N/A switch (c = getchr(ex))
1N/A {
1N/A case 0:
1N/A goto done;
1N/A case ')':
1N/A if (!precedence) error(ex, "too many )'s");
1N/A goto done;
1N/A case '(':
1N/A n = expr(ex, 1);
1N/A if (getchr(ex) != ')')
1N/A {
1N/A ungetchr(ex);
1N/A error(ex, "closing ) expected");
1N/A }
1N/A gotoperand:
1N/A if (operand) error(ex, "operator expected");
1N/A operand = 1;
1N/A continue;
1N/A case '?':
1N/A if (precedence > 1) goto done;
1N/A if (peekchr(ex) == ':')
1N/A {
1N/A getchr(ex);
1N/A x = expr(ex, 2);
1N/A if (!n) n = x;
1N/A }
1N/A else
1N/A {
1N/A x = expr(ex, 2);
1N/A if (getchr(ex) != ':')
1N/A {
1N/A ungetchr(ex);
1N/A error(ex, ": expected for ? operator");
1N/A }
1N/A if (n)
1N/A {
1N/A n = x;
1N/A expr(ex, 2);
1N/A }
1N/A else n = expr(ex, 2);
1N/A }
1N/A break;
1N/A case ':':
1N/A goto done;
1N/A case '|':
1N/A if (peekchr(ex) == '|')
1N/A {
1N/A if (precedence > 2) goto done;
1N/A getchr(ex);
1N/A x = expr(ex, 3);
1N/A n = n || x;
1N/A }
1N/A else
1N/A {
1N/A if (precedence > 4) goto done;
1N/A x = expr(ex, 5);
1N/A n |= x;
1N/A }
1N/A break;
1N/A case '^':
1N/A if (precedence > 5) goto done;
1N/A x = expr(ex, 6);
1N/A n ^= x;
1N/A break;
1N/A case '&':
1N/A if (peekchr(ex) == '&')
1N/A {
1N/A if (precedence > 3) goto done;
1N/A getchr(ex);
1N/A x = expr(ex, 4);
1N/A n = n && x;
1N/A }
1N/A else
1N/A {
1N/A if (precedence > 6) goto done;
1N/A x = expr(ex, 7);
1N/A n &= x;
1N/A }
1N/A break;
1N/A case '=':
1N/A case '!':
1N/A if (peekchr(ex) != '=') error(ex, "operator syntax error");
1N/A if (precedence > 7) goto done;
1N/A getchr(ex);
1N/A x = expr(ex, 8);
1N/A if (c == '=') n = n == x;
1N/A else n = n != x;
1N/A break;
1N/A case '<':
1N/A case '>':
1N/A if (peekchr(ex) == c)
1N/A {
1N/A if (precedence > 9) goto done;
1N/A getchr(ex);
1N/A x = expr(ex, 10);
1N/A if (c == '<') n <<= x;
1N/A else n >>= x;
1N/A }
1N/A else
1N/A {
1N/A if (precedence > 8) goto done;
1N/A if (peekchr(ex) == '=')
1N/A {
1N/A getchr(ex);
1N/A x = expr(ex, 9);
1N/A if (c == '<') n = n <= x;
1N/A else n = n >= x;
1N/A }
1N/A else
1N/A {
1N/A x = expr(ex, 9);
1N/A if (c == '<') n = n < x;
1N/A else n = n > x;
1N/A }
1N/A }
1N/A break;
1N/A case '+':
1N/A case '-':
1N/A if (precedence > 10) goto done;
1N/A x = expr(ex, 11);
1N/A if (c == '+') n += x;
1N/A else n -= x;
1N/A break;
1N/A case '*':
1N/A case '/':
1N/A case '%':
1N/A if (precedence > 11) goto done;
1N/A x = expr(ex, 12);
1N/A if (c == '*') n *= x;
1N/A else if (x == 0) error(ex, "divide by zero");
1N/A else if (c == '/') n /= x;
1N/A else n %= x;
1N/A break;
1N/A default:
1N/A if (isspace(c)) continue;
1N/A pos = --ex->nextchr;
1N/A if (isdigit(c)) n = strton(ex->nextchr, &ex->nextchr, NiL, 0);
1N/A else if (ex->convert) n = (*ex->convert)(ex->nextchr, &ex->nextchr, ex->handle);
1N/A if (ex->nextchr == pos) error(ex, "syntax error");
1N/A goto gotoperand;
1N/A }
1N/A if (ex->errmsg) return(0);
1N/A if (!operand) error(ex, "operand expected");
1N/A }
1N/A done:
1N/A ungetchr(ex);
1N/A if (!operand) error(ex, "operand expected");
1N/A return(n);
1N/A}
1N/A
1N/A/*
1N/A * evaluate an integer arithmetic expression in s
1N/A *
1N/A * (long)(*convert)(const char* string, char** end, void* handle)
1N/A * is a user supplied conversion routine that is called when unknown
1N/A * chars are encountered; string points to the part to be
1N/A * converted and end is adjusted to point to the next non-converted
1N/A * character; if string is 0 then end points to an error message string
1N/A *
1N/A * NOTE: (*convert)() may call strexpr(ex, )
1N/A */
1N/A
1N/Along
1N/Astrexpr(const char* s, char** end, long(*convert)(const char*, char**, void*), void* handle)
1N/A{
1N/A long n;
1N/A Expr_t ex;
1N/A
1N/A ex.nextchr = (char*)s;
1N/A ex.errmsg = 0;
1N/A ex.convert = convert;
1N/A ex.handle = handle;
1N/A n = expr(&ex, 0);
1N/A if (peekchr(&ex) == ':')
1N/A seterror(&ex, "invalid use of :");
1N/A if (ex.errmsg)
1N/A {
1N/A if (convert) (*convert)(NiL, &ex.errmsg, handle);
1N/A ex.nextchr = ex.errchr;
1N/A n = 0;
1N/A }
1N/A if (end) *end = ex.nextchr;
1N/A return(n);
1N/A}