| home | contents | previous | next page | send comment | send link | add bookmark |

2. Data and declarations

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.

identifiers

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.

variable declarations

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.

constant declarations

Declaration of a constant uses this form:

const name : type specification := expression
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.

literal constants

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:

-9.954                  % fixed point notation
 7.43e-4                % scientific notation
These forms of real numbers are invalid:
.95                     % no digit before decimal point
9.                      % no digit after decimal point
A literal integer is written as a sequence of digits. Here also, a unary operator can precede the first digit:
123
-46
A literal string is a sequence of characters between a pair of quotation marks:
"The rain in Spain falls mainly on the plain."
A literal character is a single character between a pair of apostrophes:
't'

numerical declarations

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:

const i : int := 0              % an integer
const pi : real := 3.14159      % a real number
A variable number does not need to be initialized when declared; but can be:
var s : real                    % a real number
var i, j, k : int               % integers
var a, b : real := 1.0          % both are initialized

boolean declarations

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

character declarations

Characters are individual text characters. They can be declared as follows:

const one : char := '1'
var input : char

string declarations

Strings are a sequence of text characters. A string is declared using this type specification:

string[(integer expression)]
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:
const s : string := "Hello"     % length is 5
var name : string(32)           % length is 32
var message : string            % length is default
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.

backslash characters

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)

type declarations

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:

type name : type specification
in which type specification can be one of the standard types supported by the T language:
int
real
boolean
char
string[(integer expression)]
or a data type you define in your program using one of these:
enum
array
record
union
which are explained below.

enumerated data type

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:
type color : enum(red, yellow, green)
var light : color := color.green
Note that enumerated items are identified using the dot operator.
name.item

array data type

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:
var A : array(2, 2) of real
valid identifiers for A are:
A(0, 0)   A(0, 1)
A(1, 0)   A(1, 1)

record data type

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:
name.item
where name is the identifier of a constant or a variable. Example:
var point : record 
            x, y, z : real
            end record

r := sqrt( point.x^2 + point.y^2 + point.z^2 )

union data type

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

summary

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.


| home | contents | previous | next page | send comment | send link | add bookmark |

Copyright © 2004, Stephen R. Schmitt