Tcl Source Code

Check-in [ceec540b41]
Login
Bounty program for improvements to Tcl and certain Tcl packages.
Tcl 2019 Conference, Houston/TX, US, Nov 4-8
Send your abstracts to [email protected]
or submit via the online form by Sep 9.

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add support for arrays in assignment Lvalues
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | bg-tip-282
Files: files | file ages | folders
SHA1: ceec540b41aa13609e9407d5582a217f523a9d08
User & Date: griffin 2017-02-22 00:39:02
Original User & Date: briang42 2017-02-22 00:39:02
Context
2017-02-26
13:22
Create new branch named "avl-tip-282" check-in: c14187f8b5 user: avl tags: avl-tip-282
2017-02-25
16:22
Some cleanup check-in: b8e3a1cb71 user: griffin tags: bg-tip-282
2017-02-22
00:39
Add support for arrays in assignment Lvalues check-in: ceec540b41 user: griffin tags: bg-tip-282
2017-02-21
01:35
Add [::tcl::unsupported::parseexpr] to display expr parse trees prettily check-in: 4a658ddd10 user: ferrieux tags: bg-tip-282
2017-02-19
01:06
namespace qualifier support, assignment target limitations. check-in: d64c1ea4de user: griffin tags: bg-tip-282
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to generic/tclBasic.c.

832
833
834
835
836
837
838



839
840
841
842
843
844
845
    Tcl_CreateObjCommand(interp, "::tcl::unsupported::disassemble",
	    Tcl_DisassembleObjCmd, INT2PTR(0), NULL);
    Tcl_CreateObjCommand(interp, "::tcl::unsupported::getbytecode",
	    Tcl_DisassembleObjCmd, INT2PTR(1), NULL);
    Tcl_CreateObjCommand(interp, "::tcl::unsupported::representation",
	    Tcl_RepresentationCmd, NULL, NULL);




    /* Adding the bytecode assembler command */
    cmdPtr = (Command *) Tcl_NRCreateCommand(interp,
            "::tcl::unsupported::assemble", Tcl_AssembleObjCmd,
            TclNRAssembleObjCmd, NULL, NULL);
    cmdPtr->compileProc = &TclCompileAssembleCmd;







>
>
>







832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
    Tcl_CreateObjCommand(interp, "::tcl::unsupported::disassemble",
	    Tcl_DisassembleObjCmd, INT2PTR(0), NULL);
    Tcl_CreateObjCommand(interp, "::tcl::unsupported::getbytecode",
	    Tcl_DisassembleObjCmd, INT2PTR(1), NULL);
    Tcl_CreateObjCommand(interp, "::tcl::unsupported::representation",
	    Tcl_RepresentationCmd, NULL, NULL);
    Tcl_CreateObjCommand(interp, "::tcl::unsupported::parseexpr",
	    Tcl_ParseAndDumpExprObjCmd, NULL, NULL);
    

    /* Adding the bytecode assembler command */
    cmdPtr = (Command *) Tcl_NRCreateCommand(interp,
            "::tcl::unsupported::assemble", Tcl_AssembleObjCmd,
            TclNRAssembleObjCmd, NULL, NULL);
    cmdPtr->compileProc = &TclCompileAssembleCmd;

Changes to generic/tclCompExpr.c.

730
731
732
733
734
735
736


































737
738
739
740
741
742
743
744
745
746


747
748
749
750


751
752
753
754
755
756
757
...
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
...
882
883
884
885
886
887
888















889
890
891
892
893
894
895
...
927
928
929
930
931
932
933







934
935
936
937
938
939
940
....
1491
1492
1493
1494
1495
1496
1497

























































































































































































































































































