diff options
author | hobbs <hobbs> | 2000-05-09 00:00:34 (GMT) |
---|---|---|
committer | hobbs <hobbs> | 2000-05-09 00:00:34 (GMT) |
commit | 427c904742d9d5aec8068fce38a28be9ae65af08 (patch) | |
tree | 1e3bab60e826e937b67fa7d70d97f4f352fe315a /generic | |
parent | 9f7b9b72befea46deb71233146973eea88223af0 (diff) | |
download | tcl-427c904742d9d5aec8068fce38a28be9ae65af08.zip tcl-427c904742d9d5aec8068fce38a28be9ae65af08.tar.gz tcl-427c904742d9d5aec8068fce38a28be9ae65af08.tar.bz2 |
* doc/expr.n:
* tests/expr.test:
* tests/expr-old.test: added tests for 'eq' and 'ne'
* generic/tclExecute.c:
* generic/tclCompile.h: added INST_STREQ and INST_STRNEQ opcodes
that do strict string comparisons.
* generic/tclCompExpr.c: added 'eq' and 'ne' string comparison
operators.
* generic/tclParseExpr.c (GetLexeme): added 'eq' and 'ne' expr
parse terms (string (in)equality check).
Diffstat (limited to 'generic')
-rw-r--r-- | generic/tclCompExpr.c | 10 | ||||
-rw-r--r-- | generic/tclCompile.h | 9 | ||||
-rw-r--r-- | generic/tclExecute.c | 55 | ||||
-rw-r--r-- | generic/tclParseExpr.c | 42 |
4 files changed, 104 insertions, 12 deletions
diff --git a/generic/tclCompExpr.c b/generic/tclCompExpr.c index 0b8fabf..8a781b9 100644 --- a/generic/tclCompExpr.c +++ b/generic/tclCompExpr.c @@ -4,11 +4,12 @@ * This file contains the code to compile Tcl expressions. * * Copyright (c) 1997 Sun Microsystems, Inc. + * Copyright (c) 1998-2000 by Scriptics Corporation. * * See the file "license.terms" for information on usage and redistribution * of this file, and for a DISCLAIMER OF ALL WARRANTIES. * - * RCS: @(#) $Id: tclCompExpr.c,v 1.4 1999/08/19 02:59:08 hobbs Exp $ + * RCS: @(#) $Id: tclCompExpr.c,v 1.5 2000/05/09 00:00:34 hobbs Exp $ */ #include "tclInt.h" @@ -101,6 +102,8 @@ typedef struct ExprInfo { #define OP_QUESTY 18 #define OP_LNOT 19 #define OP_BITNOT 20 +#define OP_STREQ 21 +#define OP_STRNEQ 22 /* * Table describing the expression operators. Entries in this table must @@ -141,6 +144,8 @@ OperatorDesc operatorTable[] = { {"?", 0}, {"!", 1, INST_LNOT}, {"~", 1, INST_BITNOT}, + {"eq", 2, INST_STREQ}, + {"ne", 2, INST_STRNEQ}, {NULL} }; @@ -536,7 +541,8 @@ CompileSubExpr(exprTokenPtr, infoPtr, envPtr) infoPtr->hasOperators = 1; infoPtr->exprIsJustVarRef = 0; infoPtr->exprIsComparison = - ((opIndex >= OP_LESS) && (opIndex <= OP_NEQ)); + (((opIndex >= OP_LESS) && (opIndex <= OP_NEQ)) + || ((opIndex >= OP_STREQ) && (opIndex <= OP_STRNEQ))); break; } diff --git a/generic/tclCompile.h b/generic/tclCompile.h index 310e67e..aa72c93 100644 --- a/generic/tclCompile.h +++ b/generic/tclCompile.h @@ -2,11 +2,12 @@ * tclCompile.h -- * * Copyright (c) 1996-1998 Sun Microsystems, Inc. + * Copyright (c) 1998-2000 by Scriptics Corporation. * * See the file "license.terms" for information on usage and redistribution * of this file, and for a DISCLAIMER OF ALL WARRANTIES. * - * RCS: @(#) $Id: tclCompile.h,v 1.11 1999/05/23 16:37:14 surles Exp $ + * RCS: @(#) $Id: tclCompile.h,v 1.12 2000/05/09 00:00:34 hobbs Exp $ */ #ifndef _TCLCOMPILATION @@ -493,8 +494,12 @@ typedef struct ByteCode { #define INST_PUSH_RESULT 71 #define INST_PUSH_RETURN_CODE 72 +/* Opcodes 73 to 74 */ +#define INST_STREQ 73 +#define INST_STRNEQ 74 + /* The last opcode */ -#define LAST_INST_OPCODE 72 +#define LAST_INST_OPCODE 74 /* * Table describing the Tcl bytecode instructions: their name (for diff --git a/generic/tclExecute.c b/generic/tclExecute.c index bc026b3..f19a968 100644 --- a/generic/tclExecute.c +++ b/generic/tclExecute.c @@ -5,11 +5,12 @@ * commands. * * Copyright (c) 1996-1997 Sun Microsystems, Inc. + * Copyright (c) 1998-2000 by Scriptics Corporation. * * See the file "license.terms" for information on usage and redistribution * of this file, and for a DISCLAIMER OF ALL WARRANTIES. * - * RCS: @(#) $Id: tclExecute.c,v 1.10 2000/03/27 22:18:55 hobbs Exp $ + * RCS: @(#) $Id: tclExecute.c,v 1.11 2000/05/09 00:00:34 hobbs Exp $ */ #include "tclInt.h" @@ -98,7 +99,8 @@ int (*tclMatherrPtr)() = matherr; static char *operatorStrings[] = { "||", "&&", "|", "^", "&", "==", "!=", "<", ">", "<=", ">=", "<<", ">>", "+", "-", "*", "/", "%", "+", "-", "~", "!", - "BUILTIN FUNCTION", "FUNCTION" + "BUILTIN FUNCTION", "FUNCTION", + "", "", "", "", "", "", "", "", "eq", "ne", }; /* @@ -1755,6 +1757,55 @@ TclExecuteByteCode(interp, codePtr) } ADJUST_PC(1); + case INST_STREQ: + case INST_STRNEQ: + { + /* + * String (in)equality check + */ + char *s1, *s2; + int s1len, s2len; + long iResult; + + value2Ptr = POP_OBJECT(); + valuePtr = POP_OBJECT(); + + s1 = Tcl_GetStringFromObj(valuePtr, &s1len); + s2 = Tcl_GetStringFromObj(value2Ptr, &s2len); + if (s1len == s2len) { + /* + * We only need to check (in)equality when we have equal + * length strings. + */ + if (*pc == INST_STRNEQ) { + iResult = (strcmp(s1, s2) != 0); + } else { + /* INST_STREQ */ + iResult = (strcmp(s1, s2) == 0); + } + } else { + iResult = (*pc == INST_STRNEQ); + } + + /* + * Reuse the valuePtr object already on stack if possible. + */ + + if (Tcl_IsShared(valuePtr)) { + PUSH_OBJECT(Tcl_NewLongObj(iResult)); + TRACE(("%.20s %.20s => %ld\n", + O2S(valuePtr), O2S(value2Ptr), iResult)); + TclDecrRefCount(valuePtr); + } else { /* reuse the valuePtr object */ + TRACE(("%.20s %.20s => %ld\n", + O2S(valuePtr), O2S(value2Ptr), iResult)); + Tcl_SetLongObj(valuePtr, iResult); + ++stackTop; /* valuePtr now on stk top has right r.c. */ + } + TclDecrRefCount(value2Ptr); + } + ADJUST_PC(1); + case INST_EQ: case INST_NEQ: case INST_LT: diff --git a/generic/tclParseExpr.c b/generic/tclParseExpr.c index b9c9d71..fbdd9ea 100644 --- a/generic/tclParseExpr.c +++ b/generic/tclParseExpr.c @@ -7,11 +7,12 @@ * code analysis, etc. * * Copyright (c) 1997 Sun Microsystems, Inc. + * Copyright (c) 1998-2000 by Scriptics Corporation. * * See the file "license.terms" for information on usage and redistribution * of this file, and for a DISCLAIMER OF ALL WARRANTIES. * - * RCS: @(#) $Id: tclParseExpr.c,v 1.6 1999/12/04 06:15:42 hobbs Exp $ + * RCS: @(#) $Id: tclParseExpr.c,v 1.7 2000/05/09 00:00:35 hobbs Exp $ */ #include "tclInt.h" @@ -118,6 +119,9 @@ typedef struct ParseInfo { #define NOT 31 #define BIT_NOT 32 +#define STREQ 33 +#define STRNEQ 34 + /* * Mapping from lexemes to strings; used for debugging messages. These * entries must match the order and number of the lexeme definitions above. @@ -130,7 +134,7 @@ static char *lexemeStrings[] = { "*", "/", "%", "+", "-", "<<", ">>", "<", ">", "<=", ">=", "==", "!=", "&", "^", "|", "&&", "||", "?", ":", - "!", "~" + "!", "~", "eq", "ne", }; #endif /* TCL_COMPILE_DEBUG */ @@ -720,7 +724,8 @@ ParseBitAndExpr(infoPtr) * ParseEqualityExpr -- * * This procedure parses a Tcl equality (inequality) expression: - * equalityExpr ::= relationalExpr {('==' | '!=') relationalExpr} + * equalityExpr ::= relationalExpr + * {('==' | '!=' | 'ne' | 'eq') relationalExpr} * * Results: * The return value is TCL_OK on a successful parse and TCL_ERROR @@ -754,9 +759,10 @@ ParseEqualityExpr(infoPtr) } lexeme = infoPtr->lexeme; - while ((lexeme == EQUAL) || (lexeme == NEQ)) { + while ((lexeme == EQUAL) || (lexeme == NEQ) + || (lexeme == STREQ) || (lexeme == STRNEQ)) { operator = infoPtr->start; - code = GetLexeme(infoPtr); /* skip over == or != */ + code = GetLexeme(infoPtr); /* skip over ==, !=, 'eq' or 'ne' */ if (code != TCL_OK) { return code; } @@ -766,7 +772,8 @@ ParseEqualityExpr(infoPtr) } /* - * Generate tokens for the subexpression and '==' or '!=' operator. + * Generate tokens for the subexpression and '==', '!=', 'eq' or 'ne' + * operator. */ PrependSubExprTokens(operator, 2, srcStart, @@ -1735,7 +1742,30 @@ GetLexeme(infoPtr) infoPtr->lexeme = BIT_NOT; return TCL_OK; + case 'e': + if (src[1] == 'q') { + infoPtr->lexeme = STREQ; + infoPtr->size = 2; + infoPtr->next = src+2; + parsePtr->term = infoPtr->next; + return TCL_OK; + } else { + goto checkFuncName; + } + + case 'n': + if (src[1] == 'e') { + infoPtr->lexeme = STRNEQ; + infoPtr->size = 2; + infoPtr->next = src+2; + parsePtr->term = infoPtr->next; + return TCL_OK; + } else { + goto checkFuncName; + } + default: + checkFuncName: offset = Tcl_UtfToUniChar(src, &ch); c = UCHAR(ch); if (isalpha(UCHAR(c))) { /* INTL: ISO only. */ |