Friday, September 28, 2007

Pascal

Language constructs

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.

A syntactical comparison with C

Syntactically, Pascal is distinguished from languages in the C family by being much more Algol-like. English keywords are retained where C uses punctuation symbols — pascal has and, or, and mod where C uses &&, ||, and % for example. However, C is actually more Algol-like than Pascal regarding (simple) declarations, retaining the type-name variable-name syntax which Pascal abandoned to allow for (easily read) complex type expressions and a better perceived clarity in educational situations.

Another, more subtle, difference is the role of the semicolon. In Pascal semicolons separate individual statements within a compound statement whereas, in C, they are syntactically part of the statement itself (transforming an expression into a statement). This difference manifests itself primarily in two situations:

  • there can never be a semicolon directly before else in Pascal whereas it is mandatory in C (unless a block statement is used)
  • the last statement before an end is not required to be followed by a semicolon

Some programmers nevertheless put a semicolon on the last line before end, thereby formally inserting an empty statement. This is discouraged by some educators, worried that it may confuse students' perception of the formal role of the semicolon.

Hello world

Pascal programs start with the program keyword, an optional list of external file descriptors and then a statement block is indicated with the begin and end keywords. Semicolons separate statements, and the full stop ends the 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);
begin
writeln('Hello, World!');
end.

Data structures

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

var
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:
var
x: 1..10;
y: 'a'..'z';
z: pear..orange;

Types can be defined from other types using type declarations:

type
x = Integer;
y = x;
...

Further, complex types can be constructed from simple types:

type
a = Array [1..10] of Integer;
b = record
a: Integer;
b: Char
end;
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 leads to serious problems with interactive programs. Pascal 6000 and VAX Pascal had incompatible solutions for this problem and most other implementations omit the get and put procedures.

In Jensen & Wirth Pascal, strings are represented as packed arrays of chars; they therefore have fixed length and are usually space-padded. Later dialects commonly add a string type where the length of the contents can vary up to a declared maximum length. These are usually implemented by a length byte (limiting the maximum length to 255) and a fixed array of payload characters, and are therefore space-inefficient if the maximal length is seldom used in practice.

Pointers

Pascal supports the use of pointers:

type
a = ^b;
b = record
a: Integer;
b: Char;
c: a
end;
var
pointer_to_b: a;

Here the variable pointer_to_b is a pointer to the data type b, a record. To create a new record and assign the values 10 and A to the fields a and b in the record, the commands would be;

  new(pointer_to_b);
pointer_to_b^.a := 10;
pointer_to_b^.b := 'A';
pointer_to_b^.c := nil;
...

This could also be done using the with statement, as follows

new(pointer_to_b);
with pointer_to_b^ do
begin
a := 10;
b := 'A';
c := nil
end;
...

Note that inside of the scope of the with statement, the compiler knows that a and b refer to the subfields of the record pointer pointer_to_b and not to the record b or the pointer type a.

Linked lists, stacks and queues can be created by including a pointer type field (c) in the record (see also nil and null (computer)).

Control structures

Pascal is a structured programming language, meaning that the flow of control is structured into standard statements, ideally without 'go to' commands.

while a <> b do writeln('Waiting');

if a > b then
writeln('Condition met')
else
writeln('Condition false');

for i := 1 to 10 do writeln('Iteration: ', i:1);

repeat a := a + 1 until a = 10;

Procedures and functions

Pascal structures programs into procedures and functions.

program mine(output);
var i : integer;

procedure print(var j: integer);

function next(k: integer): integer;
begin
next := k + 1
end;

begin
writeln('The total is: ', j);
j := next(j)
end;

begin
i := 1;
while i <= 10 do print(i)
end.

Procedures and functions can nest to any depth, and the 'program' construct is the logical outermost block.

Each procedure or function can have its own declarations of goto labels, constants, types, variables, and other procedures and functions, which must all be in that order. This ordering requirement was originally intended to allow efficient single-pass compilation. However, in most modern dialects the strict ordering requirement of declaration sections has been abandoned.

Resources

Compilers

