-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathobjects.c
2449 lines (2072 loc) · 94.2 KB
/
objects.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
/* ------------------------------------------------------------------------- */
/* "objects" : [1] the object-maker, which constructs objects and enters */
/* them into the tree, given a low-level specification; */
/* */
/* [2] the parser of Object/Nearby/Class directives, which */
/* checks syntax and translates such directives into */
/* specifications for the object-maker. */
/* */
/* Part of Inform 6.43 */
/* copyright (c) Graham Nelson 1993 - 2024 */
/* */
/* ------------------------------------------------------------------------- */
#include "header.h"
/* ------------------------------------------------------------------------- */
/* Objects. */
/* ------------------------------------------------------------------------- */
int no_objects; /* Number of objects made so far */
static int no_embedded_routines; /* Used for naming routines which
are given as property values: these
are called EmbeddedRoutine__1, ... */
static fpropt full_object; /* "fpropt" is a typedef for a struct
containing an array to hold the
attribute and property values of
a single object. We only keep one
of these, for the current object
being made, and compile it into
Z-machine tables when each object
definition is complete, since
sizeof(fpropt) is about 6200 bytes */
static fproptg full_object_g; /* Equivalent for Glulx. This object
is very small, since the large arrays
are allocated dynamically as
memory-lists */
static char *shortname_buffer; /* Text buffer to hold the short name
(which is read in first, but
written almost last) */
static memory_list shortname_buffer_memlist;
static int parent_of_this_obj;
static memory_list current_object_name; /* The name of the object currently
being defined. */
static int current_classname_symbol; /* The symbol index of the class
currently being defined.
For error-checking and printing
names of embedded routines only. */
static memory_list embedded_function_name; /* Temporary storage for inline
function name in property. */
/* ------------------------------------------------------------------------- */
/* Classes. */
/* ------------------------------------------------------------------------- */
/* Arrays defined below: */
/* */
/* classinfo class_info[] Object number and prop offset */
/* int classes_to_inherit_from[] The list of classes to inherit */
/* from as taken from the current */
/* Nearby/Object/Class definition */
/* ------------------------------------------------------------------------- */
int no_classes; /* Number of class defns made so far */
static int current_defn_is_class, /* TRUE if current Nearby/Object/Class
defn is in fact a Class definition */
no_classes_to_inherit_from; /* Number of classes in the list
of classes to inherit in the
current Nearby/Object/Class defn */
/* ------------------------------------------------------------------------- */
/* Making attributes and properties. */
/* ------------------------------------------------------------------------- */
int no_attributes, /* Number of attributes defined so far */
no_properties; /* Number of properties defined so far,
plus 1 (properties are numbered from
1 and Inform creates "name" and two
others itself, so the variable begins
the compilation pass set to 4) */
/* Print a PROPS trace line. The f flag is 0 for an attribute, 1 for
a common property, 2 for an individual property. */
static void trace_s(char *name, int32 number, int f)
{ char *stype = "";
if (!printprops_switch) return;
if (f == 0) stype = "Attr";
else if (f == 1) stype = "Prop";
else if (f == 2) stype = "Indiv";
printf("%-5s %02ld ", stype, (long int) number);
if (f != 1) printf(" ");
else printf("%s%s",(commonprops[number].is_long)?"L":" ",
(commonprops[number].is_additive)?"A":" ");
printf(" %-24s (%s)\n", name, current_location_text());
}
extern void make_attribute(void)
{ int i; char *name;
debug_location_beginning beginning_debug_location =
get_token_location_beginning();
if (!glulx_mode) {
if (no_attributes==((version_number==3)?32:48))
{ discard_token_location(beginning_debug_location);
if (version_number==3)
error("All 32 attributes already declared (compile as Advanced \
game to get an extra 16)");
else
error("All 48 attributes already declared");
panic_mode_error_recovery();
put_token_back();
return;
}
}
else {
if (no_attributes==NUM_ATTR_BYTES*8) {
discard_token_location(beginning_debug_location);
error_fmt(
"All %d attributes already declared -- increase NUM_ATTR_BYTES to use \
more",
NUM_ATTR_BYTES*8);
panic_mode_error_recovery();
put_token_back();
return;
}
}
get_next_token();
i = token_value; name = token_text;
/* We hold onto token_text through the end of this Property directive, which should be okay. */
if (token_type != SYMBOL_TT)
{ discard_token_location(beginning_debug_location);
ebf_curtoken_error("new attribute name");
panic_mode_error_recovery();
put_token_back();
return;
}
if (!(symbols[i].flags & UNKNOWN_SFLAG))
{ discard_token_location(beginning_debug_location);
ebf_symbol_error("new attribute name", token_text, typename(symbols[i].type), symbols[i].line);
panic_mode_error_recovery();
put_token_back();
return;
}
directive_keywords.enabled = TRUE;
get_next_token();
directive_keywords.enabled = FALSE;
if ((token_type == DIR_KEYWORD_TT) && (token_value == ALIAS_DK))
{ get_next_token();
if (!((token_type == SYMBOL_TT)
&& (symbols[token_value].type == ATTRIBUTE_T)))
{ discard_token_location(beginning_debug_location);
ebf_curtoken_error("an existing attribute name after 'alias'");
panic_mode_error_recovery();
put_token_back();
return;
}
assign_symbol(i, symbols[token_value].value, ATTRIBUTE_T);
symbols[token_value].flags |= ALIASED_SFLAG;
symbols[i].flags |= ALIASED_SFLAG;
}
else
{ assign_symbol(i, no_attributes++, ATTRIBUTE_T);
put_token_back();
}
if (debugfile_switch)
{ debug_file_printf("<attribute>");
debug_file_printf("<identifier>%s</identifier>", name);
debug_file_printf("<value>%d</value>", symbols[i].value);
write_debug_locations(get_token_location_end(beginning_debug_location));
debug_file_printf("</attribute>");
}
trace_s(name, symbols[i].value, 0);
return;
}
/* Format:
Property [long] [additive] name
Property [long] [additive] name alias oldname
Property [long] [additive] name defaultvalue
Property [long] individual name
*/
extern void make_property(void)
{ int32 default_value, i;
int keywords, prevkeywords;
char *name;
int namelen;
int additive_flag, indiv_flag;
debug_location_beginning beginning_debug_location =
get_token_location_beginning();
/* The next bit is tricky. We want to accept any number of the keywords
"long", "additive", "individual" before the property name. But we
also want to accept "Property long" -- that's a legitimate
property name.
The solution is to keep track of which keywords we've seen in
a bitmask, and another for one token previous. That way we
can back up one token if there's no name visible. */
keywords = prevkeywords = 0;
do
{ directive_keywords.enabled = TRUE;
get_next_token();
if ((token_type == DIR_KEYWORD_TT) && (token_value == LONG_DK)) {
prevkeywords = keywords;
keywords |= 1;
}
else if ((token_type == DIR_KEYWORD_TT) && (token_value == ADDITIVE_DK)) {
prevkeywords = keywords;
keywords |= 2;
}
else if ((token_type == DIR_KEYWORD_TT) && (token_value == INDIVIDUAL_DK)) {
prevkeywords = keywords;
keywords |= 4;
}
else {
break;
}
} while (TRUE);
/* Re-parse the name with keywords turned off. (This allows us to
accept a property name like "table".) */
put_token_back();
directive_keywords.enabled = FALSE;
get_next_token();
if (token_type != SYMBOL_TT && keywords) {
/* This can't be a name. Try putting back the last keyword. */
keywords = prevkeywords;
put_token_back();
put_token_back();
get_next_token();
}
additive_flag = indiv_flag = FALSE;
if (keywords & 1)
obsolete_warning("all properties are now automatically 'long'");
if (keywords & 2)
additive_flag = TRUE;
if (keywords & 4)
indiv_flag = TRUE;
i = token_value; name = token_text;
/* We hold onto token_text through the end of this Property directive, which should be okay. */
if (token_type != SYMBOL_TT)
{ discard_token_location(beginning_debug_location);
ebf_curtoken_error("new property name");
panic_mode_error_recovery();
put_token_back();
return;
}
if (!(symbols[i].flags & UNKNOWN_SFLAG))
{ discard_token_location(beginning_debug_location);
ebf_symbol_error("new property name", token_text, typename(symbols[i].type), symbols[i].line);
panic_mode_error_recovery();
put_token_back();
return;
}
if (indiv_flag) {
int this_identifier_number;
if (additive_flag)
{ error("'individual' incompatible with 'additive'");
panic_mode_error_recovery();
put_token_back();
return;
}
this_identifier_number = no_individual_properties++;
assign_symbol(i, this_identifier_number, INDIVIDUAL_PROPERTY_T);
if (debugfile_switch) {
debug_file_printf("<property>");
debug_file_printf
("<identifier>%s</identifier>", name);
debug_file_printf
("<value>%d</value>", this_identifier_number);
debug_file_printf("</property>");
}
trace_s(name, symbols[i].value, 2);
return;
}
directive_keywords.enabled = TRUE;
get_next_token();
directive_keywords.enabled = FALSE;
namelen = strlen(name);
if (namelen > 3 && strcmp(name+namelen-3, "_to") == 0) {
/* Direction common properties "n_to", etc are compared in some
libraries. They have STAR_SFLAG to tell us to skip a warning. */
symbols[i].flags |= STAR_SFLAG;
}
/* Now we might have "alias" or a default value (but not both). */
if ((token_type == DIR_KEYWORD_TT) && (token_value == ALIAS_DK))
{ discard_token_location(beginning_debug_location);
if (additive_flag)
{ error("'alias' incompatible with 'additive'");
panic_mode_error_recovery();
put_token_back();
return;
}
get_next_token();
if (!((token_type == SYMBOL_TT)
&& (symbols[token_value].type == PROPERTY_T)))
{ ebf_curtoken_error("an existing property name after 'alias'");
panic_mode_error_recovery();
put_token_back();
return;
}
assign_symbol(i, symbols[token_value].value, PROPERTY_T);
trace_s(name, symbols[i].value, 1);
symbols[token_value].flags |= ALIASED_SFLAG;
symbols[i].flags |= ALIASED_SFLAG;
return;
}
/* We now know we're allocating a new common property. Make sure
there's room. */
if (!glulx_mode) {
if (no_properties==((version_number==3)?32:64))
{ discard_token_location(beginning_debug_location);
/* The maximum listed here includes "name" but not the
unused zero value or the two hidden properties (class
inheritance and indiv table). */
if (version_number==3)
error("All 29 properties already declared (compile as \
Advanced game to get 32 more)");
else
error("All 61 properties already declared");
panic_mode_error_recovery();
put_token_back();
return;
}
}
else {
if (no_properties==INDIV_PROP_START) {
discard_token_location(beginning_debug_location);
error_fmt(
"All %d properties already declared (increase INDIV_PROP_START to get more)",
INDIV_PROP_START-3);
panic_mode_error_recovery();
put_token_back();
return;
}
}
default_value = 0;
put_token_back();
if (!((token_type == SEP_TT) && (token_value == SEMICOLON_SEP)))
{
assembly_operand AO = parse_expression(CONSTANT_CONTEXT);
default_value = AO.value;
if (AO.marker != 0)
backpatch_zmachine(AO.marker, PROP_DEFAULTS_ZA,
(no_properties-1) * WORDSIZE);
}
commonprops[no_properties].default_value = default_value;
commonprops[no_properties].is_long = TRUE;
commonprops[no_properties].is_additive = additive_flag;
assign_symbol(i, no_properties++, PROPERTY_T);
if (debugfile_switch)
{ debug_file_printf("<property>");
debug_file_printf("<identifier>%s</identifier>", name);
debug_file_printf("<value>%d</value>", symbols[i].value);
write_debug_locations
(get_token_location_end(beginning_debug_location));
debug_file_printf("</property>");
}
trace_s(name, symbols[i].value, 1);
}
/* ------------------------------------------------------------------------- */
/* Properties. */
/* ------------------------------------------------------------------------- */
commonpropinfo *commonprops; /* Info about common properties
(fixed allocation of
INDIV_PROP_START entries) */
uchar *properties_table; /* Holds the table of property values
(holding one block for each object
and coming immediately after the
object tree in Z-memory) */
memory_list properties_table_memlist;
int properties_table_size; /* Number of bytes in this table */
/* ------------------------------------------------------------------------- */
/* Individual properties */
/* */
/* Each new i.p. name is given a unique number. These numbers start from */
/* 72, since 0 is reserved as a null, 1 to 63 refer to common properties */
/* and 64 to 71 are kept for methods of the metaclass Class (for example, */
/* 64 is "create"). */
/* */
/* An object provides individual properties by having property 3 set to a */
/* non-zero value, which must be a byte address of a table in the form: */
/* */
/* <record-1> ... <record-n> 00 00 */
/* */
/* where a <record> looks like */
/* */
/* <identifier> <size> <up to 255 bytes of data> */
/* or <identifier + 0x8000> */
/* ----- 2 bytes ---------- 1 byte <size> number of bytes */
/* */
/* The <identifier> part is the number allocated to the name of what is */
/* being provided. The top bit of this word is set to indicate that */
/* although the individual property is being provided, it is provided */
/* only privately (so that it is inaccessible except to the object's own */
/* embedded routines). */
/* */
/* In Glulx: i-props are numbered from INDIV_PROP_START+8 up. And all */
/* properties, common and individual, are stored in the same table. */
/* ------------------------------------------------------------------------- */
int no_individual_properties; /* Actually equal to the next
identifier number to be allocated,
so this is initially 72 even though
none have been made yet. */
static int individual_prop_table_size; /* Size of the table of individual
properties so far for current obj */
uchar *individuals_table; /* Table of records, each being the
i.p. table for an object */
memory_list individuals_table_memlist;
int i_m; /* Write mark position in the above */
int individuals_length; /* Extent of individuals_table */
/* ------------------------------------------------------------------------- */
/* Arrays used by this file */
/* ------------------------------------------------------------------------- */
objecttz *objectsz; /* Allocated to no_objects; Z-code only */
memory_list objectsz_memlist;
objecttg *objectsg; /* Allocated to no_objects; Glulx only */
static memory_list objectsg_memlist;
uchar *objectatts; /* Allocated to no_objects; Glulx only */
static memory_list objectatts_memlist;
static int *classes_to_inherit_from; /* Allocated to no_classes_to_inherit_from */
static memory_list classes_to_inherit_from_memlist;
classinfo *class_info; /* Allocated up to no_classes */
memory_list class_info_memlist;
/* ------------------------------------------------------------------------- */
/* Tracing for compiler maintenance */
/* ------------------------------------------------------------------------- */
extern void list_object_tree(void)
{ int i;
printf("Object tree:\n");
printf("obj name par nxt chl:\n");
for (i=0; i<no_objects; i++) {
if (!glulx_mode) {
int sym = objectsz[i].symbol;
char *symname = ((sym > 0) ? symbols[sym].name : "...");
printf("%3d %-32s %3d %3d %3d\n",
i+1, symname,
objectsz[i].parent, objectsz[i].next, objectsz[i].child);
}
else {
int sym = objectsg[i].symbol;
char *symname = ((sym > 0) ? symbols[sym].name : "...");
printf("%3d %-32s %3d %3d %3d\n",
i+1, symname,
objectsg[i].parent, objectsg[i].next, objectsg[i].child);
}
}
}
/* ------------------------------------------------------------------------- */
/* Object and class manufacture begins here. */
/* */
/* These definitions have headers (parsed far, far below) and a series */
/* of segments, introduced by keywords and optionally separated by commas. */
/* Each segment has its own parsing routine. Note that when errors are */
/* detected, parsing continues rather than being abandoned, which assists */
/* a little in "error recovery" (i.e. in stopping lots more errors being */
/* produced for essentially the same mistake). */
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
/* [1] The object-maker: builds an object from a specification, viz.: */
/* */
/* full_object, */
/* shortname_buffer, */
/* parent_of_this_obj, */
/* current_defn_is_class (flag) */
/* classes_to_inherit_from[], no_classes_to_inherit_from, */
/* individual_prop_table_size (to date ) */
/* */
/* For efficiency's sake, the individual properties table has already been */
/* created (as far as possible, i.e., all except for inherited individual */
/* properties); unless the flag is clear, in which case the actual */
/* definition did not specify any individual properties. */
/* ========================================================================= */
/* Property inheritance from classes. */
/* ------------------------------------------------------------------------- */
static void property_inheritance_z(void)
{
/* Apply the property inheritance rules to full_object, which should
initially be complete (i.e., this routine takes place after the whole
Nearby/Object/Class definition has been parsed through).
On exit, full_object contains the final state of the properties to
be written. */
int i, j, k, kmax, class, mark,
prop_number, prop_length, prop_in_current_defn;
uchar *class_prop_block;
ASSERT_ZCODE();
for (class=0; class<no_classes_to_inherit_from; class++)
{
j=0;
mark = class_info[classes_to_inherit_from[class] - 1].begins_at;
class_prop_block = (properties_table + mark);
while (class_prop_block[j]!=0)
{ if (version_number == 3)
{ prop_number = class_prop_block[j]%32;
prop_length = 1 + class_prop_block[j++]/32;
}
else
{ prop_number = class_prop_block[j]%64;
prop_length = 1 + class_prop_block[j++]/64;
if (prop_length > 2)
prop_length = class_prop_block[j++]%64;
}
/* So we now have property number prop_number present in the
property block for the class being read: its bytes are
class_prop_block[j, ..., j + prop_length - 1]
Question now is: is there already a value given in the
current definition under this property name? */
prop_in_current_defn = FALSE;
kmax = full_object.l;
if (kmax > 64)
fatalerror("More than 64 property entries in an object");
for (k=0; k<kmax; k++)
if (full_object.pp[k].num == prop_number)
{ prop_in_current_defn = TRUE;
/* (Note that the built-in "name" property is additive) */
if ((prop_number==1) || (commonprops[prop_number].is_additive))
{
/* The additive case: we accumulate the class
property values onto the end of the full_object
property */
for (i=full_object.pp[k].l;
i<full_object.pp[k].l+prop_length/2; i++)
{
if (i >= 32)
{ error("An additive property has inherited \
so many values that the list has overflowed the maximum 32 entries");
break;
}
if ((version_number==3) && i >= 4)
{ error("An additive property has inherited \
so many values that the list has overflowed the maximum 4 entries");
break;
}
INITAOTV(&full_object.pp[k].ao[i], LONG_CONSTANT_OT, mark + j);
j += 2;
full_object.pp[k].ao[i].marker = INHERIT_MV;
}
full_object.pp[k].l += prop_length/2;
}
else
/* The ordinary case: the full_object property
values simply overrides the class definition,
so we skip over the values in the class table */
j+=prop_length;
if (prop_number==3)
{ int y, z, class_block_offset;
/* Property 3 holds the address of the table of
instance variables, so this is the case where
the object already has instance variables in its
own table but must inherit some more from the
class */
class_block_offset = class_prop_block[j-2]*256
+ class_prop_block[j-1];
z = class_block_offset;
while ((individuals_table[z]!=0)||(individuals_table[z+1]!=0))
{ int already_present = FALSE, l;
for (l = full_object.pp[k].ao[0].value; l < i_m;
l = l + 3 + individuals_table[l + 2])
if (individuals_table[l] == individuals_table[z]
&& individuals_table[l + 1] == individuals_table[z+1])
{ already_present = TRUE; break;
}
if (already_present == FALSE)
{
ensure_memory_list_available(&individuals_table_memlist, i_m+3+individuals_table[z+2]);
individuals_table[i_m++] = individuals_table[z];
individuals_table[i_m++] = individuals_table[z+1];
individuals_table[i_m++] = individuals_table[z+2];
for (y=0;y < individuals_table[z+2]/2;y++)
{ individuals_table[i_m++] = (z+3+y*2)/256;
individuals_table[i_m++] = (z+3+y*2)%256;
backpatch_zmachine(INHERIT_INDIV_MV,
INDIVIDUAL_PROP_ZA, i_m-2);
}
}
z += individuals_table[z+2] + 3;
}
individuals_length = i_m;
}
/* For efficiency we exit the loop now (this property
number has been dealt with) */
break;
}
if (!prop_in_current_defn)
{
/* The case where the class defined a property which wasn't
defined at all in full_object: we copy out the data into
a new property added to full_object */
k=full_object.l++;
if (k >= 64)
fatalerror("More than 64 property entries in an object");
full_object.pp[k].num = prop_number;
full_object.pp[k].l = prop_length/2;
for (i=0; i<prop_length/2; i++)
{
INITAOTV(&full_object.pp[k].ao[i], LONG_CONSTANT_OT, mark + j);
j+=2;
full_object.pp[k].ao[i].marker = INHERIT_MV;
}
if (prop_number==3)
{ int y, z, class_block_offset;
/* Property 3 holds the address of the table of
instance variables, so this is the case where
the object had no instance variables of its own
but must inherit some more from the class */
if (individual_prop_table_size++ == 0)
{ full_object.pp[k].num = 3;
full_object.pp[k].l = 1;
INITAOTV(&full_object.pp[k].ao[0], LONG_CONSTANT_OT, individuals_length);
full_object.pp[k].ao[0].marker = INDIVPT_MV;
i_m = individuals_length;
}
class_block_offset = class_prop_block[j-2]*256
+ class_prop_block[j-1];
z = class_block_offset;
while ((individuals_table[z]!=0)||(individuals_table[z+1]!=0))
{
ensure_memory_list_available(&individuals_table_memlist, i_m+3+individuals_table[z+2]);
individuals_table[i_m++] = individuals_table[z];
individuals_table[i_m++] = individuals_table[z+1];
individuals_table[i_m++] = individuals_table[z+2];
for (y=0;y < individuals_table[z+2]/2;y++)
{ individuals_table[i_m++] = (z+3+y*2)/256;
individuals_table[i_m++] = (z+3+y*2)%256;
backpatch_zmachine(INHERIT_INDIV_MV,
INDIVIDUAL_PROP_ZA, i_m-2);
}
z += individuals_table[z+2] + 3;
}
individuals_length = i_m;
}
}
}
}
if (individual_prop_table_size > 0)
{
ensure_memory_list_available(&individuals_table_memlist, i_m+2);
individuals_table[i_m++] = 0;
individuals_table[i_m++] = 0;
individuals_length += 2;
}
}
static void property_inheritance_g(void)
{
/* Apply the property inheritance rules to full_object, which should
initially be complete (i.e., this routine takes place after the whole
Nearby/Object/Class definition has been parsed through).
On exit, full_object contains the final state of the properties to
be written. */
int i, j, k, class, num_props,
prop_number, prop_length, prop_flags, prop_in_current_defn;
int32 mark, prop_addr;
uchar *cpb, *pe;
ASSERT_GLULX();
for (class=0; class<no_classes_to_inherit_from; class++) {
mark = class_info[classes_to_inherit_from[class] - 1].begins_at;
cpb = (properties_table + mark);
/* This now points to the compiled property-table for the class.
We'll have to go through and decompile it. (For our sins.) */
num_props = ReadInt32(cpb);
for (j=0; j<num_props; j++) {
pe = cpb + 4 + j*10;
prop_number = ReadInt16(pe);
pe += 2;
prop_length = ReadInt16(pe);
pe += 2;
prop_addr = ReadInt32(pe);
pe += 4;
prop_flags = ReadInt16(pe);
pe += 2;
/* So we now have property number prop_number present in the
property block for the class being read. Its bytes are
cpb[prop_addr ... prop_addr + prop_length - 1]
Question now is: is there already a value given in the
current definition under this property name? */
prop_in_current_defn = FALSE;
for (k=0; k<full_object_g.numprops; k++) {
if (full_object_g.props[k].num == prop_number) {
prop_in_current_defn = TRUE;
break;
}
}
if (prop_in_current_defn) {
if ((prop_number==1)
|| (prop_number < INDIV_PROP_START
&& commonprops[prop_number].is_additive)) {
/* The additive case: we accumulate the class
property values onto the end of the full_object
properties. Remember that k is still the index number
of the first prop-block matching our property number. */
int prevcont;
if (full_object_g.props[k].continuation == 0) {
full_object_g.props[k].continuation = 1;
prevcont = 1;
}
else {
prevcont = full_object_g.props[k].continuation;
for (k++; k<full_object_g.numprops; k++) {
if (full_object_g.props[k].num == prop_number) {
prevcont = full_object_g.props[k].continuation;
}
}
}
k = full_object_g.numprops++;
ensure_memory_list_available(&full_object_g.props_memlist, k+1);
full_object_g.props[k].num = prop_number;
full_object_g.props[k].flags = 0;
full_object_g.props[k].datastart = full_object_g.propdatasize;
full_object_g.props[k].continuation = prevcont+1;
full_object_g.props[k].datalen = prop_length;
ensure_memory_list_available(&full_object_g.propdata_memlist, full_object_g.propdatasize + prop_length);
for (i=0; i<prop_length; i++) {
int ppos = full_object_g.propdatasize++;
INITAOTV(&full_object_g.propdata[ppos], CONSTANT_OT, prop_addr + 4*i);
full_object_g.propdata[ppos].marker = INHERIT_MV;
}
}
else {
/* The ordinary case: the full_object_g property
values simply overrides the class definition,
so we skip over the values in the class table. */
}
}
else {
/* The case where the class defined a property which wasn't
defined at all in full_object_g: we copy out the data into
a new property added to full_object_g. */
k = full_object_g.numprops++;
ensure_memory_list_available(&full_object_g.props_memlist, k+1);
full_object_g.props[k].num = prop_number;
full_object_g.props[k].flags = prop_flags;
full_object_g.props[k].datastart = full_object_g.propdatasize;
full_object_g.props[k].continuation = 0;
full_object_g.props[k].datalen = prop_length;
ensure_memory_list_available(&full_object_g.propdata_memlist, full_object_g.propdatasize + prop_length);
for (i=0; i<prop_length; i++) {
int ppos = full_object_g.propdatasize++;
INITAOTV(&full_object_g.propdata[ppos], CONSTANT_OT, prop_addr + 4*i);
full_object_g.propdata[ppos].marker = INHERIT_MV;
}
}
}
}
}
/* ------------------------------------------------------------------------- */
/* Construction of Z-machine-format property blocks. */
/* ------------------------------------------------------------------------- */
static int write_properties_between(int mark, int from, int to)
{ int j, k, prop_number;
for (prop_number=to; prop_number>=from; prop_number--)
{ for (j=0; j<full_object.l; j++)
{ if ((full_object.pp[j].num == prop_number)
&& (full_object.pp[j].l != 100))
{
int prop_length = 2*full_object.pp[j].l;
ensure_memory_list_available(&properties_table_memlist, mark+2+prop_length);
if (version_number == 3)
properties_table[mark++] = prop_number + (prop_length - 1)*32;
else
{ switch(prop_length)
{ case 1:
properties_table[mark++] = prop_number; break;
case 2:
properties_table[mark++] = prop_number + 0x40; break;
default:
properties_table[mark++] = prop_number + 0x80;
properties_table[mark++] = prop_length + 0x80; break;
}
}
for (k=0; k<full_object.pp[j].l; k++)
{
if (k >= 32) {
/* We catch this earlier, but we'll check again to avoid overflowing ao[] */
error("Too many values for Z-machine property");
break;
}
if (full_object.pp[j].ao[k].marker != 0)
backpatch_zmachine(full_object.pp[j].ao[k].marker,
PROP_ZA, mark);
properties_table[mark++] = full_object.pp[j].ao[k].value/256;
properties_table[mark++] = full_object.pp[j].ao[k].value%256;
}
}
}
}
ensure_memory_list_available(&properties_table_memlist, mark+1);
properties_table[mark++]=0;
return(mark);
}
static int write_property_block_z(char *shortname)
{
/* Compile the (now complete) full_object properties into a
property-table block at "p" in Inform's memory.
"shortname" is the object's short name, if specified; otherwise
NULL.
Return the number of bytes written to the block. */
int32 mark = properties_table_size, i;
/* printf("Object at %04x\n", mark); */
if (shortname != NULL)
{
/* The limit of 510 bytes, or 765 Z-characters, is a Z-spec limit. */
i = translate_text(510,shortname,STRCTX_OBJNAME);
if (i < 0) {
error ("Short name of object exceeded 765 Z-characters");
i = 0;
}
ensure_memory_list_available(&properties_table_memlist, mark+1+i);
memcpy(properties_table + mark+1, translated_text, i);
properties_table[mark] = i/2;
mark += i+1;
}
if (current_defn_is_class)
{ mark = write_properties_between(mark,3,3);
ensure_memory_list_available(&properties_table_memlist, mark+6);
for (i=0;i<6;i++)
properties_table[mark++] = full_object.atts[i];
ensure_memory_list_available(&class_info_memlist, no_classes+1);
class_info[no_classes++].begins_at = mark;
}
mark = write_properties_between(mark, 1, (version_number==3)?31:63);
i = mark - properties_table_size;
properties_table_size = mark;
return(i);
}
static int gpropsort(void *ptr1, void *ptr2)
{
propg *prop1 = ptr1;
propg *prop2 = ptr2;
if (prop2->num == -1)
return -1;
if (prop1->num == -1)
return 1;
if (prop1->num < prop2->num)
return -1;
if (prop1->num > prop2->num)
return 1;
return (prop1->continuation - prop2->continuation);
}
static int32 write_property_block_g(void)
{
/* Compile the (now complete) full_object properties into a
property-table block at "p" in Inform's memory.
Return the number of bytes written to the block.
In Glulx, the shortname property isn't used here; it's already
been compiled into an ordinary string. */
int32 i;
int ix, jx, kx, totalprops;
int32 mark = properties_table_size;
int32 datamark;
if (current_defn_is_class) {
ensure_memory_list_available(&properties_table_memlist, mark+NUM_ATTR_BYTES);
for (i=0;i<NUM_ATTR_BYTES;i++)
properties_table[mark++] = full_object_g.atts[i];
ensure_memory_list_available(&class_info_memlist, no_classes+1);
class_info[no_classes++].begins_at = mark;
}
qsort(full_object_g.props, full_object_g.numprops, sizeof(propg),
(int (*)(const void *, const void *))(&gpropsort));
full_object_g.finalpropaddr = mark;
totalprops = 0;
for (ix=0; ix<full_object_g.numprops; ix=jx) {
int propnum = full_object_g.props[ix].num;
if (propnum == -1)
break;
for (jx=ix;
jx<full_object_g.numprops && full_object_g.props[jx].num == propnum;
jx++);
totalprops++;
}
/* Write out the number of properties in this table. */
ensure_memory_list_available(&properties_table_memlist, mark+4);
WriteInt32(properties_table+mark, totalprops);
mark += 4;
datamark = mark + 10*totalprops;
for (ix=0; ix<full_object_g.numprops; ix=jx) {
int propnum = full_object_g.props[ix].num;
int flags = full_object_g.props[ix].flags;
int totallen = 0;
int32 datamarkstart = datamark;
if (propnum == -1)
break;
for (jx=ix;
jx<full_object_g.numprops && full_object_g.props[jx].num == propnum;
jx++) {
int32 datastart = full_object_g.props[jx].datastart;
ensure_memory_list_available(&properties_table_memlist, datamark+4*full_object_g.props[jx].datalen);
for (kx=0; kx<full_object_g.props[jx].datalen; kx++) {
int32 val = full_object_g.propdata[datastart+kx].value;
WriteInt32(properties_table+datamark, val);
if (full_object_g.propdata[datastart+kx].marker != 0)
backpatch_zmachine(full_object_g.propdata[datastart+kx].marker,
PROP_ZA, datamark);