Transcript INTERCAL

INTERCAL
Aaron Bloomfield
CS 415
Fall 2005
A note about the sources

The main sources for this lecture set are:

The INTERCAL page in Wikipedia


The INTERCAL Programming Language Revised Reference
Manual


On the INTERCAL resources page: http://www.catb.org/~esr/intercal/
Both sources are available under “free” licenses



http://en.wikipedia.org/wiki/Intercal
Wikipedia under the GNU Free Documentation License
The Reference Manual under the GPL
In accordance with these licenses, this work is also
released under those same licenses
2
History



Originally designed in 1972
Main goal: “to have a compiler language which has
nothing at all in common with any other major
language”
Only similarities are “basic” things such as:


Nothing else!


Variables, arrays, I/O, assignment
Including all the arithmetic operators you are familiar with…
INTERCAL stands for “Programming language without
a pronounceable acronym”
3
Politeness



INTERCAL makes sure the programmer is polite
Statements may be prefixed by PLEASE
If not enough PLEASEes are there, the compiler
complains:


IF too many are there, the compiler also complains:


PROGRAMMER IS INSUFFICIENTLY POLITE
PROGRAMMER IS OVERLY POLITE
Between ¼ and 1/3 of the statements must have
PLEASE
4
Constants



Constants are prefixed by a mesh (#)
Can not be negative, and range from 0 to 65535
Examples on next line

#0 #32 #65535
5
Variables

Two types of variables:

16-bit integer



32-bit integer



Represented by the two-spot (:)
Examples on next line
 :0 :32 :4294967295
Note that you can’t have negative numbers


Represented by the spot (.) followed by a number between 0 and 65535
Examples on next line
 .1 .32 .65535
You have to keep track of the sign separately
Further notes



.123 and :123 are distinct variables
But .1 and .0001 are identical
But .001 is not 1E-3
6
Arrays



Array (of numbers) are represented by a tail (,) or a
hybrid (;) for 16-bit and 32-bit values, respectively
Array elements are suffixed by the word SUB, followed
by the subscripts
In summary:

.123 :123 ,123 ;123 and #123 are all distinct
7
Operators




INTERCAL recognized 5 operators: 2 binary, 3 unary
“Please be kind to our operators: they may not be very
intelligent, but they’re all we’ve got”
The design intent was to be different than any other
operators that existed
Binary operators

Interleave (aka mingle): represented by a change (¢)




In an ASCII environment, you can use big money ($) instead
Takes two 16-bit values and interleaves their bits, producing a 32-bit
value
So #65535$#0 has 32-bit form 101010…10 (or 2863311530 decimal)
Select: represented by a sqiggle [sic] (~)
8
Binary operators

Interleave (aka mingle): represented by a change (¢)




In an ASCII environment, you can use big money ($) instead
Takes two 16-bit values and interleaves their bits, producing a 32-bit value
So #65535$#0 has 32-bit form 101010…10 (or 2863311530 decimal)
Select: represented by a sqiggle [sic] (~)



Finds which bits are 1 in the second operand
Selects those bits from the first operand
Consider #179~#201 (binary 10110011~11001001)




From the second operand, selects bits 1, 2, 5, and 8
Those bits in the first operand are 1, 0, 0, and 1, resepectively
Thus, the result is value #9
Consider #201~#179 (binary 11001001~10110011)



From the second operand, selects bits 1, 3, 4, 7, 8
Those bits in the first operand are 1, 0, 0, 0, 1
Thus, the result is #17
9
Unary operators

Three types:



Logical AND (&)
Logical OR (V)
Logical XOR ()


These characters are inserted between the spot, two-spot, mesh, etc., and the
number:

Examples on the next line



.&123 #?123
Cannot use multiple unary operators
These operators perform their respective operations on pairs of adjacent bits,
with the result going into the position of where the first bit (of the pair) was in
the original number


In ASCII, written as a what (?)
The result of the first and last bits goes into the first bit of the result
Examples (77 is binary 1001101):



#&77 is 0000000000000100 = 4
#V77 is 1000000001101111 = 32879
#?77 is 1000000001101011 = 32875
10
Precedence


As INTERCAL was intended “to have no precedents”,
the compiler does not define what the operator
precedence is
“The precedence (or lack thereof) may be overruled by
grouping expressions between pairs of sparks (‘) or
rabbit-ears (“)”.

Thus, ‘#165$#203’~#358 (binary value
‘10100101$11001011’~101100110) has value 15
11
Statements

Line labels are enclosed in wax-wane pairs (())



After the line label (if present), one of the following must occur:




Labels are (unique) integers from 1 to 65535
Labels between 1000 and 1999 are used by the INTERCAL System
Library functions
DO
PLEASE
PLEASE DO
Following that, “either, neither, or both” of the following occur:


