Comments: Difference between revisions

From Rosetta Code
Content added Content deleted
m (TCL -> Tcl)
Line 103: Line 103:
Standard Forth includes a number of ways to add comment text. As with everything in Forth, comment characters are actually words that control the compiler.
Standard Forth includes a number of ways to add comment text. As with everything in Forth, comment characters are actually words that control the compiler.
\ The backslash skips everything else on the line
\ The backslash skips everything else on the line
( The left paren skips everything up to the next right paren)
( The left paren skips everything up to the next right paren on the same line)


Traditionally, the paren comments are used for "stack effect" notation:
Traditionally, the paren comments are used for "stack effect" notation:

Revision as of 04:56, 2 June 2007

Task
Comments
You are encouraged to solve this task according to the task description, using any language you may know.

Demonstrate all ways to include text in a language source file which is completely ignored by the compiler or interpreter.

Ada

-- All Ada comments begin with "--" and extend to the end of the line


BASIC

100 REM Standard BASIC comments begin with "REM" (remark) and extend to the end of the line

Brainf***

This is a comment

*Note*: Although most ASCII characters may be used for comments, extra care much be used when using punctuation, particularly the comma or period. These are I/O operators and are actual commands rather than comments, and are instead compiled into the program if used and may have to be "debugged" and removed if you forget this issue.

C++

C++ has several ways to comment out some text. The first one is the traditional C-style comment:

/* This is a comment */

The C-style comment starts at the /*, and ends at the */. A C-style comment may be used between any tokens. It cannot be used inside tokens, that is, given the code

struct charisma {};
void f(char/* comment */isma) {}

the function takes an argument of type char, named isma, not an unnamed argument of type charisma.

C-style comments cannot be nested; that is, if you write

/* some comment /* trying to nest some other comment */ inside */

the comment ends at the first */, and inside */ is again interpreted as C++ source code (almost certainly causing a compile error).

The second way to comment text in C++ are so-called C++-style comments

// This is a comment

C++-style start with // and reach up to, but not including, the end of line (more exactly, up to the next unescaped newline). While formally, C++-style comments cannot be nested either, in practice they can:

// This is a valid comment // with a "nested" comment

That's because starting with the first // everything in the line is ignored, including the second //. The fact that the newline is not part of the comment is important for multi-line macro defnitions. It means that in the code

#define FOO \
  (macro text) // comment
  (no more macro text)

the line (no more macro text) is not part of the macro definition. Also escaping the line break at the end of the comment with '\' doesn't help, because that would make the third line part of the comment instead. Comments inside macros therefore have to be C-style.

Finally, conditional compilation also can be used to make the compiler ignore some text:

#if 0
While technically not a comment, this is also ignored by the compiler
#endif

The trick is that 0 is always false, therefore the text between #if 0 and #endif is never compiled. Note however that while the compiler doesn't see that text, the preprocessor does. Therefore some minimal rules still have to be followed. For example, the following code is not valid:

#if 0
This isn't valid.
#endif

That's because the preprocessor will interpret the apostrophe as beginning of a character constant, and will complain because that character constant isn't terminated with another apostrophe.

While this should never be used for actual comments, it's an easy way to comment out some code, especially because it doesn't interfere with normal (documentation) comments. Note that the problem mentioned above cannot occur if there's valid code between the #if 0 and #endif.

Conditional compile "comments" can be nested:

#if 0
This is not compiled.
#if 0
Nor is this.
#endif
And this still is not compiled.
#endif

Clean

Clean comments are similar to C++.

Start = /* This is a multi-
           line comment     */ 17 // This is a single-line comment 

In contrast to C++ comments can be nested.

Start = /* This is a comment /* Nested comment */ still a comment */ 17

Common Lisp

; This is a single-line comment.
(defun foo ()
  "This function has a documentation string."
  (do-something))

Documentation strings are optional and may span multiple lines as string literals do. In addition to defun, many other constructs may also include documentation strings. Among these are defmacro, defmethod, defstruct, defclass, defvar, defparameter, and defconstant.

E

# This is a regular comment.
? "This is an Updoc comment, which
> is an executable example or test case.".split(" ")
# value: ["This", "is", "an", "Updoc", "comment,", "which
#        is", "an", "executable", "example", "or", "test", "case."]

Erlang

% Erlang comments begin with "%" and extend to the end of the line.

Forth

Standard Forth includes a number of ways to add comment text. As with everything in Forth, comment characters are actually words that control the compiler.

