expr.c

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 716 行 · 第 1/2 页

C
716
字号
/****************************************************************************
*
*                            Open Watcom Project
*
*    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
*
*  ========================================================================
*
*    This file contains Original Code and/or Modifications of Original
*    Code as defined in and that are subject to the Sybase Open Watcom
*    Public License version 1.0 (the 'License'). You may not use this file
*    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
*    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
*    provided with the Original Code and Modifications, and is also
*    available at www.sybase.com/developer/opensource.
*
*    The Original Code and all software distributed under the License are
*    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
*    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
*    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
*    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
*    NON-INFRINGEMENT. Please see the License for the specific language
*    governing rights and limitations under the License.
*
*  ========================================================================
*
* Description:  WHEN YOU FIGURE OUT WHAT THIS FILE DOES, PLEASE
*               DESCRIBE IT HERE!
*
****************************************************************************/


#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <setjmp.h>
#include "vi.h"
#include "source.h"
#include "expr.h"

static long _NEAR cExpr1( void );
static long _NEAR cExpr2( void );
static long _NEAR cExpr3( void );
static long _NEAR cExpr4( void );
static long _NEAR cExpr5( void );
static long _NEAR cExpr6( void );
static long _NEAR cExpr7( void );
static long _NEAR cExpr8( void );
static long _NEAR cExpr9( void );
static long _NEAR cExpr10( void );
static long _NEAR cExpr11( void );
static long _NEAR cExpr12( void );
static token nextToken( void );

static char     wasString;
static char     lastString[TBUFF_SIZE];
static int      nextCh;
static token    currToken;
static char     tokenBuff[TBUFF_SIZE];
static long     constantVal;
static char     *exprData;
static jmp_buf  abortAddr;
static int      tokenBuffCnt;

static char     colorTokens[] = {
    "BLACK\0"
    "BLUE\0"
    "GREEN\0"
    "CYAN\0"
    "RED\0"
    "MAGENTA\0"
    "BROWN\0"
    "WHITE\0"
    "DARK_GRAY\0"
    "LIGHT_BLUE\0"
    "LIGHT_GREEN\0"
    "LIGHT_CYAN\0"
    "LIGHT_RED\0"
    "LIGHT_MAGENTA\0"
    "YELLOW\0"
    "BRIGHT_WHITE\0"
};

#ifdef __WIN__
static char     ddeTokens[] = {
    "DDE_FACK\0"
    "DDE_FBUSY\0"
    "DDE_FDEFERUPD\0"
    "DDE_FACKREQ\0"
    "DDE_FRELEASE\0"
    "DDE_FREQUESTED\0"
    "DDE_FAPPSTATUS\0"
    "DDE_FNOTPROCESSED\0"
    "XTYP_CONNECT\0"
    "XTYP_CONNECT_CONFIRM\0"
    "XTYP_DISCONNECT\0"
    "XTYP_REQUEST\0"
    "XTYP_POKE\0"
    "\0"
};

static unsigned long ddeNums[] = {
    0x8000,
    0x4000,
    0x4000,
    0x8000,
    0x2000,
    0x1000,
    0x00ff,
    0x0000,
    0x1062,
    0x8072,
    0x80c2,
    0x20b0,
    0x4090
};
#endif

/*
 * nextChar - fetch next character in buffer
 */
static void nextChar( void )
{
    if( exprData == NULL ) {
        nextCh = 0;
    } else {
        nextCh = *exprData;
        exprData++;
        if( nextCh == 0 ) {
            exprData = NULL;
        }
    }
}

/*
 * StartExprParse - get read to parse an expression
 */
void StartExprParse( char *data, jmp_buf abort_addr )
{
    exprData = data;
    memcpy( abortAddr, abort_addr, sizeof( jmp_buf ) );
    nextChar();
    nextToken();

} /* StartExprParse */

static void Abort( int err )
{
    longjmp( abortAddr, err );
}

/*
 * _nextToken - get the next raw token
 */