1498
1499
1500
1501
1502
1503
1504
		 * exceptions are that when a bareword is followed by an open
		 * paren, it might be a function call, and when the bareword
		 * is a legal literal boolean value, we accept that as well.
		 */

		if (start[scanned+TclParseAllWhiteSpace(
			start+scanned, numBytes-scanned)] == '(') {


































		    lexeme = FUNCTION;

		    /*
		     * When we compile the expression we'll need the function
		     * name, and there's no place in the parse tree to store
		     * it, so we keep a separate list of all the function
		     * names we've parsed in the order we found them.
		     */

		    Tcl_ListObjAppendElement(NULL, funcList, literal);


		} else if (start[scanned+TclParseAllWhiteSpace(
			start+scanned, numBytes-scanned)] == ':' &&
			   start[scanned+TclParseAllWhiteSpace(
			start+scanned, numBytes-scanned)+1] == '=') {


		    lexeme = VARNAME;

		    /* The variable name is stored as an OT_LITERAL below */

		} else if (Tcl_GetBooleanFromObj(NULL,literal,&b) == TCL_OK) {
		    lexeme = BOOLEAN;
		} else {
................................................................................
		}
		goto error;
	    }

	    switch (lexeme) {
	    case NUMBER:
	    case BOOLEAN:
	    case VARNAME:
		/*
		 * TODO: Consider using a dict or hash to collapse all
		 * duplicate literals into a single representative value.
		 * (Like what is done with [split $s {}]).
		 * Pro:	~75% memory saving on expressions like
		 *	{1+1+1+1+1+.....+1} (Convert "pointer + Tcl_Obj" cost
		 *	to "pointer" cost only)
................................................................................

		Tcl_ListObjAppendElement(NULL, litList, literal);
		complete = lastParsed = OT_LITERAL;
		start += scanned;
		numBytes -= scanned;
		continue;
















	    default:
		break;
	    }

	    /*
	     * Remaining LEAF cases may involve filling Tcl_Tokens, so make
	     * room for at least 2 more tokens.
................................................................................
		if (code == TCL_OK && tokenPtr->type != TCL_TOKEN_VARIABLE) {
		    TclNewLiteralStringObj(msg, "invalid character \"$\"");
		    errCode = "BADCHAR";
		    goto error;
		}
		scanned = tokenPtr->size;
		break;








	    case SCRIPT: {
		Tcl_Parse *nestedPtr =
			TclStackAlloc(interp, sizeof(Tcl_Parse));

		tokenPtr = parsePtr->tokenPtr + parsePtr->numTokens;
		tokenPtr->type = TCL_TOKEN_COMMAND;
................................................................................
	    Tcl_SetErrorCode(interp, "TCL", "PARSE", "EXPR", errCode,
		    subErrCode, NULL);
	}
    }

    return TCL_ERROR;
}

























































































































































































































































































 
/*
 *----------------------------------------------------------------------
 *
 * ConvertTreeToTokens --
 *
 *	Given a string, the numBytes bytes starting at start, and an OpNode






>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|

|
|
|
|
|
|

|
>
>




>
>







 







<







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
...
896
897
898
899
900
901
902

903
904
905
906
907
908
909
...
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
...
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
....
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
		 * exceptions are that when a bareword is followed by an open
		 * paren, it might be a function call, and when the bareword
		 * is a legal literal boolean value, we accept that as well.
		 */

		if (start[scanned+TclParseAllWhiteSpace(
			start+scanned, numBytes-scanned)] == '(') {

		    /* Look ahead for assignment operator */

		    /* 
		     * TODO: this can probably be simplified.  
		     * For now, it is working. 
		     */

		    Tcl_Parse vparse;
		    const char *varend, *varstart = &start[TclParseAllWhiteSpace(start, numBytes)];
		    int code, len;
		    TclParseInit(interp, varstart, numBytes, &vparse);
		    code = Tcl_ParseVarName(NULL, varstart, numBytes, &vparse, 0);
		    if (code != TCL_OK) {
			//fprintf(stderr, "Replace me with proper error!\n");
		    }
		    len = vparse.tokenPtr[0].size;
		    varend = varstart+len;
		    Tcl_FreeParse(&vparse);

		    /* Look ahead for Assignment operator ':=' */
		    if (code == TCL_OK &&
			varend[TclParseAllWhiteSpace(varend,numBytes-len)] == ':' && 
			varend[TclParseAllWhiteSpace(varend,numBytes-len)+1] == '=') {

			lexeme = VARNAME;

			/* Adjust scanned bytes */
			scanned = varend-start;

			/* The variable name is tokenized below as a quoted string */

		    } else {

			lexeme = FUNCTION;

			/*
			 * When we compile the expression we'll need the function
			 * name, and there's no place in the parse tree to store
			 * it, so we keep a separate list of all the function
			 * names we've parsed in the order we found them.
			 */

			Tcl_ListObjAppendElement(NULL, funcList, literal);
		    }

		} else if (start[scanned+TclParseAllWhiteSpace(
			start+scanned, numBytes-scanned)] == ':' &&
			   start[scanned+TclParseAllWhiteSpace(
			start+scanned, numBytes-scanned)+1] == '=') {

		    /* Simple bareword */
		    lexeme = VARNAME;

		    /* The variable name is stored as an OT_LITERAL below */

		} else if (Tcl_GetBooleanFromObj(NULL,literal,&b) == TCL_OK) {
		    lexeme = BOOLEAN;
		} else {
................................................................................
		}
		goto error;
	    }

	    switch (lexeme) {
	    case NUMBER:
	    case BOOLEAN:

		/*
		 * TODO: Consider using a dict or hash to collapse all
		 * duplicate literals into a single representative value.
		 * (Like what is done with [split $s {}]).
		 * Pro:	~75% memory saving on expressions like
		 *	{1+1+1+1+1+.....+1} (Convert "pointer + Tcl_Obj" cost
		 *	to "pointer" cost only)
................................................................................

		Tcl_ListObjAppendElement(NULL, litList, literal);
		complete = lastParsed = OT_LITERAL;
		start += scanned;
		numBytes -= scanned;
		continue;

	    case VARNAME: {
		int length;
		TclGetStringFromObj(literal, &length);
		if (length < scanned) {
		    // Go tokenize the literal...
		    break;
		} else {
		    Tcl_ListObjAppendElement(NULL, litList, literal);
		    complete = lastParsed = OT_LITERAL;
		    start += scanned;
		    numBytes -= scanned;
		    continue;
		}
	    } /* VARNAME case */

	    default:
		break;
	    }

	    /*
	     * Remaining LEAF cases may involve filling Tcl_Tokens, so make
	     * room for at least 2 more tokens.
................................................................................
		if (code == TCL_OK && tokenPtr->type != TCL_TOKEN_VARIABLE) {
		    TclNewLiteralStringObj(msg, "invalid character \"$\"");
		    errCode = "BADCHAR";
		    goto error;
		}
		scanned = tokenPtr->size;
		break;

	    case VARNAME:
		code = TclParseTokens(NULL, start, scanned, TCL_SUBST_ALL, 1, 
				      parsePtr);

		// scanned already adjusted...
		break;

	    case SCRIPT: {
		Tcl_Parse *nestedPtr =
			TclStackAlloc(interp, sizeof(Tcl_Parse));

		tokenPtr = parsePtr->tokenPtr + parsePtr->numTokens;
		tokenPtr->type = TCL_TOKEN_COMMAND;
................................................................................
	    Tcl_SetErrorCode(interp, "TCL", "PARSE", "EXPR", errCode,
		    subErrCode, NULL);
	}
    }

    return TCL_ERROR;
}

 
/*
 * Tree Dump Utilities
 */

