codeParser.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. case Models.COMMAND_TYPES.iftrue:
  125. return parseIfTrue(commands_block, function_obj);
  126. //case Models.COMMAND_TYPES.repeatNtimes:
  127. default:
  128. return parseRepeatNTimes(commands_block, function_obj);
  129. }
  130. return null;
  131. }
  132. }
  133. function parseReader (command, function_obj) {
  134. var temp = CodeParser.expressionParserCodeVisual(command.variable, function_obj);
  135. if (Array.isArray(temp) && temp[0])
  136. temp = temp[0];
  137. return new Models.Reader(
  138. temp
  139. );
  140. }
  141. function parseReturn (command, function_obj) {
  142. var temp = CodeParser.expressionParserCodeVisual(command.expression, function_obj);
  143. return new Models.Return(
  144. temp
  145. );
  146. }
  147. function parseWriter (command, function_obj) {
  148. var temp = CodeParser.expressionParserCodeVisual(command.content[0], function_obj);
  149. return new Models.Writer(
  150. temp,
  151. command.newLine
  152. );
  153. }
  154. function parseBreak (command, function_obj) {
  155. return new Models.Break();
  156. }
  157. function parseAttribution (command, function_obj) {
  158. var variable = CodeParser.expressionParserCodeVisual(command.variable, function_obj);
  159. var expression = CodeParser.expressionParserCodeVisual(command.expression, function_obj);
  160. if (Array.isArray(variable))
  161. variable = variable[0];
  162. return new Models.Attribution(
  163. variable,
  164. expression
  165. );
  166. }
  167. function parseComment(command, function_obj) {
  168. // TODO
  169. return new Models.Comment(
  170. null
  171. );
  172. }
  173. function parseFunctionCall (command, function_obj) {
  174. var parameters = [];
  175. if (command.parameters_list) {
  176. command.parameters_list.forEach(function(el) {
  177. var temp = CodeParser.expressionParserCodeVisual(el, function_obj);
  178. if (temp.content === 0) temp.content = "0";
  179. parameters.push(temp[0]);
  180. });
  181. }
  182. var function_called = CodeParser.searchFunction(command.name);
  183. var temp = new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.only_function, null, null, null, false);
  184. temp.function_called = function_called;
  185. temp.parameters_list = parameters;
  186. return new Models.FunctionCall(
  187. temp,
  188. null
  189. );
  190. }
  191. function parseIfTrue(command, function_obj) {
  192. var expression = CodeParser.expressionParserCodeVisual(command.expression, function_obj);
  193. var ifTrueBlock = parseCommands(command.ifTrue, function_obj);
  194. var ifFalseBlock = parseCommands(command.ifFalse, function_obj);
  195. return new Models.IfTrue(expression, ifTrueBlock, ifFalseBlock);
  196. }
  197. function parseRepeatNTimes(command, function_obj) {
  198. var var_attribution = CodeParser.expressionParserCodeVisual(command.var_attribution, function_obj);
  199. var_attribution = var_attribution[0];
  200. var expression1 = CodeParser.expressionParserCodeVisual(command.var_initial, function_obj);
  201. expression1 = expression1[0];
  202. var expression2 = CodeParser.expressionParserCodeVisual(command.condition, function_obj);
  203. expression2 = expression2[0];
  204. var expression3 = CodeParser.expressionParserCodeVisual(command.step_expression, function_obj);
  205. console.log('expression3', expression3);
  206. var commands_block = parseCommands(command.commands, function_obj);
  207. // var_attribution,
  208. // var_incrementation,
  209. // expression1,
  210. // expression2,
  211. // expression3,
  212. // commands_block
  213. return new Models.RepeatNTimes(
  214. var_attribution,
  215. new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.only_variable, null, null, null, false),
  216. expression1,
  217. expression2,
  218. null,
  219. commands_block);
  220. }