(This is a low-level sequel to another answer. Posting this as a separate answer because it would make the other one very long, and this is probably more detail than anyone wants to know. But if your “why” and “how” are not answered by the high-level description and you're further interested in all the gory details of the implementation…)
TeX is a program. Internally, this program maintains a table called the “table of equivalents” — a large global variable of array type, called eqtb
, defined in section 253 of the program:
program TEX; { ... }
var
{ ... }
eqtb: array [1 .. eqtb_size] of memory_word; {§253}
{ ... }
where memory_word
is basically a 4-byte blob of memory, with interpretation as given below.
As documented in section 220, this table of equivalents stores:
- the equivalents for control sequences (in regions 1 and 2),
- the values of various glue parameters (region 3),
- the values of various other integer parameters (regions 4 and 5), and
- the values of various dimension (length) parameters (region 6).
Each of these equivalents is stored (§221) as a level (used for saving and restoring inside groups), a eq_type (a command code, §207–210), and the actual equiv, an integer whose interpretation depends on the type (a table index, a pointer to a token list…).
Whenever the program needs to access the value of one of its internal parameters, it uses the corresponding entry from the table. For example, suppose the baseline skip is stored in table entry 2883. When appending to a vertical list the program may need the baseline skip (§679), in which case it uses (essentially) eqtb[2883].equiv
.
Additionally, the program provides some “primitives” like \baselineskip
or \escapechar
for setting some of these table entries: these simply map a control sequence name to a command that sets that table entry. More precisely, in the equivalent for that name, the eq_type
is set to be a command code like assign_int
or assign_glue
, with equiv
holding which table entry is to be assigned. For example, if you don't redefine the primitive, the equivalent for the control sequence baselineskip
(stored in region 2) would have an eq_type
of assign_glue
, and an equiv
of 2883.
When the program encounters that corresponding control sequence in the input (as a command by itself), it follows the equivalent and does the corresponding assignment.
Here is a miniature version of the TeX program, with only the relevant parts, and some with some liberties taken for simplicity:
program TEX; { §4 ... }
var {§13}
{ ... }
cur_cmd: int; {§297}
cur_chr: int;
eqtb: array [1 .. eqtb_size] of memory_word; {§253}
{ ... }
procedure primitive(s: string, eq_type: int, equiv: int); {§264}
begin
{ ... set eqtb[hash(s)] to (level 1 and) the eq_type and equiv passed here ...}
end
procedure init_prim; {§1336}
begin {...}
primitive("baselineskip", assign_glue, 2883); {§226}
{...}
primitive("escapechar", assign_int, 5308); {§238}
{...}
end
procedure prefixed_command; {§1211}
begin {...}
case cur_cmd of
set_font: { ... §1217 ...}
def: {... §1218 ...}
{ ... }
assign_int: {§1228}
begin
p ← cur_chr; {e.g., 5308 in case of the primitive \escapechar}
scan_optional_equals;
scan_int;
word_define(p, cur_val); {...sets eqtb[p], accounting for level...}
end;
assign_glue: {...similar...}
{...}
endcases;
end;
procedure main_control; {§1030}
begin
while true do
begin
{... read a token, i.e. set cur_cmd and cur_chr ...}
case cur_cmd of
{...}
assign_int: prefixed_command; {§1210}
assign_glue: prefixed_command;
{...}
endcases;
end;
end;
{...}
begin {§1332}
{...}
init_prim;
{...}
main_control;
{... cleanup ... }
end.
\let
for creating more "entrances" for accessing the same thing. E.g.,you can do\let\escapecharcopy=\escapechar
and then use\escapecharcopy=...
for assigning values to the same parameter of TeX that usually can have values assigned via\escapechar=...
. – Ulrich Diez Jun 14 '19 at 18:24