code_generator.js 32 KB

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