code_generator.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. #include "code_generator.hpp"
  2. string generate(vector<string> &tokens, int &i, map<string, string> &aliases, map<string, Pointer*> &pointers,
  3. int &line_num, int &current_cell, bool &error)
  4. {
  5. string bfcode = "";
  6. if (tokens[i] == "newline")
  7. {
  8. ++line_num;
  9. return bfcode;
  10. }
  11. if (tokens[i] == "read")
  12. {
  13. return ",";
  14. }
  15. if (tokens[i] == "reads")
  16. {
  17. string come_back;
  18. int lenght;
  19. try
  20. {
  21. ++i;
  22. lenght = stoi(tokens[i]);
  23. }
  24. catch(invalid_argument)
  25. {
  26. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")<<"invalid argument for 'reads'"
  27. <<" (line "<<line_num<<")\n";
  28. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
  29. error = true;
  30. return bfcode;
  31. }
  32. for (int i = 0; i < lenght; i++)
  33. {
  34. bfcode += ",>";
  35. come_back += "<";
  36. }
  37. return bfcode + "\n" + come_back + "\n";
  38. }
  39. if (tokens[i] == "sub")
  40. {
  41. int p1, p2, resultpos;
  42. ++i;
  43. if (tokens[i] == "loadpointer")
  44. {
  45. try
  46. {
  47. ++i;
  48. p1 = pointers[tokens[i]]->start;
  49. }
  50. catch(out_of_range)
  51. {
  52. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")<<"invalid argument 1 for 'sum'"
  53. <<" (line "<<line_num<<")\n";
  54. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
  55. error = true;
  56. return bfcode;
  57. }
  58. }
  59. else
  60. {
  61. try
  62. {
  63. ++i;
  64. p1 = stoi(tokens[i]);
  65. }
  66. catch (invalid_argument)
  67. {
  68. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")<<"invalid argument 1 for 'sum'"
  69. <<" (line "<<line_num<<")\n";
  70. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
  71. error = true;
  72. return bfcode;
  73. }
  74. }
  75. ++i;
  76. if (tokens[i] == "loadpointer")
  77. {
  78. try
  79. {
  80. ++i;
  81. p2 = pointers[tokens[i]]->start;
  82. }
  83. catch(out_of_range)
  84. {
  85. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")<<"invalid argument 2 for 'sum'"
  86. <<" (line "<<line_num<<")\n";
  87. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
  88. error = true;
  89. return bfcode;
  90. }
  91. }
  92. else
  93. {
  94. try
  95. {
  96. ++i;
  97. p2 = stoi(tokens[i]);
  98. }
  99. catch (invalid_argument)
  100. {
  101. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")<<"invalid argument 2 for 'sum'"
  102. <<" (line "<<line_num<<")\n";
  103. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
  104. error = true;
  105. return bfcode;
  106. }
  107. }
  108. ++i;
  109. if (tokens[i] == "loadpointer")
  110. {
  111. try
  112. {
  113. ++i;
  114. resultpos = pointers[tokens[i]]->start;
  115. }
  116. catch(out_of_range)
  117. {
  118. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")<<"invalid argument 3 for 'sum'"
  119. <<" (line "<<line_num<<")\n";
  120. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
  121. error = true;
  122. return bfcode;
  123. }
  124. }
  125. else
  126. {
  127. try
  128. {
  129. ++i;
  130. resultpos = stoi(tokens[i]);
  131. }
  132. catch (invalid_argument)
  133. {
  134. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")<<"invalid argument 3 for 'sum'"
  135. <<" (line "<<line_num<<")\n";
  136. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
  137. error = true;
  138. return bfcode;
  139. }
  140. }
  141. return sub(p1, p2, resultpos, current_cell);
  142. }
  143. if (tokens[i] == "sum")
  144. {
  145. int p1, p2, resultpos;
  146. ++i;
  147. if (tokens[i] == "loadpointer")
  148. {
  149. try
  150. {
  151. ++i;
  152. p1 = pointers[tokens[i]]->start;
  153. }
  154. catch(out_of_range)
  155. {
  156. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")<<"invalid argument 1 for 'sum'"
  157. <<" (line "<<line_num<<")\n";
  158. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
  159. error = true;
  160. return bfcode;
  161. }
  162. }
  163. else
  164. {
  165. try
  166. {
  167. ++i;
  168. p1 = stoi(tokens[i]);
  169. }
  170. catch (invalid_argument)
  171. {
  172. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")<<"invalid argument 1 for 'sum'"
  173. <<" (line "<<line_num<<")\n";
  174. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
  175. error = true;
  176. return bfcode;
  177. }
  178. }
  179. ++i;
  180. if (tokens[i] == "loadpointer")
  181. {
  182. try
  183. {
  184. ++i;
  185. p2 = pointers[tokens[i]]->start;
  186. }
  187. catch(out_of_range)
  188. {
  189. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")<<"invalid argument 2 for 'sum'"
  190. <<" (line "<<line_num<<")\n";
  191. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
  192. error = true;
  193. return bfcode;
  194. }
  195. }
  196. else
  197. {
  198. try
  199. {
  200. ++i;
  201. p2 = stoi(tokens[i]);
  202. }
  203. catch (invalid_argument)
  204. {
  205. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")<<"invalid argument 2 for 'sum'"
  206. <<" (line "<<line_num<<")\n";
  207. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
  208. error = true;
  209. return bfcode;
  210. }
  211. }
  212. ++i;
  213. if (tokens[i] == "loadpointer")
  214. {
  215. try
  216. {
  217. ++i;
  218. resultpos = pointers[tokens[i]]->start;
  219. }
  220. catch(out_of_range)
  221. {
  222. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")<<"invalid argument 3 for 'sum'"
  223. <<" (line "<<line_num<<")\n";
  224. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
  225. error = true;
  226. return bfcode;
  227. }
  228. }
  229. else
  230. {
  231. try
  232. {
  233. ++i;
  234. resultpos = stoi(tokens[i]);
  235. }
  236. catch (invalid_argument)
  237. {
  238. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")<<"invalid argument 3 for 'sum'"
  239. <<" (line "<<line_num<<")\n";
  240. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
  241. error = true;
  242. return bfcode;
  243. }
  244. }
  245. return sum(p1, p2, resultpos, current_cell);
  246. }
  247. if (tokens[i] == "putchar")
  248. {
  249. return ".";
  250. }
  251. if (tokens[i] == "pointer")
  252. {
  253. ++i;
  254. try
  255. {
  256. pointers[tokens[i]] = new Pointer("pointer", stoi(tokens[i+1]));
  257. ++i;
  258. return bfcode;
  259. }
  260. catch(invalid_argument err)
  261. {
  262. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")<<"invalid argument for 'pointer'"
  263. <<" (line "<<line_num<<")\n";
  264. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
  265. error = true;
  266. return bfcode;
  267. }
  268. }
  269. if (tokens[i] == "array")
  270. {
  271. ++i;
  272. try
  273. {
  274. pointers[tokens[i]] = new Pointer("array", stoi(tokens[i+1]), stoi(tokens[i+1]) + stoi(tokens[i+2]));
  275. ++i;
  276. ++i;
  277. return bfcode;
  278. }
  279. catch(invalid_argument err)
  280. {
  281. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")<<"invalid argument(s) for 'array'"
  282. <<" (line "<<line_num<<")\n";
  283. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
  284. error = true;
  285. return bfcode;
  286. }
  287. }
  288. if (tokens[i] == "loadalias")
  289. {
  290. try
  291. {
  292. ++i;
  293. tokens[i] = aliases.at(tokens[i]);
  294. return bfcode;
  295. }
  296. catch(out_of_range)
  297. {
  298. cout<<BOLD_MSG("bfscript2: code generator: ")<<ERROR_MSG("name error: ")
  299. <<"alias '"<<tokens[i]<<"' is not defined (line "<<line_num<<")\n";
  300. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
  301. error = true;
  302. return bfcode;
  303. }
  304. }
  305. if (tokens[i] == "bypointer")
  306. {
  307. ++i;
  308. Pointer *pointer = nullptr;
  309. try
  310. {
  311. pointer = pointers.at(tokens[i]);
  312. }
  313. catch(out_of_range)
  314. {
  315. cout<<BOLD_MSG("bfscript: code generator")<<ERROR_MSG("name error: ")<<"pointer '"<<tokens[i]
  316. <<"' is not defined (line "<<line_num<<")\n";
  317. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
  318. error = true;
  319. return bfcode;
  320. }
  321. ++i;
  322. if (tokens[i] == "assigment")
  323. {
  324. bfcode += go_to(current_cell, pointer->start);
  325. ++i;
  326. bfcode += setchar(tokens[i][0]);
  327. bfcode += go_to(pointer->start, current_cell);
  328. return bfcode;
  329. }
  330. if (tokens[i] == "clear")
  331. {
  332. bfcode += go_to(current_cell, pointer->start);
  333. ++i;
  334. bfcode += "[-]\n";
  335. bfcode += go_to(pointer->start, current_cell);
  336. return bfcode;
  337. }
  338. if (tokens[i] == "add")
  339. {
  340. bfcode += go_to(current_cell, pointer->start);
  341. ++i;
  342. try
  343. {
  344. bfcode += change(stoi(tokens[i]));
  345. bfcode += go_to(pointer->start, current_cell);
  346. return bfcode;
  347. }
  348. catch (invalid_argument)
  349. {
  350. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")
  351. <<"invalid argument for pointer's 'add' (line "<<line_num<<")\n";
  352. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
  353. error = true;
  354. return bfcode;
  355. }
  356. }
  357. if (tokens[i] == "sub")
  358. {
  359. bfcode += go_to(current_cell, pointer->start);
  360. ++i;
  361. try
  362. {
  363. bfcode += change(-stoi(tokens[i]));
  364. bfcode += go_to(pointer->start, current_cell);
  365. return bfcode;
  366. }
  367. catch (invalid_argument)
  368. {
  369. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")
  370. <<"invalid argument for pointer's 'sub' (line "<<line_num<<")\n";
  371. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
  372. error = true;
  373. return bfcode;
  374. }
  375. }
  376. }
  377. if (tokens[i] == "byarray")
  378. {
  379. ++i;
  380. Pointer *pointer = nullptr;
  381. try
  382. {
  383. pointer = pointers.at(tokens[i]);
  384. }
  385. catch(out_of_range)
  386. {
  387. cout<<BOLD_MSG("bfscript: code generator")<<ERROR_MSG("name error: ")<<"pointer '"<<tokens[i]
  388. <<"' is not defined (line "<<line_num<<")\n";
  389. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
  390. error = true;
  391. return bfcode;
  392. }
  393. ++i;
  394. int at;
  395. try
  396. {
  397. at = pointer->start + stoi(tokens[i]);
  398. if (at > pointer->end || at < pointer->start)
  399. {
  400. cout<<BOLD_MSG("bfscript: code generator")<<ERROR_MSG("out of range error: ")<<"array index out of range"
  401. <<" (line "<<line_num<<")\n";
  402. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
  403. error = true;
  404. return bfcode;
  405. }
  406. }
  407. catch(invalid_argument)
  408. {
  409. cout<<BOLD_MSG("bfscript: code generator")<<ERROR_MSG("argument error: ")<<"invalid argument for 'byarray'"
  410. <<" (line "<<line_num<<")\n";
  411. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
  412. error = true;
  413. return bfcode;
  414. }
  415. ++i;
  416. if (tokens[i] == "assigment")
  417. {
  418. bfcode += go_to(current_cell, at);
  419. ++i;
  420. bfcode += setchar(tokens[i][0]);
  421. bfcode += go_to(at, current_cell);
  422. return bfcode;
  423. }
  424. if (tokens[i] == "clear")
  425. {
  426. bfcode += go_to(current_cell, at);
  427. ++i;
  428. bfcode += "[-]\n";
  429. bfcode += go_to(at, current_cell);
  430. return bfcode;
  431. }
  432. if (tokens[i] == "add")
  433. {
  434. bfcode += go_to(current_cell, at);
  435. ++i;
  436. try
  437. {
  438. bfcode += change(stoi(tokens[i]));
  439. bfcode += go_to(at, current_cell);
  440. return bfcode;
  441. }
  442. catch (invalid_argument)
  443. {
  444. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")
  445. <<"invalid argument for pointer's 'add' (line "<<line_num<<")\n";
  446. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
  447. error = true;
  448. return bfcode;
  449. }
  450. }
  451. if (tokens[i] == "sub")
  452. {
  453. bfcode += go_to(current_cell, at);
  454. ++i;
  455. try
  456. {
  457. bfcode += change(-stoi(tokens[i]));
  458. bfcode += go_to(at, current_cell);
  459. return bfcode;
  460. }
  461. catch (invalid_argument)
  462. {
  463. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")
  464. <<"invalid argument for pointer's 'sub' (line "<<line_num<<")\n";
  465. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
  466. error = true;
  467. return bfcode;
  468. }
  469. }
  470. }
  471. if (tokens[i] == "goto")
  472. {
  473. ++i;
  474. int at;
  475. bool ploaded = false;
  476. if (tokens[i] == "loadpointer")
  477. {
  478. try
  479. {
  480. ++i;
  481. ploaded = true;
  482. at = pointers.at(tokens[i])->start;
  483. }
  484. catch(out_of_range)
  485. {
  486. cout<<BOLD_MSG("bfscript: code generator")<<ERROR_MSG("name error: ")<<"pointer '"<<tokens[i]
  487. <<"' is not defined (line "<<line_num<<")\n";
  488. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
  489. error = true;
  490. return bfcode;
  491. }
  492. }
  493. try
  494. {
  495. if (!ploaded) at = stoi(tokens[i]);
  496. go_to(current_cell, at);
  497. current_cell = stoi(tokens[i]);
  498. }
  499. catch(invalid_argument)
  500. {
  501. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")
  502. <<"invalid argument for 'goto' (line "<<line_num<<")\n";
  503. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
  504. error = true;
  505. return bfcode;
  506. }
  507. }
  508. if (tokens[i] == "alias")
  509. {
  510. ++i;
  511. aliases[tokens[i]] = tokens[i+1];
  512. ++i;
  513. return bfcode;
  514. }
  515. if (tokens[i] == "setchar")
  516. {
  517. ++i;
  518. if (tokens[i] == "loadalias")
  519. {
  520. bfcode += generate(tokens, i, aliases, pointers, line_num, current_cell, error);
  521. if (error) return bfcode;
  522. }
  523. bfcode += setchar(tokens[i][0]);
  524. return bfcode;
  525. }
  526. if (tokens[i] == "walk")
  527. {
  528. ++i;
  529. if (tokens[i] == "loadalias")
  530. {
  531. bfcode += generate(tokens, i, aliases, pointers, line_num, current_cell, error);
  532. if (error) return bfcode;
  533. }
  534. try
  535. {
  536. bfcode += walk(stoi(tokens[i]));
  537. current_cell += stoi(tokens[i]);
  538. return bfcode;
  539. }
  540. catch(invalid_argument)
  541. {
  542. cout<<BOLD_MSG("bfscript2: code generator: ")<<ERROR_MSG("argument error: ")
  543. <<"invalid argument for 'walk' (line "<<line_num<<")\n";
  544. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
  545. error = true;
  546. return bfcode;
  547. }
  548. }
  549. if (tokens[i] == "change")
  550. {
  551. ++i;
  552. if (tokens[i] == "loadalias")
  553. {
  554. bfcode += generate(tokens, i, aliases, pointers, line_num, current_cell, error);
  555. if (error) return bfcode;
  556. }
  557. try
  558. {
  559. bfcode += change(stoi(tokens[i]));
  560. return bfcode;
  561. }
  562. catch(invalid_argument)
  563. {
  564. cout<<BOLD_MSG("bfscript2: code generator: ")<<ERROR_MSG("argument error: ")
  565. <<"invalid argument for 'change' (line "<<line_num<<")\n";
  566. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
  567. error = true;
  568. return bfcode;
  569. }
  570. }
  571. if (tokens[i] == "mov")
  572. {
  573. ++i;
  574. if (tokens[i] == "loadalias")
  575. {
  576. bfcode += generate(tokens, i, aliases, pointers, line_num, current_cell, error);
  577. if (error) return bfcode;
  578. }
  579. try
  580. {
  581. bfcode += mov(stoi(tokens[i]));
  582. return bfcode;
  583. }
  584. catch(invalid_argument)
  585. {
  586. cout<<BOLD_MSG("bfscript2: code generator: ")<<ERROR_MSG("argument error: ")
  587. <<"invalid argument for 'mov' (line "<<line_num<<")\n";
  588. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
  589. error = true;
  590. return bfcode;
  591. }
  592. }
  593. if (tokens[i] == "print")
  594. {
  595. ++i;
  596. if (tokens[i] == "loadalias")
  597. {
  598. bfcode += generate(tokens, i, aliases, pointers, line_num, current_cell, error);
  599. if (error) return bfcode;
  600. }
  601. try
  602. {
  603. bfcode += print(stoi(tokens[i]));
  604. return bfcode;
  605. }
  606. catch(invalid_argument)
  607. {
  608. cout<<BOLD_MSG("bfscript2: code generator: ")<<ERROR_MSG("argument error: ")
  609. <<"invalid argument for 'print' (line "<<line_num<<")\n";
  610. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
  611. error = true;
  612. return bfcode;
  613. }
  614. return bfcode;
  615. }
  616. if (tokens[i] == "split")
  617. {
  618. ++i;
  619. if (tokens[i] == "loadalias")
  620. {
  621. bfcode += generate(tokens, i, aliases, pointers, line_num, current_cell, error);
  622. if (error) return bfcode;
  623. }
  624. bfcode += split(tokens[i]);
  625. return bfcode;
  626. }
  627. if (tokens[i] == "copy")
  628. {
  629. ++i;
  630. string arg1, arg2;
  631. if (tokens[i] == "loadalias")
  632. {
  633. ++i;
  634. try
  635. {
  636. arg1 = aliases[tokens[i]];
  637. }
  638. catch(out_of_range)
  639. {
  640. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("name error: ")
  641. <<"alias '"<<tokens[i]<<"' is not defined (line "<<line_num<<")\n";
  642. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
  643. error = true;
  644. return bfcode;
  645. }
  646. }
  647. ++i;
  648. if (tokens[i] == "loadalias")
  649. {
  650. ++i;
  651. try
  652. {
  653. arg1 = aliases[tokens[i]];
  654. }
  655. catch(out_of_range)
  656. {
  657. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("name error: ")
  658. <<"alias '"<<tokens[i]<<"' is not defined (line "<<line_num<<")\n";
  659. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
  660. error = true;
  661. return bfcode;
  662. }
  663. }
  664. try
  665. {
  666. bfcode += copy(stoi(arg1), stoi(arg2));
  667. }
  668. catch(invalid_argument)
  669. {
  670. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")
  671. <<"invalid argument(s) for 'copy' (line "<<line_num<<")\n";
  672. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
  673. error = true;
  674. return bfcode;
  675. }
  676. return bfcode;
  677. }
  678. cout<<ERROR_MSG("Error: ")<<"unexpected token at line "<<line_num<<": "<<BOLD_MSG(tokens[i])<<endl;
  679. cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
  680. error = true;
  681. return bfcode;
  682. }