static int
MeasureTree(
	     OpNode *nodes,
	     int index)
{
    switch(nodes[index].lexeme&NODE_TYPE) {
    case UNARY:
	return 1+((nodes[index].right>=0)?MeasureTree(nodes,nodes[index].right):0);
    case BINARY:
	return 1+((nodes[index].left>=0)?MeasureTree(nodes,nodes[index].left):0)
	    +((nodes[index].right>=0)?MeasureTree(nodes,nodes[index].right):0);
    }
    return 1;
}

struct OpExtra {
    void *leftThing;
    void *rightThing;
    void *middleThing;
};

static void
AdornTreeRec(OpNode *nodes,int index,struct OpExtra *extra,Tcl_Obj ***litObjvPtr,Tcl_Obj ***funcObjvPtr,Tcl_Token **tokensPtr)
{
    int ty,x;
    
    ty=nodes[index].lexeme;
    switch(ty&NODE_TYPE) {
    case BINARY:
	x=nodes[index].left;
	if (x>=0) {
	    AdornTreeRec(nodes,x,extra,litObjvPtr,funcObjvPtr,tokensPtr);
	} else {
	    switch(x) {
	    case OT_LITERAL:
		extra[index].leftThing=(void *)**litObjvPtr;
		(*litObjvPtr)++;
		break;
	    case OT_TOKENS:
		extra[index].leftThing=(void *)*tokensPtr;
		(*tokensPtr)+=(*tokensPtr)->numComponents+1;
		break;
	    }
	}
    }
    if (ty==FUNCTION) {
	extra[index].middleThing=(void *)**funcObjvPtr;
	(*funcObjvPtr)++;
    }
    switch(ty&NODE_TYPE) {
    case UNARY:
    case BINARY:
	x=nodes[index].right;
	if (x>=0) {
	    AdornTreeRec(nodes,x,extra,litObjvPtr,funcObjvPtr,tokensPtr);
	} else {
	    switch(x) {
	    case OT_LITERAL:
		extra[index].rightThing=(void *)**litObjvPtr;
		(*litObjvPtr)++;
		break;
	    case OT_TOKENS:
		extra[index].rightThing=(void *)*tokensPtr;
		(*tokensPtr)+=(*tokensPtr)->numComponents+1;
		break;
	    }
	}
    }
}