\ The backslash skips everything else on the line
( The left paren skips everything up to the next right paren on the same line)

Traditionally, the paren comments are used for "stack effect" notation:

: myword ( a b -- c )  ...

This comment means "myword takes two cells on the stack and leaves one". Sometimes, stack effect comment names give clues about the word's function:

: add'em ( a b -- a+b )   + ;
: strlen ( addr -- len )   count nip ;

Some Forth systems implement other commenting words, such as these words from Win32Forth:

\s skips all remaining text in the file
(( skips until the next double-paren, 
   stretching across multiple lines ))
comment:
   Ignore all text in this section
comment;
doc
   Another comment block
enddoc
/* C-style comment */
(* Pascal-style comment *)

Fortran

Compiler: ANSI F77 or compatible (like g77 -strict)

The first six columns in Fortran are traditionally reserved for labels and certain special characters. In particular a letter in the first column indicates a comment. This can be any character but is usually a "C" by convention:

C     This would be some kind of comment
C     Usually one would avoid columns 2-6 even in a comment.

Some Fortran compilers have the extension that comments starting with D are treated as non-comments if a special debugging flag is given at the compiler invocation. For example:

C     If compiled in debugging mode, print the current value of I
D     PRINT *, I

IDL

The comment character in IDL is the semicolon - everything starting with it and to the end of the line is a comment. Like this:

; The following computes the factorial of a number "n"
fact = product(indgen( n )+1) ; where n should be an integer

Java

Java has two ways to enter comments.

/* This is a comment */

This C-style comment starts with /* and ends with */. The two delimiters may be on the same or separate lines. This style comment may be used anywhere white space is permitted.

// This is a comment

This C++-style comment starts with // and extends to the end of line.

JavaScript

JavaScript has two ways to enter comments.

/* This is a comment */

This C-style comment starts with /* and ends with */. The two delimiters may be on the same or separate lines. This style comment may be used anywhere white space is permitted.

// This is a comment

This C++-style comment starts with // and extends to the end of line.

LaTeX

In LaTeX, comments look like this:

% This is a comment

LaTeX comments start with % and continue up to and including the line break. The fact that the line break itself is commented out as well makes it useful for adding line breaks in the source code of complex macros without LaTeX interpreting them (which may cause extra space or even a paragraph break in the resulting typeset text). For example, the following results in the one word "understandable":

\newcommand{\firstpart}{understand}
\newcommand{\secondpart}{able}
\newcommand{\complete}{%
\firstpart%
\secondpart}

\complete

Without the percent sign after \firstpart, it would have been the two words "understand able".

Perl

Interpreter: Perl 5.x

Perl officially only has single line comments

# this is commented

These may also be at the end of a line

my $var = 1; # this is the comment part

The following is a hack which to some degree simulate multi-line comments

<<COMMENT;
Here are my comments
this is multi-line
COMMENT

While this is not stripped out at compile time, making it an unwise choice to use for leaving comments in a program, you can easily use this to disable a block of code and prevent it from executing during the development and testing stages.

Pop11

Pop11 has two kinds of comments: endline and C-like. Endline comment begins with tree consequitive semicolons and ends at the end of line:

;;; This is a comment

C-like comments may be multiline:

/* First line
   Second line */

C-like comments (unlike C) may be nested:

 /* This is a comment /* containing nested comment */ */

One can also use conditional compilation to comment out sections of code

#_IF false
some code 
#_ENDIF

however, commented out part must consist of valid Pop11 tokens. In particular, C-like comments must balance and strings must be terminated. The following is an error:

#_IF false
This w'ont work
#_ENDIF

because apostrophe starts an unterminated string.

Tcl

Tcl follows the usual scripting language rules: a comment starts at a "#" symbol, which can be placed after a command if that is terminated by a semicolon:

# comment on a line by itself. The next is a command by itself:
set var1 $value1
set var2 $value2 ; # comment that follows a line of code

Toka

Toka provides a number of ways to add comment text. As with everything in Toka, comment characters are actually words which control the compiler.

 #! The backslash skips everything else on the line
 ( The left paren skips everything up to the next right paren)

The paren comments are normally used for "stack effect" notation:

 [ ( a b -- c ) ... ] is myword

This comment means "myword takes two cells on the stack and leaves one". Sometimes, stack effect comment names give clues about the word's function:

 [ ( a b -- a+b ) + ] is add'em