The T language supports two categories of data, constants and variables. Constants and variables must be declared before they are used. This is done with a declaration statement.
Every constant and variable you declare must be identified with a name. T is case sensitive, a variable named sum is not the same variable as one named Sum. The maximum length of an identifier is 64 characters. An identifier can be made from letters, digits, and the underscore character _ but must start with a letter.
Declarations of variables must have the following form:
var name{, name} : type specification [:= expression]
Each name in the list is declared with the same type specification and is
optionally initialized to the same expression value.
Declaration of a constant uses this form:
The syntax of a constant declaration is similar to that of a variable declaration; however, only one name at a time is declared. Note that a constant must be initialized when it is declared.const name : type specification := expression
A literal real number, that is, one written into your source code, begins and ends with a digit and must contain a decimal point. A unary operator, + or -, can optionally precede the first digit. The following forms are valid:
These forms of real numbers are invalid:-9.954 % fixed point notation 7.43e-4 % scientific notation
A literal integer is written as a sequence of digits. Here also, a unary operator can precede the first digit:.95 % no digit before decimal point 9. % no digit after decimal point
A literal string is a sequence of characters between a pair of quotation marks:123 -46
"The rain in Spain falls mainly on the plain."A literal character is a single character between a pair of apostrophes:
't'
Only two types of numerical data are available in the T language. They are integers and real numbers. A table showing the ranges of values allowed for numerical data is included in the appendices at the end of this manual.
A constant number is declared as follows:
A variable number does not need to be initialized when declared; but can be:const i : int := 0 % an integer const pi : real := 3.14159 % a real number
var s : real % a real number var i, j, k : int % integers var a, b : real := 1.0 % both are initialized
These are similar to numerical declarations. The range of values is limited to true or false. The following declarations are valid:
var flag : boolean var result, done : boolean := false
Characters are individual text characters. They can be declared as follows:
const one : char := '1' var input : char
Strings are a sequence of text characters. A string is declared using this type specification:
where the optional integer expression is used to define the string's length. The default length is the maximum string length given in Section 10. The following are valid string declarations:string[(integer expression)]
The end of a string is marked by a null byte. The T interpreter appends this marker automatically in many of its functions. If a program you write inserts individual characters into a string, you could inadvertently overwrite the end character with unpredictable results.const s : string := "Hello" % length is 5 var name : string(32) % length is 32 var message : string % length is default
The T interpreter supports the use of several non-text characters in strings and as character data:
\" embedded quote \' embedded apostrophe \\ embedded backslash \b or \B back space \f or \F form feed \n or \N carriage return, line feed pair \t or \T tab \0 null (end of string character)
A type declaration creates an identifier or name for a data type which you may use elsewhere in a program to declare a variable or a constant with name as the type specification. The declaration of a data type takes this form:
in which type specification can be one of the standard types supported by the T language:type name : type specification
or a data type you define in your program using one of these:int real boolean char string[(integer expression)]
which are explained below.enum array record union
An enumeration type specification is declared using the key word enum with the syntax:
type name : enum(item{, item})
The items are valued sequentially and increasing. Example:
Note that enumerated items are identified using the dot operator.type color : enum(red, yellow, green) var light : color := color.green
name.item
An array type specification is declared using the key words array and of with the syntax:
array(index{, index}) of type specification
Where each index must be an integer expression. Array indices are zero
based. Example, for:
valid identifiers for A are:var A : array(2, 2) of real
A(0, 0) A(0, 1) A(1, 0) A(1, 1)
A record type specification is declared using the key words record and end with the syntax:
record
item{, item} : type specification
{item{, item}} : type specification
end record
A record item is identified using the dot operator:
where name is the identifier of a constant or a variable. Example:name.item
var point : record
x, y, z : real
end record
r := sqrt( point.x^2 + point.y^2 + point.z^2 )
A union type specification is declared using the key words union and end with the syntax:
union
item{, item} : type specification
{item{, item}} : type specification
end union
Unlike a record, the items in a union occupy the same memory location.
Your program must keep track of the current type of data stored in a
union. Unpredictable results can occur if you access data in a union
incorrectly. Like a record, a union item is also identified using the
dot operator:
name.item
This section showed you how to declare all of T's data types. Complex data structures can be created in the T programming language by combining arrays, records, and unions.