code_generator.js 32 KB

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