Differences Between XPL and Pascal
By Larry Fisn

XPL and Pascal are very similar languages. In fact, many of the constructs in XPL were based on Pascal syntax. Nevertheless, there are some difference between the two languages. Some are due to the fact that XPL also used C constructs in its syntax. Some are due to the fact that Pascal supports Structures and Object Oriented Programming. The follow table illustrates that basic syntactic differences between XPL and Pascal:

I. Basic Syntax Differences. The majority of the Pascal syntax is the same as XPL. This list describes the differences.

Description

XPL Format

Pascal Equivalent

Variable declaration

integer X;

var X: integer;

Constants

define X=20;

const X=20;

Comment

\Comment\

{Comment} or //Comment

Procedure declaration

procedure Test(Arg);
integer Arg;

procedure Test(Arg: integer);

Function declaration

function real Test;

function Test: double;

Array declaration

integer Test(52);
or
integer Test;
Test:=Reserve(52*4);

var Test: array [0..51] of integer

Array indexing

X(5):=Y(6);

X[5]:=Y[6];

Loop exit

quit;

break; (works on all loops)

For loop (+)

for I:=0,100 do

for I:=0 to 100 do

For loop (-)

for I:=-100,0 do X:=-I;

for I:=100 downto 0 do X:=I;

Loop

loop
  begin
  if Frog then quit;
  end;

while true do
  begin
  if Frog then break;
  end;

Case statement

case X of
 1,2: T:=5;
 3,4: T:=10;
 other T:=15;

case X of
 1,2: T:=5;
 3,4: T:=10;
 else T:=15;
end;

Convert float to integer

I:=Fix(Y);

I:=trunc(Y);

 

II. Operators

Description

XPL Format

Pascal Equivalent

Not Equals

#

<>

Shift Left or Right

<< >>

shl shr

And Or

& !

and or

Integer Divide

/

div

String Constant

"Now is the time";

‘Now is the time’;

Meta-character

X:=^A;

X:=’A’;

III. Items with no direct Pascal Equivalent.

Description

XPL Format

Pascal Equivalent

Shortcut Begin/End

[ ];

Use begin/end

Conditional assignment

Z:=if X=20 then 25 else 30;

if X=20 then Z:=25 else Z:=30;

Return function results.

return X;

result:=X;
exit;

IV. Features Available in Pascal but not XPL. There are a large number of features available in Pascal that aren’t available in XPL. Here is an overview of the main features:

A. Structures. Structures allow you to create new variable types that combine one or more different data items into a single variable. For example, you could create a new variable type for dealing with geographic coordinates that combines degrees, minutes and seconds into a single variable:

type TGeographic = record
Degrees, Minutes, Seconds: double;
end;

You can then declare and manipulate degrees, minutes and second variables like this:

var MyHouse, YourHouse: Tgeographic;
MyHouse.Degrees:=104;
MyHouse.Minutes:=45;
MyHouse.Seconds:=10;
YourHouse:=MyHouse;

B. Objects. Objects are an extension of structures. They give you the ability to add subroutines to a data structure. Generally speaking, the subroutines are used to manipulate some aspect of the underlying data.

In this way, an object knows how to manipulate itself. For example, the Tgeographic object described above could have a subroutine that knows how to convert the Degree, Minutes and Seconds to Decimal Degrees. The syntax might look something like this:

X:=YourHouse.Decimal;

C. Components. Components are object that can be manipulated at design time. They are primarily used to create objects that will be visible on the screen. Because they can be manipulated while you are designing the screen layout, they save a lot of trial-and-error time getting the screen to look the way you want.

V. Other Issues.

A. Boolean Expressions. Boolean expressions in Pascal require explicit parenthesis to specify precedence. For example, the expression "if X=0 & Y=1 then" would compile in XPL but would have to be parenthesized to "if (X=0) and (Y=1) then" for it to compile in Pascal.

B. Case Variables. XPL allows case-statement values to be either constants or variables. Pascal only accepts constants. (This allows Pascal to optimize case-statements for speed.) If you are converting XPL code that has variables as the Case values, you must convert them to if-then-else structures.

C. For Loop Variables. In the most recent versions of Delphi, the for loop variable must be a local variable. This allows the code to be optimized for speed.

D. Float/Fix. In XPL, the Float function is used to convert integers to floating point numbers. Likewise, the Fix function is used convert floating point numbers to integers. In Pascal these conversion are handled automatically, so it is important to understand how Pascal treats these conversions.

1. Float. Since floating point numbers have a greater range and precision than integers, Pascal assumes that integers can be routinely converted to floating point number without any consequence. As a result, if you just assign an integer to a floating point variable, Pascal will automatically convert the integer to a real. For example, in the statement R:=I, Pascal will automatically convert I to a real number and store it in R.

2. Fix. On other hand, since floating point number have a greater range and precision than integer, Pascal will not allow you to assign a real variable to an integer. You must use the function Trunc or Round to convert the value to a real. For example, I := Trunc(R); truncates and integerizes R and store the result in I.

3. Mixed Expressions. In an expression that contains a mixture of integer and floating point values, Pascal looks at each term and decides which number type is expected and applies the rules and conversion necessary. For example, in the expression R := I1 / I2, Pascal knows that the result must be stored in the Real "R", so it converts "I1" and "I2" to reals and then performs the divide. On the other hand, in the expression R:= I1 div I2, Pascal does an integer divide first and then converts the result to a real number.

E. Variable References vs. Values. In most languages, values can be passed to a subroutine by reference or by value. When variables are passed by value, only the value is passed and so the original variable cannot be modified by the subroutine. When variables are passed by reference, the original variable can be modified by the subroutine. This is often used to return multiple values from a subroutine.

