forked from vcmi/vcmi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathERMInterpreter.cpp
More file actions
3255 lines (2967 loc) · 79.8 KB
/
ERMInterpreter.cpp
File metadata and controls
3255 lines (2967 loc) · 79.8 KB
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 "StdInc.h"
#include "ERMInterpreter.h"
#include <cctype>
#include "../../lib/mapObjects/CObjectHandler.h"
#include "../../lib/mapObjects/MapObjects.h"
#include "../../lib/CHeroHandler.h"
#include "../../lib/CCreatureHandler.h"
#include "../../lib/VCMIDirs.h"
#include "../../lib/IGameCallback.h"
#include "../../lib/mapObjects/CGHeroInstance.h"
#include "../../lib/mapObjects/MiscObjects.h"
/*
* ERMInterpreter.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
namespace spirit = boost::spirit;
using namespace VERMInterpreter;
typedef int TUnusedType;
ERMInterpreter *erm;
Environment *topDyn;
namespace ERMPrinter
{
//console printer
using namespace ERM;
struct VarPrinterVisitor : boost::static_visitor<>
{
void operator()(TVarExpNotMacro const& val) const
{
logGlobal->debugStream() << val.varsym;
if(val.val.is_initialized())
{
logGlobal->debugStream() << val.val.get();
}
}
void operator()(TMacroUsage const& val) const
{
logGlobal->debugStream() << "$" << val.macro << "&";
}
};
void varPrinter(const TVarExp & var)
{
boost::apply_visitor(VarPrinterVisitor(), var);
}
struct IExpPrinterVisitor : boost::static_visitor<>
{
void operator()(int const & constant) const
{
logGlobal->warnStream() << constant;
}
void operator()(TVarExp const & var) const
{
varPrinter(var);
}
};
void iexpPrinter(const TIexp & exp)
{
boost::apply_visitor(IExpPrinterVisitor(), exp);
}
struct IdentifierPrinterVisitor : boost::static_visitor<>
{
void operator()(TIexp const& iexp) const
{
iexpPrinter(iexp);
}
void operator()(TArithmeticOp const& arop) const
{
iexpPrinter(arop.lhs);
logGlobal->debugStream() << " " << arop.opcode << " ";
iexpPrinter(arop.rhs);
}
};
void identifierPrinter(const boost::optional<Tidentifier> & id)
{
if(id.is_initialized())
{
logGlobal->debugStream() << "identifier: ";
for (auto x : id.get())
{
logGlobal->debugStream() << "#";
boost::apply_visitor(IdentifierPrinterVisitor(), x);
}
}
}
struct ConditionCondPrinterVisitor : boost::static_visitor<>
{
void operator()(TComparison const& cmp) const
{
iexpPrinter(cmp.lhs);
logGlobal->debugStream() << " " << cmp.compSign << " ";
iexpPrinter(cmp.rhs);
}
void operator()(int const& flag) const
{
logGlobal->debugStream() << "condflag " << flag;
}
};
void conditionPrinter(const boost::optional<Tcondition> & cond)
{
if(cond.is_initialized())
{
Tcondition condp = cond.get();
logGlobal->debugStream() << " condition: ";
boost::apply_visitor(ConditionCondPrinterVisitor(), condp.cond);
logGlobal->debugStream() << " cond type: " << condp.ctype;
//recursive call
if(condp.rhs.is_initialized())
{
logGlobal->debugStream() << "rhs: ";
boost::optional<Tcondition> rhsc = condp.rhs.get().get();
conditionPrinter(rhsc);
}
else
{
logGlobal->debugStream() << "no rhs; ";
}
}
}
struct BodyVarpPrinterVisitor : boost::static_visitor<>
{
void operator()(TVarExpNotMacro const& cmp) const
{
if(cmp.questionMark.is_initialized())
{
logGlobal->debugStream() << cmp.questionMark.get();
}
if(cmp.val.is_initialized())
{
logGlobal->debugStream() << "val:" << cmp.val.get();
}
logGlobal->debugStream() << "varsym: |" << cmp.varsym << "|";
}
void operator()(TMacroUsage const& cmp) const
{
logGlobal->debugStream() << "???$$" << cmp.macro << "$$";
}
};
struct BodyOptionItemPrinterVisitor : boost::static_visitor<>
{
void operator()(TVarConcatString const& cmp) const
{
logGlobal->debugStream() << "+concat\"";
varPrinter(cmp.var);
logGlobal->debugStream() << " with " << cmp.string.str;
}
void operator()(TStringConstant const& cmp) const
{
logGlobal->debugStream() << " \"" << cmp.str << "\" ";
}
void operator()(TCurriedString const& cmp) const
{
logGlobal->debugStream() << "cs: ";
iexpPrinter(cmp.iexp);
logGlobal->debugStream() << " '" << cmp.string.str << "' ";
}
void operator()(TSemiCompare const& cmp) const
{
logGlobal->debugStream() << cmp.compSign << "; rhs: ";
iexpPrinter(cmp.rhs);
}
void operator()(TMacroUsage const& cmp) const
{
logGlobal->debugStream() << "$$" << cmp.macro << "$$";
}
void operator()(TMacroDef const& cmp) const
{
logGlobal->debugStream() << "@@" << cmp.macro << "@@";
}
void operator()(TIexp const& cmp) const
{
iexpPrinter(cmp);
}
void operator()(TVarpExp const& cmp) const
{
logGlobal->debugStream() << "varp";
boost::apply_visitor(BodyVarpPrinterVisitor(), cmp.var);
}
void operator()(spirit::unused_type const& cmp) const
{
logGlobal->debugStream() << "nothing";
}
};
struct BodyOptionVisitor : boost::static_visitor<>
{
void operator()(TVRLogic const& cmp) const
{
logGlobal->debugStream() << cmp.opcode << " ";
iexpPrinter(cmp.var);
}
void operator()(TVRArithmetic const& cmp) const
{
logGlobal->debugStream() << cmp.opcode << " ";
iexpPrinter(cmp.rhs);
}
void operator()(TNormalBodyOption const& cmp) const
{
logGlobal->debugStream() << cmp.optionCode << "~";
for (auto optList : cmp.params)
{
boost::apply_visitor(BodyOptionItemPrinterVisitor(), optList);
}
}
};
void bodyPrinter(const Tbody & body)
{
logGlobal->debugStream() << " body items: ";
for (auto bi: body)
{
logGlobal->debugStream() << " (";
apply_visitor(BodyOptionVisitor(), bi);
logGlobal->debugStream() << ") ";
}
}
struct CommandPrinterVisitor : boost::static_visitor<>
{
void operator()(Ttrigger const& trig) const
{
logGlobal->debugStream() << "trigger: " << trig.name << " ";
identifierPrinter(trig.identifier);
conditionPrinter(trig.condition);
}
void operator()(Tinstruction const& trig) const
{
logGlobal->debugStream() << "instruction: " << trig.name << " ";
identifierPrinter(trig.identifier);
conditionPrinter(trig.condition);
bodyPrinter(trig.body);
}
void operator()(Treceiver const& trig) const
{
logGlobal->debugStream() << "receiver: " << trig.name << " ";
identifierPrinter(trig.identifier);
conditionPrinter(trig.condition);
if(trig.body.is_initialized())
bodyPrinter(trig.body.get());
}
void operator()(TPostTrigger const& trig) const
{
logGlobal->debugStream() << "post trigger: " << trig.name << " ";
identifierPrinter(trig.identifier);
conditionPrinter(trig.condition);
}
};
struct LinePrinterVisitor : boost::static_visitor<>
{
void operator()(Tcommand const& cmd) const
{
CommandPrinterVisitor un;
boost::apply_visitor(un, cmd.cmd);
logGlobal->debugStream() << "Line comment: " << cmd.comment;
}
void operator()(std::string const& comment) const
{
}
void operator()(spirit::unused_type const& nothing) const
{
}
};
void printERM(const TERMline & ast)
{
logGlobal->debugStream() << "";
boost::apply_visitor(LinePrinterVisitor(), ast);
}
void printTVExp(const TVExp & exp);
struct VOptionPrinterVisitor : boost::static_visitor<>
{
void operator()(TVExp const& cmd) const
{
printTVExp(cmd);
}
void operator()(TSymbol const& cmd) const
{
for(auto mod : cmd.symModifier)
{
logGlobal->debugStream() << mod << " ";
}
logGlobal->debugStream() << cmd.sym;
}
void operator()(char const& cmd) const
{
logGlobal->debugStream() << "'" << cmd << "'";
}
void operator()(int const& cmd) const
{
logGlobal->debugStream() << cmd;
}
void operator()(double const& cmd) const
{
logGlobal->debugStream() << cmd;
}
void operator()(TERMline const& cmd) const
{
printERM(cmd);
}
void operator()(TStringConstant const& cmd) const
{
logGlobal->debugStream() << "^" << cmd.str << "^";
}
};
void printTVExp(const TVExp & exp)
{
for (auto mod: exp.modifier)
{
logGlobal->debugStream() << mod << " ";
}
logGlobal->debugStream() << "[ ";
for (auto opt: exp.children)
{
boost::apply_visitor(VOptionPrinterVisitor(), opt);
logGlobal->debugStream() << " ";
}
logGlobal->debugStream() << "]";
}
struct TLPrinterVisitor : boost::static_visitor<>
{
void operator()(TVExp const& cmd) const
{
printTVExp(cmd);
}
void operator()(TERMline const& cmd) const
{
printERM(cmd);
}
};
void printAST(const TLine & ast)
{
boost::apply_visitor(TLPrinterVisitor(), ast);
}
}
void ERMInterpreter::scanForScripts()
{
using namespace boost::filesystem;
//parser checking
const path dataPath = VCMIDirs::get().dataPaths().back() / "Data" / "s";
if(!exists(dataPath))
{
logGlobal->warnStream() << "Warning: Folder " << dataPath << " doesn't exist!";
return;
}
directory_iterator enddir;
for (directory_iterator dir(dataPath); dir!=enddir; dir++)
{
if(is_regular(dir->status()))
{
const std::string ext = boost::to_upper_copy(dir->path().extension().string());
if (ext == ".ERM" || ext == ".VERM")
{
ERMParser ep(dir->path().string());
FileInfo * finfo = new FileInfo;
finfo->filename = dir->path().string();
std::vector<LineInfo> buf = ep.parseFile();
finfo->length = buf.size();
files.push_back(finfo);
for(int g=0; g<buf.size(); ++g)
{
scripts[LinePointer(finfo, g, buf[g].realLineNum)] = buf[g].tl;
}
}
}
}
}
void ERMInterpreter::printScripts( EPrintMode mode /*= EPrintMode::ALL*/ )
{
std::map< LinePointer, ERM::TLine >::const_iterator prevIt;
for(std::map< LinePointer, ERM::TLine >::const_iterator it = scripts.begin(); it != scripts.end(); ++it)
{
if(it == scripts.begin() || it->first.file != prevIt->first.file)
{
logGlobal->debugStream() << "----------------- script " << it->first.file->filename << " ------------------";
}
logGlobal->debugStream() << it->first.realLineNum;
ERMPrinter::printAST(it->second);
prevIt = it;
}
}
struct ScriptScanner : boost::static_visitor<>
{
ERMInterpreter * interpreter;
LinePointer lp;
ScriptScanner(ERMInterpreter * interpr, const LinePointer & _lp) : interpreter(interpr), lp(_lp)
{}
void operator()(TVExp const& cmd) const
{
//
}
void operator()(TERMline const& cmd) const
{
if(cmd.which() == 0) //TCommand
{
Tcommand tcmd = boost::get<Tcommand>(cmd);
switch (tcmd.cmd.which())
{
case 0: //trigger
{
Trigger trig;
trig.line = lp;
interpreter->triggers[ TriggerType(boost::get<ERM::Ttrigger>(tcmd.cmd).name) ].push_back(trig);
}
break;
case 3: //post trigger
{
Trigger trig;
trig.line = lp;
interpreter->postTriggers[ TriggerType(boost::get<ERM::TPostTrigger>(tcmd.cmd).name) ].push_back(trig);
}
break;
default:
break;
}
}
}
};
void ERMInterpreter::scanScripts()
{
for(std::map< LinePointer, ERM::TLine >::const_iterator it = scripts.begin(); it != scripts.end(); ++it)
{
boost::apply_visitor(ScriptScanner(this, it->first), it->second);
}
}
ERMInterpreter::ERMInterpreter()
{
erm = this;
curFunc = nullptr;
curTrigger = nullptr;
globalEnv = new Environment();
topDyn = globalEnv;
}
void ERMInterpreter::executeTrigger( VERMInterpreter::Trigger & trig, int funNum /*= -1*/, std::vector<int> funParams/*=std::vector<int>()*/ )
{
//function-related logic
if(funNum != -1)
{
curFunc = getFuncVars(funNum);
for(int g=1; g<=FunctionLocalVars::NUM_PARAMETERS; ++g)
{
curFunc->getParam(g) = g-1 < funParams.size() ? funParams[g-1] : 0;
}
}
else
curFunc = getFuncVars(0);
//skip the first line
LinePointer lp = trig.line;
++lp;
for(; lp.isValid(); ++lp)
{
ERM::TLine curLine = retrieveLine(lp);
if(isATrigger(curLine))
break;
executeLine(lp);
}
curFunc = nullptr;
}
bool ERMInterpreter::isATrigger( const ERM::TLine & line )
{
switch(line.which())
{
case 0: //v-exp
{
TVExp vexp = boost::get<TVExp>(line);
if(vexp.children.size() == 0)
return false;
switch (getExpType(vexp.children[0]))
{
case SYMBOL:
{
//TODO: what about sym modifiers?
//TOOD: macros?
ERM::TSymbol sym = boost::get<ERM::TSymbol>(vexp.children[0]);
return sym.sym == triggerSymbol || sym.sym == postTriggerSymbol;
}
break;
case TCMD:
return isCMDATrigger( boost::get<ERM::Tcommand>(vexp.children[0]) );
break;
default:
return false;
break;
}
}
break;
case 1: //erm
{
TERMline ermline = boost::get<TERMline>(line);
switch(ermline.which())
{
case 0: //tcmd
return isCMDATrigger( boost::get<ERM::Tcommand>(ermline) );
break;
default:
return false;
break;
}
}
break;
default:
assert(0); //it should never happen
break;
}
assert(0);
return false;
}
ERM::EVOtions ERMInterpreter::getExpType( const ERM::TVOption & opt )
{
//MAINTENANCE: keep it correct!
return static_cast<ERM::EVOtions>(opt.which());
}
bool ERMInterpreter::isCMDATrigger( const ERM::Tcommand & cmd )
{
switch (cmd.cmd.which())
{
case 0: //trigger
case 3: //post trigger
return true;
break;
default:
return false;
break;
}
}
ERM::TLine &ERMInterpreter::retrieveLine( LinePointer linePtr )
{
return scripts.find(linePtr)->second;
}
/////////
//code execution
template<typename OwnerType>
struct StandardBodyOptionItemVisitor : boost::static_visitor<>
{
typedef OwnerType TReceiverType;
OwnerType & owner;
explicit StandardBodyOptionItemVisitor(OwnerType & _owner) : owner(_owner)
{}
virtual void operator()(TVarConcatString const& cmp) const
{
throw EScriptExecError("String concatenation not allowed in this receiver");
}
virtual void operator()(TStringConstant const& cmp) const
{
throw EScriptExecError("String constant not allowed in this receiver");
}
virtual void operator()(TCurriedString const& cmp) const
{
throw EScriptExecError("Curried string not allowed in this receiver");
}
virtual void operator()(TSemiCompare const& cmp) const
{
throw EScriptExecError("Semi comparison not allowed in this receiver");
}
// virtual void operator()(TMacroUsage const& cmp) const
// {
// throw EScriptExecError("Macro usage not allowed in this receiver");
// }
virtual void operator()(TMacroDef const& cmp) const
{
throw EScriptExecError("Macro definition not allowed in this receiver");
}
virtual void operator()(TIexp const& cmp) const
{
throw EScriptExecError("i-expression not allowed in this receiver");
}
virtual void operator()(TVarpExp const& cmp) const
{
throw EScriptExecError("Varp expression not allowed in this receiver");
}
virtual void operator()(spirit::unused_type const& cmp) const
{
throw EScriptExecError("\'Nothing\' not allowed in this receiver");
}
};
template<typename T>
struct StandardReceiverVisitor : boost::static_visitor<>
{
ERMInterpreter * interp;
T identifier;
StandardReceiverVisitor(ERMInterpreter * _interpr, T ident) : interp(_interpr), identifier(ident)
{}
virtual void operator()(TVRLogic const& trig) const
{
throw EScriptExecError("VR logic not allowed in this receiver!");
}
virtual void operator()(TVRArithmetic const& trig) const
{
throw EScriptExecError("VR arithmetic not allowed in this receiver!");
}
virtual void operator()(TNormalBodyOption const& trig) const = 0;
template<typename OptionPerformer>
void performOptionTakingOneParamter(const ERM::TNormalBodyOptionList & params) const
{
if(params.size() == 1)
{
ERM::TBodyOptionItem boi = params[0];
boost::apply_visitor(
OptionPerformer(*const_cast<typename OptionPerformer::TReceiverType*>(static_cast<const typename OptionPerformer::TReceiverType*>(this))), boi);
}
else
throw EScriptExecError("This receiver option takes exactly 1 parameter!");
}
template<template <int opcode> class OptionPerformer>
void performOptionTakingOneParamterWithIntDispatcher(const ERM::TNormalBodyOptionList & params) const
{
if(params.size() == 2)
{
int optNum = erm->getIexp(params[0]).getInt();
ERM::TBodyOptionItem boi = params[1];
switch(optNum)
{
case 0:
boost::apply_visitor(
OptionPerformer<0>(*const_cast<typename OptionPerformer<0>::TReceiverType*>(static_cast<const typename OptionPerformer<0>::TReceiverType*>(this))), boi);
break;
default:
throw EScriptExecError("Wrong number of option code!");
break;
}
}
else
throw EScriptExecError("This receiver option takes exactly 2 parameters!");
}
};
////HE
struct HEPerformer;
template<int opcode>
struct HE_BPerformer : StandardBodyOptionItemVisitor<HEPerformer>
{
explicit HE_BPerformer(HEPerformer & _owner) : StandardBodyOptionItemVisitor<HEPerformer>(_owner)
{}
using StandardBodyOptionItemVisitor<HEPerformer>::operator();
void operator()(TIexp const& cmp) const override;
void operator()(TVarpExp const& cmp) const override;
};
template<int opcode>
void HE_BPerformer<opcode>::operator()( TIexp const& cmp ) const
{
throw EScriptExecError("Setting hero name is not implemented!");
}
template<int opcode>
struct HE_CPerformer : StandardBodyOptionItemVisitor<HEPerformer>
{
explicit HE_CPerformer(HEPerformer & _owner) : StandardBodyOptionItemVisitor<HEPerformer>(_owner)
{}
using StandardBodyOptionItemVisitor<HEPerformer>::operator();
void operator()(TIexp const& cmp) const override;
void operator()(TVarpExp const& cmp) const override;
};
template<int opcode>
void HE_CPerformer<opcode>::operator()( TIexp const& cmp ) const
{
throw EScriptExecError("Setting hero army is not implemented!");
}
struct HEPerformer : StandardReceiverVisitor<const CGHeroInstance *>
{
HEPerformer(ERMInterpreter * _interpr, const CGHeroInstance * hero) : StandardReceiverVisitor<const CGHeroInstance *>(_interpr, hero)
{}
using StandardReceiverVisitor<const CGHeroInstance *>::operator();
void operator()(TNormalBodyOption const& trig) const override
{
switch(trig.optionCode)
{
case 'B':
{
performOptionTakingOneParamterWithIntDispatcher<HE_BPerformer>(trig.params);
}
break;
case 'C':
{
const ERM::TNormalBodyOptionList & params = trig.params;
if(params.size() == 4)
{
if(erm->getIexp(params[0]).getInt() == 0)
{
SlotID slot = SlotID(erm->getIexp(params[1]).getInt());
const CStackInstance *stack = identifier->getStackPtr(slot);
if(params[2].which() == 6) //varp
{
IexpValStr lhs = erm->getIexp(boost::get<ERM::TVarpExp>(params[2]));
if(stack)
lhs.setTo(stack->getCreatureID());
else
lhs.setTo(-1);
}
else
throw EScriptExecError("Setting stack creature type is not implemented!");
if(params[3].which() == 6) //varp
{
erm->getIexp(boost::get<ERM::TVarpExp>(params[3])).setTo(identifier->getStackCount(SlotID(slot)));
}
else
throw EScriptExecError("Setting stack count is not implemented!");
}
else
throw EScriptExecError("Slot number must be an evaluable i-exp");
}
//todo else if(14 params)
else
throw EScriptExecError("Slot number must be an evaluable i-exp");
}
break;
case 'E':
break;
case 'N':
break;
default:
break;
}
}
};
struct IFPerformer;
struct IF_MPerformer : StandardBodyOptionItemVisitor<IFPerformer>
{
explicit IF_MPerformer(IFPerformer & _owner) : StandardBodyOptionItemVisitor<IFPerformer>(_owner){}
using StandardBodyOptionItemVisitor<IFPerformer>::operator();
void operator()(TStringConstant const& cmp) const override;
};
//according to the ERM help:
//"%%" -> "%"
//"%V#" -> current value of # flag.
//"%Vf"..."%Vt" -> current value of corresponding variable.
//"%W1"..."%W100" -> current value of corresponding hero variable.
//"%X1"..."%X16" -> current value of corresponding function parameter.
//"%Y1"..."%Y100" -> current value of corresponding local variable.
//"%Z1"..."%Z500" -> current value of corresponding string variable.
//"%$macro$" -> macro name of corresponding variable
//"%Dd" -> current day of week
//"%Dw" -> current week
//"%Dm" -> current month
//"%Da" -> current day from beginning of the game
//"%Gc" -> the color of current gamer in text
struct StringFormatter
{
int pos;
int tokenLength;
size_t percentPos;
int charsToReplace;
std::string &msg;
StringFormatter(std::string &MSG) : pos(0), msg(MSG) {}
static void format(std::string &msg)
{
StringFormatter sf(msg);
sf.format();
}
// startpos is the first digit
// digits will be converted to number and returned
// ADDITIVE on digitsUsed
int getNum()
{
int toAdd = 0;
int numStart = percentPos + 2;
size_t numEnd = msg.find_first_not_of("1234567890", numStart);
if(numEnd == std::string::npos)
toAdd = msg.size() - numStart;
else
toAdd = numEnd - numStart;
charsToReplace += toAdd;
return boost::lexical_cast<int>(msg.substr(numStart, toAdd));
}
void format()
{
while(pos < msg.size())
{
percentPos = msg.find_first_of('%', pos);
charsToReplace = 1; //at least the same '%' needs to be replaced
std::ostringstream replaceWithWhat;
if(percentPos == std::string::npos) //processing done?
break;
if(percentPos + 1 >= msg.size()) //at least one character after % is required
throw EScriptExecError("Formatting error: % at the end of string!");
charsToReplace++; //the sign after % is consumed
switch(msg[percentPos+1])
{
case '%':
replaceWithWhat << '%';
break;
case 'F':
replaceWithWhat << erm->ermGlobalEnv->getFlag(getNum());
break;
case 'V':
if(std::isdigit(msg[percentPos + 2]))
replaceWithWhat << erm->ermGlobalEnv->getStandardVar(getNum());
else
{
charsToReplace++;
replaceWithWhat << erm->ermGlobalEnv->getQuickVar(msg[percentPos+2]);
}
break;
case 'X':
replaceWithWhat << erm->getVar("x", getNum()).getInt();
break;
case 'Z':
replaceWithWhat << erm->ermGlobalEnv->getZVar(getNum());
break;
default:
throw EScriptExecError("Formatting error: unrecognized token indicator after %!");
}
msg.replace(percentPos, charsToReplace, replaceWithWhat.str());
pos = percentPos + 1;
}
}
};
struct IFPerformer : StandardReceiverVisitor<TUnusedType>
{
IFPerformer(ERMInterpreter * _interpr) : StandardReceiverVisitor<TUnusedType>(_interpr, 0)
{}
using StandardReceiverVisitor<TUnusedType>::operator();
void operator()(TNormalBodyOption const& trig) const override
{
switch(trig.optionCode)
{
case 'M': //Show the message (Text) or contents of z$ variable on the screen immediately.
performOptionTakingOneParamter<IF_MPerformer>(trig.params);
break;
default:
break;
}
}
void showMessage(const std::string &msg)
{
std::string msgToFormat = msg;
StringFormatter::format(msgToFormat);
acb->showInfoDialog(msgToFormat, icb->getLocalPlayer());
}
};
void IF_MPerformer::operator()(TStringConstant const& cmp) const
{
owner.showMessage(cmp.str);
}
template<int opcode>
void HE_BPerformer<opcode>::operator()( TVarpExp const& cmp ) const
{
erm->getIexp(cmp).setTo(owner.identifier->name);
}
template<int opcode>
void HE_CPerformer<opcode>::operator()( TVarpExp const& cmp ) const
{
erm->getIexp(cmp).setTo(owner.identifier->name);
}
////MA
struct MAPerformer;
struct MA_PPerformer : StandardBodyOptionItemVisitor<MAPerformer>
{
explicit MA_PPerformer(MAPerformer & _owner);
using StandardBodyOptionItemVisitor<MAPerformer>::operator();
void operator()(TIexp const& cmp) const override;
void operator()(TVarpExp const& cmp) const override;
};
struct MAPerformer : StandardReceiverVisitor<TUnusedType>
{
MAPerformer(ERMInterpreter * _interpr) : StandardReceiverVisitor<TUnusedType>(_interpr, 0)
{}
using StandardReceiverVisitor<TUnusedType>::operator();
void operator()(TNormalBodyOption const& trig) const override
{
switch(trig.optionCode)
{
case 'A': //sgc monster attack
break;
case 'B': //spell?
break;
case 'P': //hit points
{
//TODO
}
break;
default:
break;
}
}
};
void MA_PPerformer::operator()( TIexp const& cmp ) const
{
}
void MA_PPerformer::operator()( TVarpExp const& cmp ) const
{
}
////MO
struct MOPerformer;
struct MO_GPerformer : StandardBodyOptionItemVisitor<MOPerformer>
{
explicit MO_GPerformer(MOPerformer & _owner) : StandardBodyOptionItemVisitor<MOPerformer>(_owner)
{}
using StandardBodyOptionItemVisitor<MOPerformer>::operator();
void operator()(TVarpExp const& cmp) const override;
void operator()(TIexp const& cmp) const override;
};
struct MOPerformer: StandardReceiverVisitor<int3>
{
MOPerformer(ERMInterpreter * _interpr, int3 pos) : StandardReceiverVisitor<int3>(_interpr, pos)
{}
using StandardReceiverVisitor<int3>::operator();
void operator()(TNormalBodyOption const& trig) const override
{
switch(trig.optionCode)