% $Header: /home/tully/hilfingr/lib/tex/macros/RCS/prog-lang.sty,v 1.7 1995/05/29 23:38:08 hilfingr Exp $
% Definitions for program and BNF environments
% (Typewriter-font identifiers version).
%
% Copyright (C) 1987 by the Regents of the University of California
% Author: P. N. Hilfinger
%

{\catcode`\|=\active
\gdef\defineBNF@{
    \def\@bnfnewline{\- \- \pagebreak[2] \\{}}
    \def\define##1{\@bnfnewline	##1 ::= \+ \+  \def\@bnfnewline{\- \- \\{}}}
    \def\alt{ \\ \nopagebreak \< $|$ \> }
    \catcode`\|=\active\let|=\progBR@
}}


\newenvironment {bnf} {
	\defineBNF@\begin{tabbing}
	XXX \= XXX \= $|~~$ \= XXX \= \kill\+\def\@bnfnewline{\>}}{\end{tabbing}}

\newcommand {\k} [1] {\hbox{\bf #1}}

{\catcode`\ =\active\gdef\progblank@{\catcode`\ \active\let \@progobeysp}}

\def\a@#1 {\catcode`#1=\active}

\def\progPuncAct@{\a@\| \a@\# \a@\_ \a@\^ \a@\$ }

{\progPuncAct@%
\gdef\progPuncDef@{%
\progPuncAct@%
\let|\progBR@ \let#\progHS@ \let_\progUN@ \let^\progCA@ \let$\progDO@}}

\def\progPuncDeact{%
\catcode`\|=12 \catcode`\_=8 \catcode`\^=7 \catcode`\ =10 %
\catcode`\^^M=5 \catcode`\`=12 }

\def\m@#1{\ifmmode#1\else$#1$\fi}
\def\progBR@{\m@|}
\def\progHS@{\/{\tt \#}}
\def\progUN@{\ifmmode_\else\_\fi}
\def\progCA@{\ifmmode^\else\^\relax\fi}
\def\progDO@{\ifmmode$\egroup\else\bgroup\progPuncDeact$\fi}

\def\@progobeysp{\leavevmode{} }

{\catcode`\`=\active
\gdef\defProgLeftQuote@{\let`=\progLeftQuote@}}
\def\progLeftQuote@{\futurelet\nextChar@\progLeftQuote@@}
\def\progLeftQuote@@{\ifx\nextChar@\bgroup\let\nextAction@=\relax\else%
\let\nextAction@=\progLeftQuote@@@\fi\nextAction@}
\def\progLeftQuote@@@#1{%
\let\a@@=a\ifcat\noexpand#1\a@@{\bf #1}\let\nextAction@=\progLeftQuote@%
\else\let\nextAction@=#1\fi%
\nextAction@}

\def\progdocr@{\@ifnextchar\end{}{\\{}}}

{\catcode`\^^M=\active%
\gdef\progcr@{\catcode`\^^M=\active\def^^M{\progdocr@}}%
\gdef\progcrgobble@{\@ifnextchar^^M{\@gobble}{}}}

\newenvironment {program} {
	\protect\frenchspacing\progPuncDef@\progblank@\progcr@
	\catcode`\`=\active\defProgLeftQuote@
	\tt \begin{tabbing} XXX \= \kill \+ \> \progcrgobble@}{\end{tabbing}}

\newcommand {\prog} {\bgroup\protect\frenchspacing\progPuncDef@\progblank@\catcode`\`=\active\defProgLeftQuote@\tt\prog@}
\def\prog@#1{#1\egroup}

\def\dogt@{\m@>}
\def\dolt@{\m@<}
{\catcode`\<=\active\catcode`\>=\active
 \gdef\nicegtlt{
	\catcode`\<=\active\catcode`\>=\active\def>{\dogt@}\def<{\dolt@}}}

\newcounter{algorithm}
\def\thealgorithm{\@arabic\c@algorithm}
\def\fps@algorithm{tp}
\def\ftype@algorithm{4}
\def\ext@algorithm{loa}
\def\fnum@algorithm{Algorithm \thealgorithm}
\def\algorithm{\@float{algorithm}}
\let\endalgorithm\end@float
\@namedef{algorithm*}{\@dblfloat{algorithm}}
\@namedef{endalgorithm*}{\end@dblfloat}
