home *** CD-ROM | disk | FTP | other *** search
/ Ultra Pack / UltraComputing Partner Applications.iso / SunLabs / tclTK / src / tcl7.4 / tclExpr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-21  |  53.5 KB  |  2,046 lines

  1. /* 
  2.  * tclExpr.c --
  3.  *
  4.  *    This file contains the code to evaluate expressions for
  5.  *    Tcl.
  6.  *
  7.  *    This implementation of floating-point support was modelled
  8.  *    after an initial implementation by Bill Carpenter.
  9.  *
  10.  * Copyright (c) 1987-1994 The Regents of the University of California.
  11.  * Copyright (c) 1994 Sun Microsystems, Inc.
  12.  *
  13.  * See the file "license.terms" for information on usage and redistribution
  14.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  15.  */
  16.  
  17. static char sccsid[] = "@(#) tclExpr.c 1.84 95/06/21 08:46:22";
  18.  
  19. #include "tclInt.h"
  20. #ifdef NO_FLOAT_H
  21. #   include "compat/float.h"
  22. #else
  23. #   include <float.h>
  24. #endif
  25. #ifndef TCL_NO_MATH
  26. #include <math.h>
  27. #endif
  28.  
  29. /*
  30.  * The stuff below is a bit of a hack so that this file can be used
  31.  * in environments that include no UNIX, i.e. no errno.  Just define
  32.  * errno here.
  33.  */
  34.  
  35. #ifndef TCL_GENERIC_ONLY
  36. #include "tclPort.h"
  37. extern int errno;
  38. #else
  39. #define NO_ERRNO_H
  40. #endif
  41.  
  42. #ifdef NO_ERRNO_H
  43. int errno;
  44. #define EDOM 33
  45. #define ERANGE 34
  46. #endif
  47.  
  48. /*
  49.  * The data structure below is used to describe an expression value,
  50.  * which can be either an integer (the usual case), a double-precision
  51.  * floating-point value, or a string.  A given number has only one
  52.  * value at a time.
  53.  */
  54.  
  55. #define STATIC_STRING_SPACE 150
  56.  
  57. typedef struct {
  58.     long intValue;        /* Integer value, if any. */
  59.     double  doubleValue;    /* Floating-point value, if any. */
  60.     ParseValue pv;        /* Used to hold a string value, if any. */
  61.     char staticSpace[STATIC_STRING_SPACE];
  62.                 /* Storage for small strings;  large ones
  63.                  * are malloc-ed. */
  64.     int type;            /* Type of value:  TYPE_INT, TYPE_DOUBLE,
  65.                  * or TYPE_STRING. */
  66. } Value;
  67.  
  68. /*
  69.  * Valid values for type:
  70.  */
  71.  
  72. #define TYPE_INT    0
  73. #define TYPE_DOUBLE    1
  74. #define TYPE_STRING    2
  75.  
  76. /*
  77.  * The data structure below describes the state of parsing an expression.
  78.  * It's passed among the routines in this module.
  79.  */
  80.  
  81. typedef struct {
  82.     char *originalExpr;        /* The entire expression, as originally
  83.                  * passed to Tcl_ExprString et al. */
  84.     char *expr;            /* Position to the next character to be
  85.                  * scanned from the expression string. */
  86.     int token;            /* Type of the last token to be parsed from
  87.                  * expr.  See below for definitions.
  88.                  * Corresponds to the characters just
  89.                  * before expr. */
  90. } ExprInfo;
  91.  
  92. /*
  93.  * The token types are defined below.  In addition, there is a table
  94.  * associating a precedence with each operator.  The order of types
  95.  * is important.  Consult the code before changing it.
  96.  */
  97.  
  98. #define VALUE        0
  99. #define OPEN_PAREN    1
  100. #define CLOSE_PAREN    2
  101. #define COMMA        3
  102. #define END        4
  103. #define UNKNOWN        5
  104.  
  105. /*
  106.  * Binary operators:
  107.  */
  108.  
  109. #define MULT        8
  110. #define DIVIDE        9
  111. #define MOD        10
  112. #define PLUS        11
  113. #define MINUS        12
  114. #define LEFT_SHIFT    13
  115. #define RIGHT_SHIFT    14
  116. #define LESS        15
  117. #define GREATER        16
  118. #define LEQ        17
  119. #define GEQ        18
  120. #define EQUAL        19
  121. #define NEQ        20
  122. #define BIT_AND        21
  123. #define BIT_XOR        22
  124. #define BIT_OR        23
  125. #define AND        24
  126. #define OR        25
  127. #define QUESTY        26
  128. #define COLON        27
  129.  
  130. /*
  131.  * Unary operators:
  132.  */
  133.  
  134. #define    UNARY_MINUS    28
  135. #define UNARY_PLUS    29
  136. #define NOT        30
  137. #define BIT_NOT        31
  138.  
  139. /*
  140.  * Precedence table.  The values for non-operator token types are ignored.
  141.  */
  142.  
  143. static int precTable[] = {
  144.     0, 0, 0, 0, 0, 0, 0, 0,
  145.     12, 12, 12,                /* MULT, DIVIDE, MOD */
  146.     11, 11,                /* PLUS, MINUS */
  147.     10, 10,                /* LEFT_SHIFT, RIGHT_SHIFT */
  148.     9, 9, 9, 9,                /* LESS, GREATER, LEQ, GEQ */
  149.     8, 8,                /* EQUAL, NEQ */
  150.     7,                    /* BIT_AND */
  151.     6,                    /* BIT_XOR */
  152.     5,                    /* BIT_OR */
  153.     4,                    /* AND */
  154.     3,                    /* OR */
  155.     2,                    /* QUESTY */
  156.     1,                    /* COLON */
  157.     13, 13, 13, 13            /* UNARY_MINUS, UNARY_PLUS, NOT,
  158.                      * BIT_NOT */
  159. };
  160.  
  161. /*
  162.  * Mapping from operator numbers to strings;  used for error messages.
  163.  */
  164.  
  165. static char *operatorStrings[] = {
  166.     "VALUE", "(", ")", ",", "END", "UNKNOWN", "6", "7",
  167.     "*", "/", "%", "+", "-", "<<", ">>", "<", ">", "<=",
  168.     ">=", "==", "!=", "&", "^", "|", "&&", "||", "?", ":",
  169.     "-", "+", "!", "~"
  170. };
  171.  
  172. /*
  173.  * The following slight modification to DBL_MAX is needed because of
  174.  * a compiler bug on Sprite (4/15/93).
  175.  */
  176.  
  177. #ifdef sprite
  178. #undef DBL_MAX
  179. #define DBL_MAX 1.797693134862316e+307
  180. #endif
  181.  
  182. /*
  183.  * Macros for testing floating-point values for certain special
  184.  * cases.  Test for not-a-number by comparing a value against
  185.  * itself;  test for infinity by comparing against the largest
  186.  * floating-point value.
  187.  */
  188.  
  189. #define IS_NAN(v) ((v) != (v))
  190. #ifdef DBL_MAX
  191. #   define IS_INF(v) (((v) > DBL_MAX) || ((v) < -DBL_MAX))
  192. #else
  193. #   define IS_INF(v) 0
  194. #endif
  195.  
  196. /*
  197.  * The following global variable is use to signal matherr that Tcl
  198.  * is responsible for the arithmetic, so errors can be handled in a
  199.  * fashion appropriate for Tcl.  Zero means no Tcl math is in
  200.  * progress;  non-zero means Tcl is doing math.
  201.  */
  202.  
  203. int tcl_MathInProgress = 0;
  204.  
  205. /*
  206.  * The variable below serves no useful purpose except to generate
  207.  * a reference to matherr, so that the Tcl version of matherr is
  208.  * linked in rather than the system version.  Without this reference
  209.  * the need for matherr won't be discovered during linking until after
  210.  * libtcl.a has been processed, so Tcl's version won't be used.
  211.  */
  212.  
  213. #ifdef NEED_MATHERR
  214. extern int matherr();
  215. int (*tclMatherrPtr)() = matherr;
  216. #endif
  217.  
  218. /*
  219.  * Declarations for local procedures to this file:
  220.  */
  221.  
  222. static int        ExprAbsFunc _ANSI_ARGS_((ClientData clientData,
  223.                 Tcl_Interp *interp, Tcl_Value *args,
  224.                 Tcl_Value *resultPtr));
  225. static int        ExprBinaryFunc _ANSI_ARGS_((ClientData clientData,
  226.                 Tcl_Interp *interp, Tcl_Value *args,
  227.                 Tcl_Value *resultPtr));
  228. static int        ExprDoubleFunc _ANSI_ARGS_((ClientData clientData,
  229.                 Tcl_Interp *interp, Tcl_Value *args,
  230.                 Tcl_Value *resultPtr));
  231. static int        ExprGetValue _ANSI_ARGS_((Tcl_Interp *interp,
  232.                 ExprInfo *infoPtr, int prec, Value *valuePtr));
  233. static int        ExprIntFunc _ANSI_ARGS_((ClientData clientData,
  234.                 Tcl_Interp *interp, Tcl_Value *args,
  235.                 Tcl_Value *resultPtr));
  236. static int        ExprLex _ANSI_ARGS_((Tcl_Interp *interp,
  237.                 ExprInfo *infoPtr, Value *valuePtr));
  238. static int        ExprLooksLikeInt _ANSI_ARGS_((char *p));
  239. static void        ExprMakeString _ANSI_ARGS_((Tcl_Interp *interp,
  240.                 Value *valuePtr));
  241. static int        ExprMathFunc _ANSI_ARGS_((Tcl_Interp *interp,
  242.                 ExprInfo *infoPtr, Value *valuePtr));
  243. static int        ExprParseString _ANSI_ARGS_((Tcl_Interp *interp,
  244.                 char *string, Value *valuePtr));
  245. static int        ExprRoundFunc _ANSI_ARGS_((ClientData clientData,
  246.                 Tcl_Interp *interp, Tcl_Value *args,
  247.                 Tcl_Value *resultPtr));
  248. static int        ExprTopLevel _ANSI_ARGS_((Tcl_Interp *interp,
  249.                 char *string, Value *valuePtr));
  250. static int        ExprUnaryFunc _ANSI_ARGS_((ClientData clientData,
  251.                 Tcl_Interp *interp, Tcl_Value *args,
  252.                 Tcl_Value *resultPtr));
  253.  
  254. /*
  255.  * Built-in math functions:
  256.  */
  257.  
  258. typedef struct {
  259.     char *name;            /* Name of function. */
  260.     int numArgs;        /* Number of arguments for function. */
  261.     Tcl_ValueType argTypes[MAX_MATH_ARGS];
  262.                 /* Acceptable types for each argument. */
  263.     Tcl_MathProc *proc;        /* Procedure that implements this function. */
  264.     ClientData clientData;    /* Additional argument to pass to the function
  265.                  * when invoking it. */
  266. } BuiltinFunc;
  267.  
  268. static BuiltinFunc funcTable[] = {
  269. #ifndef TCL_NO_MATH
  270.     {"acos", 1, {TCL_DOUBLE}, ExprUnaryFunc, (ClientData) acos},
  271.     {"asin", 1, {TCL_DOUBLE}, ExprUnaryFunc, (ClientData) asin},
  272.     {"atan", 1, {TCL_DOUBLE}, ExprUnaryFunc, (ClientData) atan},
  273.     {"atan2", 2, {TCL_DOUBLE, TCL_DOUBLE}, ExprBinaryFunc, (ClientData) atan2},
  274.     {"ceil", 1, {TCL_DOUBLE}, ExprUnaryFunc, (ClientData) ceil},
  275.     {"cos", 1, {TCL_DOUBLE}, ExprUnaryFunc, (ClientData) cos},
  276.     {"cosh", 1, {TCL_DOUBLE}, ExprUnaryFunc, (ClientData) cosh},
  277.     {"exp", 1, {TCL_DOUBLE}, ExprUnaryFunc, (ClientData) exp},
  278.     {"floor", 1, {TCL_DOUBLE}, ExprUnaryFunc, (ClientData) floor},
  279.     {"fmod", 2, {TCL_DOUBLE, TCL_DOUBLE}, ExprBinaryFunc, (ClientData) fmod},
  280.     {"hypot", 2, {TCL_DOUBLE, TCL_DOUBLE}, ExprBinaryFunc, (ClientData) hypot},
  281.     {"log", 1, {TCL_DOUBLE}, ExprUnaryFunc, (ClientData) log},
  282.     {"log10", 1, {TCL_DOUBLE}, ExprUnaryFunc, (ClientData) log10},
  283.     {"pow", 2, {TCL_DOUBLE, TCL_DOUBLE}, ExprBinaryFunc, (ClientData) pow},
  284.     {"sin", 1, {TCL_DOUBLE}, ExprUnaryFunc, (ClientData) sin},
  285.     {"sinh", 1, {TCL_DOUBLE}, ExprUnaryFunc, (ClientData) sinh},
  286.     {"sqrt", 1, {TCL_DOUBLE}, ExprUnaryFunc, (ClientData) sqrt},
  287.     {"tan", 1, {TCL_DOUBLE}, ExprUnaryFunc, (ClientData) tan},
  288.     {"tanh", 1, {TCL_DOUBLE}, ExprUnaryFunc, (ClientData) tanh},
  289. #endif
  290.     {"abs", 1, {TCL_EITHER}, ExprAbsFunc, 0},
  291.     {"double", 1, {TCL_EITHER}, ExprDoubleFunc, 0},
  292.     {"int", 1, {TCL_EITHER}, ExprIntFunc, 0},
  293.     {"round", 1, {TCL_EITHER}, ExprRoundFunc, 0},
  294.  
  295.     {0},
  296. };
  297.  
  298. /*
  299.  *--------------------------------------------------------------
  300.  *
  301.  * ExprParseString --
  302.  *
  303.  *    Given a string (such as one coming from command or variable
  304.  *    substitution), make a Value based on the string.  The value
  305.  *    will be a floating-point or integer, if possible, or else it
  306.  *    will just be a copy of the string.
  307.  *
  308.  * Results:
  309.  *    TCL_OK is returned under normal circumstances, and TCL_ERROR
  310.  *    is returned if a floating-point overflow or underflow occurred
  311.  *    while reading in a number.  The value at *valuePtr is modified
  312.  *    to hold a number, if possible.
  313.  *
  314.  * Side effects:
  315.  *    None.
  316.  *
  317.  *--------------------------------------------------------------
  318.  */
  319.  
  320. static int
  321. ExprParseString(interp, string, valuePtr)
  322.     Tcl_Interp *interp;        /* Where to store error message. */
  323.     char *string;        /* String to turn into value. */
  324.     Value *valuePtr;        /* Where to store value information. 
  325.                  * Caller must have initialized pv field. */
  326. {
  327.     char *term, *p, *start;
  328.  
  329.     if (*string != 0) {
  330.     if (ExprLooksLikeInt(string)) {
  331.         valuePtr->type = TYPE_INT;
  332.         errno = 0;
  333.     
  334.         /*
  335.          * Note: use strtoul instead of strtol for integer conversions
  336.          * to allow full-size unsigned numbers, but don't depend on
  337.          * strtoul to handle sign characters;  it won't in some
  338.          * implementations.
  339.          */
  340.     
  341.         for (p = string; isspace(UCHAR(*p)); p++) {
  342.         /* Empty loop body. */
  343.         }
  344.         if (*p == '-') {
  345.         start = p+1;
  346.         valuePtr->intValue = -strtoul(start, &term, 0);
  347.         } else if (*p == '+') {
  348.         start = p+1;
  349.         valuePtr->intValue = strtoul(start, &term, 0);
  350.         } else {
  351.         start = p;
  352.         valuePtr->intValue = strtoul(start, &term, 0);
  353.         }
  354.         if (*term == 0) {
  355.         if (errno == ERANGE) {
  356.             /*
  357.              * This procedure is sometimes called with string in
  358.              * interp->result, so we have to clear the result before
  359.              * logging an error message.
  360.              */
  361.     
  362.             Tcl_ResetResult(interp);
  363.             interp->result = "integer value too large to represent";
  364.             Tcl_SetErrorCode(interp, "ARITH", "IOVERFLOW",
  365.                 interp->result, (char *) NULL);
  366.             return TCL_ERROR;
  367.         } else {
  368.             return TCL_OK;
  369.         }
  370.         }
  371.     } else {
  372.         errno = 0;
  373.         valuePtr->doubleValue = strtod(string, &term);
  374.         if ((term != string) && (*term == 0)) {
  375.         if (errno != 0) {
  376.             Tcl_ResetResult(interp);
  377.             TclExprFloatError(interp, valuePtr->doubleValue);
  378.             return TCL_ERROR;
  379.         }
  380.         valuePtr->type = TYPE_DOUBLE;
  381.         return TCL_OK;
  382.         }
  383.     }
  384.     }
  385.  
  386.     /*
  387.      * Not a valid number.  Save a string value (but don't do anything
  388.      * if it's already the value).
  389.      */
  390.  
  391.     valuePtr->type = TYPE_STRING;
  392.     if (string != valuePtr->pv.buffer) {
  393.     int length, shortfall;
  394.  
  395.     length = strlen(string);
  396.     valuePtr->pv.next = valuePtr->pv.buffer;
  397.     shortfall = length - (valuePtr->pv.end - valuePtr->pv.buffer);
  398.     if (shortfall > 0) {
  399.         (*valuePtr->pv.expandProc)(&valuePtr->pv, shortfall);
  400.     }
  401.     strcpy(valuePtr->pv.buffer, string);
  402.     }
  403.     return TCL_OK;
  404. }
  405.  
  406. /*
  407.  *----------------------------------------------------------------------
  408.  *
  409.  * ExprLex --
  410.  *
  411.  *    Lexical analyzer for expression parser:  parses a single value,
  412.  *    operator, or other syntactic element from an expression string.
  413.  *
  414.  * Results:
  415.  *    TCL_OK is returned unless an error occurred while doing lexical
  416.  *    analysis or executing an embedded command.  In that case a
  417.  *    standard Tcl error is returned, using interp->result to hold
  418.  *    an error message.  In the event of a successful return, the token
  419.  *    and field in infoPtr is updated to refer to the next symbol in
  420.  *    the expression string, and the expr field is advanced past that
  421.  *    token;  if the token is a value, then the value is stored at
  422.  *    valuePtr.
  423.  *
  424.  * Side effects:
  425.  *    None.
  426.  *
  427.  *----------------------------------------------------------------------
  428.  */
  429.  
  430. static int
  431. ExprLex(interp, infoPtr, valuePtr)
  432.     Tcl_Interp *interp;            /* Interpreter to use for error
  433.                      * reporting. */
  434.     register ExprInfo *infoPtr;        /* Describes the state of the parse. */
  435.     register Value *valuePtr;        /* Where to store value, if that is
  436.                      * what's parsed from string.  Caller
  437.                      * must have initialized pv field
  438.                      * correctly. */
  439. {
  440.     register char *p;
  441.     char *var, *term;
  442.     int result;
  443.  
  444.     p = infoPtr->expr;
  445.     while (isspace(UCHAR(*p))) {
  446.     p++;
  447.     }
  448.     if (*p == 0) {
  449.     infoPtr->token = END;
  450.     infoPtr->expr = p;
  451.     return TCL_OK;
  452.     }
  453.  
  454.     /*
  455.      * First try to parse the token as an integer or floating-point number.
  456.      * Don't want to check for a number if the first character is "+"
  457.      * or "-".  If we do, we might treat a binary operator as unary by
  458.      * mistake, which will eventually cause a syntax error.
  459.      */
  460.  
  461.     if ((*p != '+')  && (*p != '-')) {
  462.     if (ExprLooksLikeInt(p)) {
  463.         errno = 0;
  464.         valuePtr->intValue = strtoul(p, &term, 0);
  465.         if (errno == ERANGE) {
  466.         interp->result = "integer value too large to represent";
  467.         Tcl_SetErrorCode(interp, "ARITH", "IOVERFLOW",
  468.             interp->result, (char *) NULL);
  469.         return TCL_ERROR;
  470.         }
  471.         infoPtr->token = VALUE;
  472.         infoPtr->expr = term;
  473.         valuePtr->type = TYPE_INT;
  474.         return TCL_OK;
  475.     } else {
  476.         errno = 0;
  477.         valuePtr->doubleValue = strtod(p, &term);
  478.         if (term != p) {
  479.         if (errno != 0) {
  480.             TclExprFloatError(interp, valuePtr->doubleValue);
  481.             return TCL_ERROR;
  482.         }
  483.         infoPtr->token = VALUE;
  484.         infoPtr->expr = term;
  485.         valuePtr->type = TYPE_DOUBLE;
  486.         return TCL_OK;
  487.         }
  488.     }
  489.     }
  490.  
  491.     infoPtr->expr = p+1;
  492.     switch (*p) {
  493.     case '$':
  494.  
  495.         /*
  496.          * Variable.  Fetch its value, then see if it makes sense
  497.          * as an integer or floating-point number.
  498.          */
  499.  
  500.         infoPtr->token = VALUE;
  501.         var = Tcl_ParseVar(interp, p, &infoPtr->expr);
  502.         if (var == NULL) {
  503.         return TCL_ERROR;
  504.         }
  505.         Tcl_ResetResult(interp);
  506.         if (((Interp *) interp)->noEval) {
  507.         valuePtr->type = TYPE_INT;
  508.         valuePtr->intValue = 0;
  509.         return TCL_OK;
  510.         }
  511.         return ExprParseString(interp, var, valuePtr);
  512.  
  513.     case '[':
  514.         infoPtr->token = VALUE;
  515.         ((Interp *) interp)->evalFlags = TCL_BRACKET_TERM;
  516.         result = Tcl_Eval(interp, p+1);
  517.         infoPtr->expr = ((Interp *) interp)->termPtr;
  518.         if (result != TCL_OK) {
  519.         return result;
  520.         }
  521.         infoPtr->expr++;
  522.         if (((Interp *) interp)->noEval) {
  523.         valuePtr->type = TYPE_INT;
  524.         valuePtr->intValue = 0;
  525.         Tcl_ResetResult(interp);
  526.         return TCL_OK;
  527.         }
  528.         result = ExprParseString(interp, interp->result, valuePtr);
  529.         if (result != TCL_OK) {
  530.         return result;
  531.         }
  532.         Tcl_ResetResult(interp);
  533.         return TCL_OK;
  534.  
  535.     case '"':
  536.         infoPtr->token = VALUE;
  537.         result = TclParseQuotes(interp, infoPtr->expr, '"', 0,
  538.             &infoPtr->expr, &valuePtr->pv);
  539.         if (result != TCL_OK) {
  540.         return result;
  541.         }
  542.         Tcl_ResetResult(interp);
  543.         return ExprParseString(interp, valuePtr->pv.buffer, valuePtr);
  544.  
  545.     case '{':
  546.         infoPtr->token = VALUE;
  547.         result = TclParseBraces(interp, infoPtr->expr, &infoPtr->expr,
  548.             &valuePtr->pv);
  549.         if (result != TCL_OK) {
  550.         return result;
  551.         }
  552.         Tcl_ResetResult(interp);
  553.         return ExprParseString(interp, valuePtr->pv.buffer, valuePtr);
  554.  
  555.     case '(':
  556.         infoPtr->token = OPEN_PAREN;
  557.         return TCL_OK;
  558.  
  559.     case ')':
  560.         infoPtr->token = CLOSE_PAREN;
  561.         return TCL_OK;
  562.  
  563.     case ',':
  564.         infoPtr->token = COMMA;
  565.         return TCL_OK;
  566.  
  567.     case '*':
  568.         infoPtr->token = MULT;
  569.         return TCL_OK;
  570.  
  571.     case '/':
  572.         infoPtr->token = DIVIDE;
  573.         return TCL_OK;
  574.  
  575.     case '%':
  576.         infoPtr->token = MOD;
  577.         return TCL_OK;
  578.  
  579.     case '+':
  580.         infoPtr->token = PLUS;
  581.         return TCL_OK;
  582.  
  583.     case '-':
  584.         infoPtr->token = MINUS;
  585.         return TCL_OK;
  586.  
  587.     case '?':
  588.         infoPtr->token = QUESTY;
  589.         return TCL_OK;
  590.  
  591.     case ':':
  592.         infoPtr->token = COLON;
  593.         return TCL_OK;
  594.  
  595.     case '<':
  596.         switch (p[1]) {
  597.         case '<':
  598.             infoPtr->expr = p+2;
  599.             infoPtr->token = LEFT_SHIFT;
  600.             break;
  601.         case '=':
  602.             infoPtr->expr = p+2;
  603.             infoPtr->token = LEQ;
  604.             break;
  605.         default:
  606.             infoPtr->token = LESS;
  607.             break;
  608.         }
  609.         return TCL_OK;
  610.  
  611.     case '>':
  612.         switch (p[1]) {
  613.         case '>':
  614.             infoPtr->expr = p+2;
  615.             infoPtr->token = RIGHT_SHIFT;
  616.             break;
  617.         case '=':
  618.             infoPtr->expr = p+2;
  619.             infoPtr->token = GEQ;
  620.             break;
  621.         default:
  622.             infoPtr->token = GREATER;
  623.             break;
  624.         }
  625.         return TCL_OK;
  626.  
  627.     case '=':
  628.         if (p[1] == '=') {
  629.         infoPtr->expr = p+2;
  630.         infoPtr->token = EQUAL;
  631.         } else {
  632.         infoPtr->token = UNKNOWN;
  633.         }
  634.         return TCL_OK;
  635.  
  636.     case '!':
  637.         if (p[1] == '=') {
  638.         infoPtr->expr = p+2;
  639.         infoPtr->token = NEQ;
  640.         } else {
  641.         infoPtr->token = NOT;
  642.         }
  643.         return TCL_OK;
  644.  
  645.     case '&':
  646.         if (p[1] == '&') {
  647.         infoPtr->expr = p+2;
  648.         infoPtr->token = AND;
  649.         } else {
  650.         infoPtr->token = BIT_AND;
  651.         }
  652.         return TCL_OK;
  653.  
  654.     case '^':
  655.         infoPtr->token = BIT_XOR;
  656.         return TCL_OK;
  657.  
  658.     case '|':
  659.         if (p[1] == '|') {
  660.         infoPtr->expr = p+2;
  661.         infoPtr->token = OR;
  662.         } else {
  663.         infoPtr->token = BIT_OR;
  664.         }
  665.         return TCL_OK;
  666.  
  667.     case '~':
  668.         infoPtr->token = BIT_NOT;
  669.         return TCL_OK;
  670.  
  671.     default:
  672.         if (isalpha(UCHAR(*p))) {
  673.         infoPtr->expr = p;
  674.         return ExprMathFunc(interp, infoPtr, valuePtr);
  675.         }
  676.         infoPtr->expr = p+1;
  677.         infoPtr->token = UNKNOWN;
  678.         return TCL_OK;
  679.     }
  680. }
  681.  
  682. /*
  683.  *----------------------------------------------------------------------
  684.  *
  685.  * ExprGetValue --
  686.  *
  687.  *    Parse a "value" from the remainder of the expression in infoPtr.
  688.  *
  689.  * Results:
  690.  *    Normally TCL_OK is returned.  The value of the expression is
  691.  *    returned in *valuePtr.  If an error occurred, then interp->result
  692.  *    contains an error message and TCL_ERROR is returned.
  693.  *    InfoPtr->token will be left pointing to the token AFTER the
  694.  *    expression, and infoPtr->expr will point to the character just
  695.  *    after the terminating token.
  696.  *
  697.  * Side effects:
  698.  *    None.
  699.  *
  700.  *----------------------------------------------------------------------
  701.  */
  702.  
  703. static int
  704. ExprGetValue(interp, infoPtr, prec, valuePtr)
  705.     Tcl_Interp *interp;            /* Interpreter to use for error
  706.                      * reporting. */
  707.     register ExprInfo *infoPtr;        /* Describes the state of the parse
  708.                      * just before the value (i.e. ExprLex
  709.                      * will be called to get first token
  710.                      * of value). */
  711.     int prec;                /* Treat any un-parenthesized operator
  712.                      * with precedence <= this as the end
  713.                      * of the expression. */
  714.     Value *valuePtr;            /* Where to store the value of the
  715.                      * expression.   Caller must have
  716.                      * initialized pv field. */
  717. {
  718.     Interp *iPtr = (Interp *) interp;
  719.     Value value2;            /* Second operand for current
  720.                      * operator.  */
  721.     int operator;            /* Current operator (either unary
  722.                      * or binary). */
  723.     int badType;            /* Type of offending argument;  used
  724.                      * for error messages. */
  725.     int gotOp;                /* Non-zero means already lexed the
  726.                      * operator (while picking up value
  727.                      * for unary operator).  Don't lex
  728.                      * again. */
  729.     int result;
  730.  
  731.     /*
  732.      * There are two phases to this procedure.  First, pick off an initial
  733.      * value.  Then, parse (binary operator, value) pairs until done.
  734.      */
  735.  
  736.     gotOp = 0;
  737.     value2.pv.buffer = value2.pv.next = value2.staticSpace;
  738.     value2.pv.end = value2.pv.buffer + STATIC_STRING_SPACE - 1;
  739.     value2.pv.expandProc = TclExpandParseValue;
  740.     value2.pv.clientData = (ClientData) NULL;
  741.     result = ExprLex(interp, infoPtr, valuePtr);
  742.     if (result != TCL_OK) {
  743.     goto done;
  744.     }
  745.     if (infoPtr->token == OPEN_PAREN) {
  746.  
  747.     /*
  748.      * Parenthesized sub-expression.
  749.      */
  750.  
  751.     result = ExprGetValue(interp, infoPtr, -1, valuePtr);
  752.     if (result != TCL_OK) {
  753.         goto done;
  754.     }
  755.     if (infoPtr->token != CLOSE_PAREN) {
  756.         Tcl_AppendResult(interp, "unmatched parentheses in expression \"",
  757.             infoPtr->originalExpr, "\"", (char *) NULL);
  758.         result = TCL_ERROR;
  759.         goto done;
  760.     }
  761.     } else {
  762.     if (infoPtr->token == MINUS) {
  763.         infoPtr->token = UNARY_MINUS;
  764.     }
  765.     if (infoPtr->token == PLUS) {
  766.         infoPtr->token = UNARY_PLUS;
  767.     }
  768.     if (infoPtr->token >= UNARY_MINUS) {
  769.  
  770.         /*
  771.          * Process unary operators.
  772.          */
  773.  
  774.         operator = infoPtr->token;
  775.         result = ExprGetValue(interp, infoPtr, precTable[infoPtr->token],
  776.             valuePtr);
  777.         if (result != TCL_OK) {
  778.         goto done;
  779.         }
  780.         if (!iPtr->noEval) {
  781.         switch (operator) {
  782.             case UNARY_MINUS:
  783.             if (valuePtr->type == TYPE_INT) {
  784.                 valuePtr->intValue = -valuePtr->intValue;
  785.             } else if (valuePtr->type == TYPE_DOUBLE){
  786.                 valuePtr->doubleValue = -valuePtr->doubleValue;
  787.             } else {
  788.                 badType = valuePtr->type;
  789.                 goto illegalType;
  790.             } 
  791.             break;
  792.             case UNARY_PLUS:
  793.             if ((valuePtr->type != TYPE_INT)
  794.                 && (valuePtr->type != TYPE_DOUBLE)) {
  795.                 badType = valuePtr->type;
  796.                 goto illegalType;
  797.             } 
  798.             break;
  799.             case NOT:
  800.             if (valuePtr->type == TYPE_INT) {
  801.                 valuePtr->intValue = !valuePtr->intValue;
  802.             } else if (valuePtr->type == TYPE_DOUBLE) {
  803.                 /*
  804.                  * Theoretically, should be able to use
  805.                  * "!valuePtr->intValue", but apparently some
  806.                  * compilers can't handle it.
  807.                  */
  808.                 if (valuePtr->doubleValue == 0.0) {
  809.                 valuePtr->intValue = 1;
  810.                 } else {
  811.                 valuePtr->intValue = 0;
  812.                 }
  813.                 valuePtr->type = TYPE_INT;
  814.             } else {
  815.                 badType = valuePtr->type;
  816.                 goto illegalType;
  817.             }
  818.             break;
  819.             case BIT_NOT:
  820.             if (valuePtr->type == TYPE_INT) {
  821.                 valuePtr->intValue = ~valuePtr->intValue;
  822.             } else {
  823.                 badType  = valuePtr->type;
  824.                 goto illegalType;
  825.             }
  826.             break;
  827.         }
  828.         }
  829.         gotOp = 1;
  830.     } else if (infoPtr->token != VALUE) {
  831.         goto syntaxError;
  832.     }
  833.     }
  834.  
  835.     /*
  836.      * Got the first operand.  Now fetch (operator, operand) pairs.
  837.      */
  838.  
  839.     if (!gotOp) {
  840.     result = ExprLex(interp, infoPtr, &value2);
  841.     if (result != TCL_OK) {
  842.         goto done;
  843.     }
  844.     }
  845.     while (1) {
  846.     operator = infoPtr->token;
  847.     value2.pv.next = value2.pv.buffer;
  848.     if ((operator < MULT) || (operator >= UNARY_MINUS)) {
  849.         if ((operator == END) || (operator == CLOSE_PAREN)
  850.             || (operator == COMMA)) {
  851.         result = TCL_OK;
  852.         goto done;
  853.         } else {
  854.         goto syntaxError;
  855.         }
  856.     }
  857.     if (precTable[operator] <= prec) {
  858.         result = TCL_OK;
  859.         goto done;
  860.     }
  861.  
  862.     /*
  863.      * If we're doing an AND or OR and the first operand already
  864.      * determines the result, don't execute anything in the
  865.      * second operand:  just parse.  Same style for ?: pairs.
  866.      */
  867.  
  868.     if ((operator == AND) || (operator == OR) || (operator == QUESTY)) {
  869.         if (valuePtr->type == TYPE_DOUBLE) {
  870.         valuePtr->intValue = valuePtr->doubleValue != 0;
  871.         valuePtr->type = TYPE_INT;
  872.         } else if ((valuePtr->type == TYPE_STRING) && !iPtr->noEval) {
  873.         badType = TYPE_STRING;
  874.         goto illegalType;
  875.         }
  876.         if (((operator == AND) && !valuePtr->intValue)
  877.             || ((operator == OR) && valuePtr->intValue)) {
  878.         iPtr->noEval++;
  879.         result = ExprGetValue(interp, infoPtr, precTable[operator],
  880.             &value2);
  881.         iPtr->noEval--;
  882.         if (operator == OR) {
  883.             valuePtr->intValue = 1;
  884.         }
  885.         continue;
  886.         } else if (operator == QUESTY) {
  887.         /*
  888.          * Special note:  ?: operators must associate right to
  889.          * left.  To make this happen, use a precedence one lower
  890.          * than QUESTY when calling ExprGetValue recursively.
  891.          */
  892.  
  893.         if (valuePtr->intValue != 0) {
  894.             valuePtr->pv.next = valuePtr->pv.buffer;
  895.             result = ExprGetValue(interp, infoPtr,
  896.                 precTable[QUESTY] - 1, valuePtr);
  897.             if (result != TCL_OK) {
  898.             goto done;
  899.             }
  900.             if (infoPtr->token != COLON) {
  901.             goto syntaxError;
  902.             }
  903.             value2.pv.next = value2.pv.buffer;
  904.             iPtr->noEval++;
  905.             result = ExprGetValue(interp, infoPtr,
  906.                 precTable[QUESTY] - 1, &value2);
  907.             iPtr->noEval--;
  908.         } else {
  909.             iPtr->noEval++;
  910.             result = ExprGetValue(interp, infoPtr,
  911.                 precTable[QUESTY] - 1, &value2);
  912.             iPtr->noEval--;
  913.             if (result != TCL_OK) {
  914.             goto done;
  915.             }
  916.             if (infoPtr->token != COLON) {
  917.             goto syntaxError;
  918.             }
  919.             valuePtr->pv.next = valuePtr->pv.buffer;
  920.             result = ExprGetValue(interp, infoPtr,
  921.                 precTable[QUESTY] - 1, valuePtr);
  922.         }
  923.         continue;
  924.         } else {
  925.         result = ExprGetValue(interp, infoPtr, precTable[operator],
  926.             &value2);
  927.         }
  928.     } else {
  929.         result = ExprGetValue(interp, infoPtr, precTable[operator],
  930.             &value2);
  931.     }
  932.     if (result != TCL_OK) {
  933.         goto done;
  934.     }
  935.     if ((infoPtr->token < MULT) && (infoPtr->token != VALUE)
  936.         && (infoPtr->token != END) && (infoPtr->token != COMMA)
  937.         && (infoPtr->token != CLOSE_PAREN)) {
  938.         goto syntaxError;
  939.     }
  940.  
  941.     if (iPtr->noEval) {
  942.         continue;
  943.     }
  944.  
  945.     /*
  946.      * At this point we've got two values and an operator.  Check
  947.      * to make sure that the particular data types are appropriate
  948.      * for the particular operator, and perform type conversion
  949.      * if necessary.
  950.      */
  951.  
  952.     switch (operator) {
  953.  
  954.         /*
  955.          * For the operators below, no strings are allowed and
  956.          * ints get converted to floats if necessary.
  957.          */
  958.  
  959.         case MULT: case DIVIDE: case PLUS: case MINUS:
  960.         if ((valuePtr->type == TYPE_STRING)
  961.             || (value2.type == TYPE_STRING)) {
  962.             badType = TYPE_STRING;
  963.             goto illegalType;
  964.         }
  965.         if (valuePtr->type == TYPE_DOUBLE) {
  966.             if (value2.type == TYPE_INT) {
  967.             value2.doubleValue = value2.intValue;
  968.             value2.type = TYPE_DOUBLE;
  969.             }
  970.         } else if (value2.type == TYPE_DOUBLE) {
  971.             if (valuePtr->type == TYPE_INT) {
  972.             valuePtr->doubleValue = valuePtr->intValue;
  973.             valuePtr->type = TYPE_DOUBLE;
  974.             }
  975.         }
  976.         break;
  977.  
  978.         /*
  979.          * For the operators below, only integers are allowed.
  980.          */
  981.  
  982.         case MOD: case LEFT_SHIFT: case RIGHT_SHIFT:
  983.         case BIT_AND: case BIT_XOR: case BIT_OR:
  984.          if (valuePtr->type != TYPE_INT) {
  985.              badType = valuePtr->type;
  986.              goto illegalType;
  987.          } else if (value2.type != TYPE_INT) {
  988.              badType = value2.type;
  989.              goto illegalType;
  990.          }
  991.          break;
  992.  
  993.         /*
  994.          * For the operators below, any type is allowed but the
  995.          * two operands must have the same type.  Convert integers
  996.          * to floats and either to strings, if necessary.
  997.          */
  998.  
  999.         case LESS: case GREATER: case LEQ: case GEQ:
  1000.         case EQUAL: case NEQ:
  1001.         if (valuePtr->type == TYPE_STRING) {
  1002.             if (value2.type != TYPE_STRING) {
  1003.             ExprMakeString(interp, &value2);
  1004.             }
  1005.         } else if (value2.type == TYPE_STRING) {
  1006.             if (valuePtr->type != TYPE_STRING) {
  1007.             ExprMakeString(interp, valuePtr);
  1008.             }
  1009.         } else if (valuePtr->type == TYPE_DOUBLE) {
  1010.             if (value2.type == TYPE_INT) {
  1011.             value2.doubleValue = value2.intValue;
  1012.             value2.type = TYPE_DOUBLE;
  1013.             }
  1014.         } else if (value2.type == TYPE_DOUBLE) {
  1015.              if (valuePtr->type == TYPE_INT) {
  1016.             valuePtr->doubleValue = valuePtr->intValue;
  1017.             valuePtr->type = TYPE_DOUBLE;
  1018.             }
  1019.         }
  1020.         break;
  1021.  
  1022.         /*
  1023.          * For the operators below, no strings are allowed, but
  1024.          * no int->double conversions are performed.
  1025.          */
  1026.  
  1027.         case AND: case OR:
  1028.         if (valuePtr->type == TYPE_STRING) {
  1029.             badType = valuePtr->type;
  1030.             goto illegalType;
  1031.         }
  1032.         if (value2.type == TYPE_STRING) {
  1033.             badType = value2.type;
  1034.             goto illegalType;
  1035.         }
  1036.         break;
  1037.  
  1038.         /*
  1039.          * For the operators below, type and conversions are
  1040.          * irrelevant:  they're handled elsewhere.
  1041.          */
  1042.  
  1043.         case QUESTY: case COLON:
  1044.         break;
  1045.  
  1046.         /*
  1047.          * Any other operator is an error.
  1048.          */
  1049.  
  1050.         default:
  1051.         interp->result = "unknown operator in expression";
  1052.         result = TCL_ERROR;
  1053.         goto done;
  1054.     }
  1055.  
  1056.     /*
  1057.      * Carry out the function of the specified operator.
  1058.      */
  1059.  
  1060.     switch (operator) {
  1061.         case MULT:
  1062.         if (valuePtr->type == TYPE_INT) {
  1063.             valuePtr->intValue = valuePtr->intValue * value2.intValue;
  1064.         } else {
  1065.             valuePtr->doubleValue *= value2.doubleValue;
  1066.         }
  1067.         break;
  1068.         case DIVIDE:
  1069.         case MOD:
  1070.         if (valuePtr->type == TYPE_INT) {
  1071.             long divisor, quot, rem;
  1072.             int negative;
  1073.  
  1074.             if (value2.intValue == 0) {
  1075.             divideByZero:
  1076.             interp->result = "divide by zero";
  1077.             Tcl_SetErrorCode(interp, "ARITH", "DIVZERO",
  1078.                 interp->result, (char *) NULL);
  1079.             result = TCL_ERROR;
  1080.             goto done;
  1081.             }
  1082.  
  1083.             /*
  1084.              * The code below is tricky because C doesn't guarantee
  1085.              * much about the properties of the quotient or
  1086.              * remainder, but Tcl does:  the remainder always has
  1087.              * the same sign as the divisor and a smaller absolute
  1088.              * value.
  1089.              */
  1090.  
  1091.             divisor = value2.intValue;
  1092.             negative = 0;
  1093.             if (divisor < 0) {
  1094.             divisor = -divisor;
  1095.             valuePtr->intValue = -valuePtr->intValue;
  1096.             negative = 1;
  1097.             }
  1098.             quot = valuePtr->intValue / divisor;
  1099.             rem = valuePtr->intValue % divisor;
  1100.             if (rem < 0) {
  1101.             rem += divisor;
  1102.             quot -= 1;
  1103.             }
  1104.             if (negative) {
  1105.             rem = -rem;
  1106.             }
  1107.             valuePtr->intValue = (operator == DIVIDE) ? quot : rem;
  1108.         } else {
  1109.             if (value2.doubleValue == 0.0) {
  1110.             goto divideByZero;
  1111.             }
  1112.             valuePtr->doubleValue /= value2.doubleValue;
  1113.         }
  1114.         break;
  1115.         case PLUS:
  1116.         if (valuePtr->type == TYPE_INT) {
  1117.             valuePtr->intValue = valuePtr->intValue + value2.intValue;
  1118.         } else {
  1119.             valuePtr->doubleValue += value2.doubleValue;
  1120.         }
  1121.         break;
  1122.         case MINUS:
  1123.         if (valuePtr->type == TYPE_INT) {
  1124.             valuePtr->intValue = valuePtr->intValue - value2.intValue;
  1125.         } else {
  1126.             valuePtr->doubleValue -= value2.doubleValue;
  1127.         }
  1128.         break;
  1129.         case LEFT_SHIFT:
  1130.         valuePtr->intValue <<= value2.intValue;
  1131.         break;
  1132.         case RIGHT_SHIFT:
  1133.         /*
  1134.          * The following code is a bit tricky:  it ensures that
  1135.          * right shifts propagate the sign bit even on machines
  1136.          * where ">>" won't do it by default.
  1137.          */
  1138.  
  1139.         if (valuePtr->intValue < 0) {
  1140.             valuePtr->intValue =
  1141.                 ~((~valuePtr->intValue) >> value2.intValue);
  1142.         } else {
  1143.             valuePtr->intValue >>= value2.intValue;
  1144.         }
  1145.         break;
  1146.         case LESS:
  1147.         if (valuePtr->type == TYPE_INT) {
  1148.             valuePtr->intValue =
  1149.             valuePtr->intValue < value2.intValue;
  1150.         } else if (valuePtr->type == TYPE_DOUBLE) {
  1151.             valuePtr->intValue =
  1152.             valuePtr->doubleValue < value2.doubleValue;
  1153.         } else {
  1154.             valuePtr->intValue =
  1155.                 strcmp(valuePtr->pv.buffer, value2.pv.buffer) < 0;
  1156.         }
  1157.         valuePtr->type = TYPE_INT;
  1158.         break;
  1159.         case GREATER:
  1160.         if (valuePtr->type == TYPE_INT) {
  1161.             valuePtr->intValue =
  1162.             valuePtr->intValue > value2.intValue;
  1163.         } else if (valuePtr->type == TYPE_DOUBLE) {
  1164.             valuePtr->intValue =
  1165.             valuePtr->doubleValue > value2.doubleValue;
  1166.         } else {
  1167.             valuePtr->intValue =
  1168.                 strcmp(valuePtr->pv.buffer, value2.pv.buffer) > 0;
  1169.         }
  1170.         valuePtr->type = TYPE_INT;
  1171.         break;
  1172.         case LEQ:
  1173.         if (valuePtr->type == TYPE_INT) {
  1174.             valuePtr->intValue =
  1175.             valuePtr->intValue <= value2.intValue;
  1176.         } else if (valuePtr->type == TYPE_DOUBLE) {
  1177.             valuePtr->intValue =
  1178.             valuePtr->doubleValue <= value2.doubleValue;
  1179.         } else {
  1180.             valuePtr->intValue =
  1181.                 strcmp(valuePtr->pv.buffer, value2.pv.buffer) <= 0;
  1182.         }
  1183.         valuePtr->type = TYPE_INT;
  1184.         break;
  1185.         case GEQ:
  1186.         if (valuePtr->type == TYPE_INT) {
  1187.             valuePtr->intValue =
  1188.             valuePtr->intValue >= value2.intValue;
  1189.         } else if (valuePtr->type == TYPE_DOUBLE) {
  1190.             valuePtr->intValue =
  1191.             valuePtr->doubleValue >= value2.doubleValue;
  1192.         } else {
  1193.             valuePtr->intValue =
  1194.                 strcmp(valuePtr->pv.buffer, value2.pv.buffer) >= 0;
  1195.         }
  1196.         valuePtr->type = TYPE_INT;
  1197.         break;
  1198.         case EQUAL:
  1199.         if (valuePtr->type == TYPE_INT) {
  1200.             valuePtr->intValue =
  1201.             valuePtr->intValue == value2.intValue;
  1202.         } else if (valuePtr->type == TYPE_DOUBLE) {
  1203.             valuePtr->intValue =
  1204.             valuePtr->doubleValue == value2.doubleValue;
  1205.         } else {
  1206.             valuePtr->intValue =
  1207.                 strcmp(valuePtr->pv.buffer, value2.pv.buffer) == 0;
  1208.         }
  1209.         valuePtr->type = TYPE_INT;
  1210.         break;
  1211.         case NEQ:
  1212.         if (valuePtr->type == TYPE_INT) {
  1213.             valuePtr->intValue =
  1214.             valuePtr->intValue != value2.intValue;
  1215.         } else if (valuePtr->type == TYPE_DOUBLE) {
  1216.             valuePtr->intValue =
  1217.             valuePtr->doubleValue != value2.doubleValue;
  1218.         } else {
  1219.             valuePtr->intValue =
  1220.                 strcmp(valuePtr->pv.buffer, value2.pv.buffer) != 0;
  1221.         }
  1222.         valuePtr->type = TYPE_INT;
  1223.         break;
  1224.         case BIT_AND:
  1225.         valuePtr->intValue &= value2.intValue;
  1226.         break;
  1227.         case BIT_XOR:
  1228.         valuePtr->intValue ^= value2.intValue;
  1229.         break;
  1230.         case BIT_OR:
  1231.         valuePtr->intValue |= value2.intValue;
  1232.         break;
  1233.  
  1234.         /*
  1235.          * For AND and OR, we know that the first value has already
  1236.          * been converted to an integer.  Thus we need only consider
  1237.          * the possibility of int vs. double for the second value.
  1238.          */
  1239.  
  1240.         case AND:
  1241.         if (value2.type == TYPE_DOUBLE) {
  1242.             value2.intValue = value2.doubleValue != 0;
  1243.             value2.type = TYPE_INT;
  1244.         }
  1245.         valuePtr->intValue = valuePtr->intValue && value2.intValue;
  1246.         break;
  1247.         case OR:
  1248.         if (value2.type == TYPE_DOUBLE) {
  1249.             value2.intValue = value2.doubleValue != 0;
  1250.             value2.type = TYPE_INT;
  1251.         }
  1252.         valuePtr->intValue = valuePtr->intValue || value2.intValue;
  1253.         break;
  1254.  
  1255.         case COLON:
  1256.         interp->result = "can't have : operator without ? first";
  1257.         result = TCL_ERROR;
  1258.         goto done;
  1259.     }
  1260.     }
  1261.  
  1262.     done:
  1263.     if (value2.pv.buffer != value2.staticSpace) {
  1264.     ckfree(value2.pv.buffer);
  1265.     }
  1266.     return result;
  1267.  
  1268.     syntaxError:
  1269.     Tcl_AppendResult(interp, "syntax error in expression \"",
  1270.         infoPtr->originalExpr, "\"", (char *) NULL);
  1271.     result = TCL_ERROR;
  1272.     goto done;
  1273.  
  1274.     illegalType:
  1275.     Tcl_AppendResult(interp, "can't use ", (badType == TYPE_DOUBLE) ?
  1276.         "floating-point value" : "non-numeric string",
  1277.         " as operand of \"", operatorStrings[operator], "\"",
  1278.         (char *) NULL);
  1279.     result = TCL_ERROR;
  1280.     goto done;
  1281. }
  1282.  
  1283. /*
  1284.  *--------------------------------------------------------------
  1285.  *
  1286.  * ExprMakeString --
  1287.  *
  1288.  *    Convert a value from int or double representation to
  1289.  *    a string.
  1290.  *
  1291.  * Results:
  1292.  *    The information at *valuePtr gets converted to string
  1293.  *    format, if it wasn't that way already.
  1294.  *
  1295.  * Side effects:
  1296.  *    None.
  1297.  *
  1298.  *--------------------------------------------------------------
  1299.  */
  1300.  
  1301. static void
  1302. ExprMakeString(interp, valuePtr)
  1303.     Tcl_Interp *interp;            /* Interpreter to use for precision
  1304.                      * information. */
  1305.     register Value *valuePtr;        /* Value to be converted. */
  1306. {
  1307.     int shortfall;
  1308.  
  1309.     shortfall = 150 - (valuePtr->pv.end - valuePtr->pv.buffer);
  1310.     if (shortfall > 0) {
  1311.     (*valuePtr->pv.expandProc)(&valuePtr->pv, shortfall);
  1312.     }
  1313.     if (valuePtr->type == TYPE_INT) {
  1314.     sprintf(valuePtr->pv.buffer, "%ld", valuePtr->intValue);
  1315.     } else if (valuePtr->type == TYPE_DOUBLE) {
  1316.     Tcl_PrintDouble(interp, valuePtr->doubleValue, valuePtr->pv.buffer);
  1317.     }
  1318.     valuePtr->type = TYPE_STRING;
  1319. }
  1320.  
  1321. /*
  1322.  *--------------------------------------------------------------
  1323.  *
  1324.  * ExprTopLevel --
  1325.  *
  1326.  *    This procedure provides top-level functionality shared by
  1327.  *    procedures like Tcl_ExprInt, Tcl_ExprDouble, etc.
  1328.  *
  1329.  * Results:
  1330.  *    The result is a standard Tcl return value.  If an error
  1331.  *    occurs then an error message is left in interp->result.
  1332.  *    The value of the expression is returned in *valuePtr, in
  1333.  *    whatever form it ends up in (could be string or integer
  1334.  *    or double).  Caller may need to convert result.  Caller
  1335.  *    is also responsible for freeing string memory in *valuePtr,
  1336.  *    if any was allocated.
  1337.  *
  1338.  * Side effects:
  1339.  *    None.
  1340.  *
  1341.  *--------------------------------------------------------------
  1342.  */
  1343.  
  1344. static int
  1345. ExprTopLevel(interp, string, valuePtr)
  1346.     Tcl_Interp *interp;            /* Context in which to evaluate the
  1347.                      * expression. */
  1348.     char *string;            /* Expression to evaluate. */
  1349.     Value *valuePtr;            /* Where to store result.  Should
  1350.                      * not be initialized by caller. */
  1351. {
  1352.     ExprInfo info;
  1353.     int result;
  1354.  
  1355.     /*
  1356.      * Create the math functions the first time an expression is
  1357.      * evaluated.
  1358.      */
  1359.  
  1360.     if (!(((Interp *) interp)->flags & EXPR_INITIALIZED)) {
  1361.     BuiltinFunc *funcPtr;
  1362.  
  1363.     ((Interp *) interp)->flags |= EXPR_INITIALIZED;
  1364.     for (funcPtr = funcTable; funcPtr->name != NULL;
  1365.         funcPtr++) {
  1366.         Tcl_CreateMathFunc(interp, funcPtr->name, funcPtr->numArgs,
  1367.             funcPtr->argTypes, funcPtr->proc, funcPtr->clientData);
  1368.     }
  1369.     }
  1370.  
  1371.     info.originalExpr = string;
  1372.     info.expr = string;
  1373.     valuePtr->pv.buffer = valuePtr->pv.next = valuePtr->staticSpace;
  1374.     valuePtr->pv.end = valuePtr->pv.buffer + STATIC_STRING_SPACE - 1;
  1375.     valuePtr->pv.expandProc = TclExpandParseValue;
  1376.     valuePtr->pv.clientData = (ClientData) NULL;
  1377.  
  1378.     result = ExprGetValue(interp, &info, -1, valuePtr);
  1379.     if (result != TCL_OK) {
  1380.     return result;
  1381.     }
  1382.     if (info.token != END) {
  1383.     Tcl_AppendResult(interp, "syntax error in expression \"",
  1384.         string, "\"", (char *) NULL);
  1385.     return TCL_ERROR;
  1386.     }
  1387.     if ((valuePtr->type == TYPE_DOUBLE) && (IS_NAN(valuePtr->doubleValue)
  1388.         || IS_INF(valuePtr->doubleValue))) {
  1389.     /*
  1390.      * IEEE floating-point error.
  1391.      */
  1392.  
  1393.     TclExprFloatError(interp, valuePtr->doubleValue);
  1394.     return TCL_ERROR;
  1395.     }
  1396.     return TCL_OK;
  1397. }
  1398.  
  1399. /*
  1400.  *--------------------------------------------------------------
  1401.  *
  1402.  * Tcl_ExprLong, Tcl_ExprDouble, Tcl_ExprBoolean --
  1403.  *
  1404.  *    Procedures to evaluate an expression and return its value
  1405.  *    in a particular form.
  1406.  *
  1407.  * Results:
  1408.  *    Each of the procedures below returns a standard Tcl result.
  1409.  *    If an error occurs then an error message is left in
  1410.  *    interp->result.  Otherwise the value of the expression,
  1411.  *    in the appropriate form, is stored at *resultPtr.  If
  1412.  *    the expression had a result that was incompatible with the
  1413.  *    desired form then an error is returned.
  1414.  *
  1415.  * Side effects:
  1416.  *    None.
  1417.  *
  1418.  *--------------------------------------------------------------
  1419.  */
  1420.  
  1421. int
  1422. Tcl_ExprLong(interp, string, ptr)
  1423.     Tcl_Interp *interp;            /* Context in which to evaluate the
  1424.                      * expression. */
  1425.     char *string;            /* Expression to evaluate. */
  1426.     long *ptr;                /* Where to store result. */
  1427. {
  1428.     Value value;
  1429.     int result;
  1430.  
  1431.     result = ExprTopLevel(interp, string, &value);
  1432.     if (result == TCL_OK) {
  1433.     if (value.type == TYPE_INT) {
  1434.         *ptr = value.intValue;
  1435.     } else if (value.type == TYPE_DOUBLE) {
  1436.         *ptr = value.doubleValue;
  1437.     } else {
  1438.         interp->result = "expression didn't have numeric value";
  1439.         result = TCL_ERROR;
  1440.     }
  1441.     }
  1442.     if (value.pv.buffer != value.staticSpace) {
  1443.     ckfree(value.pv.buffer);
  1444.     }
  1445.     return result;
  1446. }
  1447.  
  1448. int
  1449. Tcl_ExprDouble(interp, string, ptr)
  1450.     Tcl_Interp *interp;            /* Context in which to evaluate the
  1451.                      * expression. */
  1452.     char *string;            /* Expression to evaluate. */
  1453.     double *ptr;            /* Where to store result. */
  1454. {
  1455.     Value value;
  1456.     int result;
  1457.  
  1458.     result = ExprTopLevel(interp, string, &value);
  1459.     if (result == TCL_OK) {
  1460.     if (value.type == TYPE_INT) {
  1461.         *ptr = value.intValue;
  1462.     } else if (value.type == TYPE_DOUBLE) {
  1463.         *ptr = value.doubleValue;
  1464.     } else {
  1465.         interp->result = "expression didn't have numeric value";
  1466.         result = TCL_ERROR;
  1467.     }
  1468.     }
  1469.     if (value.pv.buffer != value.staticSpace) {
  1470.     ckfree(value.pv.buffer);
  1471.     }
  1472.     return result;
  1473. }
  1474.  
  1475. int
  1476. Tcl_ExprBoolean(interp, string, ptr)
  1477.     Tcl_Interp *interp;            /* Context in which to evaluate the
  1478.                      * expression. */
  1479.     char *string;            /* Expression to evaluate. */
  1480.     int *ptr;                /* Where to store 0/1 result. */
  1481. {
  1482.     Value value;
  1483.     int result;
  1484.  
  1485.     result = ExprTopLevel(interp, string, &value);
  1486.     if (result == TCL_OK) {
  1487.     if (value.type == TYPE_INT) {
  1488.         *ptr = value.intValue != 0;
  1489.     } else if (value.type == TYPE_DOUBLE) {
  1490.         *ptr = value.doubleValue != 0.0;
  1491.     } else {
  1492.         result = Tcl_GetBoolean(interp, value.pv.buffer, ptr);
  1493.     }
  1494.     }
  1495.     if (value.pv.buffer != value.staticSpace) {
  1496.     ckfree(value.pv.buffer);
  1497.     }
  1498.     return result;
  1499. }
  1500.  
  1501. /*
  1502.  *--------------------------------------------------------------
  1503.  *
  1504.  * Tcl_ExprString --
  1505.  *
  1506.  *    Evaluate an expression and return its value in string form.
  1507.  *
  1508.  * Results:
  1509.  *    A standard Tcl result.  If the result is TCL_OK, then the
  1510.  *    interpreter's result is set to the string value of the
  1511.  *    expression.  If the result is TCL_OK, then interp->result
  1512.  *    contains an error message.
  1513.  *
  1514.  * Side effects:
  1515.  *    None.
  1516.  *
  1517.  *--------------------------------------------------------------
  1518.  */
  1519.  
  1520. int
  1521. Tcl_ExprString(interp, string)
  1522.     Tcl_Interp *interp;            /* Context in which to evaluate the
  1523.                      * expression. */
  1524.     char *string;            /* Expression to evaluate. */
  1525. {
  1526.     Value value;
  1527.     int result;
  1528.  
  1529.     result = ExprTopLevel(interp, string, &value);
  1530.     if (result == TCL_OK) {
  1531.     if (value.type == TYPE_INT) {
  1532.         sprintf(interp->result, "%ld", value.intValue);
  1533.     } else if (value.type == TYPE_DOUBLE) {
  1534.         Tcl_PrintDouble(interp, value.doubleValue, interp->result);
  1535.     } else {
  1536.         if (value.pv.buffer != value.staticSpace) {
  1537.         interp->result = value.pv.buffer;
  1538.         interp->freeProc = (Tcl_FreeProc *) free;
  1539.         value.pv.buffer = value.staticSpace;
  1540.         } else {
  1541.         Tcl_SetResult(interp, value.pv.buffer, TCL_VOLATILE);
  1542.         }
  1543.     }
  1544.     }
  1545.     if (value.pv.buffer != value.staticSpace) {
  1546.     ckfree(value.pv.buffer);
  1547.     }
  1548.     return result;
  1549. }
  1550.  
  1551. /*
  1552.  *----------------------------------------------------------------------
  1553.  *
  1554.  * Tcl_CreateMathFunc --
  1555.  *
  1556.  *    Creates a new math function for expressions in a given
  1557.  *    interpreter.
  1558.  *
  1559.  * Results:
  1560.  *    None.
  1561.  *
  1562.  * Side effects:
  1563.  *    The function defined by "name" is created;  if such a function
  1564.  *    already existed then its definition is overriden.
  1565.  *
  1566.  *----------------------------------------------------------------------
  1567.  */
  1568.  
  1569. void
  1570. Tcl_CreateMathFunc(interp, name, numArgs, argTypes, proc, clientData)
  1571.     Tcl_Interp *interp;            /* Interpreter in which function is
  1572.                      * to be available. */
  1573.     char *name;                /* Name of function (e.g. "sin"). */
  1574.     int numArgs;            /* Nnumber of arguments required by
  1575.                      * function. */
  1576.     Tcl_ValueType *argTypes;        /* Array of types acceptable for
  1577.                      * each argument. */
  1578.     Tcl_MathProc *proc;            /* Procedure that implements the
  1579.                      * math function. */
  1580.     ClientData clientData;        /* Additional value to pass to the
  1581.                      * function. */
  1582. {
  1583.     Interp *iPtr = (Interp *) interp;
  1584.     Tcl_HashEntry *hPtr;
  1585.     MathFunc *mathFuncPtr;
  1586.     int new, i;
  1587.  
  1588.     hPtr = Tcl_CreateHashEntry(&iPtr->mathFuncTable, name, &new);
  1589.     if (new) {
  1590.     Tcl_SetHashValue(hPtr, ckalloc(sizeof(MathFunc)));
  1591.     }
  1592.     mathFuncPtr = (MathFunc *) Tcl_GetHashValue(hPtr);
  1593.     if (numArgs > MAX_MATH_ARGS) {
  1594.     numArgs = MAX_MATH_ARGS;
  1595.     }
  1596.     mathFuncPtr->numArgs = numArgs;
  1597.     for (i = 0; i < numArgs; i++) {
  1598.     mathFuncPtr->argTypes[i] = argTypes[i];
  1599.     }
  1600.     mathFuncPtr->proc = proc;
  1601.     mathFuncPtr->clientData = clientData;
  1602. }
  1603.  
  1604. /*
  1605.  *----------------------------------------------------------------------
  1606.  *
  1607.  * ExprMathFunc --
  1608.  *
  1609.  *    This procedure is invoked to parse a math function from an
  1610.  *    expression string, carry out the function, and return the
  1611.  *    value computed.
  1612.  *
  1613.  * Results:
  1614.  *    TCL_OK is returned if all went well and the function's value
  1615.  *    was computed successfully.  If an error occurred, TCL_ERROR
  1616.  *    is returned and an error message is left in interp->result.
  1617.  *    After a successful return infoPtr has been updated to refer
  1618.  *    to the character just after the function call, the token is
  1619.  *    set to VALUE, and the value is stored in valuePtr.
  1620.  *
  1621.  * Side effects:
  1622.  *    Embedded commands could have arbitrary side-effects.
  1623.  *
  1624.  *----------------------------------------------------------------------
  1625.  */
  1626.  
  1627. static int
  1628. ExprMathFunc(interp, infoPtr, valuePtr)
  1629.     Tcl_Interp *interp;            /* Interpreter to use for error
  1630.                      * reporting. */
  1631.     register ExprInfo *infoPtr;        /* Describes the state of the parse.
  1632.                      * infoPtr->expr must point to the
  1633.                      * first character of the function's
  1634.                      * name. */
  1635.     register Value *valuePtr;        /* Where to store value, if that is
  1636.                      * what's parsed from string.  Caller
  1637.                      * must have initialized pv field
  1638.                      * correctly. */
  1639. {
  1640.     Interp *iPtr = (Interp *) interp;
  1641.     MathFunc *mathFuncPtr;        /* Info about math function. */
  1642.     Tcl_Value args[MAX_MATH_ARGS];    /* Arguments for function call. */
  1643.     Tcl_Value funcResult;        /* Result of function call. */
  1644.     Tcl_HashEntry *hPtr;
  1645.     char *p, *funcName;
  1646.     int i, savedChar, result;
  1647.  
  1648.     /*
  1649.      * Find the end of the math function's name and lookup the MathFunc
  1650.      * record for the function.
  1651.      */
  1652.  
  1653.     p = funcName = infoPtr->expr;
  1654.     while (isalnum(UCHAR(*p)) || (*p == '_')) {
  1655.     p++;
  1656.     }
  1657.     infoPtr->expr = p;
  1658.     result = ExprLex(interp, infoPtr, valuePtr);
  1659.     if (result != TCL_OK) {
  1660.     return TCL_ERROR;
  1661.     }
  1662.     if (infoPtr->token != OPEN_PAREN) {
  1663.     goto syntaxError;
  1664.     }
  1665.     savedChar = *p;
  1666.     *p = 0;
  1667.     hPtr = Tcl_FindHashEntry(&iPtr->mathFuncTable, funcName);
  1668.     if (hPtr == NULL) {
  1669.     Tcl_AppendResult(interp, "unknown math function \"", funcName,
  1670.         "\"", (char *) NULL);
  1671.     *p = savedChar;
  1672.     return TCL_ERROR;
  1673.     }
  1674.     *p = savedChar;
  1675.     mathFuncPtr = (MathFunc *) Tcl_GetHashValue(hPtr);
  1676.  
  1677.     /*
  1678.      * Scan off the arguments for the function, if there are any.
  1679.      */
  1680.  
  1681.     if (mathFuncPtr->numArgs == 0) {
  1682.     result = ExprLex(interp, infoPtr, valuePtr);
  1683.     if ((result != TCL_OK) || (infoPtr->token != CLOSE_PAREN)) {
  1684.         goto syntaxError;
  1685.     }
  1686.     } else {
  1687.     for (i = 0; ; i++) {
  1688.         valuePtr->pv.next = valuePtr->pv.buffer;
  1689.         result = ExprGetValue(interp, infoPtr, -1, valuePtr);
  1690.         if (result != TCL_OK) {
  1691.         return result;
  1692.         }
  1693.         if (valuePtr->type == TYPE_STRING) {
  1694.         interp->result =
  1695.             "argument to math function didn't have numeric value";
  1696.         return TCL_ERROR;
  1697.         }
  1698.     
  1699.         /*
  1700.          * Copy the value to the argument record, converting it if
  1701.          * necessary.
  1702.          */
  1703.     
  1704.         if (valuePtr->type == TYPE_INT) {
  1705.         if (mathFuncPtr->argTypes[i] == TCL_DOUBLE) {
  1706.             args[i].type = TCL_DOUBLE;
  1707.             args[i].doubleValue = valuePtr->intValue;
  1708.         } else {
  1709.             args[i].type = TCL_INT;
  1710.             args[i].intValue = valuePtr->intValue;
  1711.         }
  1712.         } else {
  1713.         if (mathFuncPtr->argTypes[i] == TCL_INT) {
  1714.             args[i].type = TCL_INT;
  1715.             args[i].intValue = valuePtr->doubleValue;
  1716.         } else {
  1717.             args[i].type = TCL_DOUBLE;
  1718.             args[i].doubleValue = valuePtr->doubleValue;
  1719.         }
  1720.         }
  1721.     
  1722.         /*
  1723.          * Check for a comma separator between arguments or a close-paren
  1724.          * to end the argument list.
  1725.          */
  1726.     
  1727.         if (i == (mathFuncPtr->numArgs-1)) {
  1728.         if (infoPtr->token == CLOSE_PAREN) {
  1729.             break;
  1730.         }
  1731.         if (infoPtr->token == COMMA) {
  1732.             interp->result = "too many arguments for math function";
  1733.             return TCL_ERROR;
  1734.         } else {
  1735.             goto syntaxError;
  1736.         }
  1737.         }
  1738.         if (infoPtr->token != COMMA) {
  1739.         if (infoPtr->token == CLOSE_PAREN) {
  1740.             interp->result = "too few arguments for math function";
  1741.             return TCL_ERROR;
  1742.         } else {
  1743.             goto syntaxError;
  1744.         }
  1745.         }
  1746.     }
  1747.     }
  1748.     if (iPtr->noEval) {
  1749.     valuePtr->type = TYPE_INT;
  1750.     valuePtr->intValue = 0;
  1751.     infoPtr->token = VALUE;
  1752.     return TCL_OK;
  1753.     }
  1754.  
  1755.     /*
  1756.      * Invoke the function and copy its result back into valuePtr.
  1757.      */
  1758.  
  1759.     tcl_MathInProgress++;
  1760.     result = (*mathFuncPtr->proc)(mathFuncPtr->clientData, interp, args,
  1761.         &funcResult);
  1762.     tcl_MathInProgress--;
  1763.     if (result != TCL_OK) {
  1764.     return result;
  1765.     }
  1766.     if (funcResult.type == TCL_INT) {
  1767.     valuePtr->type = TYPE_INT;
  1768.     valuePtr->intValue = funcResult.intValue;
  1769.     } else {
  1770.     valuePtr->type = TYPE_DOUBLE;
  1771.     valuePtr->doubleValue = funcResult.doubleValue;
  1772.     }
  1773.     infoPtr->token = VALUE;
  1774.     return TCL_OK;
  1775.  
  1776.     syntaxError:
  1777.     Tcl_AppendResult(interp, "syntax error in expression \"",
  1778.         infoPtr->originalExpr, "\"", (char *) NULL);
  1779.     return TCL_ERROR;
  1780. }
  1781.  
  1782. /*
  1783.  *----------------------------------------------------------------------
  1784.  *
  1785.  * TclExprFloatError --
  1786.  *
  1787.  *    This procedure is called when an error occurs during a
  1788.  *    floating-point operation.  It reads errno and sets
  1789.  *    interp->result accordingly.
  1790.  *
  1791.  * Results:
  1792.  *    Interp->result is set to hold an error message.
  1793.  *
  1794.  * Side effects:
  1795.  *    None.
  1796.  *
  1797.  *----------------------------------------------------------------------
  1798.  */
  1799.  
  1800. void
  1801. TclExprFloatError(interp, value)
  1802.     Tcl_Interp *interp;        /* Where to store error message. */
  1803.     double value;        /* Value returned after error;  used to
  1804.                  * distinguish underflows from overflows. */
  1805. {
  1806.     char buf[20];
  1807.  
  1808.     if ((errno == EDOM) || (value != value)) {
  1809.     interp->result = "domain error: argument not in valid range";
  1810.     Tcl_SetErrorCode(interp, "ARITH", "DOMAIN", interp->result,
  1811.         (char *) NULL);
  1812.     } else if ((errno == ERANGE) || IS_INF(value)) {
  1813.     if (value == 0.0) {
  1814.         interp->result = "floating-point value too small to represent";
  1815.         Tcl_SetErrorCode(interp, "ARITH", "UNDERFLOW", interp->result,
  1816.             (char *) NULL);
  1817.     } else {
  1818.         interp->result = "floating-point value too large to represent";
  1819.         Tcl_SetErrorCode(interp, "ARITH", "OVERFLOW", interp->result,
  1820.             (char *) NULL);
  1821.     }
  1822.     } else {
  1823.     sprintf(buf, "%d", errno);
  1824.     Tcl_AppendResult(interp, "unknown floating-point error, ",
  1825.         "errno = ", buf, (char *) NULL);
  1826.     Tcl_SetErrorCode(interp, "ARITH", "UNKNOWN", interp->result,
  1827.         (char *) NULL);
  1828.     }
  1829. }
  1830.  
  1831. /*
  1832.  *----------------------------------------------------------------------
  1833.  *
  1834.  * Math Functions --
  1835.  *
  1836.  *    This page contains the procedures that implement all of the
  1837.  *    built-in math functions for expressions.
  1838.  *
  1839.  * Results:
  1840.  *    Each procedure returns TCL_OK if it succeeds and places result
  1841.  *    information at *resultPtr.  If it fails it returns TCL_ERROR
  1842.  *    and leaves an error message in interp->result.
  1843.  *
  1844.  * Side effects:
  1845.  *    None.
  1846.  *
  1847.  *----------------------------------------------------------------------
  1848.  */
  1849.  
  1850. static int
  1851. ExprUnaryFunc(clientData, interp, args, resultPtr)
  1852.     ClientData clientData;        /* Contains address of procedure that
  1853.                      * takes one double argument and
  1854.                      * returns a double result. */
  1855.     Tcl_Interp *interp;
  1856.     Tcl_Value *args;
  1857.     Tcl_Value *resultPtr;
  1858. {
  1859.     double (*func)() = (double (*)()) clientData;
  1860.  
  1861.     errno = 0;
  1862.     resultPtr->type = TCL_DOUBLE;
  1863.     resultPtr->doubleValue = (*func)(args[0].doubleValue);
  1864.     if (errno != 0) {
  1865.     TclExprFloatError(interp, resultPtr->doubleValue);
  1866.     return TCL_ERROR;
  1867.     }
  1868.     return TCL_OK;
  1869. }
  1870.  
  1871. static int
  1872. ExprBinaryFunc(clientData, interp, args, resultPtr)
  1873.     ClientData clientData;        /* Contains address of procedure that
  1874.                      * takes two double arguments and
  1875.                      * returns a double result. */
  1876.     Tcl_Interp *interp;
  1877.     Tcl_Value *args;
  1878.     Tcl_Value *resultPtr;
  1879. {
  1880.     double (*func)() = (double (*)()) clientData;
  1881.  
  1882.     errno = 0;
  1883.     resultPtr->type = TCL_DOUBLE;
  1884.     resultPtr->doubleValue = (*func)(args[0].doubleValue, args[1].doubleValue);
  1885.     if (errno != 0) {
  1886.     TclExprFloatError(interp, resultPtr->doubleValue);
  1887.     return TCL_ERROR;
  1888.     }
  1889.     return TCL_OK;
  1890. }
  1891.  
  1892.     /* ARGSUSED */
  1893. static int
  1894. ExprAbsFunc(clientData, interp, args, resultPtr)
  1895.     ClientData clientData;
  1896.     Tcl_Interp *interp;
  1897.     Tcl_Value *args;
  1898.     Tcl_Value *resultPtr;
  1899. {
  1900.     resultPtr->type = TCL_DOUBLE;
  1901.     if (args[0].type == TCL_DOUBLE) {
  1902.     resultPtr->type = TCL_DOUBLE;
  1903.     if (args[0].doubleValue < 0) {
  1904.         resultPtr->doubleValue = -args[0].doubleValue;
  1905.     } else {
  1906.         resultPtr->doubleValue = args[0].doubleValue;
  1907.     }
  1908.     } else {
  1909.     resultPtr->type = TCL_INT;
  1910.     if (args[0].intValue < 0) {
  1911.         resultPtr->intValue = -args[0].intValue;
  1912.         if (resultPtr->intValue < 0) {
  1913.         interp->result = "integer value too large to represent";
  1914.         Tcl_SetErrorCode(interp, "ARITH", "IOVERFLOW", interp->result,
  1915.             (char *) NULL);
  1916.         return TCL_ERROR;
  1917.         }
  1918.     } else {
  1919.         resultPtr->intValue = args[0].intValue;
  1920.     }
  1921.     }
  1922.     return TCL_OK;
  1923. }
  1924.  
  1925.     /* ARGSUSED */
  1926. static int
  1927. ExprDoubleFunc(clientData, interp, args, resultPtr)
  1928.     ClientData clientData;
  1929.     Tcl_Interp *interp;
  1930.     Tcl_Value *args;
  1931.     Tcl_Value *resultPtr;
  1932. {
  1933.     resultPtr->type = TCL_DOUBLE;
  1934.     if (args[0].type == TCL_DOUBLE) {
  1935.     resultPtr->doubleValue = args[0].doubleValue;
  1936.     } else {
  1937.     resultPtr->doubleValue = args[0].intValue;
  1938.     }
  1939.     return TCL_OK;
  1940. }
  1941.  
  1942.     /* ARGSUSED */
  1943. static int
  1944. ExprIntFunc(clientData, interp, args, resultPtr)
  1945.     ClientData clientData;
  1946.     Tcl_Interp *interp;
  1947.     Tcl_Value *args;
  1948.     Tcl_Value *resultPtr;
  1949. {
  1950.     resultPtr->type = TCL_INT;
  1951.     if (args[0].type == TCL_INT) {
  1952.     resultPtr->intValue = args[0].intValue;
  1953.     } else {
  1954.     if (args[0].doubleValue < 0) {
  1955.         if (args[0].doubleValue < (double) (long) LONG_MIN) {
  1956.         tooLarge:
  1957.         interp->result = "integer value too large to represent";
  1958.         Tcl_SetErrorCode(interp, "ARITH", "IOVERFLOW",
  1959.             interp->result, (char *) NULL);
  1960.         return TCL_ERROR;
  1961.         }
  1962.     } else {
  1963.         if (args[0].doubleValue > (double) LONG_MAX) {
  1964.         goto tooLarge;
  1965.         }
  1966.     }
  1967.     resultPtr->intValue = args[0].doubleValue;
  1968.     }
  1969.     return TCL_OK;
  1970. }
  1971.  
  1972.     /* ARGSUSED */
  1973. static int
  1974. ExprRoundFunc(clientData, interp, args, resultPtr)
  1975.     ClientData clientData;
  1976.     Tcl_Interp *interp;
  1977.     Tcl_Value *args;
  1978.     Tcl_Value *resultPtr;
  1979. {
  1980.     resultPtr->type = TCL_INT;
  1981.     if (args[0].type == TCL_INT) {
  1982.     resultPtr->intValue = args[0].intValue;
  1983.     } else {
  1984.     if (args[0].doubleValue < 0) {
  1985.         if (args[0].doubleValue <= (((double) (long) LONG_MIN) - 0.5)) {
  1986.         tooLarge:
  1987.         interp->result = "integer value too large to represent";
  1988.         Tcl_SetErrorCode(interp, "ARITH", "IOVERFLOW",
  1989.             interp->result, (char *) NULL);
  1990.         return TCL_ERROR;
  1991.         }
  1992.         resultPtr->intValue = (args[0].doubleValue - 0.5);
  1993.     } else {
  1994.         if (args[0].doubleValue >= (((double) LONG_MAX + 0.5))) {
  1995.         goto tooLarge;
  1996.         }
  1997.         resultPtr->intValue = (args[0].doubleValue + 0.5);
  1998.     }
  1999.     }
  2000.     return TCL_OK;
  2001. }
  2002.  
  2003. /*
  2004.  *----------------------------------------------------------------------
  2005.  *
  2006.  * ExprLooksLikeInt --
  2007.  *
  2008.  *    This procedure decides whether the leading characters of a
  2009.  *    string look like an integer or something else (such as a
  2010.  *    floating-point number or string).
  2011.  *
  2012.  * Results:
  2013.  *    The return value is 1 if the leading characters of p look
  2014.  *    like a valid Tcl integer.  If they look like a floating-point
  2015.  *    number (e.g. "e01" or "2.4"), or if they don't look like a
  2016.  *    number at all, then 0 is returned.
  2017.  *
  2018.  * Side effects:
  2019.  *    None.
  2020.  *
  2021.  *----------------------------------------------------------------------
  2022.  */
  2023.  
  2024. static int
  2025. ExprLooksLikeInt(p)
  2026.     char *p;            /* Pointer to string. */
  2027. {
  2028.     while (isspace(UCHAR(*p))) {
  2029.     p++;
  2030.     }
  2031.     if ((*p == '+') || (*p == '-')) {
  2032.     p++;
  2033.     }
  2034.     if (!isdigit(UCHAR(*p))) {
  2035.     return 0;
  2036.     }
  2037.     p++;
  2038.     while (isdigit(UCHAR(*p))) {
  2039.     p++;
  2040.     }
  2041.     if ((*p != '.') && (*p != 'e') && (*p != 'E')) {
  2042.     return 1;
  2043.     }
  2044.     return 0;
  2045. }
  2046.