In XPL, all variables are passed to subroutines by value unless you use the address operator and an array. In Pascal, you can force to a variable to be passed by reference by using the var keyword. As an example, in the first procedure Frog is passed by value but in the second it is passed by reference:

procedure Test(Frog: integer); procedure Test(var Frog: integer);

In Pascal, passing by reference is handled using pointers, however the "pointer" aspect of the operation is hidden from the programming and handled by the compiler.

F. Pointer Arithmetic. Pascal has the same ability to work with pointers that C does. However, in most cases, it is best to use Pascal’s ability to pass values by reference instead of pointers. They accomplish the same thing and at the same time, they are simpler to use and understand. (Be aware that when you are working directly with the Windows API, there may be times where you will be forced to use pointer operations.)

Pointer Operators. In Pascal, there are two operators that are used to reference and dereference pointers:

Op.

Definition

Example

Description

@

Returns the address of a variable.

P:=@X;

Sets P to the address of X.

^

Returns the value pointed by a pointer.

Y:=P^;"

Returns the original value of X.

^

Is used to define a pointer variable.

var P: ^integer

Defines P as an integer pointer.

To help illustrate pointers, Pascal has two predefined simple pointer types that are available in Pascal:

type PByte = ^byte;

This pointer points to the address of byte in memory.

type TByteArray = array [0..32676] of byte;
type PByteArray = ^TbyteArray;

This is a pointer to an array of bytes.

These predefined pointers allows access bytes in memory in various ways. Once you learn to use these predefined types, you can declare other pointer types that can be accessed in the same way.

Accessing PByte values. This variable type points to a single byte variable. However, because it is a pointer, it is possible to access arrays of bytes by incrementing the pointer itself or by treating it as a pointer to an array:

Access Values

Point To Next Value

P^

Inc(P)

PbyteArray(P)[I]

Inc(I) or Inc(P)

Accessing PByteArray values. This type points to an array of bytes, so you can treat the variable as an array or as a pointer to an array.

Access Values

Point To Next Value

P[I] or P^[I]

Inc(I)

P[0]

Inc(PByte(P))

PByte(P)^

Inc(PByte(P))

 

G. Sets. Pascal also supports mathematical set operators. They are often useful for replacing sets of options based on bit-masks.

1. Set Declarations. Sets are declared using the "set of" operator:

var MyCars: set of (Ford,Chevy,VW,Porche,Audi);

Depending on your needs, you can also create "Set" types:

type TCars = set of (Ford,Chevy,VW,Porche,Audi);
var MyCars: Tcars;

Finally, you can create Sets based on separate enumerated types:

type TCarModels = (Ford,Chevy,VW,Porche,Audi);
type TCars = set of TCarModels
var MyCars: TCars;

2. Set Constructors. A particular set can be constructed by square brackets as follows:

MyCars:=[Ford,Chevy];

3. Set Operators. Just like other variables, Sets can be manipulated with their special operators. The following Set operators are available:

Operator

Operation

Operand types

Result type

Example

+

union

set

set

Set1 + Set2

-

difference

set

set

S - T

*

intersection

set

set

S * T

<=

subset

set

Boolean

Q <= MySet

>=

superset

set

Boolean

S1 >= S2

=

equality

set

Boolean

S2 = MySet

<>

inequality

set

Boolean

MySet <> S1

in

membership

ordinal, set

Boolean

A in Set1

The following rules apply to +, -, and *.

1. Union (+). The Union operator returns all items from both arguments. For example if A contains [Ford,Chevy] and B contains [Porche,VW], A + B will contain [Ford,Chevy,Porche,VW]. In formal language, "An ordinal O is in X + Y if and only if O is in X or Y (or both)."

2. Difference (-). The Difference operator removes from the first argument all items found in the second argument. For example, if the A contains [Ford,Chevy,Porche] and B contains [Chevy,Porche], A – B will contain [Ford]. In formal language, "O is in X - Y if and only if O is in X but not in Y."

3. Intersection. The Intersection operator only returns the items that are common to both arguments. For example, if the A contains [Ford,Chevy,Porche] and B contains [Chevy,Porche], A * B will contain [Chevy,Porche]. In formal language, "O is in X * Y if and only if O is in both X and Y.

4. Subset (<=). The Subset operator returns true if all items in the first argument can be found in the second argument. For example, if the A contains [Chevy,Porche] and B contains [Ford,Chevy,Porche], A >= B will return true because both [Chevy,Porche] exist in B. In more formal language, X <= Y is True just in case every member of X is a member of Y.

5. Superset (>=). The Super set is the reverse of the Subset described above. The Subset operator returns true if all items in the second argument can be found in the first. For example, if the A contains [Ford,Chevy,Porche] and B contains [Chevy,Porche], A <= B will return true because [Chevy,Porche] exist in A. In more formal language, Z >= W is equivalent to W <= Z.

6. Equality (=). The Equality operator returns true if the first argument contains exactly the same items as the second. For example, if A contains [Ford,Chevy,Porche] and B contains [Ford,Chevy,Porche] then A = B will return true.

7. Inequality (<>). The Inequality operator returns true if the first argument does not contain exactly the same items as the second. It is the opposite of the Equality operator described above.

8. Membership (In). The Membership operator requires a Set Item and a Set as arguments. It returns true if the Set Item is found in the Set. For example, if B contained [Ford,Chevy,Porche], then the expression "Chevy In B" would return true. In more formal language, for an ordinal O and a set S, O in S is True just in case O is a member of S.