%
%  Hi there, folks!!!
%
%  Here is a macro file that is intended to make TeX programs easy
%  to read!
%
%  It is based on my BaSiX interpreter.
%
%  - Andrew Marc Greene
%    <amgreene@mit.edu|
%
%  Begun 19-May-90
%
\def\flageol{\catcode13=13}
\def\endflageol{\catcode13=5}
\flageol\let\eol
\endflageol
\def\spc{ }
\def\empty{}
\long\def\gobble#1{}
\def\IW#1{\immediate\write16{#1}\ignorespaces}

\catcode`\@=11%  Let us use @'s in commands
\catcode`\!=11%  Screamers, too.
\catcode`\|=13%  | is active,
\let|\expandafter%  and it means expandafter
|\def|\oldeverypar|{\the\everypar}
\let\oldpar\par \parskip=0pt 

\def\RestoreEnvironment{%
\catcode`\@=12%  Now other
\catcode`\!=12%  This, too.
\catcode`\|=12%  Aren't we all?
|\everypar|{\oldeverypar}\let\par\oldpar}

\def\True{TT}
\def\False{TF}
%
%  The \scan routine reads the next WORD and then calls \afterscan.
%
%  As syntactic sugar, one can write \after\foo to set \afterscan to
%  \foo.
%
%  Here are the rules governing WORD.  Initial whitespace is
%  discarded.  The word is the next single character, unless that
%  character is one of the following:
%
%   A-Z or a-z:  [A-Z,a-z]+
%
\newif\ifscan  %  shall we continue scanning?
%
\def\scan{\def\word{}\futurelet\q\scanFirst}
%
\def\scanFirst{%  Checks the first character to determine type.
\let\next\scanIter
\expandafter\if\spc\noexpand\q         %  Space -- ignore it
  \let\next\scanSpace\else
\if\eol\noexpand\q                     %  End of line -- no word here
  \ifx\empty\word\let\next\scanPar\else
  \let\next\scanEnd\fi\else
\ifcat A\noexpand\q                    %  Then we have an identifier
  \let\scanTest\scanIdentifier
\else
  \let\scanTest\scanfalse
\fi\fi\fi\next}
%
\def\scanIter#1{|\def|\word|{\word #1}\futurelet\q\scanContinueP}
\def\scanContinueP{\scanTest\ifscan\let\next\scanIter
  \else\let\next\scanEnd\fi\next}
