code_generator.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  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. }