About Component Pascal


Component Pascal is an object-oriented language of the Pascal family. Its ancestors are Pascal, Modula-2, and Oberon. The language is almost a strict superset of Oberon-2. Listing E.1 is the canonical " hello world " program for Component Pascal, shown in the so-called publication format.

Listing E.1
 module Hello;   import CPmain, Console; begin   Console.WriteString("Hello CP World");   Console.WriteLn; End Hello. 

The object-oriented features of Component Pascal include single implementation inheritance, with explicit annotations that control the extensibility of types and of methods . Compared to other members of the Pascal family, the signatures of procedures are more expressive, with Ada -like annotations specifying whether particular reference parameters carry values into the invocation ( in mode), out of the invocation ( out mode), or in both directions ( var mode). The purpose of all these annotations is to allow a higher level of compile-time checking of source code and to provide stricter invariants for program analysis.

The Type System

As with other Pascal-family languages, the type system is based on statically declared types. The available type constructors are records, pointers, and arrays, as well as the usual built-in types. There are no union types (i.e., no variant records), and no subrange or enumeration types.

In common with all other Pascal dialects, the definition of an array includes a declared length. Unlike C-family languages, where all arrays of characters are declared as " char [] ", in Pascal two arrays are the same type only if they share the same type definition, including the length attribute. There are significant semantic consequences of this difference.

Pointers may be declared with bound types that are array or record types. There is also an open array construct in which a pointer type is bound to an array of indeterminate length. A variable of such type may refer to an array of dynamically determined length, corresponding to the one-dimensional arrays native to Java and C#.

Statically typed, non “object-oriented languages usually include a union construct, so as to allow a primitive kind of type polymorphism. Object-oriented languages have more advanced facilities for polymorphism and are able to avoid the notoriously error-provoking union construct. Most modern languages, including Java, C#, and Component Pascal, do not have a union construct.

In Component Pascal, the record types are extensible. Record type definitions may declare that they extend some existing record type, provided that the original type declared that it was extensible. The new type is said to be an extension of its base type. Pointers to such extensible types have subtype polymorphism. That is, a variable declared to be of some particular pointer type may contain a reference to an object of the declared type or to an object of any direct or indirect extension of that type.

The code fragment in Listing E.2 declares an abstract record type Figure as well as an extension of that type named Circle. The type name in parentheses in the record declaration of the Circle type specifies the base type, Figure, that the new type extends.

Listing E.2
 type     Figure = abstract record (* no fields *) end;     Circle = record (Figure)                  x, y, radius : REAL;              end; 

It is possible to declare static variables and local variables that are of array or record types. These structures are allocated implicitly. Variables of pointer type are initially nil , and they require an explicit call of new to allocate the variable instance.

Procedures may be declared as being "bound" to a particular record type. Such type-bound procedures correspond to methods in the usual object-oriented terminology. Methods are inherited by all extensions of a particular type, unless the subtype declares a method with the same name and signature to override the inherited method. Thus, in the example in Listing E.2, methods declared on the Figure type will be inherited by Circle and all other extensions of the base type.

An inherited method may specify a return type that differs covariantly from the return type of the method that it overrides . That is, the return type of the method bound to the subtype may be a subtype of the return type of the method that was overridden. [2]

[2] This rather fine point is mentioned here because the implementation of such covariance is one of the real challenges of mapping to the CLR.

Just as record types may be extended only if they are declared to be extensible, so methods may be overridden only if they are declared to be extensible. The default is thus for types to be sealed and for methods to be final.

The basic, built-in types include signed integers ranging from 8 to 64 bits, and two floating-point types REAL and SHORTREAL corresponding to IEEE double and float , respectively. The CHAR type corresponds to 16-bit Unicode characters.

Statements

Component Pascal contains the usual assignment, procedure call, loop, and choice statements. The loops include pre- and post- tested loops , a structured for loop, and an endless loop construct. The choice statements include an " if, elsif, else, end " construct, and a multiway case statement corresponding to the familiar switch of C-family languages.

An unusual feature of the language is the presence of a "type case" statement. This statement, which begins with the keyword with , is a multiway branch where the target branch selected depends on the type of the selecting variable. In many common situations, this statement is very convenient because within the selected code the selecting variable is known to be of the selected type and may be referenced without the repeated use of a narrowing cast as would be required by many other languages.

Static procedures may be nested within other procedures and within methods. Such procedure declarations may be nested to any depth, and the code in such nested procedures may access the local variables and arguments of the lexically enclosing procedures. The efficient implementation of such nonlocal addressing is a significant challenge for the compiler writer who chooses to target the CLR.

Recall that in Pascal-family languages arrays have a statically known fixed length. Array values may thus be assigned to variables of the same array type, as they are necessarily of the same length. Similarly, it is possible to assign entire values of the record type if the record type is not extensible. It is a characteristic of the Pascal languages that assignments have value semantics. That is, when arrays or records are assigned, a copy of the value on the right-hand side of the assignment statement is created at the destination location. Similarly, when pointer values are assigned, a value copy of the pointer is created. Of course, this value copy will be an alias reference to the bound value of the original pointer, and both pointer values will reference the same object.

Module Structure

The main structuring construct of Component Pascal programs is the module . In this language, modules correspond to compilation units, and identifiers are made visible across module boundaries by explicit export and import statements.

At the top of a module is a list of all modules whose public names are to be visible in that module. Such imported names are known by their fully qualified denotations, although there is a facility for creating short "alias" names for commonly used module names . There is nothing corresponding to the using statement that in C# imports names and makes them accessible unqualified by a prepended namespace identifier.

Every name that is declared at the outer lexical level in a module may be optionally marked for export. In the case of variables, the export may be full or read-only. In the case of record declarations, individual fields may be private to the current compilation unit, fully exported, or exported in read-only mode.

Notice that the visibility of names and the accessibility of features are controlled only on a module-by-module basis. There is nothing corresponding to either the private or protected modes of some other object-oriented languages.

Modules in Component Pascal have an optional body. The body, if it exists, is executed just once, when the module is loaded at runtime.



Programming in the .NET Environment
Programming in the .NET Environment
ISBN: 0201770180
EAN: 2147483647
Year: 2002
Pages: 146

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net