Contents | Prev | Next | The T Language Specification Spring 2005 |
CHAPTER 7
The sequence of execution of a T program is controlled by a sequence of statements, which are executed for their effect and do not have values.
Some statements contain other statements as part of their structure; such other statements are substatements of the statement. In the same manner, some statements contain expressions (§8) as part of their structure.
Sequences of statements are organized into blocks. There are two primary blocks defined for a T program: the main block, which is explained in section (§7.1), and statement blocks which are explained in section (§7.2).
Statements that will be familiar to C and C++ programmers are the
block (§7.3), empty (§7.4),
if
(§7.5), while
(§7.6),
return
(§7.7), and delete
(§7.8) statements.
A program shall contain a global construct called main
(the main block), which is the designated start of the execution
of a program. Exactly one main block must exist for every T program.
The block is entered when program execution starts.
Program execution continues until a return
statement in
the main block is executed or the end of the main block is reached
(or a run-time error is encountered).
If the end of the main block is reached, then the result is as if
the program executed a return of 0.
The declaration of a main block includes the definition of a return
type, which must be int
.
The return value exists to provide a way to pass a single status value
to the surrounding environment.
All implementations of the main block will have the following definition:
MainFunctionDeclaration:int
main()
{
MainBlockStatementsopt}
T does not allow for arguments to be passed into the main block.
The main block can exist at the beginning of the source file, at the end of the source file or in between any two class definitions within the source file.
Variable declarations can be provided at the outermost level of the main block (not at an inner block). The scope of a variable declared in the main block is from its declaration point to the end of the main block.
There are many kinds of statements in the T programming language. Most correspond to statements in the C and C++ languages. Statements are given by the following grammar:
Statement: Block EmptyStatement (7.4) ExpressionStatement (7.5) IfThenElseStatement (7.6) WhileStatement (7.7) ReturnStatement (7.8) DeleteExpression (7.9)
A block is a sequence of statements within braces.
Block:{
BlockStatementsopt}
BlockStatements: BlockStatement BlockStatements BlockStatement BlockStatement: Statement
A block is executed by executing each of the statements in order from
first to last (left to right).
It is possible for a block to terminate early through a return
statement.
An empty statement does nothing.
EmptyStatement:
;
Certain kinds of expressions may be used as statements by following them with semicolons:
ExpressionStatement: StatementExpression ; StatementExpression: Assignment MethodInvocation
An expression statement is executed by evaluating the expression; if the expression has a value, the value is discarded.
if-then-else
Statement
The if-then-else
statement allows a conditional choice of two
statements, executing one or the other but not both.
IfThenElseStatement:if
(
Expression)
Statementelse
Statement
The Expression must have type int, or a compile-time error occurs.
An if-then-else
statement is executed by first evaluating the
Expression.
Execution continues by making a choice based on the resulting value:
else
keyword) is executed.
else
keyword) is executed.
while
Statement
The while
statement executes an Expression and
a Statement repeatedly until the value of the Expression is 0.
WhileStatement:while
(
Expression)
Statement
The Expression must have type int, or a compile-time error occurs.
A while
statement is executed by first evaluating the
Expression.
Execution continues by making a choice based on the resulting value:
return
statement.)
If the value of the Expression is 0 the first time it is evaluated, then the Statement is not executed.
return
Statement
A return
statement returns control to the invoker of a method
(§5.4, §8.10),
returns control to the invoker of a constructor (§5.5, §8.7),
returns control to the invoker of a destructor (§5.6, §7.9),
or terminates the main block (§7.1),
and is given by the following grammar:
ReturnStatement:return
expression;
return
;
Use of a return
without an expression shall only be
used within a constructor or destructor.
If a return
statement is contained within a method,
the value of the Expression becomes the value of the
method invocation. More precisely, execution of such a return
statement first evaluates the Expression.
The value produced by the Expression is communicated to the invoker.
A return
statement with no Expression is not allowed
in this context and will result in a compile-time error.
return
statement is contained within the main block,
the value of the Expression becomes the value of the
program. More precisely, execution of such a return
statement first evaluates the Expression.
The value produced by the Expression is communicated to the
surrounding execution environment.
A return
statement with no Expression is not allowed
in this context and will result in a compile-time error.
It is possible to return
from the middle of a while
or if-then-else
block.
A compile-time error occurs if the type of the return expression is
not convertible by assignment conversion to the return type of the
enclosing method, or
to int
if the return statement is in the main block.
A run-time error occurs if the return type is an array or reference type and the run-time type of the return expression is not assignable to that type. That is, for reference types, the run-time type of the return expression must be the same type as the return type, or it must be a subclass of that type. For array types,
int
; or
Object
.
delete
StatementThe DeleteExpression operator destroys a class or array object.
DeleteExpression:
delete
Expression
The Expression must denote a reference to a reference type.
If not, a compile-time error is produced.
A delete
that attempts to access a null reference will result in a run-time error. The use of a reference (by a delete
or in any other context) that has been deleted will result in undefined behavior.
The delete
statement first invokes the destructor for the
class of the object being deleted.
Destructors automatically call the destructor of its class' immediate
superclass, except for the destructor for the primordial Object
class.
The memory for the object is reclaimed after all superclass
destructors have completed.
out
Statement
The out
statement is a rudimentary mechanism for printing integers and it provides the only way to generate output in the T programming language.
Its syntax is described by the following grammar:
OutputStatement:out
Expression;
If the Expression has type int
then the out
statement will print to stdout the ASCII equivalent of the integer
Expression, followed by a newline (ASCII LF
) character.
All ASCII equivalents of integer expressions will be represented in decimal
with no leading 0's and a single leading "-
" if the integer is
negative.
It is a compile-time error for the Expression to have a type other
than int
.
Contents | Prev | Next | The T Language Specification Spring 2005 |
Author(s): Brian Mehlman (§7.1-7.5, §7.8-7.9), and Yu Li (§7.6-7.7)