code_generator.js 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  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('integer');
  32. break;
  33. case Types.REAL:
  34. ret += LocalizedStrings.getUI('real');
  35. break;
  36. case Types.TEXT:
  37. ret += LocalizedStrings.getUI('text');
  38. break;
  39. case Types.BOOLEAN:
  40. ret += LocalizedStrings.getUI('boolean');
  41. break;
  42. case Types.VOID:
  43. ret += LocalizedStrings.getUI('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('and') + ' ';
  459. break;
  460. case Models.LOGIC_COMPARISON.or:
  461. ret += ' ' + LocalizedStrings.getUI('or') + ' ';
  462. break;
  463. case Models.LOGIC_COMPARISON.not:
  464. ret += ' ' + LocalizedStrings.getUI('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.getUI(variable_obj.function_called.category)+'.'+LocalizedStrings.getUI(variable_obj.function_called.identifier) + ' ( ';
  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('integer')+' ';
  575. break;
  576. case Types.REAL:
  577. ret += ' '+LocalizedStrings.getUI('real')+' ';
  578. break;
  579. case Types.TEXT:
  580. ret += ' '+LocalizedStrings.getUI('text')+' ';
  581. break;
  582. case Types.BOOLEAN:
  583. ret += ' '+LocalizedStrings.getUI('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('integer')+' ';
  604. break;
  605. case Types.REAL:
  606. ret += LocalizedStrings.getUI('real')+' ';
  607. break;
  608. case Types.TEXT:
  609. ret += LocalizedStrings.getUI('text')+' ';
  610. break;
  611. case Types.BOOLEAN:
  612. ret += LocalizedStrings.getUI('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 += '= {' + temp.value + '}';
  621. break;
  622. case Types.REAL:
  623. ret += '= {' + temp.value.toFixed(2) + '}';
  624. break;
  625. case Types.TEXT:
  626. ret += '= {';
  627. for (var j = 0; j < temp.value.length; j++) {
  628. ret += '"'+temp.value[j]+'"';
  629. if ((j + 1) < temp.value.length) {
  630. ret += ',';
  631. }
  632. }
  633. ret += '}';
  634. break;
  635. case Types.BOOLEAN:
  636. ret += '= {';
  637. for (var j = 0; j < temp.value.length; j++) {
  638. if (temp.value[j]) {
  639. ret += LocalizedStrings.getUI("true");
  640. } else {
  641. ret += LocalizedStrings.getUI("false");
  642. }
  643. if ((j + 1) < temp.value.length) {
  644. ret += ',';
  645. }
  646. }
  647. ret += '}';
  648. break;
  649. }
  650. } else if (temp.dimensions == 2) {
  651. ret += '[' + temp.rows + '][' + temp.columns + '] ';
  652. switch (temp.type) {
  653. case Types.INTEGER:
  654. ret += '= {';
  655. for (var j = 0; j < temp.rows; j++) {
  656. ret += '{' + temp.value[j] + '}';
  657. if ((j + 1) < temp.rows) {
  658. ret += ',';
  659. }
  660. }
  661. ret += '}';
  662. break;
  663. case Types.REAL:
  664. ret += '= {';
  665. for (var j = 0; j < temp.rows; j++) {
  666. ret += '{' + temp.value[j].toFixed(2) + '}';
  667. if ((j + 1) < temp.rows) {
  668. ret += ',';
  669. }
  670. }
  671. ret += '}';
  672. break;
  673. case Types.TEXT:
  674. ret += '= {';
  675. for (var j = 0; j < temp.rows; j++) {
  676. ret += '{';
  677. for (var k = 0; k < temp.columns; k++) {
  678. ret += '"' + temp.value[j][k] + '"';
  679. if ((k + 1) < temp.columns) {
  680. ret += ',';
  681. }
  682. }
  683. ret += '}';
  684. if ((j + 1) < temp.rows) {
  685. ret += ',';
  686. }
  687. }
  688. ret += '}';
  689. break;
  690. case Types.BOOLEAN:
  691. ret += '= {';
  692. for (var j = 0; j < temp.rows; j++) {
  693. ret += '{';
  694. for (var k = 0; k < temp.columns; k++) {
  695. if (temp.value[j][k]) {
  696. ret += LocalizedStrings.getUI("true");
  697. } else {
  698. ret += LocalizedStrings.getUI("false");
  699. }
  700. if ((k + 1) < temp.columns) {
  701. ret += ',';
  702. }
  703. }
  704. ret += '}';
  705. if ((j + 1) < temp.rows) {
  706. ret += ',';
  707. }
  708. }
  709. ret += '}';
  710. break;
  711. }
  712. } else {
  713. switch (temp.type) {
  714. case Types.INTEGER:
  715. ret += '= ' + temp.value;
  716. break;
  717. case Types.REAL:
  718. ret += '= ' + temp.value.toFixed(2);
  719. break;
  720. case Types.TEXT:
  721. ret += '= "' + temp.value + '"';
  722. break;
  723. case Types.BOOLEAN:
  724. ret += '= ';
  725. if (temp.value) {
  726. ret += LocalizedStrings.getUI("true");
  727. } else {
  728. ret += LocalizedStrings.getUI("false");
  729. }
  730. break;
  731. }
  732. }
  733. return ret;
  734. }
  735. function globalsCode () {
  736. var ret = '';
  737. if (window.program_obj.globals) {
  738. for (var i = 0; i < window.program_obj.globals.length; i++) {
  739. var temp = window.program_obj.globals[i];
  740. ret += '\n\t';
  741. if (temp.is_constant) {
  742. ret += 'const ';
  743. }
  744. switch (temp.type) {
  745. case Types.INTEGER:
  746. ret += LocalizedStrings.getUI('integer');
  747. break;
  748. case Types.REAL:
  749. ret += LocalizedStrings.getUI('real');
  750. break;
  751. case Types.TEXT:
  752. ret += LocalizedStrings.getUI('text');
  753. break;
  754. case Types.BOOLEAN:
  755. ret += LocalizedStrings.getUI('boolean');
  756. break;
  757. }
  758. ret += ' ' + temp.name + ' ';
  759. if (temp.dimensions == 1) {
  760. ret += '[' + temp.columns + '] ';
  761. switch (temp.type) {
  762. case Types.INTEGER:
  763. ret += '= {' + temp.value + '}';
  764. break;
  765. case Types.REAL:
  766. ret += '= {';
  767. for (var j = 0; j < temp.value.length; j++) {
  768. ret += temp.value[j].toFixed(2);
  769. if ((j + 1) < temp.value.length) {
  770. ret += ',';
  771. }
  772. }
  773. ret += '}';
  774. break;
  775. case Types.TEXT:
  776. ret += '= {';
  777. for (var j = 0; j < temp.value.length; j++) {
  778. ret += '"'+temp.value[j]+'"';
  779. if ((j + 1) < temp.value.length) {
  780. ret += ',';
  781. }
  782. }
  783. ret += '}';
  784. break;
  785. case Types.BOOLEAN:
  786. ret += '= {';
  787. for (var j = 0; j < temp.value.length; j++) {
  788. if (temp.value[j]) {
  789. ret += LocalizedStrings.getUI("true");
  790. } else {
  791. ret += LocalizedStrings.getUI("false");
  792. }
  793. if ((j + 1) < temp.value.length) {
  794. ret += ',';
  795. }
  796. }
  797. ret += '}';
  798. break;
  799. }
  800. } else if (temp.dimensions == 2) {
  801. ret += '[' + temp.rows + '][' + temp.columns + '] ';
  802. switch (temp.type) {
  803. case Types.INTEGER:
  804. ret += '= {';
  805. for (var j = 0; j < temp.rows; j++) {
  806. ret += '{' + temp.value[j] + '}';
  807. if ((j + 1) < temp.rows) {
  808. ret += ',';
  809. }
  810. }
  811. ret += '}';
  812. break;
  813. case Types.REAL:
  814. ret += '= {';
  815. for (var j = 0; j < temp.rows; j++) {
  816. ret += '{';
  817. for (var k = 0; k < temp.columns; k++) {
  818. ret += temp.value[j][k].toFixed(2);
  819. if ((k + 1) < temp.columns) {
  820. ret += ',';
  821. }
  822. }
  823. ret += '}';
  824. if ((j + 1) < temp.rows) {
  825. ret += ',';
  826. }
  827. }
  828. ret += '}';
  829. break;
  830. case Types.TEXT:
  831. ret += '= {';
  832. for (var j = 0; j < temp.rows; j++) {
  833. ret += '{';
  834. for (var k = 0; k < temp.columns; k++) {
  835. ret += '"' + temp.value[j][k] + '"';
  836. if ((k + 1) < temp.columns) {
  837. ret += ',';
  838. }
  839. }
  840. ret += '}';
  841. if ((j + 1) < temp.rows) {
  842. ret += ',';
  843. }
  844. }
  845. ret += '}';
  846. break;
  847. case Types.BOOLEAN:
  848. ret += '= {';
  849. for (var j = 0; j < temp.rows; j++) {
  850. ret += '{';
  851. for (var k = 0; k < temp.columns; k++) {
  852. if (temp.value[j][k]) {
  853. ret += LocalizedStrings.getUI("true");
  854. } else {
  855. ret += LocalizedStrings.getUI("false");
  856. }
  857. if ((k + 1) < temp.columns) {
  858. ret += ',';
  859. }
  860. }
  861. ret += '}';
  862. if ((j + 1) < temp.rows) {
  863. ret += ',';
  864. }
  865. }
  866. ret += '}';
  867. break;
  868. }
  869. } else {
  870. switch (temp.type) {
  871. case Types.INTEGER:
  872. ret += '= ' + temp.value;
  873. break;
  874. case Types.REAL:
  875. ret += '= ' + temp.value.toFixed(2);
  876. break;
  877. case Types.TEXT:
  878. ret += '= "' + temp.value + '"';
  879. break;
  880. case Types.BOOLEAN:
  881. ret += '= ';
  882. if (temp.value) {
  883. ret += LocalizedStrings.getUI("true");;
  884. } else {
  885. ret += LocalizedStrings.getUI("false");;
  886. }
  887. break;
  888. }
  889. }
  890. }
  891. }
  892. return ret;
  893. }