Several Pascal compilers are available for the use of general public:

  • Delphi is Borland's flagship RAD (Rapid Application Development) product. It uses the Object Pascal language (Dubbed the 'Delphi programming language' by Borland), descended from Pascal, to create applications for the windows platform. The latest versions 2005 and 2006 also support compiling to the .NET platform. A version of Delphi, Turbo Delphi Explorer, is available for free download.
  • Free Pascal (www.freepascal.org) is a multi-platform compiler written in Pascal (it is Self-hosting). It is aimed at providing a convenient and powerful compiler, both able to compile legacy applications and to be the means of developing new ones. It is distributed under the GNU GPL. Apart from compatibility modes for Turbo Pascal, Delphi and Mac Pascal, it also has its own procedural and object oriented syntax modes with support for extended features such as operator overloading. It supports many platforms and operating systems.
  • Dev-Pas (Dev-Pascal) is a Pascal IDE that was designed in Borland Delphi and which supports both Free Pascal and GNU Pascal as backend.
  • Chrome (Chrome programming language) (website: www.chromesville.com) is a next generation Visual Studio plugin and stand-alone (in the .NET environment only) compiler for the Object Pascal language with the .NET and Mono Platforms. It was created and is sold by RemObjects Software.
  • Kylix is Borland's newest reiteration of the Pascal branch of their products. It is the descendant of Delphi, with support for the Linux operating system and an improved object library. The compiler and the IDE are available now for non-commercial use. The product is currently no longer supported by Borland.
  • GNU Pascal Compiler (GPC) is the Pascal compiler of the GNU Compiler Collection (GCC). The compiler itself is written in C, the runtime library mostly in Pascal. Distributed freely under the GNU General Public License, it runs on many platforms and operating systems. It supports the ANSI/ISO standard languages and partial Borland/Turbo Pascal language support. One of the more painful omissions is the absence of a 100% TP compatible string type. Support for Borland Delphi and other language variations is quite limited, except maybe for Mac Pascal, the support for which is growing fast.
  • Virtual Pascal was created by Vitaly Miryanov in 1995 as a native OS/2 compiler compatible with Borland Pascal syntax. Then, it had been commercially developed by fPrint, adding Win32 support, and in 2000 it became freeware. Today it can compile for Win32, OS/2 and Linux, and is mostly compatible with Borland Pascal and Delphi. Development on this compiler was canceled on April 4, 2005.
  • P4 compiler, the basis for many subsequent Pascal-implemented-in-Pascal compilers, including the UCSD p-System.
  • Turbo Pascal was the dominant Pascal compiler for PCs during the 80s and early 90s, popular both because of its powerful extensions and extremely low compilation times. Turbo Pascal was compactly written and could compile, run, and debug all from memory without accessing disk. Slow floppy disk drives were common for programmers at the time, further magnifying Turbo Pascal's speed advantage. Currently, older versions of Turbo Pascal (up to 5.5) are available for free download from Borland's site.
  • Dr. Pascal is an interpreter that runs Standard Pascal. Notable are the "visible execution" mode that shows a running program and its variables, and the extensive runtime error checking. Runs programs but does not produce a separate executable binary. Runs on MS-DOS, Windows in DOS window, and old Macintosh.
  • IP Pascal Originally a Z80/CP/M Pascal that was ported and recoded for Intel 80386/PC, IP Pascal has a built-in portability library that is custom tailored to the Pascal language. For example, a standard text output application from 1970's original Pascal can be recompiled to work in a window and even have graphical constructs added. IP Pascal supports the ISO 7185 standard and upgrades the language logically. For example, original Pascal "padded right" strings are supported and integrated upwards seamlessly into dynamic strings. Standard Pascal static arrays are enhanced with dynamic arrays which are fully downward compatible with static arrays, etc.
  • Pocket Studio is a Pascal subset compiler/RAD targeting Palm / MC68xxx with some own extensions to assist interfacing with the Palm OS API.
  • MIDletPascal - A Pascal compiler and IDE that generates small and fast Java bytecode specifically designed to create software for mobiles
  • Vector Pascal Vector Pascal is a language targeted at SIMD instruction sets such as the MMX and the AMD 3d Now, supporting all Intel and AMD processors, as well as the Sony Playstation 2 Emotion Engine.
  • Morfik Pascal allows the development of Web applications entirely written in Object Pascal (both server and browser side).
  • web Pascal (www.codeide.com) is an online IDE and Pascal compiler.
  • WDSibyl - Visual Development Environment and Pascal compiler for Win32 and OS/2

A very extensive list can be found on Pascaland. The site is in French, but it is basically a list with URLs to compilers; there is little barrier for non-Francophones. The site, Pascal Central, a Mac centric Pascal info and advocacy site with a rich collection of article archives, plus links to many compilers and tutorials, may also be of interest.

Standards

In 1983, the language was standardized, in the international standard ISO/IEC 7185, as well as several local country specific standards, including the American ANSI/IEEE770X3.97-1983. In 1990, an extended Pascal standard was created as ISO/IEC 10206.

The ISO 7185 was stated to be a clarification of Wirth's 1974 language as detailed by the User Manual and Report [Jensen and Wirth], but was also notable for adding "Conformant Array Parameters" as a level 1 to the standard, level 0 being Pascal without Conformant Arrays.

Note that Niklaus Wirth himself referred to the 1974 language as "the Standard", for example, to differentiate it from the machine specific features of the CDC 6000 compiler.