static struct OpExtra *
AdornTree(OpNode *nodes,Tcl_Obj **litObjv,Tcl_Obj **funcObjv,Tcl_Token *tokens)
{
    int n;
    struct OpExtra *extra;

    n=MeasureTree(nodes,0);
    extra=(struct OpExtra *)ckalloc(n*sizeof(struct OpExtra));
    AdornTreeRec(nodes,0,extra,&litObjv,&funcObjv,&tokens);

    return extra;
}

static void DecodeNonOps(int x,void *thing,char *dst)
{
    switch(x) {
    case OT_EMPTY:
	strcpy(dst,"()");
	return;
    case OT_TOKENS:
	{
	    int n;
	    #define MAXTOKS 1023
	    char toks[MAXTOKS+1];

	    n=((Tcl_Token *)thing)->size;
	    if (n>=MAXTOKS) n=MAXTOKS;
	    memcpy(toks,((Tcl_Token *)thing)->start,n);
	    toks[n]=0;
	    sprintf(dst,"TOKENS: %s",toks);
	}
	return;
    case OT_LITERAL:
	sprintf(dst,"LITERAL: %s",TclGetString((Tcl_Obj *)thing));
	return;
    default:
	strcpy(dst,"N/A");
	return;
    }
}

static void
DumpExprTreeIndent(
	     OpNode *nodes,
	     int index,
	     const char *indent,
	     struct OpExtra *extra)
{
    #define INDENTMAX 1024
    char indent2[INDENTMAX];
    int ty;
    static const char *types[]={"AMBIG","BINARY","UNARY","LEAF"};
    static const char *lexemes[256];
    static const struct {int pos;const char *lex;} lexdesc[]={
	{1,"+"},{2,"-"},{3,"bareword"},{4,"incompatible"},{5,"invalid"},
	{0xc1,"number"},{0xc2,"[command]"},{0xc3,"boolean"},{0xc4,"{string}"},{0xc5,"$var"},{0xc6,"\"string\""},{0xc7,"()"},
	{0x81,"+"},{0x82,"-"},{0x83,"function"},{0x84,"start"},{0x85,"("},{0x86,"!"},{0x87,"~"},
	{0x41,"+"},{0x42,"-"},{0x43,","},{0x44,"*"},{0x45,"/"},{0x46,"%"},{0x47,","},{0x48,">"},
	{0x49,"&"},{0x4a,"^"},{0x4b,"|"},
	{0x4c,"?"},{0x4d,":"},
	{0x5B,")"},{0x5c,"end"},{0x5d,";"},{0x5e,":="}
    };
    static int lexdone=0;

    if (!lexdone) {
	int i;
	
	lexdone=1;
	for(i=0;i<256;i++) lexemes[i]="???";
	for(i=0;i<(int)(sizeof(lexdesc)/sizeof(lexdesc[0]));i++) {
	    lexemes[lexdesc[i].pos]=lexdesc[i].lex;
	}
    }

    ty=nodes[index].lexeme;
    fprintf(stderr,"--%s(%02X): %s %s\n",types[(ty&NODE_TYPE)>>6],ty,lexemes[ty],(ty==FUNCTION)?TclGetString((Tcl_Obj *)extra[index].middleThing):"");
    switch(ty&NODE_TYPE) {
    case UNARY:
	fprintf(stderr,"%s   |\n%s   `",indent,indent);
	snprintf(indent2,INDENTMAX,"%s    ",indent);
	if (nodes[index].right<0) {
	    char buf[1024];
	    DecodeNonOps(nodes[index].right,extra[index].rightThing,buf);
	    fprintf(stderr,"--%s\n",buf);
	} else {
	    DumpExprTreeIndent(nodes,nodes[index].right,indent2,extra);
	}
	break;
    case BINARY:
	fprintf(stderr,"%s   |\n%s   |",indent,indent);
	snprintf(indent2,INDENTMAX,"%s   |",indent);
	if (nodes[index].left<0) {
	    char buf[1024];
	    DecodeNonOps(nodes[index].left,extra[index].leftThing,buf);
	    fprintf(stderr,"--%s\n",buf);
	} else {
	    DumpExprTreeIndent(nodes,nodes[index].left,indent2,extra);
	}
	fprintf(stderr,"%s   |\n%s   `",indent,indent);
	snprintf(indent2,INDENTMAX,"%s    ",indent);
	if (nodes[index].right<0) {
	    char buf[1024];
	    DecodeNonOps(nodes[index].right,extra[index].rightThing,buf);
	    fprintf(stderr,"--%s\n",buf);
	} else {
	    DumpExprTreeIndent(nodes,nodes[index].right,indent2,extra);
	}
	break;
    }
}

static void
DumpExprTree(
	     OpNode *nodes,
	     int index,
	     const char *script,
	     int len,
	     Tcl_Obj **litObjv,
	     Tcl_Obj **funcObjv,
	     Tcl_Token *tokens)
{
    #define MAXSCR 1024
    char scr[MAXSCR];
    struct OpExtra *extra;

    if (len>=(MAXSCR-1)) len=MAXSCR-1;
    memcpy(scr,script,len);
    scr[len]=0;
    fprintf(stderr,"=== EXPR TREE FOR {%s} ===\n\n",scr);
    extra=AdornTree(nodes,litObjv,funcObjv,tokens);
    DumpExprTreeIndent(nodes,index,"",extra);
    fprintf(stderr,"\n=================\n");
    ckfree(extra);
}
 
/*
 *----------------------------------------------------------------------
 *
 * ParseAndDumpExpr, Tcl_ParseAndDumpExprObjCmd --
 *
 *	These procedures parse a string containing a Tcl expression and
 *      dump the resulting parse tree to stderr in a readable layout.
 *
 * Results:
 *	A standard Tcl return code and result left in interp.
 *
 * Side effects:
 *	Output to stderr
 *
 *----------------------------------------------------------------------
 */

static int
ParseAndDumpExpr(
    Tcl_Interp *interp,		/* Used for error reporting. */
    const char *script,		/* The source script to compile. */
    int numBytes)		/* Number of bytes in script. */
{
    OpNode *opTree = NULL;	/* Will point to the tree of operators */
    Tcl_Obj *litList = Tcl_NewObj();	/* List to hold the literals */
    Tcl_Obj *funcList = Tcl_NewObj();	/* List to hold the functon names*/
    Tcl_Parse *parsePtr = TclStackAlloc(interp, sizeof(Tcl_Parse));
				/* Holds the Tcl_Tokens of substitutions */

    int code = ParseExpr(interp, script, numBytes, &opTree, litList,
	    funcList, parsePtr, 0 /* parseOnly */);

    if (code == TCL_OK) {
	/*
	 * Valid parse; dump the tree.
	 */

	int objc;
	Tcl_Obj **litObjv;
	Tcl_Obj **funcObjv;

	TclListObjGetElements(NULL, litList, &objc, (Tcl_Obj ***)&litObjv);
	TclListObjGetElements(NULL, funcList, &objc, &funcObjv);
	DumpExprTree(opTree,0,script,numBytes,litObjv,funcObjv,parsePtr->tokenPtr);
    }

    Tcl_FreeParse(parsePtr);
    TclStackFree(interp, parsePtr);
    Tcl_DecrRefCount(funcList);
    Tcl_DecrRefCount(litList);
    ckfree(opTree);

    return code;
}

