code_generator.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  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. export function repeatNtimesHeader (command_obj) {
  185. let ret = "";
  186. if (command_obj.var_attribution) {
  187. ret += '<span class="repeatN_text_par_1">';
  188. ret += variableValueMenuCode(command_obj.var_attribution);
  189. ret += '</span>';
  190. ret += ` ${LocalizedStrings.getUI("text_code_for_from")} `;
  191. ret += '<span class="repeatN_text_par_2">';
  192. ret += variableValueMenuCode(command_obj.expression1);
  193. ret += '</span>';
  194. }
  195. if (command_obj.expression2) {
  196. ret += ` ${LocalizedStrings.getUI("text_code_for_to")} `;
  197. ret += '<span class="repeatN_text_par_3">';
  198. ret += variableValueMenuCode(command_obj.expression2);
  199. ret += '</span>';
  200. }
  201. if (command_obj.expression3) {
  202. ret += ` ${LocalizedStrings.getUI("text_code_for_pass")} `;
  203. ret += '<span class="repeatN_text_par_4">';
  204. switch (command_obj.expression3.itens[1]) {
  205. case Models.ARITHMETIC_TYPES.plus:
  206. ret += " +";
  207. break;
  208. case Models.ARITHMETIC_TYPES.minus:
  209. ret += " -";
  210. break;
  211. }
  212. ret += variableValueMenuCode(command_obj.expression3.itens[2]);
  213. ret += '</span>';
  214. }
  215. return ret;
  216. }
  217. function repeatNtimesCode (command_obj, indentation) {
  218. let ret = "\n";
  219. for (let i = 0; i < indentation; i++) {
  220. ret += "\t";
  221. }
  222. ret += LocalizedStrings.getUI("text_for") + " ";
  223. if (command_obj.var_attribution) {
  224. ret += variableValueMenuCode(command_obj.var_attribution);
  225. ret += ` ${LocalizedStrings.getUI("text_code_for_from")} `;
  226. ret += variableValueMenuCode(command_obj.expression1);
  227. }
  228. if (command_obj.expression2) {
  229. ret += ` ${LocalizedStrings.getUI("text_code_for_to")} `;
  230. ret += variableValueMenuCode(command_obj.expression2);
  231. }
  232. if (command_obj.expression3) {
  233. ret += ` ${LocalizedStrings.getUI("text_code_for_pass")} `;
  234. switch (command_obj.expression3.itens[1]) {
  235. case Models.ARITHMETIC_TYPES.plus:
  236. ret += " +";
  237. break;
  238. case Models.ARITHMETIC_TYPES.minus:
  239. ret += " -";
  240. break;
  241. }
  242. ret += variableValueMenuCode(command_obj.expression3.itens[2]);
  243. }
  244. ret += " { ";
  245. if (command_obj.commands_block) {
  246. for (let i = 0; i < command_obj.commands_block.length; i++) {
  247. ret += commandsCode(command_obj.commands_block[i], indentation + 1);
  248. }
  249. }
  250. ret += "\n";
  251. for (let i = 0; i < indentation; i++) {
  252. ret += "\t";
  253. }
  254. ret += "}";
  255. return ret;
  256. }
  257. function iftruesCode (command_obj, indentation) {
  258. let ret = "\n";
  259. for (let i = 0; i < indentation; i++) {
  260. ret += "\t";
  261. }
  262. ret += LocalizedStrings.getUI("text_if");
  263. if (!command_obj.expression) {
  264. Utils.renderErrorMessage(
  265. command_obj.expression.dom_object,
  266. LocalizedStrings.getUI("inform_valid_expression")
  267. );
  268. } else {
  269. ret += " ( ";
  270. ret += elementExpressionCode(command_obj.expression);
  271. ret += " ) ";
  272. }
  273. /*switch (command_obj.expression.expression.type) {
  274. case Models.EXPRESSION_TYPES.exp_logic:
  275. ret += logicExpressionCode(command_obj.expression.expression);
  276. break;
  277. case Models.EXPRESSION_TYPES.exp_arithmetic:
  278. ret += arithmeticExpressionCode(command_obj.expression.expression);
  279. break;
  280. }*/
  281. ret += " { ";
  282. if (command_obj.commands_block) {
  283. for (let i = 0; i < command_obj.commands_block.length; i++) {
  284. ret += commandsCode(command_obj.commands_block[i], indentation + 1);
  285. }
  286. }
  287. ret += "\n";
  288. for (let i = 0; i < indentation; i++) {
  289. ret += "\t";
  290. }
  291. ret += "} " + LocalizedStrings.getUI("text_else") + " {";
  292. if (command_obj.commands_else) {
  293. for (let i = 0; i < command_obj.commands_else.length; i++) {
  294. ret += commandsCode(command_obj.commands_else[i], indentation + 1);
  295. }
  296. }
  297. ret += "\n";
  298. for (let i = 0; i < indentation; i++) {
  299. ret += "\t";
  300. }
  301. ret += "}";
  302. return ret;
  303. }
  304. function doWhilesCode (command_obj, indentation) {
  305. let ret = "\n";
  306. for (let i = 0; i < indentation; i++) {
  307. ret += "\t";
  308. }
  309. ret += LocalizedStrings.getUI("text_code_do") + " { ";
  310. if (command_obj.commands_block) {
  311. for (let i = 0; i < command_obj.commands_block.length; i++) {
  312. ret += commandsCode(command_obj.commands_block[i], indentation + 1);
  313. }
  314. }
  315. ret += "\n";
  316. for (let i = 0; i < indentation; i++) {
  317. ret += "\t";
  318. }
  319. ret += "} " + LocalizedStrings.getUI("text_code_do_until");
  320. if (!command_obj.expression) {
  321. Utils.renderErrorMessage(
  322. command_obj.expression.dom_object,
  323. LocalizedStrings.getUI("inform_valid_expression")
  324. );
  325. }
  326. /*switch (command_obj.expression.expression.type) {
  327. case Models.EXPRESSION_TYPES.exp_logic:
  328. ret += logicExpressionCode(command_obj.expression.expression);
  329. break;
  330. case Models.EXPRESSION_TYPES.exp_arithmetic:
  331. ret += arithmeticExpressionCode(command_obj.expression.expression);
  332. break;
  333. }*/
  334. if (command_obj.expression) {
  335. ret += " ( ";
  336. ret += elementExpressionCode(command_obj.expression);
  337. ret += " ) ";
  338. }
  339. return ret;
  340. }
  341. function whiletruesCode (command_obj, indentation) {
  342. let ret = "\n";
  343. for (let i = 0; i < indentation; i++) {
  344. ret += "\t";
  345. }
  346. ret += LocalizedStrings.getUI("text_code_while");
  347. if (!command_obj.expression) {
  348. Utils.renderErrorMessage(
  349. command_obj.expression.dom_object,
  350. LocalizedStrings.getUI("inform_valid_expression")
  351. );
  352. }
  353. /*switch (command_obj.expression.expression.type) {
  354. case Models.EXPRESSION_TYPES.exp_logic:
  355. ret += logicExpressionCode(command_obj.expression.expression);
  356. break;
  357. case Models.EXPRESSION_TYPES.exp_arithmetic:
  358. ret += arithmeticExpressionCode(command_obj.expression.expression);
  359. break;
  360. }*/
  361. if (command_obj.expression) {
  362. ret += " ( ";
  363. ret += elementExpressionCode(command_obj.expression);
  364. ret += " ) ";
  365. }
  366. ret += " { ";
  367. if (command_obj.commands_block) {
  368. for (let i = 0; i < command_obj.commands_block.length; i++) {
  369. ret += commandsCode(command_obj.commands_block[i], indentation + 1);
  370. }
  371. }
  372. ret += "\n";
  373. for (let i = 0; i < indentation; i++) {
  374. ret += "\t";
  375. }
  376. ret += "}";
  377. return ret;
  378. }
  379. function logicExpressionCode (expression) {
  380. let ret = " ( ";
  381. if (expression.first_operand.type == Models.EXPRESSION_TYPES.exp_logic) {
  382. ret += logicExpressionCode(expression.first_operand);
  383. } else if (
  384. expression.first_operand.type == Models.EXPRESSION_TYPES.exp_arithmetic
  385. ) {
  386. ret += arithmeticExpressionCode(expression.first_operand);
  387. } else {
  388. ret += variableValueMenuCode(expression.first_operand);
  389. }
  390. if (expression.operator) {
  391. switch (expression.operator) {
  392. case Models.LOGIC_COMPARISON.equals_to:
  393. ret += " == ";
  394. break;
  395. case Models.LOGIC_COMPARISON.not_equals_to:
  396. ret += " != ";
  397. break;
  398. case Models.LOGIC_COMPARISON.and:
  399. ret += " && ";
  400. break;
  401. case Models.LOGIC_COMPARISON.or:
  402. ret += " || ";
  403. break;
  404. }
  405. if (expression.second_operand.type == Models.EXPRESSION_TYPES.exp_logic) {
  406. ret += logicExpressionCode(expression.second_operand);
  407. } else if (
  408. expression.second_operand.type == Models.EXPRESSION_TYPES.exp_arithmetic
  409. ) {
  410. ret += arithmeticExpressionCode(expression.second_operand);
  411. } else {
  412. ret += variableValueMenuCode(expression.second_operand);
  413. }
  414. }
  415. ret += " ) ";
  416. return ret;
  417. }
  418. function arithmeticExpressionCode (expression) {
  419. let ret = " ( ";
  420. if (expression.first_operand.type == Models.EXPRESSION_TYPES.exp_logic) {
  421. ret += logicExpressionCode(expression.first_operand);
  422. } else if (
  423. expression.first_operand.type == Models.EXPRESSION_TYPES.exp_arithmetic
  424. ) {
  425. ret += arithmeticExpressionCode(expression.first_operand);
  426. } else {
  427. ret += variableValueMenuCode(expression.first_operand);
  428. }
  429. switch (expression.operator) {
  430. case Models.ARITHMETIC_COMPARISON.greater_than:
  431. ret += " > ";
  432. break;
  433. case Models.ARITHMETIC_COMPARISON.less_than:
  434. ret += " < ";
  435. break;
  436. case Models.ARITHMETIC_COMPARISON.equals_to:
  437. ret += " == ";
  438. break;
  439. case Models.ARITHMETIC_COMPARISON.not_equals_to:
  440. ret += " != ";
  441. break;
  442. case Models.ARITHMETIC_COMPARISON.greater_than_or_equals_to:
  443. ret += " >= ";
  444. break;
  445. case Models.ARITHMETIC_COMPARISON.less_than_or_equals_to:
  446. ret += " <= ";
  447. break;
  448. }
  449. if (expression.second_operand.type == Models.EXPRESSION_TYPES.exp_logic) {
  450. ret += logicExpressionCode(expression.second_operand);
  451. } else if (
  452. expression.second_operand.type == Models.EXPRESSION_TYPES.exp_arithmetic
  453. ) {
  454. ret += arithmeticExpressionCode(expression.second_operand);
  455. } else {
  456. ret += variableValueMenuCode(expression.second_operand);
  457. }
  458. ret += " ) ";
  459. return ret;
  460. }
  461. function attributionsCode (command_obj, indentation) {
  462. let ret = "\n";
  463. for (let i = 0; i < indentation; i++) {
  464. ret += "\t";
  465. }
  466. ret += variableValueMenuCode(command_obj.variable) + " <- ";
  467. /*for (var i = 0; i < command_obj.expression.length; i++) {
  468. ret += elementExpressionCode(command_obj.expression[i]);
  469. }*/
  470. ret += elementExpressionCode(command_obj.expression);
  471. return ret;
  472. }
  473. export function elementExpressionCode (expression_obj) {
  474. let ret = "";
  475. for (let i = 0; i < expression_obj.length; i++) {
  476. if (expression_obj[i].type) {
  477. ret += variableValueMenuCode(expression_obj[i]);
  478. } else if (expression_obj[i].type_op) {
  479. switch (expression_obj[i].item) {
  480. case Models.ARITHMETIC_TYPES.plus:
  481. ret += " + ";
  482. break;
  483. case Models.ARITHMETIC_TYPES.minus:
  484. ret += " - ";
  485. break;
  486. case Models.ARITHMETIC_TYPES.multiplication:
  487. ret += " * ";
  488. break;
  489. case Models.ARITHMETIC_TYPES.division:
  490. ret += " / ";
  491. break;
  492. case Models.ARITHMETIC_TYPES.module:
  493. ret += " % ";
  494. break;
  495. case Models.LOGIC_COMPARISON.equals_to:
  496. ret += " == ";
  497. break;
  498. case Models.LOGIC_COMPARISON.not_equals_to:
  499. ret += " != ";
  500. break;
  501. case Models.LOGIC_COMPARISON.and:
  502. ret += " " + LocalizedStrings.getUI("logic_operator_and") + " ";
  503. break;
  504. case Models.LOGIC_COMPARISON.or:
  505. ret += " " + LocalizedStrings.getUI("logic_operator_or") + " ";
  506. break;
  507. case Models.LOGIC_COMPARISON.not:
  508. ret += " " + LocalizedStrings.getUI("logic_operator_not") + " ";
  509. break;
  510. case Models.ARITHMETIC_COMPARISON.greater_than:
  511. ret += " > ";
  512. break;
  513. case Models.ARITHMETIC_COMPARISON.less_than:
  514. ret += " < ";
  515. break;
  516. case Models.ARITHMETIC_COMPARISON.greater_than_or_equals_to:
  517. ret += " >= ";
  518. break;
  519. case Models.ARITHMETIC_COMPARISON.less_than_or_equals_to:
  520. ret += " <= ";
  521. break;
  522. case Models.EXPRESSION_TYPES.write_sep:
  523. ret += ", ";
  524. break;
  525. }
  526. } else {
  527. ret += " " + expression_obj[i] + " ";
  528. }
  529. }
  530. return ret;
  531. }
  532. function functioncallsCode (command_obj, indentation) {
  533. let ret = "\n";
  534. for (let i = 0; i < indentation; i++) {
  535. ret += "\t";
  536. }
  537. ret += variableValueMenuCode(command_obj.function_called);
  538. return ret;
  539. }
  540. function readersCode (command_obj, indentation) {
  541. let ret = "\n";
  542. for (let i = 0; i < indentation; i++) {
  543. ret += "\t";
  544. }
  545. ret += LocalizedStrings.getUI("text_command_read") + " ( ";
  546. ret += variableValueMenuCode(command_obj.variable_value_menu);
  547. ret += " ) ";
  548. return ret;
  549. }
  550. function variableValueMenuCode (variable_obj, is_return = false) {
  551. let ret = "";
  552. try {
  553. if (variable_obj.function_called) {
  554. if (variable_obj.function_called.name) {
  555. ret += variable_obj.function_called.name + " ( ";
  556. } else {
  557. ret +=
  558. LocalizedStrings.translateInternalFunction(
  559. variable_obj.function_called.identifier,
  560. variable_obj.function_called.category
  561. ) + " ( ";
  562. }
  563. if (variable_obj.parameters_list) {
  564. for (let i = 0; i < variable_obj.parameters_list.length; i++) {
  565. ret += variableValueMenuCode(variable_obj.parameters_list[i]);
  566. if (i + 1 < variable_obj.parameters_list.length) {
  567. ret += ", ";
  568. }
  569. }
  570. }
  571. ret += " )";
  572. } else if (variable_obj.content.type) {
  573. ret += variable_obj.content.name;
  574. if (
  575. variable_obj.content.dimensions == 1 &&
  576. variable_obj.dimensions != 1
  577. ) {
  578. ret += " [ " + variableValueMenuCode(variable_obj.column) + " ] ";
  579. }
  580. if (
  581. variable_obj.content.dimensions == 2 &&
  582. variable_obj.dimensions != 2
  583. ) {
  584. ret += " [ " + variableValueMenuCode(variable_obj.row) + " ] ";
  585. ret += " [ " + variableValueMenuCode(variable_obj.column) + " ] ";
  586. }
  587. } else {
  588. const content = String(variable_obj.content);
  589. if (isNaN(content)) {
  590. // console.debug("Content length: ", content.length);
  591. // console.debug("Char code at 0", content.charCodeAt(0));
  592. const isBackslash = content.charCodeAt(0) === 92;
  593. if (content.length === 1 || (content.length === 2 && isBackslash)) {
  594. ret += `'${content}'`;
  595. } else {
  596. ret += '"' + variable_obj.content + '"';
  597. }
  598. } else if (content.length == 0) {
  599. ret += '""';
  600. } else if (content.trim().length == 0) {
  601. ret += '"' + content + '"';
  602. } else {
  603. ret += variable_obj.content;
  604. }
  605. }
  606. } catch (err) {
  607. if (!is_return) {
  608. Utils.renderErrorMessage(
  609. variable_obj.dom_object,
  610. LocalizedStrings.getUI("inform_valid_content")
  611. );
  612. throw err;
  613. }
  614. }
  615. return ret;
  616. }
  617. function writersCode (command_obj, indentation) {
  618. let ret = "\n";
  619. for (let i = 0; i < indentation; i++) {
  620. ret += "\t";
  621. }
  622. ret += LocalizedStrings.getUI("text_command_write") + " ( ";
  623. /*for (var i = 0; i < command_obj.content.length; i++) {
  624. ret += variableValueMenuCode(command_obj.content[i]);
  625. if ((i + 1) < command_obj.content.length) {
  626. ret += ' + ';
  627. }
  628. }*/
  629. ret += elementExpressionCode(command_obj.content);
  630. if (command_obj.newline) {
  631. ret += ', "\\n"';
  632. }
  633. ret += " ) ";
  634. return ret;
  635. }
  636. function commentsCode (command_obj, indentation) {
  637. let ret = "\n";
  638. for (let i = 0; i < indentation; i++) {
  639. ret += "\t";
  640. }
  641. ret += "// ";
  642. ret += command_obj.comment_text.content;
  643. return ret;
  644. }
  645. function parametersCode (parameter_obj) {
  646. let ret = "";
  647. switch (parameter_obj.type) {
  648. case Types.INTEGER:
  649. ret += " " + LocalizedStrings.getUI("type_integer") + " ";
  650. break;
  651. case Types.REAL:
  652. ret += " " + LocalizedStrings.getUI("type_real") + " ";
  653. break;
  654. case Types.TEXT:
  655. ret += " " + LocalizedStrings.getUI("type_text") + " ";
  656. break;
  657. case Types.BOOLEAN:
  658. ret += " " + LocalizedStrings.getUI("type_boolean") + " ";
  659. break;
  660. case Types.CHAR:
  661. ret += " " + LocalizedStrings.getUI("type_char") + " ";
  662. break;
  663. }
  664. ret += parameter_obj.name + "";
  665. if (parameter_obj.dimensions == 1) {
  666. ret += " []";
  667. } else if (parameter_obj.dimensions == 2) {
  668. ret += " [][]";
  669. }
  670. return ret;
  671. }
  672. function variablesCode (variable_obj) {
  673. let ret = "";
  674. const temp = variable_obj;
  675. ret += "\n\t\t";
  676. if (temp.is_constant) {
  677. ret += "const ";
  678. }
  679. switch (temp.type) {
  680. case Types.INTEGER:
  681. ret += LocalizedStrings.getUI("type_integer") + " ";
  682. break;
  683. case Types.REAL:
  684. ret += LocalizedStrings.getUI("type_real") + " ";
  685. break;
  686. case Types.TEXT:
  687. ret += LocalizedStrings.getUI("type_text") + " ";
  688. break;
  689. case Types.BOOLEAN:
  690. ret += LocalizedStrings.getUI("type_boolean") + " ";
  691. break;
  692. case Types.CHAR:
  693. ret += LocalizedStrings.getUI("type_char") + " ";
  694. break;
  695. }
  696. ret += temp.name + " ";
  697. if (temp.dimensions == 1) {
  698. ret += "[" + temp.columns + "] ";
  699. switch (temp.type) {
  700. case Types.INTEGER:
  701. ret += "<- {";
  702. for (let j = 0; j < temp.value.length; j++) {
  703. ret += temp.value[j];
  704. if (j + 1 < temp.value.length) {
  705. ret += ", ";
  706. }
  707. }
  708. ret += "}";
  709. break;
  710. case Types.REAL:
  711. ret += "<- {";
  712. for (let j = 0; j < temp.value.length; j++) {
  713. ret += temp.value[j].toFixed(2);
  714. if (j + 1 < temp.value.length) {
  715. ret += ", ";
  716. }
  717. }
  718. ret += "}";
  719. break;
  720. case Types.TEXT:
  721. ret += "<- {";
  722. for (let j = 0; j < temp.value.length; j++) {
  723. ret += '"' + temp.value[j] + '"';
  724. if (j + 1 < temp.value.length) {
  725. ret += ", ";
  726. }
  727. }
  728. ret += "}";
  729. break;
  730. case Types.BOOLEAN:
  731. ret += "<- {";
  732. for (let j = 0; j < temp.value.length; j++) {
  733. if (temp.value[j]) {
  734. ret += LocalizedStrings.getUI("logic_value_true");
  735. } else {
  736. ret += LocalizedStrings.getUI("logic_value_false");
  737. }
  738. if (j + 1 < temp.value.length) {
  739. ret += ", ";
  740. }
  741. }
  742. ret += "}";
  743. break;
  744. case Types.CHAR:
  745. ret += "<- {";
  746. for (let j = 0; j < temp.value.length; j++) {
  747. ret += "'" + temp.value[j] + "'";
  748. if (j + 1 < temp.value.length) {
  749. ret += ", ";
  750. }
  751. }
  752. ret += "}";
  753. break;
  754. }
  755. } else if (temp.dimensions == 2) {
  756. ret += "[" + temp.rows + "][" + temp.columns + "] ";
  757. switch (temp.type) {
  758. case Types.INTEGER:
  759. ret += "<- {";
  760. for (let j = 0; j < temp.rows; j++) {
  761. ret += "{";
  762. for (let k = 0; k < temp.columns; k++) {
  763. ret += temp.value[j][k];
  764. if (k + 1 < temp.columns) {
  765. ret += ", ";
  766. }
  767. }
  768. ret += "}";
  769. if (j + 1 < temp.rows) {
  770. ret += ", ";
  771. }
  772. }
  773. ret += "}";
  774. break;
  775. case Types.REAL:
  776. ret += "<- {";
  777. for (let j = 0; j < temp.rows; j++) {
  778. ret += "{";
  779. for (let k = 0; k < temp.columns; k++) {
  780. ret += temp.value[j][k].toFixed(2);
  781. if (k + 1 < temp.columns) {
  782. ret += ", ";
  783. }
  784. }
  785. ret += "}";
  786. if (j + 1 < temp.rows) {
  787. ret += ", ";
  788. }
  789. }
  790. ret += "}";
  791. break;
  792. case Types.TEXT:
  793. ret += "<- {";
  794. for (let j = 0; j < temp.rows; j++) {
  795. ret += "{";
  796. for (let k = 0; k < temp.columns; k++) {
  797. ret += '"' + temp.value[j][k] + '"';
  798. if (k + 1 < temp.columns) {
  799. ret += ", ";
  800. }
  801. }
  802. ret += "}";
  803. if (j + 1 < temp.rows) {
  804. ret += ", ";
  805. }
  806. }
  807. ret += "}";
  808. break;
  809. case Types.BOOLEAN:
  810. ret += "<- {";
  811. for (let j = 0; j < temp.rows; j++) {
  812. ret += "{";
  813. for (let k = 0; k < temp.columns; k++) {
  814. if (temp.value[j][k]) {
  815. ret += LocalizedStrings.getUI("logic_value_true");
  816. } else {
  817. ret += LocalizedStrings.getUI("logic_value_false");
  818. }
  819. if (k + 1 < temp.columns) {
  820. ret += ", ";
  821. }
  822. }
  823. ret += "}";
  824. if (j + 1 < temp.rows) {
  825. ret += ", ";
  826. }
  827. }
  828. ret += "}";
  829. break;
  830. case Types.CHAR:
  831. ret += "<- {";
  832. for (let j = 0; j < temp.rows; j++) {
  833. ret += "{";
  834. for (let k = 0; k < temp.columns; k++) {
  835. ret += "'" + temp.value[j][k] + "'";
  836. if (k + 1 < temp.columns) {
  837. ret += ", ";
  838. }
  839. }
  840. ret += "}";
  841. if (j + 1 < temp.rows) {
  842. ret += ", ";
  843. }
  844. }
  845. ret += "}";
  846. break;
  847. }
  848. } else {
  849. switch (temp.type) {
  850. case Types.INTEGER:
  851. ret += "<- " + temp.value;
  852. break;
  853. case Types.REAL:
  854. ret += "<- " + temp.value.toFixed(2);
  855. break;
  856. case Types.TEXT:
  857. ret += '<- "' + temp.value + '"';
  858. break;
  859. case Types.BOOLEAN:
  860. ret += "<- ";
  861. if (temp.value) {
  862. ret += LocalizedStrings.getUI("logic_value_true");
  863. } else {
  864. ret += LocalizedStrings.getUI("logic_value_false");
  865. }
  866. break;
  867. case Types.CHAR:
  868. ret += "<- '" + temp.value + "'";
  869. break;
  870. }
  871. }
  872. return ret;
  873. }
  874. function globalsCode () {
  875. let ret = "";
  876. if (window.program_obj.globals) {
  877. for (let i = 0; i < window.program_obj.globals.length; i++) {
  878. const temp = window.program_obj.globals[i];
  879. ret += "\n\t";
  880. if (temp.is_constant) {
  881. ret += "const ";
  882. }
  883. switch (temp.type) {
  884. case Types.INTEGER:
  885. ret += LocalizedStrings.getUI("type_integer");
  886. break;
  887. case Types.REAL:
  888. ret += LocalizedStrings.getUI("type_real");
  889. break;
  890. case Types.TEXT:
  891. ret += LocalizedStrings.getUI("type_text");
  892. break;
  893. case Types.BOOLEAN:
  894. ret += LocalizedStrings.getUI("type_boolean");
  895. break;
  896. case Types.CHAR:
  897. ret += LocalizedStrings.getUI("type_char");
  898. break;
  899. }
  900. ret += " " + temp.name + " ";
  901. if (temp.dimensions == 1) {
  902. ret += "[" + temp.columns + "] ";
  903. switch (temp.type) {
  904. case Types.INTEGER:
  905. ret += "<- {";
  906. for (let j = 0; j < temp.value.length; j++) {
  907. ret += temp.value[j];
  908. if (j + 1 < temp.value.length) {
  909. ret += ", ";
  910. }
  911. }
  912. ret += "}";
  913. break;
  914. case Types.REAL:
  915. ret += "<- {";
  916. for (let j = 0; j < temp.value.length; j++) {
  917. ret += temp.value[j].toFixed(2);
  918. if (j + 1 < temp.value.length) {
  919. ret += ", ";
  920. }
  921. }
  922. ret += "}";
  923. break;
  924. case Types.TEXT:
  925. ret += "<- {";
  926. for (let j = 0; j < temp.value.length; j++) {
  927. ret += '"' + temp.value[j] + '"';
  928. if (j + 1 < temp.value.length) {
  929. ret += ", ";
  930. }
  931. }
  932. ret += "}";
  933. break;
  934. case Types.BOOLEAN:
  935. ret += "<- {";
  936. for (let j = 0; j < temp.value.length; j++) {
  937. if (temp.value[j]) {
  938. ret += LocalizedStrings.getUI("logic_value_true");
  939. } else {
  940. ret += LocalizedStrings.getUI("logic_value_false");
  941. }
  942. if (j + 1 < temp.value.length) {
  943. ret += ", ";
  944. }
  945. }
  946. ret += "}";
  947. break;
  948. case Types.CHAR:
  949. ret += "<- {";
  950. for (let j = 0; j < temp.value.length; j++) {
  951. ret += "'" + temp.value[j] + "'";
  952. if (j + 1 < temp.value.length) {
  953. ret += ", ";
  954. }
  955. }
  956. ret += "}";
  957. break;
  958. }
  959. } else if (temp.dimensions == 2) {
  960. ret += "[" + temp.rows + "][" + temp.columns + "] ";
  961. switch (temp.type) {
  962. case Types.INTEGER:
  963. ret += "<- {";
  964. for (let j = 0; j < temp.rows; j++) {
  965. ret += "{";
  966. for (let k = 0; k < temp.columns; k++) {
  967. ret += temp.value[j][k];
  968. if (k + 1 < temp.columns) {
  969. ret += ", ";
  970. }
  971. }
  972. ret += "}";
  973. if (j + 1 < temp.rows) {
  974. ret += ", ";
  975. }
  976. }
  977. ret += "}";
  978. break;
  979. case Types.REAL:
  980. ret += "<- {";
  981. for (let j = 0; j < temp.rows; j++) {
  982. ret += "{";
  983. for (let k = 0; k < temp.columns; k++) {
  984. ret += temp.value[j][k].toFixed(2);
  985. if (k + 1 < temp.columns) {
  986. ret += ", ";
  987. }
  988. }
  989. ret += "}";
  990. if (j + 1 < temp.rows) {
  991. ret += ", ";
  992. }
  993. }
  994. ret += "}";
  995. break;
  996. case Types.TEXT:
  997. ret += "<- {";
  998. for (let j = 0; j < temp.rows; j++) {
  999. ret += "{";
  1000. for (let k = 0; k < temp.columns; k++) {
  1001. ret += '"' + temp.value[j][k] + '"';
  1002. if (k + 1 < temp.columns) {
  1003. ret += ", ";
  1004. }
  1005. }
  1006. ret += "}";
  1007. if (j + 1 < temp.rows) {
  1008. ret += ", ";
  1009. }
  1010. }
  1011. ret += "}";
  1012. break;
  1013. case Types.BOOLEAN:
  1014. ret += "<- {";
  1015. for (let j = 0; j < temp.rows; j++) {
  1016. ret += "{";
  1017. for (let k = 0; k < temp.columns; k++) {
  1018. if (temp.value[j][k]) {
  1019. ret += LocalizedStrings.getUI("logic_value_true");
  1020. } else {
  1021. ret += LocalizedStrings.getUI("logic_value_false");
  1022. }
  1023. if (k + 1 < temp.columns) {
  1024. ret += ", ";
  1025. }
  1026. }
  1027. ret += "}";
  1028. if (j + 1 < temp.rows) {
  1029. ret += ", ";
  1030. }
  1031. }
  1032. ret += "}";
  1033. break;
  1034. case Types.CHAR:
  1035. ret += "<- {";
  1036. for (let j = 0; j < temp.rows; j++) {
  1037. ret += "{";
  1038. for (let k = 0; k < temp.columns; k++) {
  1039. ret += "'" + temp.value[j][k] + "'";
  1040. if (k + 1 < temp.columns) {
  1041. ret += ", ";
  1042. }
  1043. }
  1044. ret += "}";
  1045. if (j + 1 < temp.rows) {
  1046. ret += ", ";
  1047. }
  1048. }
  1049. ret += "}";
  1050. break;
  1051. }
  1052. } else {
  1053. switch (temp.type) {
  1054. case Types.INTEGER:
  1055. ret += "<- " + temp.value;
  1056. break;
  1057. case Types.REAL:
  1058. ret += "<- " + temp.value.toFixed(2);
  1059. break;
  1060. case Types.TEXT:
  1061. ret += '<- "' + temp.value + '"';
  1062. break;
  1063. case Types.BOOLEAN:
  1064. ret += "<- ";
  1065. if (temp.value) {
  1066. ret += LocalizedStrings.getUI("logic_value_true");
  1067. } else {
  1068. ret += LocalizedStrings.getUI("logic_value_false");
  1069. }
  1070. break;
  1071. case Types.CHAR:
  1072. ret += "<- '" + temp.value + "'";
  1073. break;
  1074. }
  1075. }
  1076. }
  1077. }
  1078. return ret;
  1079. }