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