Pascal

From Compsci.ca Wiki

(Difference between revisions)
Jump to: navigation, search
m (Reverted edit of LzgWlo, changed back to last version by Wtd)
 
(4 intermediate revisions not shown)
Line 3: Line 3:
==Language==
==Language==
All Pascal programs start with the "Program" [[keyword]], an optional list of external file descriptors and then a [[block]] of code is indicated with the "<code>Begin</code>" and "<code>End</code>" keywords. [[Semicolon]]s separate statements, and the [[full stop]] ends the program (or unit). Upper and lower case are considered to be the same in pascal.
All Pascal programs start with the "Program" [[keyword]], an optional list of external file descriptors and then a [[block]] of code is indicated with the "<code>Begin</code>" and "<code>End</code>" keywords. [[Semicolon]]s separate statements, and the [[full stop]] ends the program (or unit). Upper and lower case are considered to be the same in pascal.
-
<code>
+
<pre>
     program HelloWorld;
     program HelloWorld;
     begin
     begin
         writeln('Hello, World!');
         writeln('Hello, World!');
     end.
     end.
-
</code>
+
</pre>
Pascal is a purely [[procedural language]] with the standard array of <code>if</code>, <code>while</code>, <code>for</code>, and related constructs. It is a [[structured programming]] language, meaning that the flow of control is structured into standard statements, ideally without '[[goto]]' commands.
Pascal is a purely [[procedural language]] with the standard array of <code>if</code>, <code>while</code>, <code>for</code>, and related constructs. It is a [[structured programming]] language, meaning that the flow of control is structured into standard statements, ideally without '[[goto]]' commands.
-
<code>
+
<pre>
     while a <> b '''do
     while a <> b '''do
         WriteLn('Waiting');
         WriteLn('Waiting');
Line 26: Line 26:
         a := a + 1;
         a := a + 1;
     until a = 10;
     until a = 10;
-
</code>
+
</pre>
Pascal structures programs into procedures and functions.
Pascal structures programs into procedures and functions.
-
<code>
+
<pre>
     Procedure Foo(parameter:integer);
     Procedure Foo(parameter:integer);
     Begin
     Begin
Line 39: Line 39:
         Bar := a + 1;
         Bar := a + 1;
     End;
     End;
-
</code>
+
</pre>
      
      
Procedures and functions can nest to any depth, and the 'program' construct is the logical outermost block. Each block can have its own declarations of goto labels, constants, types, variables, and other procedures and functions. Depending on the compiler there may be many basic types defined. Usually integer, longint, char, Boolean, and string are defined. Other basic types for floating point might also be defined (float, double, and/or real), as well as larger integer types. Enumerated types are also supported.
Procedures and functions can nest to any depth, and the 'program' construct is the logical outermost block. Each block can have its own declarations of goto labels, constants, types, variables, and other procedures and functions. Depending on the compiler there may be many basic types defined. Usually integer, longint, char, Boolean, and string are defined. Other basic types for floating point might also be defined (float, double, and/or real), as well as larger integer types. Enumerated types are also supported.
-
<code>
+
<pre>
     program myprog;
     program myprog;
    
    
Line 51: Line 51:
         c: boolean;
         c: boolean;
         d: (one, two, three, four, five);
         d: (one, two, three, four, five);
-
</code>
+
</pre>
A "subrange" of any of the above types can be made.
A "subrange" of any of the above types can be made.
-
<code>
+
<pre>
     var
     var
         x: 1..10;
         x: 1..10;
         y: 'a'..'z';
         y: 'a'..'z';
-
</code>
+
</pre>
Types can be built from other types by the type declaration.
Types can be built from other types by the type declaration.
-
<code>
+
<pre>
     type
     type
         x = integer;
         x = integer;
         y = x;
         y = x;
-
</code>
+
</pre>
Further, complex types can be constructed from simple types:
Further, complex types can be constructed from simple types:
-
<code>
+
<pre>
     type
     type
         a = array [1..10] of integer;
         a = array [1..10] of integer;
Line 77: Line 77:
         end;
         end;
         c = file of a;
         c = file of a;
-
</code>
+
</pre>
Strings are packed arrays that start with an integer index of 1.
Strings are packed arrays that start with an integer index of 1.
Pascal also supports the use of [[pointer]]s:
Pascal also supports the use of [[pointer]]s:
-
<code>
+
<pre>
     type
     type
         a = ^b;
         a = ^b;
Line 93: Line 93:
     var
     var
         pb : a;
         pb : a;
-
</code>
+
</pre>
Here the variable ''pb'' 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;
Here the variable ''pb'' 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;
-
<code>
+
<pre>
     new(pb);
     new(pb);
     pb^.a := 10;
     pb^.a := 10;
     pb^.b := 'A';
     pb^.b := 'A';
     pb^.c := nil;
     pb^.c := nil;