On the large machines (mainframes and minicomputers) Pascal originated on, the standards were generally followed. On the IBM-PC, they were not. On IBM-PCs, the Borland standards Turbo Pascal and Delphi have the greatest number of users. Thus, it is typically important to understand whether a particular implementation corresponds to the original Pascal language, or a Borland dialect of it.

Divisions

Niklaus Wirth's Zurich version of Pascal was issued outside of ETH in two basic forms, the CDC 6000 compiler source, and a porting kit called Pascal-P system. The Pascal-P compiler left several features of the full language out. For example, procedures and functions as parameters, undiscriminated variant records, packing, dispose, interprocedural gotos and other features of the full compiler were left off.

UCSD Pascal, under professor Kenneth Bowles, used the Pascal-P2 kit, and consequentially had several of the same differences with the full Zurich Pascal compiler as the Pascal-P compiler did. UCSD Pascal later was adopted as Apple Pascal, and continued through several versions there.

Borland's Turbo Pascal, written by Anders Hejlsberg was written in assembly language independent of UCSD or the Zurich compilers. However, it adopted much of the same subset as the UCSD compiler, probably because at that time, UCSD was the most common Pascal system running on Microprocessors.

ISO Extended Pascal

ISO accepted a modification by the JPC, BSI, and SPARC to the Pascal language, called Extended Pascal, defined as ISO 10206. It adds, among other features:

  • Modular programming
  • Better string handling
  • Constant expressions
  • Complex numbers
  • External file access (by using a feature called "binding"):
PROGRAM WriteFile(INPUT, OUTPUT)
VAR
F : TEXT
B : BINDINGTYPE;
BEGIN
WRITELN('Enter filename: ');
READLN(B.NAME); { BINDINGTYPE is standard, it has a standard field NAME }
BIND(F, B);
REWIND(F);
WHILE NOT EOF(F) DO
PUT(F, GET(F));
UNBIND(F);
END.

(source: ISO 10206) The standard is publicly available.

Reception

Criticism

While very popular (although more so in the 1980s and early 1990s than now), early versions of Pascal have been widely criticised for being unsuitable for "serious" use outside of teaching. Brian Kernighan, famed popularizer of the C programming language, outlined his most notable criticisms of Pascal as early as 1981, in his paper Why Pascal Is Not My Favorite Programming Language. On the other hand, many major development efforts in the 1980s, such as for the Apple Lisa and Macintosh, heavily depended on Pascal (to the point where the C interface for the Macintosh operating system API had to deal in Pascal data types).

In the decades since then, Pascal has continued to evolve and most of Kernighan's points do not apply to current implementations. Unfortunately, just as Kernighan predicted in his article, most of the extensions to fix these issues were incompatible from compiler to compiler. In the last decade, however, the varieties seem to have condensed into two categories, ISO and Borland like, a better eventual outcome than Kernighan foresaw.

Based on his experience with Pascal (and earlier with ALGOL) Niklaus Wirth developed several more programming languages: Modula, Modula-2 and Oberon. These languages address some criticisms of Pascal, are intended for different user populations, and so on, but none has had the widespread impact on computer science and computer users as has Pascal, nor has any yet met with similar commercial success.

Praise

Beginning Programming for Dummies, 3rd edition (2004), in a comparison between Pascal and BASIC, praised Pascal as a learning tool:

The main difference between BASIC and Pascal is that Pascal encourages you to write well-structured programs that you can easily read, understand, and modify at a later date.

Compared with Pascal, BASIC is much less structured, which makes writing a BASIC program easy but makes reading and understanding large BASIC programs much more difficult. Pascal is more structured and forces you to plan your program before you write, as you do if you first plan a trip before leaving home. This planning may take longer, but your program and your trip will be more organized than if you rush into writing the program right away, which can be as disorganized as showing up in Paris in the middle of the night with no hotel reservations. On the other hand, BASIC enables you to start writing your program right away, which is more likely to lead to a more disorganized program.

In the early 1980s, the Educational Testing Service, the company that creates the primary college entrance exam in the United States, added a Computer Service exam to the placement exams taken by high-school students. The computer language chosen was Pascal (and remained the language of choice until 1999).

Pascal is best known for its high performance with structured programming techniques. Another feature of Pascal is that it highlights concepts that are similar to all computer languages. So, learning Pascal makes the learning of other languages easier. Also, Pascal uses standardized language, which makes the writing of programs less arduous.

Programming language

A programming language is an artificial language that can be used to control the behavior of a machine, particularly a computer. Programming languages, like human languages, are defined through the use of syntactic and semantic rules, to determine structure and meaning respectively.

Programming languages are used to facilitate communication about the task of organizing and manipulating information, and to express algorithms precisely. Some authors restrict the term "programming language" to those languages that can express all possible algorithms; sometimes the term "computer language" is used for more limited artificial languages.

Thousands of different programming languages have been created, and new languages are created every year.