Statement cheatsheet

A one-stop reference for every INTERCAL statement this compiler recognises, with syntax, a minimal example, the codegen function that handles it, and the runtime errors each can trigger. Aimed at somebody who knows INTERCAL and wants to look up a specific statement quickly.

For the pedagogical introduction see intercal-primer.md. For the formal grammar see appendix-grammar.md.

Conventions used below

  • <expr>: an arithmetic expression producing a 16- or 32-bit value.
  • <var>: a variable reference (.N, :N, ,N, ;N).
  • <label>: a number in parentheses, (N) where 1 ≤ N ≤ 65535.
  • <n>, <k>: decimal number literals.
  • <gerund>: one of CALCULATING, NEXTING, FORGETTING, RESUMING, STASHING, RETRIEVING, IGNORING, REMEMBERING, ABSTAINING, REINSTATING, COMING FROM, READING OUT, WRITING IN.

Every statement can optionally be prefixed with a label and the verb can be DO / PLEASE / PLEASE DO; any of these can optionally be negated with NOT / N'T / DON'T; and any can take a %N probability. The table below shows the bare form.

Assignments

StatementExampleCodegen functionErrors
<var> <- <expr>DO .1 <- #5codegen_assignICL275I (32→16 overflow), ICL533I (arithmetic overflow)
<array> <- <expr>DO ,1 <- #10codegen_array_dimICL240I (zero dimension), ICL533I
<array> <- <expr> BY <expr>DO ,1 <- #3 BY #4codegen_array_dimICL240I, ICL533I
<array> SUB <expr> ... <- <expr>DO ,1 SUB #2 <- #42codegen_array_elem_assignICL241I (bad subscript), ICL275I, ICL533I

Control flow

StatementExampleCodegen functionErrors
<label> NEXTDO (100) NEXTcodegen_nextICL123I (stack overflow, runtime), ICL129I (unknown label, compile-time)
RESUME <expr>DO RESUME #1codegen_resumeICL621I (RESUME 0, runtime), ICL632I (stack underflow, runtime)
FORGET <expr>DO FORGET #1codegen_forgetnone
COME FROM <label>DO COME FROM (100)implicit in target statementICL555I (duplicate target, compile-time), ICL129I (unknown target, compile-time)
GIVE UPDO GIVE UPcodegen_give_upnone

Modifiers

StatementExampleCodegen functionErrors
ABSTAIN FROM <label>DO ABSTAIN FROM (100)codegen_abstainICL139I (unknown label)
ABSTAIN FROM <gerund>[+<gerund>]*DO ABSTAIN FROM CALCULATINGcodegen_gerund_modifynone
REINSTATE <label>DO REINSTATE (100)codegen_reinstateICL139I
REINSTATE <gerund>[+<gerund>]*DO REINSTATE NEXTING + RESUMINGcodegen_gerund_modifynone
IGNORE <var>[ <var>]*DO IGNORE .1 :2codegen_ignorenone
REMEMBER <var>[ <var>]*DO REMEMBER .1 :2codegen_remembernone

Stash / retrieve

StatementExampleCodegen functionErrors
STASH <var>[ <var>]*DO STASH .1 .2codegen_stashICL999I (stash overflow, not yet enforced)
RETRIEVE <var>[ <var>]*DO RETRIEVE .1 .2codegen_retrieveICL436I (retrieve without stash)

I/O

StatementExampleCodegen functionErrors
READ OUT <var>[ <var>]*DO READ OUT .1codegen_read_outnone (scalar)
READ OUT <array>DO READ OUT ,1codegen_read_outnone (array)
WRITE IN <var>[ <var>]*DO WRITE IN .1codegen_write_inICL562I (end of input), ICL579I (bad format)
WRITE IN <array>DO WRITE IN ,1codegen_write_inICL562I

Whole-program

