code_generator.js 33 KB

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