summaryrefslogtreecommitdiff
path: root/info/examples/tip/doloop.tip
blob: b15b23cf8f11df6048a25609ef70c042bbda180a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
% This macro source file is from the four volume series
% "TeX in Practice" by Stephan von Bechtolsheim, published
% 1993 by Springer-Verlag, New York.
% Copyright 1993 Stephan von Bechtolsheim.
% No warranty or liability is assumed.
% This macro may be copied freely if no fees other than
% media cost or shipping charges are charged and as long
% as this copyright and the following source code itself
% is not changed. Please see the series for further information.
%
% Version: 1.0
% Date: May 1, 1993
%
%
% This source code is documented in 27.1.8, p. III-412.
% Original source in file "macros7.TEX", starting line 768.
\wlog{L: "doloop.tip" ["macros7.TEX," l. 768, p. III-412]}%
% This file DOES belong to format "texip."
\catcode`\@ = 11
\newcount\@DoLoopNesting
\@DoLoopNesting = 0
\newcount\@DoLoopLimit
\newcount\@DoLoopLimitTwo
\newcount\@DoLoopLimitThree
\def\DoLoop #1#2#3#4#5{%
    \global\advance\@DoLoopNesting by 1
    \ifnum\@DoLoopNesting > 3
        \errmessage{\string\DoLoop: nesting beyond three levels
            is not supported.}%
    \fi
    \ifnum #3 = 0
        \errmessage{\string\DoLoop: step value (parameter 3) is 0!}% 
    \else
        \ifcase\@DoLoopNesting
        \or
            \@DoLoop{#1}{#2}{#3}{#4}{#5}%
            {\@DoLoopLimit}{\DoLoopBody}%
        \or
            \@DoLoop{#1}{#2}{#3}{#4}{#5}%
            {\@DoLoopLimitTwo}{\DoLoopBodyTwo}%
        \or
            \@DoLoop{#1}{#2}{#3}{#4}{#5}%
            {\@DoLoopLimitThree}{\DoLoopBodyThree}%
        \fi
    \fi
    \global\advance\@DoLoopNesting by -1
}
\def\@DoLoop #1#2#3#4#5#6#7{%
    \ifnum #3 > 0
        #6 = #4% 
        \advance#6 by 1
        #1 = #2% 
        \@DoLoopBodyPositive{#1}{#3}{#5}% 
            {#6}{#7}%
    \else
        #6 = #4% 
        \advance#6 by -1
        #1 = #2% 
        \@DoLoopBodyNegative{#1}{#3}{#5}% 
            {#6}{#7}%
    \fi
    \@DoLoopIterate{#7}%
}
\def\@DoLoopBodyPositive #1#2#3#4#5{%
    \def #5{% 
        \ifnum #1 < #4\relax
            #3% 
            \advance #1 by #2% 
    }%
}
\def\@DoLoopBodyNegative #1#2#3#4#5{%
    \def #5{%
        \ifnum #1 > #4\relax
            #3% 
            \advance #1 by #2% 
    }% 
}
\def\@DoLoopIterate #1{%
        #1\relax
        \def\@DoLoopNext{\@DoLoopIterate{#1}}%
    \else
        \def\@DoLoopNext{\relax}%
    \fi
    \@DoLoopNext
}
\catcode`\@ = 12