⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 language.tcl.html

📁 ecos源代码包
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!-- 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">
<META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
"><LINK
REL="HOME"
TITLE="The eCos Component Writer's Guide"
HREF="cdl-guide.html"><LINK
REL="UP"
TITLE="The CDL Language"
HREF="language.html"><LINK
REL="PREVIOUS"
TITLE="Option Naming Convention"
HREF="language.naming.html"><LINK
REL="NEXT"
TITLE="Values and Expressions"
HREF="language.values.html"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>The <SPAN
CLASS="APPLICATION"
>eCos</SPAN
> Component Writer's Guide</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="language.naming.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 3. The CDL Language</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="language.values.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="LANGUAGE.TCL">An Introduction to Tcl</H1
><P
>All <SPAN
CLASS="APPLICATION"
>CDL</SPAN
> scripts are implemented as <SPAN
CLASS="APPLICATION"
>Tcl</SPAN
> scripts, and are read in by
running the data through a standard <SPAN
CLASS="APPLICATION"
>Tcl</SPAN
> interpreter, extended with a
small number of additional commands such as
<TT
CLASS="LITERAL"
>cdl_option</TT
> and <TT
CLASS="LITERAL"
>cdl_component</TT
>.
Often it is not necessary to know the full details of <SPAN
CLASS="APPLICATION"
>Tcl</SPAN
> syntax.
Instead it is possible to copy an existing script, perform some copy
and paste operations, and make appropriate changes to names and to
various properties. However there are also cases where an
understanding of <SPAN
CLASS="APPLICATION"
>Tcl</SPAN
> syntax is very desirable, for example:</P
><TABLE
BORDER="5"
BGCOLOR="#E0E0F0"
WIDTH="70%"
><TR
><TD
><PRE
CLASS="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 ];"}
    &#8230;
}</PRE
></TD
></TR
></TABLE
><P
>This causes the <TT
CLASS="LITERAL"
>cdl_option</TT
> command to be executed, which in turn
evaluates its body in a recursive invocation of the <SPAN
CLASS="APPLICATION"
>Tcl</SPAN
> interpreter.
When the <SPAN
CLASS="PROPERTY"
>default_value</SPAN
> property is encountered the braces around the
value part are processed by the interpreter, stopping it from doing
further processing of the braced contents (except for backslash
processing at the end of a line, that is special). In particular it
prevents command substitution for
<TT
CLASS="LITERAL"
>[&nbsp;2000&nbsp;]</TT
>. A single argument will be
passed to the <SPAN
CLASS="PROPERTY"
>default_value</SPAN
> command which expects a <SPAN
CLASS="APPLICATION"
>CDL</SPAN
>
expression, so the expression parsing code is passed the following:</P
><TABLE
BORDER="5"
BGCOLOR="#E0E0F0"
WIDTH="70%"
><TR
><TD
><PRE
CLASS="SCREEN"
>"static char fpool1[ 2000 ], \\\n fpool2[ 2000 ], \\\n fpool3[ 2000 ];"</PRE
></TD
></TR
></TABLE
><P
>The <SPAN
CLASS="APPLICATION"
>CDL</SPAN
> expression parsing code will treat this as a simple string
constant, as opposed to a more complicated expression involving other
options and various operators. The string parsing code will perform
the usual backslash substitutions so the actual default value will be:</P
><TABLE
BORDER="5"
BGCOLOR="#E0E0F0"
WIDTH="70%"
><TR
><TD
><PRE
CLASS="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 following
will be generated in the appropriate configuration header file:</P
><TABLE
BORDER="5"
BGCOLOR="#E0E0F0"
WIDTH="70%"
><TR
><TD
><PRE
CLASS="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
<SPAN
CLASS="APPLICATION"
>Tcl</SPAN
> syntax and <SPAN
CLASS="APPLICATION"
>CDL</SPAN
> expression syntax. Sometimes it is possible to
substitute a certain amount of trial and error instead, but this may
prove frustrating. It is also worth pointing out that many <SPAN
CLASS="APPLICATION"
>CDL</SPAN
>
scripts do not involve this level of complexity. On the other hand,
some of the more advanced features of the <SPAN
CLASS="APPLICATION"
>CDL</SPAN
> language involve
fragments of <SPAN
CLASS="APPLICATION"
>Tcl</SPAN
> code, for example the <SPAN
CLASS="PROPERTY"
>define_proc</SPAN
> property. To
use these component writers will need to know about the full <SPAN
CLASS="APPLICATION"
>Tcl</SPAN
>
language as well as the syntax.</P
><P
>Although the current example may seem to suggest that <SPAN
CLASS="APPLICATION"
>Tcl</SPAN
> is rather
complicated, it is actually a very simple yet powerful scripting
language: the syntax is defined by just eleven rules. On occasion this
simplicity means that Tcl's behavior is subtly different from other
languages, which can confuse newcomers.</P
><P
>When the Tcl interpreter is passed some data such as
<TT
CLASS="LITERAL"
>puts&nbsp;Hello</TT
>, it splits this data into a command
and its arguments. The command will be terminated by a newline or by a
semicolon, unless one of the quoting mechanisms is used. The command
and each of its arguments are separated by white space. So in the
following example:</P
><TABLE
BORDER="5"
BGCOLOR="#E0E0F0"
WIDTH="70%"
><TR
><TD
><PRE
CLASS="SCREEN"
>puts Hello
set x 42</PRE
></TD
></TR
></TABLE
><P
>This will result in two separate commands being executed. The first
command is <TT
CLASS="LITERAL"
>puts</TT
> and is passed a single argument,
<TT
CLASS="LITERAL"
>Hello</TT
>. The second command is <TT
CLASS="LITERAL"
>set</TT
>
and is passed two arguments, <TT
CLASS="LITERAL"
>x</TT
> and
<TT
CLASS="LITERAL"
>42</TT
>. The intervening newline character serves to
terminate the first command, and a semi-colon separator could be used
instead: </P
><TABLE
BORDER="5"
BGCOLOR="#E0E0F0"
WIDTH="70%"
><TR
><TD
><PRE
CLASS="SCREEN"
>puts Hello;set x 42</PRE
></TD
></TR
></TABLE
><P
>Any white space surrounding the semicolon is just ignored because it
does not serve to separate arguments.</P
><P
>Now consider the following:</P
><TABLE
BORDER="5"
BGCOLOR="#E0E0F0"
WIDTH="70%"
><TR
><TD
><PRE
CLASS="SCREEN"
>set x Hello world</PRE
></TD
></TR
></TABLE
><P
>This is not valid <SPAN
CLASS="APPLICATION"
>Tcl</SPAN
>. It is an attempt to invoke the
<TT
CLASS="LITERAL"
>set</TT
> command with three arguments:
<TT
CLASS="LITERAL"
>x</TT
>, <TT
CLASS="LITERAL"
>Hello</TT
>, and
<TT
CLASS="LITERAL"
>world</TT
>. The <TT
CLASS="LITERAL"
>set</TT
> only takes two
arguments, a variable name and a value, so it is necessary to combine
the data into a single argument by quoting:</P
><TABLE
BORDER="5"
BGCOLOR="#E0E0F0"
WIDTH="70%"
><TR
><TD
><PRE
CLASS="SCREEN"
>set x "Hello world"</PRE
></TD
></TR
></TABLE
><P
>When the <SPAN
CLASS="APPLICATION"
>Tcl</SPAN
> interpreter encounters the first quote character it
treats all subsequent data up to but not including the closing quote
as part of the current argument. The quote marks are removed by the
interpreter, so the second argument passed to the
<TT
CLASS="LITERAL"
>set</TT
> command is just <TT
CLASS="LITERAL"
>Hello world</TT
>
without the quote characters. This can be significant in the context
of <SPAN
CLASS="APPLICATION"
>CDL</SPAN
> scripts. For example:</P
><TABLE
BORDER="5"
BGCOLOR="#E0E0F0"
WIDTH="70%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>cdl_option CYG_HAL_STARTUP {
    &#8230;
    default_value "RAM"
}</PRE
></TD
></TR
></TABLE
><P
>The <SPAN
CLASS="APPLICATION"
>Tcl</SPAN
> interpreter strips off the quote marks so the <SPAN
CLASS="APPLICATION"
>CDL</SPAN
>
expression parsing code sees <TT
CLASS="LITERAL"
>RAM</TT
> instead of
<TT
CLASS="LITERAL"
>"RAM"</TT
>. It will treat this as a reference to
some unknown option <TT
CLASS="VARNAME"
>RAM</TT
> rather than as a string
constant, and the expression evaluation code will use a value of
<TT
CLASS="LITERAL"
>0</TT
> when it encounters an option that is not
currently loaded. Therefore the option
<TT
CLASS="VARNAME"
>CYG_HAL_STARTUP</TT
> ends up with a default value of
<TT
CLASS="LITERAL"
>0</TT
>. Either braces or backslashes should be used to
avoid this, for example
<TT
CLASS="LITERAL"
>default_value&nbsp;{&nbsp;"RAM"&nbsp;}</TT
>. </P
><DIV
CLASS="NOTE"
><BLOCKQUOTE
CLASS="NOTE"
><P
><B
>Note: </B
>There are long-term plans to implement some sort of <SPAN
CLASS="APPLICATION"
>CDL</SPAN
> validation
utility <SPAN
CLASS="APPLICATION"
>cdllint</SPAN
> which
could 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 + -