Diff
Not logged in

Differences From Artifact [7319d81542]:

To Artifact [faac545a7e]:


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
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+

-
-
-
-
-
-
+
+
+
+
+
+

-
+
+
+




+
+







		 * 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;
			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.
		     */
			/*
			 * 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);
			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 {
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
896
897
898
899
900
901
902

903
904
905
906
907
908
909







-







		}
		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)
882
883
884
885
886
887
888















889
890
891
892
893
894
895
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+








		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.
927
928
929
930
931
932
933







934
935
936
937
938
939
940
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999







+
+
+
+
+
+
+







		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;
1491
1492
1493
1494
1495
1496
1497

























































































































































































































































































1498
1499
1500
1501
1502
1503
1504
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







	    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