Table of Contents

5.4.5.8 Print Statement

Note

This section describes the implementation of MS-VBAL §5.4.5.8 Print Statement.

Syntax

Form AST node Instruction kind(s) Notes
Print # PrintStatementNode Simple The file-number form. Token: Print.
Print PrintStatementNode Simple The bare form, with no file number (FileNumber is null): Print "x". See below.
Owner.Print ObjectPrintExpressionNode None The object-qualified form: a call of the Print method of Owner, the callee of a call statement. See Runtime Semantics.
Debug.Print DebugPrintStatementNode Simple The object-qualified form whose owner is Debug.

Debug is written only as this statement and Debug.Assert: any other use of the name is a syntax error, as it is in MS-VBA (see RD-VBAL §5.4.2.23 Assert Statement).

An output list is a list of expressions, so in Debug.Print x = 0 and Owner.Print x = 0 the output list is the comparison x = 0: neither is a Let statement, whose target is never the Print of an object with an output list (RD-VBAL §5.4.3.8 Let Statement).

The output list these nodes carry is described in 5.4.5.8.1 Output Lists below. See RD-VBAL §3.4.3 File Statements.

The bare Print

Whether there is a bare Print at all is the language's to say (SupportedLanguage.HasBarePrint):

Language A Print with no file number
RD-VBA Not a statement. Print is a reserved identifier of VBA, illegal as a name and with no semantics, and it is no recognized statement inside a procedure. (Only the Immediate window accepts it, as Debug.Print; that is not procedure scope.) It is VBC09331, the same as any other name the language does not declare.
VB6 Undefined: it is the Print member of the form or report it is written in, and the platform hosts no forms.
BASIC A statement. It writes to the session's own output - the one Debug.Print writes to - by the same output rules (5.4.5.8.1).

In BASIC it differs from Debug.Print in one way: it is not a debug statement, and is lowered in every release build, including one that drops Debug.Print, which then leaves no instruction at all (RD-VBAL §3.5.1 InstructionList). It is what an interactive shell - which is written in BASIC - writes its output with: the shell's ? shorthand expands to it.

Instruction-list lowering is told the language the body is written in (InstructionLoweringOptions.Language, from the environment's IRuntimeEnvironmentProfile.Language) and reports the undefined form; a body lowered for no language is lowered as written.

Note

Not implemented. StatementStaticSemanticsEvaluator does not report it, having no language to ask: the language server has no live caller of it yet. A document module (a form, a report) with a Print member of its own is not yet the target of a bare Print written in it.

The ? shorthand has no lexer or grammar token: the shell expands it before the line is parsed.

Static Semantics

Note

Reserved. This section has no content yet.

Runtime Semantics

A number or a date in an output list is let-coerced to String, as MS-VBAL §5.4.5.8.1 says, in the regional settings of the session's environment (RD-VBAL §5.5.1.2.4 Let-coercion to and from String). A number is followed by a space, and preceded by one where its sign would be: a negative number's sign takes that place, so 1 is printed " 1 " and -1 is printed "-1 ", as in MS-VBA (recorded in Excel). The Runtime Semantics of MS-VBAL §5.4.5.8 insert a space before every number, so the specification prints -1 as " -1 ".

Every form of Print is a text channel: a String is written as its whole characters, and a trailing byte is not written (RD-VBAL §2.4.1.2 VBStringType).

An object is printed as what it stands for, its default member's value, as the value it is (7 for an Excel range that holds 7). As in MS-VBA (recorded in Excel), for Print # and Debug.Print alike, Nothing is error 91 and an object whose default member takes an argument (a Collection) error 450.

Owner.Print

Owner.Print calls the Print method of the object, once its output list is evaluated in source order: an item that raises an error raises it before the object is called. The objects that have one are a Visual Basic 6.0 form or PictureBox and an Access form or report. The platform hosts none of them:

Owner Error
An object VBR00438: the object has no Print method.
Nothing VBR00091.
A value that is not an object VBR00424.
Note

Not implemented. .Print in a With block, the Print method of the block's object, is a syntax error: the grammar has no .Print.

Implementation

Print # writes through the channel's character output surface, IFileChannelOutput, which it shares with Write #. Width # sets that surface's maximum line length (RD-VBAL §5.4.5 File Statements).

5.4.5.8.1 Output Lists

Note

This section describes the implementation of MS-VBAL §5.4.5.8.1 Output Lists.

PrintStatementNode and ObjectPrintExpressionNode share one output-list shape:

Node Represents
PrintOutputItemNode An output item: a value and/or a ; or , separator.
PrintSpcClauseNode A Spc clause.
PrintTabClauseNode A Tab clause.
Tip

In a parsed output list, a value and the ; or , after it are always two separate PrintOutputItemNodes: one value-only, one separator-only. The grammar reads them so: an output item and a separator are two rules of their own. A consumer of Items should walk the list as it is rather than assume (value, separator) pairs: two separators can follow each other (1;;2), and two values with a space between them have none (1 2).

Note

RD-VBAL diverges from MS-VBAL in what may stand between two output items.

MS-VBAL §5.4.5.8.1 reads output-list = *output-item, so an output item may follow another with nothing between them: Debug.Print 1(2) is the two items 1 and (2). The VBE does not keep such a line as it is written: it lists it again with a ; between the items (Debug.Print 1; (2)), in every form of the statement, Write # included.

RD-VBA requires a space, a ; or a , between two output items, and reports two items written side by side as VBC00061, at the second one, once for each statement. A name right before parentheses is not two items: in Debug.Print F(2), F is called with the argument 2, as in MS-VBA.

Nor is a name, spaces, then parentheses. MS-VBAL §3.3 makes the spaces before a token part of it, so Debug.Print F (2) reads both as the index expression F(2) and as the two items F and (2). MS-VBA reads the index expression, and the VBE writes the line Debug.Print F(2). RD-VBA reads it as MS-VBA does, in an output item, an operand of one (Debug.Print x + F (2)) and the arguments of a call within one (Debug.Print F (G (1))), as in any expression (RD-VBAL §5.6.13 Index Expressions). A value that is no name stays two items: Debug.Print 1 (2) prints 1, then 2.

See RD-VBAL §3.4.3 File Statements.