Debian Codegraph @

texmf-dist/tex/generic/gates/gates.tex

raw
1 % This is the Gates package.
2 % Relevant information can be found in gates-doc.pdf
3 % (or gates-doc.txt in a text editor).
4 %
5 % Author: Paul Isambert.
6 % E-mail: zappathustra AT free DOT fr
7 % Comments and suggestions are welcome.
8 % Date: May 2012.
9
10 \csname Come on, Gates is already loaded\endcsname
11 \expandafter\let\csname Come on, Gates is already loaded\endcsname\endinput
12 \input texapi
13 \setcatcodes{_@=11}
14
15 \def\gates@error{\senderror{Gates}}%
16 \def\gates@error_nogate#1{\gates@error{`#1' isn't a gate}}
17 \def\gates@error_nogatelist#1{\gates@error{`#1' isn't a gate list}}
18 \def\gates@error_nogatein#1#2{\gates@error{No gate `#1' in list `#2'}}
19
20 \def\gates@family_update#1{%
21 \def\gates@family_current{#1}%
22 }
23 \newstring{:}
24 \def\gates@family_check{%
25 \gates@family_pass\unbrace
26 }
27 \def\gates@family_pass#1#2{%
28 \passexpanded{\passtrim{#2}{\gates@family_passdo{#1}}}{\gates@family_current}%
29 }
30 \def\gates@family_passdo#1#2#3{%
31 \passexpanded{\ifcontains{:}}{#2}
32 {#1{#2}}{#1{#3:#2}}%
33 }
34 \def\gates@ifexists#1#2{%
35 \ifcs{gate \gates@family_check{#1}:#2}%
36 }
37
38
39 \def\gates@gates#1#2{%
40 \ifstring{#2}[
41 {\gates@family_update{#1}%
42 \gates@gates_macro}
43 {\ifstring{#2}(
44 {\gates@family_update{#1}%
45 \gates@gates_list}
46 {\gates@gates_check{#1}}}#2%
47 }
48 \def\gates@gates_macro[#1]{%
49 \passtrim{#1}\gates@action@def
50 }
51 \def\gates@gates_list(#1){%
52 \passtrim{#1}\gates@action@list
53 }
54 \def\gates@gates_check#1#2 {%
55 \ifelseif{%
56 {\ifcs{gates@action@#2}}
57 {\gates@family_update{#1}\usecs{gates@action@#2}}
58 {\ifcs{gates@action@#2_withfamily}}
59 {\usecs{gates@action@#2_withfamily}{#1}}
60 {\ifcs{gate \gates@family_passdo\unbrace{#2}{#1}:code}}
61 {\gates@family_update{#1}\gates@action@execute{#2}}
62 \iftrue
63 {\gates@error{Action `\gates@family_passdo\unbrace{#2}{#1}' doesn't exist and there is no gate with that name}}}%
64 }
65
66 \def\gates{%
67 \gates@gates{gates}%
68 }
69
70 \def\gates@action#1{%
71 \long\defcs{gates@action@#1}%
72 }
73 \def\gates@action_withfamily#1{%
74 \long\defcs{gates@action@#1_withfamily}%
75 }
76
77 \gates@action{new}#1#2{%
78 \def#1{%
79 \gates@gates{#2}%
80 }%
81 }
82
83 \gates@action_withfamily{family}#1{#1}
84
85 %%%
86 %%% The gate stack.
87 %%%
88 \def\gates@stack{}
89 \protected\def\gates@stack_push#1{%
90 \xdef\gates@current_gate{#1}%
91 \xdef\gates@stack{{#1}{\gates@stack}}%
92 }
93 \protected\def\gates@stack_pop{%
94 \expandafter\gates@stack_popit\gates@stack\gates@end
95 }
96 \def\gates@stack_popit#1#2\gates@end{%
97 \gdef\gates@stack{#2}%
98 \gates@stack_getcurrent#2{}\gates@end
99 }
100 \def\gates@stack_getcurrent#1#2\gates@end{%
101 \gdef\gates@current_gate{#1}%
102 }
103
104 %%%
105 %%% Tracing and showing.
106 %%%
107 \def\gates@trace_value{0}
108 \gates@action{trace}#1{%
109 \defcs{gates@trace_value:\gates@family_current}{#1}%
110 }
111 \def\gates@trace_tab{}
112 \def\gates@trace_white{}
113 \def\gates@trace_addtab{%
114 \edef\gates@trace_tab{\gates@trace_tab. }%
115 \edef\gates@trace_white{\gates@trace_white\unbrace{ } }%
116 }
117 \def\gates@trace_untab{%
118 \passexpanded{\removeprefixin{. }}\gates@trace_tab\gates@trace_tab
119 \passexpanded{\passexpanded\removeprefixin{\unbrace{ } }}\gates@trace_white\gates@trace_white
120 }
121 \def\gates@trace_write#1#2{%
122 \iffcs{gates@trace_value:\gates@trace_getfamily#1\gates@terminator}
123 {\ifnum\usecs{gates@trace_value:\gates@trace_getfamily#1\gates@terminator}>0
124 \immediate\write16{\gates@trace_white #2}%
125 \fi}}
126 \def\gates@trace_writetab#1#2{%
127 \iffcs{gates@trace_value:\gates@trace_getfamily#1\gates@terminator}
128 {\ifnum\usecs{gates@trace_value:\gates@trace_getfamily#1\gates@terminator}>0
129 \immediate\write16{\gates@trace_tab #2}%
130 \fi}}
131 \def\gates@trace_getfamily#1:#2\gates@terminator{#1}
132
133 \gates@action{show}#1{%
134 \immediate\write16{}%
135 \gates@family_pass\gates@show{#1}%
136 }
137 \def\gates@show#1{%
138 \ifcs{gate #1:list}
139 {\gates@show_write{#1 is an l-gate}{}{#1}%
140 \passexpandedcs{\gates@show_loop{#1}{. }}{gate #1:list}}%
141 {\ifcs{gate #1:code}
142 {\gates@show_write{#1 is an m-gate}{}{#1}}{\gates@error_nogate{#1}}}%
143 }
144 \newfor\gates@show_loop{2}#3,{%
145 \gates@show_write{#2#3}{#1_subgate_}{#3}%
146 \iffcs{gate #3:list}{\passexpandedcs{\gates@show_loop{#3}{#2. }}{gate #3:list}}%
147 }
148 \def\gates@show_write#1#2#3{%
149 \immediate\write16{#1
150 \reverse\iffemptystring{#2}{(\ifcs{gate #3:list}lm-gate) }%
151 [\usecs{gate #3:args}]
152 (status: \ifcase\usecs{gate #2#3:status}
153 \or open\or ajar\or skip\or close\fi)%
154 \gates@show_condition{#2#3}{conditional}%
155 \gates@show_condition{#2#3}{loop}%
156 \gates@show_condition{#2#3}{loopuntil}%
157 }%
158 }
159
160 \def\gates@show_condition#1#2{%
161 \iffcs{gate #1:#2}
162 { (#2: \expandafter\expandafter\expandafter\gates@show_conditionstrip
163 \expandafter\meaning\csname gate #1:#2\endcsname)}%
164 }
165 \def\gates@show_conditionstrip#1->{}%
166
167
168 %%%
169 %%% Checking conditions in gate definition.
170 %%%
171 \long\def\gates@conditions_get#1{%
172 \ifnextnospace?
173 {\gates@conditions_getlaunch{#1}}
174 {#1{{}{}{}{}}}%
175 }
176 \newstring{,}
177 \long\def\gates@conditions_getlaunch#1?#2{%
178 \ifsuffix,{#2}
179 {\gates@conditions_getloop{#1}{}{}{}{}{#2}}
180 {\gates@conditions_getloop{#1}{}{}{}{}{#2,}}%
181 }
182 \newfornoempty\gates@conditions_getloop{5}#6=#7,{%
183 \ifelseif{%
184 {\passtrim{#6}\ifstring{status}} {\passarguments{#1}{\trim{#7}}{#3}{#4}{#5}}
185 {\passtrim{#6}\ifstring{conditional}} {\passarguments{#1}{#2}{#7}{#4}{#5}}
186 {\passtrim{#6}\ifstring{loop}} {\passarguments{#1}{#2}{#3}{#7}{#5}}
187 {\passtrim{#6}\ifstring{loopuntil}} {\passarguments{#1}{#2}{#3}{#4}{#7}}
188 \iftrue {\gates@error{`#6' isn't a condition}}%
189 }
190 }[#1{{#2}{#3}{#4}{#5}}]
191 \long\def\gates@conditions_make#1#2#3#4#5{%
192 \gates@list_checkmaster
193 {\gates@action@add{#1}{\gates@list_master}%
194 \reverse\iffemptystring{#2}{\usecs{gates@action@#2}{#1}{\gates@list_master}}%
195 \reverse\iffemptystring{#3}{\gates@action@conditional{#1}{\gates@list_master}{#3}}
196 \reverse\iffemptystring{#4}{\gates@action@loop{#1}{\gates@list_master}{#4}}
197 \reverse\iffemptystring{#5}{\gates@action@loopuntil{#1}{\gates@list_master}{#5}}}
198 {\straightenif{ifnum}{\gates@list_subcount>0 }
199 {\gates@error{Wrong level of embedding for gate `#1'}}%
200 {\reverse\iffemptystring{#2}{\usecs{gates@action@#2!}{#1}}%
201 \reverse\iffemptystring{#3}{\usecs{gates@action@conditional!}{#1}{#3}}
202 \reverse\iffemptystring{#4}{\usecs{gates@action@loop!}{#1}{#4}}%
203 \reverse\iffemptystring{#5}{\usecs{gates@action@loopuntil!}{#1}{#5}}}}%
204 }
205
206 %%%
207 %%% Defining list-gates.
208 %%%
209 \newcount\gates@list_subcount
210 \def\gates@list_masterlist{}%
211 \gates@action{list}#1{%
212 \ifnextnospace[
213 {\gates@family_pass\gates@list_checkarg{#1}}
214 {\gates@family_pass\gates@list_checkarg{#1}[0]}%
215 }
216
217 \def\gates@list_checkarg#1[#2]{%
218 \ifcs{gates@execute_get_#2_args}
219 {\gates@conditions_get{\gates@list_create{#1}{#2}}}
220 {\gates@list_create{#1}{0}{{}{}{}{}}[#2]}%
221 }
222
223 \long\def\gates@list_create#1#2#3{%
224 \straightenif{ifnum}{#2<10 }
225 {\xdefcs{gate #1:args}{#2}%
226 \gdefcs{gate #1:status}{\gates@state_open}%
227 \gdefcs{gate #1:conditional}##1##2##3##4##5##6##7##8##9{\iftrue}%
228 \gdefcs{gate #1:list}{}%
229 \long\xdefcs{gate #1:code}##1##2##3##4##5##6##7##8##9{%
230 \ifcase#2
231 \noexpand\passexpandedcs{\noexpand\gates@process{#1}{{}{}{}{}{}{}{}{}{}}}{gate #1:list}%
232 \or \noexpand\passexpandedcs{\noexpand\gates@process{#1}{{##1}{}{}{}{}{}{}{}{}}}{gate #1:list}%
233 \or \noexpand\passexpandedcs{\noexpand\gates@process{#1}{{##1}{##2}{}{}{}{}{}{}{}}}{gate #1:list}%
234 \or \noexpand\passexpandedcs{\noexpand\gates@process{#1}{{##1}{##2}{##3}{}{}{}{}{}{}}}{gate #1:list}%
235 \or \noexpand\passexpandedcs{\noexpand\gates@process{#1}{{##1}{##2}{##3}{##4}{}{}{}{}{}}}{gate #1:list}%
236 \or \noexpand\passexpandedcs{\noexpand\gates@process{#1}{{##1}{##2}{##3}{##4}{##5}{}{}{}{}}}{gate #1:list}%
237 \or \noexpand\passexpandedcs{\noexpand\gates@process{#1}{{##1}{##2}{##3}{##4}{##5}{##6}{}{}{}}}{gate #1:list}%
238 \or \noexpand\passexpandedcs{\noexpand\gates@process{#1}{{##1}{##2}{##3}{##4}{##5}{##6}{##7}{}{}}}{gate #1:list}%
239 \or \noexpand\passexpandedcs{\noexpand\gates@process{#1}{{##1}{##2}{##3}{##4}{##5}{##6}{##7}{##8}{}}}{gate #1:list}%
240 \or \noexpand\passexpandedcs{\noexpand\gates@process{#1}{{##1}{##2}{##3}{##4}{##5}{##6}{##7}{##8}{##9}}}{gate #1:list}%
241 \fi}%
242 \gates@conditions_make{#1}#3%
243 \eaddright\gates@list_masterlist{{#1}}%
244 \gates@list_checknext}
245 {\gates@error{Gates can't take more than 9 arguments}}%
246 }
247
248 \def\gates@list_checknext{%
249 \gates@list_subcount=1
250 \skipspace{\futurelet\gates@list_nextchar\gates@list_checknextchar}%
251 }
252
253 \def\gates@list_checknextchar{%
254 \expandafter\gates@list_checknextcharloop\expandafter{\gates@list_charlist}%
255 }
256
257 \def\gates@list_charlist{.}
258 \gates@action{subchar}#1{%
259 \ifexpression{%
260 \if\noexpand#1[%
261 | \if\noexpand#1(%
262 | \if\noexpand#1<%
263 | \if\noexpand#1?}
264 {\gates@error{You can't use `#1' as a continuation char}}
265 {\eaddright\gates@list_charlist{\string#1}}%
266 }
267
268 \newfor\gates@list_checknextcharloop#1{%
269 \if\noexpand\gates@list_nextchar\noexpand#1%
270 \advance\gates@list_subcount1
271 \afterfi{\breakfor{\gobbleoneand{%
272 \skipspace{\futurelet\gates@list_nextchar\gates@list_checknextchar}}}}%
273 \fi
274 }[\gates@list_getsub]
275
276 \def\gates@list_getsub{%
277 \ifnext[
278 {\gates@list_getsubdef}
279 {\ifnext(
280 {\gates@list_getsublist}
281 {\ifnext<
282 {\gates@list_getsubgate}
283 {\gates@list_subcount=0
284 \def\gates@list_masterlist{}}}}%
285 }
286
287 \def\gates@list_getsublist(#1){%
288 \gates@action@list{#1}%
289 }
290
291 \def\gates@list_getsubdef[#1]{%
292 \gates@def\gdefcs{#1}%
293 }
294
295 \def\gates@list_getsubgate<#1>{%
296 \gates@conditions_get{\gates@list_dosubgate{#1}}%
297 }
298
299 \def\gates@list_dosubgate#1#2{%
300 \gates@list_subgateloop#2{}{#1,}%
301 }
302
303 \newfornoempty\gates@list_subgateloop{5}#6,{%
304 \gates@ifexists{#6}{code}
305 {\gates@family_pass\gates@conditions_make{#6}{#1}{#2}{#3}{#4}%
306 \passarguments{#1}{#2}{#3}{#4}{#6}}
307 {\gates@error_nogate{\gates@family_check{#6}}}%
308 }[\gates@ifexists{#5}{list}
309 {\eaddright\gates@list_masterlist{{\gates@family_check{#5}}}}{}%
310 \gates@list_checknext]
311
312 \def\gates@list_checkmaster{%
313 \let\gates@list_master\gates@HopefullyThisRemainsUndefined
314 \passexpanded{\gates@list_getmaster{}{1}}\gates@list_masterlist
315 }
316
317 \newfor\gates@list_getmaster{2}#3{%
318 \ifnum\gates@list_subcount=\numexpr#2\relax
319 \def\gates@list_master{#3}%
320 \afterfi{\breakfor{\def\gates@list_masterlist{#1{#3}}\firstoftwo}}%
321 \else
322 \afterfi{\passarguments{#1{#3}}{#2+1}}%
323 \fi
324 }[\def\gates@list_masterlist{#1}\secondoftwo]%
325
326
327 %%%
328 %%% Defining macro-gates.
329 %%%
330 \gates@action{def}{\gates@def\gdefcs}
331 \gates@action{edef}{\gates@def\xdefcs}
332
333 \def\gates@def#1#2{%
334 \ifnextnospace[
335 {\gates@family_pass{\gates@def_getargs{#1}}{#2}}
336 {\gates@family_pass{\gates@def_getargs{#1}}{#2}[0]}%
337 }
338
339 \def\gates@def_getargs#1#2[#3]{%
340 \gates@conditions_get{\gates@def_prepare{#1}{#2}{#3}}%
341 }
342
343 \long\def\gates@def_prepare#1#2#3#4#5{%
344 \ifelseif{% Can't use \ifcase, because #5 might contain an unbalanced conditional.
345 {\ifnum#3=0 } {\gates@def_do#1{#2}{#3}{}{}{#5}{#4}}
346 {\ifnum#3=1 } {\gates@def_do#1{#2}{#3}{##1}{{##1}}{#5}{#4}}
347 {\ifnum#3=2 } {\gates@def_do#1{#2}{#3}{##1##2}{{##1}{##2}}{#5}{#4}}
348 {\ifnum#3=3 } {\gates@def_do#1{#2}{#3}{##1##2##3}{{##1}{##2}{##3}}{#5}{#4}}
349 {\ifnum#3=4 } {\gates@def_do#1{#2}{#3}{##1##2##3##4}{{##1}{##2}{##3}{##4}}{#5}{#4}}
350 {\ifnum#3=5 } {\gates@def_do#1{#2}{#3}{##1##2##3##4##5}{{##1}{##2}{##3}{##4}{##5}}{#5}{#4}}
351 {\ifnum#3=6 } {\gates@def_do#1{#2}{#3}{##1##2##3##4##5##6}{{##1}{##2}{##3}{##4}{##5}{##6}}{#5}{#4}}
352 {\ifnum#3=7 } {\gates@def_do#1{#2}{#3}{##1##2##3##4##5##6##7}{{##1}{##2}{##3}{##4}{##5}{##6}{##7}}{#5}{#4}}
353 {\ifnum#3=8 } {\gates@def_do#1{#2}{#3}{##1##2##3##4##5##6##7##8}{{##1}{##2}{##3}{##4}{##5}{##6}{##7}{##8}}{#5}{#4}}
354 {\ifnum#3=9 } {\gates@def_do#1{#2}{#3}{##1##2##3##4##5##6##7##8##9}{{##1}{##2}{##3}{##4}{##5}{##6}{##7}{##8}{##9}}{#5}{#4}}
355 {\iftrue} {\gates@error{Wrong number of arguments}}}%
356 }
357
358 % #1: \gdef or \xdef
359 % #2: gate's name
360 % #3: number of arguments
361 % #4: parameter text (e.g., ##1, ##1##2, etc.) for internal code
362 % #5: replacement text (e.g., {##1}, {##1}{##2}, etc.) for internal code
363 % #6: definition of internal code
364 % #7: conditions
365 \long\def\gates@def_do#1#2#3#4#5#6#7{%
366 \xdefcs{gate #2:args}{#3}%
367 \gdefcs{gate #2:status}{\gates@state_open}%
368 \gdefcs{gate #2:conditional}##1##2##3##4##5##6##7##8##9{\iftrue}%
369 \long#1{gate #2:code}##1##2##3##4##5##6##7##8##9{%
370 \iffcs{gates@trace_value:\gates@trace_getfamily#2\gates@terminator}
371 {\ifnum\usecs{gates@trace_value:\gates@trace_getfamily#2\gates@terminator}=2
372 \gates@trace_args{1}{#2}{#5}%
373 \fi}%
374 \usecs{gate #2:innercode}#5}%
375 \long#1{gate #2:innercode}#4{#6}%
376 \gates@conditions_make{#2}#7%
377 \reverse\iffemptycommand\gates@list_masterlist{\gates@list_checknext}%
378 }
379
380 \newfor\gates@trace_args{2}#3{%
381 \gates@trace_write{#2}{\string##\the\numexpr#1<-\unexpanded{#3}}%
382 \passarguments{#1+1}{#2}%
383 }
384
385 \gates@action_withfamily{type}#1#2{%
386 \ifcs{gate \gates@family_passdo\unbrace{#2}{#1}:code}
387 {\ifcs{gate \gates@family_passdo\unbrace{#2}{#1}:list}{2}{1}}
388 {0}%
389 }
390
391 %%%
392 %%% Copying gates.
393 %%%
394 \gates@action{copy}#1#2{%
395 \gates@family_pass{\gates@family_pass\gates@copy_do{#1}}{#2}%
396 }
397 \def\gates@copy_do#1#2{%
398 \gates@ifexists{#2}{code}
399 {\global\letcstocs{gate #1:args}{gate #2:args}%
400 \gdefcs{gate #1:status}{\gates@state_open}% Better than to copy the gate's status.
401 \global\letcstocs{gate #1:conditional}{gate #2:conditional}%
402 \global\letcstocs{gate #1:loop}{gate #2:loop}%
403 \global\letcstocs{gate #1:loopuntil}{gate #2:loopuntil}%
404 \global\letcstocs{gate #1:code}{gate #2:code}
405 \gates@ifexists{#2}{list}
406 {\global\letcstocs{gate #1:list}{gate #2:list}%
407 \passexpandedcs{\gates@let_list{#1}{#2}}{gate #2:list}}
408 {\global\letcstocs{gate #1:internalcode}{gate #2:internalcode}}}%
409 {\gates@error_nogate{#2}}%
410 }
411
412 \newfor\gates@let_list{2}#3,{%
413 \global\letcstocs{gate #1_subgate_#3:status}{gate #2_subgate_#3:status}%
414 \global\letcstocs{gate #1_subgate_#3:conditional}{gate #2_subgate_#3:conditional}%
415 \global\letcstocs{gate #1_subgate_#3:loop}{gate #2_subgate_#3:loop}%
416 \global\letcstocs{gate #1_subgate_#3:loopuntil}{gate #2_subgate_#3:loopuntil}%
417 }
418
419 %%%
420 %%% Executing gates.
421 %%%
422 \def\gates@ifinlist{%
423 \ifdefined\gates@current_gate
424 \ifcsname gate \gates@current_gate:list\endcsname
425 \expandafter\expandafter\expandafter\firstoftwo
426 \else
427 \expandafter\expandafter\expandafter\secondoftwo
428 \fi
429 \else
430 \expandafter\secondoftwo
431 \fi
432 }
433 \gates@action{execute}#1{%
434 \gates@family_pass\gates@execute{#1}%
435 }
436 \def\gates@execute#1{%
437 \gates@ifexists{#1}{code}
438 {\gates@ifinlist
439 {\usecs{gates@execute_get_9_args}{#1}}
440 {\usecs{gates@execute_get_\usecs{gate #1:args}_args}{#1}}}
441 {\gates@error_nogate{#1}}%
442 }
443 \long\defcs{gates@execute_get_0_args}#1{%
444 \gates@execute_do{#1}{{}{}{}{}{}{}{}{}{}}%
445 }
446 \long\defcs{gates@execute_get_1_args}#1#2{%
447 \gates@execute_do{#1}{{#2}{}{}{}{}{}{}{}{}}%
448 }
449 \long\defcs{gates@execute_get_2_args}#1#2#3{%
450 \gates@execute_do{#1}{{#2}{#3}{}{}{}{}{}{}{}}%
451 }
452 \long\defcs{gates@execute_get_3_args}#1#2#3#4{%
453 \gates@execute_do{#1}{{#2}{#3}{#4}{}{}{}{}{}{}}%
454 }
455 \long\defcs{gates@execute_get_4_args}#1#2#3#4#5{%
456 \gates@execute_do{#1}{{#2}{#3}{#4}{#5}{}{}{}{}{}}%
457 }
458 \long\defcs{gates@execute_get_5_args}#1#2#3#4#5#6{%
459 \gates@execute_do{#1}{{#2}{#3}{#4}{#5}{#6}{}{}{}{}}%
460 }
461 \long\defcs{gates@execute_get_6_args}#1#2#3#4#5#6#7{%
462 \gates@execute_do{#1}{{#2}{#3}{#4}{#5}{#6}{#7}{}{}{}}%
463 }
464 \long\defcs{gates@execute_get_7_args}#1#2#3#4#5#6#7#8{%
465 \gates@execute_do{#1}{{#2}{#3}{#4}{#5}{#6}{#7}{#8}{}{}}%
466 }
467 \long\defcs{gates@execute_get_8_args}#1#2#3#4#5#6#7#8#9{%
468 \gates@execute_do{#1}{{#2}{#3}{#4}{#5}{#6}{#7}{#8}{#9}{}}%
469 }
470 \long\defcs{gates@execute_get_9_args}#1#2#3#4#5#6#7#8#9{%
471 \gates@execute_get_nine_arg{#1}{{#2}{#3}{#4}{#5}{#6}{#7}{#8}{#9}}%
472 }
473 \long\def\gates@execute_get_nine_arg#1#2#3{%
474 \gates@execute_do{#1}{#2{#3}}%
475 }
476
477 \long\def\gates@execute_do#1#2{%
478 \ifcase\usecs{gate #1:status}
479 \or % open
480 \gates@processtrue
481 \def\gates@trace_status{}%
482 \or % ajar
483 \gates@processtrue
484 \gdefcs{gate #1:status}{\gates@state_close}%
485 \def\gates@trace_status{ (ajar -> close)}%
486 \or % skip
487 \gates@processfalse
488 \gdefcs{gate #1:status}{\gates@state_open}%
489 \def\gates@trace_status{ (skip -> open)}%
490 \or % close
491 \gates@processfalse
492 \def\gates@trace_status{ (close)}%
493 \fi
494 %
495 \iffemptycommand\gates@trace_tab{\gates@trace_write{#1}{}}%
496 %
497 \edef\gates@process_tempconditional{\primunexpanded\expandafter\expandafter\expandafter{\csname gate #1:conditional\endcsname#2}}%
498 \ifexpression{\ifgates@process & \expandafter\ifexpression\expandafter{\gates@process_tempconditional}}
499 {\gates@trace_write{#1}{Executing \ifcs{gate #1:list}{l-}{m-}gate #1\gates@trace_status}%
500 \gates@stack_push{#1}%
501 \gates@execute_launchloop{#1}{#2}}
502 {\gates@trace_write{#1}{Ignoring \ifcs{gate #1:list}{l-}{m-}gate #1\gates@trace_status
503 \expandafter\reverse\expandafter\iffexpression{\gates@process_tempconditional}{ (False conditional)}}%
504 \gates@ifinlist{\gates@execute_backtolist{#2}}{}}%
505 }
506
507 \long\def\gates@execute_launchloop#1#2{%
508 \iffcs{gate #1:list}\gates@trace_addtab
509 \ifcs{gate #1:loop}
510 {\gates@execute_loop{#1}{#2}}
511 {\usecs{gate #1:code}#2\gates@execute_stoploop{#1}{#2}}%
512 }
513 \long\def\gates@execute_loop#1#2{%
514 \expandafter\expandafter\expandafter\ifexpression
515 \expandafter\expandafter\expandafter{\csname gate #1:loop\endcsname#2}
516 {\usecs{gate #1:code}#2\gates@execute_stoploop{#1}{#2}}%
517 {\gates@execute_end{#2}}%
518 }
519 \long\def\gates@execute_loopuntil#1#2{%
520 \expandafter\expandafter\expandafter\ifexpression
521 \expandafter\expandafter\expandafter{\csname gate #1:loopuntil\endcsname#2}
522 {\gates@execute_end{#2}}%
523 {\usecs{gate #1:code}#2\gates@execute_stoploop{#1}{#2}}%
524 }
525 \long\def\gates@execute_stoploop#1#2{%
526 \ifcs{gate #1:loop}
527 {\gates@execute_loop{#1}{#2}}
528 {\ifcs{gate #1:loopuntil}
529 {\gates@execute_loopuntil{#1}{#2}}
530 {\gates@execute_end{#2}}}%
531 }
532 \long\def\gates@execute_end#1{%
533 \iffcs{gate \gates@current_gate:list}\gates@trace_untab
534 \gates@stack_pop
535 \iffcs{gate \gates@current_gate:list}
536 {\gates@execute_backtolist{#1}}%
537 }
538 \long\def\gates@execute_backtolist#1#2#3{%
539 \gates@process_stoploop{#2}{#3}{#1}{#2}{#3}%
540 }
541
542 \gates@action{return}{\usecs{gates@action@return\usecs{gate \gates@current_gate:args}}}
543 \gates@action{return!}#1{%
544 \gates@process_returncount#1{gates@end}{gates@end}{gates@end}{gates@end}{gates@end}{gates@end}{gates@end}{gates@end}{gates@end}\gates@reallytheend#1%
545 }%
546 \long\def\gates@process_returncount#1#2#3#4#5#6#7#8#9{%
547 \gates@process_returngobble{%
548 \ifelseif{%
549 {\ifstring{#1}{gates@end}} {\usecs{gates@action@return0}}
550 {\ifstring{#2}{gates@end}} {\usecs{gates@action@return1}}
551 {\ifstring{#3}{gates@end}} {\usecs{gates@action@return2}}
552 {\ifstring{#4}{gates@end}} {\usecs{gates@action@return3}}
553 {\ifstring{#5}{gates@end}} {\usecs{gates@action@return4}}
554 {\ifstring{#6}{gates@end}} {\usecs{gates@action@return5}}
555 {\ifstring{#7}{gates@end}} {\usecs{gates@action@return6}}
556 {\ifstring{#8}{gates@end}} {\usecs{gates@action@return7}}
557 {\ifstring{#9}{gates@end}} {\usecs{gates@action@return8}}
558 \ifrue {\usecs{gates@action@return9}}}
559 }%
560 }
561 \gates@action{return0}{%
562 \gates@return_pass{\unbrace{}}%
563 }
564 \gates@action{return1}#1{%
565 \gates@return_pass{\gobbleoneand{{#1}}}%
566 }
567 \gates@action{return2}#1#2{%
568 \gates@return_pass{\gobbletwoand{{#1}{#2}}}%
569 }
570 \gates@action{return3}#1#2#3{%
571 \gates@return_pass{\gobblethreeand{{#1}{#2}{#3}}}%
572 }
573 \gates@action{return4}#1#2#3#4{%
574 \gates@return_pass{\gobblefourand{{#1}{#2}{#3}{#4}}}%
575 }
576 \gates@action{return5}#1#2#3#4#5{%
577 \gates@return_pass{\gobblefiveand{{#1}{#2}{#3}{#4}{#5}}}%
578 }
579 \gates@action{return6}#1#2#3#4#5#6{%
580 \gates@return_pass{\gobblesixand{{#1}{#2}{#3}{#4}{#5}{#6}}}%
581 }
582 \gates@action{return7}#1#2#3#4#5#6#7{%
583 \gates@return_pass{\gobblesevenand{{#1}{#2}{#3}{#4}{#5}{#6}{#7}}}%
584 }
585 \gates@action{return8}#1#2#3#4#5#6#7#8{%
586 \gates@return_pass{\gobbleeightand{{#1}{#2}{#3}{#4}{#5}{#6}{#7}{#8}}}%
587 }
588 \gates@action{return9}#1#2#3#4#5#6#7#8#9{%
589 \gates@return_passnine{{#1}{#2}{#3}{#4}{#5}{#6}{#7}{#8}{#9}}%
590 }
591 \long\def\gates@return_pass#1#2\gates@execute_stoploop#3#4{%
592 \passexpanded{\gates@execute_stoploop{#3}}{#1#4}%
593 }
594 \long\def\gates@return_passnine#1#2\gates@execute_stoploop#3#4{%
595 \gates@execute_stoploop{#3}{#1}%
596 }
597
598
599 %%%
600 %%% Processing l-gates.
601 %%%
602 \newife\ifgates@process
603 \newfornoempty\gates@process{2}#3,{%
604 \ifcase\usecs{gate #1_subgate_#3:status}
605 \or % open
606 \gates@processtrue
607 \def\gates@trace_status{}%
608 \or % ajar
609 \gates@processtrue
610 \gdefcs{gate #1_subgate_#3:status}{\gates@state_close}%
611 \def\gates@trace_status{ (ajar -> close)}%
612 \or % skip
613 \gates@processfalse
614 \gdefcs{gate #1_subgate_#3:status}{\gates@state_open}%
615 \def\gates@trace_status{ (skip -> open)}%
616 \or % close
617 \gates@processfalse
618 \def\gates@trace_status{ (close)}%
619 \fi
620 \edef\gates@process_tempconditional{\primunexpanded\expandafter\expandafter\expandafter{\csname gate #1_subgate_#3:conditional\endcsname#2}}%
621 \ifexpression{\ifgates@process & \expandafter\ifexpression\expandafter{\gates@process_tempconditional}}
622 {\gates@trace_writetab{#1}{Calling subgate #3\gates@trace_status}%
623 \gates@process_launchloop{#1}{#3}{#2}{#1}{#3}}%
624 {\gates@trace_writetab{#1}{Ignoring subgate #3\gates@trace_status
625 \expandafter\ifexpression\expandafter{\gates@process_tempconditional}{}{ (False conditional)}}}%
626 }[\usecs{gates@action@return\usecs{gate #1:args}}#2]
627
628 \def\gates@process_launchloop#1#2#3{%
629 \ifcs{gate #1_subgate_#2:loop}
630 {\gates@process_loop{#1}{#2}{#3}}
631 {\gates@action@execute{#2}#3}%
632 }
633 \def\gates@process_loop#1#2#3{%
634 \expandafter\expandafter\expandafter\ifexpression
635 \expandafter\expandafter\expandafter{\csname gate #1_subgate_#2:loop\endcsname#3}
636 {\gates@action@execute{#2}#3}
637 {\gates@process_nextgate{#3}}%
638 }
639 \def\gates@process_loopuntil#1#2#3{%
640 \expandafter\expandafter\expandafter\ifexpression
641 \expandafter\expandafter\expandafter{\csname gate #1_subgate_#2:loopuntil\endcsname#3}
642 {\gates@process_nextgate{#3}}%
643 {\gates@action@execute{#2}#3}
644 }
645 \def\gates@process_stoploop#1#2#3{%
646 \ifcs{gate #1_subgate_#2:loop}
647 {\gates@process_loop{#1}{#2}{#3}}
648 {\ifcs{gate #1_subgate_#2:loopuntil}
649 {\gates@process_loopuntil{#1}{#2}{#3}}
650 {\gates@process_nextgate{#3}}}%
651 }
652 \long\def\gates@process_nextgate#1#2{%
653 \passarguments{#2}{#1}%
654 }
655
656
657 \gates@action{subgates}#1{%
658 \gates@family_pass{\def\gates@subgates_gate}{#1}%
659 \afterassignment\gates@subgates_create
660 \def\gates@subgates_macro##1%
661 }
662
663 \def\gates@subgates_create{%
664 \ifcs{gate \gates@subgates_gate:list}
665 {\passexpandedcs{\gates@subgates_process{\gates@subgates_macro}}{gate \gates@subgates_gate:list}}
666 {\gates@error_nogatelist{\gates@subgates_gate}}%
667 }
668
669 \newfor\gates@subgates_process{1}#2,{%
670 #1{#2}%
671 }
672
673 %%%
674 %%% Managing gate lists.
675 %%%
676 \gates@action{add}#1{%
677 \ifnextnospace[
678 {\gates@add{#1}}
679 {\gates@add{#1}[]}%
680 }
681
682 \newstring{before } \newstring{after }
683 \def\gates@add#1[#2]#3{%
684 \gates@family_pass{\gates@add_prepare{#1}{#2}}{#3}%
685 }
686 \def\gates@add_prepare#1#2#3{%
687 \ifcs{gate #3:list}
688 {\let\gates@add_prefix\gates@somethingtotallyundefined
689 \let\gates@add_suffix\gates@somethingtotallyundefined
690 %
691 \ifelseif{%
692 {\ifexpression{ \ifemptystring{#2} | \ifstring{#2}{last} | \ifstring{#2}{after last} }}
693 {\passexpandedcs\gates@add_setends{gate #3:list}{}}
694 {\ifexpression{ \ifstring{#2}{first} | \ifstring{#2}{before first} }}
695 {\passexpandedcs{\gates@add_setends{}}{gate #3:list}}
696 {\ifprefix{before }{#2}}
697 {\removeprefixand{before }{#2}{\passexpandedcs\gates@add_split{gate #3:list}{0}{#3}}}
698 {\ifprefix{after }{#2}}
699 {\removeprefixand{after }{#2}{\passexpandedcs\gates@add_split{gate #3:list}{1}{#3}}}
700 \iftrue
701 {\gates@error{Unknown position: `#2'}}}%
702 %
703 \ifdefined\gates@add_prefix
704 \gates@add_do{#3}{#1,}%
705 \xdefcs{gate #3:list}{\gates@add_prefix\gates@add_suffix}%
706 \fi}%
707 %
708 {\gates@error_nogatelist{#3}}%
709 }
710
711 \def\gates@add_setends#1#2{%
712 \def\gates@add_prefix{#1}%
713 \def\gates@add_suffix{#2}%
714 }
715
716 \def\gates@add_split#1#2#3#4{%
717 \ifelseif{%
718 {\ifstring{#4}{first}}% "before first" has been dealt with, so this is "after first".
719 {\splitstringat{,}{#1}\gates@add_setends
720 \addright\gates@add_prefix{,}}
721 {\ifstring{#4}{last}}% Same thing, the other way around: "before last". Harder.
722 {\let\gates@add_prefix\emptycs
723 \def\gates@add_suffix{#1}%
724 \dowhile{\expandafter\gates@add_iftwo\gates@add_suffix,,\gates@end}
725 {\passexpanded{\splitstringat{,}}{\gates@add_suffix}\gates@add_setends
726 \addright\gates@add_prefix{,}}}
727 \iftrue
728 {\gates@family_pass{\gates@add_splitat{#1}{#2}{#3}}{#4}}}%
729 }
730 \def\gates@add_splitat#1#2#3#4{%
731 \ifelseif{%
732 {\ifprefix{#4,}{#1}}
733 {\ifcase#2
734 \gates@add_setends{}{#1}%
735 \or
736 \def\gates@add_prefix{#4,}%
737 \removeprefixin{#4,}{#1}\gates@add_suffix
738 \fi}
739 {\ifcontains{,#4,}{#1}}
740 {\splitstringat{,#4,}{#1}\gates@add_setends
741 \ifcase#2
742 \addright\gates@add_prefix{,}%
743 \addleft\gates@add_suffix{#4,}%
744 \or
745 \addright\gates@add_prefix{,#4,}
746 \fi}
747 \iftrue
748 {\gates@error_nogatein{#4}{#3}}}%
749 }
750
751 \def\gates@add_iftwo#1,#2,#3\gates@end{%
752 \ifemptystring{#2}\secondoftwo\firstoftwo
753 }
754
755 \newfornoempty\gates@add_do{1}#2,{%
756 \gates@ifexists{#2}{code}
757 {\eaddright\gates@add_prefix{\gates@family_check{#2},}%
758 \gdefcs{gate #1_subgate_\gates@family_check{#2}:status}{\gates@state_open}%
759 \gdefcs{gate #1_subgate_\gates@family_check{#2}:conditional}##1##2##3##4##5##6##7##8##9{\iftrue}}
760 {\gates@error_nogate{\gates@temp}}%
761 }
762
763 \gates@action{remove}#1#2{%
764 \gates@family_pass{\gates@remove_launch{#1}}{#2}%
765 }
766 \def\gates@remove_launch#1#2{%
767 \ifcs{gate #2:list}
768 {\gates@remove{#2}{#1,}}
769 {\gates@error_nogate{#2}}%
770 }
771 \newfornoempty\gates@remove{1}#2,{%
772 \gates@family_pass{\gates@remove_do{#1}}{#2}%
773 }
774 \def\gates@remove_do#1#2{%
775 \passexpandedcs\gates@remove_check{gate #1:list}{#2}
776 {\global\letcs{gate #1_subgate_#2:status}\gates@thisisabsolutelyundefined
777 \global\letcs{gate #1_subgate_#2:conditional}\gates@thisisabsolutelyundefined
778 \global\letcs{gate #1_subgate_#2:loop}\gates@thisisabsolutelyundefined
779 \global\letcs{gate #1_subgate_#2:loopuntil}\gates@thisisabsolutelyundefined
780 \passexpandedcs{\gates@remove_loop{}{#1}{#2}}{gate #1:list}}
781 {\gates@error_nogatein{#2}{#1}}%
782 }
783 \def\gates@remove_check#1#2{%
784 \ifcontains{,#2,}{#1}% The first comma so it doesn't spot ...,xxx#2,...
785 {\firstoftwo}
786 {\ifprefix{#2,}{#1}\firstoftwo\secondoftwo}%
787 }
788 \newfor\gates@remove_loop{3}#4,{%
789 \reverse\iffstring{#3}{#4}{\passarguments{#1#4,}{#2}{#3}}%
790 }[\gdefcs{gate #2:list}{#1}]
791
792 %%%
793 %%% opening, ajaring (it exists!), skipping and closing.
794 %%%
795 \chardef\gates@state_open = 1
796 \chardef\gates@state_ajar = 2
797 \chardef\gates@state_skip = 3
798 \chardef\gates@state_close = 4
799
800 \gates@action_withfamily{status}#1#2#3{%
801 \ifcs{gate \gates@family_passdo\unbrace{#3}{#1}_subgate_\gates@family_passdo\unbrace{#2}{#1}:status}
802 {\passcs\the{gate \gates@family_passdo\unbrace{#3}{#1}_subgate_\gates@family_passdo\unbrace{#2}{#1}:status}}
803 {0}%
804 }
805
806 \gates@action_withfamily{status!}#1#2{%
807 \ifcs{gate \gates@family_passdo\unbrace{#2}{#1}:status}
808 {\passcs\the{gate \gates@family_passdo\unbrace{#2}{#1}:status}}
809 {0}%
810 }
811
812 \long\def\gates@status_change#1#2{%
813 \gates@family_pass{\gates@status_changedo{#1}}{#2}%
814 }
815 \def\gates@status_changedo#1#2#3#4{%
816 \ifcs{gate #2:list}
817 {\ifelseif{%
818 {\ifprefix{before }{#1}} {\passexpandedcs{\removeprefixand{before }{#1}{\gates@family_pass{\gates@switch_b{#3}{#2}}}{#4}}{gate #2:list}}%
819 {\ifprefix{after }{#1}} {\passexpandedcs{\removeprefixand{after }{#1}{\gates@family_pass{\gates@switch_a{#3}{#2}}}{#4}}{gate #2:list}}%
820 \iftrue {\gates@switch{#3}{#2}{#4}{#1,}}}}
821 {\gates@error_nogatelist{#2}}%
822 }
823
824 \gates@action{open}#1#2{%
825 \gates@status_change{#1}{#2}{status}{\gates@state_open}%
826 }
827 \gates@action{open!}#1{%
828 \gates@switch_x{status}{\gates@state_open}{#1,}%
829 }
830
831 \gates@action{ajar}#1#2{%
832 \gates@status_change{#1}{#2}{status}{\gates@state_ajar}%
833 }
834 \gates@action{ajar!}#1{%
835 \gates@switch_x{status}{\gates@state_ajar}{#1,}%
836 }
837
838 \gates@action{skip}#1#2{%
839 \gates@status_change{#1}{#2}{status}{\gates@state_skip}%
840 }
841 \gates@action{skip!}#1{%
842 \gates@switch_x{status}{\gates@state_skip}{#1,}%
843 }
844
845 \gates@action{close}#1#2{%
846 \gates@status_change{#1}{#2}{status}{\gates@state_close}%
847 }
848 \gates@action{close!}#1{%
849 \gates@switch_x{status}{\gates@state_close}{#1,}%
850 }
851
852 \gates@action{conditional}#1#2#3{%
853 \gates@status_change{#1}{#2}{conditional}{#3}%
854 }
855 \gates@action{conditional!}#1#2{%
856 \gates@switch_x{conditional}{#2}{#1,}%
857 }
858
859 \gates@action{loop}#1#2#3{%
860 \gates@status_change{#1}{#2}{loop}{#3}%
861 }
862 \gates@action{loop!}#1#2{%
863 \gates@switch_x{loop}{#2}{#1,}%
864 }
865
866 \gates@action{noloop}#1#2{%
867 \gates@status_change{#1}{#2}{noloop}{}%
868 }
869 \gates@action{noloop!}#1{%
870 \gates@switch_x{noloop}{}{#1,}%
871 }
872
873 \gates@action{loopuntil}#1#2#3{%
874 \gates@status_change{#1}{#2}{loopuntil}{#3}%
875 }
876 \gates@action{loopuntil!}#1#2{%
877 \gates@switch_x{loopuntil}{#2}{#1,}%
878 }
879
880 \gates@action{noloopuntil}#1#2{%
881 \gates@status_change{#1}{#2}{noloopuntil}%
882 }
883 \gates@action{noloopuntil!}#1{%
884 \gates@switch_x{noloopuntil}{}{#1,}%
885 }
886
887 \newfornoempty\gates@switch{3}#4,{%
888 \gates@family_pass{\gates@switch_do{#1}{#2}{#3}}{#4}%
889 }
890 \long\def\gates@switch_do#1#2#3#4{%
891 \ifcs{gate #2_subgate_#4:status}
892 {\gates@switch_evaluate{#2_subgate_#4}{#1}{#3}}
893 {\gates@error_nogatein{#4}{#2}}%
894 }
895 \newfornoempty\gates@switch_b{4}#5,{%
896 \ifstring{#3}{#5}
897 {\breakfor{}}%
898 {\gates@switch_evaluate{#2_subgate_#5}{#1}{#4}}%
899 }
900 \newfornoempty\gates@switch_a{4}#5,{%
901 \iffstring{#3}{#5}
902 {\retrieverest{\gates@switch{#1}{#2}{#4}}}%
903 }[\gates@error_nogatein{#3}{#2}]
904 \newfornoempty\gates@switch_x{2}#3,{%
905 \ifcs{gate \gates@family_check{#3}:status}
906 {\gates@switch_evaluate{\gates@family_check{#3}}{#1}{#2}}
907 {\gates@error_nogate{\gates@family_check{#3}}}%
908 }
909 \long\def\gates@switch_evaluate#1#2#3{%
910 \ifelseif{%
911 {\ifexpression{ \ifstring{#2}{conditional} | \ifstring{#2}{loop} | \ifstring{#2}{loopuntil} }}
912 {\gdefcs{gate #1:#2}##1##2##3##4##5##6##7##8##9{#3}}
913 {\ifstring{#2}{noloop}}
914 {\global\letcs{gate #1:loop}\gates@somethingtotallyundefined}
915 {\ifstring{#2}{noloopuntil}}
916 {\global\letcs{gate #1:loopuntil}\gates@somethingtotallyundefined}
917 \iftrue
918 {\gdefcs{gate #1:#2}{#3}}}
919 }
920
921 \restorecatcodes
922 \endinput
923 % There was a time when writing this code I thought
924 % I could keep it below 200 lines. Then 300. Then I stopped
925 % counting. I'm cursed by loops.