int Tcl_ParseAndDumpExprObjCmd(ClientData clientData,
			    Tcl_Interp *interp, int objc,
			    Tcl_Obj *const objv[])
{
    char *s;
    
    if (objc != 2) {
	Tcl_WrongNumArgs(interp, 1, objv, "expression");
	return TCL_ERROR;
    }
    s=TclGetString(objv[1]);
    
    return ParseAndDumpExpr(interp,s,strlen(s));
}

 
/*
 *----------------------------------------------------------------------
 *
 * ConvertTreeToTokens --
 *
 *	Given a string, the numBytes bytes starting at start, and an OpNode

Changes to generic/tclInt.h.

3050
3051
3052
3053
3054
3055
3056


3057
3058
3059
3060
3061
3062
3063
....
3264
3265
3266
3267
3268
3269
3270



3271
3272
3273
3274
3275
3276
3277
			    int *resultPtr);
MODULE_SCOPE int	TclParseNumber(Tcl_Interp *interp, Tcl_Obj *objPtr,
			    const char *expected, const char *bytes,
			    int numBytes, const char **endPtrPtr, int flags);
MODULE_SCOPE void	TclParseInit(Tcl_Interp *interp, const char *string,
			    int numBytes, Tcl_Parse *parsePtr);
MODULE_SCOPE int	TclParseAllWhiteSpace(const char *src, int numBytes);


MODULE_SCOPE int	TclProcessReturn(Tcl_Interp *interp,
			    int code, int level, Tcl_Obj *returnOpts);
MODULE_SCOPE int	TclpObjLstat(Tcl_Obj *pathPtr, Tcl_StatBuf *buf);
MODULE_SCOPE Tcl_Obj *	TclpTempFileName(void);
MODULE_SCOPE Tcl_Obj *  TclpTempFileNameForLibrary(Tcl_Interp *interp, Tcl_Obj* pathPtr);
MODULE_SCOPE Tcl_Obj *	TclNewFSPathObj(Tcl_Obj *dirPtr, const char *addStrRep,
			    int len);
................................................................................
			    Var *arrayPtr, Tcl_Obj *part1Ptr,
			    Tcl_Obj *part2Ptr, int index, int pathc,
			    Tcl_Obj *const pathv[], Tcl_Obj *keysPtr);
MODULE_SCOPE Tcl_Obj *	TclDictWithInit(Tcl_Interp *interp, Tcl_Obj *dictPtr,
			    int pathc, Tcl_Obj *const pathv[]);
MODULE_SCOPE int	Tcl_DisassembleObjCmd(ClientData clientData,
			    Tcl_Interp *interp, int objc,



			    Tcl_Obj *const objv[]);

/* Assemble command function */
MODULE_SCOPE int	Tcl_AssembleObjCmd(ClientData clientData,
			    Tcl_Interp *interp, int objc,
			    Tcl_Obj *const objv[]);
MODULE_SCOPE int	TclNRAssembleObjCmd(ClientData clientData,






>
>







 







>
>
>







3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
....
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
			    int *resultPtr);
MODULE_SCOPE int	TclParseNumber(Tcl_Interp *interp, Tcl_Obj *objPtr,
			    const char *expected, const char *bytes,
			    int numBytes, const char **endPtrPtr, int flags);
MODULE_SCOPE void	TclParseInit(Tcl_Interp *interp, const char *string,
			    int numBytes, Tcl_Parse *parsePtr);
MODULE_SCOPE int	TclParseAllWhiteSpace(const char *src, int numBytes);
MODULE_SCOPE int	TclParseTokens(Tcl_Interp *interp, const char *bytes,
			    int numBytes, int flags, int append, Tcl_Parse *parsePtr);
MODULE_SCOPE int	TclProcessReturn(Tcl_Interp *interp,
			    int code, int level, Tcl_Obj *returnOpts);
MODULE_SCOPE int	TclpObjLstat(Tcl_Obj *pathPtr, Tcl_StatBuf *buf);
MODULE_SCOPE Tcl_Obj *	TclpTempFileName(void);
MODULE_SCOPE Tcl_Obj *  TclpTempFileNameForLibrary(Tcl_Interp *interp, Tcl_Obj* pathPtr);
MODULE_SCOPE Tcl_Obj *	TclNewFSPathObj(Tcl_Obj *dirPtr, const char *addStrRep,
			    int len);
................................................................................
			    Var *arrayPtr, Tcl_Obj *part1Ptr,
			    Tcl_Obj *part2Ptr, int index, int pathc,
			    Tcl_Obj *const pathv[], Tcl_Obj *keysPtr);
MODULE_SCOPE Tcl_Obj *	TclDictWithInit(Tcl_Interp *interp, Tcl_Obj *dictPtr,
			    int pathc, Tcl_Obj *const pathv[]);
MODULE_SCOPE int	Tcl_DisassembleObjCmd(ClientData clientData,
			    Tcl_Interp *interp, int objc,
			    Tcl_Obj *const objv[]);
