TCL评论:为什么要解释评论? [英] Tcl comments: why interpret comments?

查看:346
本文介绍了TCL评论:为什么要解释评论?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Tclers Wiki页面的"Dodeklogue" 中,提到了有关注释的信息:

In Tclers wiki page, at 'Dodeklogue' it is mentioned about comments:

评论: 如果在预期的命令位置出现#,则该行的其余部分为注释.没有尝试执行命令,并且没有字符 解释行,除了终止换行符可能是 以\进行转义,表示注释继续 下一行.

Comments: If # appears where a command is expected, the rest of the line is a comment. No command execution is attempted, and no characters in the line are interpreted, except that the terminating newline may be escaped with \, signifying that the comment continues on the subsequent line.

但是,似乎注释是在终端\上解释的:例如,让文件test.tcl的内容如下:

However, it seems that comments are interpreted byond the terminal \: for example, let the content of file test.tcl be as below:

proc test {} {
    # Open brace {
    puts "I am fine"
}

test

t

然后 tclsh test.tcl给出以下错误消息:

Then tclsh test.tcl gives the following error message:

missing close-brace: possible unbalanced brace in comment
    while executing
"proc test {} {"
    (file "hello.tcl" line 1)
Even more interesting

更有趣的是,当用大括号}代替大括号{时,错误消息完全不同.

Even more interesting, when the open brace { is replaced with close brace }, the error message is completely different.

为什么Tcl解释器试图理解注释中的内容,如果Tcl解释器(或一般意义上的任何解释器)被设计为将注释作为真实注释,那么我们将会失去什么:一旦您完全看到#忽略直到换行(除非检查注释的最后一个字符是否为\)?

Why does Tcl interpreter try to make sense of what is there in a comment, what would we lose if the Tcl interpretor (or any interpretor in general) was designed to take comments as real comments: once you see # completely ignore until the new line (except, check last character of comment if it is \) ?

推荐答案

Tcl与许多其他语言不同,它与其余语法同时处理注释.这意味着,因为它首先遇到了{(作为proc命令调用的一部分),所以它专注于匹配括号.

Tcl, unlike a number of other languages, processes comments at the same time as the rest of its syntax. This means that, because it came across a { first (as part of the proc command invocation) it is focusing on matching up the braces. It only comprehends a # as a comment when it is evaluating the procedure (i.e., you call the command defined by the proc command).

这些将用作注释:

proc commentDemonstration {} {
    puts "A"
    # if [exit] {
        puts "B"
    # } else [exit]
    puts "C"
}
# Call it and see, _no_ early exit
commentDemonstration

它们是真正的评论.只是您必须在过程定义中平衡花括号(或用反斜杠将其括起来)(除非您非常生气,无法在双引号或其他内容中定义过程主体; 不要那样做 (出于您自己的理智)),与您使用的评论无关.在大多数情况下,您不会注意到平衡要求,但这是在其中很重要的一种情况.

They are genuinely comments. It's just that you must balance (or backslash-quote) the braces in a procedure definition (unless you're mad enough to define a procedure body inside double quotes or something; don't do that for your own sanity's sake) independently of what comments you use. Most of the time you don't notice the balancing requirement, but this is one case where it matters.

能够将#放在类似的内容中,这是在Tcl中嵌入其他语言的关键.例如,Critcl允许将C源代码嵌入到Tcl中,而#意味着在C中完全不同.

Being able to put # inside things like that is a key to embedding other languages inside Tcl. For example, Critcl allows C source code to be embedded within Tcl, and # means something totally different in C.

这篇关于TCL评论:为什么要解释评论?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