Sabtu, 24 November 2007

Language constructs 1

Pascal, in its original form, is a purely procedural language and includes the traditional array of Algol-like control structures with reserved words such as if, then, else, while, for, and so on. However, pascal also has many data structuring facilities and other abstractions which were not included in the original Algol60, like type definitions, records, pointers, enumerations, and sets. Such constructs were in part inherited or inspired from Simula67, Algol68, and Niklaus Wirth's own AlgolW.

[edit] Hello world

Pascal programs start with the program keyword with a list of external file descriptors as parameters; then follows the main statement block encapsulated by the begin and end keywords. Semicolons separate statements, and the full stop ends the whole program (or unit). Letter case is ignored in Pascal source. Some compilers, Turbo Pascal among them, have made the program keyword optional.

Here is an example of the source code in use for a very simple program:

program HelloWorld(output);
writeln('Hello, World!')

[edit] Data structures

Pascal's simple (atomic) types are real, integer, character, boolean and enumerations, a new type constructor introduced with Pascal:

r: Real;
i: Integer;
c: Char;
b: Boolean;
e: (apple, pear, banana, orange, lemon);

Subranges of any ordinal type (any simple type except real) can be made:

x: 1..10;
y: 'a'..'z';

In contrast with other programming languages Pascal supports set type:

set1: set of 1..10;
set2: set of 'a'..'z';
set3: set of;

A set is fundamental concept for modern mathematics, and many algorithms are defined with sets usage. So, an implementation of such algorithm is very suitable for Pascal. Also, set's operations may be realized faster. For example, for many Pascal compilers:

if i in [5..10] then

is faster, than

if (i>4) and (i<11) then

Types can be defined from other types using type declarations:

x = Integer;
y = x;

Further, complex types can be constructed from simple types:

a = Array [1..10] of Integer;
b = record
x: Integer;
y: Char
c = File of a;

As shown in the example above, Pascal files are sequences of components. Every file has a buffer variable which is denoted by f^. The procedures get (for reading) and put (for writing) move the buffer variable to the next element. Read is introduced such that read(f, x) is the same as x:=f^; get(f);. Write is introduced such that write(f, x) is the same as f^ := x; put(f); The type text is predefined as file of char. While the buffer variable could be used to inspect the next character that would be used (check for a digit before reading an integer), this concept lead to serious problems with interactive programs with early implementations, but was solved later with the "lazy I/O" concept.

In Jensen & Wirth Pascal, strings are represented as packed arrays of chars; they therefore have fixed length and are usually space-padded. Some dialects have a custom string type.

My Headlines