MODULE_SCOPE int	Tcl_ParseAndDumpExprObjCmd(ClientData clientData,
			    Tcl_Interp *interp, int objc,
			    Tcl_Obj *const objv[]);

/* Assemble command function */
MODULE_SCOPE int	Tcl_AssembleObjCmd(ClientData clientData,
			    Tcl_Interp *interp, int objc,
			    Tcl_Obj *const objv[]);
MODULE_SCOPE int	TclNRAssembleObjCmd(ClientData clientData,

Changes to generic/tclParse.c.

2496
2497
2498
2499
2500
2501
2502
2503
2504












































































2505
2506
2507
2508
2509
2510
				 * check. */
{
    int length;
    const char *script = TclGetStringFromObj(objPtr, &length);

    return CommandComplete(script, length);
}
 
/*












































































 * Local Variables:
 * mode: c
 * c-basic-offset: 4
 * fill-column: 78
 * End:
 */








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>






2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
				 * check. */
{
    int length;
    const char *script = TclGetStringFromObj(objPtr, &length);

    return CommandComplete(script, length);
}
 
/*
 *----------------------------------------------------------------------
 *
 * TclParseTokens --
 *
 *	Token parser used by ParseExpr. Parses the string made up of
 *	'numBytes' bytes starting at 'bytes'. Parsing is controlled by the
 *	flags argument to limit which substitutions to apply, as 
 *	represented by the flag values TCL_SUBST_BACKSLASHES, 
 *	TCL_SUBST_COMMANDS, TCL_SUBST_VARIABLES.
 *
 * Results:
 *	Tokens are added to parsePtr and parsePtr->term is filled in with the
 *	address of the character that terminated the parse (the character at 
 *	parsePtr->end). The return value is TCL_OK if the parse completed 
 *	successfully and TCL_ERROR otherwise. If a parse error occurs and 
 *	parsePtr->interp is not NULL, then an error message is left in the 
 *	interpreter's result.
 *
 * Side effects:
 *	The Tcl_Parse struct '*parsePtr' is filled with parse results.
 *	The caller is expected to eventually call Tcl_FreeParse() to properly
 *	cleanup the value written there.
 *
 *	If a parse error occurs, the Tcl_InterpState value '*statePtr' is
 *	filled with the state created by that error. When *statePtr is written
 *	to, the caller is expected to make the required calls to either
 *	Tcl_RestoreInterpState() or Tcl_DiscardInterpState() to dispose of the
 *	value written there.
 *
 *----------------------------------------------------------------------
 */

int
TclParseTokens(
    Tcl_Interp *interp,
    const char *bytes,
    int numBytes,
    int flags,
    int append,
    Tcl_Parse *parsePtr)
{
    int length = numBytes;
    const char *p = bytes;
    int code, offset, i;
    int startToken;

    if (!append) {
	TclParseInit(interp, p, length, parsePtr);
    }

    startToken = parsePtr->numTokens;

    /*
     * First parse the string rep of objPtr, as if it were enclosed as a
     * "-quoted word in a normal Tcl command. Honor flags that selectively
     * inhibit types of substitution.
     */

    code = ParseTokens(p, length, /* mask */ 0, flags, parsePtr);
    /* Truncate last token to length */
    /* Hack?  Why does ParseTokens not stop at numBytes? */
    for (i=startToken; i<parsePtr->numTokens; i++) {
	offset = parsePtr->tokenPtr[i].start - p + parsePtr->tokenPtr[i].size;
	if (offset >= length) break;
    }
    if (offset > length) {
	parsePtr->tokenPtr[i].size = length - (parsePtr->tokenPtr[i].start - p);
	/* Truncate tokens */
	if (i < parsePtr->numTokens) 
	    parsePtr->numTokens = i + 1;
    }
    return code;
}

 
/*
 * Local Variables:
 * mode: c
 * c-basic-offset: 4
 * fill-column: 78
 * End:
 */

Changes to tests/expr.test.

7204
7205
7206
7207
7208
7209
7210

7211
7212
7213
7214
7215
7216
7217

7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
....
7229
7230
7231
7232
7233
7234
7235



























7236
7237
7238
7239
7240
7241
7242
		    x  := $x+$f+$center;
		    y  := round($x)
		}]
    list $ans $t $x $dx $f $fs $g $center $dx $y
} {3 10.0 3.0 -0.2 0.0 {$dx-$x/10} -0.4 1.0 -0.2 3}

