code_generator.js 29 KB

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