Key Concepts
- Semantics: The meaning and interpretation of code, distinct from syntax (structure).
- Main Control: The central driver program in Tech, acting as the "chief executive."
- Reswitch: A label within Main Control where processing can loop back to re-evaluate tokens.
- Command Codes (cur_command): Numerical identifiers for various actions Tech can perform.
- Action Procedures: Subroutines called by Main Control to perform specific semantic tasks.
- Primitives: Fundamental operations in Tech, often implemented as primitive procedures.
- Hash Table: A data structure used to store and retrieve information about primitives.
- Save Stack: A stack used for saving and restoring program state, including fixups and boundary words.
- Nest Stack: A stack used to save semantic state variables, such as the current mode, list head, and tail.
- Modes (H mode, V mode, M mode): Different operational states within Tech that dictate how lists are built and processed.
- Whatsit Nodes: Special data structures used for extensions to Tech.
- Extensions: New features added to Tech, often requiring hooks into the main program.
- Confusion: An error handling procedure in Tech used for internal consistency checks.
- H Box: A fundamental data structure in Tech representing a horizontal box.
- Scan Spec: A subroutine that scans for specifications like dimensions or keywords.
- Begin Box / End Box: Procedures that initiate and finalize the construction of a box.
- Packaging Subroutines (H pack, V pack): Procedures that convert lists into boxes.
- Hyphenation: The process of breaking words at the end of lines, a topic for a future lecture.
Semantics in Tech
This lecture delves into the semantics of the Tech typesetting system, focusing on how it interprets and acts upon the parsed syntax. Semantics is described as the "big region in the middle" of Tech's architecture, interacting with local variables, equivalence tables, and receiving tokens from syntax routines.
Math Semantics
While not the primary focus of this hour, math semantics is highlighted as a significant component, accounting for approximately 40% of the semantic code. It involves specialized data structures called "NOADs" for mathematical entities like fractions, square roots, and binary relations. A key routine, M list to H list, converts math-type lists (M list) into horizontal-type lists (H list) that the rest of Tech can process.
Main Control: The Chief Executive
The core of Tech's semantic processing lies in its "main control" procedure, also referred to as the "chief executive" or "hub of the web." This program orchestrates the execution of various subroutines.
- Structure: Main control is characterized by a large
casestatement that branches based on thecur_command(current command code) combined with the current mode (e.g.,V mode + skip,H mode + H skip). - Inner Loop Optimization: To ensure speed, especially during text processing, the main control procedure is highly optimized. Common operations like
H mode + letterandH mode + other charare handled directly within main control to avoid procedure call overhead. reswitchLabel: This label allows for re-evaluation of tokens without fetching a new one, useful for handling variations or disguises of commands. It also serves as a point for diagnostic tracing.- Action Procedures: For most command/mode combinations, main control calls specific "action procedures" to perform the actual semantic task. This modularity helps manage procedure size limitations imposed by some Pascal compilers.
- Command Codes: Section 201 and subsequent sections list the command codes. Examples include
charnum,math charnum,x-ray(internal code forshow instructions),make box, andH move. - Prefix Commands: Codes above
max_nonprefixed_command(currently 69) are often prefix commands. These can be handled more efficiently by a singleprefix_commandprocedure, potentially reducing the size of the main control'scasestatement.
Understanding Primitives and Command Codes
The lecture explains how to trace the execution of a primitive command like show box.
- Index Lookup: The index of the Tech code lists primitives alphabetically.
show box primitiveis found on page 430. InitechProgram: Primitives are loaded into the hash table by theInitechprogram.primitiveProcedure: A special procedure used to enter all primitives.show boxis called viaprimitivein module 1159.- Command and Character Codes:
show boxis associated with the command codex-rayand a character codeshow_box_code(defined as 1). print_command_and_charProcedure: This procedure is used for tracing, translating internal command codes (likex-ray) into symbolic names (like "show box") by looking at thecur_charcode. It's designed to be robust and avoid error messages, even for unexpected inputs.
Example: show box Semantics
The process of show box is traced as a typical example of simple semantics:
x-rayCommand:show boxis recognized ascur_command = x-ray.- Main Control Branch: The
any_mode x-raycase in main control calls theshow_whateveraction procedure. show_whateverProcedure: This procedure checkscur_char. If it'sshow_box_code, it calls module 1164,show_the_current_contents_of_a_box.show_the_current_contents_of_a_box: This involves scanning an 8-bit integer (for box index), printing a newline and ">", and then displaying the box's contents usingshow_box_depthandshow_box_breadthparameters.- Global Variables (
curval): The use of global variables likecurvalis discussed, highlighting the danger of them being overwritten by other procedures.
Extending Tech
Extensions to Tech are typically handled through a dedicated mechanism to minimize changes to the core program.
- Module 1209: Discusses extensions to Tech.
- Hooks: Extensions often rely on "hooks" provided in the main program.
ExtensionCommand Code: When possible, new features should use theExtensioncommand code with a specificcharfield to identify the extension type.- Action Procedures for Extensions: Module 1216 (
any_mode_extension) and 1217 (do_extension) handle extensions. confusionProcedure: Used for internal consistency checks. It has aspotlessglobal Boolean variable. Ifspotlessis true, it prints "this can't happen"; otherwise, it prints a different message.- Whatsit Nodes: New types of data structures for extensions. Module 1225 shows how
whatsitnodes can have subtypes for different extensions (e.g., open, send, close, extend). - Extension Lifecycle: Adding a new
whatsittype requires implementing procedures for creating, printing, and freeing those nodes. Modules 1225, 1226, and 1227 cover these aspects. - Recommendation for Vanilla Tech: Users are strongly advised to maintain a "vanilla" (unextended) version of Tech to ensure program compatibility across different installations.
Example: Processing an H Box
The lecture walks through the detailed processing of an H box command, illustrating the interplay of various components:
H boxSpecification: Tech needs to look ahead to determine if a size is specified (e.g., "to 100 points" - exact size, or "expand by 100 points" - relative size).- Context:
H boxcan appear in various contexts (e.g., preceded bylower,ship out,leaders,set box). This context is stored on the Save Stack. - Save Stack Usage: The Save Stack stores information about the context of the box being built, including group types (e.g.,
H box group). Make BoxCommand: The command code forH boxisMake Box.any_mode_Make_Box(Module 951): This procedure saves the context (e.g., a shift amount of 0) onto the Save Stack and callsBegin_box.Begin_box(Module 958): This procedure initiates the construction of the box. It determines the type of box (H box, V box, V top) based on thecur_charcode.H boxPrimitive: The primitive forH boxhas the command code& H boxand achar_codeofV_top_code + H_mode.- Restricted Horizontal Mode: For
H box, Tech entersrestricted horizontal mode(minus H mode). Scan_SpecSubroutine: This scans for the size specification (e.g., "100 points") and pushes the type of specification (0 for exact, 1 for expand) and the dimension onto the Save Stack.New_Save_Level H_box_group: A boundary word is pushed onto the Save Stack to mark the beginning of anH box group.Push_Nest: Semantic state variables, including the current mode, list head, tail, and space factor, are saved onto the Nest Stack. The mode is changed tominus k(e.g.,minus H mode), and an empty list is initialized. The space factor is set to 1000 (normal).- Processing Character 'A': When the character 'A' is encountered in
H mode, it's processed byMain_loop(Module 907). Main_loop: Appends the character to the currentH list. This involves creating achar_nodeand updating thetailpointer. Font information and character codes are stored.- Handling Spaces: If a space follows, Tech looks up the glue specification for the current font and creates a
glue_node. Handle_Right_Brace(Module 947): When a right brace is encountered, this procedure is called. It checks the current group type.H_box_group: If the group is anH box group, it callspackage_of_0.package_of_0(Module 965): This procedure unwinds the Save Stack (Unsave) to remove fixups and declarations. It then retrieves the context information (e.g., size specification) from the Save Stack.H_pack: This packaging subroutine takes the constructed list and converts it into anH boxnode. It calculates the box's dimensions based on font metrics and glue specifications.Pop_Nest: The semantic state is restored from the Nest Stack, and the previous list head is recycled.Box_endProcedure: The newly created box is added to the current list in the outer mode, applying any context-specific actions (e.g., lowering, raising).
This detailed walkthrough of H box processing demonstrates how Tech manages complex structures by saving and restoring state on the Save and Nest stacks, utilizing packaging subroutines, and handling various contexts and specifications. The lecture concludes by mentioning upcoming topics like breaking paragraphs into lines and hyphenation.
AI summaries can miss context or contain errors. Check important details against the original video.





