code_generator.js 24 KB

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