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