ERMInterpreter.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * ERMInterpreter.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "ERMParser.h"
  12. #include "ERMScriptModule.h"
  13. class ERMInterpreter;
  14. namespace VERMInterpreter
  15. {
  16. using namespace ERM;
  17. //different exceptions that can be thrown during interpreting
  18. class EInterpreterProblem : public std::exception
  19. {
  20. std::string problem;
  21. public:
  22. const char * what() const throw() override
  23. {
  24. return problem.c_str();
  25. }
  26. ~EInterpreterProblem() throw()
  27. {}
  28. EInterpreterProblem(const std::string & problemDesc) : problem(problemDesc)
  29. {}
  30. };
  31. struct ESymbolNotFound : public EInterpreterProblem
  32. {
  33. ESymbolNotFound(const std::string & sym) :
  34. EInterpreterProblem(std::string("Symbol \"") + sym + std::string("\" not found!"))
  35. {}
  36. };
  37. struct EInvalidTrigger : public EInterpreterProblem
  38. {
  39. EInvalidTrigger(const std::string & sym) :
  40. EInterpreterProblem(std::string("Trigger \"") + sym + std::string("\" is invalid!"))
  41. {}
  42. };
  43. struct EUsageOfUndefinedMacro : public EInterpreterProblem
  44. {
  45. EUsageOfUndefinedMacro(const std::string & macro) :
  46. EInterpreterProblem(std::string("Macro ") + macro + " is undefined")
  47. {}
  48. };
  49. struct EIexpProblem : public EInterpreterProblem
  50. {
  51. EIexpProblem(const std::string & desc) :
  52. EInterpreterProblem(desc)
  53. {}
  54. };
  55. struct ELineProblem : public EInterpreterProblem
  56. {
  57. ELineProblem(const std::string & desc) :
  58. EInterpreterProblem(desc)
  59. {}
  60. };
  61. struct EExecutionError : public EInterpreterProblem
  62. {
  63. EExecutionError(const std::string & desc) :
  64. EInterpreterProblem(desc)
  65. {}
  66. };
  67. //internal interpreter error related to execution
  68. struct EInterpreterError : public EExecutionError
  69. {
  70. EInterpreterError(const std::string & desc) :
  71. EExecutionError(desc)
  72. {}
  73. };
  74. //wrong script
  75. struct EScriptExecError : public EExecutionError
  76. {
  77. EScriptExecError(const std::string & desc) :
  78. EExecutionError(desc)
  79. {}
  80. };
  81. //wrong script
  82. struct EVermScriptExecError : public EScriptExecError
  83. {
  84. EVermScriptExecError(const std::string & desc) :
  85. EScriptExecError(desc)
  86. {}
  87. };
  88. // All numeric variables are integer variables and have a range of -2147483647...+2147483647
  89. // c stores game active day number //indirect variable
  90. // d current value //not an actual variable but a modifier
  91. // e1..e100 Function floating point variables //local
  92. // e-1..e-100 Trigger local floating point variables //local
  93. // 'f'..'t' Standard variables ('quick variables') //global
  94. // v1..v1000 Standard variables //global
  95. // w1..w100 Hero variables
  96. // w101..w200 Hero variables
  97. // x1..x16 Function parameters //local
  98. // y1..y100 Function local variables //local
  99. // y-1..y-100 Trigger-based local integer variables //local
  100. // z1..z1000 String variables //global
  101. // z-1..z-10 Function local string variables //local
  102. struct TriggerType
  103. {
  104. //the same order of trigger types in this enum and in validTriggers array is obligatory!
  105. enum ETrigType
  106. {
  107. AE, BA, BF, BG, BR, CM, CO, FU, GE, GM, HE, HL, HM, IP, LE, MF, MG, MM, MR, MW, OB, PI, SN, TH, TM
  108. };
  109. ETrigType type;
  110. static ETrigType convertTrigger(const std::string & trig)
  111. {
  112. static const std::string validTriggers[] =
  113. {
  114. "AE", "BA", "BF", "BG", "BR", "CM", "CO", "FU",
  115. "GE", "GM", "HE", "HL", "HM", "IP", "LE", "MF", "MG", "MM", "MR", "MW", "OB", "PI", "SN",
  116. "TH", "TM"
  117. };
  118. for(int i=0; i<std::size(validTriggers); ++i)
  119. {
  120. if(validTriggers[i] == trig)
  121. return static_cast<ETrigType>(i);
  122. }
  123. throw EInvalidTrigger(trig);
  124. }
  125. bool operator<(const TriggerType & t2) const
  126. {
  127. return type < t2.type;
  128. }
  129. TriggerType(const std::string & sym)
  130. {
  131. type = convertTrigger(sym);
  132. }
  133. };
  134. struct LinePointer
  135. {
  136. int lineNum;
  137. int realLineNum;
  138. int fileLength;
  139. LinePointer()
  140. : fileLength(-1)
  141. {}
  142. LinePointer(int _fileLength, int line, int _realLineNum)
  143. : fileLength(_fileLength),
  144. lineNum(line),
  145. realLineNum(_realLineNum)
  146. {}
  147. bool operator<(const LinePointer & rhs) const
  148. {
  149. return lineNum < rhs.lineNum;
  150. }
  151. bool operator!=(const LinePointer & rhs) const
  152. {
  153. return lineNum != rhs.lineNum;
  154. }
  155. LinePointer & operator++()
  156. {
  157. ++lineNum;
  158. return *this;
  159. }
  160. bool isValid() const
  161. {
  162. return fileLength > 0 && lineNum < fileLength;
  163. }
  164. };
  165. struct Trigger
  166. {
  167. LinePointer line;
  168. Trigger()
  169. {}
  170. };
  171. //verm goodies
  172. struct VSymbol
  173. {
  174. std::string text;
  175. VSymbol(const std::string & txt) : text(txt)
  176. {}
  177. };
  178. struct VNode;
  179. struct VOptionList;
  180. struct VNIL
  181. {};
  182. typedef std::variant<char, double, int, std::string> TLiteral;
  183. typedef std::variant<VNIL, boost::recursive_wrapper<VNode>, VSymbol, TLiteral, ERM::Tcommand> VOption; //options in v-expression, VNIl should be the default
  184. template<typename T, typename SecType>
  185. T& getAs(SecType & opt)
  186. {
  187. if(opt.type() == typeid(T))
  188. return std::get<T>(opt);
  189. else
  190. throw EVermScriptExecError("Wrong type!");
  191. }
  192. template<typename T, typename SecType>
  193. bool isA(const SecType & opt)
  194. {
  195. if(opt.type() == typeid(T))
  196. return true;
  197. else
  198. return false;
  199. }
  200. struct VermTreeIterator
  201. {
  202. private:
  203. friend struct VOptionList;
  204. VOptionList * parent;
  205. enum Estate {NORM, CAR} state;
  206. int basePos; //car/cdr offset
  207. public:
  208. VermTreeIterator(VOptionList & _parent) : parent(&_parent), state(NORM), basePos(0)
  209. {}
  210. VermTreeIterator() : parent(nullptr), state(NORM)
  211. {}
  212. VermTreeIterator & operator=(const VOption & opt);
  213. VermTreeIterator & operator=(const std::vector<VOption> & opt);
  214. VermTreeIterator & operator=(const VOptionList & opt);
  215. VOption & getAsItem();
  216. VOptionList getAsList();
  217. size_t size() const;
  218. VermTreeIterator& operator=(const VermTreeIterator & rhs)
  219. {
  220. if(this == &rhs)
  221. {
  222. return *this;
  223. }
  224. parent = rhs.parent;
  225. state = rhs.state;
  226. basePos = rhs.basePos;
  227. return *this;
  228. }
  229. };
  230. struct VOptionList : public std::vector<VOption>
  231. {
  232. private:
  233. friend struct VermTreeIterator;
  234. public:
  235. VermTreeIterator car();
  236. VermTreeIterator cdr();
  237. };
  238. struct VNode
  239. {
  240. private:
  241. void processModifierList(const std::vector<TVModifier> & modifierList, bool asSymbol);
  242. public:
  243. VOptionList children;
  244. VNode( const ERM::TVExp & exp);
  245. VNode( const VOptionList & cdren );
  246. VNode( const ERM::TSymbol & sym ); //only in case sym has modifiers!
  247. VNode( const VOption & first, const VOptionList & rest); //merges given arguments into [a, rest];
  248. void setVnode( const VOption & first, const VOptionList & rest);
  249. };
  250. }
  251. class ERMInterpreter
  252. {
  253. /*not so*/ public:
  254. std::map<VERMInterpreter::LinePointer, ERM::TLine> scripts;
  255. typedef std::map<VERMInterpreter::TriggerType, std::vector<VERMInterpreter::Trigger> > TtriggerListType;
  256. TtriggerListType triggers;
  257. TtriggerListType postTriggers;
  258. std::vector<VERMInterpreter::LinePointer> instructions;
  259. static bool isCMDATrigger(const ERM::Tcommand & cmd);
  260. static bool isATrigger(const ERM::TLine & line);
  261. static ERM::EVOptions getExpType(const ERM::TVOption & opt);
  262. ERM::TLine & retrieveLine(const VERMInterpreter::LinePointer & linePtr);
  263. static ERM::TTriggerBase & retrieveTrigger(ERM::TLine & line);
  264. public:
  265. vstd::CLoggerBase * logger;
  266. ERMInterpreter(vstd::CLoggerBase * logger_);
  267. virtual ~ERMInterpreter();
  268. std::string loadScript(const std::string & name, const std::string & source);
  269. };