forked from william8000/lout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
z08.c
2223 lines (1942 loc) · 76.6 KB
/
z08.c
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*@z08.c:Object Manifest:ReplaceWithSplit()@**********************************/
/* */
/* THE LOUT DOCUMENT FORMATTING SYSTEM (VERSION 3.42) */
/* COPYRIGHT (C) 1991, 2008 Jeffrey H. Kingston */
/* */
/* Jeffrey H. Kingston (jeff@it.usyd.edu.au) */
/* School of Information Technologies */
/* The University of Sydney 2006 */
/* AUSTRALIA */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either Version 3, or (at your option) */
/* any later version. */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program; if not, write to the Free Software */
/* Foundation, Inc., 59 Temple Place, Suite 330, Boston MA 02111-1307 USA */
/* */
/* FILE: z08.c */
/* MODULE: Object Manifest */
/* EXTERNS: Manifest() */
/* */
/*****************************************************************************/
#include "externs.h"
#define line_breaker(g) \
(vspace(g) > 0 || (units(gap(g)) == FRAME_UNIT && width(gap(g)) > FR))
/*****************************************************************************/
/* */
/* static SetUnderline(x) */
/* */
/* Set underline() flags in object x to UNDER_ON as appropriate. */
/* */
/*****************************************************************************/
static void SetUnderline(OBJECT x)
{ OBJECT y, link;
debug2(DOM, DD, " Manifest underlining %s %s", Image(type(x)),EchoObject(x));
if( type(x) == ACAT )
{ for( link = Down(x); link != x; link = NextDown(link) )
{ Child(y, link);
SetUnderline(y);
}
}
debug3(DOM, DDD, " SetUnderline underline() := %s for %s %s",
"UNDER_ON", Image(type(x)), EchoObject(x));
underline(x) = UNDER_ON;
} /* end SetUnderline */
/*****************************************************************************/
/* */
/* static ReplaceWithSplit(x, bthr, fthr) */
/* */
/* Replace object x with a SPLIT object, if threads for this object are */
/* requested by bthr and/or fthr. */
/* */
/*****************************************************************************/
#define ReplaceWithSplit(x, bthr, fthr) \
if( bthr[ROWM] || bthr[COLM] || fthr[ROWM] || fthr[COLM] ) \
x = insert_split(x, bthr, fthr)
static OBJECT insert_split(OBJECT x, OBJECT bthr[2], OBJECT fthr[2])
{ OBJECT res, new_op; int dim;
debug1(DOM, DD, "ReplaceWithSplit(%s, -)", EchoObject(x));
assert( type(x) != SPLIT, "ReplaceWithSplit: type(x) already SPLIT!" );
New(res, SPLIT);
back(res, COLM) = back(res, ROWM) = fwd(res, COLM) = fwd(res, ROWM) = 0;
FposCopy(fpos(res), fpos(x));
ReplaceNode(res, x);
for( dim = COLM; dim <= ROWM; dim++ )
{ if( bthr[dim] || fthr[dim] )
{
debug0(DGP, DD, " calling New(thread) from Manifest now");
New(new_op, dim == COLM ? COL_THR : ROW_THR);
thr_state(new_op) = NOTSIZED;
fwd(new_op, 1-dim) = 0; /* will hold max frame_size */
back(new_op, 1-dim) = 0; /* will hold max frame_origin */
FposCopy(fpos(new_op), fpos(x));
Link(res, new_op); Link(new_op, x);
if( bthr[dim] ) Link(bthr[dim], new_op);
if( fthr[dim] ) Link(fthr[dim], new_op);
}
else Link(res, x);
}
debug1(DOM, DD, "ReplaceWithSplit returning %s", EchoObject(res));
return res;
} /* end insert_split */
/*@::ReplaceWithTidy()@*******************************************************/
/* */
/* OBJECT ReplaceWithTidy(x, spacing) */
/* */
/* Replace object x with a tidier version in which juxtapositions are */
/* folded. If this is not possible, return the original object. */
/* */
/* Spacing can have value 0, 1, or 2, with the following meaning: */
/* */
/* ACAT_TIDY An ACAT is the preferred result */
/* */
/* WORD_TIDY The result is to be a single QWORD with interword spaces */
/* converted to as many space characters as there were. */
/* */
/* PARA_TIDY The result is to be a single QWORD with interword spaces */
/* replaced by single spaces, and line ends marked by */
/* newline characters. */
/* */
/*****************************************************************************/
OBJECT ReplaceWithTidy(OBJECT x, int spacing)
{ static FULL_CHAR buff[MAX_WORD]; /* the growing current word */
static int buff_len; /* length of current word */
static FILE_POS buff_pos; /* filepos of current word */
static unsigned buff_typ; /* WORD or QWORD of current */
OBJECT link, y, tmp, res; /* temporaries */
int i;
debug2(DOM, DD, "ReplaceWithTidy(%s, %s)", EchoObject(x),
spacing == ACAT_TIDY ? "ACAT_TIDY" :
spacing == WORD_TIDY ? "WORD_TIDY" : "PARA_TIDY");
switch( type(x) )
{
case ACAT:
for( link = Down(x); link != x; link = NextDown(link) )
{ Child(y, link);
if( type(y) == ACAT )
{ tmp = Down(y); TransferLinks(tmp, y, link);
DisposeChild(link); link = PrevDown(tmp);
}
}
res = nilobj; buff_len = 0; buff_typ = WORD;
FposCopy(buff_pos, fpos(x));
for( link = Down(x); link != x; link = NextDown(link) )
{ Child(y, link);
if( is_word(type(y)) )
{ if( buff_len + StringLength(string(y)) >= MAX_WORD )
Error(8, 1, "word is too long (%d characters)", WARN, &fpos(y),
buff_len + StringLength(string(y)));
else
{ if( buff_len == 0 ) FposCopy(buff_pos, fpos(y));
StringCopy(&buff[buff_len], string(y));
buff_len += StringLength(string(y));
if( type(y) == QWORD ) buff_typ = QWORD;
}
}
else if( type(y) == GAP_OBJ )
{ if( Down(y) != y || hspace(y) + vspace(y) > 0 )
{
switch( spacing )
{
case WORD_TIDY:
if( buff_len + hspace(y) + vspace(y) >= MAX_WORD )
Error(8, 2, "word is too long (%d characters)", WARN, &fpos(y),
buff_len + hspace(y) + vspace(y) );
else
{ for( i = 0; i < hspace(y) + vspace(y); i++ )
{ StringCopy(&buff[buff_len], AsciiToFull(" "));
buff_len++;
}
buff_typ = QWORD;
}
break;
case ACAT_TIDY:
tmp = MakeWord(buff_typ, buff, &buff_pos);
buff_len = 0; buff_typ = WORD;
if( res == nilobj )
{ New(res, ACAT);
FposCopy(fpos(res), fpos(x));
}
Link(res, tmp); Link(res, y);
break;
case PARA_TIDY:
if( buff_len + hspace(y) + vspace(y) >= MAX_WORD )
Error(8, 2, "word is too long (%d characters)", WARN, &fpos(y),
buff_len + hspace(y) + vspace(y));
else
{
for( i = 0; i < vspace(y); i++ )
{ StringCopy(&buff[buff_len], STR_NEWLINE);
buff_len++;
}
for( i = 0; i < hspace(y); i++ )
{ StringCopy(&buff[buff_len], STR_SPACE);
buff_len++;
}
buff_typ = QWORD;
}
break;
default:
assert(FALSE, "bad spacing type in ReplaceWithTidy");
break;
}
}
}
else /* error */
{ if( res != nilobj ) DisposeObject(res);
debug0(DOM, DD, "ReplaceWithTidy returning unchanged");
return x;
}
}
tmp = MakeWord(buff_typ, buff, &buff_pos);
if( res == nilobj ) res = tmp;
else Link(res, tmp);
ReplaceNode(res, x); DisposeObject(x);
debug1(DOM, DD, "ReplaceWithTidy returning %s", EchoObject(res));
return res;
case WORD:
case QWORD:
debug1(DOM, DD, "ReplaceWithTidy returning %s", EchoObject(x));
return x;
default:
debug0(DOM, DD, "ReplaceWithTidy returning unchanged");
return x;
}
} /* end ReplaceWithTidy */
/*@::GetScaleFactor()@********************************************************/
/* */
/* static float GetScaleFactor(x) */
/* */
/* Find a scale factor in object x and return it as a float, after checks. */
/* */
/*****************************************************************************/
float GetScaleFactor(OBJECT x)
{ float scale_factor;
if( !is_word(type(x)) )
{ Error(8, 3, "replacing invalid scale factor by 1.0", WARN, &fpos(x));
scale_factor = 1.0;
}
else if( sscanf( (char *) string(x), "%f", &scale_factor) != 1 )
{ Error(8, 4, "replacing invalid scale factor %s by 1.0",
WARN, &fpos(x), string(x));
scale_factor = 1.0;
}
else if( scale_factor < 0.01 )
{ Error(8, 5, "replacing undersized scale factor %s by 1.0",
WARN, &fpos(x), string(x));
scale_factor = 1.0;
}
else if( scale_factor > 100 )
{ Error(8, 6, "replacing oversized scale factor %s by 1.0",
WARN, &fpos(x), string(x));
scale_factor = 1.0;
}
return scale_factor;
} /* GetScaleFactor */
static OBJECT nbt[2] = { nilobj, nilobj }; /* constant nilobj threads */
static OBJECT nft[2] = { nilobj, nilobj }; /* constant nilobj threads */
static OBJECT ntarget = nilobj; /* constant nilobj target */
static OBJECT nenclose = nilobj; /* constant nilobj enclose */
/*@::ManifestCat@*************************************************************/
/* */
/* OBJECT ManifestCat(x,env,style,bthr, fthr, target, crs, ok, need_expand, */
/* enclose, fcr) */
/* */
/* This procedure implements Manifest (see below) when x is HCAT or VCAT. */
/* */
/*****************************************************************************/
static OBJECT ManifestCat(OBJECT x, OBJECT env, STYLE *style, OBJECT bthr[2],
OBJECT fthr[2], OBJECT *target, OBJECT *crs, BOOLEAN ok, BOOLEAN need_expand,
OBJECT *enclose, BOOLEAN fcr)
{ OBJECT bt[2], ft[2], y, link, gaplink, g, first_bt, last_ft, z;
int par, perp;
unsigned res_inc; BOOLEAN still_backing;
STYLE new_style, gap_style;
debug1(DOM, DD, "[ ManifestCat(%s)", EchoObject(x));
StyleCopy(new_style, *style);
if( type(x) == HCAT )
{ par = ROWM;
adjust_cat(x) = hadjust(*style);
hadjust(new_style) = FALSE;
}
else
{ par = COLM;
adjust_cat(x) = vadjust(*style);
vadjust(new_style) = FALSE;
}
perp = 1 - par;
link = Down(x);
gaplink = NextDown(link);
assert( link!=x && gaplink!=x, "Manifest/VCAT: less than two children!" );
Child(y, link); Child(g, gaplink);
/* set bt and ft threads for y */
bt[perp] = bthr[perp];
ft[perp] = fthr[perp];
if( bthr[par] ) { New(first_bt, THREAD); }
else first_bt = nilobj;
bt[par] = first_bt;
if( join(gap(g)) ) { New(ft[par], THREAD); }
else ft[par] = nilobj;
still_backing = first_bt != nilobj;
/* manifest y and insinuate any cross-references */
y = Manifest(y, env, &new_style, bt, ft, target, crs, ok, FALSE, enclose, fcr);
if( type(x) == VCAT && ok && *crs != nilobj )
{ debug1(DCR, DD, " insinuating %s", EchoObject(*crs));
TransferLinks(Down(*crs), *crs, link);
DisposeObject(*crs);
*crs = nilobj;
}
/* manifest the remaining children */
while( g != nilobj )
{
/* manifest the gap object, store it in gap(g), add perp threads */
assert( type(g) == GAP_OBJ, "Manifest/VCAT: type(g) != GAP_OBJECT!" );
assert( Down(g) != g, "Manifest/VCAT: GAP_OBJ has no child!" );
Child(z, Down(g));
debug2(DOM, DD, "manifesting gap, z = %s, style = %s",
EchoObject(z), EchoStyle(style));
z = Manifest(z, env, &new_style, nbt, nft, &ntarget, crs, FALSE,
FALSE, enclose, fcr);
debug1(DOM, DD, "after manifesting gap, z = %s", EchoObject(z));
if( type(z) == ACAT )
StyleCopy(gap_style, save_style(z));
else
StyleCopy(gap_style, *style);
z = ReplaceWithTidy(z, ACAT_TIDY);
debug1(DOM, DD, "calling GetGap, style = %s", EchoStyle(&gap_style));
GetGap(z, &gap_style, &gap(g), &res_inc);
if( bt[perp] ) Link(bt[perp], g);
if( ft[perp] ) Link(ft[perp], g);
/* find the next child y, and following gap if any */
link = NextDown(gaplink);
assert( link != x, "Manifest/VCAT: GAP_OBJ is last child!" );
Child(y, link);
gaplink = NextDown(link);
if( gaplink == x ) g = nilobj;
else Child(g, gaplink);
/* set bt and ft threads for y */
last_ft = ft[par];
if( ft[par] ) { New(bt[par], THREAD); } else bt[par] = nilobj;
if( g != nilobj )
{ if( join(gap(g)) ) { New(ft[par], THREAD); } else ft[par] = nilobj;
}
else
{
if( fthr[par] ) { New(ft[par], THREAD); } else ft[par] = nilobj;
}
/* manifest y and insinuate any cross references */
y = Manifest(y, env, &new_style, bt, ft, target, crs, ok, FALSE, enclose, fcr);
if( type(x) == VCAT && ok && *crs != nilobj )
{ debug1(DCR, DD, " insinuating %s", EchoObject(*crs));
TransferLinks(Down(*crs), *crs, link);
DisposeObject(*crs);
*crs = nilobj;
}
if( bt[par] ) /* then thread lists last_ft and bt[par] must merge */
{ OBJECT llink, rlink, lthread, rthread;
BOOLEAN goes_through;
assert( Down(bt[par]) != bt[par], "Manifest: bt[par] no children!" );
assert( last_ft!=nilobj && Down(last_ft)!=last_ft, "Manifest:last_ft!" );
/* check whether marks run right through y in par direction */
goes_through = FALSE;
if( ft[par] )
{ assert( Down(ft[par]) != ft[par], "Manifest: ft[par] child!" );
Child(lthread, LastDown(bt[par]));
Child(rthread, LastDown(ft[par]));
goes_through = lthread == rthread;
}
/* merge the thread lists */
llink = Down(last_ft); rlink = Down(bt[par]);
while( llink != last_ft && rlink != bt[par] )
{ Child(lthread, llink);
Child(rthread, rlink);
assert( lthread != rthread, "Manifest: lthread == rthread!" );
MergeNode(lthread, rthread);
llink = NextDown(llink);
rlink = NextDown(rlink);
}
/* attach leftover back threads to first_bt if required */
if( rlink != bt[par] )
{
if( still_backing ) TransferLinks(rlink, bt[par], first_bt);
}
DisposeObject(bt[par]);
/* attach leftover forward threads to ft[par] if required */
if( llink != last_ft )
{
if( goes_through ) TransferLinks(llink, last_ft, ft[par]);
}
DisposeObject(last_ft);
if( !goes_through ) still_backing = FALSE;
}
else still_backing = FALSE;
} /* end while */
/* export par threads */
if( fthr[par] ) MergeNode(fthr[par], ft[par]);
if( bthr[par] ) MergeNode(bthr[par], first_bt);
debug0(DOM, DD, "] ManifestCat returning");
return x;
} /* end ManifestCat */
/*@::ManifestCase@************************************************************/
/* */
/* OBJECT ManifestCase(x,env,style,bthr,fthr, target, crs, ok, need_expand, */
/* enclose, fcr) */
/* */
/* This procedure implements Manifest (see below) when x is CASE. */
/* */
/*****************************************************************************/
static OBJECT ManifestCase(OBJECT x, OBJECT env, STYLE *style, OBJECT bthr[2],
OBJECT fthr[2], OBJECT *target, OBJECT *crs, BOOLEAN ok, BOOLEAN need_expand,
OBJECT *enclose, BOOLEAN fcr)
{ OBJECT y, tag, ylink, yield, ytag, zlink;
OBJECT res, z, firsttag, firstres = nilobj;
/* make sure left parameter (the tag) is in order */
debug0(DOM, DD, " manifesting CASE now");
Child(tag, Down(x));
debug1(DOM, DD, " manifesting CASE tag %s now", EchoObject(tag));
tag = Manifest(tag, env, style, nbt, nft, &ntarget, crs, FALSE, FALSE,
&nenclose, fcr);
tag = ReplaceWithTidy(tag, ACAT_TIDY);
/* make sure the right parameter is an ACAT */
Child(y, LastDown(x));
if( type(y) == YIELD )
{ New(z, ACAT);
MoveLink(Up(y), z, PARENT);
Link(x, z);
y = z;
}
if( type(y) != ACAT )
{ Error(8, 7, "%s deleted (right parameter is malformed)",
WARN, &fpos(y), KW_CASE);
y = MakeWord(WORD, STR_EMPTY, &fpos(x));
ReplaceNode(y, x); DisposeObject(x);
x = Manifest(y, env, style, bthr, fthr, target, crs, ok, FALSE, enclose, fcr);
return x;
}
/* hunt through right parameter for res, the selected case */
res = nilobj; firsttag = nilobj;
for( ylink = Down(y); ylink != y && res == nilobj; ylink = NextDown(ylink) )
{ Child(yield, ylink);
if( type(yield) == GAP_OBJ ) continue;
if( type(yield) != YIELD )
{ Error(8, 8, "%s expected here", WARN, &fpos(yield), KW_YIELD);
break;
}
Child(ytag, Down(yield));
ytag = Manifest(ytag, env, style, nbt, nft, &ntarget, crs, FALSE, FALSE,
&nenclose, fcr);
ytag = ReplaceWithTidy(ytag, ACAT_TIDY);
if( is_word(type(ytag)) )
{ if( firsttag == nilobj )
{ firsttag = ytag;
Child(firstres, LastDown(yield));
}
if( (is_word(type(tag)) && StringEqual(string(ytag), string(tag))) ||
StringEqual(string(ytag), STR_ELSE) )
{ Child(res, LastDown(yield));
break;
}
}
else if( type(ytag) == ACAT )
{ z = ytag;
for( zlink = Down(z); zlink != z; zlink = NextDown(zlink) )
{ Child(ytag, zlink);
if( type(ytag) == GAP_OBJ ) continue;
if( !is_word(type(ytag)) )
{ Error(8, 9, "error in left parameter of %s",
WARN, &fpos(ytag), KW_YIELD);
break;
}
if( firsttag == nilobj )
{ firsttag = ytag;
Child(firstres, LastDown(yield));
}
if( (is_word(type(tag)) && StringEqual(string(ytag), string(tag)))
|| StringEqual(string(ytag), STR_ELSE) )
{ Child(res, LastDown(yield));
break;
}
}
}
else Error(8, 10, "error in left parameter of %s",
WARN, &fpos(ytag), KW_YIELD);
}
if( res == nilobj )
{ if( firsttag != nilobj )
{ Error(8, 11, "replacing unknown %s option %s by %s",
WARN, &fpos(tag), KW_CASE, string(tag), string(firsttag));
res = firstres;
debug1(DGP, D, " res = %s", EchoObject(res));
}
else
{ Error(8, 12, "%s deleted (choice %s unknown)",
WARN, &fpos(tag), KW_CASE, string(tag));
y = MakeWord(WORD, STR_EMPTY, &fpos(x));
ReplaceNode(y, x); DisposeObject(x);
x = Manifest(y, env, style, bthr, fthr, target, crs, ok, FALSE, enclose, fcr);
return x;
}
}
/* now manifest the result and replace x with it */
DeleteLink(Up(res));
ReplaceNode(res, x);
DisposeObject(x);
x = Manifest(res, env, style, bthr, fthr, target, crs, ok, FALSE, enclose, fcr);
return x;
} /* ManifestCase */
/*@::ManifestTg@**************************************************************/
/* */
/* OBJECT ManifestTg(x,env,style,bthr, fthr, target, crs, ok, need_expand, */
/* enclose, fcr) */
/* */
/* This procedure implements Manifest (see below) when x is TAGGED. */
/* */
/*****************************************************************************/
static OBJECT ManifestTg(OBJECT x, OBJECT env, STYLE *style, OBJECT bthr[2],
OBJECT fthr[2], OBJECT *target, OBJECT *crs, BOOLEAN ok, BOOLEAN need_expand,
OBJECT *enclose, BOOLEAN fcr)
{ OBJECT y, tag, z;
/* make sure first argument is a cross-reference */
assert( Down(x) != x && NextDown(Down(x)) != x &&
NextDown(NextDown(Down(x))) == x, "Manifest TAGGED: children!" );
Child(y, Down(x));
if( !is_cross(type(y)) )
{
y = Manifest(y, env, style, nbt, nft, &ntarget, crs, FALSE, FALSE, &nenclose, TRUE);
if( !is_cross(type(y)) )
{ Error(8, 13, "left parameter of %s is not a cross reference",
WARN, &fpos(y), KW_TAGGED);
y = MakeWord(WORD, STR_EMPTY, &fpos(x));
ReplaceNode(y, x); DisposeObject(x);
x = Manifest(y, env, style, bthr, fthr, target, crs, ok, FALSE, enclose, fcr);
return x;
}
}
/* make sure the arguments of the cross-reference are OK */
Child(z, Down(y));
if( type(z) != CLOSURE )
{ Error(8, 14, "left parameter of %s must be a symbol",
WARN, &fpos(y), KW_TAGGED);
y = MakeWord(WORD, STR_EMPTY, &fpos(x));
ReplaceNode(y, x); DisposeObject(x);
x = Manifest(y, env, style, bthr, fthr, target, crs, ok, FALSE, enclose, fcr);
return x;
}
if( !has_tag(actual(z)) )
{ Error(8, 15, "symbol %s not allowed here (it has no %s)",
WARN, &fpos(z), SymName(actual(z)), KW_TAG);
y = MakeWord(WORD, STR_EMPTY, &fpos(x));
ReplaceNode(y, x); DisposeObject(x);
x = Manifest(y, env, style, bthr, fthr, target, crs, ok, FALSE, enclose, fcr);
return x;
}
Child(z, NextDown(Down(y)));
z = Manifest(z, env, style, nbt, nft, &ntarget, crs, FALSE, FALSE, &nenclose, fcr);
z = ReplaceWithTidy(z, ACAT_TIDY);
if( is_word(type(z)) && StringEqual(string(z), KW_PRECEDING) )
cross_type(y) = CROSS_PREC;
else if( is_word(type(z)) && StringEqual(string(z), KW_FOLLOWING) )
cross_type(y) = CROSS_FOLL;
else if( is_word(type(z)) && StringEqual(string(z), KW_FOLL_OR_PREC) )
cross_type(y) = CROSS_FOLL_OR_PREC;
else
{ Error(8, 16, "%s, %s or %s expected in left parameter of %s",
WARN, &fpos(z), KW_PRECEDING, KW_FOLLOWING, KW_FOLL_OR_PREC, KW_TAGGED);
y = MakeWord(WORD, STR_EMPTY, &fpos(x));
ReplaceNode(y, x); DisposeObject(x);
x = Manifest(y, env, style, bthr, fthr, target, crs, ok, FALSE, enclose, fcr);
return x;
}
/* make sure second argument (the new key) is ok */
Child(tag, LastDown(x));
tag = Manifest(tag, env, style, nbt, nft, &ntarget, crs, FALSE, FALSE, &nenclose, fcr);
tag = ReplaceWithTidy(tag, WORD_TIDY); /* && */
if( !is_word(type(tag)) )
{ Error(8, 17, "right parameter of %s must be a simple word",
WARN, &fpos(tag), KW_TAGGED);
ifdebug(DOM, DD, DebugObject(tag));
y = MakeWord(WORD, STR_EMPTY, &fpos(x));
ReplaceNode(y, x); DisposeObject(x);
x = Manifest(y, env, style, bthr, fthr, target, crs, ok, FALSE, enclose, fcr);
return x;
}
/* assemble insinuated cross reference which replaces x */
ReplaceNode(tag, z);
DisposeObject(z);
ReplaceNode(y, x);
DisposeObject(x);
x = y;
ReplaceWithSplit(x, bthr, fthr);
debug1(DCR, DD, " tagged manifesting %s", EchoObject(x));
return x;
} /* end ManifestTg */
/*@::ManifestCl@**************************************************************/
/* */
/* OBJECT ManifestCl(x,env,style,bthr, fthr, target, crs, ok, need_expand, */
/* enclose, fcr) */
/* */
/* This procedure implements Manifest (see below) when x is CLOSURE. */
/* */
/*****************************************************************************/
static OBJECT ManifestCl(OBJECT x, OBJECT env, STYLE *style, OBJECT bthr[2],
OBJECT fthr[2], OBJECT *target, OBJECT *crs, BOOLEAN ok, BOOLEAN need_expand,
OBJECT *enclose, BOOLEAN fcr)
{ OBJECT y, link, sym, res_env, hold_env, hold_env2, z, newz, command;
BOOLEAN symbol_free;
sym = actual(x);
StyleCopy(save_style(x), *style);
debugcond2(DOM, DD, StringEqual(SymName(sym), "@Section"),
"manifesting %s at %s", SymName(sym), EchoFilePos(&fpos(x)));
debug1(DOM, DD, " [ manifesting closure %s", SymName(sym));
/* enclose, if required */
if( *enclose != nilobj && (actual(x)==GalleySym || actual(x)==ForceGalleySym) )
{ OBJECT sym, par;
ReplaceNode(*enclose, x);
Child(sym, Down(*enclose));
Child(par, Down(sym));
DisposeChild(Down(par));
Link(par, x);
x = *enclose;
*enclose = nilobj;
debug1(DHY, DD, " Manifest/enclose: %s", EchoObject(x));
debug1(DOM, DD, " Manifest/enclose: %s", EchoObject(x));
x = Manifest(x, env, style, bthr, fthr, target, crs, ok, FALSE, enclose, fcr);
debug0(DOM, DD, " ] returning from manifesting closure (enclose)");
return x;
}
/* expand parameters where possible, and find if they are all free */
symbol_free = TRUE;
debugcond1(DOM, DD, indefinite(sym), " freeing %s", EchoObject(x));
for( link = Down(x); link != x; link = NextDown(link) )
{ Child(y, link);
assert( type(y) == PAR, "Manifest/CLOSURE: type(y) != PAR!" );
Child(z, Down(y));
/* try to evaluate the actual parameter z */
if( !is_word(type(z)) && !has_par(actual(y)) )
{
if( is_tag(actual(y)) || is_key(actual(y)) )
{ z = Manifest(z, env, style, nbt, nft, &ntarget, crs, FALSE, FALSE, &nenclose, fcr);
z = ReplaceWithTidy(z, WORD_TIDY);
if( !is_word(type(z)) )
{
debug2(ANY, D, "z = %s %s", Image(type(z)), EchoObject(z));
Error(8, 41, "this %s is not a sequence of one or more words", FATAL,
&fpos(y), SymName(actual(y)));
}
}
else if( type(z) == NEXT )
{ z = Manifest(z, env, style, nbt, nft, &ntarget, crs, FALSE, FALSE, &nenclose, fcr);
z = ReplaceWithTidy(z, ACAT_TIDY);
}
else if( type(z) == CLOSURE && is_par(type(actual(z))) )
{
/* see whether z would come to something simple if expanded */
newz = ParameterCheck(z, env);
debugcond2(DOM, DD, indefinite(sym), " ParameterCheck(%s, env) = %s",
EchoObject(z), EchoObject(newz));
if( newz != nilobj )
{ ReplaceNode(newz, z);
DisposeObject(z);
z = newz;
}
}
}
if( !is_word(type(z)) )
{ symbol_free = FALSE;
}
}
debugcond2(DOM, DD, indefinite(sym)," s_f = %s, x = %s",
bool(symbol_free), EchoObject(x));
/* if all parameters are free of symbols, optimize environment */
if( symbol_free && imports(sym) == nilobj && enclosing(sym) != StartSym )
{ y = SearchEnv(env, enclosing(sym));
if( y != nilobj && type(y) == CLOSURE )
{ OBJECT prntenv;
Parent(prntenv, Up(y));
assert(type(prntenv) == ENV, "Manifest: prntenv!");
if( Down(prntenv) == LastDown(prntenv) )
{ env = prntenv;
}
else
{ debug0(DCR, DDD, "calling SetEnv from Manifest (a)");
env = SetEnv(y, nilobj);
}
New(hold_env2, ACAT); Link(hold_env2, env);
}
else
{ /* *** letting this through now
if( has_par(enclosing(sym)) )
Error(8, 18, "symbol %s used outside %s", WARN, &fpos(x), SymName(sym),
SymName(enclosing(sym)));
*** */
hold_env2 = nilobj;
}
}
else hold_env2 = nilobj;
debug3(DOM, DD, " expansion: has_target %s, indefinite %s, recursive %s",
bool(has_target(sym)), bool(indefinite(sym)), bool(recursive(sym)));
if( has_target(sym) && !need_expand )
{
/* convert symbols with targets to unsized galleys */
OBJECT hd;
New(hd, HEAD);
FposCopy(fpos(hd), fpos(x));
actual(hd) = sym;
limiter(hd) = opt_components(hd) = opt_constraints(hd) = nilobj;
gall_dir(hd) = horiz_galley(sym);
ready_galls(hd) = nilobj;
must_expand(hd) = TRUE;
sized(hd) = FALSE;
seen_nojoin(hd) = FALSE;
ReplaceNode(hd, x);
Link(hd, x);
AttachEnv(env, x);
SetTarget(hd);
enclose_obj(hd) = (has_enclose(sym) ? BuildEnclose(hd) : nilobj);
ClearHeaders(hd);
x = hd;
threaded(x) = bthr[COLM] != nilobj || fthr[COLM] != nilobj;
ReplaceWithSplit(x, bthr, fthr);
debug3(DGA, D, " manifesting %sgalley %s at %s",
force_gall(hd) ? "force " : "", SymName(actual(hd)),
EchoFilePos(&fpos(hd)));
}
else if(
*target == sym ? (*target = nilobj, TRUE) :
need_expand ? TRUE :
uses_galley(sym) && !recursive(sym) ? TRUE :
!indefinite(sym) && !recursive(sym) ? TRUE :
indefinite(sym) && *target != nilobj ? SearchUses(sym, *target)
: FALSE
)
{
/* expand the closure and manifest the result */
debug1(DOM, DD, "expanding; style: %s", EchoStyle(style));
debug0(DCE, DD, " calling ClosureExpand from Manifest/CLOSURE");
/* *** now requesting cross refs always, not only if ok
x = ClosureExpand(x, env, ok, crs, &res_env);
*** */
x = ClosureExpand(x, env, TRUE, crs, &res_env);
New(hold_env, ACAT); Link(hold_env, res_env);
debug1(DOM, DD, "recursive call; style: %s", EchoStyle(style));
if( type(x) == FILTERED )
{ assert( type(sym) == RPAR, "ManifestCl/filtered: type(sym)!" );
assert( filter(enclosing(sym)) != nilobj, "ManifestCl filter-encl!" );
New(command, CLOSURE);
FposCopy(fpos(command), fpos(x));
actual(command) = filter(enclosing(sym));
FilterSetFileNames(x);
command = Manifest(command,env,style,nbt,nft,&ntarget,crs,FALSE,FALSE, &nenclose, fcr);
command = ReplaceWithTidy(command, WORD_TIDY);
if( !is_word(type(command)) )
Error(8, 19, "filter parameter of %s symbol is not simple",
FATAL, &fpos(command), SymName(enclosing(sym)));
y = FilterExecute(x, string(command), res_env);
debug2(DFH, D, "after \"%s\", will manifest result with style %s",
string(command), EchoStyle(style));
DisposeObject(command);
ReplaceNode(y, x);
DisposeObject(x);
x = y;
}
x = Manifest(x, res_env, style, bthr, fthr, target, crs, ok, FALSE, enclose, fcr);
DisposeObject(hold_env);
}
else
{
AttachEnv(env, x);
threaded(x) = bthr[COLM] != nilobj || fthr[COLM] != nilobj;
debug0(DOM, DD, " closure; calling ReplaceWithSplit");
ReplaceWithSplit(x, bthr, fthr);
}
if( hold_env2 != nilobj ) DisposeObject(hold_env2);
debug0(DOM, DD, " ] returning from manifesting closure");
return x;
} /* end ManifestCl */
/*@::Manifest()@**************************************************************/
/* */
/* OBJECT Manifest(x, env, style, bthr, fthr, target, crs, ok, need_expand, */
/* enclose, fcr) */
/* */
/* Manifest object x, interpreted in environment env and style style. */
/* The result replaces x, and is returned also. */
/* The manifesting operation converts x from a pure parse tree object */
/* containing closures and no threads, to an object ready for sizing, */
/* with fonts propagated to the words, fill styles propagated to the */
/* ACATs, and line spacings propagated to all interested parties. */
/* All non-recursive, non-indefinite closures are expanded. */
/* Threads joining objects on a mark are constructed, and SPLIT objects */
/* inserted, so that sizing becomes a trivial operation. */
/* */
/* Manifest will construct threads and pass them up as children of bthr[] */
/* and fthr[] whenever non-nilobj values of these variables are passed in: */
/* */
/* bthr[COLM] protrudes upwards from x */
/* fthr[COLM] protrudes downwards from x */
/* bthr[ROWM] protrudes leftwards from x */
/* fthr[ROWM] protrudes rightwards from x */
/* */
/* If *target != nilobj, Manifest will expand indefinite closures leading */
/* to the first @Galley lying within an object of type *target. */
/* */
/* If *target != nilobj and *enclose != nilobj, Manifest will enclose */
/* any @Galley or @ForceGalley it comes across in *enclose. */
/* */
/* The env parameter contains the environment in which x is to be */
/* evaluated. Environments are shared, so their correct disposal is not */
/* simple. The rule is this: the code which creates an environment, or */
/* detaches it, is responsible for holding it with a dummy parent until */
/* it is no longer required. */
/* */
/* Some objects x are not "real" in the sense that they do not give rise */
/* to rectangles in the final printed document. The left parameter of */
/* @Wide and similar operators, and the gap following a concatenation */
/* operator, are examples of such non-real objects. The ok flag is true */
/* when x is part of a real object. This is needed because some things, */
/* such as the insinuation of cross references, the breaking of */
/* lines @Break ACAT objects, and conversion to small capitals, only apply */
/* to real objects. */
/* */
/* If *crs != nilobj, it points to a list of indexes to cross-references */
/* which are to be insinuated into the manifested form of x if x is real. */
/* */
/* If need_expand is TRUE it forces closure x to expand. */
/* */
/* If fcr is TRUE, the objective is to expand until a cross-reference is */
/* the result; so expansion will stop at a CROSS or FORCE_CROSS object. */
/* */
/* A postcondition of Manifest() is that the underline() flag is set to */
/* either UNDER_ON or UNDER_OFF in every WORD, every QWORD, and every child */
/* of every ACAT, including the gaps. This can be verified by checking */
/* that the WORD and QWORD cases set underline() to UNDER_OFF, and the ACAT */
/* case sets every child of the ACAT to UNDER_OFF. To see that the correct */
/* subset of these flags gets changed to UNDER_ON, consult SetUnderline(). */
/* The underline() flag is undefined otherwise, and should have value */
/* UNDER_UNDEF. */
/* */
/*****************************************************************************/
#define MAX_DEPTH 2000
OBJECT Manifest(OBJECT x, OBJECT env, STYLE *style, OBJECT bthr[2],
OBJECT fthr[2], OBJECT *target, OBJECT *crs, BOOLEAN ok, BOOLEAN need_expand,
OBJECT *enclose, BOOLEAN fcr)
{ OBJECT bt[2], ft[2], y, link, nextlink, gaplink, g, gword;
register FULL_CHAR *p;
OBJECT res = nilobj, res_env, res_env2, hold_env, hold_env2, z, prev;
OBJECT link1, link2, x1, x2, y1, y2, vc, value_env, key, value;
int i, par, num1, num2; GAP res_gap; unsigned res_inc; STYLE new_style;
BOOLEAN done, multiline; FULL_CHAR ch; float scale_factor;
static int depth = 0;
#if DEBUG_ON
static unsigned int debug_type[MAX_DEPTH];
static OBJECT debug_actual[MAX_DEPTH];
static int debug_lnum[MAX_DEPTH];
BOOLEAN eee = (*enclose != nilobj);
debug_type[depth] = type(x);
debug_lnum[depth] = line_num(fpos(x));
if( type(x) == CLOSURE ) debug_actual[depth] = actual(x);
depth++;
if( depth == MAX_DEPTH )
{ Error(8, 20, "maximum depth of symbol expansion (%d) reached",
WARN, &fpos(x), MAX_DEPTH);
Error(8, 21, "the symbols currently being expanded are:", WARN, &fpos(x));
while( --depth >= 0 )
{
Error(8, 22, "at %d: %d %s %s", WARN, &fpos(x), depth, debug_lnum[depth],
Image(debug_type[depth]), debug_type[depth] == CLOSURE ?
FullSymName(debug_actual[depth], AsciiToFull(".")) : STR_EMPTY);
}
Error(8, 23, "exiting now", FATAL, &fpos(x));
}
#else
depth++;
if( depth == MAX_DEPTH )
{
Error(8, 40, "maximum depth of symbol expansion (%d) reached",
FATAL, &fpos(x), MAX_DEPTH);
}
#endif
debug1(DOM, DD, "[Manifest(%s)", Image(type(x)));
debug0(DOM, DD, " object: ");
ifdebug(DOM, DD, DebugObject(x));
debug1(DOM, DD, " environment: %s", EchoObject(env));
debug6(DOM, DD, " style: %s; target: %s; threads: %s%s%s%s",
EchoStyle(style), SymName(*target),
bthr[COLM] ? " up" : "", fthr[COLM] ? " down" : "",
bthr[ROWM] ? " left" : "", fthr[ROWM] ? " right" : "");
debugcond2(DHY, DD, eee, "[ Manifest(%s, *enclose = %s)",
EchoObject(x), EchoObject(*enclose));
switch( type(x) )
{
case ENV_OBJ:
debug0(DHY, DD, "[Manifest env_obj:");
ifdebug(DHY, DD, DebugObject(x));
Child(y, Down(x));
Child(res_env, NextDown(Down(x)));
assert( type(res_env) == ENV, "Manifest/ENV_OBJ: res_env!");
y = Manifest(y, res_env, style, bthr, fthr, target, crs, ok, TRUE, enclose, fcr);
/* we always expand children of ENV_OBJ (need_expand == TRUE) */
ReplaceNode(y, x);
DisposeObject(x);
x = y;
debug1(DHY, DD, "]Manifest env_obj returning %s", EchoObject(x));
break;
case CLOSURE:
x = ManifestCl(x, env, style, bthr, fthr, target, crs, ok, need_expand, enclose, fcr);
break;
case PAGE_LABEL:
Child(y, Down(x));
y = Manifest(y, env, style, nbt, nft, &ntarget, crs, FALSE, FALSE, &nenclose, fcr);
y = ReplaceWithTidy(y, WORD_TIDY);
ReplaceWithSplit(x, bthr, fthr);
break;
case NULL_CLOS:
StyleCopy(save_style(x), *style);
ReplaceWithSplit(x, bthr, fthr);
break;
case CROSS:
case FORCE_CROSS:
assert( Down(x) != x && LastDown(x) != Down(x), "Manifest: CROSS child!");