code_generator.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305
  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. export 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. variable_obj.reference_dimensions == 1
  584. ) {
  585. ret += " [ " + variableValueMenuCode(variable_obj.row) + " ] ";
  586. } else if (
  587. variable_obj.content.dimensions == 2 &&
  588. variable_obj.dimensions != 2
  589. ) {
  590. ret += " [ " + variableValueMenuCode(variable_obj.row) + " ] ";
  591. ret += " [ " + variableValueMenuCode(variable_obj.column) + " ] ";
  592. }
  593. } else {
  594. const content = String(variable_obj.content);
  595. if (isNaN(content)) {
  596. // console.debug("Content length: ", content.length);
  597. // console.debug("Char code at 0", content.charCodeAt(0));
  598. const isBackslash = content.charCodeAt(0) === 92;
  599. if (content.length === 1 || (content.length === 2 && isBackslash)) {
  600. ret += `'${content}'`;
  601. } else {
  602. ret += '"' + variable_obj.content + '"';
  603. }
  604. } else if (content.length == 0) {
  605. ret += '""';
  606. } else if (content.trim().length == 0) {
  607. ret += '"' + content + '"';
  608. } else {
  609. ret += variable_obj.content;
  610. }
  611. }
  612. } catch (err) {
  613. if (!is_return) {
  614. Utils.renderErrorMessage(
  615. variable_obj.dom_object,
  616. LocalizedStrings.getUI("inform_valid_content")
  617. );
  618. throw err;
  619. }
  620. }
  621. return ret;
  622. }
  623. function writersCode (command_obj, indentation) {
  624. let ret = "\n";
  625. for (let i = 0; i < indentation; i++) {
  626. ret += "\t";
  627. }
  628. ret += LocalizedStrings.getUI("text_command_write") + " ( ";
  629. /*for (var i = 0; i < command_obj.content.length; i++) {
  630. ret += variableValueMenuCode(command_obj.content[i]);
  631. if ((i + 1) < command_obj.content.length) {
  632. ret += ' + ';
  633. }
  634. }*/
  635. ret += elementExpressionCode(command_obj.content);
  636. if (command_obj.newline) {
  637. ret += ', "\\n"';
  638. }
  639. ret += " ) ";
  640. return ret;
  641. }
  642. function commentsCode (command_obj, indentation) {
  643. let ret = "\n";
  644. for (let i = 0; i < indentation; i++) {
  645. ret += "\t";
  646. }
  647. ret += "// ";
  648. ret += command_obj.comment_text.content;
  649. return ret;
  650. }
  651. function parametersCode (parameter_obj) {
  652. let ret = "";
  653. switch (parameter_obj.type) {
  654. case Types.INTEGER:
  655. ret += " " + LocalizedStrings.getUI("type_integer") + " ";
  656. break;
  657. case Types.REAL:
  658. ret += " " + LocalizedStrings.getUI("type_real") + " ";
  659. break;
  660. case Types.TEXT:
  661. ret += " " + LocalizedStrings.getUI("type_text") + " ";
  662. break;
  663. case Types.BOOLEAN:
  664. ret += " " + LocalizedStrings.getUI("type_boolean") + " ";
  665. break;
  666. case Types.CHAR:
  667. ret += " " + LocalizedStrings.getUI("type_char") + " ";
  668. break;
  669. }
  670. if (parameter_obj.reference)
  671. ret += "&";
  672. ret += parameter_obj.name + "";
  673. if (parameter_obj.dimensions == 1) {
  674. ret += " []";
  675. } else if (parameter_obj.dimensions == 2) {
  676. ret += " [][]";
  677. }
  678. return ret;
  679. }
  680. function variablesCode (variable_obj) {
  681. let ret = "";
  682. const temp = variable_obj;
  683. ret += "\n\t\t";
  684. if (temp.is_constant) {
  685. ret += "const ";
  686. }
  687. switch (temp.type) {
  688. case Types.INTEGER:
  689. ret += LocalizedStrings.getUI("type_integer") + " ";
  690. break;
  691. case Types.REAL:
  692. ret += LocalizedStrings.getUI("type_real") + " ";
  693. break;
  694. case Types.TEXT:
  695. ret += LocalizedStrings.getUI("type_text") + " ";
  696. break;
  697. case Types.BOOLEAN:
  698. ret += LocalizedStrings.getUI("type_boolean") + " ";
  699. break;
  700. case Types.CHAR:
  701. ret += LocalizedStrings.getUI("type_char") + " ";
  702. break;
  703. }
  704. ret += temp.name + " ";
  705. if (temp.dimensions == 1) {
  706. ret += "[" + temp.columns + "] ";
  707. switch (temp.type) {
  708. case Types.INTEGER:
  709. ret += "<- {";
  710. for (let j = 0; j < temp.value.length; j++) {
  711. ret += temp.value[j];
  712. if (j + 1 < temp.value.length) {
  713. ret += ", ";
  714. }
  715. }
  716. ret += "}";
  717. break;
  718. case Types.REAL:
  719. ret += "<- {";
  720. for (let j = 0; j < temp.value.length; j++) {
  721. ret += parseFloat(temp.value[j]).toFixed(2);
  722. if (j + 1 < temp.value.length) {
  723. ret += ", ";
  724. }
  725. }
  726. ret += "}";
  727. break;
  728. case Types.TEXT:
  729. ret += "<- {";
  730. for (let j = 0; j < temp.value.length; j++) {
  731. ret += '"' + temp.value[j] + '"';
  732. if (j + 1 < temp.value.length) {
  733. ret += ", ";
  734. }
  735. }
  736. ret += "}";
  737. break;
  738. case Types.BOOLEAN:
  739. ret += "<- {";
  740. for (let j = 0; j < temp.value.length; j++) {
  741. if (temp.value[j]) {
  742. ret += LocalizedStrings.getUI("logic_value_true");
  743. } else {
  744. ret += LocalizedStrings.getUI("logic_value_false");
  745. }
  746. if (j + 1 < temp.value.length) {
  747. ret += ", ";
  748. }
  749. }
  750. ret += "}";
  751. break;
  752. case Types.CHAR:
  753. ret += "<- {";
  754. for (let j = 0; j < temp.value.length; j++) {
  755. ret += "'" + temp.value[j] + "'";
  756. if (j + 1 < temp.value.length) {
  757. ret += ", ";
  758. }
  759. }
  760. ret += "}";
  761. break;
  762. }
  763. } else if (temp.dimensions == 2) {
  764. ret += "[" + temp.rows + "][" + temp.columns + "] ";
  765. switch (temp.type) {
  766. case Types.INTEGER:
  767. ret += "<- {";
  768. for (let j = 0; j < temp.rows; j++) {
  769. ret += "{";
  770. for (let k = 0; k < temp.columns; k++) {
  771. ret += temp.value[j][k];
  772. if (k + 1 < temp.columns) {
  773. ret += ", ";
  774. }
  775. }
  776. ret += "}";
  777. if (j + 1 < temp.rows) {
  778. ret += ", ";
  779. }
  780. }
  781. ret += "}";
  782. break;
  783. case Types.REAL:
  784. ret += "<- {";
  785. for (let j = 0; j < temp.rows; j++) {
  786. ret += "{";
  787. for (let k = 0; k < temp.columns; k++) {
  788. ret += parseFloat(temp.value[j][k]).toFixed(2);
  789. if (k + 1 < temp.columns) {
  790. ret += ", ";
  791. }
  792. }
  793. ret += "}";
  794. if (j + 1 < temp.rows) {
  795. ret += ", ";
  796. }
  797. }
  798. ret += "}";
  799. break;
  800. case Types.TEXT:
  801. ret += "<- {";
  802. for (let j = 0; j < temp.rows; j++) {
  803. ret += "{";
  804. for (let k = 0; k < temp.columns; k++) {
  805. ret += '"' + temp.value[j][k] + '"';
  806. if (k + 1 < temp.columns) {
  807. ret += ", ";
  808. }
  809. }
  810. ret += "}";
  811. if (j + 1 < temp.rows) {
  812. ret += ", ";
  813. }
  814. }
  815. ret += "}";
  816. break;
  817. case Types.BOOLEAN:
  818. ret += "<- {";
  819. for (let j = 0; j < temp.rows; j++) {
  820. ret += "{";
  821. for (let k = 0; k < temp.columns; k++) {
  822. if (temp.value[j][k]) {
  823. ret += LocalizedStrings.getUI("logic_value_true");
  824. } else {
  825. ret += LocalizedStrings.getUI("logic_value_false");
  826. }
  827. if (k + 1 < temp.columns) {
  828. ret += ", ";
  829. }
  830. }
  831. ret += "}";
  832. if (j + 1 < temp.rows) {
  833. ret += ", ";
  834. }
  835. }
  836. ret += "}";
  837. break;
  838. case Types.CHAR:
  839. ret += "<- {";
  840. for (let j = 0; j < temp.rows; j++) {
  841. ret += "{";
  842. for (let k = 0; k < temp.columns; k++) {
  843. ret += "'" + temp.value[j][k] + "'";
  844. if (k + 1 < temp.columns) {
  845. ret += ", ";
  846. }
  847. }
  848. ret += "}";
  849. if (j + 1 < temp.rows) {
  850. ret += ", ";
  851. }
  852. }
  853. ret += "}";
  854. break;
  855. }
  856. } else {
  857. switch (temp.type) {
  858. case Types.INTEGER:
  859. ret += "<- " + temp.value;
  860. break;
  861. case Types.REAL:
  862. ret += "<- " + parseFloat(temp.value).toFixed(2);
  863. break;
  864. case Types.TEXT:
  865. ret += '<- "' + temp.value + '"';
  866. break;
  867. case Types.BOOLEAN:
  868. ret += "<- ";
  869. if (temp.value) {
  870. ret += LocalizedStrings.getUI("logic_value_true");
  871. } else {
  872. ret += LocalizedStrings.getUI("logic_value_false");
  873. }
  874. break;
  875. case Types.CHAR:
  876. ret += "<- '" + temp.value + "'";
  877. break;
  878. }
  879. }
  880. return ret;
  881. }
  882. function globalsCode () {
  883. let ret = "";
  884. if (window.program_obj.globals) {
  885. for (let i = 0; i < window.program_obj.globals.length; i++) {
  886. const temp = window.program_obj.globals[i];
  887. ret += "\n\t";
  888. if (temp.is_constant) {
  889. ret += "const ";
  890. }
  891. switch (temp.type) {
  892. case Types.INTEGER:
  893. ret += LocalizedStrings.getUI("type_integer");
  894. break;
  895. case Types.REAL:
  896. ret += LocalizedStrings.getUI("type_real");
  897. break;
  898. case Types.TEXT:
  899. ret += LocalizedStrings.getUI("type_text");
  900. break;
  901. case Types.BOOLEAN:
  902. ret += LocalizedStrings.getUI("type_boolean");
  903. break;
  904. case Types.CHAR:
  905. ret += LocalizedStrings.getUI("type_char");
  906. break;
  907. }
  908. ret += " " + temp.name + " ";
  909. if (temp.dimensions == 1) {
  910. ret += "[" + temp.columns + "] ";
  911. switch (temp.type) {
  912. case Types.INTEGER:
  913. ret += "<- {";
  914. for (let j = 0; j < temp.value.length; j++) {
  915. ret += temp.value[j];
  916. if (j + 1 < temp.value.length) {
  917. ret += ", ";
  918. }
  919. }
  920. ret += "}";
  921. break;
  922. case Types.REAL:
  923. ret += "<- {";
  924. for (let j = 0; j < temp.value.length; j++) {
  925. ret += parseFloat(temp.value[j]).toFixed(2);
  926. if (j + 1 < temp.value.length) {
  927. ret += ", ";
  928. }
  929. }
  930. ret += "}";
  931. break;
  932. case Types.TEXT:
  933. ret += "<- {";
  934. for (let j = 0; j < temp.value.length; j++) {
  935. ret += '"' + temp.value[j] + '"';
  936. if (j + 1 < temp.value.length) {
  937. ret += ", ";
  938. }
  939. }
  940. ret += "}";
  941. break;
  942. case Types.BOOLEAN:
  943. ret += "<- {";
  944. for (let j = 0; j < temp.value.length; j++) {
  945. if (temp.value[j]) {
  946. ret += LocalizedStrings.getUI("logic_value_true");
  947. } else {
  948. ret += LocalizedStrings.getUI("logic_value_false");
  949. }
  950. if (j + 1 < temp.value.length) {
  951. ret += ", ";
  952. }
  953. }
  954. ret += "}";
  955. break;
  956. case Types.CHAR:
  957. ret += "<- {";
  958. for (let j = 0; j < temp.value.length; j++) {
  959. ret += "'" + temp.value[j] + "'";
  960. if (j + 1 < temp.value.length) {
  961. ret += ", ";
  962. }
  963. }
  964. ret += "}";
  965. break;
  966. }
  967. } else if (temp.dimensions == 2) {
  968. ret += "[" + temp.rows + "][" + temp.columns + "] ";
  969. switch (temp.type) {
  970. case Types.INTEGER:
  971. ret += "<- {";
  972. for (let j = 0; j < temp.rows; j++) {
  973. ret += "{";
  974. for (let k = 0; k < temp.columns; k++) {
  975. ret += temp.value[j][k];
  976. if (k + 1 < temp.columns) {
  977. ret += ", ";
  978. }
  979. }
  980. ret += "}";
  981. if (j + 1 < temp.rows) {
  982. ret += ", ";
  983. }
  984. }
  985. ret += "}";
  986. break;
  987. case Types.REAL:
  988. ret += "<- {";
  989. for (let j = 0; j < temp.rows; j++) {
  990. ret += "{";
  991. for (let k = 0; k < temp.columns; k++) {
  992. ret += parseFloat(temp.value[j][k]).toFixed(2);
  993. if (k + 1 < temp.columns) {
  994. ret += ", ";
  995. }
  996. }
  997. ret += "}";
  998. if (j + 1 < temp.rows) {
  999. ret += ", ";
  1000. }
  1001. }
  1002. ret += "}";
  1003. break;
  1004. case Types.TEXT:
  1005. ret += "<- {";
  1006. for (let j = 0; j < temp.rows; j++) {
  1007. ret += "{";
  1008. for (let k = 0; k < temp.columns; k++) {
  1009. ret += '"' + temp.value[j][k] + '"';
  1010. if (k + 1 < temp.columns) {
  1011. ret += ", ";
  1012. }
  1013. }
  1014. ret += "}";
  1015. if (j + 1 < temp.rows) {
  1016. ret += ", ";
  1017. }
  1018. }
  1019. ret += "}";
  1020. break;
  1021. case Types.BOOLEAN:
  1022. ret += "<- {";
  1023. for (let j = 0; j < temp.rows; j++) {
  1024. ret += "{";
  1025. for (let k = 0; k < temp.columns; k++) {
  1026. if (temp.value[j][k]) {
  1027. ret += LocalizedStrings.getUI("logic_value_true");
  1028. } else {
  1029. ret += LocalizedStrings.getUI("logic_value_false");
  1030. }
  1031. if (k + 1 < temp.columns) {
  1032. ret += ", ";
  1033. }
  1034. }
  1035. ret += "}";
  1036. if (j + 1 < temp.rows) {
  1037. ret += ", ";
  1038. }
  1039. }
  1040. ret += "}";
  1041. break;
  1042. case Types.CHAR:
  1043. ret += "<- {";
  1044. for (let j = 0; j < temp.rows; j++) {
  1045. ret += "{";
  1046. for (let k = 0; k < temp.columns; k++) {
  1047. ret += "'" + temp.value[j][k] + "'";
  1048. if (k + 1 < temp.columns) {
  1049. ret += ", ";
  1050. }
  1051. }
  1052. ret += "}";
  1053. if (j + 1 < temp.rows) {
  1054. ret += ", ";
  1055. }
  1056. }
  1057. ret += "}";
  1058. break;
  1059. }
  1060. } else {
  1061. switch (temp.type) {
  1062. case Types.INTEGER:
  1063. ret += "<- " + temp.value;
  1064. break;
  1065. case Types.REAL:
  1066. ret += "<- " + parseFloat(temp.value).toFixed(2);
  1067. break;
  1068. case Types.TEXT:
  1069. ret += '<- "' + temp.value + '"';
  1070. break;
  1071. case Types.BOOLEAN:
  1072. ret += "<- ";
  1073. if (temp.value) {
  1074. ret += LocalizedStrings.getUI("logic_value_true");
  1075. } else {
  1076. ret += LocalizedStrings.getUI("logic_value_false");
  1077. }
  1078. break;
  1079. case Types.CHAR:
  1080. ret += "<- '" + temp.value + "'";
  1081. break;
  1082. }
  1083. }
  1084. }
  1085. }
  1086. return ret;
  1087. }