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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* $URL$
* $Id$
*
*/
#include "sci/engine/state.h"
namespace Sci {
#define SAID_BRANCH_NULL 0
#define MAX_SAID_TOKENS 128
// Maximum number of words to be expected in a parsed sentence
#define AUGMENT_MAX_WORDS 64
// uncomment to debug parse tree augmentation
//#define SCI_DEBUG_PARSE_TREE_AUGMENTATION
#ifdef SCI_DEBUG_PARSE_TREE_AUGMENTATION
#define scidprintf printf
#else
void print_nothing(...) { }
#define scidprintf print_nothing
#endif
static int said_token;
static int said_tokens_nr;
static int said_tokens[MAX_SAID_TOKENS];
static int said_tree_pos;
#define SAID_TREE_START 4 // Reserve space for the 4 top nodes
enum SaidToken {
TOKEN_COMMA = 0xF000,
TOKEN_AMP = 0xF100,
TOKEN_SLASH = 0xF200,
TOKEN_PARENO = 0xF300,
TOKEN_PARENC = 0xF400,
TOKEN_BRACKETO = 0xF500,
TOKEN_BRACKETC = 0xF600,
TOKEN_HASH = 0xF700,
TOKEN_LT = 0xF800,
TOKEN_GT = 0xF900,
TOKEN_TERM = 0xFF00
};
enum SaidWord {
WORD_NONE = 0x0ffe,
WORD_ANY = 0x0fff
};
// TODO: maybe turn this into a proper n-ary tree instead of an
// n-ary tree implemented in terms of a binary tree.
// (Together with _parserNodes in Vocabulary)
static ParseTreeNode said_tree[VOCAB_TREE_NODES];
typedef int wgroup_t;
typedef int said_spec_t;
static ParseTreeNode* said_next_node() {
assert(said_tree_pos > 0 && said_tree_pos < VOCAB_TREE_NODES);
return &said_tree[said_tree_pos++];
}
static ParseTreeNode* said_leaf_node(ParseTreeNode* pos, int value) {
pos->type = kParseTreeLeafNode;
pos->value = value;
return pos;
}
static ParseTreeNode* said_word_node(ParseTreeNode* pos, int value) {
pos->type = kParseTreeWordNode;
pos->value = value;
return pos;
}
static ParseTreeNode* said_branch_node(ParseTreeNode* pos,
ParseTreeNode* left,
ParseTreeNode* right) {
pos->type = kParseTreeBranchNode;
pos->left = left;
pos->right = right;
return pos;
}
static ParseTreeNode* said_branch_attach_left(ParseTreeNode* pos,
ParseTreeNode* left) {
pos->type = kParseTreeBranchNode;
pos->left = left;
return pos;
}
static ParseTreeNode* said_branch_attach_right(ParseTreeNode* pos,
ParseTreeNode* right) {
pos->type = kParseTreeBranchNode;
pos->right = right;
return pos;
}
/*
pos
/ \
. \
*
/ \
/ 0
*
/ \
/ \
/ subtree
major / \
/ .
minor
. = unchanged child node
* = new branch node
0 = NULL child node. (Location for future siblings of the subtree)
*/
static bool said_attach_subtree(ParseTreeNode* pos, int major, int minor,
ParseTreeNode* subtree) {
bool retval = true;
said_branch_attach_right(pos,
said_branch_node(said_next_node(),
said_branch_node(said_next_node(),
said_leaf_node(said_next_node(), major),
said_branch_attach_left(subtree,
said_leaf_node(said_next_node(), minor))),
0));
return retval;
}
/*****************/
/**** Parsing ****/
/*****************/
static bool parseSpec(ParseTreeNode* parentNode);
static bool parsePart2(ParseTreeNode* parentNode, bool& nonempty);
static bool parsePart3(ParseTreeNode* parentNode, bool& nonempty);
static bool parseSlash(ParseTreeNode* parentNode);
static bool parseExpr(ParseTreeNode* parentNode);
static bool parseRef(ParseTreeNode* parentNode);
static bool parseComma(ParseTreeNode* parentNode);
static bool parseList(ParseTreeNode* parentNode);
static bool parseListEntry(ParseTreeNode* parentNode);
static bool parseWord(ParseTreeNode* parentNode);
static bool parseWord(ParseTreeNode* parentNode)
{
int token = said_tokens[said_token];
if (token & 0x8000)
return false;
said_token++;
ParseTreeNode* newNode = said_word_node(said_next_node(), token);
parentNode->right = newNode;
return true;
}
static bool parsePart2(ParseTreeNode* parentNode, bool& nonempty)
{
// Store current state for rolling back if we fail
int curToken = said_token;
int curTreePos = said_tree_pos;
ParseTreeNode* curRightChild = parentNode->right;
ParseTreeNode* newNode = said_branch_node(said_next_node(), 0, 0);
nonempty = true;
bool found;
found = parseSlash(newNode);
if (found) {
said_attach_subtree(parentNode, 0x142, 0x14a, newNode);
return true;
} else if (said_tokens[said_token] == TOKEN_BRACKETO) {
said_token++;
found = parsePart2(newNode, nonempty);
if (found) {
if (said_tokens[said_token] == TOKEN_BRACKETC) {
said_token++;
said_attach_subtree(parentNode, 0x152, 0x142, newNode);
return true;
}
}
}
// CHECKME: this doesn't look right if the [] section matched partially
// Should the below 'if' be an 'else if' ?
if (said_tokens[said_token] == TOKEN_SLASH) {
said_token++;
nonempty = false;
return true;
}
// Rollback
said_token = curToken;
said_tree_pos = curTreePos;
parentNode->right = curRightChild;
return false;
}
static bool parsePart3(ParseTreeNode* parentNode, bool& nonempty)
{
// Store current state for rolling back if we fail
int curToken = said_token;
int curTreePos = said_tree_pos;
ParseTreeNode* curRightChild = parentNode->right;
ParseTreeNode* newNode = said_branch_node(said_next_node(), 0, 0);
bool found;
nonempty = true;
found = parseSlash(newNode);
if (found) {
said_attach_subtree(parentNode, 0x143, 0x14a, newNode);
return true;
} else if (said_tokens[said_token] == TOKEN_BRACKETO) {
said_token++;
found = parsePart3(newNode, nonempty);
if (found) {
if (said_tokens[said_token] == TOKEN_BRACKETC) {
said_token++;
said_attach_subtree(parentNode, 0x152, 0x143, newNode);
return true;
}
}
}
// CHECKME: this doesn't look right if the [] section matched partially
// Should the below 'if' be an 'else if' ?
if (said_tokens[said_token] == TOKEN_SLASH) {
said_token++;
nonempty = false;
return true;
}
// Rollback
said_token = curToken;
said_tree_pos = curTreePos;
parentNode->right = curRightChild;
return false;
}
static bool parseSlash(ParseTreeNode* parentNode)
{
// Store current state for rolling back if we fail
int curToken = said_token;
int curTreePos = said_tree_pos;
ParseTreeNode* curRightChild = parentNode->right;
if (said_tokens[said_token] == TOKEN_SLASH) {
said_token++;
bool found = parseExpr(parentNode);
if (found)
return true;
}
// Rollback
said_token = curToken;
said_tree_pos = curTreePos;
parentNode->right = curRightChild;
return false;
}
static bool parseRef(ParseTreeNode* parentNode)
{
// Store current state for rolling back if we fail
int curToken = said_token;
int curTreePos = said_tree_pos;
ParseTreeNode* curRightChild = parentNode->right;
ParseTreeNode* newNode = said_branch_node(said_next_node(), 0, 0);
ParseTreeNode* newParent = parentNode;
bool found;
if (said_tokens[said_token] == TOKEN_LT) {
said_token++;
found = parseList(newNode);
if (found) {
said_attach_subtree(newParent, 0x144, 0x14f, newNode);
newParent = newParent->right;
newNode = said_branch_node(said_next_node(), 0, 0);
found = parseRef(newNode);
if (found) {
said_attach_subtree(newParent, 0x141, 0x144, newNode);
}
return true;
}
}
// NB: This is not an "else if'.
// If there is a "< [ ... ]", that is parsed as "< ..."
if (said_tokens[said_token] == TOKEN_BRACKETO) {
said_token++;
found = parseRef(newNode);
if (found) {
if (said_tokens[said_token] == TOKEN_BRACKETC) {
said_token++;
said_attach_subtree(parentNode, 0x152, 0x144, newNode);
return true;
}
}
}
// Rollback
said_token = curToken;
said_tree_pos = curTreePos;
parentNode->right = curRightChild;
return false;
}
static bool parseComma(ParseTreeNode* parentNode)
{
// Store current state for rolling back if we fail
int curToken = said_token;
int curTreePos = said_tree_pos;
ParseTreeNode* curRightChild = parentNode->right;
if (said_tokens[said_token] == TOKEN_COMMA) {
said_token++;
bool found = parseList(parentNode);
if (found)
return true;
}
// Rollback
said_token = curToken;
said_tree_pos = curTreePos;
parentNode->right = curRightChild;
return false;
}
static bool parseListEntry(ParseTreeNode* parentNode)
{
// Store current state for rolling back if we fail
int curToken = said_token;
int curTreePos = said_tree_pos;
ParseTreeNode* curRightChild = parentNode->right;
ParseTreeNode* newNode = said_branch_node(said_next_node(), 0, 0);
bool found;
if (said_tokens[said_token] == TOKEN_BRACKETO) {
said_token++;
found = parseExpr(newNode);
if (found) {
if (said_tokens[said_token] == TOKEN_BRACKETC) {
said_token++;
said_attach_subtree(parentNode, 0x152, 0x14c, newNode);
return true;
}
}
} else if (said_tokens[said_token] == TOKEN_PARENO) {
said_token++;
found = parseExpr(newNode);
if (found) {
if (said_tokens[said_token] == TOKEN_PARENC) {
said_token++;
said_attach_subtree(parentNode, 0x141, 0x14c, newNode);
return true;
}
}
} else if (parseWord(newNode)) {
said_attach_subtree(parentNode, 0x141, 0x153, newNode);
return true;
}
// Rollback
said_token = curToken;
said_tree_pos = curTreePos;
parentNode->right = curRightChild;
return false;
}
static bool parseList(ParseTreeNode* parentNode)
{
// Store current state for rolling back if we fail
int curToken = said_token;
int curTreePos = said_tree_pos;
ParseTreeNode* curRightChild = parentNode->right;
bool found;
ParseTreeNode* newParent = parentNode;
found = parseListEntry(newParent);
if (found) {
newParent = newParent->right;
found = parseComma(newParent);
return true;
}
// Rollback
said_token = curToken;
said_tree_pos = curTreePos;
parentNode->right = curRightChild;
return false;
}
static bool parseExpr(ParseTreeNode* parentNode)
{
// Store current state for rolling back if we fail
int curToken = said_token;
int curTreePos = said_tree_pos;
ParseTreeNode* curRightChild = parentNode->right;
ParseTreeNode* newNode = said_branch_node(said_next_node(), 0, 0);
bool ret = false;
bool found;
ParseTreeNode* newParent = parentNode;
found = parseList(newNode);
if (found) {
ret = true;
said_attach_subtree(newParent, 0x141, 0x14F, newNode);
newParent = newParent->right;
}
found = parseRef(newParent);
if (found || ret)
return true;
// Rollback
said_token = curToken;
said_tree_pos = curTreePos;
parentNode->right = curRightChild;
return false;
}
static bool parseSpec(ParseTreeNode* parentNode)
{
// Store current state for rolling back if we fail
int curToken = said_token;
int curTreePos = said_tree_pos;
ParseTreeNode* curRightChild = parentNode->right;
ParseTreeNode* newNode = said_branch_node(said_next_node(), 0, 0);
bool ret = false;
bool found;
ParseTreeNode* newParent = parentNode;
found = parseExpr(newNode);
if (found) {
// Sentence part 1 found
said_attach_subtree(newParent, 0x141, 0x149, newNode);
newParent = newParent->right;
ret = true;
}
bool nonempty;
found = parsePart2(newParent, nonempty);
if (found) {
ret = true;
if (nonempty) // non-empty part found
newParent = newParent->right;
found = parsePart3(newParent, nonempty);
if (found) {
if (nonempty)
newParent = newParent->right;
}
}
if (said_tokens[said_token] == TOKEN_GT) {
said_token++;
newNode = said_branch_node(said_next_node(), 0,
said_leaf_node(said_next_node(), TOKEN_GT));
said_attach_subtree(newParent, 0x14B, TOKEN_GT, newNode);
}
if (ret)
return true;
// Rollback
said_token = curToken;
said_tree_pos = curTreePos;
parentNode->right = curRightChild;
return false;
}
static bool buildSaidTree() {
said_branch_node(said_tree, &said_tree[1], &said_tree[2]);
said_leaf_node(&said_tree[1], 0x141); // Magic number #1
said_branch_node(&said_tree[2], &said_tree[3], 0);
said_leaf_node(&said_tree[3], 0x13f); // Magic number #2
said_tree_pos = SAID_TREE_START;
bool ret = parseSpec(&said_tree[2]);
if (!ret)
return false;
if (said_tokens[said_token] != TOKEN_TERM) {
// No terminator, so parse error.
// Rollback
said_tree[2].right = 0;
said_token = 0;
said_tree_pos = SAID_TREE_START;
return false;
}
return true;
}
static int said_parse_spec(const byte *spec) {
int nextitem;
said_token = 0;
said_tokens_nr = 0;
said_tree_pos = SAID_TREE_START;
do {
nextitem = *spec++;
if (nextitem < SAID_FIRST)
said_tokens[said_tokens_nr++] = nextitem << 8 | *spec++;
else
said_tokens[said_tokens_nr++] = SAID_LONG(nextitem);
} while ((nextitem != SAID_TERM) && (said_tokens_nr < MAX_SAID_TOKENS));
if (nextitem != SAID_TERM) {
warning("SAID spec is too long");
return 1;
}
if (!buildSaidTree()) {
warning("Error while parsing SAID spec");
return 1;
}
return 0;
}
/**********************/
/**** Augmentation ****/
/**********************/
static bool dontclaim;
static int outputDepth;
enum ScanSaidType {
SCAN_SAID_AND = 0,
SCAN_SAID_OR = 1
};
static int matchTrees(ParseTreeNode* parseT, ParseTreeNode* saidT);
static int scanSaidChildren(ParseTreeNode* parseT, ParseTreeNode* saidT,
ScanSaidType type);
static int scanParseChildren(ParseTreeNode* parseT, ParseTreeNode* saidT);
static int node_major(ParseTreeNode* node) {
assert(node->type == kParseTreeBranchNode);
assert(node->left->type == kParseTreeLeafNode);
return node->left->value;
}
static int node_minor(ParseTreeNode* node) {
assert(node->type == kParseTreeBranchNode);
assert(node->right->type == kParseTreeBranchNode);
assert(node->right->left->type == kParseTreeLeafNode);
return node->right->left->value;
}
static bool node_is_terminal(ParseTreeNode* node) {
return (node->right->right &&
node->right->right->type != kParseTreeBranchNode);
}
static int node_terminal_value(ParseTreeNode* node) {
assert(node_is_terminal(node));
return node->right->right->value;
}
#ifdef SCI_DEBUG_PARSE_TREE_AUGMENTATION
static void node_print_desc(ParseTreeNode* node) {
assert(node);
assert(node->left);
if (node->left->type == kParseTreeBranchNode) {
scidprintf("< ");
node_print_desc(node->left);
scidprintf(", ...>");
} else {
if (node_is_terminal(node)) {
scidprintf("(%03x %03x %03x)", node_major(node),
node_minor(node),
node_terminal_value(node));
} else {
scidprintf("(%03x %03x <...>)", node_major(node),
node_minor(node));
}
}
}
#else
static void node_print_desc(ParseTreeNode*) { }
#endif
static int matchTrees(ParseTreeNode* parseT, ParseTreeNode* saidT)
{
outputDepth++;
scidprintf("%*smatchTrees on ", outputDepth, "");
node_print_desc(parseT);
scidprintf(" and ");
node_print_desc(saidT);
scidprintf("\n");
bool inParen = node_minor(saidT) == 0x14F || node_minor(saidT) == 0x150;
bool inBracket = node_major(saidT) == 0x152;
int ret;
if (node_major(parseT) != 0x141 &&
node_major(saidT) != 0x141 && node_major(saidT) != 0x152 &&
node_major(saidT) != node_major(parseT))
{
ret = -1;
}
// parse major is 0x141 and/or
// said major is 0x141/0x152 and/or
// said major is parse major
else if (node_is_terminal(saidT) && node_is_terminal(parseT) ) {
// both saidT and parseT are terminals
int said_val = node_terminal_value(saidT);
int parse_val = node_terminal_value(parseT);
if (said_val != WORD_NONE &&
(said_val == parse_val || said_val == WORD_ANY ||
parse_val == WORD_ANY))
ret = 1;
else
ret = -1;
scidprintf("%*smatchTrees matching terminals: %03x vs %03x (%d)\n",
outputDepth, "", parse_val, said_val, ret);
} else if (node_is_terminal(saidT) && !node_is_terminal(parseT)) {
// saidT is a terminal, but parseT isn't
if (node_major(parseT) == 0x141 ||
node_major(parseT) == node_major(saidT))
ret = scanParseChildren(parseT->right->right, saidT);
else
ret = 0;
} else if (node_is_terminal(parseT)) {
// parseT is a terminal, but saidT isn't
if (node_major(saidT) == 0x141 || node_major(saidT) == 0x152 ||
node_major(saidT) == node_major(parseT))
ret = scanSaidChildren(parseT, saidT->right->right,
inParen ? SCAN_SAID_OR : SCAN_SAID_AND );
else
ret = 0;
} else if (node_major(saidT) != 0x141 && node_major(saidT) != 0x152 &&
node_major(saidT) != node_major(parseT)) {
// parseT and saidT both aren't terminals
// said major is not 0x141 or 0x152 or parse major
ret = scanParseChildren(parseT->right->right, saidT);
} else {
// parseT and saidT are both not terminals,
// said major 0x141 or 0x152 or equal to parse major
ret = scanSaidChildren(parseT->right->right, saidT->right->right,
inParen ? SCAN_SAID_OR : SCAN_SAID_AND);
}
if (inBracket && ret == 0) {
scidprintf("%*smatchTrees changing ret to 1 due to brackets\n",
outputDepth, "");
ret = 1;
}
scidprintf("%*smatchTrees returning %d\n", outputDepth, "", ret);
outputDepth--;
return ret;
}
static int scanSaidChildren(ParseTreeNode* parseT, ParseTreeNode* saidT,
ScanSaidType type) {
outputDepth++;
scidprintf("%*sscanSaid(%s) on ", outputDepth, "",
type == SCAN_SAID_OR ? "OR" : "AND");
node_print_desc(parseT);
scidprintf(" and ");
node_print_desc(saidT);
scidprintf("\n");
int ret = 1;
assert(!(type == SCAN_SAID_OR && !saidT));
while (saidT) {
assert(saidT->type == kParseTreeBranchNode);
ParseTreeNode* saidChild = saidT->left;
assert(saidChild);
if (node_major(saidChild) != 0x145) {
ret = scanParseChildren(parseT, saidChild);
if (type == SCAN_SAID_AND && ret != 1)
break;
if (type == SCAN_SAID_OR && ret == 1)
break;
}
saidT = saidT->right;
}
scidprintf("%*sscanSaid returning %d\n", outputDepth, "", ret);
outputDepth--;
return ret;
}
static int scanParseChildren(ParseTreeNode* parseT, ParseTreeNode* saidT) {
outputDepth++;
scidprintf("%*sscanParse on ", outputDepth, "");
node_print_desc(parseT);
scidprintf(" and ");
node_print_desc(saidT);
scidprintf("\n");
if (node_major(saidT) == 0x14B) {
dontclaim = true;
scidprintf("%*sscanParse returning 1 (0x14B)\n", outputDepth, "");
outputDepth--;
return 1;
}
bool inParen = node_minor(saidT) == 0x14F || node_minor(saidT) == 0x150;
bool inBracket = node_major(saidT) == 0x152;
int ret;
// descend further down saidT before actually scanning parseT
if ((node_major(saidT) == 0x141 || node_major(saidT) == 0x152) &&
!node_is_terminal(saidT)) {
ret = scanSaidChildren(parseT, saidT->right->right,
inParen ? SCAN_SAID_OR : SCAN_SAID_AND );
} else if (parseT && parseT->left->type == kParseTreeBranchNode) {
ret = 0;
int subresult = 0;
while (parseT) {
assert(parseT->type == kParseTreeBranchNode);
ParseTreeNode* parseChild = parseT->left;
assert(parseChild);
scidprintf("%*sscanning next: ", outputDepth, "");
node_print_desc(parseChild);
scidprintf("\n");
if (node_major(parseChild) == node_major(saidT) ||
node_major(parseChild) == 0x141)
subresult = matchTrees(parseChild, saidT);
if (subresult != 0)
ret = subresult;
if (ret == 1)
break;
parseT = parseT->right;
}
// ret is now:
// 1 if ANY matchTrees(parseSibling, saidTree) returned 1
// ELSE: -1 if ANY returned -1
// ELSE: 0
} else {
ret = matchTrees(parseT, saidT);
}
if (inBracket && ret == 0) {
scidprintf("%*sscanParse changing ret to 1 due to brackets\n",
outputDepth, "");
ret = 1;
}
scidprintf("%*sscanParse returning %d\n", outputDepth, "", ret);
outputDepth--;
return ret;
}
static int augment_parse_nodes(ParseTreeNode *parseT, ParseTreeNode *saidT) {
outputDepth = 0;
scidprintf("augment_parse_nodes on ");
node_print_desc(parseT);
scidprintf(" and ");
node_print_desc(saidT);
scidprintf("\n");
dontclaim = false;
int ret = matchTrees(parseT, saidT);
scidprintf("matchTrees returned %d\n", ret);
if (ret != 1)
return 0;
if (dontclaim)
return SAID_PARTIAL_MATCH;
return 1;
}
/*******************/
/**** Main code ****/
/*******************/
int said(EngineState *s, const byte *spec, bool verbose) {
int retval;
Vocabulary *voc = g_sci->getVocabulary();
ParseTreeNode *parse_tree_ptr = voc->_parserNodes;
if (voc->parserIsValid) {
if (said_parse_spec(spec))
return SAID_NO_MATCH;
if (verbose)
vocab_dump_parse_tree("Said-tree", said_tree);
retval = augment_parse_nodes(parse_tree_ptr, said_tree);
if (!retval)
return SAID_NO_MATCH;
else if (retval != SAID_PARTIAL_MATCH)
return SAID_FULL_MATCH;
else
return SAID_PARTIAL_MATCH;
}
return SAID_NO_MATCH;
}
/*
Some test expressions for in the ScummVM debugging console, using
Codename: ICEMAN's vocabulary:
said green board & [!*] / 8af < 1f6
True
said get green board & [!*] / 8af < 1f6
False
said green board & [!*] / 8af [< 1f6 ]
True
said climb up & 19b , 426 [< 142 ] [/ 81e ]
True
said climb up ladder & 19b , 426 [< 142 ] [/ 81e ]
True
said climb down & 19b , 426 [< 142 ] [/ 81e ]
False
said climb up tree & 19b , 426 [< 142 ] [/ 81e ]
False
said climb up & 19b , 446 , 426 [< 143 ] [/ 81e ]
False
said climb down & 19b , 446 , 426 [< 143 ] [/ 81e ]
True
said use green device & 1a5 / 8c1 [< 21d ]
False
said use electronic device & 1a5 / 8c1 [< 21d ]
True
said use device & 1a5 / 8c1 [< 21d ]
True
said eat & 429 [/ !* ]
True
said eat ladder & 429 [/ !* ]
False
said look at the ladder & 3f8 / 81e [< !* ]
True
said look at the green ladder & 3f8 / 81e [< !* ]
False
said look green book & / 7f6 [< 8d2 ]
False
said look green book & 3f8 [< ca ]
True
said get a blue board for the green ladder & 3f9 / 8af [ < 1f6 ] / 81e < 1f6
False
said get a board for the green ladder & 3f9 / 8af [ < 1f6 ] / 81e < 1f6
True
said get a blue board & 3f9 / 8af [ < 1f6 ]
False
said get up & ( 3f8 , 3f9 ) [ < ( 142 , 143 ) ]
True
said get left & ( 3f8 , 3f9 ) [ < ( 142 , 143 ) ]
False
said look down & ( 3f8 , 3f9 ) [ < ( 142 , 143 ) ]
True
said get & ( 3f8 , 3f9 ) [ < ( 142 , 143 ) ]
True
said put washer on shaft & 455 , ( 3fa < cb ) / 8c6
True
said depth correct & [!*] < 8b1 / 22
True
said depth acknowledged & / 46d , 460 , 44d < 8b1
True
said depth confirmed & / 46d , 460 , 44d < 8b1
True
said depth attained & / 46d , 460 , 44d < 8b1
True
*/
} // End of namespace Sci
|