Re: Tcl C extn - Scoping in extended cmds - how to?
- From: "MartinLemburg@UGS" <martin.lemburg.ugs@xxxxxxx>
- Date: Thu, 28 Jun 2007 03:46:16 -0700
Hi Arijit,
so far as I know, no tcl command (C function registered as tcl
command) creates a call-stack-scope on its execution.
So having a tcl command "foo" called in the procedure "faa" won't
create a new scope, so that it may access all local variables inside
the procedure "faa".
So a solution may be to have a pure tcl "framework" with such
procedures like "block", "register", "fields", "bits", etc..
This procedures then call a matching tcl command (e.g. ::myExt::Block)
in their scope, so that "upvar" still from the tcl command at the
procedure level to access variables outside the procedure.
Take a look at the twapi extension.
There are "raw" or low level tcl commands representing the WIN32 API
and then there are tcl procedures using this tcl commands for a high
level API on top of the WIN32 API.
The same I try to do, when encapsulation a CAD C(++) API to be used
with tcl.
---
Not really knowing what you are achieving with your extension and what
it is used for the source snippet you presented ...
set var 1
block b {
register r {
field f {
bits 8
}
}
}
.... does not look for my eyes as a candiate for "scoping"! It looks
like another kind of control structure, that evaluates "script-bodies"
in the scope of the caller (here where the variable "var" is known)!
You wrote, that you want to use "upvar" etc., but is this really
necessary and needed?
I don't really understand why you want this!
As example take "nested" control structures like ...
% set asciis [list]
% foreach cmd [info commands] {
foreach char [split $cmd {}] {
lappend asciis [format {0x%02X} [scan $char {%c}]];
}
}
.... or a "selfmade" control structure ...
% set counter 1
% do {
puts $counter; incr counter;
} while {$counter <= 10}
A control structure does not deal with scopes in its bodies or
conditions! It's not needed, not necessary and would create more
complex code!
Just simply declare such commands, like "fields" to work always in the
scope of the caller, so that the call by look like this:
set var 1
field f {
echo $var
bits 8
}
This would prevent probably needless complexity and sources for
errors!
Best regards,
Martin
On Jun 28, 10:56 am, ariji...@xxxxxxxxx wrote:
Hi,
I am using Tcl C extension to create extended commands like this:
set var 1
block b {
register r {
field f {
bits 8
}
}
}
Where "block", "register", "field", "bits", etc are my extended
commands.
Now, when I do command extensions like this using
"Tcl_CreateCommand(interp, ...)", the scope actually remains the same
i.e. inside the "field" in the above example, I am actually able to
access $var. In other words, the following becomes possible.
set var 1
field f {
echo $var //This works fine...In Tcl proc scoping semantics,
it is not allowed unless upvar or global is used.
bits 8
}
But I actually want my commands to have Tcl proc semantics as far as
scoping is concerned, so that the above code becomes invalid and the
following (pure Tcl way) should only be allowed.
set var 1
field f {
upvar $var local_var
echo $local_var
bits 8
}
So, I just wanted to know if there is some way by which I can
configure my extended Tcl commands to follow the "proc" semantics as
far as scoping is concerned.
Thanks for your time and willingness to help,
Arijit
.
- Follow-Ups:
- Re: Tcl C extn - Scoping in extended cmds - how to?
- From: Alexandre Ferrieux
- Re: Tcl C extn - Scoping in extended cmds - how to?
- From: Jonathan Bromley
- Re: Tcl C extn - Scoping in extended cmds - how to?
- From: arijit79
- Re: Tcl C extn - Scoping in extended cmds - how to?
- References:
- Tcl C extn - Scoping in extended cmds - how to?
- From: arijit79
- Tcl C extn - Scoping in extended cmds - how to?
- Prev by Date: Re: Daily Build
- Next by Date: Re: Daily Build
- Previous by thread: Re: Tcl C extn - Scoping in extended cmds - how to?
- Next by thread: Re: Tcl C extn - Scoping in extended cmds - how to?
- Index(es):
Relevant Pages
|