ERMInterpreter.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. #include "ERMInterpreter.h"
  2. #include <boost/filesystem.hpp>
  3. #include <boost/algorithm/string.hpp>
  4. #include <boost/foreach.hpp>
  5. /*
  6. * ERMInterpreter.cpp, part of VCMI engine
  7. *
  8. * Authors: listed in file AUTHORS in main folder
  9. *
  10. * License: GNU General Public License v2.0 or later
  11. * Full text of license available in license.txt file, in main folder
  12. *
  13. */
  14. namespace spirit = boost::spirit;
  15. namespace ERMPrinter
  16. {
  17. //console printer
  18. using namespace ERM;
  19. struct VarPrinterVisitor : boost::static_visitor<>
  20. {
  21. void operator()(TVarExpNotMacro const& val) const
  22. {
  23. tlog2 << val.varsym;
  24. if(val.val.is_initialized())
  25. {
  26. tlog2 << val.val.get();
  27. }
  28. }
  29. void operator()(TMacroUsage const& val) const
  30. {
  31. tlog2 << "$" << val.macro << "&";
  32. }
  33. };
  34. void varPrinter(const TVarExp & var)
  35. {
  36. boost::apply_visitor(VarPrinterVisitor(), var);
  37. }
  38. struct IExpPrinterVisitor : boost::static_visitor<>
  39. {
  40. void operator()(int const & constant) const
  41. {
  42. tlog2 << constant;
  43. }
  44. void operator()(TVarExp const & var) const
  45. {
  46. varPrinter(var);
  47. }
  48. };
  49. void iexpPrinter(const TIexp & exp)
  50. {
  51. boost::apply_visitor(IExpPrinterVisitor(), exp);
  52. }
  53. struct IdentifierPrinterVisitor : boost::static_visitor<>
  54. {
  55. void operator()(TIexp const& iexp) const
  56. {
  57. iexpPrinter(iexp);
  58. }
  59. void operator()(TArithmeticOp const& arop) const
  60. {
  61. iexpPrinter(arop.lhs);
  62. tlog2 << " " << arop.opcode << " ";
  63. iexpPrinter(arop.rhs);
  64. }
  65. };
  66. void identifierPrinter(const boost::optional<Tidentifier> & id)
  67. {
  68. if(id.is_initialized())
  69. {
  70. tlog2 << "identifier: ";
  71. BOOST_FOREACH(TIdentifierInternal x, id.get())
  72. {
  73. tlog2 << "#";
  74. boost::apply_visitor(IdentifierPrinterVisitor(), x);
  75. }
  76. }
  77. }
  78. struct ConditionCondPrinterVisitor : boost::static_visitor<>
  79. {
  80. void operator()(TComparison const& cmp) const
  81. {
  82. iexpPrinter(cmp.lhs);
  83. tlog2 << " " << cmp.compSign << " ";
  84. iexpPrinter(cmp.rhs);
  85. }
  86. void operator()(int const& flag) const
  87. {
  88. tlog2 << "condflag " << flag;
  89. }
  90. };
  91. void conditionPrinter(const boost::optional<Tcondition> & cond)
  92. {
  93. if(cond.is_initialized())
  94. {
  95. Tcondition condp = cond.get();
  96. tlog2 << " condition: ";
  97. boost::apply_visitor(ConditionCondPrinterVisitor(), condp.cond);
  98. tlog2 << " cond type: " << condp.ctype;
  99. //recursive call
  100. if(condp.rhs.is_initialized())
  101. {
  102. tlog2 << "rhs: ";
  103. boost::optional<Tcondition> rhsc = condp.rhs.get().get();
  104. conditionPrinter(rhsc);
  105. }
  106. else
  107. {
  108. tlog2 << "no rhs; ";
  109. }
  110. }
  111. }
  112. struct BodyVarpPrinterVisitor : boost::static_visitor<>
  113. {
  114. void operator()(TVarExpNotMacro const& cmp) const
  115. {
  116. if(cmp.questionMark.is_initialized())
  117. {
  118. tlog2 << cmp.questionMark.get();
  119. }
  120. if(cmp.val.is_initialized())
  121. {
  122. tlog2 << "val:" << cmp.val.get();
  123. }
  124. tlog2 << "varsym: |" << cmp.varsym << "|";
  125. }
  126. void operator()(TQMacroUsage const& cmp) const
  127. {
  128. tlog2 << "???$$" << cmp.qmacro << "$$";
  129. }
  130. };
  131. struct BodyOptionItemPrinterVisitor : boost::static_visitor<>
  132. {
  133. void operator()(TVarConcatString const& cmp) const
  134. {
  135. tlog2 << "+concat\"";
  136. varPrinter(cmp.var);
  137. tlog2 << " with " << cmp.string.str;
  138. }
  139. void operator()(TStringConstant const& cmp) const
  140. {
  141. tlog2 << " \"" << cmp.str << "\" ";
  142. }
  143. void operator()(TCurriedString const& cmp) const
  144. {
  145. tlog2 << "cs: ";
  146. iexpPrinter(cmp.iexp);
  147. tlog2 << " '" << cmp.string.str << "' ";
  148. }
  149. void operator()(TSemiCompare const& cmp) const
  150. {
  151. tlog2 << cmp.compSign << "; rhs: ";
  152. iexpPrinter(cmp.rhs);
  153. }
  154. void operator()(TMacroUsage const& cmp) const
  155. {
  156. tlog2 << "$$" << cmp.macro << "$$";
  157. }
  158. void operator()(TMacroDef const& cmp) const
  159. {
  160. tlog2 << "@@" << cmp.macro << "@@";
  161. }
  162. void operator()(TIexp const& cmp) const
  163. {
  164. iexpPrinter(cmp);
  165. }
  166. void operator()(TVarpExp const& cmp) const
  167. {
  168. tlog2 << "varp";
  169. boost::apply_visitor(BodyVarpPrinterVisitor(), cmp.var);
  170. }
  171. void operator()(spirit::unused_type const& cmp) const
  172. {
  173. tlog2 << "nothing";
  174. }
  175. };
  176. struct BodyOptionVisitor : boost::static_visitor<>
  177. {
  178. void operator()(TVRLogic const& cmp) const
  179. {
  180. tlog2 << cmp.opcode << " ";
  181. iexpPrinter(cmp.var);
  182. }
  183. void operator()(TVRArithmetic const& cmp) const
  184. {
  185. tlog2 << cmp.opcode << " ";
  186. iexpPrinter(cmp.rhs);
  187. }
  188. void operator()(TNormalBodyOption const& cmp) const
  189. {
  190. tlog2 << cmp.optionCode << "~";
  191. BOOST_FOREACH(TBodyOptionItem optList, cmp.params)
  192. {
  193. boost::apply_visitor(BodyOptionItemPrinterVisitor(), optList);
  194. }
  195. }
  196. };
  197. void bodyPrinter(const Tbody & body)
  198. {
  199. tlog2 << " body items: ";
  200. BOOST_FOREACH(TBodyOption bi, body)
  201. {
  202. tlog2 << " (";
  203. apply_visitor(BodyOptionVisitor(), bi);
  204. tlog2 << ") ";
  205. }
  206. }
  207. struct CommandPrinterVisitor : boost::static_visitor<>
  208. {
  209. void operator()(Ttrigger const& trig) const
  210. {
  211. tlog2 << "trigger: " << trig.name << " ";
  212. identifierPrinter(trig.identifier);
  213. conditionPrinter(trig.condition);
  214. }
  215. void operator()(Tinstruction const& trig) const
  216. {
  217. tlog2 << "instruction: " << trig.name << " ";
  218. identifierPrinter(trig.identifier);
  219. conditionPrinter(trig.condition);
  220. bodyPrinter(trig.body);
  221. }
  222. void operator()(Treceiver const& trig) const
  223. {
  224. tlog2 << "receiver: " << trig.name << " ";
  225. identifierPrinter(trig.identifier);
  226. conditionPrinter(trig.condition);
  227. if(trig.body.is_initialized())
  228. bodyPrinter(trig.body.get());
  229. }
  230. void operator()(TPostTrigger const& trig) const
  231. {
  232. tlog2 << "post trigger: " << trig.name << " ";
  233. identifierPrinter(trig.identifier);
  234. conditionPrinter(trig.condition);
  235. }
  236. };
  237. struct LinePrinterVisitor : boost::static_visitor<>
  238. {
  239. void operator()(Tcommand const& cmd) const
  240. {
  241. CommandPrinterVisitor un;
  242. boost::apply_visitor(un, cmd.cmd);
  243. std::cout << "Line comment: " << cmd.comment << std::endl;
  244. }
  245. void operator()(std::string const& comment) const
  246. {
  247. }
  248. void operator()(spirit::unused_type const& nothing) const
  249. {
  250. }
  251. };
  252. void printERM(const TERMline & ast)
  253. {
  254. tlog2 << "";
  255. boost::apply_visitor(LinePrinterVisitor(), ast);
  256. }
  257. void printTVExp(const TVExp & exp);
  258. struct VOptionPrinterVisitor : boost::static_visitor<>
  259. {
  260. void operator()(TVExp const& cmd) const
  261. {
  262. printTVExp(cmd);
  263. }
  264. void operator()(TSymbol const& cmd) const
  265. {
  266. BOOST_FOREACH(TVModifier mod, cmd.symModifier)
  267. {
  268. tlog2 << mod << " ";
  269. }
  270. tlog2 << cmd.sym;
  271. }
  272. void operator()(char const& cmd) const
  273. {
  274. tlog2 << "'" << cmd << "'";
  275. }
  276. void operator()(int const& cmd) const
  277. {
  278. tlog2 << cmd;
  279. }
  280. void operator()(double const& cmd) const
  281. {
  282. tlog2 << cmd;
  283. }
  284. void operator()(TERMline const& cmd) const
  285. {
  286. printERM(cmd);
  287. }
  288. void operator()(TStringConstant const& cmd) const
  289. {
  290. tlog2 << "^" << cmd.str << "^";
  291. }
  292. };
  293. void printTVExp(const TVExp & exp)
  294. {
  295. BOOST_FOREACH(TVModifier mod, exp.modifier)
  296. {
  297. tlog2 << mod << " ";
  298. }
  299. tlog2 << "[ ";
  300. BOOST_FOREACH(TVOption opt, exp.children)
  301. {
  302. boost::apply_visitor(VOptionPrinterVisitor(), opt);
  303. tlog2 << " ";
  304. }
  305. tlog2 << "]";
  306. }
  307. struct TLPrinterVisitor : boost::static_visitor<>
  308. {
  309. void operator()(TVExp const& cmd) const
  310. {
  311. printTVExp(cmd);
  312. }
  313. void operator()(TERMline const& cmd) const
  314. {
  315. printERM(cmd);
  316. }
  317. };
  318. void printAST(const TLine & ast)
  319. {
  320. boost::apply_visitor(TLPrinterVisitor(), ast);
  321. tlog2 << std::endl;
  322. }
  323. }
  324. void ERMInterpreter::scanForScripts()
  325. {
  326. using namespace boost::filesystem;
  327. //parser checking
  328. if(!exists(DATA_DIR "/Data/s/"))
  329. {
  330. tlog3 << "Warning: Folder " DATA_DIR "/Data/s/ doesn't exist!\n";
  331. return;
  332. }
  333. directory_iterator enddir;
  334. for (directory_iterator dir(DATA_DIR "/Data/s"); dir!=enddir; dir++)
  335. {
  336. if(is_regular(dir->status()))
  337. {
  338. std::string name = dir->path().leaf();
  339. if( boost::algorithm::ends_with(name, ".erm") ||
  340. boost::algorithm::ends_with(name, ".verm") )
  341. {
  342. ERMParser ep(dir->path().string());
  343. scripts[name] = ep.parseFile();
  344. }
  345. }
  346. }
  347. }
  348. void ERMInterpreter::printScripts( EPrintMode mode /*= EPrintMode::ALL*/ )
  349. {
  350. for(std::map<std::string, std::vector<ERM::TLine> >::const_iterator it = scripts.begin(); it != scripts.end(); ++it)
  351. {
  352. tlog2 << "----------------- script " << it->first << " ------------------\n";
  353. for(int i=0; i<it->second.size(); ++i)
  354. {
  355. ERMPrinter::printAST(it->second[i]);
  356. }
  357. }
  358. }