-
Notifications
You must be signed in to change notification settings - Fork 35
/
text.c
3100 lines (2718 loc) · 112 KB
/
text.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
/* ------------------------------------------------------------------------- */
/* "text" : Text translation, the abbreviations optimiser, the dictionary */
/* */
/* Part of Inform 6.43 */
/* copyright (c) Graham Nelson 1993 - 2024 */
/* */
/* ------------------------------------------------------------------------- */
#include "header.h"
uchar *low_strings; /* Allocated to low_strings_top */
int32 low_strings_top;
static memory_list low_strings_memlist;
int32 static_strings_extent; /* Number of bytes of static strings
made so far */
uchar *static_strings_area; /* Used to hold the static strings
area so far
Allocated to static_strings_extent */
memory_list static_strings_area_memlist;
static char *all_text; /* Text buffer holding the entire text
of the game, when it is being
recorded
(Allocated to all_text_top) */
static memory_list all_text_memlist;
static int32 all_text_top;
int abbrevs_lookup_table_made, /* The abbreviations lookup table is
constructed when the first non-
abbreviation string is translated:
this flag is TRUE after that */
abbrevs_lookup[256]; /* Once this has been constructed,
abbrevs_lookup[n] = the smallest
number of any abbreviation beginning
with ASCII character n, or -1
if none of the abbreviations do */
int no_abbreviations; /* No of abbreviations defined so far */
/* ------------------------------------------------------------------------- */
/* Glulx string compression storage */
/* ------------------------------------------------------------------------- */
int no_strings; /* No of strings in static strings
area. */
int no_dynamic_strings; /* No. of @.. string escapes used
(actually, the highest value used
plus one) */
int no_unicode_chars; /* Number of distinct Unicode chars
used. (Beyond 0xFF.) */
huffentity_t *huff_entities; /* The list of entities (characters,
abbreviations, @.. escapes, and
the terminator) */
static huffentity_t **hufflist; /* Copy of the list, for sorting */
int no_huff_entities; /* The number of entities in the list */
int huff_unicode_start; /* Position in the list where Unicode
chars begin. */
int huff_abbrev_start; /* Position in the list where string
abbreviations begin. */
int huff_dynam_start; /* Position in the list where @..
entities begin. */
int huff_entity_root; /* The position in the list of the root
entry (when considering the table
as a tree). */
int done_compression; /* Has the game text been compressed? */
int32 compression_table_size; /* Length of the Huffman table, in
bytes */
int32 compression_string_size; /* Length of the compressed string
data, in bytes */
int32 *compressed_offsets; /* The beginning of every string in
the game, relative to the beginning
of the Huffman table. (So entry 0
is equal to compression_table_size).
Allocated to no_strings at
compress_game_text() time. */
static memory_list compressed_offsets_memlist;
unicode_usage_t *unicode_usage_entries; /* Allocated to no_unicode_chars */
static memory_list unicode_usage_entries_memlist;
#define UNICODE_HASH_BUCKETS (64)
static int unicode_usage_hash[UNICODE_HASH_BUCKETS];
static int unicode_entity_index(int32 unicode);
/* ------------------------------------------------------------------------- */
/* Abbreviation arrays */
/* ------------------------------------------------------------------------- */
abbreviation *abbreviations; /* Allocated up to no_abbreviations */
static memory_list abbreviations_memlist;
/* Memory to hold the text of any abbreviation strings declared. */
static int32 abbreviations_totaltext;
static char *abbreviations_text; /* Allocated up to abbreviations_totaltext */
static memory_list abbreviations_text_memlist;
static int *abbreviations_optimal_parse_schedule;
static memory_list abbreviations_optimal_parse_schedule_memlist;
static int *abbreviations_optimal_parse_scores;
static memory_list abbreviations_optimal_parse_scores_memlist;
/* ------------------------------------------------------------------------- */
int32 total_chars_trans, /* Number of ASCII chars of text in */
total_bytes_trans, /* Number of bytes of Z-code text out */
zchars_trans_in_last_string; /* Number of Z-chars in last string:
needed only for abbrev efficiency
calculation in "directs.c" */
static int32 total_zchars_trans; /* Number of Z-chars of text out
(only used to calculate the above) */
static int zchars_out_buffer[3], /* During text translation, a buffer of
3 Z-chars at a time: when it's full
these are written as a 2-byte word */
zob_index; /* Index (0 to 2) into it */
uchar *translated_text; /* Area holding translated strings
until they are moved into the
static_strings_area below */
static memory_list translated_text_memlist;
static char *temp_symbol; /* Temporary symbol name used while
processing "@(...)". */
static memory_list temp_symbol_memlist;
static int32 text_out_pos; /* The "program counter" during text
translation: the next position to
write Z-coded text output to */
static int32 text_out_limit; /* The upper limit of text_out_pos
during text translation (or -1
for no limit) */
static int text_out_overflow; /* During text translation, becomes
true if text_out_pos tries to pass
text_out_limit */
/* ------------------------------------------------------------------------- */
/* For variables/arrays used by the dictionary manager, see below */
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
/* Prepare the abbreviations lookup table (used to speed up abbreviation */
/* detection in text translation). We first bubble-sort the abbrevs into */
/* alphabetical order (this is necessary for the detection algorithm to */
/* to work). Since the table is only prepared once, and for a table */
/* of size at most 96, there's no point using an efficient sort algorithm. */
/* ------------------------------------------------------------------------- */
static void make_abbrevs_lookup(void)
{ int bubble_sort, j, k;
char *p1, *p2;
do
{ bubble_sort = FALSE;
for (j=0; j<no_abbreviations; j++)
for (k=j+1; k<no_abbreviations; k++)
{ p1=abbreviation_text(j);
p2=abbreviation_text(k);
if (strcmp(p1,p2)<0)
{
abbreviation temp = abbreviations[j];
abbreviations[j] = abbreviations[k];
abbreviations[k] = temp;
bubble_sort = TRUE;
}
}
} while (bubble_sort);
for (j=no_abbreviations-1; j>=0; j--)
{ p1=abbreviation_text(j);
abbrevs_lookup[(uchar)p1[0]]=j;
abbreviations[j].freq=0;
}
abbrevs_lookup_table_made = TRUE;
}
/* ------------------------------------------------------------------------- */
/* Search the abbreviations lookup table (a routine which must be fast). */
/* The source text to compare is text[i], text[i+1], ... and this routine */
/* is only called if text[i] is indeed the first character of at least one */
/* abbreviation, "from" begin the least index into the abbreviations table */
/* of an abbreviation for which text[i] is the first character. Recall */
/* that the abbrevs table is in alphabetical order. */
/* */
/* The return value is -1 if there is no match. If there is a match, the */
/* text to be abbreviated out is over-written by a string of null chars */
/* with "ASCII" value 1, and the abbreviation number is returned. */
/* */
/* In Glulx, we *do not* do this overwriting with 1's. */
/* ------------------------------------------------------------------------- */
static int try_abbreviations_from(uchar *text, int i, int from)
{ int j, k; uchar *p, c;
c=text[i];
for (j=from;
j<no_abbreviations;
j++)
{
p=(uchar *)abbreviations_text+abbreviations[j].textpos;
if (c != p[0]) break;
if (text[i+1]==p[1])
{ for (k=2; p[k]!=0; k++)
if (text[i+k]!=p[k]) goto NotMatched;
if (!glulx_mode) {
for (k=0; p[k]!=0; k++) text[i+k]=1;
}
abbreviations[j].freq++;
return(j);
NotMatched: ;
}
}
return(-1);
}
/* Create an abbreviation. */
extern void make_abbreviation(char *text)
{
int alen;
int32 pos;
/* If -e mode is off, we won't waste space creating an abbreviation entry. */
if (!economy_switch)
return;
alen = strlen(text);
pos = abbreviations_totaltext;
ensure_memory_list_available(&abbreviations_memlist, no_abbreviations+1);
ensure_memory_list_available(&abbreviations_text_memlist, pos+alen+1);
strcpy(abbreviations_text+pos, text);
abbreviations_totaltext += (alen+1);
abbreviations[no_abbreviations].textpos = pos;
abbreviations[no_abbreviations].textlen = alen;
abbreviations[no_abbreviations].value = compile_string(text, STRCTX_ABBREV);
abbreviations[no_abbreviations].freq = 0;
/* The quality is the number of Z-chars saved by using this */
/* abbreviation: note that it takes 2 Z-chars to print it. */
abbreviations[no_abbreviations].quality = zchars_trans_in_last_string - 2;
if (abbreviations[no_abbreviations].quality <= 0) {
warning_named("Abbreviation does not save any characters:", text);
}
no_abbreviations++;
}
/* Return a pointer to the (uncompressed) abbreviation text.
This should be treated as temporary; it is only valid until the next
make_abbreviation() call. */
extern char *abbreviation_text(int num)
{
if (num < 0 || num >= no_abbreviations) {
compiler_error("Invalid abbrev for abbreviation_text()");
return "";
}
return abbreviations_text + abbreviations[num].textpos;
}
/* ------------------------------------------------------------------------- */
/* The front end routine for text translation. */
/* strctx indicates the purpose of the string. This is mostly used for */
/* informational output (gametext.txt), but we treat some string contexts */
/* specially during compilation. */
/* ------------------------------------------------------------------------- */
/* TODO: When called from a print statement (parse_print()), it would be
nice to detect if the generated string is exactly one character. In that
case, we could return the character value and a flag to indicate the
caller could use @print_char/@streamchar/@new_line/@streamunichar
instead of printing a compiled string.
We'd need a new STRCTX value or two to distinguish direct-printed strings
from referenceable strings.
Currently, parse_print() checks for the "^" case manually, which is a
bit icky. */
extern int32 compile_string(char *b, int strctx)
{ int32 i, j, k;
uchar *c;
int in_low_memory;
if (execution_never_reaches_here) {
/* No need to put strings into gametext.txt or the static/low
strings areas. */
if (strctx == STRCTX_GAME || strctx == STRCTX_GAMEOPC || strctx == STRCTX_LOWSTRING || strctx == STRCTX_INFIX) {
/* VENEER and VENEEROPC are only used at the translate_text level,
so we don't have to catch them here. */
return 0;
}
}
/* In Z-code, abbreviations go in the low memory pool (0x100). So
do strings explicitly defined with the Lowstring directive.
(In Glulx, the in_low_memory flag is ignored.) */
in_low_memory = (strctx == STRCTX_ABBREV || strctx == STRCTX_LOWSTRING);
if (!glulx_mode && in_low_memory)
{
k = translate_text(-1, b, strctx);
if (k<0) {
error("text translation failed");
k = 0;
}
ensure_memory_list_available(&low_strings_memlist, low_strings_top+k);
memcpy(low_strings+low_strings_top, translated_text, k);
j = low_strings_top;
low_strings_top += k;
return(0x21+(j/2));
}
if (glulx_mode && done_compression)
compiler_error("Tried to add a string after compression was done.");
i = translate_text(-1, b, strctx);
if (i < 0) {
error("text translation failed");
i = 0;
}
/* Insert null bytes as needed to ensure that the next static string */
/* also occurs at an address expressible as a packed address */
if (!glulx_mode) {
int textalign;
if (oddeven_packing_switch)
textalign = scale_factor*2;
else
textalign = scale_factor;
while ((i%textalign)!=0)
{
ensure_memory_list_available(&translated_text_memlist, i+2);
translated_text[i++] = 0;
translated_text[i++] = 0;
}
}
j = static_strings_extent;
ensure_memory_list_available(&static_strings_area_memlist, static_strings_extent+i);
for (c=translated_text; c<translated_text+i;
c++, static_strings_extent++)
static_strings_area[static_strings_extent] = *c;
if (!glulx_mode) {
return(j/scale_factor);
}
else {
/* The marker value is a one-based string number. (We reserve zero
to mean "not a string at all". */
return (++no_strings);
}
}
/* ------------------------------------------------------------------------- */
/* Output a single Z-character into the buffer, and flush it if full */
/* ------------------------------------------------------------------------- */
static void write_z_char_z(int i)
{ uint32 j;
ASSERT_ZCODE();
total_zchars_trans++;
zchars_out_buffer[zob_index++]=(i%32);
if (zob_index!=3) return;
zob_index=0;
j= zchars_out_buffer[0]*0x0400 + zchars_out_buffer[1]*0x0020
+ zchars_out_buffer[2];
if (text_out_limit >= 0) {
if (text_out_pos+2 > text_out_limit) {
text_out_overflow = TRUE;
return;
}
}
else {
ensure_memory_list_available(&translated_text_memlist, text_out_pos+2);
}
translated_text[text_out_pos++] = j/256; translated_text[text_out_pos++] = j%256;
total_bytes_trans+=2;
}
static void write_zscii(int zsc)
{
int lookup_value, in_alphabet;
if (zsc==' ')
{ write_z_char_z(0);
return;
}
if (zsc < 0x100) lookup_value = zscii_to_alphabet_grid[zsc];
else lookup_value = -1;
if (lookup_value >= 0)
{ alphabet_used[lookup_value] = 'Y';
in_alphabet = lookup_value/26;
if (in_alphabet==1) write_z_char_z(4); /* SHIFT to A1 */
if (in_alphabet==2) write_z_char_z(5); /* SHIFT to A2 */
write_z_char_z(lookup_value%26 + 6);
}
else
{ write_z_char_z(5); write_z_char_z(6);
write_z_char_z(zsc/32); write_z_char_z(zsc%32);
}
}
/* ------------------------------------------------------------------------- */
/* Finish a Z-coded string, padding out with Z-char 5s if necessary and */
/* setting the "end" bit on the final 2-byte word */
/* ------------------------------------------------------------------------- */
static void end_z_chars(void)
{
zchars_trans_in_last_string=total_zchars_trans-zchars_trans_in_last_string;
while (zob_index!=0) write_z_char_z(5);
if (text_out_pos < 2) {
/* Something went wrong. */
text_out_overflow = TRUE;
return;
}
translated_text[text_out_pos-2] += 128;
}
/* Glulx handles this much more simply -- compression is done elsewhere. */
static void write_z_char_g(int i)
{
ASSERT_GLULX();
if (text_out_limit >= 0) {
if (text_out_pos+1 > text_out_limit) {
text_out_overflow = TRUE;
return;
}
}
else {
ensure_memory_list_available(&translated_text_memlist, text_out_pos+1);
}
total_zchars_trans++;
translated_text[text_out_pos++] = i;
total_bytes_trans++;
}
/* Helper routine to compute the weight, in units, of a character handled by the Z-Machine */
static int zchar_weight(int c)
{
int lookup;
if (c == ' ') return 1;
lookup = iso_to_alphabet_grid[c];
if (lookup < 0) return 4;
if (lookup < 26) return 1;
return 2;
}
/* ------------------------------------------------------------------------- */
/* The main routine "text.c" provides to the rest of Inform: the text */
/* translator. s_text is the source text and the return value is the */
/* number of bytes translated. */
/* The translated text will be stored in translated_text. */
/* */
/* If p_limit is >= 0, the text length will not exceed that many bytes. */
/* If the translation tries to overflow this boundary, the return value */
/* will be -1. (You should display an error and not read translated_text.) */
/* */
/* If p_limit is negative, any amount of text is accepted (up to int32 */
/* anyway). */
/* */
/* Note that the source text may be corrupted by this routine. */
/* ------------------------------------------------------------------------- */
extern int32 translate_text(int32 p_limit, char *s_text, int strctx)
{ int i, j, k, in_alphabet, lookup_value, is_abbreviation;
int32 unicode; int zscii;
uchar *text_in;
if (p_limit >= 0) {
ensure_memory_list_available(&translated_text_memlist, p_limit);
}
/* For STRCTX_ABBREV, the string being translated is itself an
abbreviation string, so it can't make use of abbreviations. Set
the is_abbreviation flag to indicate this.
The compiler has historically set this flag for the Lowstring
directive as well -- the in_low_memory and is_abbreviation flag were
always the same. I am preserving that convention. */
is_abbreviation = (strctx == STRCTX_ABBREV || strctx == STRCTX_LOWSTRING);
/* Cast the input and output streams to unsigned char: text_out_pos will
advance as bytes of Z-coded text are written, but text_in doesn't */
text_in = (uchar *) s_text;
text_out_pos = 0;
text_out_limit = p_limit;
text_out_overflow = FALSE;
/* Remember the Z-chars total so that later we can subtract to find the
number of Z-chars translated on this string */
zchars_trans_in_last_string = total_zchars_trans;
/* Start with the Z-characters output buffer empty */
zob_index=0;
/* If this is the first text translated since the abbreviations were
declared, and if some were declared, then it's time to make the
lookup table for abbreviations
(Except: we don't if the text being translated is itself
the text of an abbreviation currently being defined) */
if ((!abbrevs_lookup_table_made) && (no_abbreviations > 0)
&& (!is_abbreviation))
make_abbrevs_lookup();
/* If we're storing the whole game text to memory, then add this text.
We will put two newlines between each text and four at the very end.
(The optimise code does a lot of sloppy text[i+2], so the extra
two newlines past all_text_top are necessary.) */
if ((!is_abbreviation) && (store_the_text))
{ int addlen = strlen(s_text);
ensure_memory_list_available(&all_text_memlist, all_text_top+addlen+5);
sprintf(all_text+all_text_top, "%s\n\n\n\n", s_text);
/* Advance past two newlines. */
all_text_top += (addlen+2);
}
if (transcript_switch) {
/* Omit veneer strings, unless we're using the new transcript format, which includes everything. */
if ((!veneer_mode) || TRANSCRIPT_FORMAT == 1) {
int label = strctx;
if (veneer_mode) {
if (label == STRCTX_GAME)
label = STRCTX_VENEER;
else if (label == STRCTX_GAMEOPC)
label = STRCTX_VENEEROPC;
}
write_to_transcript_file(s_text, label);
}
}
/* Computing the optimal way to parse strings to insert abbreviations with dynamic programming */
/* (ref: R.A. Wagner , "Common phrases and minimum-space text storage", Commun. ACM, 16 (3) (1973)) */
/* We compute this optimal way here; it's stored in abbreviations_optimal_parse_schedule */
if (economy_switch)
{
uchar *q, c;
int l, min_score, from;
int text_in_length;
text_in_length = strlen( (char*) text_in);
ensure_memory_list_available(&abbreviations_optimal_parse_schedule_memlist, text_in_length);
ensure_memory_list_available(&abbreviations_optimal_parse_scores_memlist, text_in_length+1);
abbreviations_optimal_parse_scores[text_in_length] = 0;
for(j=text_in_length-1; j>=0; j--)
{ /* Initial values: empty schedule, score = just write the letter without abbreviating. */
abbreviations_optimal_parse_schedule[j] = -1;
min_score = zchar_weight(text_in[j]) + abbreviations_optimal_parse_scores[j+1];
/* If there's an abbreviation starting with that letter... */
if ( (from = abbrevs_lookup[text_in[j]]) != -1)
{
c = text_in[j];
/* Loop on all abbreviations starting with what is in c. */
for (k=from;
k<no_abbreviations;
k++)
{
q=(uchar *)abbreviations_text+abbreviations[k].textpos;
if (c!=q[0]) break;
/* Let's compare; we also keep track of the length of the abbreviation. */
for (l=1; q[l]!=0; l++)
{ if (text_in[j+l]!=q[l]) {goto NotMatched;}
}
/* We have a match (length l), but is it smaller in size? */
if (min_score > 2 + abbreviations_optimal_parse_scores[j+l])
{ /* It is indeed smaller, so let's write it down in our schedule. */
min_score = 2 + abbreviations_optimal_parse_scores[j+l];
abbreviations_optimal_parse_schedule[j] = k;
}
NotMatched: ;
}
}
/* We gave it our best, this is the smallest we got. */
abbreviations_optimal_parse_scores[j] = min_score;
}
}
if (!glulx_mode) {
/* The empty string of Z-text is illegal, since it can't carry an end
bit: so we translate an empty string of ASCII text to just the
pad character 5. Printing this causes nothing to appear on screen. */
if (text_in[0]==0) write_z_char_z(5);
/* Loop through the characters of the null-terminated input text: note
that if 1 is written over a character in the input text, it is
afterwards ignored */
for (i=0; text_in[i]!=0; i++)
{ total_chars_trans++;
/* Contract ". " into ". " if double-space-removing switch set:
likewise "? " and "! " if the setting is high enough */
if ((double_space_setting >= 1)
&& (text_in[i+1]==' ') && (text_in[i+2]==' '))
{ if (text_in[i]=='.') text_in[i+2]=1;
if (double_space_setting >= 2)
{ if (text_in[i]=='?') text_in[i+2]=1;
if (text_in[i]=='!') text_in[i+2]=1;
}
}
/* Try abbreviations if the economy switch set. */
/* Look at the abbreviation schedule to see if we should abbreviate here. */
/* Note: Just because the schedule has something doesn't mean we should abbreviate there; */
/* sometimes you abbreviate before because it's better. If we have already replaced the */
/* char by a '1', it means we're in the middle of an abbreviation; don't try to abbreviate then. */
if ((economy_switch) && (!is_abbreviation) && text_in[i] != 1 &&
((j = abbreviations_optimal_parse_schedule[i]) != -1))
{
/* Fill with 1s, which will get ignored by everyone else. */
uchar *p = (uchar *)abbreviation_text(j);
for (k=0; p[k]!=0; k++) text_in[i+k]=1;
/* Actually write the abbreviation in the story file. */
abbreviations[j].freq++;
/* Abbreviations run from MAX_DYNAMIC_STRINGS to 96. */
j += MAX_DYNAMIC_STRINGS;
write_z_char_z(j/32+1); write_z_char_z(j%32);
}
/* If Unicode switch set, use text_to_unicode to perform UTF-8
decoding */
if (character_set_unicode && (text_in[i] & 0x80))
{ unicode = text_to_unicode((char *) (text_in+i));
zscii = unicode_to_zscii(unicode);
if (zscii != 5) write_zscii(zscii);
else
{ unicode_char_error(
"Character can only be used if declared in \
advance as part of 'Zcharacter table':", unicode);
}
i += textual_form_length - 1;
continue;
}
/* '@' is the escape character in Inform string notation: the various
possibilities are:
@@decimalnumber : write this ZSCII char (0 to 1023)
@twodigits or : write the abbreviation string with this
@(digits) decimal number
@(symbol) : write the abbreviation string with this
(constant) value
@accentcode : this accented character: e.g.,
for @'e write an E-acute
@{...} : this Unicode char (in hex) */
if (text_in[i]=='@')
{ if (text_in[i+1]=='@')
{
/* @@... (ascii value) */
i+=2; j=atoi((char *) (text_in+i));
switch(j)
{ /* Prevent ~ and ^ from being translated to double-quote
and new-line, as they ordinarily would be */
case 94: write_z_char_z(5); write_z_char_z(6);
write_z_char_z(94/32); write_z_char_z(94%32);
break;
case 126: write_z_char_z(5); write_z_char_z(6);
write_z_char_z(126/32); write_z_char_z(126%32);
break;
default: write_zscii(j); break;
}
while (isdigit(text_in[i])) i++;
i--;
}
else if (text_in[i+1]=='(')
{
/* @(...) (dynamic string) */
int len = 0, digits = 0;
i += 2;
/* This accepts "12xyz" as a symbol, which it really isn't,
but that just means it won't be found. */
while ((text_in[i] == '_' || isalnum(text_in[i]))) {
char ch = text_in[i++];
if (isdigit(ch)) digits++;
ensure_memory_list_available(&temp_symbol_memlist, len+1);
temp_symbol[len++] = ch;
}
ensure_memory_list_available(&temp_symbol_memlist, len+1);
temp_symbol[len] = '\0';
j = -1;
/* We would like to parse temp_symbol as *either* a decimal
number or a constant symbol. */
if (text_in[i] != ')' || len == 0) {
error("'@(...)' abbreviation must contain a symbol");
}
else if (digits == len) {
/* all digits; parse as decimal */
j = atoi(temp_symbol);
}
else {
int sym = get_symbol_index(temp_symbol);
if (sym < 0 || (symbols[sym].flags & UNKNOWN_SFLAG) || symbols[sym].type != CONSTANT_T || symbols[sym].marker) {
error_named("'@(...)' abbreviation expected a known constant value, but contained", temp_symbol);
}
else {
symbols[sym].flags |= USED_SFLAG;
j = symbols[sym].value;
}
}
if (!glulx_mode && j >= 96) {
error_max_dynamic_strings(j);
j = -1;
}
if (j >= MAX_DYNAMIC_STRINGS) {
error_max_dynamic_strings(j);
j = -1;
}
if (j >= 0) {
write_z_char_z(j/32+1); write_z_char_z(j%32);
}
else {
write_z_char_z(' '); /* error fallback */
}
}
else if (isdigit(text_in[i+1])!=0)
{ int d1, d2;
/* @.. (dynamic string) */
d1 = character_digit_value[text_in[i+1]];
d2 = character_digit_value[text_in[i+2]];
if ((d1 == 127) || (d1 >= 10) || (d2 == 127) || (d2 >= 10))
error("'@..' must have two decimal digits");
else
{
if (strctx == STRCTX_ABBREV || strctx == STRCTX_LOWSTRING)
warning("The Z-machine standard does not allow dynamic strings inside an abbreviation or dynamic string.");
j = d1*10 + d2;
if (!glulx_mode && j >= 96) {
error_max_dynamic_strings(j);
j = -1;
}
if (j >= MAX_DYNAMIC_STRINGS) {
/* Shouldn't get here with two digits */
error_max_dynamic_strings(j);
j = -1;
}
i+=2;
if (j >= 0) {
write_z_char_z(j/32+1); write_z_char_z(j%32);
}
else {
write_z_char_z(' '); /* error fallback */
}
}
}
else
{
/* A string escape specifying an unusual character */
unicode = text_to_unicode((char *) (text_in+i));
zscii = unicode_to_zscii(unicode);
if (zscii != 5) write_zscii(zscii);
else
{ unicode_char_error(
"Character can only be used if declared in \
advance as part of 'Zcharacter table':", unicode);
}
i += textual_form_length - 1;
}
}
else
{ /* Skip a character which has been over-written with the null
value 1 earlier on */
if (text_in[i]!=1)
{ if (text_in[i]==' ') write_z_char_z(0);
else
{ j = (int) text_in[i];
lookup_value = iso_to_alphabet_grid[j];
if (lookup_value < 0)
{ /* The character isn't in the standard alphabets, so
we have to use the ZSCII 4-Z-char sequence */
if (lookup_value == -5)
{ /* Character isn't in the ZSCII set at all */
unicode = iso_to_unicode(j);
unicode_char_error(
"Character can only be used if declared in \
advance as part of 'Zcharacter table':", unicode);
write_zscii(0x200 + unicode/0x100);
write_zscii(0x300 + unicode%0x100);
}
else write_zscii(-lookup_value);
}
else
{ /* The character is in one of the standard alphabets:
write a SHIFT to temporarily change alphabet if
it isn't in alphabet 0, then write the Z-char */
alphabet_used[lookup_value] = 'Y';
in_alphabet = lookup_value/26;
if (in_alphabet==1) write_z_char_z(4); /* SHIFT to A1 */
if (in_alphabet==2) write_z_char_z(5); /* SHIFT to A2 */
write_z_char_z(lookup_value%26 + 6);
}
}
}
}
}
/* Flush the Z-characters output buffer and set the "end" bit */
end_z_chars();
}
else {
/* The text storage here is, of course, temporary. Compression
will occur when we're finished compiling, so that all the
clever Huffman stuff will work.
In the stored text, we use "@@" to indicate @,
"@0" to indicate a zero byte,
"@ANNNN" to indicate an abbreviation,
"@DNNNN" to indicate a dynamic string thing.
"@UNNNN" to indicate a four-byte Unicode value (0x100 or higher).
(NNNN is a four-digit hex number using the letters A-P... an
ugly representation but a convenient one.)
*/
for (i=0; text_in[i]!=0; i++) {
/* Contract ". " into ". " if double-space-removing switch set:
likewise "? " and "! " if the setting is high enough. */
if ((double_space_setting >= 1)
&& (text_in[i+1]==' ') && (text_in[i+2]==' ')) {
if (text_in[i]=='.'
|| (double_space_setting >= 2
&& (text_in[i]=='?' || text_in[i]=='!'))) {
text_in[i+1] = text_in[i];
i++;
}
}
total_chars_trans++;
/* Try abbreviations if the economy switch set. We have to be in
compression mode too, since the abbreviation mechanism is part
of string decompression. */
if ((economy_switch) && (compression_switch) && (!is_abbreviation)
&& ((k=abbrevs_lookup[text_in[i]])!=-1)
&& ((j=try_abbreviations_from(text_in, i, k)) != -1)) {
char *cx = abbreviation_text(j);
i += (strlen(cx)-1);
write_z_char_g('@');
write_z_char_g('A');
write_z_char_g('A' + ((j >>12) & 0x0F));
write_z_char_g('A' + ((j >> 8) & 0x0F));
write_z_char_g('A' + ((j >> 4) & 0x0F));
write_z_char_g('A' + ((j ) & 0x0F));
}
else if (text_in[i] == '@') {
if (text_in[i+1]=='@') {
/* An ASCII code */
i+=2; j=atoi((char *) (text_in+i));
if (j == '@' || j == '\0') {
write_z_char_g('@');
if (j == 0) {
j = '0';
if (!compression_switch)
warning("Ascii @@0 will prematurely terminate non-compressed \
string.");
}
}
write_z_char_g(j);
while (isdigit(text_in[i])) i++;
i--;
}
else if (text_in[i+1]=='(') {
int len = 0, digits = 0;
i += 2;
/* This accepts "12xyz" as a symbol, which it really isn't,
but that just means it won't be found. */
while ((text_in[i] == '_' || isalnum(text_in[i]))) {
char ch = text_in[i++];
if (isdigit(ch)) digits++;
ensure_memory_list_available(&temp_symbol_memlist, len+1);
temp_symbol[len++] = ch;
}
ensure_memory_list_available(&temp_symbol_memlist, len+1);
temp_symbol[len] = '\0';
j = -1;
/* We would like to parse temp_symbol as *either* a decimal
number or a constant symbol. */
if (text_in[i] != ')' || len == 0) {
error("'@(...)' abbreviation must contain a symbol");
}
else if (digits == len) {
/* all digits; parse as decimal */
j = atoi(temp_symbol);
}
else {
int sym = get_symbol_index(temp_symbol);
if (sym < 0 || (symbols[sym].flags & UNKNOWN_SFLAG) || symbols[sym].type != CONSTANT_T || symbols[sym].marker) {
error_named("'@(...)' abbreviation expected a known constant value, but contained", temp_symbol);
}
else {
symbols[sym].flags |= USED_SFLAG;
j = symbols[sym].value;
}
}
if (j >= MAX_DYNAMIC_STRINGS) {
error_max_dynamic_strings(j);
j = -1;
}
if (j+1 >= no_dynamic_strings)
no_dynamic_strings = j+1;
if (j >= 0) {
write_z_char_g('@');
write_z_char_g('D');
write_z_char_g('A' + ((j >>12) & 0x0F));
write_z_char_g('A' + ((j >> 8) & 0x0F));
write_z_char_g('A' + ((j >> 4) & 0x0F));
write_z_char_g('A' + ((j ) & 0x0F));
}
else {
write_z_char_g(' '); /* error fallback */
}
}
else if (isdigit(text_in[i+1])) {
int d1, d2;
d1 = character_digit_value[text_in[i+1]];
d2 = character_digit_value[text_in[i+2]];
if ((d1 == 127) || (d1 >= 10) || (d2 == 127) || (d2 >= 10)) {
error("'@..' must have two decimal digits");
}
else {
if (!compression_switch)
warning("'@..' print variable will not work in non-compressed \
string; substituting ' '.");
i += 2;
j = d1*10 + d2;
if (j >= MAX_DYNAMIC_STRINGS) {
error_max_dynamic_strings(j);
j = -1;
}
if (j+1 >= no_dynamic_strings)
no_dynamic_strings = j+1;
if (j >= 0) {
write_z_char_g('@');
write_z_char_g('D');
write_z_char_g('A' + ((j >>12) & 0x0F));
write_z_char_g('A' + ((j >> 8) & 0x0F));
write_z_char_g('A' + ((j >> 4) & 0x0F));
write_z_char_g('A' + ((j ) & 0x0F));
}
else {
write_z_char_g(' '); /* error fallback */
}
}
}
else {
unicode = text_to_unicode((char *) (text_in+i));
i += textual_form_length - 1;
if (unicode == '@' || unicode == '\0') {
write_z_char_g('@');
write_z_char_g(unicode ? '@' : '0');
}
else if (unicode >= 0 && unicode < 256) {
write_z_char_g(unicode);
}
else {
if (!compression_switch) {
warning("Unicode characters will not work in non-compressed \
string; substituting '?'.");
write_z_char_g('?');