Debian Codegraph @

texk/web2c/luatexdir/tex/conditional.c

raw
1 /*
2
3 conditional.w
4
5 Copyright 2009-2010 Taco Hoekwater <taco@@luatex.org>
6
7 This file is part of LuaTeX.
8
9 LuaTeX is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2 of the License, or (at your
12 option) any later version.
13
14 LuaTeX is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 License for more details.
18
19 You should have received a copy of the GNU General Public License along
20 with LuaTeX; if not, see <http://www.gnu.org/licenses/>.
21
22 */
23
24 #include "ptexlib.h"
25
26 /*tex
27
28 @* We consider now the way \TeX\ handles various kinds of \.{\\if} commands.
29
30 Conditions can be inside conditions, and this nesting has a stack that is
31 independent of the |save_stack|.
32
33 Four global variables represent the top of the condition stack: |cond_ptr| points
34 to pushed-down entries, if any; |if_limit| specifies the largest code of a
35 |fi_or_else| command that is syntactically legal; |cur_if| is the name of the
36 current type of conditional; and |if_line| is the line number at which it began.
37
38 If no conditions are currently in progress, the condition stack has the special
39 state |cond_ptr=null|, |if_limit=normal|, |cur_if=0|, |if_line=0|. Otherwise
40 |cond_ptr| points to a two-word node; the |type|, |subtype|, and |link| fields of
41 the first word contain |if_limit|, |cur_if|, and |cond_ptr| at the next level,
42 and the second word contains the corresponding |if_line|.
43
44 In |cond_ptr| we keep track of the top of the condition stack while |if_limit|
45 holds the upper bound on |fi_or_else| codes. The type of conditional being worked
46 on is stored in cur_if and |if_line| keeps track of the line where that
47 conditional began. When we skip conditional text, |skip_line| keeps track of the
48 line number where skipping began, for use in error messages.
49
50 */
51
52 halfword cond_ptr;
53 int if_limit, cur_if, if_line, skip_line;
54
55 /*tex
56
57 Here is a procedure that ignores text until coming to an \.{\\or}, \.{\\else}, or
58 \.{\\fi} at level zero of $\.{\\if}\ldots\.{\\fi}$ nesting. After it has acted,
59 |cur_chr| will indicate the token that was found, but |cur_tok| will not be set
60 (because this makes the procedure run faster).
61
62 With |l| we keep track of the level of $\.{\\if}\ldots\.{\\fi}$ nesting and
63 |scanner_status| let us return to the entry status.
64
65 */
66
67 void pass_text(void)
68 {
69 int l = 0;
70 int save_scanner_status = scanner_status;
71 scanner_status = skipping;
72 skip_line = line;
73 while (1) {
74 get_next();
75 if (cur_cmd == fi_or_else_cmd) {
76 if (l == 0)
77 break;
78 if (cur_chr == fi_code)
79 decr(l);
80 } else if (cur_cmd == if_test_cmd) {
81 incr(l);
82 }
83 }
84 scanner_status = save_scanner_status;
85 if (tracing_ifs_par > 0)
86 show_cur_cmd_chr();
87 }
88
89 /*tex
90
91 When we begin to process a new \.{\\if}, we set |if_limit:=if_code|; then if\/
92 \.{\\or} or \.{\\else} or \.{\\fi} occurs before the current \.{\\if} condition
93 has been evaluated, \.{\\relax} will be inserted. For example, a sequence of
94 commands like `\.{\\ifvoid1\\else...\\fi}' would otherwise require something
95 after the `\.1'.
96
97 */
98
99 void push_condition_stack(void)
100 {
101 halfword p = new_node(if_node, 0);
102 vlink(p) = cond_ptr;
103 if_limit_type(p) = (quarterword) if_limit;
104 if_limit_subtype(p) = (quarterword) cur_if;
105 if_line_field(p) = if_line;
106 cond_ptr = p;
107 cur_if = cur_chr;
108 if_limit = if_code;
109 if_line = line;
110 }
111
112 void pop_condition_stack(void)
113 {
114 halfword p;
115 if (if_stack[in_open] == cond_ptr) {
116 /*tex Conditionals are possibly not properly nested with files. */
117 if_warning();
118 }
119 p = cond_ptr;
120 if_line = if_line_field(p);
121 cur_if = if_limit_subtype(p);
122 if_limit = if_limit_type(p);
123 cond_ptr = vlink(p);
124 flush_node(p);
125 }
126
127 /*tex
128
129 Here's a procedure that changes the |if_limit| code corresponding to a given
130 value of |cond_ptr|.
131
132 */
133
134 void change_if_limit(int l, halfword p)
135 {
136 if (p == cond_ptr) {
137 if_limit = l;
138 } else {
139 halfword q = cond_ptr;
140 while (1) {
141 if (q == null)
142 confusion("if");
143 if (vlink(q) == p) {
144 if_limit_type(q) = (quarterword) l;
145 return;
146 }
147 q = vlink(q);
148 }
149 }
150 }
151
152 /*tex
153
154 The conditional \.{\\ifcsname} is equivalent to \.{\\expandafter}
155 \.{\\expandafter} \.{\\ifdefined} \.{\\csname}, except that no new control
156 sequence will be entered into the hash table (once all tokens preceding the
157 mandatory \.{\\endcsname} have been expanded).
158
159 */
160
161 static halfword last_tested_cs ;
162
163 static boolean test_for_cs(void)
164 {
165 /*tex Is the condition true? */
166 boolean b = false;
167 /*tex To be tested against the second operand: */
168 int m, s;
169 /*tex For traversing token lists in \.{\\ifx} tests: */
170 halfword q;
171 halfword n = get_avail();
172 /*tex Head of the list of characters: */
173 halfword p = n;
174 is_in_csname += 1;
175 while (1) {
176 get_x_token();
177 if (cur_cs != 0)
178 break;
179 store_new_token(cur_tok);
180 }
181 if (cur_cmd != end_cs_name_cmd) {
182 last_tested_cs = null_cs;
183 if (suppress_ifcsname_error_par) {
184 do {
185 get_x_token();
186 } while (cur_cmd != end_cs_name_cmd);
187 flush_list(n);
188 is_in_csname -= 1;
189 return b;
190 } else {
191 complain_missing_csname();
192 }
193 }
194 /*tex Look up the characters of list |n| in the hash table, and set |cur_cs|. */
195 m = first;
196 p = token_link(n);
197 while (p != null) {
198 if (m >= max_buf_stack) {
199 max_buf_stack = m + 4;
200 if (max_buf_stack >= buf_size)
201 check_buffer_overflow(max_buf_stack);
202 }
203 s = token_chr(token_info(p));
204 if (s <= 0x7F) {
205 buffer[m++] = (packed_ASCII_code) s;
206 } else if (s <= 0x7FF) {
207 buffer[m++] = (packed_ASCII_code) (0xC0 + s / 0x40);
208 buffer[m++] = (packed_ASCII_code) (0x80 + s % 0x40);
209 } else if (s <= 0xFFFF) {
210 buffer[m++] = (packed_ASCII_code) (0xE0 + s / 0x1000);
211 buffer[m++] = (packed_ASCII_code) (0x80 + (s % 0x1000) / 0x40);
212 buffer[m++] = (packed_ASCII_code) (0x80 + (s % 0x1000) % 0x40);
213 } else {
214 buffer[m++] = (packed_ASCII_code) (0xF0 + s / 0x40000);
215 buffer[m++] = (packed_ASCII_code) (0x80 + (s % 0x40000) / 0x1000);
216 buffer[m++] = (packed_ASCII_code) (0x80 + ((s % 0x40000) % 0x1000) / 0x40);
217 buffer[m++] = (packed_ASCII_code) (0x80 + ((s % 0x40000) % 0x1000) % 0x40);
218 }
219 p = token_link(p);
220 }
221 if (m > first) {
222 /*tex |no_new_control_sequence| is |true| */
223 cur_cs = id_lookup(first, m - first);
224 } else if (m == first) {
225 /*tex the list is empty */
226 cur_cs = null_cs;
227 }
228 b = (eq_type(cur_cs) != undefined_cs_cmd);
229 flush_list(n);
230 last_cs_name = cur_cs;
231 is_in_csname -= 1;
232 return b;
233 }
234
235 /*tex
236
237 An active character will be treated as category 13 following \.{\\if\\noexpand}
238 or following \.{\\ifcat\\noexpand}.
239
240 */
241
242 #define get_x_token_or_active_char() do { \
243 get_x_token(); \
244 if (cur_cmd==relax_cmd && cur_chr==no_expand_flag) { \
245 if (is_active_cs(cs_text(cur_cs))) { \
246 cur_cmd=active_char_cmd; \
247 cur_chr=active_cs_value(cs_text(cur_tok-cs_token_flag)); \
248 } \
249 } \
250 } while (0)
251
252 /*tex
253
254 A condition is started when the |expand| procedure encounters an |if_test|
255 command; in that case |expand| reduces to |conditional|, which is a recursive
256 procedure. @^recursion@>
257
258 */
259
260 void conditional(void)
261 {
262 /*tex Is the condition true? */
263 boolean b = false;
264 /*tex The relation to be evaluated: */
265 int r;
266 /*tex To be tested against the second operand: */
267 int m, n;
268 /*tex For traversing token lists in \.{\\ifx} tests: */
269 halfword p, q;
270 /*tex The |scanner_status| upon entry: */
271 int save_scanner_status;
272 /*tex The |cond_ptr| corresponding to this conditional: */
273 halfword save_cond_ptr;
274 /*tex The type of this conditional: */
275 int this_if;
276 /*tex Was this \.{\\if} preceded by \.{\\unless}? */
277 boolean is_unless;
278 if ((tracing_ifs_par > 0) && (tracing_commands_par <= 1)) {
279 show_cur_cmd_chr();
280 }
281 push_condition_stack();
282 save_cond_ptr = cond_ptr;
283 is_unless = (cur_chr >= unless_code);
284 this_if = cur_chr % unless_code;
285 /*tex Either process \.{\\ifcase} or set |b| to the value of a boolean condition. */
286 switch (this_if) {
287 case if_char_code:
288 case if_cat_code:
289 /*tex Test if two characters match. */
290 get_x_token_or_active_char();
291 if ((cur_cmd > active_char_cmd) || (cur_chr > biggest_char)) {
292 /*tex It's not a character. */
293 m = relax_cmd;
294 n = too_big_char;
295 } else {
296 m = cur_cmd;
297 n = cur_chr;
298 }
299 get_x_token_or_active_char();
300 if ((cur_cmd > active_char_cmd) || (cur_chr > biggest_char)) {
301 cur_cmd = relax_cmd;
302 cur_chr = too_big_char;
303 }
304 if (this_if == if_char_code)
305 b = (n == cur_chr);
306 else
307 b = (m == cur_cmd);
308 break;
309 case if_int_code:
310 case if_dim_code:
311 case if_abs_dim_code:
312 case if_abs_num_code:
313 /*tex
314 Test the relation between integers or dimensions. Here we use the fact
315 that |<|, |=|, and |>| are consecutive ASCII codes.
316 */
317 if (this_if == if_int_code || this_if == if_abs_num_code)
318 scan_int();
319 else
320 scan_normal_dimen();
321 n = cur_val;
322 if ((n < 0) && (this_if == if_abs_dim_code || this_if == if_abs_num_code))
323 negate(n);
324 /*tex Get the next non-blank non-call... */
325 do {
326 get_x_token();
327 } while (cur_cmd == spacer_cmd);
328 r = cur_tok - other_token;
329 if ((r < '<') || (r > '>')) {
330 print_err("Missing = inserted for ");
331 print_cmd_chr(if_test_cmd, this_if);
332 help1("I was expecting to see `<', `=', or `>'. Didn't.");
333 back_error();
334 r = '=';
335 }
336 if (this_if == if_int_code || this_if == if_abs_num_code)
337 scan_int();
338 else
339 scan_normal_dimen();
340 if ((cur_val < 0) && (this_if == if_abs_dim_code || this_if == if_abs_num_code))
341 negate(cur_val);
342 switch (r) {
343 case '<':
344 b = (n < cur_val);
345 break;
346 case '=':
347 b = (n == cur_val);
348 break;
349 case '>':
350 b = (n > cur_val);
351 break;
352 default:
353 /*tex This can't happen. */
354 b = false;
355 break;
356 }
357 break;
358 case if_odd_code:
359 /*tex Test if an integer is odd. */
360 scan_int();
361 b = odd(cur_val);
362 break;
363 case if_vmode_code:
364 b = (abs(cur_list.mode_field) == vmode);
365 break;
366 case if_hmode_code:
367 b = (abs(cur_list.mode_field) == hmode);
368 break;
369 case if_mmode_code:
370 b = (abs(cur_list.mode_field) == mmode);
371 break;
372 case if_inner_code:
373 b = (cur_list.mode_field < 0);
374 break;
375 case if_void_code:
376 scan_register_num();
377 p = box(cur_val);
378 b = (p == null);
379 break;
380 case if_hbox_code:
381 scan_register_num();
382 p = box(cur_val);
383 b = (p != null) && (type(p) == hlist_node);
384 break;
385 case if_vbox_code:
386 scan_register_num();
387 p = box(cur_val);
388 b = (p != null) && (type(p) == vlist_node);
389 break;
390 case if_x_code:
391 /*tex
392 Test if two tokens match. Note that `\.{\\ifx}' will declare two
393 macros different if one is \\{long} or \\{outer} and the other
394 isn't, even though the texts of the macros are the same.
395
396 We need to reset |scanner_status|, since \.{\\outer} control
397 sequences are allowed, but we might be scanning a macro
398 definition or preamble.
399 */
400 save_scanner_status = scanner_status;
401 scanner_status = normal;
402 get_next();
403 n = cur_cs;
404 p = cur_cmd;
405 q = cur_chr;
406 get_next();
407 if (cur_cmd != p) {
408 b = false;
409 } else if (cur_cmd < call_cmd) {
410 b = (cur_chr == q);
411 } else {
412 /*tex
413 Test if two macro texts match. Note also that `\.{\\ifx}'
414 decides that macros \.{\\a} and \.{\\b} are different in
415 examples like this:
416
417 $$\vbox{\halign{\.{#}\hfil&\qquad\.{#}\hfil\cr
418 {}\\def\\a\{\\c\}&
419 {}\\def\\c\{\}\cr
420 {}\\def\\b\{\\d\}&
421 {}\\def\\d\{\}\cr}}$$
422 */
423 p = token_link(cur_chr);
424 /*tex Omit reference counts. */
425 q = token_link(equiv(n));
426 if (p == q) {
427 b = true;
428 } else {
429 while ((p != null) && (q != null)) {
430 if (token_info(p) != token_info(q)) {
431 p = null;
432 break;
433 } else {
434 p = token_link(p);
435 q = token_link(q);
436 }
437 }
438 b = ((p == null) && (q == null));
439 }
440 }
441 scanner_status = save_scanner_status;
442 break;
443 case if_eof_code:
444 scan_four_bit_int();
445 b = (read_open[cur_val] == closed);
446 break;
447 case if_true_code:
448 b = true;
449 break;
450 case if_false_code:
451 b = false;
452 break;
453 case if_case_code:
454 /*tex Select the appropriate case and |return| or |goto common_ending|. */
455 scan_int();
456 /*tex |n| is the number of cases to pass. */
457 n = cur_val;
458 if (tracing_commands_par > 1) {
459 begin_diagnostic();
460 tprint("{case ");
461 print_int(n);
462 print_char('}');
463 end_diagnostic(false);
464 }
465 while (n != 0) {
466 pass_text();
467 if (cond_ptr == save_cond_ptr) {
468 if (cur_chr == or_code)
469 decr(n);
470 else
471 goto COMMON_ENDING;
472 } else if (cur_chr == fi_code) {
473 pop_condition_stack();
474 }
475 }
476 change_if_limit(or_code, save_cond_ptr);
477 /*tex Wait for \.{\\or}, \.{\\else}, or \.{\\fi}. */
478 return;
479 break;
480 case if_primitive_code:
481 save_scanner_status = scanner_status;
482 scanner_status = normal;
483 get_next();
484 scanner_status = save_scanner_status;
485 m = prim_lookup(cs_text(cur_cs));
486 b = ((cur_cmd != undefined_cs_cmd) &&
487 (m != undefined_primitive) &&
488 (cur_cmd == get_prim_eq_type(m)) &&
489 (cur_chr == get_prim_equiv(m)));
490 break;
491 case if_def_code:
492 /*tex
493 The conditional \.{\\ifdefined} tests if a control sequence is
494 defined. We need to reset |scanner_status|, since \.{\\outer}
495 control sequences are allowed, but we might be scanning a macro
496 definition or preamble.
497 */
498 save_scanner_status = scanner_status;
499 scanner_status = normal;
500 get_next();
501 b = (cur_cmd != undefined_cs_cmd);
502 scanner_status = save_scanner_status;
503 break;
504 case if_cs_code:
505 b = test_for_cs();
506 break;
507 case if_in_csname_code:
508 b = is_in_csname;
509 break;
510 case if_font_char_code:
511 /*tex
512 The conditional \.{\\iffontchar} tests the existence of a
513 character in a font.
514 */
515 scan_font_ident();
516 n = cur_val;
517 scan_char_num();
518 b = char_exists(n, cur_val);
519 break;
520 default:
521 /*tex there are no other cases, but we need to please |-Wall|. */
522 b = false;
523 }
524 if (is_unless)
525 b = !b;
526 if (tracing_commands_par > 1) {
527 /*tex Display the value of |b|. */
528 begin_diagnostic();
529 if (b)
530 tprint("{true}");
531 else
532 tprint("{false}");
533 end_diagnostic(false);
534 }
535 if (b) {
536 change_if_limit(else_code, save_cond_ptr);
537 /*tex Wait for \.{\\else} or \.{\\fi}. */
538 return;
539 }
540 /*tex
541 Skip to \.{\\else} or \.{\\fi}, then |goto common_ending|. In a
542 construction like `\.{\\if\\iftrue abc\\else d\\fi}', the first
543 \.{\\else} that we come to after learning that the \.{\\if} is false is
544 not the \.{\\else} we're looking for. Hence the following curious logic
545 is needed.
546 */
547 while (1) {
548 pass_text();
549 if (cond_ptr == save_cond_ptr) {
550 if (cur_chr != or_code)
551 goto COMMON_ENDING;
552 print_err("Extra \\or");
553 help1(
554 "I'm ignoring this; it doesn't match any \\if."
555 );
556 error();
557 } else if (cur_chr == fi_code) {
558 pop_condition_stack();
559 }
560 }
561 COMMON_ENDING:
562 if (cur_chr == fi_code) {
563 pop_condition_stack();
564 } else {
565 /*tex Wait for \.{\\fi}. */
566 if_limit = fi_code;
567 }
568 }