1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
/*
* tclCompExpr.c --
*
* This file contains the code to compile Tcl expressions.
*
* Copyright (c) 1997 Sun Microsystems, Inc.
* Copyright (c) 1998-2000 by Scriptics Corporation.
* Contributions from Don Porter, NIST, 2006. (not subject to US copyright)
*
* 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.53.2.2 2007/06/25 18:53:30 dgp Exp $
*/
#include "tclInt.h"
#include "tclCompile.h"
#undef USE_EXPR_TOKENS
#undef PARSE_DIRECT_EXPR_TOKENS
#ifdef PARSE_DIRECT_EXPR_TOKENS
/*
* The ExprNode structure represents one node of the parse tree produced as an
* interim structure by the expression parser.
*/
typedef struct ExprNode {
unsigned char lexeme; /* Code that identifies the type of this
* node. */
int left; /* Index of the left operand of this operator
* node. */
int right; /* Index of the right operand of this operator
* node. */
int parent; /* Index of the operator of this operand
* node. */
int token; /* Index of the Tcl_Tokens of this leaf
* node. */
} ExprNode;
#endif
/*
* Integer codes indicating the form of an operand of an operator.
*/
enum OperandTypes {
OT_NONE = -4, OT_LITERAL = -3, OT_TOKENS = -2, OT_EMPTY = -1
};
/*
* The OpNode structure represents one operator node in the parse tree
* produced as an interim structure by the expression parser.
*/
typedef struct OpNode {
unsigned char lexeme; /* Code that identifies the operator. */
int left; /* Index of the left operand. Non-negative
* integer is an index into the parse tree,
* pointing to another operator. Value
* OT_LITERAL indicates operand is the next
* entry in the literal list. Value OT_TOKENS
* indicates the operand is the next word in
* the Tcl_Parse struct. Value OT_NONE
* indicates we haven't yet parsed the operand
* for this operator. */
int right; /* Index of the right operand. Same
* interpretation as left, with addition of
* OT_EMPTY meaning zero arguments. */
int parent; /* Index of the operator of this operand
* node. */
} OpNode;
/*
* Set of lexeme codes stored in ExprNode structs to label and categorize the
* lexemes found.
*/
#define LEAF (1<<7)
#define UNARY (1<<6)
#define BINARY (1<<5)
|
|
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
/*
* tclCompExpr.c --
*
* This file contains the code to compile Tcl expressions.
*
* Copyright (c) 1997 Sun Microsystems, Inc.
* Copyright (c) 1998-2000 by Scriptics Corporation.
* Contributions from Don Porter, NIST, 2006. (not subject to US copyright)
*
* 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.53.2.3 2007/07/03 02:28:36 dgp Exp $
*/
#include "tclInt.h"
#include "tclCompile.h" /* CompileEnv */
/*
* Set of lexeme codes stored in OpNode structs to label and categorize the
* lexemes found.
*/
#define LEAF (1<<7)
#define UNARY (1<<6)
#define BINARY (1<<5)
|
| ︙ | | | ︙ | |
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
#define STRNEQ ( BINARY | 23)
#define EXPON ( BINARY | 24)
#define IN_LIST ( BINARY | 25)
#define NOT_IN_LIST ( BINARY | 26)
#define CLOSE_PAREN ( BINARY | 27)
#define END ( BINARY | 28)
/*
* Declarations for local functions to this file:
*/
static int ParseLexeme(const char *start, int numBytes,
unsigned char *lexemePtr, Tcl_Obj **literalPtr);
#if (!defined(PARSE_DIRECT_EXPR_TOKENS) || !defined(USE_EXPR_TOKENS))
static int ParseExpr(Tcl_Interp *interp, const char *start,
int numBytes, OpNode **opTreePtr,
Tcl_Obj *litList, Tcl_Obj *funcList,
Tcl_Parse *parsePtr);
#endif
#ifdef PARSE_DIRECT_EXPR_TOKENS
static void GenerateTokens(ExprNode *nodes, Tcl_Parse *scratchPtr,
Tcl_Parse *parsePtr);
#else
static void ConvertTreeToTokens(Tcl_Interp *interp,
const char *start, int numBytes, OpNode *nodes,
Tcl_Obj *litList, Tcl_Token *tokenPtr,
Tcl_Parse *parsePtr);
static int GenerateTokensForLiteral(const char *script,
int numBytes, Tcl_Obj *litList, int nextLiteral,
Tcl_Parse *parsePtr);
static int CopyTokens(Tcl_Token *sourcePtr, Tcl_Parse *parsePtr);
#endif
#if (!defined(PARSE_DIRECT_EXPR_TOKENS) || !defined(USE_EXPR_TOKENS))
/*
*----------------------------------------------------------------------
*
* ParseExpr --
*
* Given a string, the numBytes bytes starting at start, this function
* parses it as a Tcl expression and stores information about the
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
<
<
<
<
<
<
>
>
>
|
|
|
|
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
#define STRNEQ ( BINARY | 23)
#define EXPON ( BINARY | 24)
#define IN_LIST ( BINARY | 25)
#define NOT_IN_LIST ( BINARY | 26)
#define CLOSE_PAREN ( BINARY | 27)
#define END ( BINARY | 28)
/*
* Integer codes indicating the form of an operand of an operator.
*/
enum OperandTypes {
OT_NONE = -4, OT_LITERAL = -3, OT_TOKENS = -2, OT_EMPTY = -1
};
/*
* The OpNode structure represents one operator node in the parse tree
* produced as an interim structure by the expression parser.
*/
typedef struct OpNode {
unsigned char lexeme; /* Code that identifies the operator. */
int left; /* Index of the left operand. Non-negative
* integer is an index into the parse tree,
* pointing to another operator. Value
* OT_LITERAL indicates operand is the next
* entry in the literal list. Value OT_TOKENS
* indicates the operand is the next word in
* the Tcl_Parse struct. Value OT_NONE
* indicates we haven't yet parsed the operand
* for this operator. */
int right; /* Index of the right operand. Same
* interpretation as left, with addition of
* OT_EMPTY meaning zero arguments. */
int parent; /* Index of the operator of this operand
* node. */
} OpNode;
typedef struct JumpList {
JumpFixup jump;
int depth;
int offset;
int convert;
struct JumpList *next;
} JumpList;
/*
* Declarations for local functions to this file:
*/
static int ParseLexeme(const char *start, int numBytes,
unsigned char *lexemePtr, Tcl_Obj **literalPtr);
static int ParseExpr(Tcl_Interp *interp, const char *start,
int numBytes, OpNode **opTreePtr,
Tcl_Obj *litList, Tcl_Obj *funcList,
Tcl_Parse *parsePtr);
static void ConvertTreeToTokens(Tcl_Interp *interp,
const char *start, int numBytes, OpNode *nodes,
Tcl_Obj *litList, Tcl_Token *tokenPtr,
Tcl_Parse *parsePtr);
static int GenerateTokensForLiteral(const char *script,
int numBytes, Tcl_Obj *litList, int nextLiteral,
Tcl_Parse *parsePtr);
static int CopyTokens(Tcl_Token *sourcePtr, Tcl_Parse *parsePtr);
static void CompileExprTree(Tcl_Interp *interp, OpNode *nodes,
Tcl_Obj *const litObjv[], Tcl_Obj *funcList,
Tcl_Token *tokenPtr, int *convertPtr,
CompileEnv *envPtr);
/*
*----------------------------------------------------------------------
*
* ParseExpr --
*
* Given a string, the numBytes bytes starting at start, this function
* parses it as a Tcl expression and stores information about the
|
| ︙ | | | ︙ | |
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
while ((code == TCL_OK) && (lexeme != END)) {
OpNode *nodePtr;
Tcl_Token *tokenPtr = NULL;
Tcl_Obj *literal = NULL;
const char *lastStart = start - scanned;
/*
* Each pass through this loop adds one more ExprNode. Allocate space
* for one if required.
*/
if (nodesUsed >= nodesAvailable) {
int size = nodesUsed * 2;
OpNode *newPtr;
|
|
|
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
while ((code == TCL_OK) && (lexeme != END)) {
OpNode *nodePtr;
Tcl_Token *tokenPtr = NULL;
Tcl_Obj *literal = NULL;
const char *lastStart = start - scanned;
/*
* Each pass through this loop adds one more OpNode. Allocate space
* for one if required.
*/
if (nodesUsed >= nodesAvailable) {
int size = nodesUsed * 2;
OpNode *newPtr;
|
| ︙ | | | ︙ | |
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
|
"\n (parsing expression \"%.*s%s\")",
(numBytes < limit) ? numBytes : limit - 3,
parsePtr->string, (numBytes < limit) ? "" : "..."));
}
return code;
}
#endif
#ifndef PARSE_DIRECT_EXPR_TOKENS
/*
*----------------------------------------------------------------------
*
* GenerateTokensForLiteral --
*
* Results:
* Number of bytes scanned.
|
<
<
|
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
|
"\n (parsing expression \"%.*s%s\")",
(numBytes < limit) ? numBytes : limit - 3,
parsePtr->string, (numBytes < limit) ? "" : "..."));
}
return code;
}
/*
*----------------------------------------------------------------------
*
* GenerateTokensForLiteral --
*
* Results:
* Number of bytes scanned.
|
| ︙ | | | ︙ | |
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
|
}
nodePtr = nodes + nodePtr->parent;
}
break;
}
}
}
#endif
/*
*----------------------------------------------------------------------
*
* Tcl_ParseExpr --
*
* Given a string, the numBytes bytes starting at start, this function
|
<
|
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
|
}
nodePtr = nodes + nodePtr->parent;
}
break;
}
}
}
/*
*----------------------------------------------------------------------
*
* Tcl_ParseExpr --
*
* Given a string, the numBytes bytes starting at start, this function
|
| ︙ | | | ︙ | |
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
|
int numBytes, /* Number of bytes in string. If < 0, the
* string consists of all bytes up to the
* first null character. */
Tcl_Parse *parsePtr) /* Structure to fill with information about
* the parsed expression; any previous
* information in the structure is ignored. */
{
#ifndef PARSE_DIRECT_EXPR_TOKENS
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 *exprParsePtr =
(Tcl_Parse *) TclStackAlloc(interp, sizeof(Tcl_Parse));
/* Holds the Tcl_Tokens of substitutions */
int code = ParseExpr(interp, start, numBytes, &opTree, litList,
|
<
|
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
|
int numBytes, /* Number of bytes in string. If < 0, the
* string consists of all bytes up to the
* first null character. */
Tcl_Parse *parsePtr) /* Structure to fill with information about
* the parsed expression; any previous
* information in the structure is ignored. */
{
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 *exprParsePtr =
(Tcl_Parse *) TclStackAlloc(interp, sizeof(Tcl_Parse));
/* Holds the Tcl_Tokens of substitutions */
int code = ParseExpr(interp, start, numBytes, &opTree, litList,
|
| ︙ | | | ︙ | |
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
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
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
|
Tcl_FreeParse(exprParsePtr);
TclStackFree(interp, exprParsePtr);
Tcl_DecrRefCount(funcList);
Tcl_DecrRefCount(litList);
ckfree((char *) opTree);
return code;
#else
#define NUM_STATIC_NODES 64
ExprNode staticNodes[NUM_STATIC_NODES];
ExprNode *lastOrphanPtr, *nodes = staticNodes;
int nodesAvailable = NUM_STATIC_NODES;
int nodesUsed = 0;
Tcl_Parse *scratchPtr = (Tcl_Parse *) TclStackAlloc(interp, sizeof(Tcl_Parse));
/* Parsing scratch space */
Tcl_Obj *msg = NULL, *post = NULL;
int scanned = 0, code = TCL_OK, insertMark = 0;
const char *mark = "_@_";
const int limit = 25;
static const unsigned char prec[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 15, 15, 5, 16, 16, 16, 13, 13, 11, 10, 9, 6, 6, 14, 14,
13, 13, 12, 12, 8, 7, 12, 12, 17, 12, 12, 3, 1, 0, 0, 0,
0, 18, 18, 18, 2, 4, 18, 18, 0, 0, 0, 0, 0, 0, 0, 0,
};
if (numBytes < 0) {
numBytes = (start ? strlen(start) : 0);
}
TclParseInit(interp, start, numBytes, scratchPtr);
TclParseInit(interp, start, numBytes, parsePtr);
/*
* Initialize the parse tree with the special "START" node.
*/
nodes->lexeme = START;
nodes->left = -1;
nodes->right = -1;
nodes->parent = -1;
nodes->token = -1;
lastOrphanPtr = nodes;
nodesUsed++;
while ((code == TCL_OK) && (lastOrphanPtr->lexeme != END)) {
ExprNode *nodePtr, *lastNodePtr;
Tcl_Token *tokenPtr;
/*
* Each pass through this loop adds one more ExprNode. Allocate space
* for one if required.
*/
if (nodesUsed >= nodesAvailable) {
int lastOrphanIdx = lastOrphanPtr - nodes;
int size = nodesUsed * 2;
ExprNode *newPtr;
if (nodes == staticNodes) {
nodes = NULL;
}
do {
newPtr = (ExprNode *) attemptckrealloc((char *) nodes,
(unsigned int) size * sizeof(ExprNode));
} while ((newPtr == NULL)
&& ((size -= (size - nodesUsed) / 2) > nodesUsed));
if (newPtr == NULL) {
TclNewLiteralStringObj(msg,
"not enough memory to parse expression");
code = TCL_ERROR;
continue;
}
nodesAvailable = size;
if (nodes == NULL) {
memcpy(newPtr, staticNodes,
(size_t) nodesUsed * sizeof(ExprNode));
}
nodes = newPtr;
lastOrphanPtr = nodes + lastOrphanIdx;
}
nodePtr = nodes + nodesUsed;
lastNodePtr = nodePtr - 1;
/*
* Skip white space between lexemes.
*/
scanned = TclParseAllWhiteSpace(start, numBytes);
start += scanned;
numBytes -= scanned;
scanned = ParseLexeme(start, numBytes, &(nodePtr->lexeme), NULL);
/*
* Use context to categorize the lexemes that are ambiguous.
*/
if ((NODE_TYPE & nodePtr->lexeme) == 0) {
switch (nodePtr->lexeme) {
case INVALID:
msg = Tcl_ObjPrintf(
"invalid character \"%.*s\"", scanned, start);
code = TCL_ERROR;
continue;
case INCOMPLETE:
msg = Tcl_ObjPrintf(
"incomplete operator \"%.*s\"", scanned, start);
code = TCL_ERROR;
continue;
case BAREWORD:
if (start[scanned+TclParseAllWhiteSpace(
start+scanned, numBytes-scanned)] == '(') {
nodePtr->lexeme = FUNCTION;
} else {
Tcl_Obj *objPtr = Tcl_NewStringObj(start, scanned);
Tcl_IncrRefCount(objPtr);
code = Tcl_ConvertToType(NULL, objPtr, &tclBooleanType);
Tcl_DecrRefCount(objPtr);
if (code == TCL_OK) {
nodePtr->lexeme = BOOLEAN;
} else {
msg = Tcl_ObjPrintf(
"invalid bareword \"%.*s%s\"",
(scanned < limit) ? scanned : limit - 3, start,
(scanned < limit) ? "" : "...");
post = Tcl_ObjPrintf(
"should be \"$%.*s%s\" or \"{%.*s%s}\"",
(scanned < limit) ? scanned : limit - 3,
start, (scanned < limit) ? "" : "...",
(scanned < limit) ? scanned : limit - 3,
start, (scanned < limit) ? "" : "...");
Tcl_AppendPrintfToObj(post,
" or \"%.*s%s(...)\" or ...",
(scanned < limit) ? scanned : limit - 3,
start, (scanned < limit) ? "" : "...");
continue;
}
}
break;
case PLUS:
case MINUS:
if ((NODE_TYPE & lastNodePtr->lexeme) == LEAF) {
nodePtr->lexeme |= BINARY;
} else {
nodePtr->lexeme |= UNARY;
}
}
}
/*
* Add node to parse tree based on category.
*/
switch (NODE_TYPE & nodePtr->lexeme) {
case LEAF: {
const char *end;
if ((NODE_TYPE & lastNodePtr->lexeme) == LEAF) {
const char *operand =
scratchPtr->tokenPtr[lastNodePtr->token].start;
msg = Tcl_ObjPrintf("missing operator at %s", mark);
if (operand[0] == '0') {
Tcl_Obj *copy = Tcl_NewStringObj(operand,
start + scanned - operand);
if (TclCheckBadOctal(NULL, Tcl_GetString(copy))) {
TclNewLiteralStringObj(post,
"looks like invalid octal number");
}
Tcl_DecrRefCount(copy);
}
scanned = 0;
insertMark = 1;
code = TCL_ERROR;
continue;
}
if (scratchPtr->numTokens+1 >= scratchPtr->tokensAvailable) {
TclExpandTokenArray(scratchPtr);
}
nodePtr->token = scratchPtr->numTokens;
tokenPtr = scratchPtr->tokenPtr + nodePtr->token;
tokenPtr->type = TCL_TOKEN_SUB_EXPR;
tokenPtr->start = start;
scratchPtr->numTokens++;
switch (nodePtr->lexeme) {
case NUMBER:
case BOOLEAN:
tokenPtr = scratchPtr->tokenPtr + scratchPtr->numTokens;
tokenPtr->type = TCL_TOKEN_TEXT;
tokenPtr->start = start;
tokenPtr->size = scanned;
tokenPtr->numComponents = 0;
scratchPtr->numTokens++;
break;
case QUOTED:
code = Tcl_ParseQuotedString(interp, start, numBytes,
scratchPtr, 1, &end);
if (code != TCL_OK) {
scanned = scratchPtr->term - start;
scanned += (scanned < numBytes);
continue;
}
scanned = end - start;
break;
case BRACED:
code = Tcl_ParseBraces(interp, start, numBytes,
scratchPtr, 1, &end);
if (code != TCL_OK) {
continue;
}
scanned = end - start;
break;
case VARIABLE:
code = Tcl_ParseVarName(interp, start, numBytes, scratchPtr, 1);
if (code != TCL_OK) {
scanned = scratchPtr->term - start;
scanned += (scanned < numBytes);
continue;
}
tokenPtr = scratchPtr->tokenPtr + nodePtr->token + 1;
if (tokenPtr->type != TCL_TOKEN_VARIABLE) {
TclNewLiteralStringObj(msg, "invalid character \"$\"");
code = TCL_ERROR;
continue;
}
scanned = tokenPtr->size;
break;
case SCRIPT: {
Tcl_Parse *nestedPtr =
(Tcl_Parse *) TclStackAlloc(interp, sizeof(Tcl_Parse));
tokenPtr = scratchPtr->tokenPtr + scratchPtr->numTokens;
tokenPtr->type = TCL_TOKEN_COMMAND;
tokenPtr->start = start;
tokenPtr->numComponents = 0;
end = start + numBytes;
start++;
while (1) {
code = Tcl_ParseCommand(interp,
start, (end - start), 1, nestedPtr);
if (code != TCL_OK) {
parsePtr->term = nestedPtr->term;
parsePtr->errorType = nestedPtr->errorType;
parsePtr->incomplete = nestedPtr->incomplete;
break;
}
start = (nestedPtr->commandStart + nestedPtr->commandSize);
Tcl_FreeParse(nestedPtr);
if ((nestedPtr->term < end) && (*(nestedPtr->term) == ']')
&& !(nestedPtr->incomplete)) {
break;
}
if (start == end) {
TclNewLiteralStringObj(msg, "missing close-bracket");
parsePtr->term = tokenPtr->start;
parsePtr->errorType = TCL_PARSE_MISSING_BRACKET;
parsePtr->incomplete = 1;
code = TCL_ERROR;
break;
}
}
TclStackFree(interp, nestedPtr);
end = start;
start = tokenPtr->start;
if (code != TCL_OK) {
scanned = parsePtr->term - start;
scanned += (scanned < numBytes);
continue;
}
scanned = end - start;
tokenPtr->size = scanned;
scratchPtr->numTokens++;
break;
}
}
tokenPtr = scratchPtr->tokenPtr + nodePtr->token;
tokenPtr->size = scanned;
tokenPtr->numComponents = scratchPtr->numTokens - nodePtr->token - 1;
nodePtr->left = -1;
nodePtr->right = -1;
nodePtr->parent = -1;
lastOrphanPtr = nodePtr;
nodesUsed++;
break;
}
case UNARY:
if ((NODE_TYPE & lastNodePtr->lexeme) == LEAF) {
msg = Tcl_ObjPrintf("missing operator at %s", mark);
scanned = 0;
insertMark = 1;
code = TCL_ERROR;
continue;
}
nodePtr->left = -1;
nodePtr->right = -1;
nodePtr->parent = -1;
if (scratchPtr->numTokens >= scratchPtr->tokensAvailable) {
TclExpandTokenArray(scratchPtr);
}
nodePtr->token = scratchPtr->numTokens;
tokenPtr = scratchPtr->tokenPtr + nodePtr->token;
tokenPtr->type = TCL_TOKEN_OPERATOR;
tokenPtr->start = start;
tokenPtr->size = scanned;
tokenPtr->numComponents = 0;
scratchPtr->numTokens++;
lastOrphanPtr = nodePtr;
nodesUsed++;
break;
case BINARY: {
ExprNode *otherPtr = NULL;
unsigned char precedence = prec[nodePtr->lexeme];
if ((nodePtr->lexeme == CLOSE_PAREN)
&& (lastNodePtr->lexeme == OPEN_PAREN)) {
if (lastNodePtr[-1].lexeme == FUNCTION) {
/*
* Normally, "()" is a syntax error, but as a special case
* accept it as an argument list for a function.
*/
scanned = 0;
nodePtr->lexeme = EMPTY;
nodePtr->left = -1;
nodePtr->right = -1;
nodePtr->parent = -1;
nodePtr->token = -1;
lastOrphanPtr = nodePtr;
nodesUsed++;
break;
}
msg = Tcl_ObjPrintf("empty subexpression at %s", mark);
scanned = 0;
insertMark = 1;
code = TCL_ERROR;
continue;
}
if ((NODE_TYPE & lastNodePtr->lexeme) != LEAF) {
if (prec[lastNodePtr->lexeme] > precedence) {
if (lastNodePtr->lexeme == OPEN_PAREN) {
TclNewLiteralStringObj(msg, "unbalanced open paren");
} else if (lastNodePtr->lexeme == COMMA) {
msg = Tcl_ObjPrintf(
"missing function argument at %s", mark);
scanned = 0;
insertMark = 1;
} else if (lastNodePtr->lexeme == START) {
TclNewLiteralStringObj(msg, "empty expression");
}
} else if (nodePtr->lexeme == CLOSE_PAREN) {
TclNewLiteralStringObj(msg, "unbalanced close paren");
} else if ((nodePtr->lexeme == COMMA)
&& (lastNodePtr->lexeme == OPEN_PAREN)
&& (lastNodePtr[-1].lexeme == FUNCTION)) {
msg = Tcl_ObjPrintf(
"missing function argument at %s", mark);
scanned = 0;
insertMark = 1;
}
if (msg == NULL) {
msg = Tcl_ObjPrintf("missing operand at %s", mark);
scanned = 0;
insertMark = 1;
}
code = TCL_ERROR;
continue;
}
while (1) {
if (lastOrphanPtr->parent >= 0) {
otherPtr = nodes + lastOrphanPtr->parent;
} else if (lastOrphanPtr->left >= 0) {
Tcl_Panic("Tcl_ParseExpr: left closure programming error");
} else {
lastOrphanPtr->parent = lastOrphanPtr - nodes;
otherPtr = lastOrphanPtr;
}
otherPtr--;
if (prec[otherPtr->lexeme] < precedence) {
break;
}
if (prec[otherPtr->lexeme] == precedence) {
/*
* Special association rules for the ternary operators.
*/
if ((otherPtr->lexeme == QUESTION)
&& (lastOrphanPtr->lexeme != COLON)) {
break;
}
if ((otherPtr->lexeme == COLON)
&& (nodePtr->lexeme == QUESTION)) {
break;
}
/*
* Right association rules for exponentiation.
*/
if (nodePtr->lexeme == EXPON) {
break;
}
}
/*
* Some checks before linking.
*/
if ((otherPtr->lexeme == OPEN_PAREN)
&& (nodePtr->lexeme != CLOSE_PAREN)) {
lastOrphanPtr = otherPtr;
TclNewLiteralStringObj(msg, "unbalanced open paren");
code = TCL_ERROR;
break;
}
if ((otherPtr->lexeme == QUESTION)
&& (lastOrphanPtr->lexeme != COLON)) {
msg = Tcl_ObjPrintf(
"missing operator \":\" at %s", mark);
scanned = 0;
insertMark = 1;
code = TCL_ERROR;
break;
}
if ((lastOrphanPtr->lexeme == COLON)
&& (otherPtr->lexeme != QUESTION)) {
TclNewLiteralStringObj(msg,
"unexpected operator \":\" without preceding \"?\"");
code = TCL_ERROR;
break;
}
/*
* Link orphan as right operand of otherPtr.
*/
otherPtr->right = lastOrphanPtr - nodes;
lastOrphanPtr->parent = otherPtr - nodes;
lastOrphanPtr = otherPtr;
if (otherPtr->lexeme == OPEN_PAREN) {
/*
* CLOSE_PAREN can only close one OPEN_PAREN.
*/
tokenPtr = scratchPtr->tokenPtr + otherPtr->token;
tokenPtr->size = start + scanned - tokenPtr->start;
break;
}
if (otherPtr->lexeme == START) {
/*
* Don't backtrack beyond the start.
*/
break;
}
}
if (code != TCL_OK) {
continue;
}
if (nodePtr->lexeme == CLOSE_PAREN) {
if (otherPtr->lexeme == START) {
TclNewLiteralStringObj(msg, "unbalanced close paren");
code = TCL_ERROR;
continue;
}
/*
* Create no node for a CLOSE_PAREN lexeme.
*/
break;
}
if ((nodePtr->lexeme == COMMA) && ((otherPtr->lexeme != OPEN_PAREN)
|| (otherPtr[-1].lexeme != FUNCTION))) {
TclNewLiteralStringObj(msg,
"unexpected \",\" outside function argument list");
code = TCL_ERROR;
continue;
}
if (lastOrphanPtr->lexeme == COLON) {
TclNewLiteralStringObj(msg,
"unexpected operator \":\" without preceding \"?\"");
code = TCL_ERROR;
continue;
}
/*
* Link orphan as left operand of new node.
*/
nodePtr->right = -1;
if (scratchPtr->numTokens >= scratchPtr->tokensAvailable) {
TclExpandTokenArray(scratchPtr);
}
nodePtr->token = scratchPtr->numTokens;
tokenPtr = scratchPtr->tokenPtr + nodePtr->token;
tokenPtr->type = TCL_TOKEN_OPERATOR;
tokenPtr->start = start;
tokenPtr->size = scanned;
tokenPtr->numComponents = 0;
scratchPtr->numTokens++;
nodePtr->left = lastOrphanPtr - nodes;
nodePtr->parent = lastOrphanPtr->parent;
lastOrphanPtr->parent = nodePtr - nodes;
lastOrphanPtr = nodePtr;
nodesUsed++;
break;
}
}
start += scanned;
numBytes -= scanned;
}
if (code == TCL_OK) {
/*
* Shift tokens from scratch space to caller space.
*/
GenerateTokens(nodes, scratchPtr, parsePtr);
} else {
if (parsePtr->errorType == TCL_PARSE_SUCCESS) {
parsePtr->errorType = TCL_PARSE_SYNTAX;
parsePtr->term = start;
}
if (interp == NULL) {
if (msg) {
Tcl_DecrRefCount(msg);
}
} else {
if (msg == NULL) {
msg = Tcl_GetObjResult(interp);
}
Tcl_AppendPrintfToObj(msg,
"\nin expression \"%s%.*s%.*s%s%s%.*s%s\"",
((start - limit) < scratchPtr->string) ? "" : "...",
((start - limit) < scratchPtr->string)
? (start - scratchPtr->string) : limit - 3,
((start - limit) < scratchPtr->string)
? scratchPtr->string : start - limit + 3,
(scanned < limit) ? scanned : limit - 3, start,
(scanned < limit) ? "" : "...",
insertMark ? mark : "",
(start + scanned + limit > scratchPtr->end)
? scratchPtr->end - (start + scanned) : limit-3,
start + scanned,
(start + scanned + limit > scratchPtr->end) ? "" : "...");
if (post != NULL) {
Tcl_AppendToObj(msg, ";\n", -1);
Tcl_AppendObjToObj(msg, post);
Tcl_DecrRefCount(post);
}
Tcl_SetObjResult(interp, msg);
numBytes = scratchPtr->end - scratchPtr->string;
Tcl_AppendObjToErrorInfo(interp, Tcl_ObjPrintf(
"\n (parsing expression \"%.*s%s\")",
(numBytes < limit) ? numBytes : limit - 3,
scratchPtr->string, (numBytes < limit) ? "" : "..."));
}
}
if (nodes != staticNodes) {
ckfree((char *)nodes);
}
Tcl_FreeParse(scratchPtr);
TclStackFree(interp, scratchPtr);
return code;
#endif
}
#ifdef PARSE_DIRECT_EXPR_TOKENS
/*
*----------------------------------------------------------------------
*
* GenerateTokens --
*
* Routine that generates Tcl_Tokens that represent a Tcl expression and
* writes them to *parsePtr. The parse tree of the expression is in the
* array of ExprNodes, nodes. Some of the Tcl_Tokens are copied from
* scratch space at *scratchPtr, where the parsing pass that constructed
* the parse tree left them.
*
*----------------------------------------------------------------------
*/
static void
GenerateTokens(
ExprNode *nodes,
Tcl_Parse *scratchPtr,
Tcl_Parse *parsePtr)
{
ExprNode *nodePtr = nodes + nodes->right;
Tcl_Token *sourcePtr, *destPtr, *tokenPtr = scratchPtr->tokenPtr;
int toCopy;
const char *end = tokenPtr->start + tokenPtr->size;
while (nodePtr->lexeme != START) {
switch (NODE_TYPE & nodePtr->lexeme) {
case BINARY:
if (nodePtr->left >= 0) {
if ((nodePtr->lexeme != COMMA) && (nodePtr->lexeme != COLON)) {
sourcePtr = scratchPtr->tokenPtr + nodePtr->token;
if (parsePtr->numTokens + 1 >= parsePtr->tokensAvailable) {
TclExpandTokenArray(parsePtr);
}
destPtr = parsePtr->tokenPtr + parsePtr->numTokens;
nodePtr->token = parsePtr->numTokens;
destPtr->type = TCL_TOKEN_SUB_EXPR;
destPtr->start = tokenPtr->start;
destPtr++;
*destPtr = *sourcePtr;
parsePtr->numTokens += 2;
}
nodePtr = nodes + nodePtr->left;
nodes[nodePtr->parent].left = -1;
} else if (nodePtr->right >= 0) {
tokenPtr += tokenPtr->numComponents + 1;
nodePtr = nodes + nodePtr->right;
nodes[nodePtr->parent].right = -1;
} else {
if ((nodePtr->lexeme != COMMA) && (nodePtr->lexeme != COLON)) {
destPtr = parsePtr->tokenPtr + nodePtr->token;
destPtr->size = end - destPtr->start;
destPtr->numComponents =
parsePtr->numTokens - nodePtr->token - 1;
}
nodePtr = nodes + nodePtr->parent;
}
break;
case UNARY:
if (nodePtr->right >= 0) {
sourcePtr = scratchPtr->tokenPtr + nodePtr->token;
if (nodePtr->lexeme != OPEN_PAREN) {
if (parsePtr->numTokens + 1 >= parsePtr->tokensAvailable) {
TclExpandTokenArray(parsePtr);
}
destPtr = parsePtr->tokenPtr + parsePtr->numTokens;
nodePtr->token = parsePtr->numTokens;
destPtr->type = TCL_TOKEN_SUB_EXPR;
destPtr->start = tokenPtr->start;
destPtr++;
*destPtr = *sourcePtr;
parsePtr->numTokens += 2;
}
if (tokenPtr == sourcePtr) {
tokenPtr += tokenPtr->numComponents + 1;
}
nodePtr = nodes + nodePtr->right;
nodes[nodePtr->parent].right = -1;
} else {
if (nodePtr->lexeme != OPEN_PAREN) {
destPtr = parsePtr->tokenPtr + nodePtr->token;
destPtr->size = end - destPtr->start;
destPtr->numComponents =
parsePtr->numTokens - nodePtr->token - 1;
} else {
sourcePtr = scratchPtr->tokenPtr + nodePtr->token;
end = sourcePtr->start + sourcePtr->size;
}
nodePtr = nodes + nodePtr->parent;
}
break;
case LEAF:
switch (nodePtr->lexeme) {
case EMPTY:
break;
case BRACED:
case QUOTED:
sourcePtr = scratchPtr->tokenPtr + nodePtr->token;
end = sourcePtr->start + sourcePtr->size;
if (sourcePtr->numComponents > 1) {
toCopy = sourcePtr->numComponents;
if (tokenPtr == sourcePtr) {
tokenPtr += toCopy + 1;
}
sourcePtr->numComponents++;
while (parsePtr->numTokens + toCopy + 1
>= parsePtr->tokensAvailable) {
TclExpandTokenArray(parsePtr);
}
destPtr = parsePtr->tokenPtr + parsePtr->numTokens;
*destPtr++ = *sourcePtr;
*destPtr = *sourcePtr++;
destPtr->type = TCL_TOKEN_WORD;
destPtr->numComponents = toCopy;
destPtr++;
memcpy(destPtr, sourcePtr,
(size_t) (toCopy * sizeof(Tcl_Token)));
parsePtr->numTokens += toCopy + 2;
break;
}
default:
sourcePtr = scratchPtr->tokenPtr + nodePtr->token;
end = sourcePtr->start + sourcePtr->size;
toCopy = sourcePtr->numComponents + 1;
if (tokenPtr == sourcePtr) {
tokenPtr += toCopy;
}
while (parsePtr->numTokens + toCopy - 1
>= parsePtr->tokensAvailable) {
TclExpandTokenArray(parsePtr);
}
destPtr = parsePtr->tokenPtr + parsePtr->numTokens;
memcpy(destPtr, sourcePtr,
(size_t) (toCopy * sizeof(Tcl_Token)));
parsePtr->numTokens += toCopy;
break;
}
nodePtr = nodes + nodePtr->parent;
break;
}
}
}
#endif
/*
*----------------------------------------------------------------------
*
* ParseLexeme --
*
* Parse a single lexeme from the start of a string, scanning no more
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
|
Tcl_FreeParse(exprParsePtr);
TclStackFree(interp, exprParsePtr);
Tcl_DecrRefCount(funcList);
Tcl_DecrRefCount(litList);
ckfree((char *) opTree);
return code;
}
/*
*----------------------------------------------------------------------
*
* ParseLexeme --
*
* Parse a single lexeme from the start of a string, scanning no more
|
| ︙ | | | ︙ | |
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
|
Tcl_SetStringObj(literal, start, (int) (end-start));
*literalPtr = literal;
} else {
Tcl_DecrRefCount(literal);
}
return (end-start);
}
#ifdef USE_EXPR_TOKENS
/*
* Boolean variable that controls whether expression compilation tracing is
* enabled.
*/
#ifdef TCL_COMPILE_DEBUG
static int traceExprComp = 0;
#endif /* TCL_COMPILE_DEBUG */
/*
* Definitions of numeric codes representing each expression operator. The
* order of these must match the entries in the operatorTable below. Also the
* codes for the relational operators (OP_LESS, OP_GREATER, OP_LE, OP_GE,
* OP_EQ, and OP_NE) must be consecutive and in that order. Note that OP_PLUS
* and OP_MINUS represent both unary and binary operators.
*/
#define OP_MULT 0
#define OP_DIVIDE 1
#define OP_MOD 2
#define OP_PLUS 3
#define OP_MINUS 4
#define OP_LSHIFT 5
#define OP_RSHIFT 6
#define OP_LESS 7
#define OP_GREATER 8
#define OP_LE 9
#define OP_GE 10
#define OP_EQ 11
#define OP_NEQ 12
#define OP_BITAND 13
#define OP_BITXOR 14
#define OP_BITOR 15
#define OP_LAND 16
#define OP_LOR 17
#define OP_QUESTY 18
#define OP_LNOT 19
#define OP_BITNOT 20
#define OP_STREQ 21
#define OP_STRNEQ 22
#define OP_EXPON 23
#define OP_IN_LIST 24
#define OP_NOT_IN_LIST 25
/*
* Table describing the expression operators. Entries in this table must
* correspond to the definitions of numeric codes for operators just above.
*/
static int opTableInitialized = 0; /* 0 means not yet initialized. */
TCL_DECLARE_MUTEX(opMutex)
typedef struct OperatorDesc {
const char *name; /* Name of the operator. */
int numOperands; /* Number of operands. 0 if the operator
* requires special handling. */
int instruction; /* Instruction opcode for the operator.
* Ignored if numOperands is 0. */
} OperatorDesc;
static OperatorDesc operatorTable[] = {
{"*", 2, INST_MULT},
{"/", 2, INST_DIV},
{"%", 2, INST_MOD},
{"+", 0},
{"-", 0},
{"<<", 2, INST_LSHIFT},
{">>", 2, INST_RSHIFT},
{"<", 2, INST_LT},
{">", 2, INST_GT},
{"<=", 2, INST_LE},
{">=", 2, INST_GE},
{"==", 2, INST_EQ},
{"!=", 2, INST_NEQ},
{"&", 2, INST_BITAND},
{"^", 2, INST_BITXOR},
{"|", 2, INST_BITOR},
{"&&", 0},
{"||", 0},
{"?", 0},
{"!", 1, INST_LNOT},
{"~", 1, INST_BITNOT},
{"eq", 2, INST_STR_EQ},
{"ne", 2, INST_STR_NEQ},
{"**", 2, INST_EXPON},
{"in", 2, INST_LIST_IN},
{"ni", 2, INST_LIST_NOT_IN},
{NULL}
};
/*
* Hashtable used to map the names of expression operators to the index of
* their OperatorDesc description.
*/
static Tcl_HashTable opHashTable;
#endif /* USE_EXPR_TOKENS */
/*
* Declarations for local procedures to this file:
*/
#ifdef USE_EXPR_TOKENS
static void CompileCondExpr(Tcl_Interp *interp,
Tcl_Token *exprTokenPtr, int *convertPtr,
CompileEnv *envPtr);
static void CompileLandOrLorExpr(Tcl_Interp *interp,
Tcl_Token *exprTokenPtr, int opIndex,
CompileEnv *envPtr);
static void CompileMathFuncCall(Tcl_Interp *interp,
Tcl_Token *exprTokenPtr, const char *funcName,
CompileEnv *envPtr);
static void CompileSubExpr(Tcl_Interp *interp,
Tcl_Token *exprTokenPtr, int *convertPtr,
CompileEnv *envPtr);
#endif /* USE_EXPR_TOKENS */
static void CompileExprTree(Tcl_Interp *interp, OpNode *nodes,
Tcl_Obj *const litObjv[], Tcl_Obj *funcList,
Tcl_Token *tokenPtr, int *convertPtr,
CompileEnv *envPtr);
/*
* Macro used to debug the execution of the expression compiler.
*/
#ifdef TCL_COMPILE_DEBUG
#define TRACE(exprBytes, exprLength, tokenBytes, tokenLength) \
if (traceExprComp) { \
fprintf(stderr, "CompileSubExpr: \"%.*s\", token \"%.*s\"\n", \
(exprLength), (exprBytes), (tokenLength), (tokenBytes)); \
}
#else
#define TRACE(exprBytes, exprLength, tokenBytes, tokenLength)
#endif /* TCL_COMPILE_DEBUG */
/*
*----------------------------------------------------------------------
*
* TclCompileExpr --
*
* This procedure compiles a string containing a Tcl expression into Tcl
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
|
Tcl_SetStringObj(literal, start, (int) (end-start));
*literalPtr = literal;
} else {
Tcl_DecrRefCount(literal);
}
return (end-start);
}
/*
*----------------------------------------------------------------------
*
* TclCompileExpr --
*
* This procedure compiles a string containing a Tcl expression into Tcl
|
| ︙ | | | ︙ | |
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
|
Tcl_Interp *interp, /* Used for error reporting. */
const char *script, /* The source script to compile. */
int numBytes, /* Number of bytes in script. If < 0, the
* string consists of all bytes up to the
* first null character. */
CompileEnv *envPtr) /* Holds resulting instructions. */
{
#ifndef USE_EXPR_TOKENS
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 =
(Tcl_Parse *) TclStackAlloc(interp, sizeof(Tcl_Parse));
/* Holds the Tcl_Tokens of substitutions */
|
<
|
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
|
Tcl_Interp *interp, /* Used for error reporting. */
const char *script, /* The source script to compile. */
int numBytes, /* Number of bytes in script. If < 0, the
* string consists of all bytes up to the
* first null character. */
CompileEnv *envPtr) /* Holds resulting instructions. */
{
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 =
(Tcl_Parse *) TclStackAlloc(interp, sizeof(Tcl_Parse));
/* Holds the Tcl_Tokens of substitutions */
|
| ︙ | | | ︙ | |
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
|
Tcl_FreeParse(parsePtr);
TclStackFree(interp, parsePtr);
Tcl_DecrRefCount(funcList);
Tcl_DecrRefCount(litList);
ckfree((char *) opTree);
return code;
#else
Tcl_Parse *parsePtr =
(Tcl_Parse *) TclStackAlloc(interp, sizeof(Tcl_Parse));
int needsNumConversion = 1;
/*
* If this is the first time we've been called, initialize the table of
* expression operators.
*/
if (numBytes < 0) {
numBytes = (script? strlen(script) : 0);
}
if (!opTableInitialized) {
Tcl_MutexLock(&opMutex);
if (!opTableInitialized) {
int i;
Tcl_InitHashTable(&opHashTable, TCL_STRING_KEYS);
for (i = 0; operatorTable[i].name != NULL; i++) {
int new;
Tcl_HashEntry *hPtr = Tcl_CreateHashEntry(&opHashTable,
operatorTable[i].name, &new);
if (new) {
Tcl_SetHashValue(hPtr, (ClientData) INT2PTR(i));
}
}
opTableInitialized = 1;
}
Tcl_MutexUnlock(&opMutex);
}
/*
* Parse the expression then compile it.
*/
if (TCL_OK != Tcl_ParseExpr(interp, script, numBytes, parsePtr)) {
TclStackFree(interp, parsePtr);
return TCL_ERROR;
}
/* TIP #280 : Track Lines within the expression */
TclAdvanceLines (&envPtr->line, script, parsePtr->tokenPtr->start);
CompileSubExpr(interp, parsePtr->tokenPtr, &needsNumConversion, envPtr);
if (needsNumConversion) {
/*
* Attempt to convert the primary's object to an int or double. This
* is done in order to support Tcl's policy of interpreting operands
* if at all possible as first integers, else floating-point numbers.
*/
TclEmitOpcode(INST_TRY_CVT_TO_NUMERIC, envPtr);
}
Tcl_FreeParse(parsePtr);
TclStackFree(interp, parsePtr);
return TCL_OK;
#endif
}
/*
*----------------------------------------------------------------------
*
* CompileExprTree --
* [???]
*
* Results:
* None.
*
* Side effects:
* Adds instructions to envPtr to evaluate the expression at runtime.
*
*----------------------------------------------------------------------
*/
typedef struct JumpList {
JumpFixup jump;
int depth;
int offset;
int convert;
struct JumpList *next;
} JumpList;
static void
CompileExprTree(
Tcl_Interp *interp,
OpNode *nodes,
Tcl_Obj *const litObjv[],
Tcl_Obj *funcList,
Tcl_Token *tokenPtr,
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
|
Tcl_FreeParse(parsePtr);
TclStackFree(interp, parsePtr);
Tcl_DecrRefCount(funcList);
Tcl_DecrRefCount(litList);
ckfree((char *) opTree);
return code;
}
/*
*----------------------------------------------------------------------
*
* CompileExprTree --
* [???]
*
* Results:
* None.
*
* Side effects:
* Adds instructions to envPtr to evaluate the expression at runtime.
*
*----------------------------------------------------------------------
*/
static void
CompileExprTree(
Tcl_Interp *interp,
OpNode *nodes,
Tcl_Obj *const litObjv[],
Tcl_Obj *funcList,
Tcl_Token *tokenPtr,
|
| ︙ | | | ︙ | |
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
|
TclOpCmdClientData *occdPtr = (TclOpCmdClientData *)clientData;
if (objc < 2) {
Tcl_WrongNumArgs(interp, 1, objv, occdPtr->expected);
return TCL_ERROR;
}
return TclVariadicOpCmd(clientData, interp, objc, objv);
}
/*
*----------------------------------------------------------------------
*
* TclFinalizeCompilation --
*
* Clean up the compilation environment so it can later be properly
* reinitialized. This procedure is called by Tcl_Finalize().
*
* Results:
* None.
*
* Side effects:
* Cleans up the compilation environment. At the moment, just the table
* of expression operators is freed.
*
*----------------------------------------------------------------------
*/
void
TclFinalizeCompilation(void)
{
#ifdef USE_EXPR_TOKENS
Tcl_MutexLock(&opMutex);
if (opTableInitialized) {
Tcl_DeleteHashTable(&opHashTable);
opTableInitialized = 0;
}
Tcl_MutexUnlock(&opMutex);
#endif
}
#ifdef USE_EXPR_TOKENS
/*
*----------------------------------------------------------------------
*
* CompileSubExpr --
*
* Given a pointer to a TCL_TOKEN_SUB_EXPR token describing a
* subexpression, this procedure emits instructions to evaluate the
* subexpression at runtime.
*
* Results:
* None.
*
* Side effects:
* Adds instructions to envPtr to evaluate the subexpression.
*
*----------------------------------------------------------------------
*/
static void
CompileSubExpr(
Tcl_Interp *interp, /* Interp in which to compile expression */
Tcl_Token *exprTokenPtr, /* Points to TCL_TOKEN_SUB_EXPR token to
* compile. */
int *convertPtr, /* Writes 0 here if it is determined the
* final INST_TRY_CVT_TO_NUMERIC is
* not needed */
CompileEnv *envPtr) /* Holds resulting instructions. */
{
/*
* Switch on the type of the first token after the subexpression token.
*/
Tcl_Token *tokenPtr = exprTokenPtr+1;
TRACE(exprTokenPtr->start, exprTokenPtr->size,
tokenPtr->start, tokenPtr->size);
switch (tokenPtr->type) {
case TCL_TOKEN_WORD:
TclCompileTokens(interp, tokenPtr+1, tokenPtr->numComponents, envPtr);
break;
case TCL_TOKEN_TEXT:
TclEmitPush(TclRegisterNewLiteral(envPtr,
tokenPtr->start, tokenPtr->size), envPtr);
break;
case TCL_TOKEN_BS: {
char buffer[TCL_UTF_MAX];
int length = Tcl_UtfBackslash(tokenPtr->start, NULL, buffer);
TclEmitPush(TclRegisterNewLiteral(envPtr, buffer, length), envPtr);
break;
}
case TCL_TOKEN_COMMAND:
TclCompileScript(interp, tokenPtr->start+1, tokenPtr->size-2, envPtr);
break;
case TCL_TOKEN_VARIABLE:
TclCompileTokens(interp, tokenPtr, 1, envPtr);
break;
case TCL_TOKEN_SUB_EXPR:
CompileSubExpr(interp, tokenPtr, convertPtr, envPtr);
break;
case TCL_TOKEN_OPERATOR: {
/*
* Look up the operator. If the operator isn't found, treat it as a
* math function.
*/
OperatorDesc *opDescPtr;
Tcl_HashEntry *hPtr;
const char *operator;
Tcl_DString opBuf;
int opIndex;
Tcl_DStringInit(&opBuf);
operator = Tcl_DStringAppend(&opBuf, tokenPtr->start, tokenPtr->size);
hPtr = Tcl_FindHashEntry(&opHashTable, operator);
if (hPtr == NULL) {
CompileMathFuncCall(interp, exprTokenPtr, operator, envPtr);
Tcl_DStringFree(&opBuf);
break;
}
Tcl_DStringFree(&opBuf);
opIndex = PTR2INT(Tcl_GetHashValue(hPtr));
opDescPtr = &(operatorTable[opIndex]);
/*
* If the operator is "normal", compile it using information from the
* operator table.
*/
if (opDescPtr->numOperands > 0) {
tokenPtr++;
CompileSubExpr(interp, tokenPtr, convertPtr, envPtr);
tokenPtr += (tokenPtr->numComponents + 1);
if (opDescPtr->numOperands == 2) {
CompileSubExpr(interp, tokenPtr, convertPtr, envPtr);
}
TclEmitOpcode(opDescPtr->instruction, envPtr);
*convertPtr = 0;
break;
}
/*
* The operator requires special treatment, and is either "+" or "-",
* or one of "&&", "||" or "?".
*/
switch (opIndex) {
case OP_PLUS:
case OP_MINUS: {
Tcl_Token *afterSubexprPtr = exprTokenPtr
+ exprTokenPtr->numComponents+1;
tokenPtr++;
CompileSubExpr(interp, tokenPtr, convertPtr, envPtr);
tokenPtr += (tokenPtr->numComponents + 1);
/*
* Check whether the "+" or "-" is unary.
*/
if (tokenPtr == afterSubexprPtr) {
TclEmitOpcode(((opIndex==OP_PLUS)? INST_UPLUS : INST_UMINUS),
envPtr);
break;
}
/*
* The "+" or "-" is binary.
*/
CompileSubExpr(interp, tokenPtr, convertPtr, envPtr);
TclEmitOpcode(((opIndex==OP_PLUS)? INST_ADD : INST_SUB), envPtr);
*convertPtr = 0;
break;
}
case OP_LAND:
case OP_LOR:
CompileLandOrLorExpr(interp, exprTokenPtr, opIndex, envPtr);
*convertPtr = 0;
break;
case OP_QUESTY:
CompileCondExpr(interp, exprTokenPtr, convertPtr, envPtr);
break;
default:
Tcl_Panic("CompileSubExpr: unexpected operator %d "
"requiring special treatment", opIndex);
} /* end switch on operator requiring special treatment */
break;
}
default:
Tcl_Panic("CompileSubExpr: unexpected token type %d", tokenPtr->type);
}
}
/*
*----------------------------------------------------------------------
*
* CompileLandOrLorExpr --
*
* This procedure compiles a Tcl logical and ("&&") or logical or ("||")
* subexpression.
*
* Results:
* None.
*
* Side effects:
* Adds instructions to envPtr to evaluate the expression at runtime.
*
*----------------------------------------------------------------------
*/
static void
CompileLandOrLorExpr(
Tcl_Interp *interp, /* Interp in which compile takes place */
Tcl_Token *exprTokenPtr, /* Points to TCL_TOKEN_SUB_EXPR token
* containing the "&&" or "||" operator. */
int opIndex, /* A code describing the expression operator:
* either OP_LAND or OP_LOR. */
CompileEnv *envPtr) /* Holds resulting instructions. */
{
JumpFixup shortCircuitFixup;/* Used to fix up the short circuit jump after
* the first subexpression. */
JumpFixup shortCircuitFixup2;
/* Used to fix up the second jump to the
* short-circuit target. */
JumpFixup endFixup; /* Used to fix up jump to the end. */
int convert = 0;
int savedStackDepth = envPtr->currStackDepth;
Tcl_Token *tokenPtr = exprTokenPtr+2;
/*
* Emit code for the first operand.
*/
CompileSubExpr(interp, tokenPtr, &convert, envPtr);
tokenPtr += (tokenPtr->numComponents + 1);
/*
* Emit the short-circuit jump.
*/
TclEmitForwardJump(envPtr,
((opIndex==OP_LAND)? TCL_FALSE_JUMP : TCL_TRUE_JUMP),
&shortCircuitFixup);
/*
* Emit code for the second operand.
*/
CompileSubExpr(interp, tokenPtr, &convert, envPtr);
/*
* The result is the boolean value of the second operand. We code this in
* a somewhat contorted manner to be able to reuse the shortCircuit value
* and save one INST_JUMP.
*/
TclEmitForwardJump(envPtr,
((opIndex==OP_LAND)? TCL_FALSE_JUMP : TCL_TRUE_JUMP),
&shortCircuitFixup2);
if (opIndex == OP_LAND) {
TclEmitPush(TclRegisterNewLiteral(envPtr, "1", 1), envPtr);
} else {
TclEmitPush(TclRegisterNewLiteral(envPtr, "0", 1), envPtr);
}
TclEmitForwardJump(envPtr, TCL_UNCONDITIONAL_JUMP, &endFixup);
/*
* Fixup the short-circuit jumps and push the shortCircuit value. Note
* that shortCircuitFixup2 is always a short jump.
*/
TclFixupForwardJumpToHere(envPtr, &shortCircuitFixup2, 127);
if (TclFixupForwardJumpToHere(envPtr, &shortCircuitFixup, 127)) {
/*
* shortCircuit jump grown by 3 bytes: update endFixup.
*/
endFixup.codeOffset += 3;
}
if (opIndex == OP_LAND) {
TclEmitPush(TclRegisterNewLiteral(envPtr, "0", 1), envPtr);
} else {
TclEmitPush(TclRegisterNewLiteral(envPtr, "1", 1), envPtr);
}
TclFixupForwardJumpToHere(envPtr, &endFixup, 127);
envPtr->currStackDepth = savedStackDepth + 1;
}
/*
*----------------------------------------------------------------------
*
* CompileCondExpr --
*
* This procedure compiles a Tcl conditional expression:
* condExpr ::= lorExpr ['?' condExpr ':' condExpr]
*
* Results:
* None.
*
* Side effects:
* Adds instructions to envPtr to evaluate the expression at runtime.
*
*----------------------------------------------------------------------
*/
static void
CompileCondExpr(
Tcl_Interp *interp, /* Interp in which compile takes place */
Tcl_Token *exprTokenPtr, /* Points to TCL_TOKEN_SUB_EXPR token
* containing the "?" operator. */
int *convertPtr, /* Describes the compilation state for the
* expression being compiled. */
CompileEnv *envPtr) /* Holds resulting instructions. */
{
JumpFixup jumpAroundThenFixup, jumpAroundElseFixup;
/* Used to update or replace one-byte jumps
* around the then and else expressions when
* their target PCs are determined. */
Tcl_Token *tokenPtr = exprTokenPtr+2;
int elseCodeOffset, dist, convert = 0;
int convertThen = 1, convertElse = 1;
int savedStackDepth = envPtr->currStackDepth;
/*
* Emit code for the test.
*/
CompileSubExpr(interp, tokenPtr, &convert, envPtr);
tokenPtr += (tokenPtr->numComponents + 1);
/*
* Emit the jump to the "else" expression if the test was false.
*/
TclEmitForwardJump(envPtr, TCL_FALSE_JUMP, &jumpAroundThenFixup);
/*
* Compile the "then" expression. Note that if a subexpression is only a
* primary, we need to try to convert it to numeric. We do this to support
* Tcl's policy of interpreting operands if at all possible as first
* integers, else floating-point numbers.
*/
CompileSubExpr(interp, tokenPtr, &convertThen, envPtr);
tokenPtr += (tokenPtr->numComponents + 1);
/*
* Emit an unconditional jump around the "else" condExpr.
*/
TclEmitForwardJump(envPtr, TCL_UNCONDITIONAL_JUMP, &jumpAroundElseFixup);
/*
* Compile the "else" expression.
*/
envPtr->currStackDepth = savedStackDepth;
elseCodeOffset = (envPtr->codeNext - envPtr->codeStart);
CompileSubExpr(interp, tokenPtr, &convertElse, envPtr);
/*
* Fix up the second jump around the "else" expression.
*/
dist = (envPtr->codeNext - envPtr->codeStart)
- jumpAroundElseFixup.codeOffset;
if (TclFixupForwardJump(envPtr, &jumpAroundElseFixup, dist, 127)) {
/*
* Update the else expression's starting code offset since it moved
* down 3 bytes too.
*/
elseCodeOffset += 3;
}
/*
* Fix up the first jump to the "else" expression if the test was false.
*/
dist = (elseCodeOffset - jumpAroundThenFixup.codeOffset);
TclFixupForwardJump(envPtr, &jumpAroundThenFixup, dist, 127);
*convertPtr = convertThen || convertElse;
envPtr->currStackDepth = savedStackDepth + 1;
}
/*
*----------------------------------------------------------------------
*
* CompileMathFuncCall --
*
* This procedure compiles a call on a math function in an expression:
* mathFuncCall ::= funcName '(' [condExpr {',' condExpr}] ')'
*
* Results:
* None.
*
* Side effects:
* Adds instructions to envPtr to evaluate the math function at
* runtime.
*
*----------------------------------------------------------------------
*/
static void
CompileMathFuncCall(
Tcl_Interp *interp, /* Interp in which compile takes place */
Tcl_Token *exprTokenPtr, /* Points to TCL_TOKEN_SUB_EXPR token
* containing the math function call. */
const char *funcName, /* Name of the math function. */
CompileEnv *envPtr) /* Holds resulting instructions. */
{
Tcl_DString cmdName;
int objIndex;
Tcl_Token *tokenPtr, *afterSubexprPtr;
int argCount;
/*
* Prepend "tcl::mathfunc::" to the function name, to produce the name of
* a command that evaluates the function. Push that command name on the
* stack, in a literal registered to the namespace so that resolution can
* be cached.
*/
Tcl_DStringInit(&cmdName);
Tcl_DStringAppend(&cmdName, "tcl::mathfunc::", -1);
Tcl_DStringAppend(&cmdName, funcName, -1);
objIndex = TclRegisterNewNSLiteral(envPtr, Tcl_DStringValue(&cmdName),
Tcl_DStringLength(&cmdName));
TclEmitPush(objIndex, envPtr);
Tcl_DStringFree(&cmdName);
/*
* Compile any arguments for the function.
*/
argCount = 1;
tokenPtr = exprTokenPtr+2;
afterSubexprPtr = exprTokenPtr + (exprTokenPtr->numComponents + 1);
while (tokenPtr != afterSubexprPtr) {
int convert = 0;
++argCount;
CompileSubExpr(interp, tokenPtr, &convert, envPtr);
tokenPtr += (tokenPtr->numComponents + 1);
}
/*
* Invoke the function.
*/
if (argCount < 255) {
TclEmitInstInt1(INST_INVOKE_STK1, argCount, envPtr);
} else {
TclEmitInstInt4(INST_INVOKE_STK4, argCount, envPtr);
}
}
#endif
/*
* Local Variables:
* mode: c
* c-basic-offset: 4
* fill-column: 78
* End:
*/
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
|
TclOpCmdClientData *occdPtr = (TclOpCmdClientData *)clientData;
if (objc < 2) {
Tcl_WrongNumArgs(interp, 1, objv, occdPtr->expected);
return TCL_ERROR;
}
return TclVariadicOpCmd(clientData, interp, objc, objv);
}
/*
* Local Variables:
* mode: c
* c-basic-offset: 4
* fill-column: 78
* End:
*/
|