level 2
鄙人先说明一下……这仅仅是一个手册而已!!!封人的SB都TMD给鄙人听好了!!!没有人会认为一个编程语言的手册会违反那种渣到极点的了色规定!!!!
2011年08月15日 11点08分
1
level 2
THE INTERCAL PROGRAMMING LANGUAGE
REFERENCE MANUAL
Donald R. Woods & James M. Lyon
Copyright Donald R. Woods & James M. Lyon 1973
2011年08月15日 11点08分
3
level 2
INTERCAL
1. INTRODUCTION
The names you are about to ignore are true. However, the story has been changed
significantly. Any resemblance of the programming language portrayed here
to other programming languages, living or dead, is purely coincidental.
1.1 ORIGIN AND PURPOSE
The INTERCAL programming language was designed the morning of May 26, 1972
by Donald R. Woods and James M. Lyon, at Princeton University. Exactly when
in the morning will become apparent in the course of this manual. It was
inspired by one ambition; to have a compiler language which has nothing at
all in common with any other major language. By 'major' was meant anything
with which the authors were at all familiar, e.g., FORTRAN, BASIC, COBOL,
ALGOL, SNOBOL, SPITBOL, FOCAL, SOLVE, TEACH, APL, LISP, and PL/I. For the
most part, INTERCAL has remained true to this goal, sharing only the basic
elements such as variables, arrays, and the ability to do I/O, and eschewing
all conventional operations other than the assignment statement (FORTRAN
"=").
1.2 ACRONYM
The full name of the compiler is "Compiler Language With No Pronounceable
Acronym", which is, for obvious reasons, abbreviated "INTERCAL".
1.3 ACKNOWLEDGMENTS
The authors are deeply indebted to Eric M. Van and Daniel J. Warmenhoven,
without whose unwitting assistance this manual would still have been
possible.
2011年08月15日 11点08分
4
level 2
2. FUNDAMENTAL CONCEPTS
In this section an attempt is made to describe how and why INTERCAL may be
used; i.e., what it is like and what it is good for.
2.1 SAMPLE PROGRAM
Shown below is a relatively simple INTERCAL program which will read in 32-bit
unsigned integers, treat them as signed, 2's-complement numbers, and print out
their absolute values. The program exits if the absolute value is zero. Note
in particular the inversion routine (statements 6 through 14), which could
be greatly simplified if the subroutine library (see section 5) were used.
A more detailed analysis of a program is made in section 6 of this manual.
DO (5) NEXT
(5) DO FORGET #1
PLEASE WRITE IN :1
DO .1 <- 'V":1~'#32768$
#0'"$#
1'~#3
DO (1) NEXT
DO :1 <- "'V":1~'#65535$
#0'"$#
65535'
~'
#0$#
65535'"$"'V":1~'
#0$#
65535'"
$#65535'~'
#0$#
65535'"
DO :2 <- #1
PLEASE DO (4) NEXT
(4) DO FORGET #1
DO .1 <- "'V":1~'#65535$
#0'"$":2~'#
65535
$#0'"'~'
#0$#
65535'"$"'V":1~'#0
$
#65535'"$":2~'#
65535$#0'"'~'
#0$#
65535'"
DO (1) NEXT
DO :2 <- ":2~'
#0$#
65535'"
$"'":2~'#65535$
#0'"$#
0'~'#32767$#1'"
DO (4) NEXT
(2) DO RESUME .1
(1) PLEASE DO (2) NEXT
PLEASE FORGET #1
DO READ OUT :1
PLEASE DO .1 <- 'V"':1~:1'~#1"$#1'~#3
DO (3) NEXT
PLEASE DO (5) NEXT
(3) DO (2) NEXT
PLEASE GIVE UP
2.2 USES FOR INTERCAL
INTERCAL's main advantage over other programming languages is its strict
simplicity. It has few capabilities, and thus there are few restrictions to
be kept in mind. Since it is an exceedingly easy language to learn, one
might expect it would be a good language for initiating novice programmers.
Perhaps surprising, than, is the fact that it would be more likely to
initiate a novice into a search for another line of work. As it turns out,
INTERCAL is more useful (which isn't saying much) as a challenge to
professional programmers. Those who doubt this need only refer back to the
sample program in section 2.1. This 22-statement program took somewhere
from 15 to 30 minutes to write, whereas the same objectives can be achieved
by single-statement programs in either SNOBOL;
PLEASE INPUT POS(0) ('-' ! '')
+ (SPAN('0123456789') $ OUTPUT)
+ *NE(OUTPUT) :S(PLEASE)F(END)
or APL;
[1] >-0=/?<-?
Admittedly, neither of these is likely to appear more intelligible to
anyone unfamiliar with the languages involved, but they took roughly 60
seconds and 15 seconds, respectively, to write. Such is the overwhelming
power of INTERCAL!
The other major importance of INTERCAL lies in its seemingly inexhaustible
capacity for amazing one's fellow programmers, confounding programming shop
managers, winning friends, and influencing people. It is a well-known and
oft-demonstrated fact that a person whose work is incomprehensible is held
in high esteem. For example, if one were to state that the simplest way to
store a value of 65536 in a 32-bit INTERCAL variable is:
DO :1 <-
#0$#
256
any sensible programmer would say that that was absurd. Since this is
indeed the simplest method, the programmer would be made to look foolish in
front of his boss, who would of course happened to turn up, as bosses are
wont to do. The effect would be no less devastating for the programmer
having been correct.
2011年08月15日 11点08分
5
level 2
3. DESCRIPTION
The examples of INTERCAL programming which have appeared in the preceding
sections of this manual have probably seemed highly esoteric to the reader
unfamiliar with the language. With the aim of making them more so, we
present here a description of INTERCAL.
3.1 VARIABLES
INTERCAL allows only 2 different types of variables, the 16-bit integer
and the 32-bit integer. These are represented by a spot (.) or two-spot
(:), respectively, followed by any number between 1 and 65535, inclusive.
These variables may contain only non-negative numbers; thus they have the
respective ranges of values: 0 to 65535 and 0 to 4294967295. Note: .123 and
:123 are two distinct variables. On the other hand, .1 and .0001 are
identical. Furthermore, the latter may NOT be written as 1E-3.
3.2 CONSTANTS
Constants are 16-bit values only and may range from 0 to 65535. They are
prefixed by a mesh (#). Caution! Under no circumstances confuse the mesh
with the interleave operator, except under confusing circumstances!
3.3 ARRAYS
Arrays are represented by a tail (,) for 16-bit values, or a hybrid (;) for
32-bit values, followed by a number between 1 and 65535, inclusive. The
number is suffixed by the word SUB, followed by the subscripts, separated
optionally by spaces. Subscripts may be any expressions, including those
involving subscripted variables. This occasionally leads to ambiguous
constructions, which are resolved as discussed in section 3.4.3. Definition
of array dimensions will be discussed later in greater detail, since
discussing it in less detail would be difficult. As before, ,123 and ;123
are distinct. In summary, .123, :123, #123, ,123, and :123 are all
distinct.
3.4 OPERATORS
INTERCAL recognizes 5 operators--2 binary and 3 unary. Please be kind to
our operators: they may not be very intelligent, but they're all we've got.
In a sense, all 5 operators are binary, as they are all bit-oriented, but
it is not our purpose here to quibble about bits of trivia.
3.4.1 BINARY OPERATORS
The binary operators are INTERLEAVE (also called MINGLE) and SELECT, which
are represented by a change (c/) and a sqiggle [sic] (~), respectively.
The interleave operator takes two 16-bit values and produces a 32-bit result
by alternating the bits of the operands. Thus,
#65535c/#
0 has the 32-bit
binary form 101010....10 or 2863311530 decimal, while
#0c/#
65535 =
0101....01 binary = 1431655765 decimal, and
#255c/#
255 is equivalent to
#65535.
The select operator takes from the first operand whichever bits correspond
to 1's in the second operand, and packs these bits to the right in the result.
Both operands are automatically padded on the left with zeros to 32 bits
before the selection takes place, so the variable types are unrestricted.
If more than 16 bits are selected, the result is a 32-bit value, otherwise
it is a 16-bit value. For example, #179~#201 (binary value 10110011~11001001)
2011年08月15日 11点08分
6
level 2
5. SUBROUTINE LIBRARY
INTERCAL provides several built-in subroutines to which control can be
transferred to perform various operations. These operations include many
useful functions which are not easily representable in INTERCAL, such as
addition, subtraction, etc.
5.1 Usage
In general, the operands are .1, .2, etc., or :1, :2, etc., and the result(s)
are stored in what would have been the next operand(s). For instance, one
routine adds .1 to .2 and store the sum in .3, with .4 being used to indicate
overflow. All variables not used for results are left unchanged.
5.2 Available Functions
At the time of this writing, only the most fundamental operations are offered
in the library, as a more complete selection would require prohibitive time
and coree to implement. These functions, along with their corresponding entry
points (entered via DO (entry) NEXT) are listed below.
(1000).3 <- .1 plus .2, error exit on overflow
(1009).3 <- .1 plus .2
.4 <- #1 if no overflow, else .4 <- #2
(1010).3 <- .1 minus .2, no action on overflow
(1020).1 <- .1 plus #1, no action on overflow
(1030).3 <- .1 times .2, error exit on overflow
(1039).3 <- .1 times .2
.4 <- #1 if no overflow, else .4 <- #2
(1040).3 <- .1 divided by .2
.3 <- #0 if .2 is #0
(1050) .2 <- :1 divided by .1, error exit on overflow
.2 <- #0 if .1 is #0
(1500) :3 <- :1 plus :2, error exit on overflow
(1509) :3 <- :1 plus :2
:4 <- #1 if no overflow, else :4 <- #2
(1510) :3 <- :1 minus :2, no action on overflow
(1520) :1 <- .1 concatenated with .2
(1525) This subroutine is intended solely for internal
use within the subroutine library and is therefore
not described here. Its effect is to shift .3
logically 8 bits to the left.
(1530) :1 <- .1 times .2
(1540):3 <- :1 times :2, error exit on overflow
(1549):3 <- :1 times :2
:4 <- #1 if no overflow, else :4 <- #2
(1550):3 <- :1 divided by :2
:3 <- #0 if :2 is #0
(1900).1 <- uniform random no. from #1 to #65535
(1910) .2 <- normal random no. from #0 to .1, with
standard deviation .1 divided by #12
2011年08月15日 11点08分
8
level 2
6. PROGRAMMING HINTS
For the user looking to become more familiar with the INTERCAL language, we
present in this section an analysis of a complex program, as well as some
suggested projects for the ambitious programmer.
Considering the effort involved in writing an INTERCAL program, it was
decided in putting together this manual to use an already existing program
for instructive analysis. Since there was only one such program available,
we have proceeded to use it. It is known as the "INTERCAL System Library."
6.1 Description
The program listing begins on the second page following. It is in the same
format as would be produced by the Princeton INTERCAL compiler in FORMAT
mode with WIDTH=62 (see section 8). For a description of the functions
performed by the Library, see section 5.2.
6.2 Analysis
We shall not attempt to discuss here the algorithms used, but rather we
shall point out some of the general techniques applicable to a wide range
of problems.
Statements 10, 14, 15, and 26 make up a virtual "computed GO TO". When
statement 10 is executed, control passes eventually to statement 16 or 11,
depending on whether .5 contains
#1 or #
2, respectively. The value of .5
is determined in statement 9, which demonstrates another handy technique.
To turn an expression, exp, with value
#0 or #
1, into
#1 or #
2 (for use in
a "GO TO"), use "V-'exp'c/
#1"~#
3. To reverse the condition (i.e., convert #0
to #2 and leave
#1 alone) use "V-'exp'c/#
2"~#3.
Certain conditions are easily checked. For example, to test for zero,
select the value from itself and select the bottom bit (see statement 54).
To test for all bits being 1's, select the value from itself and select the
top bit (see statement 261). The test for greater than, performed in
statements 192 and 193 on 32-bit values, employs binary logical operations,
which are performed as follows:
'V.1c.2'~'
#0c/#
65535'
for 16-bit values or, for 32-bit values:
"'V":1~'#65535c30'"c/":2~'
#65535c/#
0'"'~'#0
c/#65535'"c/"'V":1~'
#0c/#
65535'"c/":2~'#0
c/#65535'"'~'
#0c/#
65535'"
(The proofs are left as an exercise to the reader.)
Testing for greater-than with 16-bit values is somewhat simpler and is done
with the pair of statements:
DO .C <- 'V.Ac.B'~'
#0c/#
65535'
DO .C <- '&"'.A~.C'~'"V'V.C~.C'c/#32768"
~"
#0c/#
65535"'"c/".C~.CZ''ZZZ`#1
This sets .C (a dummy variable) to #1 if .A > .B, and #0 otherwise. The
expression may be expanded as described above to instead set .C to #1 or
#2.
Note also in statement 220 the occurrence of ~"#65535c65535". Although
these operations select the entire value, they are not extraneous, as they
ensure that the forthcoming Vs will be operating on 32-bit values.
In several virtual computed GO TOs the DO FORGET #1 (statement 15 in the
earlier example) has been omitted, since the next transfer of control would
be a DO RESUME #1. By making this a DO RESUME #2 instead, the FORGET may
2011年08月15日 11点08分
9
level 2
be forgotten.
In statement 64, note that .2 is STASHed twice by a single statement. This
is perfectly legal.
Lastly, note in statements 243 and 214 respectively, expressions for
shifting 16- and 32-bit variables logically one place to the left.
Statement 231 demonstrates right-shifting for 32-bit variables.
MIKE GETS TO INSERT THE PROGRAM HERE!
6.3 Program Listing
1(1000)PLEASE IGNORE .4
2PLEASE ABSTAIN FROM (1005)
3(1009)DO STASH .1 + .2 + .5 + .6
4DO .4 <- #1
5DO (1004) NEXT
6(1004)PLEASE FORGET #1
7DO .3 <- 'V.1c.2'~'
#0c/#
65535'
8DO .6 <- '&.1c.2'~'
#0c/#
65535'
9PLEASE DO .5 <- "V!6~
#32768'c/#
1"~#3
10DO (1002) NEXT
11DO .4 <- #2
12(1005)DO (1006) NEXT
* 13(1999)DOUBLE OR SINGLE PRECISION OVERFLOW
14(1002)DO (1001) NEXT
15(1006)PLEASE FORGET #1
16DO .5 <- 'V"!6~.6'~#1"c/
#1'~#
3
17DO (1003) NEXT
18DO .1 <- .3
19DO .2 <- !6c/
#0'~'#
32767c/#1'
20DO (1004) NEXT
21(1003)DO (1001) NEXT
22DO REINSTATE (1005)
23(1007)PLEASE RETRIEVE .1 + .2 + .5 + .6
24DO REMEMBER .4
25PLEASE RESUME #2
26(1001)DO RESUME .5
27(1010)DO STASH .1 + .2 + .4
28DO .4 <- .1
29DO .1 <- 'V.2c/#65535'~'
#0c/#
65535'
30DO (1020) NEXT
31PLEASE DO .2 <- .4
32PLEASE DO (1009) NEXT
33DO RETRIEVE .1 + .2 + .4
34PLEASE RESUME #1
35(1020)DO STASH .2 + .3
36DO .2 <- #1
37PLEASE DO (1021) NEXT
38(1021)DO FORGET #1
39DO .3 <- "V!1~.2'c/
#1"~#
3
40PLEASE DO .1 <- 'V.1c.2'~'
#0c/#
65535'
41DO (1022) NEXT
42DO .2 <- !2c/
#0'~'#
32767c/#1'
43DO (1021) NEXT
44(1023)PLEASE RESUME .3
45(1022)DO (1023) NEXT
46PLEASE RETRIEVE .2 + .3
47PLEASE RESUME #2
48(1030)DO ABSTAIN FROM (1033)
49PLEASE ABSTAIN FROM (1032)
50(1039)DO STASH :1 + .5
51DO (1530) NEXT
52DO .3 <- :1~#65535
53PLEASE DO .5 <- :1~'#65280c/#65280'
54DO .5 <- 'V"!5~.5'~#1"c/
#1'~#
3
55DO (1031) NEXT
56(1032)DO (1033) NEXT
57DO (1999) NEXT
58(1031)DO (1001) NEXT
59(1033)DO .4 <- .5
60DO REINSTATE (1032)
61PLEASE REINSTATE (1033)
62DO RETRIEVE :1 + .5
63PLEASE RESUME #2
64(1040)PLEASE STASH .1 + .2 + .2 + :1 + :2 + :3
65DO .2 <- #0
66DO (1520) NEXT
67DO STASH :1
68PLEASE RETRIEVE .2
69DO .1 <- .2
70DO .2 <- #0
71PLEASE DO (1520) NEXT
72DO :2 <- :1
73DO RETRIEVE .1 + .2 + :1
74DO (1550) NEXT
75PLEASE DO .3 <- :3
76DO RETRIEVE :1 + :2 + :3
77DO RESUME #1
78(1050)PLEASE STASH :2 + :3 + .5
79DO :2 <- .1
80PLEASE DO (1550) NEXT
81DO .5 <- :3~'#65280c/#65280'
2011年08月15日 11点08分
10
level 2
82DO .5 <- 'V"!5~.5'~
#1"c/#
1'~#3
83DO (1051) NEXT
84DO (1999) NEXT
85(1051)DO (1001) NEXT
86DO .2 <- :3
87PLEASE RETRIEVE :2 + :3 + .5
88DO RESUME #2
89(1500)PLEASE ABSTAIN FROM (1502)
90PLEASE ABSTAIN FROM (1506)
91(1509)PLEASE STASH :1 + .1 + .2 + .3 + .4 + .5 + .6
92DO .1 <- :1~#65535
93PLEASE DO .2 <- :2~#65535
94DO (1009) NEXT
95DO .5 <- .3
96PLEASE DO .6 <- .4
97DO .1 <- :1~'
#65280c/#
65280'
98DO .2 <- :2~'
#65280c/#
65280'
99DO (1009) NEXT
100DO .1 <- .3
101PLEASE DO (1503) NEXT
102DO .6 <- .4
103DO .2 <- #1
104DO (1009) NEXT
105DO .1 <- .3
106DO (1501) NEXT
107(1504)PLEASE RESUME .6
108(1503)DO (1504) NEXT
109(1501)DO .2 <- .5
110DO .5 <- 'V"'&.6c.4'~
#1"c/#
2'~#3
111DO (1505) NEXT
112(1506)DO (1502) NEXT
113PLEASE DO (1999) NEXT
114(1505)DO (1001) NEXT
115(1502)DO :4 <- .5
116DO (1520) NEXT
117DO :3 <- :1
118PLEASE RETRIEVE :1 + .1 + .2 + .3 + .4 + .5 + .6
119DO REINSTATE (1502)
120DO REINSTATE (1506)
121PLEASE RESUME #3
122(1510)DO STASH :1 + :2 + :4
123DO :1 <- "'V/":2~'#65535c/
#0'"c/#
65535'~'
#0c/#
6553
5'"c/"'V":2~'
#0c/#
65535'"c/
#65535'~'#
0c65535
'"
124DO :2 <- #1
125DO (1509) NEXT
126PLEASE RETRIEVE :1
127DO :2 <- :3
128PLEASE DO (1509) NEXT
129DO RETRIEVE :2 + :4
130PLEASE RESUME #1
131(1520)PLEASE STASH .3 + .4
132DO .3 <- .1~#43690
133DO (1525) NEXT
134PLEASE DO .4 <- 'V.3c/".2~#43690"'~'
#0c/#
65535'
135DO .3 <- .1~#21845
136PLEASE DO (1525) NEXT
137DO :1 <- .4c/"'V.3c/".2~#21845"'~'
#0c/#
65535'"
138PLEASE RETRIEVE .3 + .4
139DO RESUME #1
140(1525)DO .3 <- '"'"'"!3c/#0'~'#32767c/#1'"c/#0'~'#32767
c/#1'"c/#0'~'#16383c/#3'"c/#0'~'#4095c/#15'
141PLEASE RESUME #1
142(1530)DO STASH :2 + :3 + .3 + .5
143DO :1 <- #0
144DO :2 <- .2
145DO .3 <- #1
146DO (1535) NEXT
147(1535)PLEASE FORGET #1
148DO .5 <- "V!1~.3'c/#1"~#3
149DO (1531) NEXT
150DO (1500) NEXT
151DO :1 <- :3
152PLEASE DO (1533) NEXT
153(1531)PLEASE DO (1001) NEXT
154(1533)DO FORGET #1
155DO .3 <- !3c/#0'~'#32767c/#1'
156DO :2 <- ":2~'
#0c/#
65535'"c/"'":2~'#32767c/
#0'"c/#
0'~'#32767c/#1'"
157PLEASE DO .5 <- "V!3~.3'c/#1"~#3
158DO (1532) NEXT
159DO (1535) NEXT
160(1532)DO (1001) NEXT
161PLEASE RETRIEVE :2 + :3 + .3 + .5
162DO RESUME #2
163(1540)PLEASE ABSTAIN FROM (1541)
164DO ABSTAIN FROM (1542)
165(1549)PLEASE STASH :1 + :2 + :4 + :5 + .1 + .2 + .5
166DO .1 <- :1~#65535
167PLEASE DO .2 <- :2~'
#65280c/#
65280'
2011年08月15日 11点08分
11
level 2
7. ERROR MESSAGES
Due to INTERCAL's implementation of comment lines (see section 4.5), most
error messages are produced during execution instead of during compilation.
All errors except those not causing immediate termination of program execution
are treated as fatal.
7.1 Format
All error messages appear in the following form:
ICLnnnI (error message)
ON THE WAY TO STATEMENT nnnn
CORRECT SOURCE AND RESUBMIT
The message varies depending upon the error involved. For undecodable
statements the message is the statement itself. The second line tells
which statement would have been executed next had the error not occurred.
Note that if the error is due to 80 attempted levels of NEXTing, the
statement which would have been executed next need not be anywhere near the
statement causing the error.
7.2 Messages
Brief descriptions of the different error types are listed below according
to message number.
000 An undecodable statement has been encountered in the course of
execution. Note that keypunching errors can be slightly disastrous,
since if 'FORGET' were misspelled F-O-R-G-E-R, the results would
probably not be those desired. Extreme misspellings may have even
more surprising consequences. For example, misspelling 'FORGET'
R-E-S-U-M-E could have drastic results.
017 An expression contains a syntax error.
079 Improper use has been made of statement identifiers.
099 Improper use has been made of statement identifiers.
123 Program has attempted 80 levels of NEXTing.
129 Program has attempted to transfer to a non-existent line label.
139 An ABSTAIN or REINSTATE statement references a non-existent line label.
182 A line label has been multiply defined.
197 An invalid line label has been encountered.
200 An expression involves an unidentified variable.
240 An attempt has been made to give an array a dimension of zero.
241 Invalid dimensioning information was supplied in defining or using
an array.
275 A 32-bit value has been assigned to a 16-bit variable.
436 A retrieval has been attempted for an unSTASHed value.
533 A WRITE IN statement or interleave (c) operation has produced a
value requiring over 32 bits to represent.
562 Insufficient data.
579 Input data is invalid.
621 The expression of a RESUME statement evaluated to #0.
632 Program execution was terminated via a RESUME statement instead of
GIVE UP.
633 Execution has passed beyond the last statement of the program.
774 A compiler error has occurred (see section 8.1).
778 An unexplainable compiler error has occurred (see J. Lyon or B. Woods).
2011年08月15日 11点08分
13