Author: Florent Merlet <[email protected]>
State: Draft
Type: Project
Vote: Pending
Created: 29-Oct-2026
Tcl-Version: 9.2
Keywords: expr, command
Post-History:
Abstract
In an expression, some commands may be written without brackets:
expr {llength $L + 1} ;# [llength $L] + 1
proc dot {U V} {( s = 0; foreach u $U v $V {( s = $s + $u*$v )}; $s )}
It is the sixth part of TIP 759. This part is provisional: the list of command names is fixed in the parser for now, and is to be replaced by a register of the commands allowed without brackets. It is published so that the register can be designed and tested on a working base.
Specification (current implementation)
A raw command is a command whose name is one of
concat dict for foreach global if incr lappend lassign lindex llength lmap lrange lset namespace return string switch upvar variable while, followed by a space, where an operand is expected.It ends at a
;, at an operator or a close parenthesis that begins a word, or at the end of the expression. The terminator is then parsed as part of the expression. An operator inside a word does not end it:lindex $L end-1is one command. Newlines separate the commands of a raw script.Its result is the value of the operand.
C API. A new token type
TCL_TOKEN_CMD_IN_EXPR(512), the text of the command, no components.Tcl_ParseCommandaccepts a new value ofnested,TCL_CMD_NESTED_IN_EXPR(internal), and the character table of the parser gets aTYPE_OPclass for the operator characters.
Open questions, for the register
- Which commands? (
lrepeatis missing, for instance.) Defined where: fixed list, per interpreter, per namespace? - The names are reserved in expressions: a variable named
incrcannot be assigned withincr = 1. :is in theTYPE_OPclass, so a word that begins with::ends a raw command.
Implementation
Patch 0006-TIP-759-6-7-raw-commands-in-expressions-provisional.patch:
tclParse.h,tclParse.c:TYPE_OP,intcharacter table,TCL_CMD_NESTED_IN_EXPR; theTYPE_NORMALfast paths ofTcl_ParseBracesandTclScanElementignoreTYPE_OP.tcl.h:TCL_TOKEN_CMD_IN_EXPR.tclCompExpr.c:COMMANDlexeme inParseLexeme, parsing inParseExpr.tclCompile.c: compiled as a script.tclTest.c:testexprparsershows the token asrawcommand.- Tests:
tests/exprRawCmd.test. Documentation:expr.n.
Copyright
This document has been placed in the public domain.