-
</code>
+
</pre>
[[Linked list]]s can be created by including a pointer type field (c) in the record.
[[Linked list]]s can be created by including a pointer type field (c) in the record.
Line 128: Line 128:
[http://www.freepascal.org/ Free Pascal]
[http://www.freepascal.org/ Free Pascal]
[http://www.gnu-pascal.de/ GNU Pascal]
[http://www.gnu-pascal.de/ GNU Pascal]
 +
 +
==Derivatives==
 +
Pascal has had enough of an impact on the programming language world that several languages are modelled after it, either in syntax and semantics, and sometimes both.  Some of these languages in fact have been developed by [[Niklaus Wirth]] as successors to Pascal.
 +
 +
* Modula, Modula-2, Modula-3
 +
* Oberon and variants
 +
* Ada
 +
* Eiffel
 +
* [[Turing]] was designed as a replacement for Pascal in Ontario high schools and is heavily influenced by Pascal.
 +
* [[Ruby]] has a syntax which takes some inspiration from Eiffel and is thus indirectly inspired by Pascal.

Latest revision as of 00:18, 11 June 2007

Pascal is an imperative computer programming language, developed in 1970 by Niklaus Wirth as a language particularly suitable for structured programming. Pascal is based on the ALGOL programming language and named in honor of mathematician and philosopher Blaise Pascal.

Contents

Language

All Pascal programs start with the "Program" keyword, an optional list of external file descriptors and then a block of code is indicated with the "Begin" and "End" keywords. Semicolons separate statements, and the full stop ends the program (or unit). Upper and lower case are considered to be the same in pascal.

    program HelloWorld;
    begin
        writeln('Hello, World!');
    end.

Pascal is a purely procedural language with the standard array of if, while, for, and related constructs. It 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 false');
  
    for i := 1 to 10 do 
        writeln('Iteration: ', i);
  
    repeat
        a := a + 1;
    until a = 10;

Pascal structures programs into procedures and functions.

    Procedure Foo(parameter:integer);
    Begin
        Writeln(‘Foo(‘, parameter,’);’);
    End;

    Function Bar(a:integer):integer;
    Begin
        Bar := a + 1;
    End;


Procedures and functions can nest to any depth, and the 'program' construct is the logical outermost block. Each block can have its own declarations of goto labels, constants, types, variables, and other procedures and functions. Depending on the compiler there may be many basic types defined. Usually integer, longint, char, Boolean, and string are defined. Other basic types for floating point might also be defined (float, double, and/or real), as well as larger integer types. Enumerated types are also supported.

    program myprog;
  
    var
        a: integer;
        b: char;
        c: boolean;
        d: (one, two, three, four, five);

A "subrange" of any of the above types can be made.

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

Types can be built from other types by the type declaration.

    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;

Strings are packed arrays that start with an integer index of 1.

Pascal also supports the use of pointers:

    type
        a = ^b;
        b = record
            a: integer;
            b: char;
            c: a;
        end;

    var
        pb : a;

Here the variable pb 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(pb);
    pb^.a := 10;
    pb^.b := 'A';
    pb^.c := nil;

Linked lists can be created by including a pointer type field (c) in the record.

Features

Pascal was originally intended to be a teaching language, so its syntax is relatively easy to learn. Because of its age it lacks support for newer concepts such as classes and objects, however many newer compilers do provide some support for these features. Pascal can be interpreted or compiled directly to machine code, depending on the compiler you are using. Mostly it is just a compiled language.

As pascal was intended to be a teaching language, it contains all the essential programming features -- Input/Output, Control Flow, Iteration, etc. Common tasks (such as receiving keyboard input) are quite simple, allowing students to focus on the logic of the program rather than details about the implementation. In addition, many compilers also provide easy to use graphics libraries so that students don’t need to learn DirectX or OpenGL to do simple drawing; though interfaces to both libraries can usually be found.

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.


Compilers

There are many compilers for pascal, the most notable ones being the Free Pascal compiler, and the GNU Pascal compiler.

Borland also makes pseudo pascal compilers under the names “Turbo Pascal” and “Delphi”, however despite their similarity to pascal, they do not make any effort to be standards compliant.


External Links

Free Pascal GNU Pascal

Derivatives

Pascal has had enough of an impact on the programming language world that several languages are modelled after it, either in syntax and semantics, and sometimes both. Some of these languages in fact have been developed by Niklaus Wirth as successors to Pascal.

  • Modula, Modula-2, Modula-3
  • Oberon and variants
  • Ada
  • Eiffel
  • Turing was designed as a replacement for Pascal in Ontario high schools and is heavily influenced by Pascal.
  • Ruby has a syntax which takes some inspiration from Eiffel and is thus indirectly inspired by Pascal.
Personal tools