Debian Codegraph @

texk/web2c/weav-twill.ch

raw
1 Changes for WEAVE.WEB to mogrify WEAVE into TWILL.
2
3 See the 'git log' in https://github.com/ascherer/twill and
4 https://github.com/ascherer/web for details.
5
6 Public domain. Originally written by Andreas Scherer, 2022.
7
8 Limbo.
9
10 @x 2,29c2,5
11 % Version 0 was released in December, 1981.
12 % Version 1 was released in September, 1982, with version 0 of TeX.
13 % Slight changes were made in October, 1982, for version 0.6 of TeX.
14 % Version 1.1 changed "_" to "\_" if not within an identifier (November, 1982).
15 % Version 1.2 added @@= and @@\ and marked changed modules (December, 1982).
16 % Version 1.3 marked and indexed changed modules better (January, 1983).
17 % Version 1.4 added "history" (February, 1983).
18 % Version 1.5 conformed to TeX version 0.96 (March, 1983).
19 % Version 1.6 conformed to TeX version 0.98 (May, 1983).
20 % Version 1.7 introduced the new change file format (June, 1983).
21 % Version 2 was released in July, 1983, with version 0.999 of TeX.
22 % Version 2.1 corrected a bug in changed_module reckoning (August, 1983).
23 % Version 2.2 corrected it better (August, 1983).
24 % Version 2.3 starts the output with \input webmac (August, 1983).
25 % Version 2.4 fixed a bug in compress(#) (September, 1983).
26 % Version 2.5 cleared xrefswitch after module names (November, 1983).
27 % Version 2.6 fixed a bug in declaration of trans array (January, 1984).
28 % Version 2.7 fixed a bug in real constants (August, 1984).
29 % Version 2.8 fixed a bug in change_buffer movement (August, 1985).
30 % Version 2.9 increased max_refs and max_toks to 30000 each (January, 1987).
31 % Version 3, for Sewell's book, fixed long-line bug in input_ln (March, 1989).
32 % Version 3.1 fixed a bug for programs with only one module (April, 1989).
33 % Version 4 was major change to allow 8-bit input (September, 1989).
34 % Version 4.1, for Breitenlohner, avoids English-only output (March, 1990).
35 % Version 4.2 conforms to ANSI standard for-loop rules (September, 1990).
36 % Version 4.3 catches extra } in input (Breitenlohner, September, 1991).
37 % Version 4.4 corrects changed_module logic, %-overflow (January, 1992).
38 % Version 4.5 corrects archaic @@z logic and empty change file (January, 2021).
39 @y
40 % But it is not polished either, so you probably shouldn't try....
41 % Version 2.8 (based on WEAVE 2.8) was hacked together on 28 Sep 1985.
42 % Version 2.9 fixed output for dtype_string (14 Jun 1987).
43 % Version 4.5 (based on WEAVE 4.5) was hacked together on 04 Jan 2022.
44 @z
45
46 @x 43c19
47 \let\maybe=\iffalse
48 \def\title{WEAVE changes for C}
49 @y
50 \let\maybe=\iftrue
51 \def\title{TWILL for \TeX~Live}
52 @z
53
54 @x 47c23
55 \def\rheader{\mainfont Appendix D\hfil \contentspagenumber}
56 \centerline{\titlefont The {\ttitlefont WEAVE} processor}
57 @y
58 \def\rheader{\mainfont Appendix D${}'$\hfil \contentspagenumber}
59 \centerline{\titlefont The {\ttitlefont TWILL} processor}
60 @z
61
62 Section 1.
63
64 @x 54a31,35
65 @* Introduction.
66 @y
67 @* Introduction.
68 [Apology: This modification of \.{WEAVE} was put together hastily at the
69 end of September, 1985, in order to prepare the listing of \TeX,
70 on the WAITS system. No attempt has been made to polish anything
71 or to make this code usable by anyone else but its author.]
72
73 @z
74
75 @x 77c58
76 @d my_name=='weave'
77 @d banner=='This is WEAVE, Version 4.5'
78 @y
79 @d my_name=='twill'
80 @d banner=='This is TWILL, Version 4.5'
81 @z
82
83 Section 8.
84
85 @x 203a185
86 @!stack_size=2000; {number of simultaneous output levels}
87 @y
88 @!stack_size=2000; {number of simultaneous output levels}
89 @!max_new_refs=200; {number of different references to other modules}
90 @z
91
92 Section 37.
93
94 @x 742a725
95 @!xref: array [0..max_names] of sixteen_bits; {heads of cross-reference lists}
96 @y
97 @!xref: array [0..max_names] of sixteen_bits; {heads of cross-reference lists}
98 @!def_val: array [0..max_names] of integer; {values of numeric macros}
99 @z
100
101 Section 46.
102
103 @x 889a873,876
104 @d xlink(#)==xmem[#].xlink_field
105 @y
106 @d xlink(#)==xmem[#].xlink_field
107 @d dtype(#)==xmem[#].dtype_field
108 @d dname(#)==xmem[#].dname_field
109 @d dback(#)==xmem[#].dback_field
110 @d dlink(#)==xmem[#].dlink_field
111 @z
112
113 Section 48.
114
115 @x 895c882,899
116 @ @<Globals...@>=
117 @y
118 @ Definitions are classified into 15 types.
119 @d dtype_none=0
120 @d dtype_macro=1
121 @d dtype_const=2
122 @d dtype_string=3
123 @d dtype_colon_bold=4
124 @d dtype_equal_bold=5
125 @d dtype_colon_packed=6
126 @d dtype_equal_packed=7
127 @d dtype_colon_ital=8
128 @d dtype_equal_ital=9
129 @d dtype_comma=10
130 @d dtype_colon_const_dots=11
131 @d dtype_equal_const_dots=12
132 @d dtype_colon_ital_dots=13
133 @d dtype_equal_ital_dots=14
134
135 @<Globals...@>=
136 @z
137
138 @x 898a903,906
139 @!xlink_field: sixteen_bits; {pointer to the previous cross reference}
140 @y
141 @!xlink_field: sixteen_bits; {pointer to the previous cross reference}
142 @!dtype_field: sixteen_bits; {type of definition}
143 @!dname_field: sixteen_bits; {identifier or constant}
144 @!dback_field: sixteen_bits; {if nonzero, this is a reference to name}
145 @!dlink_field: sixteen_bits; {link to definitions only}
146 @z
147
148 @x 901a910,912
149 @!xref_switch,@!mod_xref_switch:0..def_flag; {either zero or |def_flag|}
150 @y
151 @!xref_switch,@!mod_xref_switch:0..def_flag; {either zero or |def_flag|}
152 @!def_type,@!def_name,@!def_subtype,@!def_subname,
153 @!const_name,@!packed_name:integer;
154 @!danger_zone:boolean;
155 @z
156
157 Section 49.
158
159 @x 903a915
160 @ @<Set init...@>=xref_ptr:=0; xref_switch:=0; mod_xref_switch:=0; num(0):=0;
161 @y
162 @ @<Set init...@>=xref_ptr:=0; xref_switch:=0; mod_xref_switch:=0; num(0):=0;
163 danger_zone:=false;
164 @z
165
166 Section 50.
167
168 @x 908a...
169 to one-letter identifiers or \PASCAL's reserved words.
170 @y
171 to one-letter identifiers or \PASCAL's reserved words.
172
173 The |new_blank_xref| is similar but it ignores the |xref_switch|.
174 @z
175
176 @x 911a924
177 else begin incr(xref_ptr); num(xref_ptr):=#;
178 @y
179 else begin incr(xref_ptr); num(xref_ptr):=#;
180 dback(xref_ptr):=0;
181 @z
182
183 @x 925,926c938,942
184 begin num(q):=m; return;
185 end;
186 @y
187 if (def_type>=dtype_comma) or danger_zone then
188 q:=xlink(q) {delete entry}
189 else begin num(q):=m; dtype(q):=def_type;
190 dname(q):=def_name; dback(q):=p; return;
191 end;
192 @z
193
194 @x 928,929c945,952
195 append_xref(m); xlink(xref_ptr):=q; xref[p]:=xref_ptr;
196 exit: end;
197 @y
198 append_xref(m); xlink(xref_ptr):=q; xref[p]:=xref_ptr;
199 if m>def_flag then
200 begin dtype(xref_ptr):=def_type; dname(xref_ptr):=def_name;
201 dback(xref_ptr):=p;
202 if def_type>dtype_comma then
203 begin append_xref(0); dtype(xref_ptr):=def_subtype;
204 dname(xref_ptr):=def_subname;
205 end;
206 end;
207 exit: end;
208 @#
209 procedure new_blank_xref(@!p:integer);
210 var xs:integer;
211 begin xs:=xref_switch; xref_switch:=0; new_xref(p); xref_switch:=xs;
212 end;
213 @z
214
215 Section 62.
216
217 @x 1064a1096,1097
218 @ When we begin the following segment of the program, |p=name_ptr|.
219 @y
220 @ When we begin the following segment of the program, |p=name_ptr|.
221
222 @d undef_val==10000000
223
224 @z
225
226 @x 1074c1107
227 ilk[p]:=t; xref[p]:=0;
228 @y
229 ilk[p]:=t; xref[p]:=0; def_val[p]:=undef_val;
230 @z
231
232 Section 64.
233
234 @x 1112c1145
235 id5("c")("o")("n")("s")("t")(const_like);@/
236 @y
237 id5("c")("o")("n")("s")("t")(const_like); const_name:=cur_name;@/
238 @z
239
240 @x 1130c1163
241 id6("p")("a")("c")("k")("e")("d")(goto_like);@/
242 @y
243 id6("p")("a")("c")("k")("e")("d")(goto_like); packed_name:=cur_name;@/
244 @z
245
246 Section 71.
247
248 @x 1278,1279d1310
249 @!change_pending: boolean; {if |true|, the current change is not yet
250 recorded in |changed_module[module_count]|}
251 @y
252 @z
253
254 Section 75.
255
256 @x 1323
257 @<Move |buffer| and |limit| to |change_buffer| and |change_limit|@>;
258 @y
259 @<Move \(|buffer| and |limit| to |change_buffer| and |change_limit|@>;
260 @z
261
262 Section 78.
263
264 @x 1357
265 @ @<Move |buffer| and |limit| to |change_buffer| and |change_limit|@>=
266 @y
267 @ @<Move \(|buffer| and |limit| to |change_buffer| and |change_limit|@>=
268 @z
269
270 Section 79.
271
272 @x 1371,1383d1401
273 When a match is found, the current module is marked as changed unless
274 the first line after the \.{@@x} and after the \.{@@y} both start with
275 either |'@@*'| or |'@@ '| (possibly preceded by whitespace).
276
277 @d if_module_start_then_make_change_pending(#)==
278 loc:=0; buffer[limit]:="!";
279 while (buffer[loc]=" ")or(buffer[loc]=tab_mark) do incr(loc);
280 buffer[limit]:=" ";
281 if buffer[loc]="@@" then
282 if (buffer[loc+1]="*") or
283 (buffer[loc+1]=" ") or (buffer[loc+1]=tab_mark) then
284 change_pending:=#
285
286 @y
287 @z
288
289 @x 1389,1393d1406
290 change_pending:=false;
291 if not changed_module[module_count] then
292 begin if_module_start_then_make_change_pending(true);
293 if not change_pending then changed_module[module_count]:=true;
294 end;
295 @y
296 @z
297
298 @x 1405
299 @<Move |buffer| and |limit|...@>;
300 @y
301 @<Move \(|buffer| and |limit|...@>;
302 @z
303
304 Section 82.
305
306 @x 1450,1454c1463,1469
307 begin restart:if changing then
308 @<Read from |change_file| and maybe turn off |changing|@>;
309 if not changing then
310 begin @<Read from |web_file| and maybe turn on |changing|@>;
311 if changing then goto restart;
312 @y
313 begin restart: if changing then changed_module[module_count]:=true
314 else @<Read from |web_file| and maybe turn on |changing|@>;
315 if changing then
316 begin @<Read from |change_file| and maybe turn off |changing|@>;
317 if not changing then
318 begin changed_module[module_count]:=true; goto restart;
319 end;
320 @z
321
322 Section 83.
323
324 @x 1462c1477,1479
325 else if change_limit>0 then check_change;
326 @y
327 else if limit=change_limit then
328 if buffer[0]=change_buffer[0] then
329 if change_limit>0 then check_change;
330 @z
331
332 Section 84.
333
334 @x 1472,1479c1489
335 if limit>0 then {check if the change has ended}
336 begin if change_pending then
337 begin if_module_start_then_make_change_pending(false);
338 if change_pending then
339 begin changed_module[module_count]:=true; change_pending:=false;
340 end;
341 end;
342 buffer[limit]:=" ";
343 @y
344 if limit>1 then {check if the change has ended}
345 @z
346
347 @x 1491d1500
348 end;
349 @y
350 @z
351
352 Section 88.
353
354 @x 1594a1604,1606
355 "0","1","2": begin tracing:=c-"0"; control_code:=ignore;
356 @y
357 "0","1","2": begin tracing:=c-"0"; control_code:=ignore;
358 "3": begin @{'*****************************************************'@}
359 control_code:=ignore; {to set a breakpoint}
360 end;
361 @z
362
363 Section 110.
364
365 @x 1965c2032
366 changed_module[module_count]:=changing;
367 @y
368 changed_module[module_count]:=false;
369 @z
370
371 Section 111.
372
373 @x 1991c...
374 @p procedure Pascal_xref; {makes cross references for \PASCAL\ identifiers}
375 @y
376 @p @<Functions |scan_const| and |scan_exp|@>
377 @#
378 procedure Pascal_xref; {makes cross references for \PASCAL\ identifiers}
379 @z
380
381 @x 1992c2059
382 label exit;
383 @y
384 label exit,done,found,not_found;
385 @z
386
387 @x 1993a2061
388 var p:name_pointer; {a referenced name}
389 @y
390 var p:name_pointer; {a referenced name}
391 @!eq:0..3; {addition to |dtype| code}
392 @z
393
394 @x 1997c2065,2069
395 begin p:=id_lookup(next_control-identifier); new_xref(p);
396 @y
397 begin p:=id_lookup(next_control-identifier);
398 if next_control=identifier then if xref_switch<>0 then if ilk[p]=normal then
399 @<Figure out the |def_type| and |def_name|, etc.@>;
400 new_xref(p);
401 danger_zone:=(def_type=dtype_comma);
402 @z
403
404 @x 1999a2072,2078
405 xref_switch:=def_flag; {implied `\.{@@!}'}
406 @y
407 xref_switch:=def_flag; {implied `\.{@@!}'}
408 if ilk[p]=proc_like then
409 begin def_name:=p; next_control:=get_next;
410 if next_control<>identifier then goto done;
411 if xref_switch=0 then goto done;
412 p:=id_lookup(normal); def_type:=dtype_colon_bold;
413 new_xref(p);
414 end;
415 @z
416
417 @x 2002c2081
418 if (next_control="|")or(next_control="{") then return;
419 @y
420 done: if (next_control="|")or(next_control="{") then return;
421 @z
422
423 Section 115.
424
425 @x 2061c2208,2226
426 if next_control=definition then next_control:=get_next
427 @y
428 if next_control=definition then
429 begin next_control:=get_next;
430 if next_control=identifier then
431 begin lhs:=id_lookup(normal);
432 next_control:=get_next;
433 if (next_control=equivalence_sign)or(next_control="(") then
434 begin def_type:=dtype_macro; new_xref(lhs);
435 end
436 else if next_control="=" then
437 begin xref_switch:=0; next_control:=get_next;
438 def_val[lhs]:=scan_exp;
439 def_name:=def_val[lhs]; def_type:=dtype_const;
440 xref_switch:=def_flag;
441 if abs(def_name)>=32768 then def_type:=dtype_macro
442 else if def_name<0 then def_name:=def_name+65536;
443 new_xref(lhs);
444 end;
445 end;
446 end
447 @z
448
449 Section 124.
450
451 @x 2199,2201c2364
452 `\.{\\input webmac}'.
453 @.\\input webmac@>
454 @.webmac@>
455
456 If the user has sent the |pdf_output| flag (the `\.{-p}' option on the
457 command line), then we use alternative \TeX\ macros from `\.{\\input pwebmac}'.
458 @.\\input pwebmac@>
459 @.pwebmac@>
460 @y
461 `\.{\\input twimac-web}'.
462 @.\\input twimac-web@>
463 @.twimac-web@>
464 @z
465
466 @x 2204c2367
467 out_ptr:=1; out_line:=1; out_buf[1]:="c";
468 if pdf_output then write(tex_file,'\input pwebma')
469 else write(tex_file,'\input webma');
470 @y
471 out_ptr:=1; out_line:=1; out_buf[1]:="b"; write(tex_file,'\input twimac-we');
472 @z
473
474 Section 125.
475
476 @x 2219a2383,2389
477 @d oot5(#)==oot(#)@,oot4
478 @y
479 @d oot5(#)==oot(#)@,oot4
480 @d oot6(#)==oot(#)@,oot5
481 @d oot7(#)==oot(#)@,oot6
482 @d oot8(#)==oot(#)@,oot7
483 @d oot9(#)==oot(#)@,oot8
484 @d oot10(#)==oot(#)@,oot9
485 @d oot11(#)==oot(#)@,oot10
486 @d oot12(#)==oot(#)@,oot11
487 @z
488
489 @x 2224a2395,2401
490 @d out5==@+begin oot5
491 @y
492 @d out5==@+begin oot5
493 @d out6==@+begin oot6
494 @d out7==@+begin oot7
495 @d out8==@+begin oot8
496 @d out9==@+begin oot9
497 @d out10==@+begin oot10
498 @d out11==@+begin oot11
499 @d out12==@+begin oot12
500 @z
501
502 Section 130.
503
504 @x 2273c...
505 @ The number to be converted by |out_mod| is known to be less than
506 |def_flag|, so it cannot have more than five decimal digits. If
507 the module is changed, we output `\.{\\*}' just after the number.
508 @y
509 @ The number to be converted by |out_mod| is known to be less than
510 |def_flag|, so it cannot have more than five decimal digits.
511 @z
512
513 @x 2285d
514 if changed_module[m] then out2("\")("*");
515 @.\\*@>
516 @y
517 @z
518
519 Section 133.
520
521 @x 2333,2334c2510,2512
522 out("@@");
523 if c<>"@@" then err_print('! Double @@ required outside of sections');
524 @.Double \AT! required...@>
525 @y
526 if (c<>"z")and(c<>"Z") then
527 begin out("@@");
528 if c<>"@@" then err_print('! Double @@ required outside of sections');
529 @.Double \AT! required...@>
530 end;
531 @z
532
533 Section 146.
534
535 @x 2888c...
536 @ Token lists in |@!tok_mem| are composed of the following kinds of
537 @y
538 @ Token lists in |tok_mem| are composed of the following kinds of
539 @z
540
541 Section 183.
542
543 @x 3522a3702
544 @!p:name_pointer; {identifier designator}
545 @y
546 @!p:name_pointer; {identifier designator}
547 @!q,@!qq,@!r:integer; {registers for new reference insertion loop}
548 @z
549
550 Section 191.
551
552 @x 3698a3879
553 begin p:=id_lookup(normal);
554 @y
555 begin p:=id_lookup(normal);
556 if ilk[p]=normal then @<Insert a new reference, if this is new@>;
557 @z
558
559 Section 197.
560
561 @x 3816c...
562 @p function Pascal_translate: text_pointer;
563 @y
564 @d flaky=1
565 @d guaranteed=0
566
567 @p function Pascal_translate: text_pointer;
568 @z
569 @x 3819c4033
570 begin save_base:=scrap_base; scrap_base:=scrap_ptr+1;
571 @y
572 begin save_base:=scrap_base; scrap_base:=scrap_ptr+1; safety:=flaky;
573 @z
574
575 @x 3827c4041
576 Pascal_translate:=p;
577 @y
578 safety:=guaranteed; Pascal_translate:=p;
579 @z
580
581 Section 218.
582
583 @x 4226c4440
584 module_count:=0;
585 @y
586 module_count:=0; xx:=0;
587 @z
588
589 @x 4228a4443
590 finish_line; flush_buffer(0,false,false); {insert a blank line, it looks nice}
591 @y
592 finish_line; flush_buffer(0,false,false); {insert a blank line, it looks nice}
593 @<Prepare high-speed access to definitions via |dlink| and |def_val|@>;
594 @z
595
596 Section 220.
597
598 @x 4249a4492,4493
599 begin incr(module_count);@/
600 @y
601 begin incr(module_count);@/
602 mm:=module_count+def_flag; ref_lnk[0]:=0; ref_loc[0]:=0; new_ref_ptr:=0;
603 safety:=guaranteed;
604 @z
605
606 Section 236.
607
608 @x 4503a4749
609 if flag=0 then out("U")@+else out("A");
610 @y
611 if flag=0 then out("U")@+else out("A");
612 out8(" ")("s")("e")("c")("t")("i")("o")("n");
613 @z
614
615 @x 4514,4515c4760
616 @.\\As@>
617 @.\\Us@>
618 @y
619 out("~");
620 @z
621
622 Section 237.
623
624 @x 4519,4524c4764,4767
625 if num(xlink(cur_xref))>flag then out2(",")(" ") {not the last}
626 else begin out3("\")("E")("T"); {the last}
627 @.\\ET@>
628 if cur_xref<>xlink(q) then out("s"); {the last of more than two}
629 @.\\ETs@>
630 end;
631 @y
632 if (num(xlink(cur_xref))>flag)or(cur_xref<>xlink(q)) then out(",");
633 {not the last of two}
634 out(" ");
635 if num(xlink(cur_xref))<=flag then out4("a")("n")("d")("~"); {the last}
636 @z
637
638 Section 238.
639
640 @x 4529c4772,4776
641 out3("\")("f")("i"); finish_line;
642 @y
643 out6("\")("m")("i")("n")("i")("%");
644 @.\\mini@>
645 beta_out;
646 flush_buffer(out_ptr,false,false);
647 out4("}")("\")("F")("I"); finish_line;
648 @z
649
650 @x 4531c4778,4893
651 @.\\fi@>
652 @y
653 @.\\FI@>
654 @z
655
656 Section 241.
657
658 @x 4561a4924
659 k_module:=1;
660 @y
661 k_module:=1;
662 while not changed_module[k_module] do incr(k_module);
663 @z
664
665 @x 4563,4568d4925
666 while k_module<module_count do
667 begin if changed_module[k_module] then
668 begin out_mod(k_module); out2(",")(" ");
669 end;
670 incr(k_module);
671 end;
672 @y
673 @z
674
675 @x 4569a4927,4929
676 out_mod(k_module);
677 @y
678 out_mod(k_module);
679 repeat repeat incr(k_module)@+ until changed_module[k_module];
680 out2(",")(" "); out_mod(k_module);
681 until k_module=module_count;
682 @z
683
684 Section 254.
685
686 @x 4726c5086,5093
687 if cur_val<def_flag then out_mod(cur_val)
688 @y
689 if cur_val<def_flag then
690 begin out_mod(cur_val);
691 if cur_val+1=num(xlink(cur_xref)) then
692 begin out2("-")("-");
693 repeat cur_xref:=xlink(cur_xref); incr(cur_val);
694 until cur_val+1<>num(xlink(cur_xref)); out_mod(cur_val);
695 end;
696 end
697 @z
698
699 Section 261.
700
701 @x 4842c...
702 @p procedure Phase_I;
703 @y
704 @p @<Functions |alpha_out|, |beta_out|, and helpers@>
705 @#
706 procedure Phase_I;
707 @z
708
709 @x 4846a5214
710 procedure Phase_II;
711 @y
712 procedure Phase_II;
713 var lhs:integer;
714 @z
715
716 Section 264.
717
718 @x WEAVE.CH
719 const n_options = 4; {Pascal won't count array lengths for us.}
720 @y
721 const n_options = 3; {Pascal won't count array lengths for us.}
722 @z
723
724 @x WEAVE.CH
725 usage_help (WEAVE_HELP, nil);
726 @y
727 usage_help (TWILL_HELP, nil);
728 @z
729
730 Section 268.
731
732 @x WEAVE.CH
733 @ Use alternative \TeX\ macros more suited for {\mc PDF} output?
734 @.-p@>
735
736 @<Define the option...@> =
737 long_options[current_option].name := char_to_string ('p');
738 long_options[current_option].has_arg := 0;
739 long_options[current_option].flag := address_of (pdf_output);
740 long_options[current_option].val := 1;
741 incr (current_option);
742
743 @y
744 @z
745
746 @x
747 @!pdf_output:c_int_type;
748 @y
749 @z
750
751 @x
752 @!web_name,@!chg_name,@!tex_name:const_c_string;
753 @y
754 @!web_name,@!chg_name,@!tex_name:const_c_string;
755
756 @* New material for {\tentex TWILL}.
757 Here's a new subroutine needed for \.{TWILL}. Assuming that |next_control|
758 is the beginning of a numeric constant, and that string constants have
759 length~1, the |scan_const| function returns the value of the constant
760 and sets |next_control| to the following token.
761
762 @<Functions |scan_const| and |scan_exp|@>=
763 function scan_const:integer;
764 label done;
765 var radix,@!accum,p:integer;
766 begin if next_control=string then
767 begin accum:=buffer[id_first+1]; next_control:=get_next; goto done;
768 end
769 else if next_control=identifier then
770 begin p:=id_lookup(normal); new_blank_xref(p);
771 accum:=def_val[p]; next_control:=get_next; goto done;
772 end
773 else begin accum:=0;
774 if next_control=hex then radix:=16
775 else if next_control=octal then radix:=8
776 else begin radix:=10; accum:=next_control-"0";
777 end;
778 loop begin next_control :=get_next;
779 if next_control<"0" then goto done;
780 if radix=16 then
781 begin if (next_control>="A")and(next_control<="F") then
782 next_control:=next_control-"A"+"0"+10
783 else if next_control>"9" then goto done;
784 end
785 else if next_control>="0"+radix then goto done;
786 accum:=accum*radix+next_control-"0";
787 end;
788 end;
789 done: scan_const:=accum;
790 end;
791
792 @ Simple linear arithmetic is handled by the following
793 subroutine, which doesn't complain about certain syntactic errors.
794
795 @d start_of_const(#)==(((#>="0")and(#<="9"))or(#=hex)or(#=octal)or(#=string)
796 or(#=identifier))
797 @d sign(#)==(abs(#-",")=1)
798 @d start_of_num(#)==(start_of_const(#)or sign(#))
799
800 @<Functions |scan_const| and |scan_exp|@>=
801 function scan_exp:integer;
802 label done;
803 var @!accum,s:integer;
804 begin if sign(next_control) then accum:=0
805 else accum:=scan_const;
806 loop begin if not sign(next_control) then goto done;
807 s:=","-next_control; next_control:=get_next;
808 if not start_of_const(next_control) then goto done;
809 accum:=accum+s*scan_const;
810 end;
811 done:scan_exp:=accum;
812 end;
813
814 @ @d found_it(#)==begin def_type:=#; goto found; end
815
816 @<Figure out the |def_type| and |def_name|, etc.@>=
817 begin next_control:=get_next;
818 if next_control="," then found_it(dtype_comma);
819 if(next_control=":")or(next_control="=") then
820 @<Figure out a type and |goto| either |found| or |not_found|@>;
821 not_found:def_type:=dtype_none; new_xref(p); goto done;
822 found: end
823
824 @ @<Figure out a type and...@>=
825 begin eq:=(next_control-":") div ("="-":");
826 next_control:=get_next;
827 if next_control=identifier then
828 @<Figure out a type starting with an identifier;
829 |goto| |found| or |not_found| unless it's a subrange@>
830 else if (next_control=string)and(id_loc-id_first>2) then found_it(dtype_string)
831 else if start_of_num(next_control) then
832 @<Figure out a type starting with a constant;
833 |goto| |found| or |not_found| unless it's a subrange@>
834 else goto not_found;
835 next_control:=get_next;
836 if next_control=identifier then
837 begin def_subname:=id_lookup(normal);
838 if ilk[def_subname]<>normal then goto not_found;
839 if def_val[def_subname]=undef_val then
840 begin new_blank_xref(def_subname);
841 def_subtype:=dtype_colon_ital_dots; goto found;
842 end;
843 end;
844 if start_of_num(next_control) then
845 begin def_subname:=scan_exp; def_subtype:=dtype_colon_const_dots;
846 if abs(def_subname)>=32768 then goto not_found;
847 if def_subname<0 then def_subname:=def_subname+65536;
848 goto found;
849 end;
850 goto not_found;
851 end
852
853 @ @<Figure out a type starting with an identifier...@>=
854 begin def_name:=id_lookup(normal);
855 if ilk[def_name]=goto_like then {\&{packed}}
856 begin next_control:=get_next; eq:=eq+2;
857 if next_control<>identifier then goto not_found;
858 def_name:=id_lookup(normal);
859 end;
860 if (ilk[def_name]=array_like)or(ilk[def_name]=record_like) then
861 found_it(dtype_colon_bold+eq);
862 if ilk[def_name]<>normal then goto found;
863 new_blank_xref(def_name);
864 if def_val[def_name]=undef_val then
865 begin next_control:=get_next;
866 if next_control=double_dot then def_type:=dtype_colon_ital_dots+eq
867 else found_it(dtype_colon_ital+eq);
868 end
869 else @<Figure out a type starting with a constant...@>;
870 end
871
872 @ @<Figure out a type starting with a constant...@>=
873 begin def_name:=scan_exp;
874 if next_control<>double_dot then
875 if eq=1 then found_it(dtype_equal_bold; def_name:=const_name)
876 else goto not_found;
877 if abs(def_name)>=32768 then goto not_found;
878 if def_name<0 then def_name:=def_name+65536;
879 def_type:=dtype_colon_const_dots+eq;
880 end
881
882 @ Some identifiers can be inserted during Phase II that weren't
883 seen in Phase I (namely, if they appear only in module names);
884 so we have to watch out that |def_val| might be |undef_val|.
885
886 @<Insert a new reference, if this is new@>=
887 begin q:=def_val[p];
888 if q<>0 then if q<>undef_val then if not phase_three then
889 begin repeat r:=q; q:=dlink(q);
890 until (q=0) or (num(q)>mm);
891 if num(r)<>mm then
892 begin qq:=0; q:=ref_lnk[0];
893 while ref_loc[q]>r do
894 begin qq:=q; q:=ref_lnk[qq];
895 end;
896 if ref_loc[q]=r then
897 begin if safety=guaranteed then
898 if ref_safety[q]=flaky then ref_safety[q]:=guaranteed;
899 end
900 else begin if new_ref_ptr=max_new_refs then overflow('new references');
901 incr(new_ref_ptr);
902 ref_lnk[new_ref_ptr]:=q; ref_lnk[qq]:=new_ref_ptr;
903 ref_loc[new_ref_ptr]:=r;
904 if dlink(def_val[p])=0 then ref_safety[new_ref_ptr]:=guaranteed
905 else ref_safety[new_ref_ptr]:=safety;
906 end;
907 end;
908 end;
909 end
910
911 @ @<Prepare high-speed access to definitions via |dlink| and |def_val|@>=
912 for lhs:=1 to name_ptr do if ilk[lhs]=normal then
913 begin def_val[lhs]:=0; rhs:=xref[lhs];
914 while rhs<>0 do
915 begin if num(rhs)>def_flag then
916 begin dlink(rhs):=def_val[lhs];
917 def_val[lhs]:=rhs;
918 end;
919 rhs:=xlink(rhs);
920 end;
921 end
922
923 @ We keep a separate list of all references made in the current module,
924 sorted by |xref| number. A reference is considered to need manual checking
925 if it appears only in a comment within the section and if the corresponding
926 identifier is multiply defined.
927
928 @<Glob...@>=
929 @!mm:integer; {current module number plus |def_flag|}
930 @!ref_loc:array[0..max_new_refs] of sixteen_bits;
931 @!ref_lnk:array[0..max_new_refs] of sixteen_bits;
932 @!ref_safety:array[0..max_new_refs] of guaranteed..flaky;
933 @!new_ref_ptr:0..max_new_refs;
934 @!safety:guaranteed..flaky;
935 @!xx:xref_number;
936
937 @ The |alpha_out| procedure makes entries for all identifiers defined
938 in the current module. (However, I no longer need these!)
939
940 @<Functions |alpha_out|, |beta_out|, and helpers@>=
941 procedure alpha_out;
942 label exit;
943 var p,w,k:integer;
944 begin loop begin
945 if xx=xref_ptr then return;
946 if num(xx+1)>mm then return;
947 incr(xx);
948 if num(xx)>def_flag then if dback(xx)>0 then
949 if ilk[dback(xx)]=normal then
950 begin flush_buffer(out_ptr,false,false);
951 out5("\")("m")("i")("n")("i");
952 @.\\mini@>
953 p:=dback(xx);
954 w:=p mod ww;
955 for k:=byte_start[p] to byte_start[p+ww]-1 do out(byte_mem[w,k]);
956 if dtype(xx)>dtype_comma then incr(xx);
957 end;
958 end;
959 exit:end;
960
961 @ Here's a procedure that's very much like |out_mod|.
962
963 @<Functions |alpha_out|, |beta_out|, and helpers@>=
964 procedure out_const(@!n:sixteen_bits);
965 var k:0..5; {index into |dig|}
966 @!a:integer; {accumulator}
967 begin k:=0; a:=n;
968 if a>=32768 then
969 begin out("-"); a:=65536-a;
970 end;
971 repeat dig[k]:=a mod 10; a:=a div 10; incr(k);
972 until a=0;
973 repeat decr(k); out(dig[k]+"0");
974 until k=0;
975 end;
976
977 @ And here's something that could have been made a subroutine earlier.
978
979 @<Functions |alpha_out|, |beta_out|, and helpers@>=
980 procedure out_id(@!p:integer);
981 begin out("\");
982 if ilk[p]=normal then
983 if length(p)=1 then out("|")
984 @.\\|@>
985 else out("\")
986 @.\\\\@>
987 else out("&");
988 @.\\\&@>
989 if length(p)=1 then out(byte_mem[p mod ww,byte_start[p]])
990 else out_name(p);
991 end;
992
993 @ The |beta_out| procedure makes entries for all identifiers used
994 but not defined in the current module.
995
996 @<Functions |alpha_out|, |beta_out|, and helpers@>=
997 procedure beta_out;
998 label done,888,found;
999 var k,p,q,w,xx,mmm:integer;
1000 begin p:=ref_lnk[0];
1001 while p<>0 do
1002 begin flush_buffer(out_ptr,false,false);
1003 out2("\")("[");
1004 xx:=ref_loc[p]; q:=dback(xx); w:=q mod ww;
1005 for k:=byte_start[q] to byte_start[q+ww]-1 do out(byte_mem[w,k]);
1006 out(" "); out_const(num(xx)-def_flag);
1007 out(" ");
1008 @<Move past commas@>;
1009 @<Output the reference, based on its |dtype|@>;
1010 if ref_safety[p]=flaky then out3(" ")("%")("?");
1011 p:=ref_lnk[p];
1012 end;
1013 end;
1014
1015 @ @<Output the reference, based on its |dtype|@>=
1016 case dtype(xx) of
1017 dtype_none: out5("\")("n")("o")("n")("e");
1018 dtype_macro: out6("=")("m")("a")("c")("r")("o");
1019 dtype_const: begin out2("=")("$"); out_const(dname(xx)); out("$");
1020 end;
1021 dtype_string:
1022 out12("\")(".")("{")("""")("s")("t")("r")("i")("n")("g")("""")("}");
1023 dtype_colon_bold,dtype_colon_ital: begin out(":"); out_id(dname(xx)); end;
1024 dtype_equal_bold,dtype_equal_ital: begin out("="); out_id(dname(xx)); end;
1025 dtype_colon_packed: begin out(":"); out_id(packed_name); out2("\")(" ");
1026 out_id(dname(xx)); end;
1027 dtype_equal_packed: begin out("="); out_id(packed_name); out2("\")(" ");
1028 out_id(dname(xx)); end;
1029 dtype_colon_const_dots: begin out2(":")("$"); out_const(dname(xx));
1030 goto 888; end;
1031 dtype_equal_const_dots: begin out2("=")("$"); out_const(dname(xx));
1032 goto 888; end;
1033 dtype_colon_ital_dots: begin out2(":")("$"); out_id(dname(xx)); goto 888; end;
1034 dtype_equal_ital_dots: begin out2("=")("$"); out_id(dname(xx)); goto 888; end;
1035 end; {there are no other cases}
1036 goto found;
1037 888: out3("\")("t")("o");
1038 if dtype(xx+1)=dtype_colon_ital_dots then
1039 out_id(dname(xx+1))
1040 else out_const(dname(xx+1));
1041 out("$");
1042 found:
1043
1044 @ @<Move past commas@>=
1045 mmm:=num(xx);
1046 loop begin if dtype(xx)<>dtype_comma then if num(xx)=mmm then goto done;
1047 if xx=xref_ptr then
1048 begin dtype(xx):=dtype_none; goto done;
1049 end;
1050 if num(xx+1)>mmm then
1051 begin dtype(xx):=dtype_none; goto done;
1052 end;
1053 incr(xx);
1054 end;
1055 done:
1056 @z