var
x: 1..10;
y: 'a'..'z';
z: pear..orange;
Set types
In contrast with other programming languages from its time, Pascal supports a set type:
var
set1: set of 1..10;
set2: set of 'a'..'z';
set3: set of pear..orange;
A set is a fundamental concept for modern mathematics, and they may be used in a many algorithms. Such a feature is useful and may be faster than an equivalent construct in a language that does not support sets. For example, for many Pascal compilers:
executes faster than:
if (i>4) and (i<11) then
...
Sets of non-contiguous values can be particularly useful, in terms of both performance and readability:
if i in [0..3, 7, 9, 12..15] then
...
For these examples, which involve sets over small domains, the improved performance is usually achieved by the compiler representing set variables as bitmasks. The set operators can then be implemented efficiently as bitwise machine code operations.
However, for examples where the range of values is significantly larger than the native word size, set expressions are likely to result in worse performance and greater memory usage than equivalent expressions using relational operators.
[edit] Type declarations
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
x: Integer;
y: Char
end;
c = File of a;
[edit] File type
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 for inspecting the next character to be used (check for a digit before reading an integer), this concept leads to serious problems with interactive programs in 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.
[edit] Pointer types
Pascal supports the use of pointers:
type
Nodeptr = ^Node;
Node = record
a: Integer;
b: Char;
c: Nodeptr
end;
var
ptoNode: Nodeptr;
pInt : ^Integer;
Here the variable ptoNode is a pointer to the data type Node, a record. Pointers can be used before they are declared. This is a forward declaration, an exception to the rule that things must be declared before they are used. To create a new record and assign the value 10 and character A to the fields a and b in the record, and to initialise the pointer c to nil, the commands would be:
new(ptoNode);
...
ptoNode^.a := 10;
ptoNode^.b := 'A';
ptoNode^.c := nil;
...
This could also be done using the with statement, as follows
new(ptoNode);
...
with ptoNode^ do
begin
a := 10;
b := 'A';
c := nil
end;
...
Inside of the scope of the with statement, a and b refer to the subfields of the record pointer ptoNode and not to the record Node or the pointer type Nodeptr.
Linked lists, stacks and queues can be created by including a pointer type field (c) in the record (see also nil).
Unlike many languages that feature pointers, Pascal only allows pointers to reference dynamically created variables that are anonymous, and does not allow them to reference standard static or local variables. Pointers also must have an associated type, and a pointer to one type is not compatible with a pointer to another type (e.g. a pointer to a char is not compatible with a pointer to an integer). This helps eliminate the type security issues inherent with other pointer implementations, particularly those used for PL/I or C. It also removes some risks caused by dangling pointers, but the ability to dynamically let go of referenced space by using the dispose function (which has the same effect as the free library function found in C) means that the risk of dangling pointers has not been entirely eliminated.[8]
[edit] Control structures
Pascal is a structured programming language, meaning that the flow of control is structured into standard statements, ideally without 'goto' commands.
while a <> b do writeln('Waiting');
if a > b then writeln('Condition met')
else writeln('Condition not met');
for i := 1 to 10 do writeln('Iteration: ', i:1);
repeat
a := a + 1
until a = 10;
case i of
0: write('zero');
1: write('one');
2: write('two')
end;
[edit] 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 some dialects (such as Borland Delphi) the strict ordering requirement of declaration sections is not required.
[edit] Semicolons as statement separators
Pascal adopted many language syntax features from the ALGOL language, including the use of a semicolon as a statement separator. This is in contrast to other languages, such as PL/I, C etc. which use the semicolon as a statement terminator. As illustrated in the above examples, no semicolon is needed before the end keyword of a record type declaration, a block, or a case statement; before the until keyword of a repeat statement; and before the else keyword of an if statement.
The presence of an extra semicolon was not permitted in early versions of Pascal. However, the addition of ALGOL-like empty statements in the 1973 Revised Report and later changes to the language in ISO 7185:1983 now allow for optional semicolons in most of these cases. The exception is that a semicolon is still not permitted immediately before the else keyword in an if statement.
[edit] Resources
[edit] Compilers and interpreters
Several Pascal compilers and interpreters are available for the use of general public:
-
Delphi is Embarcadero's (formerly Borland/CodeGear) 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 .NET support that existed from D8 through D2005,D2006 and D2007 has been terminated, and replaced by a new language (Prism, which is rebranded Oxygene, see below) that is not fully backwards compatible. The most recent iteration of the win32 range (D2009) adds unicode and generics support.
-
Free Pascal is a multi-platform compiler written in Object Pascal (and 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, while packages and runtime library come under a modified GNU LGPL. 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.
-
Lazarus is a Delphi-like visual cross-platform IDE for rapid application development (RAD). Based on Free Pascal, Lazarus is available for numerous platforms including Linux, FreeBSD, Mac OS X and Microsoft Windows.
-
Dev-Pascal is a Pascal IDE that was designed in Borland Delphi and which supports both Free Pascal and GNU Pascal as backend.
-
Turbo51 is a free Pascal compiler for the 8051 family of microcontrollers (uses Turbo Pascal 7 syntax).
-
Oxygene (formerly known as Chrome) is an Object Pascal compiler for the .NET and Mono platforms. It was created and is sold by RemObjects Software, and recently by Embarcadero as the backend compiler of Prism.
-
Kylix was a 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 no longer supported.
-
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 under the GNU General Public License, it runs on many platforms and operating systems. It supports the ANSI/ISO standard languages and has partial Turbo Pascal dialect support. One of the more painful omissions is the absence of a 100% Turbo Pascal-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.
-
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.
-
Dr. Pascal's Extended Pascal Compiler tested on DOS, Windows 3.1, 95, 98, NT.
-
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. It implements a subset of full Pascal.
-
P5 compiler, is an ISO 7185 (full Pascal) adaption of P4.
-
Turbo Pascal was the dominant Pascal compiler for PCs during the 80s and early 90s, popular both because of its powerful extensions and extremely short 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.
-
IP Pascal Implements the language "Pascaline" (named after Pascal's calculator), which is a highly extended Pascal compatible with original Pascal according to ISO 7185. It features modules with namespace control, including parallel tasking modules with semaphores, objects, dynamic arrays of any dimensions that are allocated at runtime, overloads, overrides, and many other extensions. 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.
-
Pascal-XT was created by Siemens for their mainframe operating systems BS2000 and SINIX.
-
PocketStudio is a Pascal subset compiler and RAD tool targeting Palm OS and MC68xxx processors with some own extensions to assist interfacing with the Palm OS API. It resembles Delphi and Lazarus with a visual form designer, an object inspector and a source code editor.
-
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).
-
WDSibyl - Visual Development Environment and Pascal compiler for Win32 and OS/2
- PP Compiler, a compiler for Palm OS that runs directly on the handheld computer
-
CDC 6000 Pascal compiler The source code for the first (CDC 6000) Pascal compiler.
-
Pascal-S - "Pascal-S: A Subset and Its Implementation", N. Wirth in Pascal - The Language and Its Implementation, by D.W. Barron, Wiley 1979.
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.
[edit] Standards
In 1983, the language was standardized, in the international standard IEC/ISO 7185, as well as several local country specific standards, including the American ANSI/IEEE770X3.97-1983, and ISO 7185:1983. These two standards differed only in that the ISO standard included a "level 1" extension for conformant arrays, where ANSI did not allow for this extension to the original (Wirth version) language. In 1989, ISO 7185 was revised (ISO 7185:1990) to correct various errors and ambiguities found in the original document.
In 1990, an extended Pascal standard was created as ISO/IEC 10206. In 1993 the ANSI standard was replaced by the ANSI organization with a "pointer" to the ISO 7185:1990 standard, effectively ending its status as a different standard.
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. This addition was made at the request of C. A. R. Hoare, and with the approval of Niklaus Wirth. The precipitating cause was that Hoare wanted to create a Pascal version of the (NAG) Numerical Algorithms Library, which had originally been written in FORTRAN, and found that it was not possible to do so without an extension that would allow array parameters of varying size. Similar considerations motivated the inclusion in ISO 7185 of the facility to specify the parameter types of procedural and functional parameters.
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. This language was documented in "The Pascal Report", the second part of the "Pascal users manual and report".
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.
The IBM-PC versions of the language began to differ with the advent of UCSD Pascal, an interpreted implementation that featured several extensions to the language, along with several omissions and changes. Many UCSD language features survive today, including in Borland's dialect.
[edit] 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 out several features of the full language. For example, procedures and functions used as parameters, undiscriminated variant records, packing, dispose, interprocedural gotos and other features of the full compiler were omitted.
UCSD Pascal, under Professor Kenneth Bowles, was based on the Pascal-P2 kit, and consequently shared several of the Pascal-P language restrictions. UCSD Pascal was later adopted as Apple Pascal, and continued through several versions there. Although UCSD Pascal actually expanded the subset Pascal in the Pascal-P kit by adding back standard Pascal constructs, it was still not a complete standard installation of Pascal.
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 and extensions as the UCSD compiler. This is probably because the UCSD system was the most common Pascal system suitable for developing applications on the resource-limited microprocessor systems available at that time.
[edit] List of related standards
-
ISO 8651-2:1988 Information processing systems—Computer graphics—Graphical Kernel System (GKS) language bindings—Part 2: Pascal
[edit] Reception
Pascal generated a wide variety of responses in the computing community, both critical and complimentary.
[edit] Criticism
While very popular (although more so in the 1980s and early 1990s than now), implementations of Pascal which closely followed Wirth's initial definition of the language were widely criticized for being unsuitable for use outside of teaching. Brian Kernighan, who popularized 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.[9] The most serious problem, described in this article, seems that array sizes and string lengths were part of the type so it was not possible to write a function that would accept variable length arrays or even strings as parameters (like a sorting library, for instance). The author also criticized the unpredictable order of evaluation of boolean expressions, poor library support, lack of static variables and a number of smaller issues. Also, he stated that the language did not provide any simple constructs to "escape" (knowingly and forcibly ignore) restrictions and limitations where this is really necessary. (However, there is a feature of "record variants" that does allow such an "escape," though it is decidedly cumbersome.) More general complaints from other sources[10][11] noted that the scope of declarations were not clearly defined in the original language definition, which sometimes had serious consequences when using forward declarations to define pointer types, or when record declarations lead to mutual recursion, or when an identifier may or may not have been used in an enumeration list. Another difficulty was that, like ALGOL 60, the language did not allow procedures or functions passed as parameters to pre-define what their parameters are supposed to be.
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).
[edit] Reactions
Pascal continued to evolve, and most of Kernighan's points do not apply to versions of the language which were enhanced to be suitable for commercial product development, such as Borland's Turbo Pascal. Unfortunately, just as Kernighan predicted in his article, most of the extensions to fix these issues were incompatible from compiler to compiler. Since the early 1990s, however, the varieties seem to have condensed into two categories, ISO and Borland-like, a better eventual outcome than Kernighan foresaw.[original research?]
Although Kernighan decried Pascal's lack of type escapes ("there is no escape" from "Why Pascal is not my Favorite Programming language"), the uncontrolled use of pointers and type escapes have become highly criticized features in their own right, and the languages Java, C# and others feature a sharp turn-around to the Pascal point of view. What these languages call "managed pointers" were in fact foreseen by Wirth with the creation of Pascal.
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.
[edit] See also
[edit] Further reading
- Niklaus Wirth: The Programming Language Pascal. 35-63, Acta Informatica, Volume 1, 1971.
- C A R Hoare: Notes on data structuring. In O-J Dahl, E W Dijkstra and C A R Hoare, editors, Structured Programming, pages 83–174. Academic Press, 1972.
- C. A. R. Hoare, Niklaus Wirth: An Axiomatic Definition of the Programming Language Pascal. 335-355, Acta Informatica, Volume 2, 1973.
-
Kathleen Jensen and Niklaus Wirth: PASCAL - User Manual and Report. Springer-Verlag, 1974, 1985, 1991, ISBN 0-387-97649-3 and ISBN 3-540-97649-3 [1]
- Niklaus Wirth: Algorithms + Data Structures = Programs. Prentice-Hall, 1975, ISBN 0-13-022418-9 [2]
- Niklaus Wirth: An assessment of the programming language PASCAL 23-30 ACM SIGPLAN Notices Volume 10, Issue 6, June 1975.
- N. Wirth, and A. I. Wasserman, ed: Programming Language Design. IEEE Computer Society Press, 1980
- D. W. Barron (Ed.): Pascal - The Language and its Implementation. John Wiley 1981, ISBN 0-471-27835-1
- Peter Grogono: Programming in Pascal, Revised Edition, Addison-Wesley, 1980
- Richard S. Forsyth: Pascal in Work and Play, Chapman and Hall, 1982
- N. Wirth, M. Broy, ed, and E. Denert, ed: Pascal and its Successors in Software Pioneers: Contributions to Software Engineering. Springer-Verlag, 2002, ISBN 3-540-43081-4
- N. Wirth: Recollections about the Development of Pascal. ACM SIGPLAN Notices, Volume 28, No 3, March 1993.
[edit] References
-
^ «We looked very carefully at Delphi Object Pascal and built a working prototype of bound method references in order to understand their interaction with the Java programming language and its APIs.»
«Our conclusion was that bound method references are unnecessary and detrimental to the language. This decision was made in consultation with Borland International, who had previous experience with bound method references in Delphi Object Pascal.» White Paper.About Microsoft's "Delegates", java.sun.com
-
^ TechMetrix Research (1999). "History of Java". Java Application Servers Report. http://www.fscript.org/prof/javapassport.pdf. "The project went ahead under the name "green" and the language was based on an old model of UCSD Pascal, which makes it possible to generate interpretive code"
-
^ A Conversation with James Gosling
-
^ http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html
-
^ Jon Udell, Crash of the Object-Oriented Pascals, BYTE, July, 1989.
-
^ M.I.Trofimov, The End of Pascal?, BYTE, March, 1990, p.36.
-
^ Pascal ISO 7185:1990 6.10
-
^ J. Welsh, W. J. Sneeringer, and C. A. R. Hoare, "Ambiguities and Insecurities in Pascal," Software Practice and Experience 7, pp. 685-696 (1977)
-
^ Brian W. Kernighan (1981).Why Pascal is Not My Favorite Programming Language
-
^ O. Lecarme, P. Desjardins, "More Comments on the Programming Language Pascal," Acta Informatica 4, pp. 231-243 (1975)
-
^ J. Welsh, W. J. Sneeringer, C. A. R. Hoare, "Ambiguities and Insecurities in Pascal," Software Practice and Experience 7, pp. 685-696 (1977)
از وبلاگ جهان یکه نگار