BSD 4_3 release
[unix-history] / usr / doc / ps1 / 04.pascal / pumanA.n
CommitLineData
38b87663
KM
1.\" Copyright (c) 1980 Regents of the University of California.
2.\" All rights reserved. The Berkeley software License Agreement
3.\" specifies the terms and conditions for redistribution.
4.\"
95f51977 5.\" @(#)pumanA.n 6.2 (Berkeley) 5/7/86
38b87663 6.\"
38b87663
KM
7.if !\n(xx \{\
8.so tmac.p \}
9.ND
10.nr H1 0
11.af H1 A
12.NH
13Appendix to Wirth's Pascal Report
14.PP
15This section is an appendix to
16the definition of the Pascal language in Niklaus Wirth's
17.I "Pascal Report"
18and, with that Report, precisely defines the
19Berkeley
20implementation.
21This appendix includes a summary of extensions to the language,
22gives the ways in which the undefined specifications were resolved,
23gives limitations and restrictions of the current implementation,
24and lists the added functions and procedures available.
25It concludes with a list of differences with the commonly available
26Pascal 6000\-3.4 implementation,
27and some comments on standard and portable Pascal.
28.NH 2
29Extensions to the language Pascal
30.PP
31This section defines non-standard language constructs available in
32.UP .
33The
34.B s
35standard Pascal option of the translators
36.PI
37and
38.PC
39can be used to detect these extensions in programs which are to be transported.
40.SH
41String padding
42.PP
43.UP
44will pad constant strings with blanks in expressions and as
45value parameters to make them as long as is required.
46The following is a legal
47.UP
48program:
49.LS
50\*bprogram\fP x(output);
51\*bvar\fP z : \*bpacked\fP \*barray\fP [ 1 .. 13 ] \*bof\fP char;
52\*bbegin\fP
53 z := 'red';
54 writeln(z)
55\*bend\fP;
56.LE
57The padded blanks are added on the right.
58Thus the assignment above is equivalent to:
59.LS
60z := 'red '
61.LE
62which is standard Pascal.
63.SH
64Octal constants, octal and hexadecimal write
65.PP
66Octal constants may be given as a sequence of octal digits followed
67by the character `b' or `B'.
68The forms
69.LS
70write(a:n \*boct\fP)
71.LE
72and
73.LS
74write(a:n \*bhex\fP)
75.LE
76cause the internal representation of
77expression
78.I a,
79which must be Boolean, character, integer, pointer, or a user-defined enumerated
80type,
81to be written in octal or hexadecimal respectively.
82.SH
83Assert statement
84.PP
85An
86.B assert
87statement causes a
88.I Boolean
89expression to be evaluated
90each time the statement is executed.
91A runtime error results if any of the expressions evaluates to be
92.I false .
93The
94.B assert
95statement is treated as a comment if run-time tests are disabled.
96The syntax for
97.B assert
98is:
99.LS
100\*bassert\fP <expr>
101.LE
102.SH
103Enumerated type input-output
104.PP
105Enumerated types may be read and written.
106On output the string name associated with the enumerated
107value is output.
108If the value is out of range,
109a runtime error occurs.
110On input an identifier is read and looked up
111in a table of names associated with the
112type of the variable, and
113the appropriate internal value is assigned to the variable being
114read.
115If the name is not found in the table
116a runtime error occurs.
117.SH
118Structure returning functions
119.PP
120An extension has been added which allows functions
121to return arbitrary sized structures rather than just
122scalars as in the standard.
123.SH
124Separate compilation
125.PP
126The compiler
127.PC
128has been extended to allow separate compilation of programs.
129Procedures and functions declared at the global level
130may be compiled separately.
131Type checking of calls to separately compiled routines is performed
132at load time to insure that the program as a whole
133is consistent.
134See section 5.10 for details.
135.NH 2
136Resolution of the undefined specifications
137.SH
138File name \- file variable associations
139.PP
140Each Pascal file variable is associated with a named
141.UX
142file.
143Except for
144.I input
145and
146.I output,
147which are
148exceptions to some of the rules, a name can become associated
149with a file in any of three ways:
150.IP "\ \ \ \ \ 1)" 10
151If a global Pascal file variable appears in the
152.B program
153statement
154then it is associated with
155.UX
156file of the same name.
157.IP "\ \ \ \ \ 2)"
158If a file was reset or rewritten using the
159extended two-argument form of
160.I reset
161or
162.I rewrite
163then the given name
164is associated.
165.IP "\ \ \ \ \ 3)"
166If a file which has never had
167.UX
168name associated
169is reset or rewritten without specifying a name
170via the second argument, then a temporary name
171of the form `tmp.x'
172is associated with the file.
173Temporary names start with
174`tmp.1' and continue by incrementing the last character in the
175.SM
176USASCII
177.NL
178ordering.
179Temporary files are removed automatically
180when their scope is exited.
181.SH
182The program statement
183.PP
184The syntax of the
185.B program
186statement is:
187.LS
188\*bprogram\fP <id> ( <file id> { , <file id > } ) ;
189.LE
190The file identifiers (other than
191.I input
192and
193.I output )
194must be declared as variables of
195.B file
196type in the global declaration part.
197.SH
198The files input and output
199.PP
200The formal parameters
201.I input
202and
203.I output
204are associated with the
205.UX
206standard input and output and have a
207somewhat special status.
208The following rules must be noted:
209.IP "\ \ \ \ \ 1)" 10
210The program heading
211.B must
212contains the formal parameter
213.I output.
214If
215.I input
216is used, explicitly or implicitly, then it must
217also be declared here.
218.IP "\ \ \ \ \ 2)"
219Unlike all other files, the
220Pascal files
221.I input
222and
223.I output
224must not be defined in a declaration,
225as their declaration is automatically:
226.LS
227\*bvar\fP input, output: text
228.LE
229.IP "\ \ \ \ \ 3)"
230The procedure
231.I reset
232may be used on
233.I input.
234If no
235.UX
236file name has ever been associated with
237.I input,
238and no file name is given, then an attempt will be made
239to `rewind'
240.I input.
241If this fails, a run time
242error will occur.
243.I Rewrite
244calls to output act as for any other file, except that
245.I output
246initially has no associated file.
247This means that a simple
248.LS
249rewrite(output)
250.LE
251associates a temporary name with
252.I output.
253.SH
254Details for files
255.PP
256If a file other than
257.I input
258is to be read,
259then reading must be initiated by a call to the
260procedure
261.I reset
262which causes the Pascal system to attempt to open the
263associated
264.UX
265file for reading.
266If this fails, then a runtime error occurs.
267Writing of a file other than
268.I output
269must be initiated by a
270.I rewrite
271call,
272which causes the Pascal system to create the associated
273.UX
274file and
275to then open the file for writing only.
276.SH
277Buffering
278.PP
279The buffering for
280.I output
281is determined by the value of the
282.B b
283option
284at the end of the
285.B program
286statement.
287If it has its default value 1,
288then
289.I output
290is
291buffered in blocks of up to 512 characters,
292flushed whenever a writeln occurs
293and at each reference to the file
294.I input.
295If it has the value 0,
296.I output
297is unbuffered.
298Any value of
2992 or more gives block buffering without line or
300.I input
301reference flushing.
302All other output files are always buffered in blocks of 512 characters.
303All output buffers are flushed when the files are closed at scope exit,
304whenever the procedure
305.I message
306is called, and can be flushed using the
307built-in procedure
308.I flush.
309.PP
310An important point for an interactive implementation is the definition
311of `input\(ua'.
312If
313.I input
314is a teletype, and the Pascal system reads a character at the beginning
315of execution to define `input\(ua', then no prompt could be printed
316by the program before the user is required to type some input.
317For this reason, `input\(ua' is not defined by the system until its definition
318is needed, reading from a file occurring only when necessary.
319.SH
320The character set
321.PP
322Seven bit
323.SM USASCII
324is the character set used on
325.UX .
326The standard Pascal
327symbols `and', 'or', 'not', '<=', '>=', '<>',
328and the uparrow `\(ua' (for pointer qualification)
329are recognized.\*(dg
330.FS
331\*(dgOn many terminals and printers, the up arrow is represented
332as a circumflex `^'.
333These are not distinct characters, but rather different graphic
334representations of the same internal codes.
335.FE
336Less portable are the
337synonyms tilde `~'
338for
339.B not ,
340`&' for
341.B and ,
342and `|' for
343.B or .
344.PP
345Upper and lower case are considered to be distinct.\*(st
346.FS
347\*(stThe proposed standard for Pascal considers them to be the same.
348.FE
349Keywords and built-in
350.B procedure
351and
352.B function
353names are
354composed of all lower case letters.
355Thus the identifiers GOTO and GOto are distinct both from each other and
356from the keyword
357\*bgoto\fP.
358The standard type `boolean' is also available as `Boolean'.
359.PP
360Character strings and constants may be delimited by the character
361`\''
362or by the character `#';
363the latter is sometimes convenient when programs are to be transported.
364Note that the `#' character has special meaning
365.up
366when it is the first character on a line \- see
367.I "Multi-file programs"
368below.
369.SH
370The standard types
371.PP
372The standard type
373.I integer
374is conceptually defined as
375.LS
376\*btype\fP integer = minint .. maxint;
377.LE
378.I Integer
379is implemented with 32 bit twos complement arithmetic.
380Predefined constants of type
381.I integer
382are:
383.LS
384\*bconst\fP maxint = 2147483647; minint = -2147483648;
385.LE
386.PP
387The standard type
388.I char
389is conceptually defined as
390.LS
391\*btype\fP char = minchar .. maxchar;
392.LE
393Built-in character constants are `minchar' and `maxchar', `bell' and `tab';
394ord(minchar) = 0, ord(maxchar) = 127.
395.PP
396The type
397.I real
398is implemented using 64 bit floating point arithmetic.
399The floating point arithmetic is done in `rounded' mode, and
400provides approximately 17 digits of precision
401with numbers as small as 10 to the negative 38th power and as large as
40210 to the 38th power.
403.SH
404Comments
405.PP
406Comments can be delimited by either `{' and `}' or by `(*' and `*)'.
407If the character `{' appears in a comment delimited by `{' and `}',
408a warning diagnostic is printed.
409A similar warning will be printed if the sequence `(*' appears in
410a comment delimited by `(*' and `*)'.
411The restriction implied by this warning is not part of standard Pascal,
412but detects many otherwise subtle errors.
413.SH
414Option control
415.PP
416Options of the translators may be controlled
417in two distinct ways.
418A number of options may appear on the command line invoking the translator.
419These options are given as one or more strings of letters preceded by the
420character `\-' and cause the default setting of
421each given option to be changed.
422This method of communication of options is expected to predominate
423for
424.UX .
425Thus the command
426.LS
427% \*bpi \-l \-s foo.p\fR
428.LE
429translates the file foo.p with the listing option enabled (as it normally
430is off), and with only standard Pascal features available.
431.PP
432If more control over the portions of the program where options are enabled is
433required, then option control in comments can and should be used.
434The
435format for option control in comments is identical to that used in Pascal
4366000\-3.4.
437One places the character `$' as the first character of the comment
438and follows it by a comma separated list of directives.
439Thus an equivalent to the command line example given above would be:
440.LS
441{$l+,s+ listing on, standard Pascal}
442.LE
443as the first line of the program.
444The `l'
445option is more appropriately specified on the command line,
446since it is extremely unlikely in an interactive environment
447that one wants a listing of the program each time it is translated.
448.PP
449Directives consist of a letter designating the option,
450followed either by a `+' to turn the option on, or by a `\-' to turn the
451option off.
452The
453.B b
454option takes a single digit instead of
455a `+' or `\-'.
456.SH
457Notes on the listings
458.PP
459The first page of a listing
460includes a banner line indicating the version and date of generation of
461.PI
462or
463.PC .
464It also
465includes the
466.UX
467path name supplied for the source file and the date of
468last modification of that file.
469.PP
470Within the body of the listing, lines are numbered consecutively and
471correspond to the line numbers for the editor.
472Currently, two special
473kinds of lines may be used to format the listing:
474a line consisting of a form-feed
475character, control-l, which causes a page
476eject in the listing, and a line with
477no characters which causes the line number to be suppressed in the listing,
478creating a truly blank line.
479These lines thus correspond to `eject' and `space' macros found in many
480assemblers.
481Non-printing characters are printed as the character `?' in the listing.\*(dg
482.FS
483\*(dgThe character generated by a control-i indents
484to the next `tab stop'.
485Tab stops are set every 8 columns in
486.UX .
487Tabs thus provide a quick way of indenting in the program.
488.FE
489.SH
490The standard procedure write
491.PP
492If no minimum field length parameter is specified
493for a
494.I write,
495the following default
496values are assumed:
497.KS
498.TS
499center;
500l n.
501integer 10
502real 22
503Boolean length of `true' or `false'
504char 1
505string length of the string
506oct 11
507hex 8
508.TE
509.KE
510The end of each line in a text file should be explicitly
511indicated by `writeln(f)', where `writeln(output)' may be written
512simply as `writeln'.
513For
514.UX ,
515the built-in function `page(f)' puts a single
516.SM ASCII
517form-feed character on the output file.
518For programs which are to be transported the filter
519.I pcc
520can be used to interpret carriage control, as
521.UX
522does not normally do so.
523.NH 2
524Restrictions and limitations
525.SH
526Files
527.PP
528Files cannot be members of files or members of dynamically
529allocated structures.
530.SH
531Arrays, sets and strings
532.PP
533The calculations involving array subscripts and set elements
534are done with 16 bit arithmetic.
535This
536restricts the types over which arrays and sets may be defined.
537The lower bound of such a range must be greater than or equal to
538\-32768, and the upper bound less than 32768.
539In particular, strings may have any length from 1 to 65535 characters,
540and sets may contain no more than 65535 elements.
541.SH
542Line and symbol length
543.PP
544There is no intrinsic limit on the length of identifiers.
545Identifiers
546are considered to be distinct if they differ
547in any single position over their entire length.
548There is a limit, however, on the maximum input
549line length.
550This limit is quite generous however, currently exceeding 160
551characters.
552.SH
553Procedure and function nesting and program size
554.PP
555At most 20 levels of
556.B procedure
557and
558.B function
559nesting are allowed.
560There is no fundamental, translator defined limit on the size of the
561program which can be translated.
562The ultimate limit is supplied by the
563hardware and thus, on the \s-2PDP\s0-11,
564by the 16 bit address space.
565If
566one runs up against the `ran out of memory' diagnostic the program may yet
567translate if smaller procedures are used, as a lot of space is freed
568by the translator at the completion of each
569.B procedure
570or
571.B function
572in the current
573implementation.
574.PP
575On the \s-2VAX\s0-11, there is an implementation defined limit
576of 65536 bytes per variable.
577There is no limit on the number of variables.
578.SH
579Overflow
580.PP
581There is currently no checking for overflow on arithmetic operations at
582run-time on the \s-2PDP\s0-11.
583Overflow checking is performed on the \s-2VAX\s0-11 by the hardware.
584.br
585.ne 15
586.NH 2
587Added types, operators, procedures and functions
588.SH
589Additional predefined types
590.PP
591The type
592.I alfa
593is predefined as:
594.LS
595\*btype\fP alfa = \*bpacked\fP \*barray\fP [ 1..10 ] \*bof\fP \*bchar\fP
596.LE
597.PP
598The type
599.I intset
600is predefined as:
601.LS
602\*btype\fP intset = \*bset of\fP 0..127
603.LE
604In most cases the context of an expression involving a constant
605set allows the translator to determine the type of the set, even though the
606constant set itself may not uniquely determine this type.
607In the
608cases where it is not possible to determine the type of the set from
609local context, the expression type defaults to a set over the entire base
610type unless the base type is integer\*(dg.
611.FS
612\*(dgThe current translator makes a special case of the construct
613`if ... in [ ... ]' and enforces only the more lax restriction
614on 16 bit arithmetic given above in this case.
615.FE
616In the latter case the type defaults to the current
617binding of
618.I intset,
619which must be ``type set of (a subrange of) integer'' at that point.
620.PP
621Note that if
622.I intset
623is redefined via:
624.LS
625\*btype\fP intset = \*bset of\fP 0..58;
626.LE
627then the default integer set is the implicit
628.I intset
629of
630Pascal 6000\-3.4
631.SH
632Additional predefined operators
633.PP
634The relationals `<' and `>' of proper set
635inclusion are available.
636With
637.I a
638and
639.I b
640sets, note that
641.LS
642(\*bnot\fR (\fIa\fR < \fIb\fR)) <> (\fIa\fR >= \fIb\fR)
643.LE
644As an example consider the sets
645.I a
646= [0,2]
647and
648.I b
649= [1].
650The only relation true between these sets is `<>'.
651.SH
652Non-standard procedures
653.IP argv(i,a) 25
654where
655.I i
656is an integer and
657.I a
658is a string variable
659assigns the (possibly truncated or blank padded)
660.I i \|'th
661argument
662of the invocation of the current
663.UX
664process to the variable
665.I a .
666The range of valid
667.I i
668is
669.I 0
670to
671.I argc\-1 .
672.IP date(a)
673assigns the current date to the alfa variable
674.I a
675in the format `dd mmm yy ', where `mmm' is the first
676three characters of the month, i.e. `Apr'.
677.IP flush(f)
678writes the output buffered for Pascal file
679.I f
680into the associated
681.UX
682file.
683.IP halt
684terminates the execution of the program with
685a control flow backtrace.
686.IP linelimit(f,x)\*(dd
687.FS
688\*(ddCurrently ignored by pdp-11
689.X .
690.FE
691with
692.I f
693a textfile and
694.I x
695an integer expression
696causes
697the program to be abnormally terminated if more than
698.I x
699lines are
700written on file
701.I f .
702If
703.I x
704is less than 0 then no limit is imposed.
705.IP message(x,...)
706causes the parameters, which have the format of those
707to the
708built-in
709.B procedure
710.I write,
711to be written unbuffered on the diagnostic unit 2,
712almost always the user's terminal.
713.IP null
714a procedure of no arguments which does absolutely nothing.
715It is useful as a place holder,
716and is generated by
717.XP
718in place of the invisible empty statement.
719.IP remove(a)
720where
721.I a
722is a string causes the
723.UX
724file whose
725name is
726.I a,
727with trailing blanks eliminated, to be removed.
728.IP reset(f,a)
729where
730.I a
731is a string causes the file whose name
732is
733.I a
734(with blanks trimmed) to be associated with
735.I f
736in addition
737to the normal function of
738.I reset.
739.IP rewrite(f,a)
740is analogous to `reset' above.
741.IP stlimit(i)
742where
743.I i
744is an integer sets the statement limit to be
745.I i
746statements.
747Specifying the
748.B p
749option to
750.I pc
751disables statement limit counting.
752.IP time(a)
753causes the current time in the form `\ hh:mm:ss\ ' to be
754assigned to the alfa variable
755.I a.
756.SH
757Non-standard functions
758.IP argc 25
759returns the count of arguments when the Pascal program
760was invoked.
761.I Argc
762is always at least 1.
763.IP card(x)
764returns the cardinality of the set
765.I x,
766i.e. the
767number of elements contained in the set.
768.IP clock
769returns an integer which is the number of central processor
770milliseconds of user time used by this process.
771.IP expo(x)
772yields the integer valued exponent of the floating-point
773representation of
774.I x ;
775expo(\fIx\fP) = entier(log2(abs(\fIx\fP))).
776.IP random(x)
777where
778.I x
779is a real parameter, evaluated but otherwise
780ignored, invokes a linear congruential random number generator.
781Successive seeds are generated as (seed*a + c) mod m and
782the new random number is a normalization of the seed to the range 0.0 to 1.0;
783a is 62605, c is 113218009, and m is
784536870912.
785The initial seed
786is 7774755.
787.IP seed(i)
788where
789.I i
790is an integer sets the random number generator seed
791to
792.I i
793and returns the previous seed.
794Thus seed(seed(i))
795has no effect except to yield value
796.I i.
797.IP sysclock
798an integer function of no arguments returns the number of central processor
799milliseconds of system time used by this process.
800.IP undefined(x)
801a Boolean function.
802Its argument is a real number and
803it always returns false.
804.IP wallclock
805an integer function of no arguments returns the time
806in seconds since 00:00:00 GMT January 1, 1970.
807.NH 2
808Remarks on standard and portable Pascal
809.PP
810It is occasionally desirable to prepare Pascal programs which will be
811acceptable at other Pascal installations.
812While certain system dependencies are bound to creep in,
813judicious design and programming practice can usually eliminate
814most of the non-portable usages.
815Wirth's
816.I "Pascal Report"
817concludes with a standard for implementation and program exchange.
818.PP
819In particular, the following differences may cause trouble when attempting
820to transport programs between this implementation and Pascal 6000\-3.4.
821Using the
822.B s
823translator option may serve to indicate many problem areas.\*(dg
824.FS
825\*(dgThe
826.B s
827option does not, however, check that identifiers differ
828in the first 8 characters.
829.I Pi
830and
831.PC
832also do not check the semantics of
833.B packed .
834.FE
835.SH
836Features not available in Berkeley Pascal
837.IP
838Segmented files and associated functions and procedures.
839.IP
840The function
841.I trunc
842with two arguments.
843.IP
844Arrays whose indices exceed the capacity of 16 bit arithmetic.
845.SH
846Features available in Berkeley Pascal but not in Pascal 6000-3.4
847.IP
848The procedures
849.I reset
850and
851.I rewrite
852with file names.
853.IP
854The functions
855.I argc,
856.I seed,
857.I sysclock,
858and
859.I wallclock.
860.IP
861The procedures
862.I argv,
863.I flush,
864and
865.I remove.
866.IP
867.I Message
868with arguments other than character strings.
869.IP
870.I Write
871with keyword
872.B hex .
873.IP
874The
875.B assert
876statement.
877.IP
878Reading and writing of enumerated types.
879.IP
880Allowing functions to return structures.
881.IP
882Separate compilation of programs.
883.IP
884Comparison of records.
885.SH
886Other problem areas
887.PP
888Sets and strings are more general in \*
889.UP ;
890see the restrictions given in
891the
892Jensen-Wirth
893.I "User Manual"
894for details on the 6000\-3.4 restrictions.
895.PP
896The character set differences may cause problems,
897especially the use of the function
898.I chr,
899characters as arguments to
900.I ord,
901and comparisons of characters,
902since the character set ordering
903differs between the two machines.
904.PP
905The Pascal 6000\-3.4 compiler uses a less strict notion of type equivalence.
906In
907.UP ,
908types are considered identical only if they are represented
909by the same type identifier.
910Thus, in particular, unnamed types are unique
911to the variables/fields declared with them.
912.PP
913Pascal 6000\-3.4 doesn't recognize our option
914flags, so it is wise to
915put the control of
916.UP
917options to the end of option lists or, better
918yet, restrict the option list length to one.
919.PP
920For Pascal 6000\-3.4 the ordering of files in the program statement has
921significance.
922It is desirable to place
923.I input
924and
925.I output
926as the first two files in the
927.B program
928statement.
bfa2fca9
KM
929.sp 2
930.SH
931Acknowledgments
932.PP
933The financial support of William Joy and Susan Graham by
934the National Science Foundation under grants
935MCS74-07644-A04, MCS78-07291, and MCS80-05144,
936and the William Joy by an
937.SM IBM
938Graduate Fellowship are gratefully acknowledged.