Re: TCL source code



ndxp@xxxxxxxxxxx wrote:
> Hello
>
> I have been playing around with TCL source code for a couple
> of days. So far I have not made much sense of it. Can somebody
> tell me where can I start from ?
>
> -tony

It me again, and this time I should remember to warn you, I am still
learning Tcl, and I am not a professional programmer.

The best way to understand something, is to decompose into its
components
and to decompose the components you don't understand furthur down.

I think the best way to understand Tcl is to decompose it, into it's
components
each components is more or less a set of rules that describes its

Tcl is a command line interface.

You can say that interface is a language, but I think it's better to
think of it as a command line interface first, a language second, or
a language in an abstract sense.

<b>First component, How Tcl parses a command?</b>
Each command much have the form of

cmdName Arg1 Arg2 ...

A command is one word, one Tcl word, also each argument is one word.
Tcl by default seperate words using spaces (I dont know if this is
configurable or not)

A word may include spaces, by following a set of grouping rules.

To parse a command Tcl first perform grouping, than performs command
substitution (in Tcl 8.5 you can use the keyword {expand} to turn
around this, or to break this sequence rule), this rule is very very
important, as it explains a lot.

After doing so, Tcl passed the args to the command named cmdName.

A command name can only be one word (would it a good idea to extend tcl
to support multiple word command names, I dont know!)

If a command name doesn't exist, tcl asks the command called "unknow"
about it, else if a command called unknow is not defined, Tclraise an
error. This is alo a Tcl rule that shouldn't be forgotten.

All args passed to cmdName are strings, thus the saying in Tcl
everything is a string! Don't let name passing or file handlers confuse
you, Tcl is passing the name which is a string, but this string, this
name means something usefull to either Tcl if its a command name (Tcl
only name commands, Tcl doesn't really the concept of variable, I will
elaborate on this later), or this string may mean something usefull to
a service provided by a command, like set, or the set fo channel
handling services (a.k.a commands) (open, socket, close, etc... (int
Tcl 8.5 they will be unified under the chan ensemble)). But the point
is regardless of this, its just a string.

Then Tcl returns the command result.

<b>Second Component, How does Tcl parse a script?</b>

A script is a sequence of commands Tcl execute them one after the other

Commands are terminated with new line or a semicolon ';'

A command may span multiple line, accorind to the word grouping or
string escaping

Each command must have a unique name, this is why, Tcl doesn't support
anonymous comands, Tcl takes the names and somehow find it's code,
there is no inline command representation, Tcl doesn't subsitute the
command name with its code. How, you can create scripts and pass them
to commands as arguments

A Tcl script is sequencial, Tcl doens't understand flow control, flow
control is implemented by commands that take anonymous scripts as their
arguments

So how does command integrate, i.e. work together?

There is only one way to do so, command substituion, a command can send
it's result as the value or part of value, or an argument passed to the
command it wants to tell something, with Tcl 8.5 {expand} a command
might be able to send multiple arguments to another command!

So how does Tcl do with out variable?

The set command or service (I prefer the word service), set assiciate
one string value to another

>> set systems wannabeTclHacker
>> set systems
wannabeTclHacker

I like analogies, you can think ot set as the RAM or virtually memory
interface for a Tcl script.

Note that set in the one that store and retrive the value,
which mean, in the previous example, the set service makes sense of the
name
systems
But Tcl doesn't. Cause set didn't create a command called systems.
This examples why command names and proclaimed var names dont conflict,
cause actually Tcl doesn't do vars, set does!!!

so within a script you can do things like this to pass values between
commands

>> set tempVar [cmd1 arg2 arg2]
....
>> cmd2 arg1 [set tempVar]
....

This way cmd2 received the output of cmd1 as one of it's arguments
[cmd arg1 arg1]
is the syntax that initiate command substitution

since set is very important for the procedural programmer, Tcl have
syntax sugar for set
so instead of doing [set tempVar] you can do $tempVar, this doesn't
mean Tcl knows about tempVar

So how do you create a command really?

For now I know you can do so using a C lib, and the proc command
(service)
proc is a tcl service that create command

There are other more sophisticade Tclish service that create commands
like XOTcl and [incr tcl]

But for more usages proc is very good.

There is probably a lot more to Tcl but I am still learning, I mainly
made this post to sum up what I know about Tcl and get guidance if I
misunderstood stuff.

Tcl is fun to learn, because it makes it easy to have a fundamental
understand of how things work, unlike other langs, there is magic, if
you wonna know the internals, you can.

.



Relevant Pages

  • TIP #185: Null Handling
    ... nulls, and command modifications for manipulating them. ... Tcl deals with strings, the universal medium for representing data. ... is know and it is an empty string, but if a respondent forgets to give ...
    (comp.lang.tcl)
  • All commands are equal, but some are more equal than others.
    ... just finished implementing a HTTP webcrawler using the http package ... be able to scramble a string. ... Very surprised AH starts searching the Tcl wiki to find out what might ... % <command to add subcommands to namespace ensemble> string scramble ...
    (comp.lang.tcl)
  • ANN: AOLserver 4.5.0 released!
    ... On behalf of the AOLserver Team, I have the honor of announcing the ... AOLserver 4.5.0 is a major upgrade including several new Tcl commands, ... AOLserver is America Online's Open-Source web server. ... the "ns_zlib" command for compressing and uncompressing ...
    (comp.lang.tcl)
  • Re: Why doesnt foreach return a value
    ... Every Tcl command is implemented as a C level function. ... What we want at the script level is the ... What changes occur is very specific to the context. ...
    (comp.lang.tcl)
  • Re: No argv, argc for worker thread
    ... I expected that a Tcl interpreter started in any manner would accept options ... This is how the interpreter works with command line options ... a "main" script processes command line ...
    (comp.lang.tcl)