code_generator.js 23 KB

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