|
From: Colin M. <col...@ya...> - 2025-11-01 16:49:40
|
On 30/10/2025 01:18, Andreas Leitgeb wrote:
> - [= ...] with a modified expr-language using barewords as variable
> names would probably solve many (if not most) of the cases, where
> [expr {...}] really "hurts" for its verbosity.
Here is a pure Tcl implementation of that:
proc = args {
set ex [join $args]
set exp [regsub -all
{(::)?[[:alpha:]]([[:alnum:]_]|::)*([^[:alnum:]_(]|$)} $ex {$&}]
uplevel expr $exp
}
This just finds all the possible variable names in the expression,
prefixes them with $ and then runs expr on the result. The regex is a
little complicated because:
* The variable name could include :: at the beginning or in the middle
for namespacing.
* We disallow single colon : within the name because that creates
ambiguity with the ternary operator ?: .
* We also disallow array references because we can't distinguish them
from mathfunc calls.
The fact that this can be implemented in pure Tcl shows that it doesn't
require any change to the dodekalogue rules, and so there is no
consequence for subst either.
Of course a real implementation should be done in C. It could then also
be possible to implement the extension Kevin Kenny suggested of adding
`=` as an assignment operator as in C within an expression, and perhaps
even chaining multiple assignments with `,` .
I saw the `expr` question is on the agenda for the call on Monday. I
don't usually join these calls but I would join it for this issue except
that I have an urgent dentist's appointment at that time. Perhaps I
should create a new TIP for this proposal?
Colin.
|