code_generator.js 26 KB

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