From bc5ba48345fe066e283ee96e2a7529ae6b63f1f7 Mon Sep 17 00:00:00 2001
From: Florent Merlet <[email protected]>
Date: Thu, 29 Oct 2026 10:57:32 +0100
Subject: [PATCH 3/7] TIP 759 (3/7): lists (a, b, ...) in expressions
In an expression, two or more expressions separated by commas, in
parentheses that do not follow a function name, give the list of their
values: expr {($x, $y)}, lmap a $L0 b $L1 {($a, $b)}.
- ParseExpr: a NULL_FUNC node is made before each open paren that is not
a function's; a comma at the top of the parens turns it into a LIST
node. NULL_FUNC does nothing and keeps constant folding.
- CompileExprTree: LIST compiles to INST_LIST; its value is not
converted to a number.
- ConvertTreeToTokens (Tcl_ParseExpr): NULL_FUNC has no tokens; a list
is shown as a call whose operator token is empty.
- Tests: exprList.test; parseExpr-21.32. Docs: expr.n.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01MrNWAEyXLqekovNBvbMZ4z
---
doc/Tcl.n | 4 +-
doc/expr.n | 18 ++++++
generic/tclCompExpr.c | 96 +++++++++++++++++++++++++++---
tests/exprList.test | 134 ++++++++++++++++++++++++++++++++++++++++++
tests/parseExpr.test | 7 ++-
5 files changed, 247 insertions(+), 12 deletions(-)
create mode 100644 tests/exprList.test
diff --git a/doc/Tcl.n b/doc/Tcl.n
index b577927..4ae8957 100644
--- a/doc/Tcl.n
+++ b/doc/Tcl.n
@@ -111,11 +111,11 @@ separate operands: it is ignored, except that it ends a number or a
name. An operand ends at the first character that cannot continue it,
such as white space, an operator, a comma or a parenthesis.
Two operands must be separated by an operator, or by a comma between
-the arguments of a function; otherwise the expression is an error, as
+the arguments of a function or the elements of a list; otherwise the expression is an error, as
in \fB1 2\fR or \fB\N'34'a\N'34'\N'34'b\N'34'\fR, whereas
\fB\N'34'a\N'34'eq\N'34'a\N'34'\fR is valid.
An operand is a number, a boolean value, a quoted word (rule [4]),
-a braced word (rule [6]), a substitution (rules [7] to [9]) or a function call.
+a braced word (rule [6]), a substitution (rules [7] to [9]), a function call or a list.
The value of a substituted operand is always a single operand: it is
never parsed again as an expression.
.VE TIP759
diff --git a/doc/expr.n b/doc/expr.n
index 626f975..4132fd5 100644
--- a/doc/expr.n
+++ b/doc/expr.n
@@ -125,6 +125,17 @@ A function call.
This is mathematical function such as \fBsin($x)\fR, whose arguments have any of
the above forms for operands. See \fBMATH FUNCTIONS\fR below for
a discussion of how mathematical functions are handled.
+.TP
+A \fBlist\fR.
+.
+.VS TIP759
+Two or more expressions separated by commas, in parentheses that do
+not follow a function name: \fB(\fIa\fB, \fIb\fB, \fR...\fB)\fR.
+The value is the list of the values of the expressions, as built by the
+\fBlist\fR command; it is not converted to a number. Lists may be
+nested: the value of \fB((1, 2), 3)\fR is \fB{1 2} 3\fR. A
+parenthesized expression without a comma is not a list.
+.VE TIP759
.RE
.PP
Because \fBexpr\fR parses and performs substitutions on values that have
@@ -464,6 +475,13 @@ proc hypot {a b} {( sqrt($a**2 + $b**2) )}
lmap x {1 2 3} {( $x * $x )}
.CE
.PP
+Build lists of computed values, for instance coordinates for a canvas:
+.PP
+.CS
+set pairs [lmap a $xs b $ys {( $a, $b )}]
+set coords [lmap x $xs {( $x, sin($x) )}]
+.CE
+.PP
Compute a value with an expression substitution, the short form of
\fB[expr {...}]\fR in a word:
.PP
diff --git a/generic/tclCompExpr.c b/generic/tclCompExpr.c
index 1d9d309..15d554e 100644
--- a/generic/tclCompExpr.c
+++ b/generic/tclCompExpr.c
@@ -212,6 +212,11 @@ enum LexemeCodes {
* CLOSE_PAREN below. */
NOT = UNARY | 6,
BIT_NOT = UNARY | 7,
+ NULL_FUNC = UNARY | 8, /* TIP 759: made before an open paren that
+ * does not follow a function name. It does
+ * nothing, unless a comma follows at the top
+ * of the parens: it then becomes LIST. */
+ LIST = UNARY | 9, /* TIP 759: the list (a, b, ...) */
/* Binary operator lexemes */
@@ -309,7 +314,8 @@ enum Precedence {
PREC_ADD, /* "+", "-" */
PREC_MULT, /* "*", "/", "%" */
PREC_EXPON, /* "**" */
- PREC_UNARY /* "+", "-", FUNCTION, "!", "~" */
+ PREC_UNARY /* "+", "-", FUNCTION, "!", "~", NULL_FUNC,
+ * LIST */
};
/*
@@ -369,6 +375,8 @@ static const unsigned char prec[] = {
PREC_OPEN_PAREN, /* OPEN_PAREN */
PREC_UNARY, /* NOT*/
PREC_UNARY, /* BIT_NOT*/
+ PREC_UNARY, /* NULL_FUNC */
+ PREC_UNARY, /* LIST */
};
/*
@@ -426,6 +434,8 @@ static const unsigned char instruction[] = {
0, /* OPEN_PAREN */
INST_LNOT, /* NOT*/
INST_BITNOT, /* BIT_NOT*/
+ 0, /* NULL_FUNC */
+ 0, /* LIST */
};
/*
@@ -682,11 +692,11 @@ ParseExpr(
* worth preserving. */
/*
- * Each pass through this loop adds up to one more OpNode. Allocate
- * space for one if required.
+ * Each pass through this loop adds up to two more OpNodes (TIP 759: a
+ * NULL_FUNC before an open paren). Allocate space if required.
*/
- if (nodesUsed >= nodesAvailable) {
+ if (nodesUsed + 1 >= nodesAvailable) {
size_t size = nodesUsed * 2;
OpNode *newPtr = NULL;
@@ -1113,6 +1123,25 @@ ParseExpr(
goto error;
}
+ if ((lexeme == OPEN_PAREN) && (nodes[lastParsed].lexeme != FUNCTION)) {
+ /*
+ * TIP 759: an open paren that is not the one of a function
+ * call. Whether it starts a list (a, b, ...) is not known
+ * yet: make room for a LIST node before it, a NULL_FUNC
+ * node, which does nothing unless a comma turns it into a
+ * LIST node.
+ */
+
+ nodePtr->lexeme = NULL_FUNC;
+ nodePtr->precedence = prec[NULL_FUNC];
+ nodePtr->mark = MARK_RIGHT;
+ nodePtr->constant = 1;
+ nodePtr->prev = incomplete;
+ incomplete = lastParsed = nodesUsed;
+ nodesUsed++;
+ nodePtr++;
+ }
+
/*
* Create an OpNode for the unary operator.
*/
@@ -1388,10 +1417,19 @@ ParseExpr(
}
}
- /* Commas must appear only in function argument lists. */
+ /*
+ * Commas must appear only in function argument lists, or in a
+ * list (a, b, ...) (TIP 759): the NULL_FUNC node made before its
+ * open paren then becomes a LIST node.
+ */
+
if (lexeme == COMMA) {
- if ((incompletePtr->lexeme != OPEN_PAREN)
- || (incompletePtr[-1].lexeme != FUNCTION)) {
+ if ((incompletePtr->lexeme == OPEN_PAREN)
+ && (incompletePtr[-1].lexeme == NULL_FUNC)) {
+ incompletePtr[-1].lexeme = LIST;
+ } else if ((incompletePtr->lexeme != OPEN_PAREN)
+ || ((incompletePtr[-1].lexeme != FUNCTION)
+ && (incompletePtr[-1].lexeme != LIST))) {
TclNewLiteralStringObj(msg,
"unexpected \",\" outside function argument list");
errCode = "SURPRISE";
@@ -1707,13 +1745,15 @@ ConvertTreeToTokens(
*/
switch (nodePtr->lexeme) {
+ case NULL_FUNC:
case OPEN_PAREN:
case COMMA:
case COLON:
/*
* Historical practice has been to have no Tcl_Tokens for
- * these operators.
+ * these operators. The NULL_FUNC node made before an open
+ * paren (TIP 759) has none either.
*/
break;
@@ -1780,6 +1820,22 @@ ConvertTreeToTokens(
start += scanned;
numBytes -= scanned;
+ if (nodePtr->lexeme == NULL_FUNC || nodePtr->lexeme == LIST) {
+ /*
+ * TIP 759: these nodes have no text: the open paren that
+ * follows is the one of the OPEN_PAREN node. A list
+ * (a, b, ...) is shown as a call of a function whose name,
+ * the TCL_TOKEN_OPERATOR token, is empty.
+ */
+
+ if (nodePtr->lexeme == LIST) {
+ subExprTokenPtr = parsePtr->tokenPtr + subExprTokenIdx;
+ subExprTokenPtr[1].start = start;
+ subExprTokenPtr[1].size = 0;
+ }
+ break;
+ }
+
/*
* Here we scan from the string the operator corresponding to
* nodePtr->lexeme.
@@ -1822,6 +1878,7 @@ ConvertTreeToTokens(
/* When we get back to the START node, we're done. */
return;
+ case NULL_FUNC:
case COMMA:
case COLON:
@@ -2541,6 +2598,17 @@ CompileExprTree(
numWords = 2; /* Command plus one argument */
break;
}
+ case LIST:
+ /*
+ * TIP 759: start a count of the number of elements of this
+ * list. In case there's already a count in progress (nested
+ * functions or lists), save it in our unused "left" field
+ * for restoring later.
+ */
+
+ nodePtr->left = numWords;
+ numWords = 1;
+ break;
case QUESTION:
newJump = (JumpList *)TclStackAlloc(interp, sizeof(JumpList));
newJump->next = jumpPtr;
@@ -2579,10 +2647,22 @@ CompileExprTree(
OP( TRY_CVT_TO_NUMERIC);
}
break;
+ case NULL_FUNC:
case OPEN_PAREN:
/* do nothing */
break;
+ case LIST:
+ /*
+ * TIP 759: build the list (a, b, ...) from its elements, then
+ * restore any saved numWords value. A list is not converted
+ * to a number.
+ */
+
+ OP4( LIST, numWords);
+ numWords = nodePtr->left;
+ convert = false;
+ break;
case FUNCTION:
/*
* Use the numWords count we've kept to invoke the function
diff --git a/tests/exprList.test b/tests/exprList.test
new file mode 100644
index 0000000..9f2616a
--- /dev/null
+++ b/tests/exprList.test
@@ -0,0 +1,134 @@
+# Commands covered: expr, lists (a, b, ...) (TIP 759)
+#
+# This file contains a collection of tests for the comma lists of TIP 759 in
+# expressions. Sourcing this file into Tcl runs the tests and generates
+# output for errors. No output means no errors were found.
+#
+# See the file "license.terms" for information on usage and redistribution of
+# this file, and for a DISCLAIMER OF ALL WARRANTIES.
+
+if {"::tcltest" ni [namespace children]} {
+ package require tcltest 2.5
+ namespace import -force ::tcltest::*
+}
+
+::tcltest::loadTestedCommands
+catch [list package require -exact tcl::test [info patchlevel]]
+
+testConstraint testexprparser [llength [info commands testexprparser]]
+
+test exprList-1.1 {two elements} {
+ expr {(1,2)}
+} {1 2}
+test exprList-1.2 {computed elements, string element} {
+ set a 3
+ expr {($a, $a*2, "x y")}
+} {3 6 {x y}}
+test exprList-1.3 {nested lists} {
+ expr {((1,2),(3,4))}
+} {{1 2} {3 4}}
+test exprList-1.4 {nested, mixed} {
+ expr {((1,2), 3)}
+} {{1 2} 3}
+test exprList-1.5 {not converted to a number} {
+ expr {("0x10", 1)}
+} {0x10 1}
+test exprList-1.7 {list argument of a function} -setup {
+ proc ::tcl::mathfunc::len {l} {llength $l}
+} -body {
+ expr {len((1,2,3))}
+} -cleanup {
+ rename ::tcl::mathfunc::len {}
+} -result 3
+test exprList-1.8 {string comparison of a list} {
+ expr {(1, (2,3)) eq "1 {2 3}"}
+} 1
+test exprList-1.9 {conditional inside an element} {
+ expr {(1 ? 2 : 3, 4)}
+} {2 4}
+test exprList-1.10 {braced element} {
+ expr {("a b", {c})}
+} {{a b} c}
+test exprList-1.11 {lmap over two lists} {
+ lmap a {1 2 3} b {a b c} {expr {($a,$b)}}
+} {{1 a} {2 b} {3 c}}
+test exprList-1.12 {in a procedure} -setup {
+ proc p {x} {expr {($x, $x*$x)}}
+} -body {
+ p 4
+} -cleanup {
+ rename p {}
+} -result {4 16}
+test exprList-1.13 {not compiled} {
+ set e {(1, 2+3)}
+ expr $e
+} {1 5}
+test exprList-1.14 {many elements} {
+ expr {(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)}
+} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20}
+test exprList-1.15 {deep nesting} {
+ expr {(((((1,2),3),4),5),6)}
+} {{{{{1 2} 3} 4} 5} 6}
+
+# Parentheses without a comma are unchanged
+
+test exprList-2.1 {parenthesized expression} {
+ expr {(1+2)*3}
+} 9
+test exprList-2.2 {constant folding is kept} {
+ string match {*push*"9"*done*} \
+ [tcl::unsupported::disassemble script {expr {(1+2)*3}}]
+} 1
+test exprList-2.3 {parens in a function argument} {
+ expr {max((1),2)}
+} 2
+test exprList-2.4 {unary minus and power} {
+ list [expr {-(2)**2}] [expr {(2)**2}]
+} {4 4}
+test exprList-2.5 {nested parens} {
+ expr {(((5)))}
+} 5
+test exprList-2.6 {numeric conversion of a function result is kept} {
+ list [expr {abs(0x80000000)}] [expr {min(300, "0xFF")}]
+} {2147483648 255}
+
+# Errors
+
+test exprList-3.1 {comma outside parens} -body {
+ expr {1,2}
+} -returnCodes error -match glob -result {unexpected "," outside function argument list*}
+test exprList-3.2 {comma after parens} -body {
+ expr {(1),2}
+} -returnCodes error -match glob -result {unexpected "," outside function argument list*}
+test exprList-3.3 {missing element} -body {
+ expr {(1,)}
+} -returnCodes error -match glob -result {missing function argument*}
+test exprList-3.4 {empty parens} -body {
+ expr {()}
+} -returnCodes error -match glob -result {empty subexpression*}
+
+# Tcl_ParseExpr
+
+test exprList-4.1 {Tcl_ParseExpr: a list is a call with no name} -constraints {
+ testexprparser
+} -body {
+ testexprparser {(1,2)} -1
+} -result {- {} 0 subexpr (1,2) 5 operator {} 0 subexpr 1 1 text 1 0 subexpr 2 1 text 2 0 {}}
+test exprList-4.2 {Tcl_ParseExpr: parens without a comma} -constraints {
+ testexprparser
+} -body {
+ testexprparser {(1+2)} -1
+} -result {- {} 0 subexpr 1+2 5 operator + 0 subexpr 1 1 text 1 0 subexpr 2 1 text 2 0 {}}
+test exprList-4.3 {Tcl_ParseExpr: list in a function call} -constraints {
+ testexprparser
+} -body {
+ testexprparser {f(1,(2,3))} -1
+} -result {- {} 0 subexpr f(1,(2,3)) 9 operator f 0 subexpr 1 1 text 1 0 subexpr (2,3) 5 operator {} 0 subexpr 2 1 text 2 0 subexpr 3 1 text 3 0 {}}
+
+# cleanup
+::tcltest::cleanupTests
+return
+
+# Local Variables:
+# mode: tcl
+# End:
diff --git a/tests/parseExpr.test b/tests/parseExpr.test
index b0fc4bf..949984b 100644
--- a/tests/parseExpr.test
+++ b/tests/parseExpr.test
@@ -862,9 +862,12 @@ test parseExpr-21.31 {error messages} -body {
} -returnCodes error -result {unexpected "," outside function argument list
in expression "0,0"}
test parseExpr-21.32 {error messages} -body {
- expr (0,0)
+ expr 0,0
} -returnCodes error -result {unexpected "," outside function argument list
-in expression "(0,0)"}
+in expression "0,0"}
+test parseExpr-21.32.1 {comma list, TIP 759} -body {
+ expr (0,0)
+} -result {0 0}
test parseExpr-21.33 {error messages} -body {
expr a(0:0,0)
} -returnCodes error -result {unexpected operator ":" without preceding "?"
--
2.43.0