Google
 

Trailing-Edge - PDP-10 Archives - mit_emacs_170_teco_1220 - info/sail-examples.doc
There are no other files named sail-examples.doc in the archive.
The following are examples of simple SAIL programs.  You should
already know how to program in some language like ALGOL-W or
FORTRAN before reading this.  The examples are, hopefully,
completely explained, but the SAIL reference manual has more
details about everything.

Example 1

This program prints the numbers from 1 to 15 and their square-roots.
The line numbers at the left are not part of the program and are
included here for reference only.


00100	BEGIN "sdemo1"
00200	Comment demonstration of SAIL;
00300	integer i;  Real R;
00400	DEFINE CRLF="'15&'12";
00500	
00600	SETFORMAT(12,7);  Comment Make numbers print in 12 columns;
00700	for i:=1 step 1 until 15 do begin 
00800		R:=SQRT(i);
00900		PRINT (I,"  ",R,CRLF);
01000		END;
01100	END "sdemo1"

Every SAIL program must begin with a BEGIN (line 100), and end with
an END (Line 1100).  If you follow BEGIN with a string, then
that string will name the block.  (String constants are text
that is enclosed in double quotes, e.g., "sdemo1" on line 100).
If you follow the first BEGIN with a string, that string names
the program (if you omit this string, the program name is "M").

If you follow an END with a string, SAIL will verify that the
string following the END exactly matches the string following
the corresponding BEGIN.  If they don't match, SAIL complains.
The strings must match exactly, including the case of each letter.

A SAIL program can be described as "BEGIN" followed by declarations,
followed by statements, followed by "END".  Note that as in all
Algol-style languages, all variables must be declared.  Declarations
follow block structure (the structure of BEGINs and ENDs).

Comments can appear anywhere in your program where a declaration or
a statement could appear.  A comment consists of the word COMMENT
and includes all text up to and including the next semi-colon.


Line 300 contains examples of declarations.  I is declared to
be an integer, R is declared to be a real (i.e., floating point
number).   Other SAIL data types include STRING, BOOLEAN,
RECORD!POINTER, and LONG!REAL.  All of these data types may
be declared as ARRAYs as well.

Line 300 also shows that multiple declarations may appear on one
line. The same is true of statements.  Generally, lines and
spacing are ignored in SAIL, with a new line being treated
as a single space (except in string constants).  Multiple spaces
(and tabs) are equivalent to a single space.


