codeParser.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import * as Models from '../visualUI/ivprog_elements';
  2. import { LocalizedStrings } from "./../services/localizedStringsService";
  3. import * as VariableValueMenuManagement from '../visualUI/commands/variable_value_menu';
  4. import * as CodeParser from '../visualUI/commands/generic_expression';
  5. function parseBoolean (var_value, dimensions) {
  6. if (dimensions == 0)
  7. return var_value == LocalizedStrings.getUI("logic_value_true");
  8. if (dimensions == 1) {
  9. var final_array = [];
  10. var_value.forEach(function(el){
  11. final_array.push(el == LocalizedStrings.getUI("logic_value_true"));
  12. });
  13. return final_array;
  14. }
  15. if (dimensions == 2) {
  16. var final_array = [];
  17. var_value.forEach(function(row){
  18. var temp = [];
  19. row.forEach(function(el){
  20. temp.push(el == LocalizedStrings.getUI("logic_value_true"));
  21. });
  22. final_array.push(temp);
  23. });
  24. return final_array;
  25. }
  26. }
  27. function parseGlobal (global_obj) {
  28. var new_global = new Models.Variable(
  29. global_obj.type,
  30. global_obj.name,
  31. global_obj.value);
  32. new_global.is_constant = global_obj.is_const;
  33. new_global.columns = global_obj.columns;
  34. new_global.dimensions = global_obj.dimension;
  35. new_global.rows = global_obj.rows;
  36. if (global_obj.type == "boolean")
  37. new_global.value = parseBoolean(global_obj.value, global_obj.dimension);
  38. window.program_obj.addGlobal(new_global);
  39. }
  40. function parseParameter (parameter_obj) {
  41. const new_parameter = new Models.Variable(
  42. parameter_obj.type,
  43. parameter_obj.name,
  44. null,
  45. parameter_obj.dimension,
  46. parameter_obj.is_const,
  47. parameter_obj.rows,
  48. parameter_obj.columns,
  49. parameter_obj.reference
  50. );
  51. new_parameter.value = parameter_obj.value;
  52. if (parameter_obj.type == "boolean" && parameter_obj.value)
  53. new_parameter.value = parseBoolean(parameter_obj.value, parameter_obj.dimension);
  54. return new_parameter;
  55. }
  56. function parseFunction (function_obj) {
  57. const new_function = new Models.Function(
  58. function_obj.name,
  59. function_obj.return_type,
  60. function_obj.return_dimensions,
  61. [],
  62. false,
  63. false,
  64. []);
  65. if (!new_function.name) {
  66. new_function.name = LocalizedStrings.getUI("start");
  67. new_function.is_main = true;
  68. }
  69. if (function_obj.parameters_list) {
  70. function_obj.parameters_list.forEach(function(el){
  71. new_function.parameters_list.push(parseParameter(el));
  72. });
  73. }
  74. if (function_obj.variables_list) {
  75. function_obj.variables_list.forEach(function(el){
  76. new_function.variables_list.push(parseParameter(el));
  77. });
  78. }
  79. window.program_obj.addFunction(new_function);
  80. }
  81. export function parserCodeVisual (code_obj = null) {
  82. window.conteudo = code_obj
  83. // Globals:
  84. window.program_obj.globals = [];
  85. code_obj.globals.forEach(parseGlobal);
  86. // Functions:
  87. window.program_obj.functions = [];
  88. code_obj.functions.forEach(parseFunction);
  89. // Commands:
  90. window.program_obj.functions.forEach(function(preparedFunction) {
  91. code_obj.functions.forEach(function(rawFunction) {
  92. if ((preparedFunction.name == rawFunction.name)
  93. ||
  94. (!rawFunction.name && preparedFunction.name == LocalizedStrings.getUI("start"))) {
  95. preparedFunction.commands = parseCommands(rawFunction.commands, preparedFunction);
  96. }
  97. })
  98. });
  99. }
  100. function parseCommands (commands_block, function_obj) {
  101. if (Array.isArray(commands_block)) {
  102. var temp = [];
  103. commands_block.forEach(function(command) {
  104. temp.push(parseCommands(command, function_obj));
  105. });
  106. return temp;
  107. }
  108. else {
  109. switch (commands_block.type) {
  110. case Models.COMMAND_TYPES.reader:
  111. return parseReader(commands_block, function_obj);
  112. case Models.COMMAND_TYPES.return:
  113. return parseReturn(commands_block, function_obj);
  114. case Models.COMMAND_TYPES.writer:
  115. return parseWriter(commands_block, function_obj);
  116. case Models.COMMAND_TYPES.break:
  117. return parseBreak(commands_block, function_obj);
  118. case Models.COMMAND_TYPES.comment:
  119. return parseComment(commands_block, function_obj);
  120. case Models.COMMAND_TYPES.attribution:
  121. return parseAttribution(commands_block, function_obj);
  122. case Models.COMMAND_TYPES.functioncall:
  123. return parseFunctionCall(commands_block, function_obj);
  124. }
  125. return null;
  126. }
  127. }
  128. function parseReader (command, function_obj) {
  129. var temp = CodeParser.expressionParserCodeVisual(command.variable, function_obj);
  130. if (Array.isArray(temp) && temp[0])
  131. temp = temp[0];
  132. return new Models.Reader(
  133. temp
  134. );
  135. }
  136. function parseReturn (command, function_obj) {
  137. var temp = CodeParser.expressionParserCodeVisual(command.expression, function_obj);
  138. return new Models.Return(
  139. temp
  140. );
  141. }
  142. function parseWriter (command, function_obj) {
  143. var temp = CodeParser.expressionParserCodeVisual(command.content[0], function_obj);
  144. return new Models.Writer(
  145. temp,
  146. command.newLine
  147. );
  148. }
  149. function parseBreak (command, function_obj) {
  150. return new Models.Break();
  151. }
  152. function parseAttribution (command, function_obj) {
  153. var variable = CodeParser.expressionParserCodeVisual(command.variable, function_obj);
  154. var expression = CodeParser.expressionParserCodeVisual(command.expression, function_obj);
  155. if (Array.isArray(variable))
  156. variable = variable[0];
  157. return new Models.Attribution(
  158. variable,
  159. expression
  160. );
  161. }
  162. function parseComment(command, function_obj) {
  163. // TODO
  164. return new Models.Comment(
  165. null
  166. );
  167. }
  168. function parseFunctionCall (command, function_obj) {
  169. var parameters = CodeParser.expressionParserCodeVisual(command.parameters_list[0], function_obj);
  170. var function_called = CodeParser.searchFunction(command.name);
  171. //function_called.parameters_list = parameters;
  172. console.log('atenção: ', parameters, command.parameters_list);
  173. var temp = new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.only_function, null, null, null, false);
  174. temp.function_called = function_called;
  175. temp.parameters_list = parameters;
  176. return new Models.FunctionCall(
  177. temp,
  178. parameters
  179. );
  180. //new Models.FunctionCall(new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.only_function, null, null, null, false), null
  181. }