ERMInterpreter.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755
  1. /*
  2. * ERMInterpreter.cpp, 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. #include "StdInc.h"
  11. #include "ERMInterpreter.h"
  12. #include <cctype>
  13. namespace spirit = boost::spirit;
  14. using ::scripting::ContextBase;
  15. using namespace ::VERMInterpreter;
  16. typedef int TUnusedType;
  17. namespace ERMConverter
  18. {
  19. //console printer
  20. using namespace ERM;
  21. static const std::map<std::string, std::string> CMP_OPERATION =
  22. {
  23. {"<", "<"},
  24. {">", ">"},
  25. {">=", ">="},
  26. {"=>", ">="},
  27. {"<=", "<="},
  28. {"=<", "<="},
  29. {"=", "=="},
  30. {"<>", "~="},
  31. {"><", "~="},
  32. };
  33. struct Variable
  34. {
  35. std::string name = "";
  36. std::string macro = "";
  37. int index = 0;
  38. Variable(const std::string & name_, int index_)
  39. {
  40. name = name_;
  41. index = index_;
  42. }
  43. Variable(const std::string & macro_)
  44. {
  45. macro = macro_;
  46. }
  47. bool isEmpty() const
  48. {
  49. return (name == "") && (macro == "");
  50. }
  51. bool isMacro() const
  52. {
  53. return (name == "") && (macro != "");
  54. }
  55. bool isSpecial() const
  56. {
  57. return (name.size() > 0) && (name[0] == 'd');
  58. }
  59. std::string str() const
  60. {
  61. if(isEmpty())
  62. {
  63. return std::to_string(index);
  64. }
  65. else if(isMacro())
  66. {
  67. return boost::to_string(boost::format("M['%s']") % macro);
  68. }
  69. else if(isSpecial() && (name.size() == 1))
  70. {
  71. boost::format fmt;
  72. fmt.parse("{'d', %d}");
  73. fmt % index;
  74. return fmt.str();
  75. }
  76. else if(isSpecial() && (name.size() != 1))
  77. {
  78. std::string ret;
  79. {
  80. boost::format fmt;
  81. if(index == 0)
  82. {
  83. fmt.parse("Q['%s']");
  84. fmt % name[name.size()-1];
  85. }
  86. else
  87. {
  88. fmt.parse("%s['%d']");
  89. fmt % name[name.size()-1] % index;
  90. }
  91. ret = fmt.str();
  92. }
  93. for(int i = ((int) name.size())-2; i > 0; i--)
  94. {
  95. boost::format fmt("%s[tostring(%s)]");
  96. fmt % name[i] % ret;
  97. ret = fmt.str();
  98. }
  99. {
  100. boost::format fmt;
  101. fmt.parse("{'d', %s}");
  102. fmt % ret;
  103. return fmt.str();
  104. }
  105. }
  106. else
  107. {
  108. std::string ret;
  109. {
  110. boost::format fmt;
  111. if(index == 0)
  112. {
  113. fmt.parse("Q['%s']");
  114. fmt % name[name.size()-1];
  115. }
  116. else
  117. {
  118. fmt.parse("%s['%d']");
  119. fmt % name[name.size()-1] % index;
  120. }
  121. ret = fmt.str();
  122. }
  123. for(int i = ((int) name.size())-2; i >= 0; i--)
  124. {
  125. boost::format fmt("%s[tostring(%s)]");
  126. fmt % name[i] % ret;
  127. ret = fmt.str();
  128. }
  129. return ret;
  130. }
  131. }
  132. };
  133. struct LVL2IexpToVar
  134. {
  135. LVL2IexpToVar() = default;
  136. Variable operator()(const TVarExpNotMacro & val) const
  137. {
  138. if(val.val.has_value())
  139. return Variable(val.varsym, val.val.get());
  140. else
  141. return Variable(val.varsym, 0);
  142. }
  143. Variable operator()(const TMacroUsage & val) const
  144. {
  145. return Variable(val.macro);
  146. }
  147. };
  148. struct LVL1IexpToVar
  149. {
  150. LVL1IexpToVar() = default;
  151. Variable operator()(const int & constant) const
  152. {
  153. return Variable("", constant);
  154. }
  155. Variable operator()(const TVarExp & var) const
  156. {
  157. return std::visit(LVL2IexpToVar(), var);
  158. }
  159. };
  160. struct Condition
  161. {
  162. std::string operator()(const TComparison & cmp) const
  163. {
  164. Variable lhs = std::visit(LVL1IexpToVar(), cmp.lhs);
  165. Variable rhs = std::visit(LVL1IexpToVar(), cmp.rhs);
  166. auto sign = CMP_OPERATION.find(cmp.compSign);
  167. if(sign == std::end(CMP_OPERATION))
  168. throw EScriptExecError(std::string("Wrong comparison sign: ") + cmp.compSign);
  169. boost::format fmt("(%s %s %s)");
  170. fmt % lhs.str() % sign->second % rhs.str();
  171. return fmt.str();
  172. }
  173. std::string operator()(const int & flag) const
  174. {
  175. return boost::to_string(boost::format("F['%d']") % flag);
  176. }
  177. };
  178. struct ParamIO
  179. {
  180. ParamIO() = default;
  181. std::string name = "";
  182. bool isInput = false;
  183. bool semi = false;
  184. std::string semiCmpSign = "";
  185. };
  186. struct Converter
  187. {
  188. mutable std::ostream * out;
  189. Converter(std::ostream * out_)
  190. : out(out_)
  191. {}
  192. protected:
  193. void put(const std::string & text) const
  194. {
  195. (*out) << text;
  196. }
  197. void putLine(const std::string & line) const
  198. {
  199. (*out) << line << std::endl;
  200. }
  201. void endLine() const
  202. {
  203. (*out) << std::endl;
  204. }
  205. };
  206. struct GetBodyOption
  207. {
  208. virtual std::string operator()(const TVarConcatString & cmp) const
  209. {
  210. throw EScriptExecError("String concatenation not allowed in this receiver");
  211. }
  212. virtual std::string operator()(const TStringConstant & cmp) const
  213. {
  214. throw EScriptExecError("String constant not allowed in this receiver");
  215. }
  216. virtual std::string operator()(const TCurriedString & cmp) const
  217. {
  218. throw EScriptExecError("Curried string not allowed in this receiver");
  219. }
  220. virtual std::string operator()(const TSemiCompare & cmp) const
  221. {
  222. throw EScriptExecError("Semi comparison not allowed in this receiver");
  223. }
  224. virtual std::string operator()(const TMacroDef & cmp) const
  225. {
  226. throw EScriptExecError("Macro definition not allowed in this receiver");
  227. }
  228. virtual std::string operator()(const TIexp & cmp) const
  229. {
  230. throw EScriptExecError("i-expression not allowed in this receiver");
  231. }
  232. virtual std::string operator()(const TVarpExp & cmp) const
  233. {
  234. throw EScriptExecError("Varp expression not allowed in this receiver");
  235. }
  236. virtual std::string operator()(const spirit::unused_type & cmp) const
  237. {
  238. throw EScriptExecError("\'Nothing\' not allowed in this receiver");
  239. }
  240. };
  241. struct BodyOption
  242. {
  243. ParamIO operator()(const TVarConcatString & cmp) const
  244. {
  245. throw EScriptExecError(std::string("String concatenation not allowed in this receiver|")+cmp.string.str+"|");
  246. }
  247. ParamIO operator()(const TStringConstant & cmp) const
  248. {
  249. boost::format fmt("[===[%s]===]");
  250. fmt % cmp.str;
  251. ParamIO ret;
  252. ret.isInput = true;
  253. ret.name = fmt.str();
  254. return ret;
  255. }
  256. ParamIO operator()(const TCurriedString & cmp) const
  257. {
  258. throw EScriptExecError("Curried string not allowed in this receiver");
  259. }
  260. ParamIO operator()(const TSemiCompare & cmp) const
  261. {
  262. ParamIO ret;
  263. ret.isInput = false;
  264. ret.semi = true;
  265. ret.semiCmpSign = cmp.compSign;
  266. ret.name = (std::visit(LVL1IexpToVar(), cmp.rhs)).str();
  267. return ret;
  268. }
  269. ParamIO operator()(const TMacroDef & cmp) const
  270. {
  271. throw EScriptExecError("Macro definition not allowed in this receiver");
  272. }
  273. ParamIO operator()(const TIexp & cmp) const
  274. {
  275. ParamIO ret;
  276. ret.isInput = true;
  277. ret.name = (std::visit(LVL1IexpToVar(), cmp)).str();;
  278. return ret;
  279. }
  280. ParamIO operator()(const TVarpExp & cmp) const
  281. {
  282. ParamIO ret;
  283. ret.isInput = false;
  284. ret.name = (std::visit(LVL2IexpToVar(), cmp.var)).str();
  285. return ret;
  286. }
  287. ParamIO operator()(const spirit::unused_type & cmp) const
  288. {
  289. throw EScriptExecError("\'Nothing\' not allowed in this receiver");
  290. }
  291. };
  292. struct Receiver : public Converter
  293. {
  294. Receiver(std::ostream * out_)
  295. : Converter(out_)
  296. {}
  297. virtual void operator()(const TVRLogic & trig) const
  298. {
  299. throw EInterpreterError("VR logic is not allowed in this receiver!");
  300. }
  301. virtual void operator()(const TVRArithmetic & trig) const
  302. {
  303. throw EInterpreterError("VR arithmetic is not allowed in this receiver!");
  304. }
  305. virtual void operator()(const TNormalBodyOption & trig) const
  306. {
  307. throw EInterpreterError("Normal body is not allowed in this receiver!");
  308. }
  309. };
  310. struct GenericReceiver : public Receiver
  311. {
  312. std::string name;
  313. bool specialSemiCompare = false;
  314. GenericReceiver(std::ostream * out_, const std::string & name_, bool specialSemiCompare_)
  315. : Receiver(out_),
  316. name(name_),
  317. specialSemiCompare(specialSemiCompare_)
  318. {}
  319. using Receiver::operator();
  320. void operator()(const TNormalBodyOption & trig) const override
  321. {
  322. std::string outParams;
  323. std::string inParams;
  324. std::string semiCompareDecl;
  325. std::vector<std::string> compares;
  326. bool hasOutput = false;
  327. bool hasSemiCompare = false;
  328. std::vector<ParamIO> optionParams;
  329. if(trig.params.has_value())
  330. {
  331. for(auto & p : trig.params.get())
  332. optionParams.push_back(std::visit(BodyOption(), p));
  333. }
  334. int idx = 1;
  335. int fidx = 1;
  336. for(const ParamIO & p : optionParams)
  337. {
  338. if(p.isInput)
  339. {
  340. if(outParams.empty())
  341. outParams = "_";
  342. else
  343. outParams += ", _";
  344. inParams += ", ";
  345. inParams += p.name;
  346. }
  347. else if(p.semi)
  348. {
  349. hasOutput = true;
  350. hasSemiCompare = true;
  351. std::string tempVar = std::string("s")+std::to_string(idx);
  352. if(semiCompareDecl.empty())
  353. {
  354. semiCompareDecl = "local "+tempVar;
  355. }
  356. else
  357. {
  358. semiCompareDecl += ", ";
  359. semiCompareDecl += tempVar;
  360. }
  361. if(outParams.empty())
  362. {
  363. outParams = tempVar;
  364. }
  365. else
  366. {
  367. outParams += ", ";
  368. outParams += tempVar;
  369. }
  370. inParams += ", nil";
  371. auto sign = CMP_OPERATION.find(p.semiCmpSign);
  372. if(sign == std::end(CMP_OPERATION))
  373. throw EScriptExecError(std::string("Wrong comparison sign: ") + p.semiCmpSign);
  374. boost::format cmpFmt("F[%d] = (%s %s %s)");
  375. cmpFmt % fidx % p.name % sign->second % tempVar;
  376. compares.push_back(cmpFmt.str());
  377. fidx++;
  378. }
  379. else
  380. {
  381. hasOutput = true;
  382. if(outParams.empty())
  383. {
  384. outParams = p.name;
  385. }
  386. else
  387. {
  388. outParams += ", ";
  389. outParams += p.name;
  390. }
  391. inParams += ", nil";
  392. }
  393. idx++;
  394. }
  395. if(hasSemiCompare)
  396. {
  397. putLine(semiCompareDecl);
  398. }
  399. boost::format callFormat;
  400. if(hasOutput)
  401. {
  402. callFormat.parse("%s = %s:%s(x%s)");
  403. callFormat % outParams;
  404. }
  405. else
  406. {
  407. callFormat.parse("%s:%s(x%s)");
  408. }
  409. callFormat % name;
  410. callFormat % trig.optionCode;
  411. callFormat % inParams;
  412. putLine(callFormat.str());
  413. for(auto & str : compares)
  414. putLine(str);
  415. }
  416. };
  417. struct FU : public Receiver
  418. {
  419. Variable v;
  420. FU(std::ostream * out_, const ERM::TIexp & tid)
  421. : Receiver(out_),
  422. v(std::visit(LVL1IexpToVar(), tid))
  423. {
  424. }
  425. FU(std::ostream * out_)
  426. : Receiver(out_),
  427. v("", 0)
  428. {
  429. }
  430. using Receiver::operator();
  431. void operator()(const TNormalBodyOption & trig) const override
  432. {
  433. switch(trig.optionCode)
  434. {
  435. case 'E':
  436. {
  437. putLine("do return end");
  438. }
  439. break;
  440. default:
  441. throw EInterpreterError("Unknown opcode in FU receiver");
  442. break;
  443. }
  444. }
  445. };
  446. struct MC_S : public GetBodyOption
  447. {
  448. MC_S()
  449. {}
  450. using GetBodyOption::operator();
  451. std::string operator()(const TMacroDef & cmp) const override
  452. {
  453. return cmp.macro;
  454. }
  455. };
  456. struct MC : public Receiver
  457. {
  458. Variable v;
  459. MC(std::ostream * out_, const ERM::TIexp & tid)
  460. : Receiver(out_),
  461. v(std::visit(LVL1IexpToVar(), tid))
  462. {
  463. }
  464. MC(std::ostream * out_)
  465. : Receiver(out_),
  466. v("", 0)
  467. {
  468. }
  469. using Receiver::operator();
  470. void operator()(const TNormalBodyOption & option) const override
  471. {
  472. switch(option.optionCode)
  473. {
  474. case 'S':
  475. {
  476. if(option.params.has_value())
  477. {
  478. for(auto & p : option.params.get())
  479. {
  480. std::string macroName = std::visit(MC_S(), p);
  481. boost::format callFormat;
  482. if(v.isEmpty())
  483. {
  484. callFormat.parse("ERM:addMacro('%s', 'v', '%s')");
  485. callFormat % macroName % macroName;
  486. }
  487. else
  488. {
  489. callFormat.parse("ERM:addMacro('%s', '%s', '%d')");
  490. callFormat % macroName % v.name % v.index;
  491. }
  492. putLine(callFormat.str());
  493. }
  494. }
  495. }
  496. break;
  497. default:
  498. throw EInterpreterError("Unknown opcode in MC receiver");
  499. break;
  500. }
  501. }
  502. };
  503. struct VR_S : public GetBodyOption
  504. {
  505. VR_S()
  506. {}
  507. using GetBodyOption::operator();
  508. std::string operator()(const TIexp & cmp) const override
  509. {
  510. auto v = std::visit(LVL1IexpToVar(), cmp);
  511. return v.str();
  512. }
  513. std::string operator()(const TStringConstant & cmp) const override
  514. {
  515. boost::format fmt("[===[%s]===]");
  516. fmt % cmp.str;
  517. return fmt.str();
  518. }
  519. };
  520. struct VR_H : public GetBodyOption
  521. {
  522. VR_H()
  523. {}
  524. using GetBodyOption::operator();
  525. std::string operator()(const TIexp & cmp) const override
  526. {
  527. Variable p = std::visit(LVL1IexpToVar(), cmp);
  528. if(p.index <= 0)
  529. throw EScriptExecError("VR:H requires flag index");
  530. if(p.name != "")
  531. throw EScriptExecError("VR:H accept only flag index");
  532. boost::format fmt("'%d'");
  533. fmt % p.index;
  534. return fmt.str();
  535. }
  536. };
  537. struct VR_X : public GetBodyOption
  538. {
  539. VR_X()
  540. {
  541. }
  542. using GetBodyOption::operator();
  543. std::string operator()(const TIexp & cmp) const override
  544. {
  545. Variable p = std::visit(LVL1IexpToVar(), cmp);
  546. return p.str();
  547. }
  548. };
  549. struct VR : public Receiver
  550. {
  551. Variable v;
  552. VR(std::ostream * out_, const ERM::TIexp & tid)
  553. : Receiver(out_),
  554. v(std::visit(LVL1IexpToVar(), tid))
  555. {
  556. }
  557. using Receiver::operator();
  558. void operator()(const TVRLogic & trig) const override
  559. {
  560. Variable rhs = std::visit(LVL1IexpToVar(), trig.var);
  561. std::string opcode;
  562. switch (trig.opcode)
  563. {
  564. case '&':
  565. opcode = "bit.band";
  566. break;
  567. case '|':
  568. opcode = "bit.bor";
  569. break;
  570. default:
  571. throw EInterpreterError("Wrong opcode in VR logic expression!");
  572. break;
  573. }
  574. boost::format fmt("%s = %s(%s, %s)");
  575. fmt % v.str() % opcode % v.str() % rhs.str();
  576. putLine(fmt.str());
  577. }
  578. void operator()(const TVRArithmetic & trig) const override
  579. {
  580. Variable rhs = std::visit(LVL1IexpToVar(), trig.rhs);
  581. std::string opcode;
  582. switch (trig.opcode)
  583. {
  584. case '+':
  585. opcode = v.name[0] == 'z' ? ".." : "+";
  586. break;
  587. case '-':
  588. case '*':
  589. case '%':
  590. opcode = trig.opcode;
  591. break;
  592. case ':':
  593. opcode = "/";
  594. break;
  595. default:
  596. throw EInterpreterError("Wrong opcode in VR arithmetic!");
  597. break;
  598. }
  599. boost::format fmt("%s = %s %s %s");
  600. fmt % v.str() % v.str() % opcode % rhs.str();
  601. putLine(fmt.str());
  602. }
  603. void operator()(const TNormalBodyOption & trig) const override
  604. {
  605. switch(trig.optionCode)
  606. {
  607. case 'C': //setting/checking v vars
  608. {
  609. if(v.index <= 0)
  610. throw EScriptExecError("VR:C requires indexed variable");
  611. std::vector<ParamIO> optionParams;
  612. if(trig.params.has_value())
  613. {
  614. for(auto & p : trig.params.get())
  615. optionParams.push_back(std::visit(BodyOption(), p));
  616. }
  617. auto index = v.index;
  618. for(auto & p : optionParams)
  619. {
  620. boost::format fmt;
  621. if(p.isInput)
  622. fmt.parse("%s['%d'] = %s") % v.name % index % p.name;
  623. else
  624. fmt.parse("%s = %s['%d']") % p.name % v.name % index;
  625. putLine(fmt.str());
  626. index++;
  627. }
  628. }
  629. break;
  630. case 'H': //checking if string is empty
  631. {
  632. if(!trig.params.has_value() || trig.params.get().size() != 1)
  633. throw EScriptExecError("VR:H option takes exactly 1 parameter!");
  634. std::string opt = std::visit(VR_H(), trig.params.get()[0]);
  635. boost::format fmt("ERM.VR(%s):H(%s)");
  636. fmt % v.str() % opt;
  637. putLine(fmt.str());
  638. }
  639. break;
  640. case 'U':
  641. {
  642. if(!trig.params.has_value() || trig.params.get().size() != 1)
  643. throw EScriptExecError("VR:H/U need 1 parameter!");
  644. std::string opt = std::visit(VR_S(), trig.params.get()[0]);
  645. boost::format fmt("ERM.VR(%s):%c(%s)");
  646. fmt % v.str() % (trig.optionCode) % opt;
  647. putLine(fmt.str());
  648. }
  649. break;
  650. case 'M': //string operations
  651. {
  652. if(!trig.params.has_value() || trig.params.get().size() < 2)
  653. throw EScriptExecError("VR:M needs at least 2 parameters!");
  654. std::string opt = std::visit(VR_X(), trig.params.get()[0]);
  655. int paramIndex = 1;
  656. if(opt == "3")
  657. {
  658. boost::format fmt("%s = ERM.VR(%s):M3(");
  659. fmt % v.str() % v.str();
  660. put(fmt.str());
  661. }
  662. else
  663. {
  664. auto target = std::visit(VR_X(), trig.params.get()[paramIndex++]);
  665. boost::format fmt("%s = ERM.VR(%s):M%s(");
  666. fmt % target % v.str() % opt;
  667. put(fmt.str());
  668. }
  669. for(int i = paramIndex; i < trig.params.get().size(); i++)
  670. {
  671. opt = std::visit(VR_X(), trig.params.get()[i]);
  672. if(i > paramIndex) put(",");
  673. put(opt);
  674. }
  675. putLine(")");
  676. }
  677. break;
  678. case 'X': //bit xor
  679. {
  680. if(!trig.params.has_value() || trig.params.get().size() != 1)
  681. throw EScriptExecError("VR:X option takes exactly 1 parameter!");
  682. std::string opt = std::visit(VR_X(), trig.params.get()[0]);
  683. boost::format fmt("%s = bit.bxor(%s, %s)");
  684. fmt % v.str() % v.str() % opt;putLine(fmt.str());
  685. }
  686. break;
  687. case 'R': //random variables
  688. {
  689. //TODO
  690. putLine("--VR:R not implemented");
  691. }
  692. break;
  693. case 'S': //setting variable
  694. {
  695. if(!trig.params.has_value() || trig.params.get().size() != 1)
  696. throw EScriptExecError("VR:S option takes exactly 1 parameter!");
  697. std::string opt = std::visit(VR_S(), trig.params.get()[0]);
  698. put(v.str());
  699. put(" = ");
  700. put(opt);
  701. endLine();
  702. }
  703. break;
  704. case 'T': //random variables
  705. {
  706. //TODO
  707. putLine("--VR:T not implemented");
  708. }
  709. break;
  710. case 'V': //convert string to value
  711. {
  712. if(!trig.params.has_value() || trig.params.get().size() != 1)
  713. throw EScriptExecError("VR:V option takes exactly 1 parameter!");
  714. std::string opt = std::visit(VR_X(), trig.params.get()[0]);
  715. boost::format fmt("%s = tostring(%s)");
  716. fmt % v.str() % opt;
  717. putLine(fmt.str());
  718. }
  719. break;
  720. default:
  721. throw EScriptExecError("Wrong VR receiver option!");
  722. break;
  723. }
  724. }
  725. };
  726. struct ERMExp : public Converter
  727. {
  728. ERMExp(std::ostream * out_)
  729. : Converter(out_)
  730. {}
  731. template <typename Visitor>
  732. void performBody(const std::optional<ERM::Tbody> & body, const Visitor & visitor) const
  733. {
  734. if(body.has_value())
  735. {
  736. const ERM::Tbody & bo = body.get();
  737. for(int g=0; g<bo.size(); ++g)
  738. {
  739. std::visit(visitor, bo[g]);
  740. }
  741. }
  742. }
  743. void convert(const std::string & name, const std::optional<Tidentifier> & identifier, const std::optional<Tbody> & body) const
  744. {
  745. if(name == "VR")
  746. {
  747. if(!identifier.has_value())
  748. throw EScriptExecError("VR receiver requires arguments");
  749. ERM::Tidentifier tid = identifier.value();
  750. if(tid.size() != 1)
  751. throw EScriptExecError("VR receiver takes exactly 1 argument");
  752. performBody(body, VR(out, tid[0]));
  753. }
  754. else if(name == "re")
  755. {
  756. if(!identifier.has_value())
  757. throw EScriptExecError("re receiver requires arguments");
  758. ERM::Tidentifier tid = identifier.value();
  759. auto argc = tid.size();
  760. if(argc > 0)
  761. {
  762. std::string loopCounter = (std::visit(LVL1IexpToVar(), tid.at(0))).str();
  763. std::string startVal = argc > 1 ? (std::visit(LVL1IexpToVar(), tid.at(1))).str() : loopCounter;
  764. std::string stopVal = argc > 2 ? (std::visit(LVL1IexpToVar(), tid.at(2))).str() : loopCounter;
  765. std::string increment = argc > 3 ? (std::visit(LVL1IexpToVar(), tid.at(3))).str() : "1";
  766. boost::format fmt("for __iter = %s, %s, %s do");
  767. fmt % startVal % stopVal % increment;
  768. putLine(fmt.str());
  769. fmt.parse("%s = __iter");
  770. fmt % loopCounter;
  771. putLine(fmt.str());
  772. }
  773. else
  774. {
  775. throw EScriptExecError("re receiver requires arguments");
  776. }
  777. }
  778. else if(name == "FU" && !identifier.has_value())
  779. {
  780. performBody(body, FU(out)); //assume FU:E
  781. }
  782. else if(name == "MC")
  783. {
  784. if(identifier.has_value())
  785. {
  786. ERM::Tidentifier tid = identifier.value();
  787. if(tid.size() != 1)
  788. throw EScriptExecError("MC receiver takes no more than 1 argument");
  789. performBody(body, MC(out, tid[0]));
  790. }
  791. else
  792. {
  793. performBody(body, MC(out));
  794. }
  795. }
  796. else
  797. {
  798. std::vector<std::string> identifiers;
  799. if(identifier.has_value())
  800. {
  801. for(const auto & id : identifier.value())
  802. {
  803. Variable v = std::visit(LVL1IexpToVar(), id);
  804. if(v.isSpecial())
  805. throw ELineProblem("Special variable syntax ('d') is not allowed in receiver identifier");
  806. identifiers.push_back(v.str());
  807. }
  808. }
  809. std::string params;
  810. for(auto iter = std::begin(identifiers); iter != std::end(identifiers); ++iter)
  811. {
  812. if(!params.empty())
  813. params += ", ";
  814. params += *iter;
  815. }
  816. if(body.has_value())
  817. {
  818. const ERM::Tbody & bo = body.get();
  819. if(bo.size() == 1)
  820. {
  821. boost::format fmt("ERM.%s(%s)");
  822. fmt % name;
  823. fmt % params;
  824. GenericReceiver gr(out, fmt.str(), (name == "DO"));
  825. bo[0].apply_visitor(gr);
  826. }
  827. else
  828. {
  829. putLine("do");
  830. boost::format fmt("local %s = ERM.%s(%s)");
  831. fmt % name;
  832. fmt % name;
  833. fmt % params;
  834. putLine(fmt.str());
  835. performBody(body, GenericReceiver(out, name, (name=="DO") ));
  836. putLine("end");
  837. }
  838. }
  839. else
  840. {
  841. //is it an error?
  842. logMod->warn("ERM receiver '%s %s' w/o body", name, params);
  843. }
  844. }
  845. }
  846. void convertConditionInner(const Tcondition & cond, char op) const
  847. {
  848. std::string lhs = std::visit(Condition(), cond.cond);
  849. if(cond.ctype != '/')
  850. op = cond.ctype;
  851. switch (op)
  852. {
  853. case '&':
  854. put(" and ");
  855. break;
  856. case '|':
  857. put(" or ");
  858. break;
  859. default:
  860. throw EInterpreterProblem(std::string("Wrong condition connection (") + cond.ctype + ") !");
  861. break;
  862. }
  863. put(lhs);
  864. if(cond.rhs.has_value())
  865. {
  866. switch (op)
  867. {
  868. case '&':
  869. case '|':
  870. break;
  871. default:
  872. throw EInterpreterProblem(std::string("Wrong condition connection (") + cond.ctype + ") !");
  873. break;
  874. }
  875. convertConditionInner(cond.rhs.get().get(), op);
  876. }
  877. }
  878. void convertCondition(const Tcondition & cond) const
  879. {
  880. //&c1/c2/c3|c4/c5/c6 -> (c1 & c2 & c3) | c4 | c5 | c6
  881. std::string lhs = std::visit(Condition(), cond.cond);
  882. put("if ");
  883. put(lhs);
  884. if(cond.rhs.has_value())
  885. {
  886. switch (cond.ctype)
  887. {
  888. case '&':
  889. case '|':
  890. break;
  891. default:
  892. throw EInterpreterProblem(std::string("Wrong condition connection (") + cond.ctype + ") !");
  893. break;
  894. }
  895. convertConditionInner(cond.rhs.get().get(), cond.ctype);
  896. }
  897. putLine(" then ");
  898. }
  899. void convertReceiverOrInstruction(const std::optional<Tcondition> & condition,
  900. const std::string & name,
  901. const std::optional<Tidentifier> & identifier,
  902. const std::optional<Tbody> & body) const
  903. {
  904. if(name=="if")
  905. {
  906. if(condition.has_value())
  907. convertCondition(condition.get());
  908. else
  909. putLine("if true then");
  910. }
  911. else if(name=="el")
  912. {
  913. putLine("else");
  914. }
  915. else if(name=="en")
  916. {
  917. putLine("end");
  918. }
  919. else
  920. {
  921. if(condition.has_value())
  922. {
  923. convertCondition(condition.get());
  924. convert(name, identifier, body);
  925. putLine("end");
  926. }
  927. else
  928. {
  929. convert(name, identifier, body);
  930. }
  931. }
  932. }
  933. void operator()(const Ttrigger & trig) const
  934. {
  935. throw EInterpreterError("Triggers cannot be executed!");
  936. }
  937. void operator()(const TPostTrigger & trig) const
  938. {
  939. throw EInterpreterError("Post-triggers cannot be executed!");
  940. }
  941. void operator()(const Tinstruction & trig) const
  942. {
  943. convertReceiverOrInstruction(trig.condition, trig.name, trig.identifier, std::make_optional(trig.body));
  944. }
  945. void operator()(const Treceiver & trig) const
  946. {
  947. convertReceiverOrInstruction(trig.condition, trig.name, trig.identifier, trig.body);
  948. }
  949. };
  950. struct Command : public Converter
  951. {
  952. Command(std::ostream * out_)
  953. : Converter(out_)
  954. {}
  955. void operator()(const Tcommand & cmd) const
  956. {
  957. std::visit(ERMExp(out), cmd.cmd);
  958. }
  959. void operator()(const std::string & comment) const
  960. {
  961. (*out) << "-- " << comment;
  962. endLine();
  963. }
  964. void operator()(const spirit::unused_type &) const
  965. {
  966. }
  967. };
  968. struct TLiteralEval
  969. {
  970. std::string operator()(const char & val)
  971. {
  972. return "{\"'\",'"+ std::to_string(val) +"'}";
  973. }
  974. std::string operator()(const double & val)
  975. {
  976. return std::to_string(val);
  977. }
  978. std::string operator()(const int & val)
  979. {
  980. return std::to_string(val);
  981. }
  982. std::string operator()(const std::string & val)
  983. {
  984. return "{\"'\",[===[" + val + "]===]}";
  985. }
  986. };
  987. struct VOptionEval : public Converter
  988. {
  989. VOptionEval(std::ostream * out_)
  990. : Converter(out_)
  991. {}
  992. void operator()(VNIL const & opt) const
  993. {
  994. (*out) << "{}";
  995. }
  996. void operator()(const VNode & opt) const;
  997. void operator()(const VSymbol & opt) const
  998. {
  999. (*out) << "\"" << opt.text << "\"";
  1000. }
  1001. void operator()(const TLiteral & opt) const
  1002. {
  1003. TLiteralEval tmp;
  1004. (*out) << std::visit(tmp, opt);
  1005. }
  1006. void operator()(ERM const ::Tcommand & opt) const
  1007. {
  1008. //this is how FP works, evaluation == producing side effects
  1009. //TODO: can we evaluate to smth more useful?
  1010. //???
  1011. throw EVermScriptExecError("Using ERM options in VERM expression is not (yet) allowed");
  1012. // std::visit(ERMExp(out), opt.cmd);
  1013. }
  1014. };
  1015. void VOptionEval::operator()(const VNode & opt) const
  1016. {
  1017. VNode tmpn(opt);
  1018. (*out) << "{";
  1019. for(VOption & op : tmpn.children)
  1020. {
  1021. std::visit(VOptionEval(out), op);
  1022. (*out) << ",";
  1023. }
  1024. (*out) << "}";
  1025. }
  1026. struct Line : public Converter
  1027. {
  1028. Line(std::ostream * out_)
  1029. : Converter(out_)
  1030. {}
  1031. void operator()(const TVExp & cmd) const
  1032. {
  1033. put("VERM:E");
  1034. VNode line(cmd);
  1035. VOptionEval eval(out);
  1036. eval(line);
  1037. endLine();
  1038. }
  1039. void operator()(const TERMline & cmd) const
  1040. {
  1041. std::visit(Command(out), cmd);
  1042. }
  1043. };
  1044. void convertInstructions(std::ostream & out, ERMInterpreter * owner)
  1045. {
  1046. out << "local function instructions()" << std::endl;
  1047. out << "local e, x, y = {}, {}, {}" << std::endl;
  1048. Line lineConverter(&out);
  1049. for(const LinePointer & lp : owner->instructions)
  1050. {
  1051. ERM::TLine & line = owner->retrieveLine(lp);
  1052. std::visit(lineConverter, line);
  1053. }
  1054. out << "end" << std::endl;
  1055. }
  1056. void convertFunctions(std::ostream & out, ERMInterpreter * owner, const std::vector<VERMInterpreter::Trigger> & triggers)
  1057. {
  1058. Line lineConverter(&out);
  1059. for(const VERMInterpreter::Trigger & trigger : triggers)
  1060. {
  1061. ERM::TLine & firstLine = owner->retrieveLine(trigger.line);
  1062. const ERM::TTriggerBase & trig = ERMInterpreter::retrieveTrigger(firstLine);
  1063. //TODO: condition
  1064. out << "ERM:addTrigger({" << std::endl;
  1065. if(!trig.identifier.has_value())
  1066. throw EInterpreterError("Function must have identifier");
  1067. ERM::Tidentifier tid = trig.identifier.value();
  1068. if(tid.empty())
  1069. throw EInterpreterError("Function must have identifier");
  1070. Variable v = std::visit(LVL1IexpToVar(), tid[0]);
  1071. if(v.isSpecial())
  1072. throw ELineProblem("Special variable syntax ('d') is not allowed in function definition");
  1073. out << "id = {" << v.str() << "}," << std::endl;
  1074. out << "name = 'FU'," << std::endl;
  1075. out << "fn = function (e, y, x)" << std::endl;
  1076. out << "local _" << std::endl;
  1077. LinePointer lp = trigger.line;
  1078. ++lp;
  1079. for(; lp.isValid(); ++lp)
  1080. {
  1081. ERM::TLine curLine = owner->retrieveLine(lp);
  1082. if(owner->isATrigger(curLine))
  1083. break;
  1084. std::visit(lineConverter, curLine);
  1085. }
  1086. out << "end," << std::endl;
  1087. out << "})" << std::endl;
  1088. }
  1089. }
  1090. void convertTriggers(std::ostream & out, ERMInterpreter * owner, const VERMInterpreter::TriggerType & type, const std::vector<VERMInterpreter::Trigger> & triggers)
  1091. {
  1092. Line lineConverter(&out);
  1093. for(const VERMInterpreter::Trigger & trigger : triggers)
  1094. {
  1095. ERM::TLine & firstLine = owner->retrieveLine(trigger.line);
  1096. const ERM::TTriggerBase & trig = ERMInterpreter::retrieveTrigger(firstLine);
  1097. //TODO: condition
  1098. out << "ERM:addTrigger({" << std::endl;
  1099. std::vector<std::string> identifiers;
  1100. if(trig.identifier.has_value())
  1101. {
  1102. for(const auto & id : trig.identifier.value())
  1103. {
  1104. Variable v = std::visit(LVL1IexpToVar(), id);
  1105. if(v.isSpecial())
  1106. throw ELineProblem("Special variable syntax ('d') is not allowed in trigger definition");
  1107. identifiers.push_back(v.str());
  1108. }
  1109. }
  1110. out << "id = {";
  1111. for(const auto & id : identifiers)
  1112. out << "'" << id << "',";
  1113. out << "}," << std::endl;
  1114. out << "name = '" << trig.name << "'," << std::endl;
  1115. out << "fn = function (e, y)" << std::endl;
  1116. out << "local _" << std::endl;
  1117. LinePointer lp = trigger.line;
  1118. ++lp;
  1119. for(; lp.isValid(); ++lp)
  1120. {
  1121. ERM::TLine curLine = owner->retrieveLine(lp);
  1122. if(owner->isATrigger(curLine))
  1123. break;
  1124. std::visit(lineConverter, curLine);
  1125. }
  1126. out << "end," << std::endl;
  1127. out << "})" << std::endl;
  1128. }
  1129. }
  1130. }
  1131. struct ScriptScanner
  1132. {
  1133. ERMInterpreter * interpreter;
  1134. LinePointer lp;
  1135. ScriptScanner(ERMInterpreter * interpr, const LinePointer & _lp) : interpreter(interpr), lp(_lp)
  1136. {}
  1137. void operator()(const TVExp & cmd) const
  1138. {
  1139. //
  1140. }
  1141. void operator()(const TERMline & cmd) const
  1142. {
  1143. if(cmd.which() == 0) //TCommand
  1144. {
  1145. Tcommand tcmd = std::get<Tcommand>(cmd);
  1146. switch (tcmd.cmd.which())
  1147. {
  1148. case 0: //trigger
  1149. {
  1150. Trigger trig;
  1151. trig.line = lp;
  1152. interpreter->triggers[ TriggerType(std::get<ERM::Ttrigger>(tcmd.cmd).name) ].push_back(trig);
  1153. }
  1154. break;
  1155. case 1: //instruction
  1156. {
  1157. interpreter->instructions.push_back(lp);
  1158. }
  1159. break;
  1160. case 3: //post trigger
  1161. {
  1162. Trigger trig;
  1163. trig.line = lp;
  1164. interpreter->postTriggers[ TriggerType(std::get<ERM::TPostTrigger>(tcmd.cmd).name) ].push_back(trig);
  1165. }
  1166. break;
  1167. default:
  1168. break;
  1169. }
  1170. }
  1171. }
  1172. };
  1173. ERMInterpreter::ERMInterpreter(vstd::CLoggerBase * logger_)
  1174. : logger(logger_)
  1175. {
  1176. }
  1177. ERMInterpreter::~ERMInterpreter()
  1178. {
  1179. }
  1180. bool ERMInterpreter::isATrigger( const ERM::TLine & line )
  1181. {
  1182. switch(line.which())
  1183. {
  1184. case 0: //v-exp
  1185. {
  1186. TVExp vexp = std::get<TVExp>(line);
  1187. if(vexp.children.empty())
  1188. return false;
  1189. switch (getExpType(vexp.children[0]))
  1190. {
  1191. case SYMBOL:
  1192. return false;
  1193. break;
  1194. case TCMD:
  1195. return isCMDATrigger( std::get<ERM::Tcommand>(vexp.children[0]) );
  1196. break;
  1197. default:
  1198. return false;
  1199. break;
  1200. }
  1201. }
  1202. break;
  1203. case 1: //erm
  1204. {
  1205. TERMline ermline = std::get<TERMline>(line);
  1206. switch(ermline.which())
  1207. {
  1208. case 0: //tcmd
  1209. return isCMDATrigger( std::get<ERM::Tcommand>(ermline) );
  1210. break;
  1211. default:
  1212. return false;
  1213. break;
  1214. }
  1215. }
  1216. break;
  1217. default:
  1218. assert(0); //it should never happen
  1219. break;
  1220. }
  1221. assert(0);
  1222. return false;
  1223. }
  1224. ERM::EVOtions ERMInterpreter::getExpType(const ERM::TVOption & opt)
  1225. {
  1226. //MAINTENANCE: keep it correct!
  1227. return static_cast<ERM::EVOtions>(opt.which());
  1228. }
  1229. bool ERMInterpreter::isCMDATrigger(const ERM::Tcommand & cmd)
  1230. {
  1231. switch (cmd.cmd.which())
  1232. {
  1233. case 0: //trigger
  1234. case 3: //post trigger
  1235. return true;
  1236. break;
  1237. default:
  1238. return false;
  1239. break;
  1240. }
  1241. }
  1242. ERM::TLine & ERMInterpreter::retrieveLine(const LinePointer & linePtr)
  1243. {
  1244. return scripts.find(linePtr)->second;
  1245. }
  1246. ERM::TTriggerBase & ERMInterpreter::retrieveTrigger(ERM::TLine & line)
  1247. {
  1248. if(line.which() == 1)
  1249. {
  1250. ERM::TERMline &tl = std::get<ERM::TERMline>(line);
  1251. if(tl.which() == 0)
  1252. {
  1253. ERM::Tcommand &tcm = std::get<ERM::Tcommand>(tl);
  1254. if(tcm.cmd.which() == 0)
  1255. {
  1256. return std::get<ERM::Ttrigger>(tcm.cmd);
  1257. }
  1258. else if(tcm.cmd.which() == 3)
  1259. {
  1260. return std::get<ERM::TPostTrigger>(tcm.cmd);
  1261. }
  1262. throw ELineProblem("Given line is not a trigger!");
  1263. }
  1264. throw ELineProblem("Given line is not a command!");
  1265. }
  1266. throw ELineProblem("Given line is not an ERM trigger!");
  1267. }
  1268. std::string ERMInterpreter::loadScript(const std::string & name, const std::string & source)
  1269. {
  1270. CERMPreprocessor preproc(source);
  1271. const bool isVERM = preproc.version == CERMPreprocessor::Version::VERM;
  1272. ERMParser ep;
  1273. std::vector<LineInfo> buf = ep.parseFile(preproc);
  1274. for(int g=0; g<buf.size(); ++g)
  1275. scripts[LinePointer(static_cast<int>(buf.size()), g, buf[g].realLineNum)] = buf[g].tl;
  1276. for(auto p : scripts)
  1277. std::visit(ScriptScanner(this, p.first), p.second);
  1278. std::stringstream out;
  1279. out << "local ERM = require(\"core:erm\")" << std::endl;
  1280. if(isVERM)
  1281. {
  1282. out << "local VERM = require(\"core:verm\")" << std::endl;
  1283. }
  1284. out << "local _" << std::endl;
  1285. out << "local v, w, z, F, M, Q = ERM.v, ERM.w, ERM.z, ERM.F, ERM.M, ERM.Q" << std::endl;
  1286. ERMConverter::convertInstructions(out, this);
  1287. for(const auto & p : triggers)
  1288. {
  1289. const VERMInterpreter::TriggerType & tt = p.first;
  1290. if(tt.type == VERMInterpreter::TriggerType::FU)
  1291. {
  1292. ERMConverter::convertFunctions(out, this, p.second);
  1293. }
  1294. else
  1295. {
  1296. ERMConverter::convertTriggers(out, this, tt, p.second);
  1297. }
  1298. }
  1299. for(const auto & p : postTriggers)
  1300. ;//TODO:postTriggers
  1301. out << "ERM:callInstructions(instructions)" << std::endl;
  1302. return out.str();
  1303. }
  1304. namespace VERMInterpreter
  1305. {
  1306. VOption convertToVOption(const ERM::TVOption & tvo)
  1307. {
  1308. return std::visit(OptionConverterVisitor(), tvo);
  1309. }
  1310. VNode::VNode( const ERM::TVExp & exp )
  1311. {
  1312. for(int i=0; i<exp.children.size(); ++i)
  1313. {
  1314. children.push_back(convertToVOption(exp.children[i]));
  1315. }
  1316. processModifierList(exp.modifier, false);
  1317. }
  1318. VNode::VNode( const VOption & first, const VOptionList & rest ) /*merges given arguments into [a, rest] */
  1319. {
  1320. setVnode(first, rest);
  1321. }
  1322. VNode::VNode( const VOptionList & cdren ) : children(cdren)
  1323. {}
  1324. VNode::VNode( const ERM::TSymbol & sym )
  1325. {
  1326. children.car() = VSymbol(sym.sym);
  1327. processModifierList(sym.symModifier, true);
  1328. }
  1329. void VNode::setVnode( const VOption & first, const VOptionList & rest )
  1330. {
  1331. children.car() = first;
  1332. children.cdr() = rest;
  1333. }
  1334. void VNode::processModifierList( const std::vector<TVModifier> & modifierList, bool asSymbol )
  1335. {
  1336. for(int g=0; g<modifierList.size(); ++g)
  1337. {
  1338. if(asSymbol)
  1339. {
  1340. children.resize(children.size()+1);
  1341. for(auto i=children.size()-1; i >0; i--)
  1342. {
  1343. children[i] = children[i-1];
  1344. }
  1345. }
  1346. else
  1347. {
  1348. children.cdr() = VNode(children);
  1349. }
  1350. if(modifierList[g] == "`")
  1351. {
  1352. children.car() = VSymbol("`");
  1353. }
  1354. else if(modifierList[g] == ",!")
  1355. {
  1356. children.car() = VSymbol("comma-unlist");
  1357. }
  1358. else if(modifierList[g] == ",")
  1359. {
  1360. children.car() = VSymbol(",");
  1361. }
  1362. else if(modifierList[g] == "#'")
  1363. {
  1364. children.car() = VSymbol("get-func");
  1365. }
  1366. else if(modifierList[g] == "'")
  1367. {
  1368. children.car() = VSymbol("'");
  1369. }
  1370. else
  1371. throw EInterpreterError("Incorrect value of modifier!");
  1372. }
  1373. }
  1374. VermTreeIterator & VermTreeIterator::operator=( const VOption & opt )
  1375. {
  1376. switch (state)
  1377. {
  1378. case CAR:
  1379. if(parent->size() <= basePos)
  1380. parent->push_back(opt);
  1381. else
  1382. (*parent)[basePos] = opt;
  1383. break;
  1384. case NORM:
  1385. parent->resize(basePos+1);
  1386. (*parent)[basePos] = opt;
  1387. break;
  1388. default://should never happen
  1389. break;
  1390. }
  1391. return *this;
  1392. }
  1393. VermTreeIterator & VermTreeIterator::operator=( const std::vector<VOption> & opt )
  1394. {
  1395. switch (state)
  1396. {
  1397. case CAR:
  1398. //TODO: implement me
  1399. break;
  1400. case NORM:
  1401. parent->resize(basePos+1);
  1402. parent->insert(parent->begin()+basePos, opt.begin(), opt.end());
  1403. break;
  1404. default://should never happen
  1405. break;
  1406. }
  1407. return *this;
  1408. }
  1409. VermTreeIterator & VermTreeIterator::operator=( const VOptionList & opt )
  1410. {
  1411. return *this = opt;
  1412. }
  1413. VOption & VermTreeIterator::getAsItem()
  1414. {
  1415. if(state == CAR)
  1416. return (*parent)[basePos];
  1417. else
  1418. throw EInterpreterError("iterator is not in car state, cannot get as list");
  1419. }
  1420. size_t VermTreeIterator::size() const
  1421. {
  1422. return parent->size() - basePos;
  1423. }
  1424. VERMInterpreter::VOptionList VermTreeIterator::getAsList()
  1425. {
  1426. VOptionList ret;
  1427. for(int g = basePos; g<parent->size(); ++g)
  1428. {
  1429. ret.push_back((*parent)[g]);
  1430. }
  1431. return ret;
  1432. }
  1433. VOption OptionConverterVisitor::operator()(ERM const ::TVExp & cmd) const
  1434. {
  1435. return VNode(cmd);
  1436. }
  1437. VOption OptionConverterVisitor::operator()(ERM const ::TSymbol & cmd) const
  1438. {
  1439. if(cmd.symModifier.empty())
  1440. return VSymbol(cmd.sym);
  1441. else
  1442. return VNode(cmd);
  1443. }
  1444. VOption OptionConverterVisitor::operator()(const char & cmd) const
  1445. {
  1446. return TLiteral(cmd);
  1447. }
  1448. VOption OptionConverterVisitor::operator()(const double & cmd) const
  1449. {
  1450. return TLiteral(cmd);
  1451. }
  1452. VOption OptionConverterVisitor::operator()(const int & cmd) const
  1453. {
  1454. return TLiteral(cmd);
  1455. }
  1456. VOption OptionConverterVisitor::operator()(ERM const ::Tcommand & cmd) const
  1457. {
  1458. return cmd;
  1459. }
  1460. VOption OptionConverterVisitor::operator()(ERM const ::TStringConstant & cmd) const
  1461. {
  1462. return TLiteral(cmd.str);
  1463. }
  1464. VermTreeIterator VOptionList::cdr()
  1465. {
  1466. VermTreeIterator ret(*this);
  1467. ret.basePos = 1;
  1468. return ret;
  1469. }
  1470. VermTreeIterator VOptionList::car()
  1471. {
  1472. VermTreeIterator ret(*this);
  1473. ret.state = VermTreeIterator::CAR;
  1474. return ret;
  1475. }
  1476. }