NOT or N’T, which causes INTERCAL to abstain from that line
A number from 0 to 100, preceded by a double-oh-seven (%)


This causes the statement to have that percentage of being executed
Following all of this are one of the 14 valid operations
12
Statements
1.
Calculate


2.
The assignment: instead of =, INTERCAL uses a angle (<) followed by a
worm (-)
32-bit values can get 16-bit values, and vise-versa if the value is less than
65535
NEXT

Of the form:




3.
DO (label) NEXT
PLEASE DO (label) NEXT
Used for subroutine calls and unconditional transfers
Transfers control to that label AND stores it in a stack
FORGET


Followed by an expression
Causes that many entries to be removed from the stack
13
Statements
4.
RESUME



5.
STASH


6.
Like a FORGET
But jumps to the line label that is the last to be popped
Used with NEXT (and FORGET) to do “subroutines”
Stores variables and arrays so that subroutines can use the
same variable names
Variables and arrays to stash are separated by intersections
(+)
RETRIEVE

Restores the previously STASHed values of the variables
and arrays
14
Statements
7.
IGNORE


8.
REMEMBER

9.
Followed by a variable (or array)
Causes the variable to be unable to be modified
Allows one to modify a variable that has been IGNOREd
ABSTAIN

Two forms



10.
First form: causes INTERCAL to abstain from executing a provided line
label
Second form: causes INTERCAL to abstain from certain functions (i.e.
PLEASE ABSTAIN FROM IGNORING + FORGETTING)
This is how you do an if statement in INTERCAL
REINSTATE

Nullifies an ABSTAIN
15
Statements
11.
GIVE UP

12.
Input

13.
Of the form DO WRITE IN list, where list represents a string
of variables and/or elements of arrays, separated by
intersections (+)
Output

14.
Exits the program
Of the from DO READ OUT list
COME FROM

The opposite of GOTO!
16
COME FROM

The following statements:
(1) DO <…>
…
(2) DO COME FROM (1)

Is equivalent to:
(1) DO <…>
(2) DO GOTO (3)
…
(3) DO NOTHING

If INTERCAL had a GOTO (or NOTHING) commands
17
Comments

Unrecognized statements are flagged with a splat (*)
during compilation



And the compiler will happily continue
But you can put a splat in there yourself
So this acts as a comment of sorts…
18
Hello World Example
DO ,1 <- #13
PLEASE DO ,1 SUB #1 <- #234
DO ,1 SUB #2 <- #112
DO ,1 SUB #3 <- #112
DO ,1 SUB #4 <- #0
DO ,1 SUB #5 <- #64
DO ,1 SUB #6 <- #194
DO ,1 SUB #7 <- #48
PLEASE DO ,1 SUB #8 <- #22
DO ,1 SUB #9 <- #248
DO ,1 SUB #10 <- #168
DO ,1 SUB #11 <- #24
DO ,1 SUB #12 <- #16
DO ,1 SUB #13 <- #214
PLEASE READ OUT ,1
PLEASE GIVE UP
19
Another Example

This program will:




Read in 2 32-bit ints
Treat them as signed, 2’s
complement numbers
Print out their absolute
values
I couldn’t get it to
work…
DO (5) NEXT
(5) DO FORGET #1
PLEASE WRITE IN :1
DO .1 <- '?":1~'#32768$#0'"$#1'~#3
DO (1) NEXT
DO :1 <- "'?":1~'#65535$#0'"$#65535'
~'#0$#65535'"$"'?":1~'#0$#65535'"
$#65535'~'#0$#65535'"
DO :2 <- #1
PLEASE DO (4) NEXT
(4) DO FORGET #1
DO .1 <- "?':1~:2'$#1"~#3
DO :1 <- "'?":1~'#65535$#0'"$":2~'#65535
$#0'"'~'#0$#65535'"$"'?":1~'#0
$#65535'"$":2~'#0$#65535'"'~'#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 <- '?"':1~:1'~#1"$#1'~#3
DO (3) NEXT
PLEASE DO (5) NEXT
(3) DO (2) NEXT
PLEASE GIVE UP
20
That last program in comparison

That last program in SNOBOL:
PLEASE INPUT POS(0) ('-' ! '')
+ (SPAN('0123456789') $ OUTPUT)
+ *NE(OUTPUT) :S(PLEASE)F(END)


Took about 60 seconds to write
That last program in APL:
[1] →0≠ ←|




Yes, that last line displayed correctly
Took about 15 seconds to write
The INTERCAL version took about 30 minutes to write
(I didn’t write any of these versions, by the way)
21
Beyond INTERCAL

TriINTERCAL





Which uses a trinary number system, not a binary number
system
Instead of a bit, you use a trit
Instead of a .i suffix, uses a .3i suffix
But why stop there?
The INTERCAL compiler can recognize any extension
of the form .Ni

Where N is from 2 to 7
22