Appendix: exercise hints
Hints, not full solutions, for the exercises that close most chapters. The hints are deliberately short. They unblock a stuck reader without short-circuiting the act of working through the problem.
If a chapter's exercises are not listed below, the exercises are direct enough that hints would be redundant.
intercal-primer.md
- 4/17 ≈ 23.5%, comfortably inside [20%, 33.3%]. The lower bound becomes binding when total = 20 (4/20 = 20%). Any further shrinkage breaks it.
- The smallest STASH/RETRIEVE program is two statements after dimensioning a politeness count. Swapping fires
ICL436I RETRIEVE WITHOUT STASH. #5is0b0000000000000101. Unary AND XORs each bit with its right neighbour. Bit 0 is1 & 0 = 0. Bit 1 is0 & 1 = 0. Bit 2 is1 & 0 = 0. The rest are0 & 0 = 0. Result: 0. Surprised? That is the lesson.- The idiom converts a 0/1 value into 1/2, pushes two NEXT targets, then
RESUMEs with the 1/2 variable.RESUME #1returns to the most recent target;RESUME #2skips one. - Alternatives:
DO .99 <- #0is nominally a no-op but actually wastes politeness count. Inserting a labelled statement and thenABSTAINing it is verbose.DON'T NOTEis shortest and idiomatically read as a comment.
pipeline.md
- The codegen prologue is everything before
_stmt_0_start.codegen_give_upproduces the exit syscall sequence (mov x0, #0; mov x16, #1; svc #0x80). Everything else is runtime. SOURCEis materialised once. The lexer splits it incrementally without making copies, thestmt_bodyentries are slices.- Swapping politeness and label checks would mean a duplicate-label program might silently fail the politeness check first. Functional difference: zero, because both are fatal.
DO (1000) NEXTflipsneeds_syslibto 1. Any label in [1000, 1999] does.- Putting the program first means its
_mainentry is followed by the runtime symbols. The linker accepts this, symbols are resolved by name, not order, but a sharper symbol search misses for the entry point. In practice the binary still works.
lexing-and-parsing.md
- Politeness is identical because both forms are negated; only the verb tokens differ. The runtime difference is that
DON'Tis one token andDO NOTis two, but both produce a negated statement. - The keyword list is in
tokenize(). None of them is a prefix of another in the current grammar (DOis not a prefix of any longer keyword;DON'Tis detected beforeDO). - The tree has 5 nodes: NODE_VAR for
.1, NODE_CONST for#5, NODE_CONST for#3, NODE_SELECT for the~, NODE_MINGLE for the$. The~is the parent of the two constants. - The two operators consume different token characters (
$vs~), so the parser can dispatch on the next token without lookahead. 'a + 'b''(or any spark inside spark) is accepted by the parser. At runtime the misnested expression fires ICL017I.
semantic-analysis.md
- For 7 statements: 2 ≤ PLEASE ≤ 2. For 12: 3 ≤ PLEASE ≤ 4. For 100: 20 ≤ PLEASE ≤ 33.
- The compiler's
label_to_stmtis bounded by the labels actually in source, so it would not need to change. The runtime's per-label tables (none, currently, labels are resolved at compile time) similarly would not change. The reduction would only affect the compile-time bounds check on label values. - Two
COME FROM (100)statements in the same program. The current behaviour is that the second silently overwrites the first; the spec saysICL555I MULTIPLE COME FROMS. - Reading an unwritten variable yields 0 (the BSS initial value). If we only recorded written variables, the codegen would not emit a BSS slot, and the read would dereference an undefined symbol, link error.
- For: catches a class of bugs at compile time. Against:
RESUMEtakes an expression that may not be statically computable. The compile-time check would flagRESUME #0but notRESUME .1where.1happens to be 0.
code-generation.md
- The abstain check is 4 instructions per statement (
adrp,add,ldrb,cbnz). On 1000 statements that is 4000 instructions. If the average statement body is 10 instructions, the overhead is ~30%. DO .1 <- '#5 $ #3'produces onebl _rt_mingle. A constant-folding pass would compute the result at compile time and emitmov w0, #Nwhere N is the precomputed mingle of 5 and 3.- Array element assignment skips the ignore check because arrays do not have an ignore flag in the current implementation. This is a feature gap, not a bug, the spec allows IGNORE on arrays but our compiler does not yet.
- The previous allocation is leaked. The runtime never calls
munmap. The OS reclaims at process exit. - Refactoring would require either a base-architecture-agnostic emit primitive or a templating layer that fills in per-architecture instruction names. Neither is large; the question is whether the duplication is bothersome enough to justify the abstraction.
runtime.md
_rt_write_romanforw0 = 0walks the Roman numeral table, finds nothing it can subtract, and writes nothing. The output is empty. Acceptable because Roman numerals do not represent zero.- With 128 entries, every character would map to two possible ASCII codes. The bit-reversal scheme requires a power-of-two tape size that is at least 256 to round-trip every byte.
- Yes.
,1is dimensioned to 3, then element 1 is 72 (ASCII H).READ OUT ,1walks the array; the first delta moves the head from 0 to 248 (= -72 mod 256), bit-reversed to ASCII H = 72. Print "H". - The runtime BSS reservation
_next_stackwould change from 80×8 to 1024×8. The codegen's overflow checkcmp w0, #79would change tocmp w0, #1023. The error message would still fire ICL123I. - The C program has to use the same ABI: AAPCS64 on ARM64, System V AMD64 on x86-64. Calling our
_rt_*functions from C is straightforward, the runtime functions follow the standard ABI. - A program that dimensions an array repeatedly inside a loop would leak memory faster than the OS reclaims it (until the program ends).
syslib.md
- The native binary is ~40 KB; the pure-syslib binary is ~1.3 MB. The factor is 33×.
- Three statements: STASH the variables, NEXT to label 1010 (subtract), then NEXT to label 1000 (add) and compare to
.1. - The 330× factor matches what you measure: a program that takes 0.09s with native takes ~30s with pure.
- Label 1900 invokes Label 666 syscall 9. Native: one syscall. Pure: ~10 INTERCAL statements that ultimately invoke the same syscall.
- The pure syslib needs the routine added in INTERCAL. The native syslib needs the matching assembly per platform. The compiler's
detect_syslibdoes not need changes, it already triggers on any 1000-1999 label.
self-hosting.md
bootstrap.shgrepscompiler.iforcodegen|_stmt_|.section. If none of those appear, it skips. The MVPcompiler.idoes not include them, sobootstrap.shexits cleanly without doing the bootstrap.- gen1 and gen2 can differ in (e.g.) register allocation; gen2 and gen3 cannot, because the compiler is now its own input and any compiler-determined difference would have shown up at gen2. Example where gen1 = gen2 = gen3: a compiler that generates byte-identical output regardless of which compiler compiled it. Possible only if the bootstrap and the self-hosted are byte-equivalent, which is exceptional.
- To distinguish DO from PLEASE DO, stage3 needs to read the verb word and branch. Smallest extension: ~30 lines of INTERCAL to scan past whitespace and compare the next 6 characters.
- Yes. Python is on every CI runner. The bootstrap would become Python instead of zsh; CI matrix and dependencies remain identical.
- No. The fixpoint test cares about the compiler reproducing itself, not about which syslib it links. Adding
--pure-syslibwould multiply compile time by 330× without gaining anything.
platforms.md
- macOS uses carry-flag conditionals because the underlying syscall ABI sets the carry on error. Linux returns negative values directly. Both predate AAPCS standardisation.
0x1002appears in themmapsyscall flags. Thesedis conservative; it would not match inside a comment because the pattern is anchored on the operand position.- Windows ARM64 should use a fresh codegen backend, the COFF format and Windows calling convention are too different for a
sedtranslation. - Yes. AT&T syntax requires every operand prefixed with
%and source-first ordering; the codegen output would have to be rewritten line by line. - About 60% slowdown is x86-64 emulation on ARM64; about 40% is Docker overhead. Measure independently by running the tests inside an x86-64 container on a real x86-64 machine.
testing-and-workflow.md
- The flags are
--filter,--verbose,--keep,--no-color,--platform.--filteris for iterating on a single failing test;--verboseshowsccstderr for assembly errors;--keeppreserves failed-test artifacts in/tmp/intercal_failuresfor post-mortem;--no-coloris for CI logs;--platformoverrides platform detection for cross-builds. - A test where the RHS is large and the variable is ignored: if the codegen reordered the checks, either the ignored write would still trigger an overflow error, or the overflow check would silently succeed because the ignore came first.
- A program exercising mingle and select with non-trivial bit patterns. Catches divergence in operator implementation that the existing add/sub/divide tests don't.
- For: workflow edits do not affect compiled program behaviour. Against: a broken workflow has surfaced subtle bugs (e.g. a CI yaml typo that masks failures).
- The slowdown ratio measures cumulative overhead. Subtract Docker startup (~3s) and you have the per-test x86-64 emulation cost.
walkthrough-hello.md
- Maximum: 5 PLEASEs is 29.4% (under 33.3%). 6 is 35.3% (over). So 5 is the maximum; one more flip is fine.
- ~12 instructions per statement (4 abstain check + 5 array element handling + 3 type/range), times 17 = ~200 instructions. Plus 20+ for the array dimensioning.
- The politeness ratio drops from 4/17 to 3/17 = 17.6%, below 20%. Compiler emits
ICL079I PROGRAMMER IS INSUFFICIENTLY POLITE. - Hello world does not call any syslib labels (1000-1999).
--pure-sysliblinking is conditional on syslib usage. - Reducing dimension to 13:
READ OUT ,1would print 13 characters instead of 14, missing the last!. Increasing to 30: the unset elements default to zero, which the TTM tape interprets as "no head movement" and emits the same character repeatedly (or near-zero noise).
debugging.md
- ICL079I tests fail because the bound moved. Reproduce: change bounds, run tests, check
politeness_*failures. - 80 NEXTs without intervening RESUMEs trigger ICL123I (the 80th attempt fails).
- Multiplication tests fail first,
_rt_mingleis on every multiplication's hot path. Roughly half of the bootstrap suite exercises mingle directly or transitively through arithmetic syslib labels. - Use
git log --oneline | grep -i 'open\|write'to find candidate commits. Test each withgit bisect run. - Yes,
stage3.imay have a politeness or label issue caught by the linter.
anatomy-of-a-binary.md
- The diff appears in a single
.databyte, the constant value at the changed offset. - Hello world calls
_rt_write_romanzero times (it uses_rt_read_out_arrayfor character output).tests/test_read_out_num.icalls it once perREAD OUTof a scalar. - 1000-statement program: ~50 KB code, 1 KB BSS for stmt_flags, 500 bytes per scalar. Roughly 60 KB total.
stripremoves the symbol table, saving roughly half the size for small binaries. The remaining content is the actual code and data.- Hello world makes ~17 syscalls (one
writeper character emitted, plus theexit).
label-666-intro.md
- Set
.1=5,(666) NEXT, READ OUT.3. Done. - Open the file (syscall 1), loop 10 times reading 1 byte each (syscall 2), READ OUT each byte from
,65535 SUB #1, close (syscall 4). ,65535is overwritten by every Label 666 read. A program that uses it for its own purposes would have its data clobbered on the next syscall..2 = 0returns a uniform 16-bit random number. The runtime treats it as "no range limit specified".- The MVP uses syscalls 6 (argv), 1 (open), 2 (read), 4 (close), 3 (write).
lexer-theory.md
- Two states: a "start" state and a "consuming digits" state. The transition between them is the
#character; the transition out is whitespace or a non-digit. - Adding NOT and N'T as separate tokens after DO requires three more states: one for "after DO", one for "consuming N", one for emitting either NOT (after
OT) or N'T (after'T). - The lexer already uppercases everything, so a lowercase variant requires zero changes.
- SUB is a body-level keyword. Detecting it during expression parsing rather than lexing means the parser can resolve subscripts in their grammatical context. Moving it to the lexer would force the lexer to know which surrounding tokens make
SUBa keyword vs an identifier, which it cannot, since INTERCAL has no identifiers. - Collapsing the DO/DON'T branch into a single state machine is straightforward: read the verb-token, then check for
'Timmediately after.
parser-theory.md
- Each non-terminal's FIRST set:
exprstarts with primary's FIRST.primarystarts with#, prefix character, unary char, or grouping char. No overlap. - Add
@as a binary_op alternative. The parser changes by adding@to the binary-op test in the parse loop. - Five sparks/rabbit-ears deep:
'a $ "b $ 'c $ "d $ 'e' "' "' "' "'. An LR parser would consume tokens left-to-right and reduce on each grouping pair; recursion depth in our parser is exactly the nesting depth. - Unbalanced sparks: error message could be
expected ' but found ; line 5. Variable prefix without number:expected number after . at offset 12. - Sacrifices: pointer indirection is harder to follow; debugger inspection requires array-index lookup; the layout cannot grow more children per node without coordinated array growth.
calling-conventions.md
- Two register moves:
_spot_1address intox0, thenldr w0to fetch the value. One stack save: thestp x29, x30in_rt_write_roman's prologue. stp x19, x30, [sp, #-16]!thenldp x19, x30, [sp], #16; ret.r10is touched by syscalls (replacesrcx). The runtime saves and restores it where needed.- Every syscall wrapper saves
rcxandr11becausesyscallclobbers them. Look forpush rcx; push r11patterns. - Some ARM64 instructions (
stp,ldp) operate on aligned 16-byte pairs. 8-byte alignment would slow them down or rule them out.
executables-and-linking.md
file ./programprints the format and architecture. Match against the table in the chapter.- Mach-O hello world: ~25
_rt_*symbols, 17_stmt_N_*pairs (34 symbols). - On-disk size is smaller than the section table sums because BSS is not stored on disk.
- Without ICL633I, the program executes past the last instruction into uninitialised memory. SIGILL or similar.
- The implicit default section is
.textfor instructions and.datafor.ascii. A program with neither directive would land in whatever section the assembler defaults to at the start of the file, usually.text.
middle-end-and-optimisation.md
- Without
--pure-syslibthe cost is dominated by the codegen of user statements; with it, by tokenising and processing 9000 lines of syslib INTERCAL. - Three consecutive statements like
mov w0, #5; str w0, [_spot_1]; ldr w0, [_spot_1]can collapse tomov w0, #5; str w0, [_spot_1]. - 100 statements × 4 abstain instructions × possibly 2 cycles = 800 cycles saved. Negligible on modern out-of-order hardware.
- ~10 opcodes: assign, mingle, select, unary_op (×3), call, branch, label, return.
- Few. Most INTERCAL programs have <10 control-flow joins.
If you find yourself stuck on an exercise not listed here, drop a question into TODO.md or open an issue.