.

Thursday, May 19, 2011

The Pascal Architecture

Pascal is a strongly typed, block structured programming language. The "type" of a Pascal variable consists of its semantic nature and its range of values, and can be expressed by a type name, an explicit value range, or a combination thereof. The range of values for a type is defined by the language itself for built-in types, or by the programmer for programmer defined types. Programmer-defined types are unique data types defined within the Pascal TYPE declaration section, and can consist of enumerated types, arrays, records, pointers, sets, and more, as well as combinations thereof. When variables are declared as one type, the compiler can assume that the variable will be used as that type throughout the life of the variable (whether it is global to the program, or local to a function or procedure). This consistent usage of variables makes the code easier to maintain. The compiler detects type inconsistency errors at compile time, catching many errors and reducing the need to run the code through a debugger. Additionally, it allows an optimizer to make assumptions during compilation, thereby providing more efficient executables. As John Reagan, the architect of Compaq Pascal, writes, "it was easy to write Pascal programs that would generate better code than their C equivalents" because the compiler was able to optimize based on the strict typing.


Declaring variables in Pascal is straightforward. The Pascal VAR declaration section gives the programmer the ability to declare strings, integers, real numbers and booleans (to name a few built-in types), as well as to declare variables as records or other programmer defined types. A variable defined as a RECORD allows a single variable to track several data components (or fields).

Type     Employee_type   =   (Hourly, Salary, SalaryExempt);    InputRec        =   RECORD       emp_name:      packed array[1..30 ] of char;       social:        packed array[1..9] of char;       salary:        real;       emp_type:      Employee_type;       end;  Var    index:      integer;    ratio:      real;    found:      boolean;    inpf:       file of InputRec;

Figure 1: Sample Code - Types and Records

Pascal also supports recursion, a powerful computing tool that allows a function or procedure within a program to make calls to itself. This allows for elegant and efficient coding solutions, eliminating the need for tedious loops. A good example of recursion is the following Pascal solution to the classic Towers of Hanoi puzzle (see Figure 2). The puzzle is to take a stack of disks in increasing sizes from top to bottom, and move them from the first peg to the second peg, with the rule that a disk must always be placed on a disk larger than itself, and only one disk can be moved at a time.

Program TowersOfHanoi(input,output);  Var    disks:   integer;   Procedure Hanoi(source, temp, destination: char;  n: integer);     begin    if n > 0 then       begin       Hanoi(source, destination, temp, n - 1);       writeln('Move disk ',n:1,' from peg ',source,' to peg ',destination);       Hanoi(temp, source, destination, n - 1);       end;    end;   begin write('Enter the number of disks: '); readln(disks); writeln('Solution:'); Hanoi('A','B','C',disks); end.

Figure 2: Sample Recursive Code ó Towers of Hanoi Solution

The solution to the Towers of Hanoi puzzle involves moving all but one disk from peg to peg, repeatedly, until the entire stack has moved from one peg to the other. The elegance of recursion is that this solution is illustrated clearly, without mundane loops and logic checks. The three steps of the solution are depicted by three lines of code. The movement of a stack, regardless of size, is always done by a call to Hanoi, thus ensuring that the rules are adhered to.

Pascal eliminates the need for clumsy "goto" statements by supporting REPEAT/UNTIL, WHILE/DO, and FOR loops; by providing an intelligent CASE statement; and by providing a means to consolidate common lines of code into PROCEDUREs and FUNCTIONs. Using the English words of BEGIN and END to delimit blocks of code within a clause, enforcing strong typing, providing ordinal-based arrays, and other useful linguistic features, Pascal facilitates the production of correct, reliable, and maintainable code. Any language can be commented and indented for better readability, but Pascal, by the nature of its syntax and architecture, encourages structured programming practices and allows the programmer to focus on developing solutions. It's important to emphasize this element of Pascal. Even programmers with the most sophisticated and disciplined of programming styles will find themselves in a time crunch. With deadlines quickly approaching, it's likely that a programmer will focus more on achieving a result and less on making the code understandable for future maintenance. The key to Pascal is that a programmer tasked with maintaining Pascal code will be able to make the same assumptions that the compiler makes about program flow and data usage. This gives the maintenance programmer a fighting chance of figuring out the behavior, purpose, and operating conditions of the code, even if itís poorly-written.

No comments:

Post a Comment