test expr-52.2 {expr assignment with literal names} {

    set ans [expr  { "-8-"  := 10.0;
		     "ary(fred)"  := 2.0;
		     {ary(" ")}  := sqrt(17)
		 }]
    list $ans [set "-8-"] [set "ary(fred)"] [set {ary(" ")}]
} {4.123105625617661 10.0 2.0 4.123105625617661}


test expr-52.3 {expr assignment error} -body {
    expr  { ary(0) := 500 }
} -returnCodes error -result {Target of assignment must be string
in expression " ary(0) := 500 "}

test expr-52.4 {expr assignment variables with qualifiers} {
    namespace eval n1 {}
    set ans [expr  {
		    ::t  := 10.0;
		    n1::x  := 2.0;
		    ::n1::dx := 0.2;
................................................................................
		    n1::f  := ($n1::dx-$n1::x/10);
		    fs := {$n1::dx-$n1::x/10};
		    y := -1
		}]
    list $ans $::t $n1::x $::n1::dx $n1::f $fs $y
} {-1 10.0 2.0 0.2 0.0 {$n1::dx-$n1::x/10} -1}




























# cleanup
if {[info exists a]} {
    unset a
}
catch {unset min}
catch {unset max}
apply {args {foreach v $args {if {[info exists $v]} {unset $v} }}} \






>







>
|
|
|
|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
....
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
		    x  := $x+$f+$center;
		    y  := round($x)
		}]
    list $ans $t $x $dx $f $fs $g $center $dx $y
} {3 10.0 3.0 -0.2 0.0 {$dx-$x/10} -0.4 1.0 -0.2 3}

test expr-52.2 {expr assignment with literal names} {
    array unset ary
    set ans [expr  { "-8-"  := 10.0;
		     "ary(fred)"  := 2.0;
		     {ary(" ")}  := sqrt(17)
		 }]
    list $ans [set "-8-"] [set "ary(fred)"] [set {ary(" ")}]
} {4.123105625617661 10.0 2.0 4.123105625617661}

# Not anymore
#test expr-52.3 {expr assignment error} -body {
#    expr  { ary(0) := 500 }
#} -returnCodes error -result {Target of assignment must be string
#in expression " ary(0) := 500 "}

test expr-52.4 {expr assignment variables with qualifiers} {
    namespace eval n1 {}
    set ans [expr  {
		    ::t  := 10.0;
		    n1::x  := 2.0;
		    ::n1::dx := 0.2;
................................................................................
		    n1::f  := ($n1::dx-$n1::x/10);
		    fs := {$n1::dx-$n1::x/10};
		    y := -1
		}]
    list $ans $::t $n1::x $::n1::dx $n1::f $fs $y
} {-1 10.0 2.0 0.2 0.0 {$n1::dx-$n1::x/10} -1}


test expr-52.5 {expr assignment array variables} {
    array unset ary
    set ans [expr {
		    i:=1;
		    jkl := 2;
		    ary($i) :=$i*$jkl;
		    ary( $jkl ):= double(int(sqrt($jkl) * 1000))/1000.0;
		    bsg(oh):= round($ary( $jkl ) * $ary( $jkl ))
		}]
    list $ans $i $jkl [array get ary] [array get bsg]
} {2 1 2 {1 2 { 2 } 1.414} {oh 2}}

test expr-52.6 {expr assignment odd cases} {
    set ans1 [expr { sin( 0 + [puts foobar; # will it be an array access?
			       string cat 0]) := sin( 0 + [puts foobar; # or will it be a function call?
							   string cat 0]) }]
    set arr(\ \}\ ) hello
    set ans2 [expr { arr( { ) := $arr( } ) }]
    set ans3 [catch {expr { arr( { ) + $arr( } ) }} err]
    set l1 [list $ans1 [array get sin]]
    set l2 [list $ans2 [array get arr]]
    set l3 [list $ans3 $err]
    list $l1 $l2 $l3

} {{0.0 {{ 0 + 0} 0.0}} {hello {\ \}\  hello \ \{\  hello}} {1 {invalid command name "tcl::mathfunc::arr"}}}

# cleanup
if {[info exists a]} {
    unset a
}
catch {unset min}
catch {unset max}
apply {args {foreach v $args {if {[info exists $v]} {unset $v} }}} \