views:

482

answers:

5

Often when reading about Tcl (e.g. http://antirez.com/articoli/tclmisunderstood.html) you read that "everything is a command". Sometimes you also hear how other languages are, like Tcl, are "command languages."

To me with my background in other languages, I just view these "commands" as statements. What precisely is the difference between a command and a statement?

+1  A: 

I'd say that a command is what you execute in a statement; different statements may execute the same command (with different arguments, typically). The command is the operation; a statement is a specific invocation of the operation.

Jonathan Leffler
A: 

I would think that a command is an instruction in code, and a statement may run several commands but at the end evaluates to true or false.

karl.r
What you say doesn't make sense in the context of the question, which is specifically asking about Tcl. Statements in Tcl don't necessarily evaluate to true or false.
Bryan Oakley
+3  A: 

A command is what other languages call a function, routine or reserved word, and can be defined by the "proc" command or in C or whatever. A statement is an invocation of a command. Using traditional definitions, a statement in Tcl is a command followed by zero or more arguments.

Consider the following code:

1   proc hello {string} {
2      puts "hello, $string"
3   }
4   hello "world"

Line 1 defines a command named "hello", line 4 is a statement that calls the "hello" command.

True enough, some articles on Tcl use the term "command" to mean both the name of a command and the invocation of the command. I wouldn't get too hung up on that. Think of statements as the invocation of a command and you'll be fine.

When you see the phrase "everything is a command", it means is that there are no reserved words. Things that look like language syntax or keywords -- if, return, exit, break, while and so on... -- are all commands. They can all be treated alike: they can be renamed, they can be removed, they can be re-implemented, etc.

Bryan Oakley
IMO a function call (with or without arguments) is a command. So, line 4 is both a statement and a command.
Nick D
nitpick alert: `else` is not a command. It's an argument to `if`
glenn jackman
@glenn: thanks. I fixed that in my answer.
Bryan Oakley
+7  A: 

Traditionally, in Tcl, the phrase "everything is a command" means that there's no such thing as a "reserved word" command, or one that is defined by the system that you can't change. Every single executable piece of Tcl code is of the format:

command ?arg1? ... ?argN?

There's no such thing as a command that's part of the syntax and can't be overwritten (like "if" and other control structures in most languages). It's entirely possible to redefine the "if" command to do something slightly different.

For example, you could redefine "while" as:

proc while {expression body} {
    for {} {[uplevel 1 expr $expression]} {} {
        uplevel 1 $body
    }
}

The above code being untested... but it shows the general idea.

RHSeeger
I think I like this answer better than mine.
Bryan Oakley
+1  A: 

I guess this is mainly a question of semantics, so there may be some variation in the understanding of these concepts. That said, this Wikipedia article provides some guidance that is in keeping with my intuition on the topic.

A statement is a unit of an imperative program. Specifically, it is the unit that is terminated by the statement terminator. In C that's a semi-colon. Or, in Pascal and its descendants, it's the unit that's separated by the statement separator. I think in most flavours of Pascal that's also a semi-colon.

A command is an action, such as a function call or a keyword that performs an action. The Wikipedia article likens them to verbs, and I think that's a good description.

So, a variable declaration is a statement, but probably not a command. And a variable assignment via an assignment operator might be considered a command by some and not by others. One sometimes hears it referred to as an assignment command, but not often. If it looks like a function call, as in TCL, then it's more 'command-like', since there's an explicit verb set.

Also, statements may consist of several commands. For example, think about several function calls in C joined with commas. I would consider that one statement, since it has one semi-colon and returns one value. However, each of the separate calls could be considered a command.

The other thing to bear in mind when considering the statement/command terminology is that statements typically refer to imperative programs, while commands typically refer to shells and scripts. So, when using a command shell like bash, one speaks of commands. If I write a bash script, then it usually thought of as a script of commands, rather than a program of statements, even though the difference is largely academic.

TCL, as one of the early scripting languages, probably wanted to draw a distinction between itself as an interpreted scripting language running in a shell (wish), versus compiled languages like C and Pascal. Perl, for example, having come to popularity somewhat later, typically doesn't harp on the distinction.

However, you still often hear people refer to Perl scripts, rather than Perl programs, and likewise for TCL.

I fear that my answer may have done nothing to clarify the distinction, but I hope it helps.

jbourque