Skip to content

Commit 1843b79

Browse files
committed
expr: TIP 526, only support a single arg
Avoid unexpected issues by concatenating multiple arguments. This does create an incompatibility with early versions, but it is generally trivial to convert existing code to one of two forms: 1. expr {$a + $b} -- usually correct 2. expr "$a + $b" -- usually incorrect Signed-off-by: Steve Bennett <steveb@workware.net.au>
1 parent 5fed880 commit 1843b79

File tree

8 files changed

+29
-43
lines changed

8 files changed

+29
-43
lines changed

jim.c

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13590,26 +13590,11 @@ static int Jim_UplevelCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *
1359013590
/* [expr] */
1359113591
static int Jim_ExprCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
1359213592
{
13593-
int retcode;
13594-
1359513593
if (argc == 2) {
13596-
retcode = Jim_EvalExpression(interp, argv[1]);
13597-
}
13598-
else if (argc > 2) {
13599-
Jim_Obj *objPtr;
13600-
13601-
objPtr = Jim_ConcatObj(interp, argc - 1, argv + 1);
13602-
Jim_IncrRefCount(objPtr);
13603-
retcode = Jim_EvalExpression(interp, objPtr);
13604-
Jim_DecrRefCount(interp, objPtr);
13594+
return Jim_EvalExpression(interp, argv[1]);
1360513595
}
13606-
else {
13607-
Jim_WrongNumArgs(interp, 1, argv, "expression ?...?");
13608-
return JIM_ERR;
13609-
}
13610-
if (retcode != JIM_OK)
13611-
return retcode;
13612-
return JIM_OK;
13596+
Jim_WrongNumArgs(interp, 1, argv, "expression");
13597+
return JIM_ERR;
1361313598
}
1361413599

1361513600
/* [break] */

jim_tcl.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Changes since 0.80
6565
8. `lsort -index` now supports multiple indices
6666
9. Add support for `lsort -stride`
6767
10. `open` now supports POSIX-style access arguments
68+
11. TIP 526, `expr` now only allows a single argument
6869

6970
Changes between 0.79 and 0.80
7071
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