Statement typeExampleCodegen functionErrors
Any unknown formDO WOT A HULLABALOOdispatcher fallthroughICL000I (executed unknown statement, runtime)
Bad spark/rabbit-ears nestingDO .1 <- 'a $ 'b''parse_exprICL017I (compile-time)
Fallthrough past last statement(missing GIVE UP)implicit epilogueICL633I (fell off the end, runtime)
Politeness out of range(whole program, ≥5 statements)check_politeness at compile timeICL079I (rude), ICL099I (polite)
Duplicate label(whole program)check_labels at compile timeICL182I
Label outside 1–65535DO (0) NEXTcheck_labels at compile timeICL197I
Multiple COME FROMs to same label(whole program)resolve_come_from at compile timeICL555I
Unknown gerund in ABSTAIN/REINSTATEDO ABSTAIN FROM PROCRASTINATINGcodegen_gerund_modify at compile time(named diagnostic, not a numeric ICL)

Expression operators

OperatorSyntaxMeaningRuntime routine
Binary mingle'<a> $ <b>'Interleave bits of two 16-bit values → 32-bit_rt_mingle
Binary select'<a> ~ <b>'Select bits from <a> where <b> has 1s, pack right_rt_select
Unary AND&<var>AND each bit with its right neighbour (wrapping)_rt_unary_and_16 or _rt_unary_and_32
Unary ORV<var>OR each bit with its right neighbour_rt_unary_or_16 or _rt_unary_or_32
Unary XOR?<var>XOR each bit with its right neighbour_rt_unary_xor_16 or _rt_unary_xor_32

Grouping: '...' (sparks) and "..." (rabbit-ears). Must alternate. No operator precedence; every subexpression that could be ambiguous must be grouped.

System library labels (call via NEXT)

16-bit:

LabelOperationInputsOutputs
1000add.1, .2.3 (silent wrap on overflow; *9 suffix variant has flag)
1009add, with flag.1, .2.3, .4 = 1 if ok / 2 if overflow
1010subtract (wraps).1, .2.3
1020increment in place.1.1
1030multiply.1, .2.3 (silent wrap on overflow; use 1039 for flag)
1039multiply, with flag.1, .2.3, .4
1040divide.1, .2.3 (0 if divisor 0)
105032/16 divide:1, .1.2 (silent wrap if quotient > 65535)

32-bit:

LabelOperationInputsOutputs
1500add:1, :2:3 (silent wrap on overflow; use 1509 for flag)
1509add, with flag:1, :2:3, :4
1510subtract (wraps):1, :2:3
1520mingle explicit.1, .2:1
153016×16 → 32 multiply.1, .2:1
154032×32 multiply:1, :2:3 (silent wrap on overflow; use 1549 for flag)
154932×32, with flag:1, :2:3, :4
155032-bit divide:1, :2:3 (0 if divisor 0)

Random:

LabelOperationInputsOutputs
1900uniform random(none).1 in [0, 65535]
1910bounded random.1.2 in [0, .1]

Internal (do not call): 1525 (shift helper), 1999 (overflow exit).

Caller responsibility: STASH every variable in .1–.4 / :1–:4 that the routine will clobber, then RETRIEVE after the return. Failure to STASH leads to silent value loss; failure to RETRIEVE (or RETRIEVE without prior STASH) fires ICL436I.

Label 666 syscalls

Invoke with .1 set to the syscall number, then DO (666) NEXT. See label-666-intro.md for a walkthrough.

.1SyscallInputsOutputs
1open.2 mode (0=read, 1=write), filename in ,65535.3 fd
2read.2 fd, .3 max bytes.4 bytes read, data in ,65535
3write.2 fd, .3 count, data in ,65535.4 bytes written
4close.2 fd(none)
5argc(none).3 count
6argv.2 index (0-based).3 length, arg in ,65535
8exit.2 status codeno return
9getrand.2 0 = uniform / >0 = range [0, .2].3 random

Program must dimension ,65535 <- #65535 at entry and not use ,65535 for any other purpose.

Comment

There is no comment syntax in INTERCAL. The idiom for a no-op pseudo-comment is:

DON'T NOTE whatever you want to say here

This parses as a negated unknown statement, skipped at runtime, ignored by the codegen.

Statement order inside a body

Every statement body's tokens appear in the order specified above (verb, negation, probability, body), but operator expressions inside the body may be freely nested:

PLEASE DO %75 .1 <- '.2 $ "#5 ~ #3"' ~ #65535

This is a probabilistic assignment with a nested expression. The parser handles each piece in sequence.

Next reading