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

📄 language.tcl.html

📁 ecos源代码包
💻 HTML
📖 第 1 页 / 共 2 页
字号:
encountered, which means that it can span multiple lines. Newline or
semicolon characters do not terminate the current command in such
cases. <SPAN
CLASS="PROPERTY"
>description</SPAN
> properties usually make use of this:</P
><TABLE
BORDER="5"
BGCOLOR="#E0E0F0"
WIDTH="70%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>cdl_package CYGPKG_ERROR {
    description   "
        This package contains the common list of error and
        status codes. It is held centrally to allow
        packages to interchange error codes and status
        codes in a common way, rather than each package
        having its own conventions for error/status
        reporting. The error codes are modelled on the
        POSIX style naming e.g. EINVAL etc. This package
        also provides the standard strerror() function to
        convert error codes to textual representation."
    &#8230;
}</PRE
></TD
></TR
></TABLE
><P
>The <SPAN
CLASS="APPLICATION"
>Tcl</SPAN
> interpreter supports much the same forms of backslash
substitution as other common programming languages. Some backslash
sequences such as <TT
CLASS="LITERAL"
>\n</TT
> will be replaced by the
appropriate character. The sequence <TT
CLASS="LITERAL"
>\\</TT
> will be
replaced by a single backslash. A backslash at the very end of a line
will cause that backslash, the newline character, and any white space
at the start of the next line to be replaced by a single space. Hence
the following two Tcl commands are equivalent:</P
><TABLE
BORDER="5"
BGCOLOR="#E0E0F0"
WIDTH="70%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>puts  "Hello\nworld\n"
puts \
"Hello
world
"</PRE
></TD
></TR
></TABLE
><P
>If a <SPAN
CLASS="PROPERTY"
>description</SPAN
> string needs to contain quote marks or other
special characters then backslash escapes can be used. In addition to
quote and backslash characters, the Tcl interpreter treats square
brackets, the <TT
CLASS="LITERAL"
>$</TT
> character, and braces specially.
Square brackets are used for command substitution, for example:</P
><TABLE
BORDER="5"
BGCOLOR="#E0E0F0"
WIDTH="70%"
><TR
><TD
><PRE
CLASS="SCREEN"
>puts "The answer is [expr 6 * 9]"</PRE
></TD
></TR
></TABLE
><P
>When the Tcl interpreter encounters the square brackets it will treat
the contents as another command that should be executed first, and the
result of executing that is used when continuing to process the
script. In this case the Tcl interpreter will execute the command
<TT
CLASS="LITERAL"
>expr 6 * 9</TT
>, yielding a result of 42
<A
NAME="AEN1270"
HREF="#FTN.AEN1270"
>[1]</A
>
and then the
Tcl interpreter will execute <TT
CLASS="LITERAL"
>puts "The answer is 42"</TT
>.
It should be noted that the interpreter performs only one level
of substitution: if the result of performing command substitution
performs further special characters such as square brackets then these
will not be treated specially.</P
><P
>Command substitution will not prove useful for many <SPAN
CLASS="APPLICATION"
>CDL</SPAN
> scripts,
except for e.g. a <SPAN
CLASS="PROPERTY"
>define_proc</SPAN
> property which involves a fragment of
<SPAN
CLASS="APPLICATION"
>Tcl</SPAN
> code. Potentially there are some interesting uses, for example
to internationalize <SPAN
CLASS="PROPERTY"
>display</SPAN
> strings. However care does have to be
taken to avoid unexpected command substitution, for example if an
option description involves square brackets then typically these would
require backslash-escapes.</P
><P
>The <TT
CLASS="LITERAL"
>$</TT
> character is used in Tcl scripts to perform
variable substitution:</P
><TABLE
BORDER="5"
BGCOLOR="#E0E0F0"
WIDTH="70%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>set x [expr 6 * 9]
puts "The answer is $x"</PRE
></TD
></TR
></TABLE
><P
>Variable substitution, like command substitution, is unlikely to
prove useful for many <SPAN
CLASS="APPLICATION"
>CDL</SPAN
> scripts except in the context of
<SPAN
CLASS="APPLICATION"
>Tcl</SPAN
> fragments. If it is necessary to have a <TT
CLASS="LITERAL"
>$</TT
>
character then a backslash escape may have to be used.</P
><P
>Braces are used to collect a sequence of characters into a single
argument, just like quotes. The difference is that variable, command
and backslash substitution do not occur inside braces (with the
sole exception of backslash substitution at the end of a line).
Therefore given a line in a <SPAN
CLASS="APPLICATION"
>CDL</SPAN
> script such as:</P
><TABLE
BORDER="5"
BGCOLOR="#E0E0F0"
WIDTH="70%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>default_value {"RAM"}</PRE
></TD
></TR
></TABLE
><P
>The braces are stripped off by the <SPAN
CLASS="APPLICATION"
>Tcl</SPAN
> interpreter, leaving
<TT
CLASS="LITERAL"
>"RAM"</TT
> which will be handled as a string constant by
the expression parsing code. The same effect could be achieved using
one of the following:</P
><TABLE
BORDER="5"
BGCOLOR="#E0E0F0"
WIDTH="70%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>default_value \"RAM\"
default_value "\"RAM\""</PRE
></TD
></TR
></TABLE
><P
>Generally the use of braces is less confusing. At this stage it is
worth noting that the basic format of <SPAN
CLASS="APPLICATION"
>CDL</SPAN
> data makes use of
braces:</P
><TABLE
BORDER="5"
BGCOLOR="#E0E0F0"
WIDTH="70%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>cdl_option &lt;name&gt; {
     &#8230;
};</PRE
></TD
></TR
></TABLE
><P
>The <TT
CLASS="LITERAL"
>cdl_option</TT
> command is passed two arguments, a name and a body,
where the body consists of everything inside the braces but not the
braces themselves. This body can then be executed in a recursive
invocation of the <SPAN
CLASS="APPLICATION"
>Tcl</SPAN
> interpreter. If a <SPAN
CLASS="APPLICATION"
>CDL</SPAN
> script contains
mismatched braces then the interpreter is likely to get rather
confused and the resulting diagnostics may be difficult to understand. </P
><P
>Comments in Tcl scripts are introduced by a hash character
<TT
CLASS="LITERAL"
>#</TT
>. However, a hash character only introduces a
comment if it occurs where a command is expected. Consider the
following:</P
><TABLE
BORDER="5"
BGCOLOR="#E0E0F0"
WIDTH="70%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
># This is a comment
puts "Hello" # world</PRE
></TD
></TR
></TABLE
><P
>The first line is a valid comment, since the hash character occurs
right at the start where a command name is expected. The second line
does not contain a comment. Instead it is an attempt to invoke the
<TT
CLASS="LITERAL"
>puts</TT
> command with three arguments:
<TT
CLASS="LITERAL"
>Hello</TT
>, <TT
CLASS="LITERAL"
>#</TT
> and
<TT
CLASS="LITERAL"
>world</TT
>. These are not valid arguments for the
<TT
CLASS="LITERAL"
>puts</TT
> command so an error will be raised.
If the second line was rewritten as:</P
><TABLE
BORDER="5"
BGCOLOR="#E0E0F0"
WIDTH="70%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>puts "Hello"; # world</PRE
></TD
></TR
></TABLE
><P
>then this is a valid Tcl script. The semicolon identifies the end of
the current command, so the hash character occurs at a point where the
next command would start and hence it is interpreted as the start of a
comment.</P
><P
>This handling of comments can lead to subtle behavior. Consider the
following:</P
><TABLE
BORDER="5"
BGCOLOR="#E0E0F0"
WIDTH="70%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>cdl_option WHATEVER {
# This is a comment }
    default_value 0
    &#8230;
}</PRE
></TD
></TR
></TABLE
><P
>Consider the way the Tcl interpreter processes this. The command name
and the first argument do not pose any special difficulties. The
opening brace is interpreted as the start of the next argument, which
continues until a closing brace is encountered. In this case the
closing brace occurs on the second line, so the second argument passed
to <TT
CLASS="LITERAL"
>cdl_option</TT
> is
<TT
CLASS="LITERAL"
>\n&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;This&nbsp;is&nbsp;a&nbsp;comment</TT
>. This second argument is processed in a recursive
invocation of the Tcl interpreter and does not contain any commands,
just a comment. Top-level script processing then resumes, and the next
command that is encountered is <TT
CLASS="LITERAL"
>default_value</TT
>. Since
the parser is not currently processing a configuration option this is
an error. Later on the Tcl interpreter would encounter a closing brace
by itself, which is also an error.</P
><P
>For component writers who need more information about <SPAN
CLASS="APPLICATION"
>Tcl</SPAN
>,
especially about the language rather than the syntax, various
resources are available. A reasonable starting point is the
<A
HREF="http://www.tcl.tk/scripting/"
TARGET="_top"
>Scriptics developer
web site</A
>.</P
></DIV
><H3
CLASS="FOOTNOTES"
>Notes</H3
><TABLE
BORDER="0"
CLASS="FOOTNOTES"
WIDTH="100%"
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN1270"
HREF="language.tcl.html#AEN1270"
>[1]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>It is possible that some versions of the Tcl interpreter will instead
produce a result of 54 when asked to multiply six by nine. Appropriate
<A
HREF="http://www.douglasadams.com/creations/hhgg.html"
TARGET="_top"
>reference
documentation</A
> should be consulted for more information on why
42 is in fact the correct answer.</P
></TD
></TR
></TABLE
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="language.naming.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="cdl-guide.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="language.values.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Option Naming Convention</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="language.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Values and Expressions</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -