-
Notifications
You must be signed in to change notification settings - Fork 11
/
parser.cpp
1528 lines (1429 loc) · 42.1 KB
/
parser.cpp
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
#include "stdafx.h"
#include "defs.h"
#include "parser.h"
#include "dung.h"
#include "object.h"
#include "funcs.h"
#include "makstr.h"
#include "util.h"
#include "globals.h"
#include "objfns.h"
#include "rooms.h"
#include "adv.h"
#include "memq.h"
const char *brks = "\"' :.,?!\n";
OrphanP orphans = std::make_shared<Orphans>();
Nefals nefals0;
Nefals nefals{ ObjectP(), 1 };
Nefals nefals2{ ObjectP(), 2 };
Nefals necontinue{ ObjectP(), 3 };
Iterator<ParseContV> unknown;
SyntaxP bunch_syn;
bool gwim_disable = false;
std::list<VerbP> bunchers;
// Takes an input string and lowercases it.
// first implies that the first letter will remain the same (if it's uppercase)
// If lc is false, the string will be unchanged.
std::string foostr(std::string nam, bool first, bool lc)
{
auto start_iter = nam.begin();
if (!lc && first)
++start_iter;
std::transform(start_iter, nam.end(), start_iter,
[](char c)
{
return tolower(c);
});
return nam;
}
ObjectP prsi()
{
ObjectP rv;
if (ObjectP *op = std::get_if<ObjectP>(&prsvec[2]))
rv = *op;
else if (PhraseP *pp = std::get_if<PhraseP>(&prsvec[2]))
rv = (*pp)->obj();
return rv;
}
std::array<ParseContP, lexsize> lex_prog()
{
std::array<ParseContP, lexsize> ls;
std::generate(ls.begin(), ls.end(), []() { return std::make_shared<ParseCont>(); });
return ls;
}
ParseContV lexv_v = lex_prog();
Iterator<ParseContV> lexv = Iterator<ParseContV>(lexv_v);
ParseContV lexv1_v = lex_prog();
Iterator<ParseContV> lexv1 = Iterator<ParseContV>(lexv1_v);
Iterator<ParseContV> unknown_lexv = lexv;
// Just swaps the values of both lexv and lexv1, and inbuf and inbuf1.
void swap_em()
{
std::swap(inbuf, inbuf1);
std::swap(lexv, lexv1);
}
Iterator<ParseContV> lex(SIterator s, SIterator sx)
{
if (!sx.is_init())
{
sx = rest(s, length(s));
}
const std::string &brks = ::brks;
auto v = lexv1;
auto tv = v;
auto s1 = s;
bool quot = false;
char brk = ' ';
int cnt;
SIterator t;
SIterator then_str;
Iterator<ParseContV> rv;
Iterator<ParseContV> then_vec;
swap_em();
auto vv = v;
while (vv.cur() != vv.end() - 1)
{
// Reset the parse vector.
vv[0]->s1 = SIterator(vv[0]->s1.cont(), vv[0]->s1.end());
++vv;
}
if (s[0] == '?')
{
v[0]->s1 = substruc("HELP", 0, 4, back(v[0]->s1, 4));
rv = tv;
}
else
{
int slen;
char char_;
bool qend = false;
while (1)
{
if ((length(s1) == length(sx) && (char_ = brk = ' ')) ||
(((char_ = s1[0]) == ';' || memq(char_, brks)) && (brk = char_)))
{
qend = false;
if (length(s1) > length(sx) && (char_ == '\'' || s1[0] == '\"'))
{
if (!quot)
{
quot = true;
v = rest(v, 1);
}
else
{
qend = true;
}
}
if (s == s1 &&
(brk == ',' || brk == '.') &&
length(v) < lexsize - 1 &&
(back(v, 3)[0]->s1 != "AND" || back(v, 3)[0]->s1 != "THEN"))
{
_ASSERT(0);
}
else if (s != s1)
{
if (empty(v))
{
_ASSERT(0);
}
else
{
cnt = length(s) - length(s1);
slen = std::min(cnt, 5);
v[0]->s1 = uppercase((substruc(s, 0, slen, back(v[0]->s1, slen))));
v[0]->s2 = s;
v[0]->i1 = cnt;
v = rest(v);
if (empty(v))
{
_ASSERT(0);
}
if (qend)
{
if (empty(v = rest(v)))
{
_ASSERT(0);
}
}
if (brk == ',')
{
v[0]->s1 = substruc("AND", 0, 3, back(v[0]->s1, 3));
v = rest(v);
}
else if (brk == '.')
{
v[0]->s1 = substruc("THEN", 0, 4, back(v[0]->s1, 4));
then_str = rest(s1);
then_vec = v;
v = rest(v);
}
if ((length(v) < lexsize - 1) &&
((t = back(v, 1)[0]->s1) == "AND" || t == "THEN") &&
((t = back(v, 2)[0]->s1) == "AND" || t == "THEN"))
{
v = back(v, 1);
v[0]->s1 = rest(v[0]->s1, length(v[0]->s1));
}
}
}
if (length(s1) == length(sx) || brk == ';')
{
if (v != tv &&
((t = ((v = back(v, 1))[0]->s1)) == "AND" || t == "THEN"))
{
v[0]->s1 = rest(v[0]->s1, length(v[0]->s1));
}
rv = tv;
break;
}
s = rest(s1);
}
s1 = rest(s1);
}
}
return rv;
}
bool eparse(Iterator<ParseContV> pv, bool vb)
{
bool rv;
const ObjectP &bobj = bunch_obj;
Iterator<ParseContV> unk = unknown;
Iterator<ParseContV> lv = lexv;
int len;
ParseVecVal obj;
trz(bobj, climbbit, tiebit, staggered );
std::string str = pv[0]->s1;
if (str == "AGAIN")
{
swap_em();
pv = lexv;
}
else if ((str == "OOPS" || str == "O") && unk)
{
len = length(lv[1]->s1);
unk[0]->s1 = substruc(lv[1]->s1, 0, (int)lv[1]->s1.size(), back(rest(unk[0]->s1, length(unk[0]->s1)), len));
unk[0]->s2 = lv[1]->s2;
unk[0]->i1 = lv[1]->i1;
swap_em();
pv = unknown_lexv;
}
unknown.clear();
parse_cont.clear();
if (empty(pv[0]->s1)) // 79
{
if (!vb)
tell("Beg pardon?");
rv = false;
}
else
{
SParseVal val;
if (!is_empty(val = sparse(pv, vb)))
{
if (vb)
{
if (auto pvp = std::get_if<ParseVec>(&val))
{
ActionP *pact;
ActionP act = (pact = std::get_if<ActionP>(&((*pvp)[0]))) ? *pact : ActionP();
if (act)
{
std::get<ParseVec>(val)[0] = act->vdecl()[0]->sfcn;
}
orphan();
}
rv = true;
}
else if (std::get_if<cwin>(&val))
{
orphan();
rv = true;
}
else if (syn_match(std::get<ParseVec>(val)))
{
rv = true;
orphan();
if (ObjectP *op = std::get_if<ObjectP>(&(obj = std::get<ParseVec>(val)[1])))
{
if (*op == bobj)
last_it = bunch[0];
else
last_it = as_obj(obj);
}
if (!is_empty(obj) && trnn(as_obj(obj), bunchbit))
{
if (memq(as_verb(std::get<ParseVec>(val)[0]), bunchers))
{
if (as_obj(obj) == bobj)
{
as_obj(obj)->obverb(as_verb(std::get<ParseVec>(val)[0]));
put(std::get<ParseVec>(val), 0, buncher);
}
rv = true;
}
else
{
vb || tell("Multiple inputs cannot be used with " +
lcify(as_verb(std::get<ParseVec>(val)[0])->w()) + ".", 1);
rv = false;
}
}
}
else
rv = false;
}
else
rv = false;
}
return rv;
}
// Duplicates a MDL return statement from a REPEAT
#define RETURN(b) { val = b; break; }
SParseVal sparse(Iterator<ParseContV> sv, bool vb)
{
StuffVecP os;
const WordsPobl &words = words_pobl;
const ObjectPobl &objob = object_pobl();
ParseVec pv = prsvec;
ParseVec pvr = put(put(rest(pv), 0, nullptr), 1, nullptr);
ParseVec andloc;
PrepVec prepvec = ::prepvec;
const ObjectP &except = sfind_obj("EXCEP");
const ActionsPobl &actions = actions_pobl;
const DirectionsPobl &dirs = directions_pobl;
ObjectP robj;
OrphanP orph = orphans;
bool orfl = orph->oflag();
bool andflg = false;
RoomP here = ::here;
ActionP action;
PrepP prep;
AdjectiveP adj;
Iterator<ObjVector> bobjs = bunuvec;
bool bunchflg = false;
PrepP nprep;
PhraseP pprep;
Nefals obj;
ObjectP lobj;
ObjectP nobj;
ParseAval aval;
const char *walk_str = "WALK";
pv[0] = ParseVecVal();
SParseVal val;
auto vv = sv;
while (1)
{
std::string y = vv[0]->s1; // 144
if (empty(y))
{
RETURN(true);
}
else if (y == "THEN") // 146
{
vv = rest(vv, 1);
if (empty(vv))
{
RETURN(true);
}
else
{
parse_cont = vv;
RETURN(true);
}
}
else if (y == "AND") // 150
{
vv = rest(vv, 1);
if (empty(vv))
{
RETURN(true);
}
andflg = true;
continue;
}
std::string x = y; // 154
bool cont_proc = true;
ActionP *ap = nullptr;
direction *dp;
if (!action && (ap = std::get_if<ActionP>(&(aval = plookup(x, actions)))) && *ap) // 156
{
orph->overb(nullptr);
action = *ap;
cont_proc = false;
}
else if ((!action || (action == plookup(walk_str, actions) && !prep)) &&
(dp = std::get_if<direction>(&(aval = plookup(x, dirs)))) && *dp && // 161
!(orfl && !orph->oname().empty() && plookup(x, words)))
{
action = find_action(walk_str);
pv[0] = find_verb(walk_str);
pv[1] = as_pvv(aval);
cont_proc = false;
// Advance pvr one since the direction is now put into the direct object.
// This is not done in the MDL code, but seems to be necessary in case
// the user supplies an object. (Like "go in house")
pvr = rest(pvr);
}
else if (as_word(aval = plookup(x, words))) // 166
{
cont_proc = false;
if (std::dynamic_pointer_cast<verb>(as_word(aval))) // 167
{
cont_proc = true;
}
else if (std::dynamic_pointer_cast<prep_t>(as_word(aval)) != nullptr) // 168
{
if (adj && (obj = get_object(adj->w(), nullptr)).first) // 169
{
os = stuff_obj(obj.first, prep, prepvec, pvr, vb);
if (os)
{
prepvec = os->iprepvec;
pvr = os->iparsevec;
adj.reset();
prep = std::dynamic_pointer_cast<prep_t>(as_word(aval));
}
else
{
RETURN(false);
}
}
else if (prep)
{
cont_proc = true;
}
else
{
prep = std::dynamic_pointer_cast<prep_t>(as_word(aval));
bunchflg = false;
andflg = false;
adj.reset();
}
}
else if (std::dynamic_pointer_cast<adjective>(as_word(aval))) // 184
{
adj = std::dynamic_pointer_cast<adjective>(as_word(aval));
if (orfl && !std::get<std::string>(aval = orph->oname()).empty()) // 186
{
x = y = std::get<std::string>(aval);
cont_proc = true;
}
}
}
const ObjList *aval_objs = nullptr;
if (cont_proc &&
(aval_objs = std::get_if<ObjList>(&(aval = plookup(x, objob)))) &&
!(*aval_objs).empty()) // 190
{
cont_proc = false;
if (aval_objs->front() == it_object) // 193
{
if (lit(here))
{
lobj = obj.first = get_it_obj();
}
else
{
tell("I can't find it in the dark.");
RETURN(false);
}
}
else // 197
{
lobj.reset();
obj = get_object(x, adj);
}
if (obj.first) // Still part of 193
{
if (obj == except) // 199
{
Bits fx;
if (length(pvr) != 2 &&
( (obj.first = as_obj(back(pvr)[0])) == sfind_obj("EVERY") && (fx = climbbit)) ||
( obj.first == sfind_obj("VALUA") && (fx = tiebit)) ||
( obj.first == sfind_obj("POSSE") && (fx = staggered)) )
{
andloc = back(pvr);
put(andloc, 0, tro(sfind_obj("*BUN*"), fx));
andflg = true;
bunchflg = true;
}
else
{
vb || tell("That doesn't make sense!");
RETURN(false);
}
}
else if (andflg) // 215
{
bunchflg || (put(bobjs = back(bobjs), 0, as_obj((andloc = back(pvr))[0])), true);
put(bobjs = back(bobjs), 0, obj.first);
adj.reset();
bunchflg = true;
}
else if (prep == plookup("OF", words)) // 223
{
prep.reset();
// Simple check to make sure that the direct object is actually an
// object. Technically more checks should be done here, in particular
// is should check that the object specified corresponds to something
// in the direct object. (e.g. "get bottle of water"). This code just
// ignores anything after "of", so even something ridiculous like
// "get bottle of sack" will parse to "get bottle".
if (std::get_if<ObjectP>(&pv[1]))
{
}
else if (vb || tell("That doesn't make sense!"))
{
RETURN(false);
}
}
else if (os = stuff_obj(obj.first, prep, prepvec, pvr, vb)) // 228
{
prepvec = os->iprepvec;
pvr = os->iparsevec;
prep.reset();
}
else // 232
{
RETURN(false);
}
}
else if (!orph->oname().empty() && // 233
std::get_if<ObjectP>(&back(pvr)[0]) && (nobj = as_obj(back(pvr)[0])) &&
this_it(x, nobj, nullptr, Globals()))
{
// NOP
cont_proc = false;
}
else
{
if (obj == nefals0)
{
if (!vb)
{
if (lit(here))
{
if (lobj)
{
tell("I can't see any " + lobj->odesc2(), 0);
}
else if (vv == top(vv))
{
tell("I can't see that", 0);
}
else
{
tell("I can't see any ", 0);
if (adj)
{
tell(lcify(adj->w()) + " ", 0);
}
tell(lcify(vv[0]->s2, vv[0]->i1), 0);
}
tell(" here.");
orphan();
}
else
{
tell("It is too dark to see.");
}
}
RETURN(false);
}
else if (obj == nefals2)
{
if (!vb)
{
tell("I can't reach that from inside the " + (robj = (*winner)->avehicle())->odesc2() + ".", 1);
}
orphan();
RETURN(false);
//_ASSERT(0); // Where do we go from here? Probably cont_proc = false;
}
else
{
aval = action ? action : ((orfl && orph->overb()) ? orph->overb() : ActionP());
const ActionP &the_action = std::get<ActionP>(aval);
orphan(true,
the_action,
as_obj(pv[1]), prep, y.substr(0, 5));
if (!vb)
{
tell("Which ", 0);
tell(lcify(vv[0]->s2, vv[0]->i1), 0); // Check this
if (the_action)
{
tell(" should I " + prlcstr(the_action->vstr()) + "?", 1);
}
else
{
tell("?");
}
}
RETURN(false);
}
}
adj.reset();
}
if (cont_proc) // 290 unknown word
{
if (!vb)
{
if (action && plookup(x, actions))
{
tell("Two verbs in command?");
}
else
{
unknown = vv;
unknown_lexv = sv;
tell("I don't know the word '", 0);
tell(lcify(vv[0]->s2, vv[0]->i1) + "'.");
}
}
RETURN(false);
}
if (empty(vv = rest(vv, 1))) // 304
RETURN(true);
}
if (andloc) // 305
{
put(andloc, 0, bunch_obj);
put(bobjs = back(bobjs), 0, sfind_obj("*BUN*"));
bunch = bobjs;
}
if (!is_empty(pv[0]) && as_verb(pv[0]) == find_verb(walk_str))
return cwin();
_ASSERT(std::holds_alternative<bool>(val));
if (std::get<bool>(val)) // 308
{
val = std::monostate();
bool proc = false;
if (adj)
{
if ((obj = get_object(adj->w(), nullptr)).first &&
(os = stuff_obj(obj.first, prep, prepvec, pvr, vb)))
{
prepvec = os->iprepvec;
pvr = os->iparsevec;
prep.reset();
adj.reset();
proc = true;
}
else if (obj.first || vb)
{
tell("I can't see any " + lcify(adj->w()) + " here.");
return SParseVal();
}
}
if (!proc && !action && !(orfl && (action = orph->overb()))) // 322
{
if (!vb)
{
// No verb.
if (ObjectP *op = std::get_if<ObjectP>(&pv[1]))
{
tell("What should I do with the " + (*op)->odesc2() + "?");
}
else
{
tell("Huh?");
}
}
if (ObjectP *op = std::get_if<ObjectP>(&pv[1]))
orphan(true, nullptr, *op);
else if (PhraseP *pp = std::get_if<PhraseP>(&pv[1]))
orphan(true, nullptr, *pp);
return SParseVal();
}
if (put(pv, 0, action) && !is_empty(pv[1]) && adj) // 332
{
if (!vb)
{
tell("Huh?");
}
}
if (orfl && (nprep = orphans->oprep()) && is_empty(pv[2]) &&
!prep &&
std::get<ActionP>(pv[0]) == orph->overb())
{
if (ObjectP *op = std::get_if<ObjectP>(&pv[1]))
obj.first = *op;
else
obj.first = std::get<PhraseP>(pv[1])->obj();
(pprep = prepvec[0])->prep(nprep);
pprep->obj(obj.first);
::prepvec = prepvec = (length(prepvec) == 1) ? top(prepvec) : rest(prepvec);
if (orph->oslot1())
{
pv[1] = orph->oslot1();
pv[2] = pprep;
}
else
{
pv[2] = pprep;
}
}
if (prep) // 352 - Change pick frob up to pick up frob
{
if (std::get_if<ObjectP>(&back(pvr)[0]))
{
pprep = prepvec[0];
::prepvec = prepvec = (length(prepvec) < 1 ? top(prepvec) : rest(prepvec));
pprep->prep(prep);
pprep->obj(obj.first);
put(back(pvr), 0, pprep);
}
else
{
orphan(true, action, std::monostate(), prep);
val = pv;
}
}
else
val = pv;
}
else
{
val = std::monostate();
}
_ASSERT(is_empty(val) || std::get_if<ParseVec>(&val));
return val;
}
const OrphanP &orphan(bool flag, ActionP action, OrphanSlotType slot1, PrepP prep,
const std::string &name, OrphanSlotType slot2)
{
if (flag)
{
orphans->oslot2(slot2);
orphans->oname(name);
orphans->oprep(prep);
orphans->oslot1(slot1);
orphans->overb(action);
orphans->oflag(flag);
}
else
{
prepvec = top(prepvec);
orphans->oflag(false);
}
return orphans;
}
std::string prlcstr(const std::string &str)
{
return foostr(str, true, true);
}
std::string prstr(const std::string &sp)
{
return foostr(sp, false);
}
std::string prfunny(WordP prep)
{
return prstr(prep->w());
}
bool ortell(VargP varg, ActionP action, ObjectP gwim, OrphanSlotType slot2)
{
bool rv = false;
PrepP prep = varg->vprep;
if (prep)
{
if (gwim)
{
tell(action->vstr() + " ", 0);
tell(gwim->odesc2() + " ", 0);
}
rv = tell(prfunny(prep) + " what?", 1);
}
else if (auto pp = std::get_if<PhraseP>(&slot2))
{
tell(action->vstr() + " what ", 0);
tell(prfunny((*pp)->prep()) + " the " + (*pp)->obj()->odesc2(), 0);
tell("?");
}
else
{
tell(action->vstr() + " what?", 1);
}
return false;
}
StuffVecP stuff_obj(ObjectP obj, PrepP prep, PrepVec prepvec, ParseVec pvr, bool vb)
{
StuffVecP stuff;
PhraseP pprep;
if (prep == plookup("OF", words_pobl))
{
ParseVecVal &a = back(pvr, 1)[0];
if (std::get<ObjectP>(a) == obj)
{
stuff = std::make_unique<StuffVec>();
stuff->iprepvec = prepvec;
stuff->iparsevec = pvr;
return stuff;
}
else if (vb)
{
tell("That doesn't make sense!");
return stuff;
}
}
if (empty(pvr))
{
vb || tell("Too many objects specified?");
return stuff;
}
else
{
pvr[0] = [&]() ->ParseVecVal
{
if (prep)
{
pprep = prepvec[0];
::prepvec = prepvec = (length(prepvec) < 1) ? top(prepvec) : rest(prepvec);
pprep->prep(prep);
pprep->obj(obj);
return pprep;
}
else
return obj;
}();
stuff = std::make_unique<StuffVec>();
stuff->iprepvec = prepvec;
stuff->iparsevec = rest(pvr);
return stuff;
}
}
ObjectP is_global(const ObjectP &obj, const std::vector<Bits> &gflags)
{
// This is a global object if it can be cast into a GObject,
// and it's in the global list.
GObjectPtr go = std::dynamic_pointer_cast<GObject>(obj);
if (go)
{
// If there is no name, then just return it.
if (!go->gbits().has_value())
return go;
// Otherwise, check to see if it's in the passed list.
if (std::find(gflags.begin(), gflags.end(), go->gbits()) != gflags.end())
return go;
}
return ObjectP();
}
// Returns true if all these are true:
// Object is global and global flag is set, otherwise always true
// Object is visible
// Object name matches the obj
// If adjective is specified, the adjective must match the object as well.
bool this_it(const std::string &objname, ObjectP obj, AdjectiveP adj, Globals global)
{
bool rv = false;
// Continue if not searching for globals, or if this is a global object.
if ((!global.has_value() || is_global(obj, *global.value())) &&
trnn(obj, ovison) &&
memq(objname, obj->onames()))
{
if (!adj)
{
rv = true;
}
else
{
rv = memq(adj, obj->oadjs());
}
}
return rv;
}
Nefals search_list(const std::string &objnam, const ObjList &slist, const AdjectiveP &adj, bool first, const Globals &global)
{
ObjectP oobj;
Nefals nobj;
Nefals nefals = ::nefals;
bool ambig_empty = false;
for (auto &obj : slist)
{
Nefals result;
if ((result = [&]() -> Nefals
{
Nefals rv = necontinue;
if (this_it(objnam, obj, adj, global))
{
if (oobj)
{
if (!adj)
{
if (oobj->oadjs().empty() && obj->oadjs().empty())
{
rv = nefals;
}
else if (obj->oadjs().empty())
{
oobj = obj;
}
else
{
ambig_empty = true;
}
}
else
{
rv = nefals;
}
}
else
{
oobj = obj;
}
}
return rv;
}()) != necontinue)
{
return result;
}
else if ((result = [&]() ->Nefals
{
Nefals result = necontinue;
if (trnn(obj, ovison) &&
(trnn(obj, openbit) || trnn(obj, transbit)) &&
(first || trnn(obj, searchbit)))
{
nobj = search_list(objnam, obj->ocontents(), adj);
if (std::get<0>(nobj))
{
if (oobj)
return nefals;
else
oobj = std::get<0>(nobj);
}
else
{
if (nobj == nefals)
return nefals;
}
}
return necontinue;
}()) != necontinue)
{
return result;
}
}
if (ambig_empty && oobj && !oobj->oadjs().empty())
{
return nefals;
}
else
{
return Nefals(oobj, 0);
}
}
Nefals get_object(const std::string &objnam, AdjectiveP adj)
{
ObjectP obj;
ObjectP oobj;
ObjectP av = (*winner)->avehicle();
bool chomp = false;
bool lit = ::lit(here);
Nefals rv;
if (lit && (obj = (rv = search_list(objnam, here->robjs(), adj)).first))
{
if (av && obj != av && !memq(obj, av->ocontents()) && !trnn(obj, findmebit))
{
chomp = true;
}
else
{
oobj = obj;
}
}
else if (lit && rv == nefals)
{
return nefals;
}