Line 400 is an example of a macro definition.  The macro being
defined is named CRLF, and the CRLF is defined to be precisely
'15&'12 (that is, the string written as "'15&'12" contains just
the characters '15&'12, so those characters comprise the
definition.  Now that CRLF has been defined, every time we
write CRLF in the program (as on line 900), SAIL treats that
name as though we had written '15&'12.  The meaning of '15 is
carriage return.  The apostrophe means the number 15 should
be treated as octal.  Octal 15 is a carriage return character
(frequently called CR).  Octal 12 is a line-feed (called LF,
hence the name of the macro - CRLF).  The ampersand (&) between
the '15 and the '12 signifies string concatenation.  Thus,
CRLF is the string composed of two characters, carriage return
and line feed.  These two characters are needed to make a new
line occur in the PRINT statement on line 900.

SETFORMAT(12,7) is the first executable statement in the program
SETFORMAT is a SAIL function that allows you to specify how
SAIL should print numbers that it outputs.  The first argument
is the field width for integers.  The second argument is the
number of digits after the decimal point for real numbers.

Line 700 is a FOR statement.  FOR specifies an index variable
which is "I" in this instance, and a range of values through
which the index variable is stepped.  In the statement shown,
I takes on integer values from 1 to 15.  Note the "STEP 1" is
required in SAIL.  Also, unlike ALGOLW, the index variable
is a genuine variable, it must be declared and it has a defined
value at the end of the FOR.  FOR specifies one statement to
be repeated for the changing values of the index.  In this
example, the one statement that is repeated is a "Compound
Statement", composed of a BEGIN, some number of statements,
and an END.  So, all the statements within the compound statement
are repeated.  

Line 800 is an assignment statement.  The function SQRT is called
and it returns a real result.  The value of that result is stored
in R.  The := is the assignment operator.  SQRT takes a real
value as its argument, so the specified argument, I, is converted
to a real value.  

Line 900 prints the results.  I will be be printed as an integer,
a string constant (some spaces) will follow the value of I, the
value of R will be printed, and the line will be terminated by
a CRLF.

The END on line 1000 ends the compound statement started on line
700, which is the statement that the FOR repeats.  Finally, the
END on line 1100 matches the BEGIN on line 100, and the program
is complete.
Example two.  This program is similar to the one in example 1,
but shows four new things.  First, the limit of the FOR is
varied. Second, that limit is set from the terminal.  Third,
a procedure is defined and used, and fourth, output is directed
to an arbitrary file.

00100	BEGIN "sdemo2"
00200	Comment demonstration of SAIL;
00300	integer i,k;  Real R;
00400	DEFINE CRLF="'15&'12";
00500	
00600	INTEGER PROCEDURE GETNUM(STRING TXT);
00700		BEGIN COMMENT READ A NUMBER FROM THE TERMINAL;
00800		STRING S;
00900		PRINT(TXT); S:=INTTY; PRINT(CRLF);
01000		RETURN(CVD(S));
01100		END;
01200	
01300	K:=GETNUM("Limit ");
01400	SETFORMAT(12,7);  Comment Make numbers print in 12 columns;
01500	SETPRINT(NULL,"F");  Comment GET A FILE NAME FOR OUTPUT FROM TERMINAL;
01600	for i:=1 step 1 until K do begin 
01700		R:=SQRT(i);
01800		PRINT (I,"  ",R,CRLF);
01900		END;
02000	END "sdemo2"


The first new thing that we see in this example is the definition
of the procedure called GETNUM, which appears on lines 600 through
1100.  Procedures must be defined before they are used.  The
definition of a procedure is considered to be a declaration, and
thus appears, with other declarations, before any executable
statements within the block where the procedure is defined.

Procedures may be typed or untyped.  A typed procedure returns
a value (via the return statement, which is mandatory in a
typed procedure).  An untyped procedure returns no value, but
performs some other actions that are (presumably) useful, such
as modifying global variables, or printing results, etc.

A procedure can be any simple type (Integer, Real, Boolean,
Record!Pointer, String, etc.).  Parameters that are passed to
procedures are usually passed by VALUE, except arrays are
passed by REFERENCE.   Any parameter can be explicitly passed
by reference, by so-declaring the procedure.

An example of a procedure declaration is:
STRING PROCEDURE FP(STRING A, B; REFERENCE INTEGER I; INTEGER ARRAY C);
	BEGIN .... END;
00100	BEGIN "sdemo3"
00200	Comment demonstration of SAIL;
00300	DEFINE CRLF="'15&'12";
00400	INTEGER I,J,K,EOF,BRKCHR,ICHAN,OCHAN;
00500	STRING S; STRING ARRAY LINES[1:200];
00600	
00700	PRINT("Name of input file: ");   ICHAN:=OPENFILE(NULL,"RO");
00800	OCHAN:=OPENFILE("SDEMO3.OUT","W");
00900	SETINPUT(ICHAN,200,BRKCHR,EOF);
01000	SETBREAK(1,'12,'15&'14,"ISN");  Comment omit break characters, 
01100					Delete line numbers;
01200	
01300	I:=0;
01400	WHILE (I:=I+1)<=200 DO BEGIN
01500		LINES[I]:=INPUT(ICHAN,1);
01600		IF EOF THEN DONE;
01700		END;
01800	FOR J:=I-1 STEP -1 UNTIL 1 DO OUT(OCHAN,LINES[J]&CRLF);
02000	CFILE(ICHAN); CFILE(OCHAN);
02100	END "sdemo3"
mi