code_generator.js 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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 += ' ' + variableValueMenuCode(command_obj.variable_value_menu, true);
  122. } catch(err) {}
  123. }
  124. return ret;
  125. }
  126. function breaksCode (command_obj, indentation) {
  127. var ret = '\n';
  128. for (var i = 0; i < indentation; i++) {
  129. ret += '\t';
  130. }
  131. ret += LocalizedStrings.getUI('text_break');
  132. return ret;
  133. }
  134. function switchsCode (command_obj, indentation) {
  135. var ret = '\n';
  136. for (var i = 0; i < indentation; i++) {
  137. ret += '\t';
  138. }
  139. ret += LocalizedStrings.getUI('text_code_switch') + ' ( ';
  140. ret += variableValueMenuCode(command_obj.variable);
  141. ret += ' ) { ';
  142. if (command_obj.cases) {
  143. for (var i = 0; i < command_obj.cases.length; i++) {
  144. ret += switchcasesCode(command_obj.cases[i], (indentation + 1));
  145. }
  146. }
  147. ret += '\n';
  148. for (var i = 0; i < indentation; i++) {
  149. ret += '\t';
  150. }
  151. ret += '} ';
  152. return ret;
  153. }
  154. function switchcasesCode (switchcase, indentation) {
  155. var ret = '\n';
  156. for (var i = 0; i < indentation; i++) {
  157. ret += '\t';
  158. }
  159. ret += LocalizedStrings.getUI('text_code_case') + ' ';
  160. ret += variableValueMenuCode(switchcase.variable_value_menu);
  161. ret += ' :';
  162. if (switchcase.commands_block) {
  163. for (var i = 0; i < switchcase.commands_block.length; i++) {
  164. ret += commandsCode(switchcase.commands_block[i], (indentation + 1));
  165. }
  166. }
  167. return ret;
  168. }
  169. function repeatNtimesCode (command_obj, indentation) {
  170. var ret = '\n';
  171. for (var i = 0; i < indentation; i++) {
  172. ret += '\t';
  173. }
  174. ret += LocalizedStrings.getUI('text_for') + ' ( ';
  175. if (command_obj.var_attribution) {
  176. ret += variableValueMenuCode(command_obj.var_attribution);
  177. ret += ' = ';
  178. ret += variableValueMenuCode(command_obj.expression1);
  179. }
  180. ret += ' ; ';
  181. if (command_obj.expression2) {
  182. switch (command_obj.expression2.expression.type) {
  183. case Models.EXPRESSION_TYPES.exp_logic:
  184. ret += logicExpressionCode(command_obj.expression2.expression);
  185. break;
  186. case Models.EXPRESSION_TYPES.exp_arithmetic:
  187. ret += arithmeticExpressionCode(command_obj.expression2.expression);
  188. break;
  189. }
  190. }
  191. ret += ' ; ';
  192. if (command_obj.var_incrementation) {
  193. ret += variableValueMenuCode(command_obj.var_incrementation);
  194. ret += ' = ';
  195. ret += variableValueMenuCode(command_obj.expression3.itens[0]);
  196. switch (command_obj.expression3.itens[1]) {
  197. case Models.ARITHMETIC_TYPES.plus:
  198. ret += ' + ';
  199. break;
  200. case Models.ARITHMETIC_TYPES.minus:
  201. ret += ' - ';
  202. break;
  203. case Models.ARITHMETIC_TYPES.multiplication:
  204. ret += ' * ';
  205. break;
  206. case Models.ARITHMETIC_TYPES.division:
  207. ret += ' / ';
  208. break;
  209. case Models.ARITHMETIC_TYPES.module:
  210. ret += ' % ';
  211. break;
  212. }
  213. ret += variableValueMenuCode(command_obj.expression3.itens[2]);
  214. }
  215. ret += ' ) { ';
  216. if (command_obj.commands_block) {
  217. for (var i = 0; i < command_obj.commands_block.length; i++) {
  218. ret += commandsCode(command_obj.commands_block[i], (indentation + 1));
  219. }
  220. }
  221. ret += '\n';
  222. for (var i = 0; i < indentation; i++) {
  223. ret += '\t';
  224. }
  225. ret += '}';
  226. return ret;
  227. }
  228. function iftruesCode (command_obj, indentation) {
  229. var ret = '\n';
  230. for (var i = 0; i < indentation; i++) {
  231. ret += '\t';
  232. }
  233. ret += LocalizedStrings.getUI('text_if');
  234. if (!command_obj.expression.expression) {
  235. Utils.renderErrorMessage(command_obj.expression.dom_object, LocalizedStrings.getUI('inform_valid_expression'));
  236. }
  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. if (!command_obj.expression.expression) {
  285. Utils.renderErrorMessage(command_obj.expression.dom_object, LocalizedStrings.getUI('inform_valid_expression'));
  286. }
  287. switch (command_obj.expression.expression.type) {
  288. case Models.EXPRESSION_TYPES.exp_logic:
  289. ret += logicExpressionCode(command_obj.expression.expression);
  290. break;
  291. case Models.EXPRESSION_TYPES.exp_arithmetic:
  292. ret += arithmeticExpressionCode(command_obj.expression.expression);
  293. break;
  294. }
  295. return ret;
  296. }
  297. function whiletruesCode (command_obj, indentation) {
  298. var ret = '\n';
  299. for (var i = 0; i < indentation; i++) {
  300. ret += '\t';
  301. }
  302. ret += LocalizedStrings.getUI('text_code_while');
  303. if (!command_obj.expression.expression) {
  304. Utils.renderErrorMessage(command_obj.expression.dom_object, LocalizedStrings.getUI('inform_valid_expression'));
  305. }
  306. switch (command_obj.expression.expression.type) {
  307. case Models.EXPRESSION_TYPES.exp_logic:
  308. ret += logicExpressionCode(command_obj.expression.expression);
  309. break;
  310. case Models.EXPRESSION_TYPES.exp_arithmetic:
  311. ret += arithmeticExpressionCode(command_obj.expression.expression);
  312. break;
  313. }
  314. ret += ' { ';
  315. if (command_obj.commands_block) {
  316. for (var i = 0; i < command_obj.commands_block.length; i++) {
  317. ret += commandsCode(command_obj.commands_block[i], (indentation + 1));
  318. }
  319. }
  320. ret += '\n';
  321. for (var i = 0; i < indentation; i++) {
  322. ret += '\t';
  323. }
  324. ret += '}';
  325. return ret;
  326. }
  327. function logicExpressionCode (expression) {
  328. var ret = ' ( ';
  329. if (expression.first_operand.type == Models.EXPRESSION_TYPES.exp_logic) {
  330. ret += logicExpressionCode(expression.first_operand);
  331. } else if (expression.first_operand.type == Models.EXPRESSION_TYPES.exp_arithmetic) {
  332. ret += arithmeticExpressionCode(expression.first_operand);
  333. } else {
  334. ret += variableValueMenuCode(expression.first_operand);
  335. }
  336. if (expression.operator) {
  337. switch (expression.operator) {
  338. case Models.LOGIC_COMPARISON.equals_to:
  339. ret += ' == ';
  340. break;
  341. case Models.LOGIC_COMPARISON.not_equals_to:
  342. ret += ' != ';
  343. break;
  344. case Models.LOGIC_COMPARISON.and:
  345. ret += ' && ';
  346. break;
  347. case Models.LOGIC_COMPARISON.or:
  348. ret += ' || ';
  349. break;
  350. }
  351. if (expression.second_operand.type == Models.EXPRESSION_TYPES.exp_logic) {
  352. ret += logicExpressionCode(expression.second_operand);
  353. } else if (expression.second_operand.type == Models.EXPRESSION_TYPES.exp_arithmetic) {
  354. ret += arithmeticExpressionCode(expression.second_operand);
  355. } else {
  356. ret += variableValueMenuCode(expression.second_operand);
  357. }
  358. }
  359. ret += ' ) ';
  360. return ret;
  361. }
  362. function arithmeticExpressionCode (expression) {
  363. var ret = ' ( ';
  364. if (expression.first_operand.type == Models.EXPRESSION_TYPES.exp_logic) {
  365. ret += logicExpressionCode(expression.first_operand);
  366. } else if (expression.first_operand.type == Models.EXPRESSION_TYPES.exp_arithmetic) {
  367. ret += arithmeticExpressionCode(expression.first_operand);
  368. } else {
  369. ret += variableValueMenuCode(expression.first_operand);
  370. }
  371. switch (expression.operator) {
  372. case Models.ARITHMETIC_COMPARISON.greater_than:
  373. ret += ' > ';
  374. break;
  375. case Models.ARITHMETIC_COMPARISON.less_than:
  376. ret += ' < ';
  377. break;
  378. case Models.ARITHMETIC_COMPARISON.equals_to:
  379. ret += ' == ';
  380. break;
  381. case Models.ARITHMETIC_COMPARISON.not_equals_to:
  382. ret += ' != ';
  383. break;
  384. case Models.ARITHMETIC_COMPARISON.greater_than_or_equals_to:
  385. ret += ' >= ';
  386. break;
  387. case Models.ARITHMETIC_COMPARISON.less_than_or_equals_to:
  388. ret += ' <= ';
  389. break;
  390. }
  391. if (expression.second_operand.type == Models.EXPRESSION_TYPES.exp_logic) {
  392. ret += logicExpressionCode(expression.second_operand);
  393. } else if (expression.second_operand.type == Models.EXPRESSION_TYPES.exp_arithmetic) {
  394. ret += arithmeticExpressionCode(expression.second_operand);
  395. } else {
  396. ret += variableValueMenuCode(expression.second_operand);
  397. }
  398. ret += ' ) ';
  399. return ret;
  400. }
  401. function attributionsCode (command_obj, indentation) {
  402. var ret = '\n';
  403. for (var i = 0; i < indentation; i++) {
  404. ret += '\t';
  405. }
  406. ret += variableValueMenuCode(command_obj.variable) + ' = ';
  407. for (var i = 0; i < command_obj.expression.length; i++) {
  408. ret += elementExpressionCode(command_obj.expression[i]);
  409. }
  410. return ret;
  411. }
  412. function elementExpressionCode (expression_obj) {
  413. var ret = '';
  414. for (var i = 0; i < expression_obj.itens.length; i++) {
  415. if (expression_obj.itens[i].type) {
  416. ret += variableValueMenuCode(expression_obj.itens[i]);
  417. } else if (expression_obj.itens[i].type_exp) {
  418. if (expression_obj.itens[i].type_exp == Models.EXPRESSION_ELEMENTS.par_exp_par) {
  419. ret += ' ( ';
  420. }
  421. ret += elementExpressionCode(expression_obj.itens[i]);
  422. if (expression_obj.itens[i].type_exp == Models.EXPRESSION_ELEMENTS.par_exp_par) {
  423. ret += ' ) ';
  424. }
  425. } else {
  426. switch (expression_obj.itens[i]) {
  427. case Models.ARITHMETIC_TYPES.plus:
  428. ret += ' + ';
  429. break;
  430. case Models.ARITHMETIC_TYPES.minus:
  431. ret += ' - ';
  432. break;
  433. case Models.ARITHMETIC_TYPES.multiplication:
  434. ret += ' * ';
  435. break;
  436. case Models.ARITHMETIC_TYPES.division:
  437. ret += ' / ';
  438. break;
  439. case Models.ARITHMETIC_TYPES.module:
  440. ret += ' % ';
  441. break;
  442. }
  443. }
  444. }
  445. return ret;
  446. }
  447. function functioncallsCode (command_obj, indentation) {
  448. var ret = '\n';
  449. for (var i = 0; i < indentation; i++) {
  450. ret += '\t';
  451. }
  452. ret += variableValueMenuCode(command_obj.function_called);
  453. return ret;
  454. }
  455. function readersCode (command_obj, indentation) {
  456. var ret = '\n';
  457. for (var i = 0; i < indentation; i++) {
  458. ret += '\t';
  459. }
  460. ret += LocalizedStrings.getUI('text_command_read') + ' ( ';
  461. ret += variableValueMenuCode(command_obj.variable_value_menu);
  462. ret += ' ) ';
  463. return ret;
  464. }
  465. function variableValueMenuCode (variable_obj, is_return = false) {
  466. var ret = '';
  467. try {
  468. if (variable_obj.function_called) {
  469. if (variable_obj.function_called.name) {
  470. ret += variable_obj.function_called.name + ' ( ';
  471. } else {
  472. ret += LocalizedStrings.getUI(variable_obj.function_called.category)+'.'+LocalizedStrings.getUI(variable_obj.function_called.identifier) + ' ( ';
  473. }
  474. if (variable_obj.parameters_list) {
  475. for (var i = 0; i < variable_obj.parameters_list.length; i++) {
  476. ret += variableValueMenuCode(variable_obj.parameters_list[i]);
  477. if ((i + 1) < variable_obj.parameters_list.length) {
  478. ret += ', ';
  479. }
  480. }
  481. }
  482. ret += ' )';
  483. } else if (variable_obj.content.type) {
  484. ret += variable_obj.content.name;
  485. if (variable_obj.content.dimensions == 1 && variable_obj.dimensions != 1) {
  486. ret += ' [ ' + variableValueMenuCode(variable_obj.column) + ' ] ';
  487. }
  488. if (variable_obj.content.dimensions == 2 && variable_obj.dimensions != 2) {
  489. ret += ' [ ' + variableValueMenuCode(variable_obj.row) + ' ] ';
  490. ret += ' [ ' + variableValueMenuCode(variable_obj.column) + ' ] ';
  491. }
  492. } else {
  493. if (isNaN(variable_obj.content)) {
  494. ret += '"' + variable_obj.content + '"';
  495. } else {
  496. ret += variable_obj.content;
  497. }
  498. }
  499. } catch (err) {
  500. if (!is_return) {
  501. Utils.renderErrorMessage(variable_obj.dom_object, LocalizedStrings.getUI('inform_valid_content'));
  502. throw err;
  503. }
  504. }
  505. return ret;
  506. }
  507. function writersCode (command_obj, indentation) {
  508. var ret = '\n';
  509. for (var i = 0; i < indentation; i++) {
  510. ret += '\t';
  511. }
  512. ret += LocalizedStrings.getUI('text_command_write') + ' ( ';
  513. for (var i = 0; i < command_obj.content.length; i++) {
  514. ret += variableValueMenuCode(command_obj.content[i]);
  515. if ((i + 1) < command_obj.content.length) {
  516. ret += ' + ';
  517. }
  518. }
  519. ret += ' ) ';
  520. return ret;
  521. }
  522. function commentsCode (command_obj, indentation) {
  523. var ret = '\n';
  524. for (var i = 0; i < indentation; i++) {
  525. ret += '\t';
  526. }
  527. ret += '// ';
  528. ret += command_obj.comment_text.content;
  529. return ret;
  530. }
  531. function parametersCode (parameter_obj) {
  532. var ret = '';
  533. switch (parameter_obj.type) {
  534. case Types.INTEGER:
  535. ret += ' '+LocalizedStrings.getUI('integer')+' ';
  536. break;
  537. case Types.REAL:
  538. ret += ' '+LocalizedStrings.getUI('real')+' ';
  539. break;
  540. case Types.TEXT:
  541. ret += ' '+LocalizedStrings.getUI('text')+' ';
  542. break;
  543. case Types.BOOLEAN:
  544. ret += ' '+LocalizedStrings.getUI('boolean')+' ';
  545. break;
  546. }
  547. ret += parameter_obj.name + '';
  548. if (parameter_obj.dimensions == 1) {
  549. ret += ' []'
  550. } else if (parameter_obj.dimensions == 2) {
  551. ret += ' [][]'
  552. }
  553. return ret;
  554. }
  555. function variablesCode (variable_obj) {
  556. var ret = '';
  557. var temp = variable_obj;
  558. ret += '\n\t\t';
  559. if (temp.is_constant) {
  560. ret += 'const ';
  561. }
  562. switch (temp.type) {
  563. case Types.INTEGER:
  564. ret += LocalizedStrings.getUI('integer')+' ';
  565. break;
  566. case Types.REAL:
  567. ret += LocalizedStrings.getUI('real')+' ';
  568. break;
  569. case Types.TEXT:
  570. ret += LocalizedStrings.getUI('text')+' ';
  571. break;
  572. case Types.BOOLEAN:
  573. ret += LocalizedStrings.getUI('boolean')+' ';
  574. break;
  575. }
  576. ret += temp.name + ' ';
  577. if (temp.dimensions == 1) {
  578. ret += '[' + temp.columns + '] ';
  579. switch (temp.type) {
  580. case Types.INTEGER:
  581. ret += '= {' + temp.value + '}';
  582. break;
  583. case Types.REAL:
  584. ret += '= {' + temp.value.toFixed(2) + '}';
  585. break;
  586. case Types.TEXT:
  587. ret += '= {';
  588. for (var j = 0; j < temp.value.length; j++) {
  589. ret += '"'+temp.value[j]+'"';
  590. if ((j + 1) < temp.value.length) {
  591. ret += ',';
  592. }
  593. }
  594. ret += '}';
  595. break;
  596. case Types.BOOLEAN:
  597. ret += '= {';
  598. for (var j = 0; j < temp.value.length; j++) {
  599. if (temp.value[j]) {
  600. ret += LocalizedStrings.getUI("true");
  601. } else {
  602. ret += LocalizedStrings.getUI("false");
  603. }
  604. if ((j + 1) < temp.value.length) {
  605. ret += ',';
  606. }
  607. }
  608. ret += '}';
  609. break;
  610. }
  611. } else if (temp.dimensions == 2) {
  612. ret += '[' + temp.rows + '][' + temp.columns + '] ';
  613. switch (temp.type) {
  614. case Types.INTEGER:
  615. ret += '= {';
  616. for (var j = 0; j < temp.rows; j++) {
  617. ret += '{' + temp.value[j] + '}';
  618. if ((j + 1) < temp.rows) {
  619. ret += ',';
  620. }
  621. }
  622. ret += '}';
  623. break;
  624. case Types.REAL:
  625. ret += '= {';
  626. for (var j = 0; j < temp.rows; j++) {
  627. ret += '{' + temp.value[j].toFixed(2) + '}';
  628. if ((j + 1) < temp.rows) {
  629. ret += ',';
  630. }
  631. }
  632. ret += '}';
  633. break;
  634. case Types.TEXT:
  635. ret += '= {';
  636. for (var j = 0; j < temp.rows; j++) {
  637. ret += '{';
  638. for (var k = 0; k < temp.columns; k++) {
  639. ret += '"' + temp.value[j][k] + '"';
  640. if ((k + 1) < temp.columns) {
  641. ret += ',';
  642. }
  643. }
  644. ret += '}';
  645. if ((j + 1) < temp.rows) {
  646. ret += ',';
  647. }
  648. }
  649. ret += '}';
  650. break;
  651. case Types.BOOLEAN:
  652. ret += '= {';
  653. for (var j = 0; j < temp.rows; j++) {
  654. ret += '{';
  655. for (var k = 0; k < temp.columns; k++) {
  656. if (temp.value[j][k]) {
  657. ret += LocalizedStrings.getUI("true");
  658. } else {
  659. ret += LocalizedStrings.getUI("false");
  660. }
  661. if ((k + 1) < temp.columns) {
  662. ret += ',';
  663. }
  664. }
  665. ret += '}';
  666. if ((j + 1) < temp.rows) {
  667. ret += ',';
  668. }
  669. }
  670. ret += '}';
  671. break;
  672. }
  673. } else {
  674. switch (temp.type) {
  675. case Types.INTEGER:
  676. ret += '= ' + temp.value;
  677. break;
  678. case Types.REAL:
  679. ret += '= ' + temp.value.toFixed(2);
  680. break;
  681. case Types.TEXT:
  682. ret += '= "' + temp.value + '"';
  683. break;
  684. case Types.BOOLEAN:
  685. ret += '= ';
  686. if (temp.value) {
  687. ret += LocalizedStrings.getUI("true");
  688. } else {
  689. ret += LocalizedStrings.getUI("false");
  690. }
  691. break;
  692. }
  693. }
  694. return ret;
  695. }
  696. function globalsCode () {
  697. var ret = '';
  698. if (window.program_obj.globals) {
  699. for (var i = 0; i < window.program_obj.globals.length; i++) {
  700. var temp = window.program_obj.globals[i];
  701. ret += '\n\t';
  702. if (temp.is_constant) {
  703. ret += 'const ';
  704. }
  705. switch (temp.type) {
  706. case Types.INTEGER:
  707. ret += LocalizedStrings.getUI('integer');
  708. break;
  709. case Types.REAL:
  710. ret += LocalizedStrings.getUI('real');
  711. break;
  712. case Types.TEXT:
  713. ret += LocalizedStrings.getUI('text');
  714. break;
  715. case Types.BOOLEAN:
  716. ret += LocalizedStrings.getUI('boolean');
  717. break;
  718. }
  719. ret += ' ' + temp.name + ' ';
  720. if (temp.dimensions == 1) {
  721. ret += '[' + temp.columns + '] ';
  722. switch (temp.type) {
  723. case Types.INTEGER:
  724. ret += '= {' + temp.value + '}';
  725. break;
  726. case Types.REAL:
  727. ret += '= {';
  728. for (var j = 0; j < temp.value.length; j++) {
  729. ret += temp.value[j].toFixed(2);
  730. if ((j + 1) < temp.value.length) {
  731. ret += ',';
  732. }
  733. }
  734. ret += '}';
  735. break;
  736. case Types.TEXT:
  737. ret += '= {';
  738. for (var j = 0; j < temp.value.length; j++) {
  739. ret += '"'+temp.value[j]+'"';
  740. if ((j + 1) < temp.value.length) {
  741. ret += ',';
  742. }
  743. }
  744. ret += '}';
  745. break;
  746. case Types.BOOLEAN:
  747. ret += '= {';
  748. for (var j = 0; j < temp.value.length; j++) {
  749. if (temp.value[j]) {
  750. ret += LocalizedStrings.getUI("true");
  751. } else {
  752. ret += LocalizedStrings.getUI("false");
  753. }
  754. if ((j + 1) < temp.value.length) {
  755. ret += ',';
  756. }
  757. }
  758. ret += '}';
  759. break;
  760. }
  761. } else if (temp.dimensions == 2) {
  762. ret += '[' + temp.rows + '][' + temp.columns + '] ';
  763. switch (temp.type) {
  764. case Types.INTEGER:
  765. ret += '= {';
  766. for (var j = 0; j < temp.rows; j++) {
  767. ret += '{' + temp.value[j] + '}';
  768. if ((j + 1) < temp.rows) {
  769. ret += ',';
  770. }
  771. }
  772. ret += '}';
  773. break;
  774. case Types.REAL:
  775. ret += '= {';
  776. for (var j = 0; j < temp.rows; j++) {
  777. ret += '{';
  778. for (var k = 0; k < temp.columns; k++) {
  779. ret += temp.value[j][k].toFixed(2);
  780. if ((k + 1) < temp.columns) {
  781. ret += ',';
  782. }
  783. }
  784. ret += '}';
  785. if ((j + 1) < temp.rows) {
  786. ret += ',';
  787. }
  788. }
  789. ret += '}';
  790. break;
  791. case Types.TEXT:
  792. ret += '= {';
  793. for (var j = 0; j < temp.rows; j++) {
  794. ret += '{';
  795. for (var k = 0; k < temp.columns; k++) {
  796. ret += '"' + temp.value[j][k] + '"';
  797. if ((k + 1) < temp.columns) {
  798. ret += ',';
  799. }
  800. }
  801. ret += '}';
  802. if ((j + 1) < temp.rows) {
  803. ret += ',';
  804. }
  805. }
  806. ret += '}';
  807. break;
  808. case Types.BOOLEAN:
  809. ret += '= {';
  810. for (var j = 0; j < temp.rows; j++) {
  811. ret += '{';
  812. for (var k = 0; k < temp.columns; k++) {
  813. if (temp.value[j][k]) {
  814. ret += LocalizedStrings.getUI("true");
  815. } else {
  816. ret += LocalizedStrings.getUI("false");
  817. }
  818. if ((k + 1) < temp.columns) {
  819. ret += ',';
  820. }
  821. }
  822. ret += '}';
  823. if ((j + 1) < temp.rows) {
  824. ret += ',';
  825. }
  826. }
  827. ret += '}';
  828. break;
  829. }
  830. } else {
  831. switch (temp.type) {
  832. case Types.INTEGER:
  833. ret += '= ' + temp.value;
  834. break;
  835. case Types.REAL:
  836. ret += '= ' + temp.value.toFixed(2);
  837. break;
  838. case Types.TEXT:
  839. ret += '= "' + temp.value + '"';
  840. break;
  841. case Types.BOOLEAN:
  842. ret += '= ';
  843. if (temp.value) {
  844. ret += LocalizedStrings.getUI("true");;
  845. } else {
  846. ret += LocalizedStrings.getUI("false");;
  847. }
  848. break;
  849. }
  850. }
  851. }
  852. }
  853. return ret;
  854. }