Previous Next Contents Index Doc Set Home


Pascal Preprocessor

A


This appendix describes the preprocessors, cpp(1) and cppas.


cpp-

cpp(1) is the C language preprocessor. Pascal runs your source program through cpp(1) when you compile it without the -xl option. For a complete description of cpp(1), see the Solaris documentation.


cppas-

The cppas preprocessor handles the Pascal conditional variables and compiler directives. You call cppas using the -xl option.

Conditional Variables

A conditional variable is defined when it appears in a %var directive; otherwise, it is undefined. In addition, we predefine:

__sun

__SVR4

sparc

__sparc

__SUNPRO_PC=0x400

unix

__unix

sun

These variables are not predefined when you use -s0, -s1, -V0, or -V1.

A defined conditional variable is enabled (true) when it appears in either the %enable directive or in the -config option; otherwise, it is disabled (false), as in:

%var one two
%enable two

The following section describes %var and %enable. Programs that contain conditional variables must be compiled with the -xl option.

Compiler Directives

A directive indicates some action for the compiler to take. You can use a directive anywhere in your program.

Each directive consists of a percent sign (%) followed by the directive name. Programs that contain compiler directives must be compiled with the -xl option.

Table A-1 summarizes the compiler directives.

Table  A-1 cppas Compiler Directives  

Compiler Directive
Description

%config

Sets a special predefined conditional variable with a value of either true or false.

%debug

Instructs pc to compile this line of code when you use the -cond compiler directive.

%else

If expression in %if expression %then is false, the compiler skips over the %then part and executes the %else part instead.

%elseif

If expression in %if expression %then is false, the compiler skips over the %then part and executes the %elseif part instead. Similar to %else.

%elseifdef

If expression in %ifdef expression %then is false, the compiler skips over the %then part and executes the %elseifdef part instead.

%enable

Sets a conditional variable to true.

%endif

Indicates the end of an %if or %ifdef directive.

%error

Prints a string on the standard output and treats it as an error.

%exit

Stops processing the current Pascal source file.

%if

When the compiler encounters a %if expression %then directive, it evaluates expression. If expression is true, pc executes the statements after %then. If expression is false, pc skips over %then.

%ifdef

Determines whether or not you previously defined a conditional variable in a %var directive.

%include

Inserts the lines from the specified file into the input stream.

%list

Enables a listing of the program.

%nolist

Disables the program listing.

%slibrary

Inserts the lines from the specified file into the input stream. Same as %include.

%var

Defines conditional variables.

%warning

Prints a warning string on the standard output.

The rest of this appendix contains detailed descriptions and examples of each directive.

The %config Directive

The %config directive is a predefined conditional variable with a value of either true or false.

Syntax
%config

Comments
%config is true when you compile your program with the -config option; otherwise, %config is false.

Use %config in an %if, %ifdef, %elseif, or %elseifdef directive to catch any undefined values specified with -config. Do not define %config in the %var directive.

Example

The Pascal program, config.p, which defines the conditional variables one and two

program config_example(output);

{ This program demonstrates the use of the
  %config compiler directive. }

var 
    a: integer := maxint;
    b: integer := minint;

%var one two 

begin
    writeln('Begin program.');
    %if one %then
        writeln('One is defined as ', a:2, '.');
    %elseif two %then
        writeln('Two is defined as ', b:2, '.');
    %elseif %config %then
        writeln('Nothing is defined.');
    %endif
    writeln('End program.')
end. { config_example }

The output when you compile config.p without the
-
config option

hostname% pc -xl config.p
hostname% a.out
Begin program.
End program.

The output when you define the variable one

hostname% pc -xl -config one config.p
hostname% a.out
Begin program.
One is defined as 32767.
End program.

The output when you define two

hostname% pc -xl -config two config.p
hostname% a.out
Begin program.
Two is defined as -32768.
End program.

The output when you define foo

hostname% pc -xl -config foo config.p
Fri Mar 3 15:22 1995 config.p

Error: -CONFIG command argument foo was never declared.
Compilation failed

The %debug Directive

The %debug directive instructs pc to compile this line of code when you use the -cond compiler directive.

Syntax
%debug;

Comments
The %debug directive works in conjunction with the -cond compiler option.
-cond causes pc to compile the lines in your program that begin with %debug. Without -cond, pc treats lines with %debug as comments.

Example

The Pascal program, debug.p

program debug_example(output);

{ This program demonstrates the use of the
  %debug compiler directive. }

begin
    writeln ('Hello, how are you?');
    %debug;  writeln ('Fine, thank you.');
end. { debug example }

The output when you compile debug.p without the -cond option

hostname% pc -xl debug.p
hostname% a.out
Hello, how are you?

The output when you use -cond

hostname% pc -xl -cond debug.p
hostname% a.out
Hello, how are you?
Fine, thank you.

The %else Directive

The %else directive provides an alternative action to the %if directive.

Syntax
%if expression %then
.
.
%else
.
.
%endif

Example

The Pascal program, if_then_else.p

program if_then_else (output);
%var red
begin
%if red %then
	writeln ('It is red.');
%else
	writeln ('It is not red.')
