code_generator.js 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  1. /* eslint-disable @typescript-eslint/no-use-before-define */
  2. /* globals $*/
  3. import { Types } from "./types";
  4. import * as Models from "./ivprog_elements";
  5. import { LocalizedStrings } from "./../services/localizedStringsService";
  6. import * as Utils from "./utils";
  7. export function generate () {
  8. $(".ivprog_visual_panel")
  9. .find(".error_icon")
  10. .remove();
  11. let code = LocalizedStrings.getUI("program") + " { ";
  12. code += globalsCode();
  13. code += "\n";
  14. let has_error = false;
  15. for (let i = 0; i < window.program_obj.functions.length; i++) {
  16. const n_code = functionsCode(window.program_obj.functions[i]);
  17. if (n_code == null) {
  18. has_error = true;
  19. }
  20. code += n_code;
  21. code += "\n";
  22. }
  23. code += "\n}";
  24. if (has_error) {
  25. return null;
  26. } else {
  27. return code;
  28. }
  29. }
  30. function functionsCode (function_obj) {
  31. let ret = "\n\t" + LocalizedStrings.getUI("function") + " ";
  32. const has_error = false;
  33. switch (function_obj.return_type) {
  34. case Types.INTEGER:
  35. ret += LocalizedStrings.getUI("type_integer");
  36. break;
  37. case Types.REAL:
  38. ret += LocalizedStrings.getUI("type_real");
  39. break;
  40. case Types.TEXT:
  41. ret += LocalizedStrings.getUI("type_text");
  42. break;
  43. case Types.BOOLEAN:
  44. ret += LocalizedStrings.getUI("type_boolean");
  45. break;
  46. case Types.CHAR:
  47. ret += LocalizedStrings.getUI("type_char");
  48. break;
  49. case Types.VOID:
  50. ret += LocalizedStrings.getUI("type_void");
  51. break;
  52. }
  53. ret += " ";
  54. if (function_obj.return_dimensions == 1) {
  55. ret += "[] ";
  56. } else if (function_obj.return_dimensions == 2) {
  57. ret += "[][] ";
  58. }
  59. if (function_obj.is_main) {
  60. ret += LocalizedStrings.getUI("start");
  61. } else {
  62. ret += function_obj.name;
  63. }
  64. ret += " ( ";
  65. for (let j = 0; j < function_obj.parameters_list.length; j++) {
  66. ret += parametersCode(function_obj.parameters_list[j]);
  67. if (j + 1 < function_obj.parameters_list.length) {
  68. ret += ", ";
  69. }
  70. }
  71. ret += " ) {";
  72. for (let j = 0; j < function_obj.variables_list.length; j++) {
  73. ret += variablesCode(function_obj.variables_list[j]);
  74. }
  75. for (let j = 0; j < function_obj.commands.length; j++) {
  76. //try {
  77. ret += commandsCode(function_obj.commands[j]);
  78. /*} catch (err) {
  79. has_error = true;
  80. console.error(err.message);
  81. var todos = $('body').find('.command_container');
  82. for (var i = 0; i < todos.length; i++) {
  83. if ($(todos[i]).data('command') == function_obj.commands[j]) {
  84. $( todos[i] ).prepend( ' <i class="ui icon red exclamation triangle error_icon"></i> ' );
  85. break;
  86. }
  87. }
  88. }*/
  89. }
  90. ret += "\n\t}";
  91. if (has_error) {
  92. return null;
  93. } else {
  94. return ret;
  95. }
  96. }
  97. function commandsCode (command_obj, indentation = 2) {
  98. switch (command_obj.type) {
  99. case Models.COMMAND_TYPES.break:
  100. return breaksCode(command_obj, indentation);
  101. case Models.COMMAND_TYPES.comment:
  102. return commentsCode(command_obj, indentation);
  103. case Models.COMMAND_TYPES.reader:
  104. return readersCode(command_obj, indentation);
  105. case Models.COMMAND_TYPES.writer:
  106. return writersCode(command_obj, indentation);
  107. case Models.COMMAND_TYPES.functioncall:
  108. return functioncallsCode(command_obj, indentation);
  109. case Models.COMMAND_TYPES.attribution:
  110. return attributionsCode(command_obj, indentation);
  111. case Models.COMMAND_TYPES.whiletrue:
  112. return whiletruesCode(command_obj, indentation);
  113. case Models.COMMAND_TYPES.dowhiletrue:
  114. return doWhilesCode(command_obj, indentation);
  115. case Models.COMMAND_TYPES.iftrue:
  116. return iftruesCode(command_obj, indentation);
  117. case Models.COMMAND_TYPES.repeatNtimes:
  118. return repeatNtimesCode(command_obj, indentation);
  119. case Models.COMMAND_TYPES.switch:
  120. return switchsCode(command_obj, indentation);
  121. case Models.COMMAND_TYPES.return:
  122. return returnsCode(command_obj, indentation);
  123. }
  124. }
  125. function returnsCode (command_obj, indentation) {
  126. let ret = "\n";
  127. for (let i = 0; i < indentation; i++) {
  128. ret += "\t";
  129. }
  130. ret += LocalizedStrings.getUI("text_return");
  131. if (command_obj.variable_value_menu) {
  132. try {
  133. ret += " " + elementExpressionCode(command_obj.variable_value_menu);
  134. //ret += ' ' + variableValueMenuCode(command_obj.variable_value_menu, true);
  135. } catch (err) {
  136. //Empty block
  137. }
  138. }
  139. return ret;
  140. }
  141. function breaksCode (_command_obj, indentation) {
  142. let ret = "\n";
  143. for (let i = 0; i < indentation; i++) {
  144. ret += "\t";
  145. }
  146. ret += LocalizedStrings.getUI("text_break");
  147. return ret;
  148. }
  149. function switchsCode (command_obj, indentation) {
  150. let ret = "\n";
  151. for (let i = 0; i < indentation; i++) {
  152. ret += "\t";
  153. }
  154. ret += LocalizedStrings.getUI("text_code_switch") + " ( ";
  155. ret += variableValueMenuCode(command_obj.variable);
  156. ret += " ) { ";
  157. if (command_obj.cases) {
  158. for (let i = 0; i < command_obj.cases.length; i++) {
  159. ret += switchcasesCode(command_obj.cases[i], indentation + 1);
  160. }
  161. }
  162. ret += "\n";
  163. for (let i = 0; i < indentation; i++) {
  164. ret += "\t";
  165. }
  166. ret += "} ";
  167. return ret;
  168. }
  169. function switchcasesCode (switchcase, indentation) {
  170. let ret = "\n";
  171. for (let i = 0; i < indentation; i++) {
  172. ret += "\t";
  173. }
  174. ret += LocalizedStrings.getUI("text_code_case") + " ";
  175. ret += variableValueMenuCode(switchcase.variable_value_menu);
  176. ret += " :";
  177. if (switchcase.commands_block) {
  178. for (let i = 0; i < switchcase.commands_block.length; i++) {
  179. ret += commandsCode(switchcase.commands_block[i], indentation + 1);
  180. }
  181. }
  182. return ret;
  183. }
  184. function repeatNtimesCode (command_obj, indentation) {
  185. let ret = "\n";
  186. for (let i = 0; i < indentation; i++) {
  187. ret += "\t";
  188. }
  189. ret += LocalizedStrings.getUI("text_for") + " ";
  190. if (command_obj.var_attribution) {
  191. ret += variableValueMenuCode(command_obj.var_attribution);
  192. ret += ` ${LocalizedStrings.getUI("text_code_for_from")} `;
  193. ret += variableValueMenuCode(command_obj.expression1);
  194. }
  195. if (command_obj.expression2) {
  196. ret += ` ${LocalizedStrings.getUI("text_code_for_to")} `;
  197. ret += variableValueMenuCode(command_obj.expression2);
  198. }
  199. if (command_obj.expression3) {
  200. ret += ` ${LocalizedStrings.getUI("text_code_for_pass")} `;
  201. switch (command_obj.expression3.itens[1]) {
  202. case Models.ARITHMETIC_TYPES.plus:
  203. ret += " +";
  204. break;
  205. case Models.ARITHMETIC_TYPES.minus:
  206. ret += " -";
  207. break;
  208. }
  209. ret += variableValueMenuCode(command_obj.expression3.itens[2]);
  210. }
  211. /*if (command_obj.var_incrementation) {
  212. ret += variableValueMenuCode(command_obj.var_incrementation);
  213. ret += ' <- ';
  214. ret += variableValueMenuCode(command_obj.expression3.itens[0]);
  215. switch (command_obj.expression3.itens[1]) {
  216. case Models.ARITHMETIC_TYPES.plus:
  217. ret += ' + ';
  218. break;
  219. case Models.ARITHMETIC_TYPES.minus:
  220. ret += ' - ';
  221. break;
  222. case Models.ARITHMETIC_TYPES.multiplication:
  223. ret += ' * ';
  224. break;
  225. case Models.ARITHMETIC_TYPES.division:
  226. ret += ' / ';
  227. break;
  228. case Models.ARITHMETIC_TYPES.module:
  229. ret += ' % ';
  230. break;
  231. }
  232. ret += variableValueMenuCode(command_obj.expression3.itens[2]);
  233. }*/
  234. ret += " { ";
  235. if (command_obj.commands_block) {
  236. for (let i = 0; i < command_obj.commands_block.length; i++) {
  237. ret += commandsCode(command_obj.commands_block[i], indentation + 1);
  238. }
  239. }
  240. ret += "\n";
  241. for (let i = 0; i < indentation; i++) {
  242. ret += "\t";
  243. }
  244. ret += "}";
  245. return ret;
  246. }
  247. function iftruesCode (command_obj, indentation) {
  248. let ret = "\n";
  249. for (let i = 0; i < indentation; i++) {
  250. ret += "\t";
  251. }
  252. ret += LocalizedStrings.getUI("text_if");
  253. if (!command_obj.expression) {
  254. Utils.renderErrorMessage(
  255. command_obj.expression.dom_object,
  256. LocalizedStrings.getUI("inform_valid_expression")
  257. );
  258. } else {
  259. ret += " ( ";
  260. ret += elementExpressionCode(command_obj.expression);
  261. ret += " ) ";
  262. }
  263. /*switch (command_obj.expression.expression.type) {
  264. case Models.EXPRESSION_TYPES.exp_logic:
  265. ret += logicExpressionCode(command_obj.expression.expression);
  266. break;
  267. case Models.EXPRESSION_TYPES.exp_arithmetic:
  268. ret += arithmeticExpressionCode(command_obj.expression.expression);
  269. break;
  270. }*/
  271. ret += " { ";
  272. if (command_obj.commands_block) {
  273. for (let i = 0; i < command_obj.commands_block.length; i++) {
  274. ret += commandsCode(command_obj.commands_block[i], indentation + 1);
  275. }
  276. }
  277. ret += "\n";
  278. for (let i = 0; i < indentation; i++) {
  279. ret += "\t";
  280. }
  281. ret += "} " + LocalizedStrings.getUI("text_else") + " {";
  282. if (command_obj.commands_else) {
  283. for (let i = 0; i < command_obj.commands_else.length; i++) {
  284. ret += commandsCode(command_obj.commands_else[i], indentation + 1);
  285. }
  286. }
  287. ret += "\n";
  288. for (let i = 0; i < indentation; i++) {
  289. ret += "\t";
  290. }
  291. ret += "}";
  292. return ret;
  293. }
  294. function doWhilesCode (command_obj, indentation) {
  295. let ret = "\n";
  296. for (let i = 0; i < indentation; i++) {
  297. ret += "\t";
  298. }
  299. ret += LocalizedStrings.getUI("text_code_do") + " { ";
  300. if (command_obj.commands_block) {
  301. for (let i = 0; i < command_obj.commands_block.length; i++) {
  302. ret += commandsCode(command_obj.commands_block[i], indentation + 1);
  303. }
  304. }
  305. ret += "\n";
  306. for (let i = 0; i < indentation; i++) {
  307. ret += "\t";
  308. }
  309. ret += "} " + LocalizedStrings.getUI("text_code_do_until");
  310. if (!command_obj.expression) {
  311. Utils.renderErrorMessage(
  312. command_obj.expression.dom_object,
  313. LocalizedStrings.getUI("inform_valid_expression")
  314. );
  315. }
  316. /*switch (command_obj.expression.expression.type) {
  317. case Models.EXPRESSION_TYPES.exp_logic:
  318. ret += logicExpressionCode(command_obj.expression.expression);
  319. break;
  320. case Models.EXPRESSION_TYPES.exp_arithmetic:
  321. ret += arithmeticExpressionCode(command_obj.expression.expression);
  322. break;
  323. }*/
  324. if (command_obj.expression) {
  325. ret += " ( ";
  326. ret += elementExpressionCode(command_obj.expression);
  327. ret += " ) ";
  328. }
  329. return ret;
  330. }
  331. function whiletruesCode (command_obj, indentation) {
  332. let ret = "\n";
  333. for (let i = 0; i < indentation; i++) {
  334. ret += "\t";
  335. }
  336. ret += LocalizedStrings.getUI("text_code_while");
  337. if (!command_obj.expression) {
  338. Utils.renderErrorMessage(
  339. command_obj.expression.dom_object,
  340. LocalizedStrings.getUI("inform_valid_expression")
  341. );
  342. }
  343. /*switch (command_obj.expression.expression.type) {
  344. case Models.EXPRESSION_TYPES.exp_logic:
  345. ret += logicExpressionCode(command_obj.expression.expression);
  346. break;
  347. case Models.EXPRESSION_TYPES.exp_arithmetic:
  348. ret += arithmeticExpressionCode(command_obj.expression.expression);
  349. break;
  350. }*/
  351. if (command_obj.expression) {
  352. ret += " ( ";
  353. ret += elementExpressionCode(command_obj.expression);
  354. ret += " ) ";
  355. }
  356. ret += " { ";
  357. if (command_obj.commands_block) {
  358. for (let i = 0; i < command_obj.commands_block.length; i++) {
  359. ret += commandsCode(command_obj.commands_block[i], indentation + 1);
  360. }
  361. }
  362. ret += "\n";
  363. for (let i = 0; i < indentation; i++) {
  364. ret += "\t";
  365. }
  366. ret += "}";
  367. return ret;
  368. }
  369. function logicExpressionCode (expression) {
  370. let ret = " ( ";
  371. if (expression.first_operand.type == Models.EXPRESSION_TYPES.exp_logic) {
  372. ret += logicExpressionCode(expression.first_operand);
  373. } else if (
  374. expression.first_operand.type == Models.EXPRESSION_TYPES.exp_arithmetic
  375. ) {
  376. ret += arithmeticExpressionCode(expression.first_operand);
  377. } else {
  378. ret += variableValueMenuCode(expression.first_operand);
  379. }
  380. if (expression.operator) {
  381. switch (expression.operator) {
  382. case Models.LOGIC_COMPARISON.equals_to:
  383. ret += " == ";
  384. break;
  385. case Models.LOGIC_COMPARISON.not_equals_to:
  386. ret += " != ";
  387. break;
  388. case Models.LOGIC_COMPARISON.and:
  389. ret += " && ";
  390. break;
  391. case Models.LOGIC_COMPARISON.or:
  392. ret += " || ";
  393. break;
  394. }
  395. if (expression.second_operand.type == Models.EXPRESSION_TYPES.exp_logic) {
  396. ret += logicExpressionCode(expression.second_operand);
  397. } else if (
  398. expression.second_operand.type == Models.EXPRESSION_TYPES.exp_arithmetic
  399. ) {
  400. ret += arithmeticExpressionCode(expression.second_operand);
  401. } else {
  402. ret += variableValueMenuCode(expression.second_operand);
  403. }
  404. }
  405. ret += " ) ";
  406. return ret;
  407. }
  408. function arithmeticExpressionCode (expression) {
  409. let ret = " ( ";
  410. if (expression.first_operand.type == Models.EXPRESSION_TYPES.exp_logic) {
  411. ret += logicExpressionCode(expression.first_operand);
  412. } else if (
  413. expression.first_operand.type == Models.EXPRESSION_TYPES.exp_arithmetic
  414. ) {
  415. ret += arithmeticExpressionCode(expression.first_operand);
  416. } else {
  417. ret += variableValueMenuCode(expression.first_operand);
  418. }
  419. switch (expression.operator) {
  420. case Models.ARITHMETIC_COMPARISON.greater_than:
  421. ret += " > ";
  422. break;
  423. case Models.ARITHMETIC_COMPARISON.less_than:
  424. ret += " < ";
  425. break;
  426. case Models.ARITHMETIC_COMPARISON.equals_to:
  427. ret += " == ";
  428. break;
  429. case Models.ARITHMETIC_COMPARISON.not_equals_to:
  430. ret += " != ";
  431. break;
  432. case Models.ARITHMETIC_COMPARISON.greater_than_or_equals_to:
  433. ret += " >= ";
  434. break;
  435. case Models.ARITHMETIC_COMPARISON.less_than_or_equals_to:
  436. ret += " <= ";
  437. break;
  438. }
  439. if (expression.second_operand.type == Models.EXPRESSION_TYPES.exp_logic) {
  440. ret += logicExpressionCode(expression.second_operand);
  441. } else if (
  442. expression.second_operand.type == Models.EXPRESSION_TYPES.exp_arithmetic
  443. ) {
  444. ret += arithmeticExpressionCode(expression.second_operand);
  445. } else {
  446. ret += variableValueMenuCode(expression.second_operand);
  447. }
  448. ret += " ) ";
  449. return ret;
  450. }
  451. function attributionsCode (command_obj, indentation) {
  452. let ret = "\n";
  453. for (let i = 0; i < indentation; i++) {
  454. ret += "\t";
  455. }
  456. ret += variableValueMenuCode(command_obj.variable) + " <- ";
  457. /*for (var i = 0; i < command_obj.expression.length; i++) {
  458. ret += elementExpressionCode(command_obj.expression[i]);
  459. }*/
  460. ret += elementExpressionCode(command_obj.expression);
  461. return ret;
  462. }
  463. function elementExpressionCode (expression_obj) {
  464. let ret = "";
  465. for (let i = 0; i < expression_obj.length; i++) {
  466. if (expression_obj[i].type) {
  467. ret += variableValueMenuCode(expression_obj[i]);
  468. } else if (expression_obj[i].type_op) {
  469. switch (expression_obj[i].item) {
  470. case Models.ARITHMETIC_TYPES.plus:
  471. ret += " + ";
  472. break;
  473. case Models.ARITHMETIC_TYPES.minus:
  474. ret += " - ";
  475. break;
  476. case Models.ARITHMETIC_TYPES.multiplication:
  477. ret += " * ";
  478. break;
  479. case Models.ARITHMETIC_TYPES.division:
  480. ret += " / ";
  481. break;
  482. case Models.ARITHMETIC_TYPES.module:
  483. ret += " % ";
  484. break;
  485. case Models.LOGIC_COMPARISON.equals_to:
  486. ret += " == ";
  487. break;
  488. case Models.LOGIC_COMPARISON.not_equals_to:
  489. ret += " != ";
  490. break;
  491. case Models.LOGIC_COMPARISON.and:
  492. ret += " " + LocalizedStrings.getUI("logic_operator_and") + " ";
  493. break;
  494. case Models.LOGIC_COMPARISON.or:
  495. ret += " " + LocalizedStrings.getUI("logic_operator_or") + " ";
  496. break;
  497. case Models.LOGIC_COMPARISON.not:
  498. ret += " " + LocalizedStrings.getUI("logic_operator_not") + " ";
  499. break;
  500. case Models.ARITHMETIC_COMPARISON.greater_than:
  501. ret += " > ";
  502. break;
  503. case Models.ARITHMETIC_COMPARISON.less_than:
  504. ret += " < ";
  505. break;
  506. case Models.ARITHMETIC_COMPARISON.greater_than_or_equals_to:
  507. ret += " >= ";
  508. break;
  509. case Models.ARITHMETIC_COMPARISON.less_than_or_equals_to:
  510. ret += " <= ";
  511. break;
  512. case Models.EXPRESSION_TYPES.write_sep:
  513. ret += ", ";
  514. break;
  515. }
  516. } else {
  517. ret += " " + expression_obj[i] + " ";
  518. }
  519. }
  520. return ret;
  521. }
  522. function functioncallsCode (command_obj, indentation) {
  523. let ret = "\n";
  524. for (let i = 0; i < indentation; i++) {
  525. ret += "\t";
  526. }
  527. ret += variableValueMenuCode(command_obj.function_called);
  528. return ret;
  529. }
  530. function readersCode (command_obj, indentation) {
  531. let ret = "\n";
  532. for (let i = 0; i < indentation; i++) {
  533. ret += "\t";
  534. }
  535. ret += LocalizedStrings.getUI("text_command_read") + " ( ";
  536. ret += variableValueMenuCode(command_obj.variable_value_menu);
  537. ret += " ) ";
  538. return ret;
  539. }
  540. function variableValueMenuCode (variable_obj, is_return = false) {
  541. let ret = "";
  542. try {
  543. if (variable_obj.function_called) {
  544. if (variable_obj.function_called.name) {
  545. ret += variable_obj.function_called.name + " ( ";
  546. } else {
  547. ret +=
  548. LocalizedStrings.translateInternalFunction(
  549. variable_obj.function_called.identifier,
  550. variable_obj.function_called.category
  551. ) + " ( ";
  552. }
  553. if (variable_obj.parameters_list) {
  554. for (let i = 0; i < variable_obj.parameters_list.length; i++) {
  555. ret += variableValueMenuCode(variable_obj.parameters_list[i]);
  556. if (i + 1 < variable_obj.parameters_list.length) {
  557. ret += ", ";
  558. }
  559. }
  560. }
  561. ret += " )";
  562. } else if (variable_obj.content.type) {
  563. ret += variable_obj.content.name;
  564. if (
  565. variable_obj.content.dimensions == 1 &&
  566. variable_obj.dimensions != 1
  567. ) {
  568. ret += " [ " + variableValueMenuCode(variable_obj.column) + " ] ";
  569. }
  570. if (
  571. variable_obj.content.dimensions == 2 &&
  572. variable_obj.dimensions != 2
  573. ) {
  574. ret += " [ " + variableValueMenuCode(variable_obj.row) + " ] ";
  575. ret += " [ " + variableValueMenuCode(variable_obj.column) + " ] ";
  576. }
  577. } else {
  578. const content = String(variable_obj.content);
  579. if (isNaN(content)) {
  580. // console.debug("Content length: ", content.length);
  581. // console.debug("Char code at 0", content.charCodeAt(0));
  582. const isBackslash = content.charCodeAt(0) === 92;
  583. if (content.length === 1 || (content.length === 2 && isBackslash)) {
  584. ret += `'${content}'`;
  585. } else {
  586. ret += '"' + variable_obj.content + '"';
  587. }
  588. } else {
  589. ret += variable_obj.content;
  590. }
  591. }
  592. } catch (err) {
  593. if (!is_return) {
  594. Utils.renderErrorMessage(
  595. variable_obj.dom_object,
  596. LocalizedStrings.getUI("inform_valid_content")
  597. );
  598. throw err;
  599. }
  600. }
  601. return ret;
  602. }
  603. function writersCode (command_obj, indentation) {
  604. let ret = "\n";
  605. for (let i = 0; i < indentation; i++) {
  606. ret += "\t";
  607. }
  608. ret += LocalizedStrings.getUI("text_command_write") + " ( ";
  609. /*for (var i = 0; i < command_obj.content.length; i++) {
  610. ret += variableValueMenuCode(command_obj.content[i]);
  611. if ((i + 1) < command_obj.content.length) {
  612. ret += ' + ';
  613. }
  614. }*/
  615. ret += elementExpressionCode(command_obj.content);
  616. if (command_obj.newline) {
  617. ret += ', "\\n"';
  618. }
  619. ret += " ) ";
  620. return ret;
  621. }
  622. function commentsCode (command_obj, indentation) {
  623. let ret = "\n";
  624. for (let i = 0; i < indentation; i++) {
  625. ret += "\t";
  626. }
  627. ret += "// ";
  628. ret += command_obj.comment_text.content;
  629. return ret;
  630. }
  631. function parametersCode (parameter_obj) {
  632. let ret = "";
  633. switch (parameter_obj.type) {
  634. case Types.INTEGER:
  635. ret += " " + LocalizedStrings.getUI("type_integer") + " ";
  636. break;
  637. case Types.REAL:
  638. ret += " " + LocalizedStrings.getUI("type_real") + " ";
  639. break;
  640. case Types.TEXT:
  641. ret += " " + LocalizedStrings.getUI("type_text") + " ";
  642. break;
  643. case Types.BOOLEAN:
  644. ret += " " + LocalizedStrings.getUI("type_boolean") + " ";
  645. break;
  646. case Types.CHAR:
  647. ret += " " + LocalizedStrings.getUI("type_char") + " ";
  648. break;
  649. }
  650. ret += parameter_obj.name + "";
  651. if (parameter_obj.dimensions == 1) {
  652. ret += " []";
  653. } else if (parameter_obj.dimensions == 2) {
  654. ret += " [][]";
  655. }
  656. return ret;
  657. }
  658. function variablesCode (variable_obj) {
  659. let ret = "";
  660. const temp = variable_obj;
  661. ret += "\n\t\t";
  662. if (temp.is_constant) {
  663. ret += "const ";
  664. }
  665. switch (temp.type) {
  666. case Types.INTEGER:
  667. ret += LocalizedStrings.getUI("type_integer") + " ";
  668. break;
  669. case Types.REAL:
  670. ret += LocalizedStrings.getUI("type_real") + " ";
  671. break;
  672. case Types.TEXT:
  673. ret += LocalizedStrings.getUI("type_text") + " ";
  674. break;
  675. case Types.BOOLEAN:
  676. ret += LocalizedStrings.getUI("type_boolean") + " ";
  677. break;
  678. case Types.CHAR:
  679. ret += LocalizedStrings.getUI("type_char") + " ";
  680. break;
  681. }
  682. ret += temp.name + " ";
  683. if (temp.dimensions == 1) {
  684. ret += "[" + temp.columns + "] ";
  685. switch (temp.type) {
  686. case Types.INTEGER:
  687. ret += "<- {";
  688. for (let j = 0; j < temp.value.length; j++) {
  689. ret += temp.value[j];
  690. if (j + 1 < temp.value.length) {
  691. ret += ", ";
  692. }
  693. }
  694. ret += "}";
  695. break;
  696. case Types.REAL:
  697. ret += "<- {";
  698. for (let j = 0; j < temp.value.length; j++) {
  699. ret += temp.value[j].toFixed(2);
  700. if (j + 1 < temp.value.length) {
  701. ret += ", ";
  702. }
  703. }
  704. ret += "}";
  705. break;
  706. case Types.TEXT:
  707. ret += "<- {";
  708. for (let j = 0; j < temp.value.length; j++) {
  709. ret += '"' + temp.value[j] + '"';
  710. if (j + 1 < temp.value.length) {
  711. ret += ", ";
  712. }
  713. }
  714. ret += "}";
  715. break;
  716. case Types.BOOLEAN:
  717. ret += "<- {";
  718. for (let j = 0; j < temp.value.length; j++) {
  719. if (temp.value[j]) {
  720. ret += LocalizedStrings.getUI("logic_value_true");
  721. } else {
  722. ret += LocalizedStrings.getUI("logic_value_false");
  723. }
  724. if (j + 1 < temp.value.length) {
  725. ret += ", ";
  726. }
  727. }
  728. ret += "}";
  729. break;
  730. case Types.CHAR:
  731. ret += "<- {";
  732. for (let j = 0; j < temp.value.length; j++) {
  733. ret += "'" + temp.value[j] + "'";
  734. if (j + 1 < temp.value.length) {
  735. ret += ", ";
  736. }
  737. }
  738. ret += "}";
  739. break;
  740. }
  741. } else if (temp.dimensions == 2) {
  742. ret += "[" + temp.rows + "][" + temp.columns + "] ";
  743. switch (temp.type) {
  744. case Types.INTEGER:
  745. ret += "<- {";
  746. for (let j = 0; j < temp.rows; j++) {
  747. ret += "{";
  748. for (let k = 0; k < temp.columns; k++) {
  749. ret += temp.value[j][k];
  750. if (k + 1 < temp.columns) {
  751. ret += ", ";
  752. }
  753. }
  754. ret += "}";
  755. if (j + 1 < temp.rows) {
  756. ret += ", ";
  757. }
  758. }
  759. ret += "}";
  760. break;
  761. case Types.REAL:
  762. ret += "<- {";
  763. for (let j = 0; j < temp.rows; j++) {
  764. ret += "{";
  765. for (let k = 0; k < temp.columns; k++) {
  766. ret += temp.value[j][k].toFixed(2);
  767. if (k + 1 < temp.columns) {
  768. ret += ", ";
  769. }
  770. }
  771. ret += "}";
  772. if (j + 1 < temp.rows) {
  773. ret += ", ";
  774. }
  775. }
  776. ret += "}";
  777. break;
  778. case Types.TEXT:
  779. ret += "<- {";
  780. for (let j = 0; j < temp.rows; j++) {
  781. ret += "{";
  782. for (let k = 0; k < temp.columns; k++) {
  783. ret += '"' + temp.value[j][k] + '"';
  784. if (k + 1 < temp.columns) {
  785. ret += ", ";
  786. }
  787. }
  788. ret += "}";
  789. if (j + 1 < temp.rows) {
  790. ret += ", ";
  791. }
  792. }
  793. ret += "}";
  794. break;
  795. case Types.BOOLEAN:
  796. ret += "<- {";
  797. for (let j = 0; j < temp.rows; j++) {
  798. ret += "{";
  799. for (let k = 0; k < temp.columns; k++) {
  800. if (temp.value[j][k]) {
  801. ret += LocalizedStrings.getUI("logic_value_true");
  802. } else {
  803. ret += LocalizedStrings.getUI("logic_value_false");
  804. }
  805. if (k + 1 < temp.columns) {
  806. ret += ", ";
  807. }
  808. }
  809. ret += "}";
  810. if (j + 1 < temp.rows) {
  811. ret += ", ";
  812. }
  813. }
  814. ret += "}";
  815. break;
  816. case Types.CHAR:
  817. ret += "<- {";
  818. for (let j = 0; j < temp.rows; j++) {
  819. ret += "{";
  820. for (let k = 0; k < temp.columns; k++) {
  821. ret += "'" + temp.value[j][k] + "'";
  822. if (k + 1 < temp.columns) {
  823. ret += ", ";
  824. }
  825. }
  826. ret += "}";
  827. if (j + 1 < temp.rows) {
  828. ret += ", ";
  829. }
  830. }
  831. ret += "}";
  832. break;
  833. }
  834. } else {
  835. switch (temp.type) {
  836. case Types.INTEGER:
  837. ret += "<- " + temp.value;
  838. break;
  839. case Types.REAL:
  840. ret += "<- " + temp.value.toFixed(2);
  841. break;
  842. case Types.TEXT:
  843. ret += '<- "' + temp.value + '"';
  844. break;
  845. case Types.BOOLEAN:
  846. ret += "<- ";
  847. if (temp.value) {
  848. ret += LocalizedStrings.getUI("logic_value_true");
  849. } else {
  850. ret += LocalizedStrings.getUI("logic_value_false");
  851. }
  852. break;
  853. case Types.CHAR:
  854. ret += "<- '" + temp.value + "'";
  855. break;
  856. }
  857. }
  858. return ret;
  859. }
  860. function globalsCode () {
  861. let ret = "";
  862. if (window.program_obj.globals) {
  863. for (let i = 0; i < window.program_obj.globals.length; i++) {
  864. const temp = window.program_obj.globals[i];
  865. ret += "\n\t";
  866. if (temp.is_constant) {
  867. ret += "const ";
  868. }
  869. switch (temp.type) {
  870. case Types.INTEGER:
  871. ret += LocalizedStrings.getUI("type_integer");
  872. break;
  873. case Types.REAL:
  874. ret += LocalizedStrings.getUI("type_real");
  875. break;
  876. case Types.TEXT:
  877. ret += LocalizedStrings.getUI("type_text");
  878. break;
  879. case Types.BOOLEAN:
  880. ret += LocalizedStrings.getUI("type_boolean");
  881. break;
  882. case Types.CHAR:
  883. ret += LocalizedStrings.getUI("type_char");
  884. break;
  885. }
  886. ret += " " + temp.name + " ";
  887. if (temp.dimensions == 1) {
  888. ret += "[" + temp.columns + "] ";
  889. switch (temp.type) {
  890. case Types.INTEGER:
  891. ret += "<- {";
  892. for (let j = 0; j < temp.value.length; j++) {
  893. ret += temp.value[j];
  894. if (j + 1 < temp.value.length) {
  895. ret += ", ";
  896. }
  897. }
  898. ret += "}";
  899. break;
  900. case Types.REAL:
  901. ret += "<- {";
  902. for (let j = 0; j < temp.value.length; j++) {
  903. ret += temp.value[j].toFixed(2);
  904. if (j + 1 < temp.value.length) {
  905. ret += ", ";
  906. }
  907. }
  908. ret += "}";
  909. break;
  910. case Types.TEXT:
  911. ret += "<- {";
  912. for (let j = 0; j < temp.value.length; j++) {
  913. ret += '"' + temp.value[j] + '"';
  914. if (j + 1 < temp.value.length) {
  915. ret += ", ";
  916. }
  917. }
  918. ret += "}";
  919. break;
  920. case Types.BOOLEAN:
  921. ret += "<- {";
  922. for (let j = 0; j < temp.value.length; j++) {
  923. if (temp.value[j]) {
  924. ret += LocalizedStrings.getUI("logic_value_true");
  925. } else {
  926. ret += LocalizedStrings.getUI("logic_value_false");
  927. }
  928. if (j + 1 < temp.value.length) {
  929. ret += ", ";
  930. }
  931. }
  932. ret += "}";
  933. break;
  934. case Types.CHAR:
  935. ret += "<- {";
  936. for (let j = 0; j < temp.value.length; j++) {
  937. ret += "'" + temp.value[j] + "'";
  938. if (j + 1 < temp.value.length) {
  939. ret += ", ";
  940. }
  941. }
  942. ret += "}";
  943. break;
  944. }
  945. } else if (temp.dimensions == 2) {
  946. ret += "[" + temp.rows + "][" + temp.columns + "] ";
  947. switch (temp.type) {
  948. case Types.INTEGER:
  949. ret += "<- {";
  950. for (let j = 0; j < temp.rows; j++) {
  951. ret += "{";
  952. for (let k = 0; k < temp.columns; k++) {
  953. ret += temp.value[j][k];
  954. if (k + 1 < temp.columns) {
  955. ret += ", ";
  956. }
  957. }
  958. ret += "}";
  959. if (j + 1 < temp.rows) {
  960. ret += ", ";
  961. }
  962. }
  963. ret += "}";
  964. break;
  965. case Types.REAL:
  966. ret += "<- {";
  967. for (let j = 0; j < temp.rows; j++) {
  968. ret += "{";
  969. for (let k = 0; k < temp.columns; k++) {
  970. ret += temp.value[j][k].toFixed(2);
  971. if (k + 1 < temp.columns) {
  972. ret += ", ";
  973. }
  974. }
  975. ret += "}";
  976. if (j + 1 < temp.rows) {
  977. ret += ", ";
  978. }
  979. }
  980. ret += "}";
  981. break;
  982. case Types.TEXT:
  983. ret += "<- {";
  984. for (let j = 0; j < temp.rows; j++) {
  985. ret += "{";
  986. for (let k = 0; k < temp.columns; k++) {
  987. ret += '"' + temp.value[j][k] + '"';
  988. if (k + 1 < temp.columns) {
  989. ret += ", ";
  990. }
  991. }
  992. ret += "}";
  993. if (j + 1 < temp.rows) {
  994. ret += ", ";
  995. }
  996. }
  997. ret += "}";
  998. break;
  999. case Types.BOOLEAN:
  1000. ret += "<- {";
  1001. for (let j = 0; j < temp.rows; j++) {
  1002. ret += "{";
  1003. for (let k = 0; k < temp.columns; k++) {
  1004. if (temp.value[j][k]) {
  1005. ret += LocalizedStrings.getUI("logic_value_true");
  1006. } else {
  1007. ret += LocalizedStrings.getUI("logic_value_false");
  1008. }
  1009. if (k + 1 < temp.columns) {
  1010. ret += ", ";
  1011. }
  1012. }
  1013. ret += "}";
  1014. if (j + 1 < temp.rows) {
  1015. ret += ", ";
  1016. }
  1017. }
  1018. ret += "}";
  1019. break;
  1020. case Types.CHAR:
  1021. ret += "<- {";
  1022. for (let j = 0; j < temp.rows; j++) {
  1023. ret += "{";
  1024. for (let k = 0; k < temp.columns; k++) {
  1025. ret += "'" + temp.value[j][k] + "'";
  1026. if (k + 1 < temp.columns) {
  1027. ret += ", ";
  1028. }
  1029. }
  1030. ret += "}";
  1031. if (j + 1 < temp.rows) {
  1032. ret += ", ";
  1033. }
  1034. }
  1035. ret += "}";
  1036. break;
  1037. }
  1038. } else {
  1039. switch (temp.type) {
  1040. case Types.INTEGER:
  1041. ret += "<- " + temp.value;
  1042. break;
  1043. case Types.REAL:
  1044. ret += "<- " + temp.value.toFixed(2);
  1045. break;
  1046. case Types.TEXT:
  1047. ret += '<- "' + temp.value + '"';
  1048. break;
  1049. case Types.BOOLEAN:
  1050. ret += "<- ";
  1051. if (temp.value) {
  1052. ret += LocalizedStrings.getUI("logic_value_true");
  1053. } else {
  1054. ret += LocalizedStrings.getUI("logic_value_false");
  1055. }
  1056. break;
  1057. case Types.CHAR:
  1058. ret += "<- '" + temp.value + "'";
  1059. break;
  1060. }
  1061. }
  1062. }
  1063. }
  1064. return ret;
  1065. }