%
\def\scanPar\par{\scan}% gobble pars 
\long\def\scanSpace#1{\scan #1}% If whitespace, gobble it and try again.
\def\scanIdentifier{\ifcat A\noexpand\q\scantrue\else\scanfalse\fi}
%
\long\def\scanEnd#1{\afterscan #1}% dumps trailing spaces.
\def\after{\let\afterscan}
%
\def\parse{|\Attempt!|{\word}}
%
\def\Attempt!#1{|\ifx\csname #1\endcsname\relax
\IW{Undefined command #1 --- check the spelling or context!}
\IW{Then enter the replacement below:}
\read16 to\next\else|\let|\next\csname #1\endcsname\fi
\next}
%
%  #1 is paramlist; #2 is replace text.
%
\def\Define{\after\Define!\scan}
\def\Define!#1:#2EndDefine{\gobble{Def}|\ifx\csname \word\endcsname\relax
\IW{Defining \word.}\else
\IW{WARNING: Duplicate Definition of \word.}\fi
|\def\csname \word\endcsname#1{#2}\trigger}
%
%  Superdefine takes the form of ``C'' code (except that params are
%  untyped), e.g.: 
%
%  void foo(bar,baz)
%  {
%    int quux;
%
%    quux = #bar + twice(#baz);
%    typeset("The answer is ", str2num(quux));
%  }
%
%  int twice(once)
%  {
%    return(#once * 2)
%  }
%
\def\SuperDefine{\after\SuperDefine!\scan}
\def\SuperDefine!#1:{\edef\csname!{\word}\def\paramlist!{#1}%
\after\SuperScan!\scan}
%
\def\SuperHash!{##}
\def\SuperOpen!{\bgroup}
\def\SuperClose!{\egroup}
%
\def\SuperScan!{% word has next significant item
\ifx\SuperHash!\word\let\next\SuperParam!\fi
\ifx\SuperClose!\word\let\next\SuperCloseBrace!\fi
\ifx\SuperOpen!\word\let\next\SuperOpenBrace!\fi


}% end of SuperScan
%
%
\def\Append #1:{|\ifx\csname #1\endcsname\relax
\IW{Attempt to append to undefined command #1.}
\IW{Please enter recovery code below:}
\read16 to\next\else\IW{Appending to #1.}
\def\!Append!{|\Append!\csname #1\endcsname !}%
\let\next\!Append!
\fi\next}
\def\Append!#1!#2EndAppend{\expandafter\def\expandafter#1\expandafter{#1#2}%
\trigger}
%
\long\def\Comment #1EndComment{\trigger}
%
%  Knuth makes these outer.  I want them to be inner.  So there.
%
\def\newcount{\alloc@0\count\countdef\insc@unt}
\def\newdimen{\alloc@1\dimen\dimendef\insc@unt}
\def\newskip{\alloc@2\skip\skipdef\insc@unt}
\def\newmuskip{\alloc@3\muskip\muskipdef\insc@unt}
\def\newbox{\alloc@4\box\chardef\insc@unt}
%\def\newhelp#1#2{\newtoks#1#1=\expandafter{\csname #2\endcsname}}
\def\newread{\alloc@6\read\chardef\sixt@@n}
\def\newwrite{\alloc@7\write\chardef\sixt@@n}
\def\newfam{\alloc@8\fam\chardef\sixt@@n}
%
% Permit initialization of stuff:
\def\!equals#1;{\csname\word\endcsname=#1\relax\trigger}
%
\def\Int{\after\!int\scan}
\def\!int#1{\IW{Count \word}
|\newcount\csname\word\endcsname
\if#1,\let\next\Int\else
\if#1;\let\next\trigger\else
\if#1=\let\next\!equals\else
\IW{ILLEGAL CHAR #1}\fi\fi\fi\next}
\let\Count=\Int
%
\def\Dimen{\after\!dimen\scan}
\def\!dimen#1{\IW{Dimen \word}
|\newdimen\csname\word\endcsname
\if#1,\let\next\Dimen\else
\if#1;\let\next\trigger\else
\if#1=\let\next\!equals\else
\IW{ILLEGAL CHAR #1}\fi\fi\fi\next}
%
\def\Skip{\after\!skip\scan}
\def\!skip#1{\IW{Skip \word}
|\newskip\csname\word\endcsname
\if#1,\let\next\Skip\else
\if#1;\let\next\trigger\else
\if#1=\let\next\!equals\else
\IW{ILLEGAL CHAR #1}\fi\fi\fi\next}
%
\def\MuSkip{\after\!muskip\scan}
\def\!muskip#1{\IW{MuSkip \word}
|\newmuskip\csname\word\endcsname
\if#1,\let\next\MuSkip\else
\if#1;\let\next\trigger\else
\if#1=\let\next\!equals\else
\IW{ILLEGAL CHAR #1}\fi\fi\fi\next}
%
\def\Box{\after\!box\scan}
\def\!box#1{\IW{Box \word}
|\newbox\csname\word\endcsname
\if#1,\let\next\Box\else
\if#1;\let\next\trigger\else % note that box can't be preinitialized.
\IW{ILLEGAL CHAR #1}\fi\fi\next}
%
\def\Font{\after\!font\scan}
\def\!font#1;{\IW{Loading font \word}%
|\font\csname\word\endcsname#1\relax\trigger}
%
{\catcode32=12\catcode`\|=12
\gdef\End{%
\IW{}
\IW{ +-------------------------------------------------------+}
\IW{ | Ending Literate TeX, returning to normal TeX endgame. |}
\IW{ +-------------------------------------------------------+}
\end}
%
\gdef\Main:{%
\IW{}%
\IW{ +------------------------------------------------------+}%
\IW{ | Ending Literate TeX, beginning normal TeX behavior.  |}%
\IW{ +------------------------------------------------------+}%
\RestoreEnvironment}
%
\IW{}%
\IW{ +---------------------------------------------------+}%
\IW{ | Welcome to              ---+---  \   /  |         |}%
\IW{ |                            | +--- \ /   |         |}%
\IW{ |             Literate       | |     X    |         |}%
\IW{ |                            | +-   / \             |}%
\IW{ |                            | |   /   \  *         |}%
\IW{ |                              +---                 |}%
\IW{ +---------------------------------------------------+}%
\IW{ |  by Andrew Marc Greene, <amgreene@mit.edu>        |}%
\IW{ +---------------------------------------------------+}%
\IW{}}%
%
\gobble{This is random text.}%
\everypar={\after\parse\scan}%
\def\trigger{\after\parse\scan}%