%endif
end.

The output when you compile if_then_else.p without the
-config

hostname% pc -xl if_then_else.p
hostname% a.out
It is not red.

The output when you supply
-config with the argument red

hostname% pc -xl -config red if_then_else.p
hostname% a.out
It is red.

The %elseif Directive

The %elseif directive provides an alternative action to the %if directive.

Syntax
%if expression %then
.
.
%elseif expression %then
.
.
%endif

Comments
If the expression in %if expression %then is false, pc skips over the %then part and executes the %elseif part instead. expression consists of a conditional variable and the optional boolean operators, and, or, and not. See the %else listing for examples of expression.

Example

The Pascal program, elseif.p

program elseif_example(output);

{ This program demonstrates the use of the
   %if, %then, and %elseif directives. }

%var blue red

begin
    %if blue %then
        writeln('The color is blue.');
    %elseif red %then
        writeln('The color is red.');
    %endif
end. { elseif_example }

The output when you supply
-config with the argument blue

hostname% pc -xl -config blue elseif.p
hostname% a.out
The color is blue.

The output when you supply
-config with the argument red

hostname% pc -xl -config red elseif.p
hostname% a.out
The color is red.

The %elseifdef Directive

The %elseifdef directive provides an alternative action to the %ifdef directive.

Syntax
%ifdef expression %then
.
.
%elseifdef expression %then
.
.
%endif

Comments
If the expression in %ifdef expression %then is false, pc skips over the %then part and executes the %elseifdef part instead. expression consists of a conditional variable and the optional boolean operators, and, or, and not. See the %else listing for examples of expression.

Example

The Pascal program, ifdef.p, which first checks if bird1 has been defined. If not, it defines it with a %var directive. If bird1 has been defined, the program checks whether or not it needs to define bird2.

program ifdef_example(output);

%include 'bird.h';

begin
    %ifdef not(bird1) %then
          %var bird1
    %elseifdef not(bird2) %then
          %var bird2
    %endif;

    %if bird1 %then
        writeln('Bird one is a ', a, '.');
    %elseif bird2 %then
        writeln('Bird two is a ', b, '.')
    %endif
end.  { ifdef_example }

The include file, bird.h

var
    a: array[1..7] of char := 'penguin';
    b: array[1..6] of char := 'toucan';

%var bird1

The output when you enable bird1 with the -config option

hostname% pc -xl -config bird1 ifdef.p
hostname% a.out
Bird two is a penguin.

The output when you enable bird2 with the -config option

hostname% pc -xl -config bird2 ifdef.p
hostname% a.out
Bird two is a toucan.

The %enable Directive

The %enable directive sets a conditional variable to true.

Syntax
%enable var1 ..., varN

Comments
A defined conditional variable is enable (true) when it appears in either the %enable directive or in the -config option. Conditional variables are false by default.

Example

The Pascal program, enable.p. This example sets the conditional variable two to true, which is equivalent to setting the -config option to two on the command-line.

program enable_example(output);

{ This program demonstrates the use of
   the %enable compiler directive. }

var 
    a: integer;
    b: integer;

%var one, two
%enable two

begin
    %if one %then
        a := maxint;
        writeln('One is defined as ', a:2, '.');
    %endif
    %if two %then
        b := minint;
        writeln('Two is defined as ', b:2, '.');
    %endif
end. { enable_example }

The commands to compile and output enable.p

hostname% pc -xl enable.p
hostname% a.out
Two is defined as -32768.

The %endif Directive

The %endif directive indicates the end of a %if or %ifdef directive. See the sections on %if and %ifdef for more information on this directive.

The %error Directive

The %error directive causes the compiler to print a string on the standard output and treat it as an error.

Syntax
%error 'string'

Comments
pc does not produce an object file.

Example

The Pascal program, error.p

program error_example(output);

{ This program demonstrates the use of the
  %error compiler directive. }

%var arch

begin
    %if arch %then
        writeln('This is a SPARC computer.');
    %else
        %error 'Unknown architecture.'
    %endif
end. { error_example }

error.p produces this error if you compile it without the
-config sparc option.

hostname% pc -xl error.p
Tue Feb 28 17:10 1995 error.p

Line 12 :			%error 'Unknown architecture.'
E --------------------^---'Unknown architecture.'
Compilation failed

The %exit Directive

The %exit directive instructs the compiler to stop processing the current Pascal source file.

Syntax
%exit

Comments
If the compiler encounters an %exit directive within an include file, it stops processing the include file, but continues processing the source file in which it is included. In effect, %exit is equivalent to an end-of-file marker.

When the compiler processes an %exit directive within an %if or %ifdef construct, it closes all %if or %ifdefs before it stops processing the current file.

Example

The Pascal program, exit_directive.p

program exit_directive(output);

begin
    writeln('Hello, world!')
end. { exit_directive }
%exit
Everything after the %exit is ignored.
So you can put anything here.

The commands to compile and execute exit_directive.p

hostname% pc -xl exit_directive.p
hostname% a.out
Hello, world!

The %if Directive

The %if directive is a conditional branching directive.

Syntax
%if expression %then
.
.
%end if