tests/expr-new.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ test expr-1.2 {TclCompileExprCmd: one expression word} {
7070
expr -25
7171
} -25
7272
test expr-1.3 {TclCompileExprCmd: two expression words} {
73-
expr -8.2 -6
73+
expr {-8.2 -6}
7474
} -14.2
7575
test expr-1.4 {TclCompileExprCmd: five expression words} {
76-
expr 20 - 5 +10 -7
76+
expr {20 - 5 +10 -7}
7777
} 18
7878
test expr-1.5 {TclCompileExprCmd: quoted expression word} {
7979
expr "0005"
@@ -111,7 +111,7 @@ test expr-1.13 {TclCompileExprCmd: second level of substitutions in expr not in
111111
} foo
112112
test expr-1.14 {TclCompileExprCmd: second level of substitutions in expr with comparison as top-level operator} {
113113
set a xxx
114-
set x 2; set b {$x}; set a [expr $b == 2]
114+
set x 2; set b {$x}; set a [expr "$b == 2"]
115115
set a
116116
} 1
117117

tests/expr-old.test

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ test expr-old-2.35 {floating-point operators} {expr 3.3||0.0} 1
128128
test expr-old-2.36 {floating-point operators} {expr 3.3>2.3?44.3:66.3} 44.3
129129
test expr-old-2.37 {floating-point operators} {expr 2.3>3.3?44.3:66.3} 66.3
130130
test expr-old-2.38 {floating-point operators} {
131-
list [catch {expr 028.1 + 09.2} msg] $msg
131+
list [catch {expr {028.1 + 09.2}} msg] $msg
132132
} {0 37.3}
133133

134134
# Operators that aren't legal on floating-point numbers
@@ -553,7 +553,7 @@ test expr-old-30.2 {long values} {
553553
# Expressions spanning multiple arguments
554554

555555
test expr-old-31.1 {multiple arguments to expr command} {
556-
expr 4 + ( 6 *12) -3
556+
expr {4 + ( 6 *12) -3}
557557
} 73
558558
test expr-old-31.2 {multiple arguments to expr command} {
559559
list [catch {expr 2 + (3 + 4} msg]
@@ -577,7 +577,7 @@ test expr-old-32.3 {math functions in expressions} mathfunc {
577577
format %.6g [expr atan(1.0)]
578578
} {0.785398}
579579
test expr-old-32.4 {math functions in expressions} mathfunc {
580-
format %.6g [expr atan2(2.0, 2.0)]
580+
format %.6g [expr {atan2(2.0, 2.0)}]
581581
} {0.785398}
582582
test expr-old-32.5 {math functions in expressions} mathfunc {
583583
format %.6g [expr ceil(1.999)]
@@ -598,10 +598,10 @@ test expr-old-32.10 {math functions in expressions} mathfunc {
598598
format %.6g [expr floor(2.001)]
599599
} {2}
600600
test expr-old-32.11 {math functions in expressions} expr_fmod {
601-
format %.6g [expr fmod(7.3, 3.2)]
601+
format %.6g [expr {fmod(7.3, 3.2)}]
602602
} {0.9}
603603
test expr-old-32.12 {math functions in expressions} expr_hypot {
604-
format %.6g [expr hypot(3.0, 4.0)]
604+
format %.6g [expr {hypot(3.0, 4.0)}]
605605
} {5}
606606
test expr-old-32.13 {math functions in expressions} mathfunc {
607607
format %.6g [expr log(2.8)]
@@ -610,7 +610,7 @@ test expr-old-32.14 {math functions in expressions} mathfunc {
610610
format %.6g [expr log10(2.8)]
611611
} {0.447158}
612612
test expr-old-32.15 {math functions in expressions} mathfunc {
613-
format %.6g [expr pow(2.1, 3.1)]
613+
format %.6g [expr {pow(2.1, 3.1)}]
614614
} {9.97424}
615615
test expr-old-32.16 {math functions in expressions} mathfunc {
616616
format %.6g [expr sin(.1)]
@@ -692,7 +692,7 @@ test expr-old-32.38 {math functions in expressions} {
692692
# list [catch {expr round(-1e60)} msg] $msg
693693
#} {1 {integer value too large to represent}}
694694
test expr-old-32.41 {math functions in expressions} mathfunc {
695-
list [catch {expr pow(1.0 + 3.0 - 2, .8 * 5)} msg] $msg
695+
list [catch {expr {pow(1.0 + 3.0 - 2, .8 * 5)}} msg] $msg
696696
} {0 16.0}
697697
if {1} {
698698
test expr-old-32.42 {math functions in expressions} expr_hypot {
@@ -702,7 +702,7 @@ test expr-old-32.43 {math functions in expressions} mathfunc {
702702
expr {pow(1.0 + 3.0, -2)}
703703
} {0.0625}
704704
test expr-old-32.45 {math functions in expressions} {
705-
expr (0 <= rand()) && (rand() < 1)
705+
expr {(0 <= rand()) && (rand() < 1)}
706706
} {1}
707707
test expr-old-32.46 {math functions in expressions} -body {
708708
expr rand(24)
@@ -719,7 +719,7 @@ test expr-old-32.49 {math functions in expressions} -body {
719719
} -returnCodes error -match glob -result *
720720
test expr-old-32.50 {math functions in expressions} mathfunc {
721721
for {set i 0} {$i < 10} {incr i} {
722-
lappend result [expr round(sin($i) * 1000)]
722+
lappend result [expr {round(sin($i) * 1000)}]
723723
}
724724
set result
725725
} {0 841 909 141 -757 -959 -279 657 989 412}
@@ -728,13 +728,13 @@ test expr-old-32.51 {math functions in expressions} -body {
728728
} -returnCodes error -match glob -result *
729729

730730
test expr-old-33.1 {conversions and fancy args to math functions} expr_hypot {
731-
expr hypot ( 3 , 4 )
731+
expr {hypot ( 3 , 4 )}
732732
} 5.0
733733
test expr-old-33.2 {conversions and fancy args to math functions} expr_hypot {
734-
expr hypot ( (2.0+1.0) , 4 )
734+
expr {hypot ( (2.0+1.0) , 4 )}
735735
} 5.0
736736
test expr-old-33.3 {conversions and fancy args to math functions} expr_hypot {
737-
expr hypot ( 3 , (3.0 + 1.0) )
737+
expr {hypot ( 3 , (3.0 + 1.0) )}
738738
} 5.0
739739
test expr-old-33.4 {conversions and fancy args to math functions} mathfunc {
740740
format %.6g [expr cos(acos(0.1))]

tests/expr.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ test expr-1.2 "Compare strings with embedded nulls" {
1111
} {1}
1212

1313
test expr-1.3 "Hex values" {
14-
set mask1 [expr 0x4050 & 0x0CCC]
14+
set mask1 [expr {0x4050 & 0x0CCC}]
1515
} {64}
1616

1717
test expr-1.4 "Ternary operator - true" {

tests/format.test

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ test format-13.1 {tcl_precision fuzzy comparison} {
419419
set a 0.0000000000001
420420
set b 0.00000000000001
421421
set c 0.00000000000000001
422-
set d [expr $a + $b + $c]
422+
set d [expr {$a + $b + $c}]
423423
format {%0.10f %0.12f %0.15f %0.17f} $d $d $d $d
424424
} {0.0000000000 0.000000000000 0.000000000000110 0.00000000000011001}
425425
test format-13.2 {tcl_precision fuzzy comparison} {
@@ -430,7 +430,7 @@ test format-13.2 {tcl_precision fuzzy comparison} {
430430
set a 0.000000000001
431431
set b 0.000000000000005
432432
set c 0.0000000000000008
433-
set d [expr $a + $b + $c]
433+
set d [expr {$a + $b + $c}]
434434
format {%0.10f %0.12f %0.15f %0.17f} $d $d $d $d
435435
} {0.0000000000 0.000000000001 0.000000000001006 0.00000000000100580}
436436
test format-13.3 {tcl_precision fuzzy comparison} {
@@ -439,7 +439,7 @@ test format-13.3 {tcl_precision fuzzy comparison} {
439439
catch {unset c}
440440
set a 0.00000000000099
441441
set b 0.000000000000011
442-
set c [expr $a + $b]
442+
set c [expr {$a + $b}]
443443
format {%0.10f %0.12f %0.15f %0.17f} $c $c $c $c
444444
} {0.0000000000 0.000000000001 0.000000000001001 0.00000000000100100}
445445
test format-13.4 {tcl_precision fuzzy comparison} {
@@ -448,7 +448,7 @@ test format-13.4 {tcl_precision fuzzy comparison} {
448448
catch {unset c}
449449
set a 0.444444444444
450450
set b 0.33333333333333
451-
set c [expr $a + $b]
451+
set c [expr {$a + $b}]
452452
format {%0.10f %0.12f %0.15f %0.16f} $c $c $c $c
453453
} {0.7777777778 0.777777777777 0.777777777777330 0.7777777777773300}
454454
test format-13.5 {tcl_precision fuzzy comparison} {
@@ -457,7 +457,7 @@ test format-13.5 {tcl_precision fuzzy comparison} {
457457
catch {unset c}
458458
set a 0.444444444444
459459
set b 0.99999999999999
460-
set c [expr $a + $b]
460+
set c [expr {$a + $b}]
461461
format {%0.10f %0.12f %0.15f} $c $c $c
462462
} {1.4444444444 1.444444444444 1.444444444443990}
463463
test format-14.1 {testing MAX_FLOAT_SIZE for 0 and 1} {
@@ -486,7 +486,7 @@ for {set i 0} {$i < 290} {incr i} {
486486
append b $a
487487
}
488488
for {set i 290} {$i < 400} {incr i} {
489-
test format-15.[expr $i -290] {testing MAX_FLOAT_SIZE} {
489+
test format-15.[expr {$i -290}] {testing MAX_FLOAT_SIZE} {
490490
format {%s} $b
491491
} $b
492492
append b "x"

tests/jim.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1976,7 +1976,7 @@ test foreach-6.1 {noncompiled foreach and shared variable or value list objects
19761976
catch {unset x}
19771977
foreach {12.0} {a b c} {
19781978
set x 12.0
1979-
set x [expr $x + 1]
1979+
set x [expr {$x + 1}]
19801980
}
19811981
set x
19821982
} 13.0

tests/list.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ concat {}
8383

8484
proc slowsort list {
8585
set result {}
86-
set last [expr [llength $list] - 1]
86+
set last [expr {[llength $list] - 1}]
8787
while {$last > 0} {
88-
set minIndex [expr [llength $list] - 1]
88+
set minIndex [expr {[llength $list] - 1}]
8989
set min [lindex $list $last]
9090
set i [expr $minIndex-1]
9191
while {$i >= 0} {

0 commit comments

Comments
 (0)