📄 language.tcl.html
字号:
<!-- Copyright (C) 2003 Red Hat, Inc. --><!-- This material may be distributed only subject to the terms --><!-- and conditions set forth in the Open Publication License, v1.0 --><!-- or later (the latest version is presently available at --><!-- http://www.opencontent.org/openpub/). --><!-- Distribution of the work or derivative of the work in any --><!-- standard (paper) book form is prohibited unless prior --><!-- permission is obtained from the copyright holder. --><HTML><HEAD><TITLE>An Introduction to Tcl</TITLE><meta name="MSSmartTagsPreventParsing" content="TRUE"><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="The eCos Component Writer's Guide"HREF="cdl-guide.html"><LINKREL="UP"TITLE="The CDL Language"HREF="language.html"><LINKREL="PREVIOUS"TITLE="Option Naming Convention"HREF="language.naming.html"><LINKREL="NEXT"TITLE="Values and Expressions"HREF="language.values.html"></HEAD><BODYCLASS="SECT1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">The <SPANCLASS="APPLICATION">eCos</SPAN> Component Writer's Guide</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="language.naming.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 3. The CDL Language</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="language.values.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="LANGUAGE.TCL">An Introduction to Tcl</H1><P>All <SPANCLASS="APPLICATION">CDL</SPAN> scripts are implemented as <SPANCLASS="APPLICATION">Tcl</SPAN> scripts, and are read in byrunning the data through a standard <SPANCLASS="APPLICATION">Tcl</SPAN> interpreter, extended with asmall number of additional commands such as<TTCLASS="LITERAL">cdl_option</TT> and <TTCLASS="LITERAL">cdl_component</TT>.Often it is not necessary to know the full details of <SPANCLASS="APPLICATION">Tcl</SPAN> syntax.Instead it is possible to copy an existing script, perform some copyand paste operations, and make appropriate changes to names and tovarious properties. However there are also cases where anunderstanding of <SPANCLASS="APPLICATION">Tcl</SPAN> syntax is very desirable, for example:</P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING">cdl_option CYGDAT_UITRON_MEMPOOLFIXED_EXTERNS { display "Externs for initialization" flavor data default_value {"static char fpool1[ 2000 ], \\\n\ fpool2[ 2000 ], \\\n\ fpool3[ 2000 ];"} …}</PRE></TD></TR></TABLE><P>This causes the <TTCLASS="LITERAL">cdl_option</TT> command to be executed, which in turnevaluates its body in a recursive invocation of the <SPANCLASS="APPLICATION">Tcl</SPAN> interpreter.When the <SPANCLASS="PROPERTY">default_value</SPAN> property is encountered the braces around thevalue part are processed by the interpreter, stopping it from doingfurther processing of the braced contents (except for backslashprocessing at the end of a line, that is special). In particular itprevents command substitution for<TTCLASS="LITERAL">[ 2000 ]</TT>. A single argument will bepassed to the <SPANCLASS="PROPERTY">default_value</SPAN> command which expects a <SPANCLASS="APPLICATION">CDL</SPAN>expression, so the expression parsing code is passed the following:</P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="SCREEN">"static char fpool1[ 2000 ], \\\n fpool2[ 2000 ], \\\n fpool3[ 2000 ];"</PRE></TD></TR></TABLE><P>The <SPANCLASS="APPLICATION">CDL</SPAN> expression parsing code will treat this as a simple stringconstant, as opposed to a more complicated expression involving otheroptions and various operators. The string parsing code will performthe usual backslash substitutions so the actual default value will be:</P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="SCREEN">static char fpool1[ 2000 ], \ fpool2[ 2000 ], \ fpool3[ 2000 ];</PRE></TD></TR></TABLE><P>If the user does not modify the option's value then the followingwill be generated in the appropriate configuration header file:</P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING">#define CYGDAT_UITRON_MEMPOOLFIXED_EXTERNS static char fpool1[ 2000 ], \ fpool2[ 2000 ], \ fpool3[ 2000 ];</PRE></TD></TR></TABLE><P>Getting this desired result usually requires an understanding of both<SPANCLASS="APPLICATION">Tcl</SPAN> syntax and <SPANCLASS="APPLICATION">CDL</SPAN> expression syntax. Sometimes it is possible tosubstitute a certain amount of trial and error instead, but this mayprove frustrating. It is also worth pointing out that many <SPANCLASS="APPLICATION">CDL</SPAN>scripts do not involve this level of complexity. On the other hand,some of the more advanced features of the <SPANCLASS="APPLICATION">CDL</SPAN> language involvefragments of <SPANCLASS="APPLICATION">Tcl</SPAN> code, for example the <SPANCLASS="PROPERTY">define_proc</SPAN> property. Touse these component writers will need to know about the full <SPANCLASS="APPLICATION">Tcl</SPAN>language as well as the syntax.</P><P>Although the current example may seem to suggest that <SPANCLASS="APPLICATION">Tcl</SPAN> is rathercomplicated, it is actually a very simple yet powerful scriptinglanguage: the syntax is defined by just eleven rules. On occasion thissimplicity means that Tcl's behavior is subtly different from otherlanguages, which can confuse newcomers.</P><P>When the Tcl interpreter is passed some data such as<TTCLASS="LITERAL">puts Hello</TT>, it splits this data into a commandand its arguments. The command will be terminated by a newline or by asemicolon, unless one of the quoting mechanisms is used. The commandand each of its arguments are separated by white space. So in thefollowing example:</P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="SCREEN">puts Helloset x 42</PRE></TD></TR></TABLE><P>This will result in two separate commands being executed. The firstcommand is <TTCLASS="LITERAL">puts</TT> and is passed a single argument,<TTCLASS="LITERAL">Hello</TT>. The second command is <TTCLASS="LITERAL">set</TT>and is passed two arguments, <TTCLASS="LITERAL">x</TT> and<TTCLASS="LITERAL">42</TT>. The intervening newline character serves toterminate the first command, and a semi-colon separator could be usedinstead: </P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="SCREEN">puts Hello;set x 42</PRE></TD></TR></TABLE><P>Any white space surrounding the semicolon is just ignored because itdoes not serve to separate arguments.</P><P>Now consider the following:</P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="SCREEN">set x Hello world</PRE></TD></TR></TABLE><P>This is not valid <SPANCLASS="APPLICATION">Tcl</SPAN>. It is an attempt to invoke the<TTCLASS="LITERAL">set</TT> command with three arguments:<TTCLASS="LITERAL">x</TT>, <TTCLASS="LITERAL">Hello</TT>, and<TTCLASS="LITERAL">world</TT>. The <TTCLASS="LITERAL">set</TT> only takes twoarguments, a variable name and a value, so it is necessary to combinethe data into a single argument by quoting:</P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="SCREEN">set x "Hello world"</PRE></TD></TR></TABLE><P>When the <SPANCLASS="APPLICATION">Tcl</SPAN> interpreter encounters the first quote character ittreats all subsequent data up to but not including the closing quoteas part of the current argument. The quote marks are removed by theinterpreter, so the second argument passed to the<TTCLASS="LITERAL">set</TT> command is just <TTCLASS="LITERAL">Hello world</TT>without the quote characters. This can be significant in the contextof <SPANCLASS="APPLICATION">CDL</SPAN> scripts. For example:</P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING">cdl_option CYG_HAL_STARTUP { … default_value "RAM"}</PRE></TD></TR></TABLE><P>The <SPANCLASS="APPLICATION">Tcl</SPAN> interpreter strips off the quote marks so the <SPANCLASS="APPLICATION">CDL</SPAN>expression parsing code sees <TTCLASS="LITERAL">RAM</TT> instead of<TTCLASS="LITERAL">"RAM"</TT>. It will treat this as a reference tosome unknown option <TTCLASS="VARNAME">RAM</TT> rather than as a stringconstant, and the expression evaluation code will use a value of<TTCLASS="LITERAL">0</TT> when it encounters an option that is notcurrently loaded. Therefore the option<TTCLASS="VARNAME">CYG_HAL_STARTUP</TT> ends up with a default value of<TTCLASS="LITERAL">0</TT>. Either braces or backslashes should be used toavoid this, for example<TTCLASS="LITERAL">default_value { "RAM" }</TT>. </P><DIVCLASS="NOTE"><BLOCKQUOTECLASS="NOTE"><P><B>Note: </B>There are long-term plans to implement some sort of <SPANCLASS="APPLICATION">CDL</SPAN> validationutility <SPANCLASS="APPLICATION">cdllint</SPAN> whichcould catch common errors like this one.</P></BLOCKQUOTE></DIV><P>A quoted argument continues until the closing quote character is
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -