Can you explain how this code works?

Most of the code here is set up, with very little actual 'payload'. Splitting things into one line per instruction is useful, with the proviso that this will cause issues with the real thing due to the spaces that get introduced as a result. \tracingall will help also, as already pointed out.

Taking the first line of the posted code we have

\let~\catcode
~`?`\

This starts with the assumption that ~ is active, and so can be \let to \catcode. Next, this new way of setting a catcode is used to make ? active, as the end-of-line character has character code 13, which is the category code for an active character. (To see this, notice that there is no space after the \ at the end of the line).

The next line in the original contains:

~`#?~`~
~`]?~`~
\let]\let
~`\.?~`~
~`,?~`~
~`\%?~`~
~`=?~`~
]=\def

So this is mainly catcode assignments (remembering that ~ is \catcode). The characters # and ] are both made active, and the ] is \let to \let. Next, ., ,, % and = are all made active. These line would normally be written

\catcode`\.\the\catcode`~

or more usually

\catcode`\.\active

The final instruction on the second line is to \let the = token to \def.

The third line of the original actually goes over slightly onto the forth one:

],\expandafter
~`[?~`~
][{
=%{\message[}
~`\$?~`~
=${\uccode`'.\uppercase{,=,%,{%'}}}

So we have another \let, a category code change and then yet another \let. There is then a \def (using the = version), which results in the active % having the definition \message[. That macro is going to be used later to build up the message that the code prints. Yet another category code change follows, to make $ active, and then this is defined in the last part of the line. The idea of that definition is to make ' active and to redefine % within a case-changing block.

The remainder of the fourth line contains

~`*?~`~
=*{\advance.by}
]#\number
~`/?~`~
=/{*-1}
\newcount.

So yet again we have a token made active, this time *, which is then defined to \advance.by. We then have # begin \let to \number, and then / is made active and defined as expanding to *-1. The last instruction on this line creates a new \count, which will be referred to as . (which was made active a while ago).

The next line can be broken up to read

=\-{*-}
~`-?~`~
]-\-
~`^?~`~
=^{*1}
~`\ ?~`~
= {.`\ $}
~`@?~`~
=@{,.,"#`@^$}

So there is a definition of \-, then - is made active and \let to the definition of \-. The ^ token is made active and defined as *1, then the space token is made active and given a definition. Finally, @ is made active and given a definition. That completes the bulk of the set up: most of the rest of the code is the message that gets printed!

The rest of the code consists of altering the value of . (a count) and then redefining %. The idea (in general) is that ' is given the upper-case code of the value of ., and this value is the character code of the letter required for the message. To keep the code complex, the numbers are done as far as possible using character codes rather than actual numbers. This is rather tedious to go through, so I'd recommend reading the \tracingall output for full details.

One more point to note is that the code finishes

%}\batchmode

As this stage, % is defined as \message[Just another TeX hacker, where [ will act as an opening brace but there is no closing brace. That is supplied by the literal }, which comes after % is finally used.


Maybe this is cheating, but try running the code with \tracingall before the first line. You can watch TeX work.

Tags:

Tex Core