ERMInterpreter.cpp 36 KB

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