Cecil Westerhof <
Ce...@decebal.nl> writes:
> I want to have the possibility to write things without expr, like:
> 12 + 34
>
> Based on:
>
http://wiki.tcl.tk/795
>
> I wrote:
> # Save the original one so we can chain to it
> rename unknown _original_unknown
>
> # Provide our own implementation
> proc unknown args {
> puts stderr "WARNING: unknown command: $args"
> expr $args
> uplevel 1 [list _original_unknown {*}$args]
> }
>
> But this results in:
> WARNING: unknown command: history event 0
> invalid bareword "history"
> in expression "history event 0";
> should be "$history" or "{history}" or "history(...)" or ...
>
> How could I get it working?
I'll skip past all the naysayers, noting that nobody seems to
have directly answered the question.
You performed ALL THREE actions on every invalid command:
report error, evaluate expr, execute (original) [unknown].
Obviously you should be selective in which action you attempt.
When you get something that works as an expression, use that,
else report the command error. Remember you will still get
actual command errors passing through!
Try this:
rename unknown _original_unknown
proc unknown args {
if {
[regexp {^\s*(\w+)\s*=\s*(.*)$} [join $args " "] -- _v _e] == 1 && \
[catch {uplevel 1 set $_v "\[expr $_e\]"} _r] == 0
} {
return $_r
} elseif {
[catch {uplevel 1 [list expr [join $args " "]] } _r] == 0
} {
return $_r
} else {
uplevel 1 [list _original_unknown {*}$args]
}
}
% 5-2
3
% x= 5-2
3
% set x
3
% foobar baz
invalid command name "foobar"
%
BUT be warned that this simple hack does not catch all syntactic
variations, AND it has naming collisions between commands, variables,
and math functions.
--
Donald Arseneau
as...@triumf.ca