1
0

code_generator.js 26 KB

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