Comments
When pc encounters a %if directive, it evaluates expression. If expression is true, pc executes the statements in the %then part. If expression is false, pc skips over the %then part and executes the %else, %elseif, or %endif directive. If no such directive exists, pc proceeds to the next statement.

The expression consists of a conditional variable and the optional boolean operators and, or, and not. You can set a conditional variable on the command-line by using the -config option. See "-config" on page 26 for information on this option.

Assuming one and two are conditional variables, expression can be any of the following:

	one
two
one and two
one or two
not one
not two
Example
See the example in the %else listing on page 258.

The %ifdef Directive

The %ifdef directive determines whether or not you previously defined a conditional variable in a %var directive.

Syntax
%ifdef expression %then
.
.
%elseifdef expression %then
.
.
%endif

Comments
expression consists of a conditional variable and the optional boolean operators and, or, and not. See the %else listing for examples of expression.

%ifdef is especially useful for determining whether or not a conditional variable has been declared in an include file.

Example
See the example in "The %elseifdef Directive."

The %include Directive

The %include directive inserts lines from the specified file in the input stream.

Syntax
%include 'filename';

Comments
When cppas encounters the %include directive, it inserts the lines from the file name into the input stream.

Example

The program unit, include_prog.p

program include_prog;

%include 'extern.h';

begin
    global := 1;
    writeln('From MAIN, before PROC: ',global);
    proc;
    writeln('From MAIN,  after PROC: ',global);
end. { include_prog }

The module unit,

include_mod.p

module include_mod;

define
    global, proc;

%include 'extern.h';

procedure proc; 

begin
    writeln('From PROC             : ',global);
    global := global + 1;
end; { proc }

The include file, include.h

var
    global : integer;

procedure proc; extern;

The commands to compile and execute ext_prog.p and ext_mod.p

hostname% pc -xl include_prog.p include_mod.p
include_prog.p:
include_mod.p:
Linking:
hostname% a.out
From MAIN, before PROC:					1
From PROC			     :		1
From MAIN,  after PROC:2

The %list Directive

The %list directive enables a listing of the program.

Syntax
%list;

Comments
The %list directive and the -l compiler option perform the same function.

Example

The Pascal program, list.p

program list_example(output);

{ This program demonstrates the use of the %list
  and %nolist directives. }

%list;
%include 'types.h';
%nolist;

begin
    pri := [red, yellow, blue];
    pos := [true, false];
    cap := ['A'..'Z'];
    dig := [0..100];
	writeln('There are ',	card(pri): 4, '	primary colors.');
	writeln('There are ',	card(pos): 4, '	possibilities.');
	writeln('There are ',	card(cap): 4, '	capital letters.'');
	writeln('There are ',	card(dig): 4, '	digits.')
end. { list_example }

The include file, types.h

type
    lowints = 0..100;
    primary_colors = set of (red, yellow, blue);
    possibilities = set of boolean;
    capital_letters = set of 'A'..'Z';
    digits = set of lowints;

var
    pri: primary_colors;
    pos: possibilities;
    cap: capital_letters;
    dig: digits;

The listing includes the time each unit was compiled and the name of each unit compiled.

hostname% pc -xl list.p
Tue Feb 28 15:48 1995  list.p:
	6  %list;
Tue Feb 28 15:50 1995  ./types.h:
	1  type
	2	lowints = 0..100;
	3	primary_colors = set of (red, yellow, blue);
	4	possibilities = set of boolean;
	5	capital_letters = set of 'A'..'Z';
	6	digits = set of lowints;

	8  var
	9	pri: primary_colors;
	10	pos: possibilities;
	11	cap: capital_letters;
	12	dig: digits;
Tue Feb 28 15:52 1995  list.p:
	7  %include 'types.h';

hostname% a.out
	There are    3 primary colors
	There are    2 possibilities 
	There are    26 capital letters
	There are    101 digits

The %nolist Directive

The %nolist directive disables the program listing.

Syntax
%nolist;

Comments
%nolist is the default.

Example
See the example under "The %list Directive."

The %slibrary Directive

cppas treats %slibrary in the same manner as the %include directive. See "The %include Directive" on page 267.

The %var Directive

The %var directive defines conditional variables for the preprocessor.

Syntax
%var var1 ..., varN

Comments
A conditional variable is defined when it appears in a %var directive; otherwise, it is undefined.

Example
See the example under "The %config Directive" on page 255.

The %warning Directive

The %warning directive instructs pc to print a string on the standard output as a compiler warning.

Syntax
%warning 'string'

Comments
pc produces an object file.

Example

The Pascal program,
warning.p

program warning_example(output);

{ This program demonstrates the use of the
  %warning compiler directives. }

%var blue

begin
    %if blue %then
        writeln('The color is blue.');
    %else
        %warning 'Color not defined'
    %endif
end. { warning_example }

The output when you compile warning.p without the
-config option

hostname% pc -xl warning.p
Fri Mar 3 15:03 1995 warning.p

Line 12:	%warning 'Color not defined'
w -------------------^----- 'Architecture not defined'


Previous Next Contents Index Doc Set Home