static token _nextToken( void )
{
    char        ch;

    tokenBuffCnt = 0;
    while( 1 ) {
        ch = nextCh;
        if( ch == 0 ) {
            nextChar();
            tokenBuff[tokenBuffCnt] = 0;
            if( tokenBuffCnt == 0 ) {
                return( T_EOF );
            }
            break;
        }
        if( isspace( ch ) ) {
            nextChar();
            if( tokenBuffCnt > 0 ) {
                break;
            }
            continue;
        }
        if( ch == '"' ) {
            if( tokenBuffCnt == 0 ) {
                while( 1 ) {
                    nextChar();
                    if( nextCh == '"' || nextCh == 0 ) {
                        nextChar();
                        tokenBuff[tokenBuffCnt] = 0;
                        return( T_STRING );
                    }
                    tokenBuff[tokenBuffCnt++] = nextCh;
                }
            }
            break;
        }
        if( ch == '%' ) {
            if( tokenBuffCnt == 0 ) {
                nextChar();
                return( T_PERCENT );
            }
            break;
        }
        if( ch == '+' ) {
            if( tokenBuffCnt == 0 ) {
                nextChar();
                return( T_PLUS );
            }
            break;
        }
        if( ch == '^' ) {
            if( tokenBuffCnt == 0 ) {
                nextChar();
                return( T_XOR );
            }
            break;
        }
        if( ch == '~' ) {
            if( tokenBuffCnt == 0 ) {
                nextChar();
                return( T_TILDE );
            }
            break;
        }
        if( ch == '-' ) {
            if( tokenBuffCnt == 0 ) {
                nextChar();
                return( T_MINUS );
            }
            break;
        }
        if( ch == '*' ) {
            if( tokenBuffCnt == 0 ) {
                nextChar();
                if( nextCh == '*' ) {
                    nextChar();
                    return( T_EXPONENT );
                }
                return( T_TIMES );
            }
            break;
        }
        if( ch == '/' ) {
            if( tokenBuffCnt == 0 ) {
                nextChar();
                return( T_DIVIDE );
            }
            break;
        }
        if( ch == '(' ) {
            if( tokenBuffCnt == 0 ) {
                nextChar();
                return( T_LEFT_PAREN );
            }
            break;
        }
        if( ch == ')' ) {
            if( tokenBuffCnt == 0 ) {
                nextChar();
                return( T_RIGHT_PAREN );
            }
            break;
        }
        if( ch == ':' ) {
            if( tokenBuffCnt == 0 ) {
                nextChar();
                return( T_COLON );
            }
            break;
        }
        if( ch == '?' ) {
            if( tokenBuffCnt == 0 ) {
                nextChar();
                return( T_QUESTION );
            }
            break;
        }
        if( ch == '>' || ch == '<' || ch == '=' || ch == '!' ||
            ch == '|' || ch == '&' ) {
            if( tokenBuffCnt > 0 ) {
                break;
            }
            nextChar();
            if( ch == '|' ) {
                if( nextCh == '|' ) {
                    nextChar();
                    return( T_OR_OR );
                }
                return( T_OR );
            }
            if( ch == '&' ) {
                if( nextCh == '&' ) {
                    nextChar();
                    return( T_AND_AND );
                }
                return( T_AND );
            }
            if( ch == '>' ) {
                if( nextCh == '=' ) {
                    nextChar();
                    return( T_GE );
                }
                if( nextCh == '>' ) {
                    nextChar();
                    return( T_RSHIFT );
                }
                return( T_GT );
            }
            if( ch == '<' ) {
                if( nextCh == '=' ) {
                    nextChar();
                    return( T_LE );
                }
                if( nextCh == '<' ) {
                    nextChar();
                    return( T_LSHIFT );
                }
                return( T_LT );
            }
            if( ch == '!' ) {
                if( nextCh == '=' ) {
                    nextChar();
                    return( T_NE );
                }
                return( T_EXCLAMATION );
            }
            if( ch == '=' ) {
                if( nextCh == '=' ) {
                    nextChar();
                    return( T_EQ );
                }
            }
        }
        tokenBuff[tokenBuffCnt++] = ch;
        nextChar();
    }
    tokenBuff[tokenBuffCnt] = 0;
    return( T_UNKNOWN );

} /* _nextToken */

/*
 * nextToken - fetch the next real token from the buffer
 */
static token nextToken( void )
{
    int         j;
    extern long LastRC;
    char        *endptr;

    currToken = _nextToken();
    if( currToken == T_UNKNOWN ) {
        currToken = T_CONSTANT;
        if( isdigit( tokenBuff[0] ) ) {
            constantVal = strtol( tokenBuff, &endptr, 10 );
            if( (endptr - tokenBuff) != tokenBuffCnt ) {
                constantVal = strtol( tokenBuff, &endptr, 16 );
                if( (endptr - tokenBuff) != tokenBuffCnt ) {
                    Abort( ERR_INVALID_VALUE );
                }
            }
        } else {
            if( tokenBuff[0] == '.' ) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?