Version 6 of Are there any newbie guides to the Tcl source?

Updated 2008-08-04 19:44:54 by Cameron

This question has been raised repeatedly. In early August 2008, Neil Madden responded: "It still changes quite a bit from release to release. Various bits are documented to some extent on the wiki (http://wiki.tcl.tk/ ) -- search for 'Category Internals'.

The actual source code layout is fairly straightforward -- most C code is in generic/ with platform-specific stuff in win/ unix/ and macosx/. The library/ folder contains parts of the implementation that are Tcl scripts (e.g. parts of clock), and the init.tcl which is loaded into new interpreters. And so on.

The best place to start if you are new to the source is to get used to how Tcl extensions are written: how Tcl represents values (Tcl_Obj structure, and Tcl_ObjType), how commands are invoked (Tcl_ObjCmdProc), and so on. Several Tcl books cover this, and the wiki has plenty of information on this. In the source, you can look at sample commands such as Tcl_IncrObjCmd in generic/tclCmdIL.c, or e.g. look at how lists are implemented in generic/tclListObj.c. Details of how [ open "|..."] is handled is platform-specific, so e.g. for unix systems this is unix/tclUnixPipe.c where TclpCreateProcess does a fork()/execvp() sequence.

If you want to get the heart of the interpreter, then look at generic/tclBasic.c, and in particular at the implementation of TclEvalObjvInternal (TEOVI as it is affectionately known). I'd advise grabbing a version of the source code from early 8.5 or even 8.4 branch, as TEOVI in HEAD (8.6) is rather more complex due to recent enhancements (indeed, it seems to have disappeared now, so look at Tcl_EvalObjv). After that, there is the byte-compiler in tclComp*.{h,c}, namespaces in tclNamesp.c, etc."