-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathverbs.c
1689 lines (1468 loc) · 61.3 KB
/
verbs.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
/* ------------------------------------------------------------------------- */
/* "verbs" : Manages actions and grammar tables; parses the directives */
/* Verb and Extend. */
/* */
/* Part of Inform 6.43 */
/* copyright (c) Graham Nelson 1993 - 2024 */
/* */
/* ------------------------------------------------------------------------- */
#include "header.h"
/* ------------------------------------------------------------------------- */
/* Grammar version. */
/* ------------------------------------------------------------------------- */
/* The grammar version is handled in a somewhat messy way. It can be:
1 for pre-Inform 6.06 table format
2 for modern Inform format
The default is 1 for Z-code (for backwards compatibility), 2 for Glulx.
This can be altered by the $GRAMMAR_VERSION compiler setting, and
then altered again during compilation by a "Constant Grammar__Version"
directive. (Note double underscore.)
Typically the library has a "Constant Grammar__Version 2;" line to
ensure we get the modern version for both VMs.
(Note also the $GRAMMAR_META_FLAG setting, which lets us indicate
which actions are meta, rather than relying on dict word flags.)
*/
int grammar_version_number;
int32 grammar_version_symbol; /* Index of "Grammar__Version"
within symbols table */
/* ------------------------------------------------------------------------- */
/* Actions. */
/* ------------------------------------------------------------------------- */
/* Array defined below: */
/* */
/* actioninfo actions[n] Symbol table index and byte offset */
/* of the ...Sub routine */
/* */
/* If GRAMMAR_META_FLAG is set, we need to reorder actions[] to put meta */
/* actions at the top. We don't try to sort the table in place, though. */
/* We just create this two-way index remapping table: */
/* */
/* actionsort sorted_actions[] Table mapping internal action */
/* indexes to final action indexes */
/* ------------------------------------------------------------------------- */
int no_actions, /* Number of actions made so far */
no_fake_actions; /* Number of fake actions made so far */
/* ------------------------------------------------------------------------- */
/* Adjectives. (The term "adjective" is traditional; they are mainly */
/* prepositions, such as "onto".) */
/* ------------------------------------------------------------------------- */
/* Arrays defined below: */
/* */
/* int32 adjectives[n] Byte address of dictionary entry */
/* for the nth adjective */
/* dict_word adjective_sort_code[n] Dictionary sort code of nth adj */
/* ------------------------------------------------------------------------- */
int no_adjectives; /* Number of adjectives made so far */
/* ------------------------------------------------------------------------- */
/* Verbs. Note that Inform-verbs are not quite the same as English verbs: */
/* for example the English verbs "take" and "drop" both normally */
/* correspond in a game's dictionary to the same Inform verb. An */
/* Inform verb (I-verb) is essentially a list of grammar lines. */
/* An English verb (E-verb, although of course it might not be */
/* English!) is a dict word which is known to be a verb. */
/* Each E-verb's #dict_par2 field contains the I-verb index that */
/* it corresponds to. */
/* ------------------------------------------------------------------------- */
/* Arrays defined below: */
/* */
/* verbt Inform_verbs[n] The n-th grammar line sequence: */
/* see "header.h" for the definition */
/* of the typedef struct verbt */
/* int32 grammar_token_routine[n] The byte offset from start of code */
/* area of the n-th one */
/* ------------------------------------------------------------------------- */
int no_Inform_verbs, /* Number of Inform-verbs made so far */
no_grammar_token_routines; /* Number of routines given in tokens */
/* ------------------------------------------------------------------------- */
/* We keep a list of English verb-words known (e.g. "take" or "eat") and */
/* which Inform-verbs they correspond to. (This list is needed for some */
/* of the grammar extension operations.) */
/* ------------------------------------------------------------------------- */
typedef struct English_verb_s {
int textpos; /* in English_verbs_text */
int dictword; /* dict word accession num */
int verbnum; /* in Inform_verbs */
} English_verb_t;
static English_verb_t *English_verbs;
static memory_list English_verbs_memlist;
static int English_verbs_count;
static char *English_verbs_text; /* Allocated to English_verbs_text_size */
static memory_list English_verbs_text_memlist;
static int English_verbs_text_size;
/* ------------------------------------------------------------------------- */
/* Arrays used by this file */
/* ------------------------------------------------------------------------- */
verbt *Inform_verbs; /* Allocated up to no_Inform_verbs */
static memory_list Inform_verbs_memlist;
uchar *grammar_lines; /* Allocated to grammar_lines_top */
static memory_list grammar_lines_memlist;
int32 grammar_lines_top;
int no_grammar_lines, no_grammar_tokens;
actioninfo *actions; /* Allocated to no_actions */
memory_list actions_memlist;
int32 *grammar_token_routine; /* Allocated to no_grammar_token_routines */
static memory_list grammar_token_routine_memlist;
actionsort *sorted_actions; /* only used if GRAMMAR_META_FLAG */
int no_meta_actions; /* only used if GRAMMAR_META_FLAG */
int32 *adjectives; /* Allocated to no_adjectives */
static memory_list adjectives_memlist;
static uchar *adjective_sort_code; /* Allocated to no_adjectives*DICT_WORD_BYTES, except it's sometimes no_adjectives+1 because we can bump it tentatively */
static memory_list adjective_sort_code_memlist;
static memory_list action_symname_memlist; /* Used for temporary symbols */
/* ------------------------------------------------------------------------- */
/* Grammar version */
/* ------------------------------------------------------------------------- */
/* Set grammar_version_number, or report an error if the number is not
valid for the current VM. */
void set_grammar_version(int val)
{
if (!glulx_mode) {
if (val != 1 && val != 2 && val != 3) {
error("Z-code only supports grammar version 1, 2, or 3.");
return;
}
}
else {
if (val != 2) {
error("Glulx only supports grammar version 2.");
return;
}
}
grammar_version_number = val;
/* We also have to adjust the symbol value. */
symbols[grammar_version_symbol].value = val;
}
/* ------------------------------------------------------------------------- */
/* Tracing for compiler maintenance */
/* ------------------------------------------------------------------------- */
static void print_verbs_by_number(int num);
static void list_grammar_line_v1(int mark)
{
int action, actsym;
int ix, len;
char *str;
/* There is no GV1 for Glulx. */
if (glulx_mode)
return;
action = (grammar_lines[mark] << 8) | (grammar_lines[mark+1]);
mark += 2;
printf(" *");
while (grammar_lines[mark] != 15) {
uchar tok = grammar_lines[mark];
mark += 3;
switch (tok) {
case 0:
printf(" noun");
break;
case 1:
printf(" held");
break;
case 2:
printf(" multi");
break;
case 3:
printf(" multiheld");
break;
case 4:
printf(" multiexcept");
break;
case 5:
printf(" multiinside");
break;
case 6:
printf(" creature");
break;
case 7:
printf(" special");
break;
case 8:
printf(" number");
break;
default:
if (tok >= 16 && tok < 48) {
printf(" noun=%d", tok-16);
}
else if (tok >= 48 && tok < 80) {
printf(" routine=%d", tok-48);
}
else if (tok >= 80 && tok < 128) {
printf(" scope=%d", tok-80);
}
else if (tok >= 128 && tok < 160) {
printf(" attr=%d", tok-128);
}
else if (tok >= 160) {
printf(" prep=%d", tok);
}
else {
printf(" ???");
}
}
}
printf(" -> ");
actsym = actions[action].symbol;
str = (symbols[actsym].name);
len = strlen(str) - 3; /* remove "__A" */
for (ix=0; ix<len; ix++) putchar(str[ix]);
if (actions[action].meta) printf(" (meta)");
printf("\n");
}
static void list_grammar_line_v2(int mark)
{
int action, flags, actsym;
int ix, len;
char *str;
if (!glulx_mode) {
action = (grammar_lines[mark] << 8) | (grammar_lines[mark+1]);
flags = (action & 0x400);
action &= 0x3FF;
mark += 2;
}
else {
action = (grammar_lines[mark] << 8) | (grammar_lines[mark+1]);
mark += 2;
flags = grammar_lines[mark++];
}
printf(" *");
while (grammar_lines[mark] != 15) {
int toktype, tokdat, tokalt;
if (!glulx_mode) {
toktype = grammar_lines[mark] & 0x0F;
tokalt = (grammar_lines[mark] >> 4) & 0x03;
mark += 1;
tokdat = (grammar_lines[mark] << 8) | (grammar_lines[mark+1]);
mark += 2;
}
else {
toktype = grammar_lines[mark] & 0x0F;
tokalt = (grammar_lines[mark] >> 4) & 0x03;
mark += 1;
tokdat = (grammar_lines[mark] << 24) | (grammar_lines[mark+1] << 16) | (grammar_lines[mark+2] << 8) | (grammar_lines[mark+3]);
mark += 4;
}
if (tokalt == 3 || tokalt == 1)
printf(" /");
switch (toktype) {
case 1:
switch (tokdat) {
case 0: printf(" noun"); break;
case 1: printf(" held"); break;
case 2: printf(" multi"); break;
case 3: printf(" multiheld"); break;
case 4: printf(" multiexcept"); break;
case 5: printf(" multiinside"); break;
case 6: printf(" creature"); break;
case 7: printf(" special"); break;
case 8: printf(" number"); break;
case 9: printf(" topic"); break;
default: printf(" ???"); break;
}
break;
case 2:
printf(" '");
print_dict_word(tokdat);
printf("'");
break;
case 3:
printf(" noun=%d", tokdat);
break;
case 4:
printf(" attr=%d", tokdat);
break;
case 5:
printf(" scope=%d", tokdat);
break;
case 6:
printf(" routine=%d", tokdat);
break;
default:
printf(" ???%d:%d", toktype, tokdat);
break;
}
}
printf(" -> ");
actsym = actions[action].symbol;
str = (symbols[actsym].name);
len = strlen(str) - 3; /* remove "__A" */
for (ix=0; ix<len; ix++) putchar(str[ix]);
if (actions[action].meta) printf(" (meta)");
if (flags) printf(" (reversed)");
printf("\n");
}
static void list_grammar_line_v3(int mark)
{
int action, flags, actsym, tokcount;
int ix, len, tx;
char *str;
/* There is no GV3 for Glulx. */
if (glulx_mode)
return;
action = (grammar_lines[mark] << 8) | (grammar_lines[mark+1]);
flags = (action & 0x400);
tokcount = (action >> 11) & 0x1F;
action &= 0x3FF;
mark += 2;
printf(" *");
for (tx=0; tx<tokcount; tx++) {
int toktype, tokdat, tokalt;
toktype = grammar_lines[mark] & 0x0F;
tokalt = (grammar_lines[mark] >> 4) & 0x03;
mark += 1;
tokdat = grammar_lines[mark];
mark += 1;
if (tokalt == 3 || tokalt == 1)
printf(" /");
switch (toktype) {
case 1:
switch (tokdat) {
case 0: printf(" noun"); break;
case 1: printf(" held"); break;
case 2: printf(" multi"); break;
case 3: printf(" multiheld"); break;
case 4: printf(" multiexcept"); break;
case 5: printf(" multiinside"); break;
case 6: printf(" creature"); break;
case 7: printf(" special"); break;
case 8: printf(" number"); break;
case 9: printf(" topic"); break;
default: printf(" ???"); break;
}
break;
case 2:
printf(" '");
print_dict_word(adjectives[tokdat]);
printf("'");
break;
case 3:
printf(" noun=%d", tokdat);
break;
case 4:
printf(" attr=%d", tokdat);
break;
case 5:
printf(" scope=%d", tokdat);
break;
case 6:
printf(" routine=%d", tokdat);
break;
default:
printf(" ???%d:%d", toktype, tokdat);
break;
}
}
printf(" -> ");
actsym = actions[action].symbol;
str = (symbols[actsym].name);
len = strlen(str) - 3; /* remove "__A" */
for (ix=0; ix<len; ix++) putchar(str[ix]);
if (actions[action].meta) printf(" (meta)");
if (flags) printf(" (reversed)");
printf("\n");
}
extern void list_verb_table(void)
{
int verb, lx;
printf("Grammar table: %d verbs\n", no_Inform_verbs);
for (verb=0; verb<no_Inform_verbs; verb++) {
printf("Verb");
print_verbs_by_number(verb);
printf("\n");
for (lx=0; lx<Inform_verbs[verb].lines; lx++) {
int mark = Inform_verbs[verb].l[lx];
switch (grammar_version_number) {
case 1:
list_grammar_line_v1(mark);
break;
case 2:
list_grammar_line_v2(mark);
break;
case 3:
list_grammar_line_v3(mark);
break;
}
}
}
}
extern void list_action_table(void)
{
int ix;
printf("Action table: %d actions, %d fake actions\n", no_actions, no_fake_actions);
for (ix=0; ix<no_actions; ix++) {
int internal = ix;
if (sorted_actions)
internal = sorted_actions[ix].external_to_int;
printf("%d: %s", ix, symbols[actions[internal].symbol].name);
if (actions[internal].meta)
printf(" (meta)");
if (sorted_actions)
printf(" (originally numbered %d)", internal);
printf("\n");
}
/* Fake action names don't get recorded anywhere, so we can't list
them. */
}
/* ------------------------------------------------------------------------- */
/* Actions. */
/* ------------------------------------------------------------------------- */
static void new_action(char *b, int c)
{
/* Called whenever a new action (or fake action) is created (either
by using make_action above, or the Fake_Action directive).
At present just a hook for some tracing code. */
if (printactions_switch > 1)
printf("%s: Action '%s' is numbered %d\n", current_location_text(), b, c);
}
/* Note that fake actions are numbered from a high base point upwards;
real actions are numbered from 0 upward in GV2/3. */
extern int lowest_fake_action(void)
{
if (grammar_version_number == 1)
return 256;
else if (grammar_version_number == 2)
return 4096;
else if (grammar_version_number == 3)
return 4096;
compiler_error("invalid grammar version");
return 0;
}
extern void make_fake_action(void)
{ char *action_sub;
int i;
debug_location_beginning beginning_debug_location =
get_token_location_beginning();
get_next_token();
if (token_type != SYMBOL_TT)
{ discard_token_location(beginning_debug_location);
ebf_curtoken_error("new fake action name");
panic_mode_error_recovery(); return;
}
/* Enough space for "token__A". */
ensure_memory_list_available(&action_symname_memlist, strlen(token_text)+4);
action_sub = action_symname_memlist.data;
strcpy(action_sub, token_text);
strcat(action_sub, "__A");
/* Action symbols (including fake_actions) may collide with other kinds of symbols. So we don't check that. */
i = symbol_index(action_sub, -1, NULL);
if (!(symbols[i].flags & UNKNOWN_SFLAG))
{ discard_token_location(beginning_debug_location);
/* The user didn't know they were defining FOO__A, but they were and it's a problem. */
ebf_symbol_error("new fake action name", action_sub, typename(symbols[i].type), symbols[i].line);
panic_mode_error_recovery(); return;
}
assign_symbol(i, lowest_fake_action()+no_fake_actions++,
FAKE_ACTION_T);
new_action(token_text, i);
if (debugfile_switch)
{ debug_file_printf("<fake-action>");
debug_file_printf("<identifier>##%s</identifier>", token_text);
debug_file_printf("<value>%d</value>", symbols[i].value);
get_next_token();
write_debug_locations
(get_token_location_end(beginning_debug_location));
put_token_back();
debug_file_printf("</fake-action>");
}
return;
}
extern assembly_operand action_of_name(char *name)
{
/* Returns the action number of the given name, creating it as a new
action name if it isn't already known as such. */
char *action_sub;
int j;
assembly_operand AO;
/* Enough space for "name__A". */
ensure_memory_list_available(&action_symname_memlist, strlen(name)+4);
action_sub = action_symname_memlist.data;
strcpy(action_sub, name);
strcat(action_sub, "__A");
j = symbol_index(action_sub, -1, NULL);
if (symbols[j].type == FAKE_ACTION_T)
{ INITAO(&AO);
AO.value = symbols[j].value;
if (!glulx_mode)
AO.type = LONG_CONSTANT_OT;
else
set_constant_ot(&AO);
symbols[j].flags |= USED_SFLAG;
return AO;
}
if (symbols[j].flags & UNKNOWN_SFLAG)
{
if (no_actions >= lowest_fake_action()) {
if (grammar_version_number == 1) {
error_named("Cannot create action (grammar version 1 is limited to 256):", name);
}
else {
/* Note that we'll never reach this limit in Z-code (see below), but it still applies in Glulx because we run into the fake action values. */
error_named("Cannot create action (grammar is limited to 4096):", name);
}
INITAO(&AO);
return AO;
}
if (!glulx_mode && no_actions >= 1024) {
/* Z grammar tokens store the action number in a 10-bit field, so we have this additional limit. */
error_named("Cannot create action (Z-machine grammar is limited to 1024):", name);
INITAO(&AO);
return AO;
}
ensure_memory_list_available(&actions_memlist, no_actions+1);
new_action(name, no_actions);
actions[no_actions].symbol = j;
actions[no_actions].meta = FALSE;
actions[no_actions].byte_offset = 0; /* fill in later */
assign_symbol(j, no_actions++, CONSTANT_T);
symbols[j].flags |= ACTION_SFLAG;
}
symbols[j].flags |= USED_SFLAG;
INITAO(&AO);
AO.value = symbols[j].value;
AO.marker = ACTION_MV;
if (!glulx_mode) {
AO.type = SHORT_CONSTANT_OT;
if (symbols[j].value >= 256) AO.type = LONG_CONSTANT_OT;
}
else {
AO.type = CONSTANT_OT;
}
return AO;
}
extern void find_the_actions(void)
{ int i; int32 j;
for (i=0; i<no_actions; i++)
{
/* The name looks like "action__A". We're going to convert that to
"actionSub". Allocate enough space for both. */
int namelen = strlen(symbols[actions[i].symbol].name);
char *action_sub, *action_name;
ensure_memory_list_available(&action_symname_memlist, 2*(namelen+1));
action_sub = action_symname_memlist.data;
action_name = (char *)action_symname_memlist.data + (namelen+1);
strcpy(action_name, symbols[actions[i].symbol].name);
action_name[namelen - 3] = '\0'; /* remove "__A" */
strcpy(action_sub, action_name);
strcat(action_sub, "Sub");
j = symbol_index(action_sub, -1, NULL);
if (symbols[j].flags & UNKNOWN_SFLAG)
{
error_named_at("No ...Sub action routine found for action:", action_name, symbols[actions[i].symbol].line);
}
else if (symbols[j].type != ROUTINE_T)
{
ebf_symbol_error("action's ...Sub routine", action_sub, typename(symbols[j].type), symbols[j].line);
}
else
{ actions[i].byte_offset = symbols[j].value;
symbols[j].flags |= USED_SFLAG;
}
}
}
/* ------------------------------------------------------------------------- */
/* Adjectives. */
/* ------------------------------------------------------------------------- */
static int make_adjective_v1(char *English_word)
{
/* Returns adjective number of the English word supplied, creating
a new adjective number if need be.
Note that (partly for historical reasons) adjectives are numbered
from 0xff downwards. (And partly to make them stand out as tokens.)
This routine is used only in grammar version 1: the corresponding
table is left empty in GV2. For GV3, see below. */
uchar *new_sort_code;
int i;
if (no_adjectives >= 255) {
error("Grammar version 1 cannot support more than 255 prepositions");
return 0;
}
if (ZCODE_LESS_DICT_DATA && !glulx_mode) {
/* We need to use #dict_par3 for the preposition number. */
error("Grammar version 1 cannot be used with ZCODE_LESS_DICT_DATA");
return 0;
}
/* Allocate the extra space even though we might not need it. We'll use
the prospective new adjective_sort_code slot as a workspace. */
ensure_memory_list_available(&adjectives_memlist, no_adjectives+1);
ensure_memory_list_available(&adjective_sort_code_memlist, (no_adjectives+1) * DICT_WORD_BYTES);
new_sort_code = adjective_sort_code+no_adjectives*DICT_WORD_BYTES;
dictionary_prepare(English_word, new_sort_code);
for (i=0; i<no_adjectives; i++)
if (compare_sorts(new_sort_code,
adjective_sort_code+i*DICT_WORD_BYTES) == 0)
return(0xff-i);
adjectives[no_adjectives]
= dictionary_add(English_word,PREP_DFLAG,0,0xff-no_adjectives);
return(0xff-no_adjectives++);
}
static int make_adjective_v3(char* English_word)
{
/* Returns adjective number of the English word supplied, creating
a new adjective number if need be.
Adjectives are numbered from 0 upwards.
This routine is used only in grammar version 3.
*/
int l;
int32 dict_address;
if (no_adjectives >= 255) {
error("Grammar version 3 cannot support more than 255 prepositions");
return 0;
}
dict_address = dictionary_add(English_word, PREP_DFLAG, 0, 0);
for (l = 0; l < no_adjectives; l++)
if (adjectives[l] == dict_address)
return l;
ensure_memory_list_available(&adjectives_memlist, no_adjectives + 1);
adjectives[no_adjectives] = dict_address;
return(no_adjectives++);
}
/* ------------------------------------------------------------------------- */
/* Parsing routines. */
/* ------------------------------------------------------------------------- */
static int make_parsing_routine(int32 routine_address)
{
/* This routine is used only in grammar version 1 and 3: the
corresponding table is left empty in GV2. */
int l;
for (l=0; l<no_grammar_token_routines; l++)
if (grammar_token_routine[l] == routine_address)
return l;
ensure_memory_list_available(&grammar_token_routine_memlist, no_grammar_token_routines+1);
grammar_token_routine[no_grammar_token_routines] = routine_address;
return(no_grammar_token_routines++);
}
/* ------------------------------------------------------------------------- */
/* The English-verb list. */
/* ------------------------------------------------------------------------- */
static int find_verb_entry(int dictword)
{
/* Returns the English-verb index which matches the given dict word,
* or -1 if not found. */
int ix;
for (ix=0; ix<English_verbs_count; ix++) {
if (English_verbs[ix].dictword == dictword) {
return ix;
}
}
return -1;
}
static int renumber_verb(int dictword, int new_number)
{
/* Renumbers the Inform-verb number which English_verb matches in
* English_verbs to account for the case when we are
* extending a verb. Returns 0 if successful, or -1 if the given
* verb is not in the dictionary (which shouldn't happen as
* get_existing_verb() has already run). */
int ix;
for (ix=0; ix<English_verbs_count; ix++) {
if (English_verbs[ix].dictword == dictword) {
English_verbs[ix].verbnum = new_number;
return 0;
}
}
return(-1);
}
static void print_verbs_by_number(int num)
{
/* Print all English verb strings with the given verb number. */
int ix;
int count = 0;
for (ix=0; ix<English_verbs_count; ix++) {
if (English_verbs[ix].verbnum == num) {
char *str = English_verbs[ix].textpos + English_verbs_text;
printf(" '%s'", str);
count++;
}
}
if (!count)
printf(" <none>");
}
static int get_existing_verb(int *dictref)
{
/* Look at the last-read token: if it's the name of an English verb
understood by Inform, in double-quotes, then return the Inform-verb
that word refers to: otherwise give an error and return -1.
Optionally also return the dictionary word index in dictref.
*/
int j, evnum, dictword;
if (dictref)
*dictref = -1;
if ((token_type != DQ_TT) && (token_type != SQ_TT)) {
ebf_curtoken_error("an English verb in quotes");
return -1;
}
dictword = dictionary_find(token_text);
if (dictword < 0) {
error_named("There is no previous grammar for the verb",
token_text);
return -1;
}
evnum = find_verb_entry(dictword);
j = (evnum < 0) ? -1 : English_verbs[evnum].verbnum;
if (j < 0) {
error_named("There is no previous grammar for the verb",
token_text);
return -1;
}
if (dictref)
*dictref = dictword;
return j;
}
void locate_dead_grammar_lines()
{
/* Run through the grammar table and check whether each entry is
associated with a verb word. (Some might have been detached by
"Extend only".)
*/
int verb;
int ix;
for (verb=0; verb<no_Inform_verbs; verb++) {
Inform_verbs[verb].used = FALSE;
}
for (ix=0; ix<English_verbs_count; ix++) {
verb = English_verbs[ix].verbnum;
if (verb < 0 || verb >= no_Inform_verbs) {
char *str = English_verbs[ix].textpos + English_verbs_text;
error_named("An entry in the English verb list had an invalid verb number", str);
}
else {
Inform_verbs[verb].used = TRUE;
}
}
for (verb=0; verb<no_Inform_verbs; verb++) {
if (!Inform_verbs[verb].used) {
warning_at("Verb declaration no longer has any verbs associated. Use \"Extend replace\" instead of \"Extend only\"?", Inform_verbs[verb].line);
}
}
}
/* ------------------------------------------------------------------------- */
/* Grammar lines for Verb/Extend directives. */
/* ------------------------------------------------------------------------- */
static void ensure_grammar_lines_available(int verbnum, int num)
{
/* Note that the size field always starts positive. */
if (num > Inform_verbs[verbnum].size) {
int newsize = 2*num+4;
my_realloc(&Inform_verbs[verbnum].l, sizeof(int) * Inform_verbs[verbnum].size, sizeof(int) * newsize, "grammar lines for one verb");
Inform_verbs[verbnum].size = newsize;
}
}
static int grammar_line(int verbnum, int allmeta, int line)
{
/* Parse a grammar line, to be written into grammar_lines[] starting
at grammar_lines_top. grammar_lines_top is left at the end
of the new line.
This stores the line position in Inform_verbs[verbnum].l[line].
(It does not increment Inform_verbs[verbnum].lines; the caller
must do that.)
Syntax: * <token1> ... <token-n> -> <action>
is compiled to a table in the form:
<action : word>
<token 1> ... <token n> <ENDIT>
where <ENDIT> is the byte 15, and each <token> is 2 or 3 bytes
long. The action word contains the action number (bottom 10 bits)
and the "reverse" flag (bit 10).
The token format is:
<bytecode> 00 00 [GV1]
<bytecode> <dat> <dat> [GV2]
<bytecode> <dat> [GV3]
If grammar_version_number is 3, we omit the <ENDIT> and instead
encode the token count in the top 5 bits of the action word.
Also, tokens only have one byte of data; we store adjective
and parsing routine index numbers instead of addresses.
Return TRUE if grammar continues after the line, FALSE if the
directive comes to an end. */
int j, bytecode, mark; int32 wordcode;
int grammar_token, slash_mode, last_was_slash;
int reverse_action, meta_action, TOKEN_SIZE;
debug_location_beginning beginning_debug_location =
get_token_location_beginning();
get_next_token();
if ((token_type == SEP_TT) && (token_value == SEMICOLON_SEP))
{ discard_token_location(beginning_debug_location);
return FALSE;
}
if (!((token_type == SEP_TT) && (token_value == TIMES_SEP)))
{ discard_token_location(beginning_debug_location);
ebf_curtoken_error("'*' divider");
panic_mode_error_recovery();
return FALSE;
}
mark = grammar_lines_top;
ensure_grammar_lines_available(verbnum, line+1);
Inform_verbs[verbnum].l[line] = mark;
if (!glulx_mode) {
mark = mark + 2;
TOKEN_SIZE = (grammar_version_number == 3) ? 2 : 3;
}
else {
mark = mark + 3;
TOKEN_SIZE = 5;
}
ensure_memory_list_available(&grammar_lines_memlist, mark);
grammar_token = 0; last_was_slash = TRUE; slash_mode = FALSE;
no_grammar_lines++;
do
{ get_next_token();
bytecode = 0; wordcode = 0;
if ((token_type == SEP_TT) && (token_value == SEMICOLON_SEP))
{ discard_token_location(beginning_debug_location);
ebf_curtoken_error("'->' clause");
return FALSE;
}
if ((token_type == SEP_TT) && (token_value == ARROW_SEP))
{ if (last_was_slash && (grammar_token>0))
ebf_curtoken_error("grammar token");
break;
}
if (!last_was_slash) slash_mode = FALSE;
if ((token_type == SEP_TT) && (token_value == DIVIDE_SEP))
{ if (grammar_version_number == 1)
error("'/' can only be used with grammar version 2 or later");
if (last_was_slash)
ebf_curtoken_error("grammar token or '->'");
else
{ last_was_slash = TRUE;
slash_mode = TRUE;
if (((grammar_lines[mark-TOKEN_SIZE]) & 0x0f) != 2)
error("'/' can only be applied to prepositions");
grammar_lines[mark-TOKEN_SIZE] |= 0x20;
continue;
}
}
else last_was_slash = FALSE;
if ((token_type == DQ_TT) || (token_type == SQ_TT))
{ if (grammar_version_number == 1)
{ bytecode = make_adjective_v1(token_text);
}
else if (grammar_version_number == 2)
{ bytecode = 0x42;
wordcode = dictionary_add(token_text, PREP_DFLAG, 0, 0);
}
else if (grammar_version_number == 3)
{ bytecode = 0x42;
wordcode = make_adjective_v3(token_text);
}
}
else if ((token_type==DIR_KEYWORD_TT)&&(token_value==NOUN_DK))
{ get_next_token();
if ((token_type == SEP_TT) && (token_value == SETEQUALS_SEP))
{
/* noun = <routine> */
get_next_token();
if ((token_type != SYMBOL_TT)
|| (symbols[token_value].type != ROUTINE_T))
{ discard_token_location(beginning_debug_location);
ebf_curtoken_error("routine name after 'noun='");
panic_mode_error_recovery();
return FALSE;
}
if (grammar_version_number == 1)
{ bytecode = 16 +
make_parsing_routine(symbols[token_value].value);
}
else if (grammar_version_number == 2)
{ bytecode = 0x83;
wordcode = symbols[token_value].value;
}
else if (grammar_version_number == 3)
{ bytecode = 0x83;
wordcode = make_parsing_routine(symbols[token_value].value);
}
symbols[token_value].flags |= USED_SFLAG;