codeParser.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. return parseRepeatNTimes(commands_block, function_obj);
  128. case Models.COMMAND_TYPES.whiletrue:
  129. return parseWhileTrue(commands_block, function_obj);
  130. case Models.COMMAND_TYPES.dowhiletrue:
  131. return parseDoWhileTrue(commands_block, function_obj);
  132. case Models.COMMAND_TYPES.switch:
  133. return parseSwitch(commands_block, function_obj);
  134. }
  135. return null;
  136. }
  137. }
  138. function parseReader (command, function_obj) {
  139. var temp = CodeParser.expressionParserCodeVisual(command.variable, function_obj);
  140. if (Array.isArray(temp) && temp[0])
  141. temp = temp[0];
  142. return new Models.Reader(
  143. temp
  144. );
  145. }
  146. function parseReturn (command, function_obj) {
  147. var temp = CodeParser.expressionParserCodeVisual(command.expression, function_obj);
  148. return new Models.Return(
  149. temp
  150. );
  151. }
  152. function parseWriter (command, function_obj) {
  153. var temp = CodeParser.expressionParserCodeVisual(command.content, function_obj);
  154. return new Models.Writer(
  155. temp,
  156. command.newLine
  157. );
  158. }
  159. function parseBreak (command, function_obj) {
  160. return new Models.Break();
  161. }
  162. function parseAttribution (command, function_obj) {
  163. var variable = CodeParser.expressionParserCodeVisual(command.variable, function_obj);
  164. var expression = CodeParser.expressionParserCodeVisual(command.expression, function_obj);
  165. if (Array.isArray(variable))
  166. variable = variable[0];
  167. return new Models.Attribution(
  168. variable,
  169. expression
  170. );
  171. }
  172. function parseComment (command, function_obj) {
  173. // TODO
  174. return new Models.Comment(
  175. null
  176. );
  177. }
  178. function parseFunctionCall (command, function_obj) {
  179. var parameters = [];
  180. if (command.parameters_list) {
  181. command.parameters_list.forEach(function(el) {
  182. var temp = CodeParser.expressionParserCodeVisual(el, function_obj);
  183. if (temp.content === 0) temp.content = "0";
  184. parameters.push(temp[0]);
  185. });
  186. }
  187. var function_called = CodeParser.searchFunction(command.name);
  188. var temp = new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.only_function, null, null, null, false);
  189. temp.function_called = function_called;
  190. temp.parameters_list = parameters;
  191. return new Models.FunctionCall(
  192. temp,
  193. null
  194. );
  195. }
  196. function parseIfTrue(command, function_obj) {
  197. var expression = CodeParser.expressionParserCodeVisual(command.expression, function_obj);
  198. var ifTrueBlock = parseCommands(command.ifTrue, function_obj);
  199. var ifFalseBlock = parseCommands(command.ifFalse, function_obj);
  200. return new Models.IfTrue(expression, ifTrueBlock, ifFalseBlock);
  201. }
  202. function parseRepeatNTimes(command, function_obj) {
  203. var var_attribution = CodeParser.expressionParserCodeVisual(command.var_attribution, function_obj);
  204. var_attribution = var_attribution[0];
  205. var expression1 = CodeParser.expressionParserCodeVisual(command.var_initial, function_obj);
  206. expression1 = expression1[0];
  207. expression1.content = "" + expression1.content;
  208. var expression2 = CodeParser.expressionParserCodeVisual(command.condition, function_obj);
  209. if (expression2[0].item == 'minus') {
  210. expression2 = expression2[1];
  211. expression2.content *= -1;
  212. } else
  213. expression2 = expression2[0];
  214. var var_step = CodeParser.expressionParserCodeVisual(command.step_expression, function_obj);
  215. var commands_block = parseCommands(command.commands, function_obj);
  216. var operator = command.step_expression[0].value == '+'
  217. ? Models.ARITHMETIC_TYPES.plus
  218. : Models.ARITHMETIC_TYPES.minus;
  219. var expression3 = new Models.ExpressionElement(
  220. Models.EXPRESSION_ELEMENTS.exp_op_exp,
  221. [
  222. null,
  223. operator,
  224. var_step[1]
  225. ]);
  226. return new Models.RepeatNTimes(
  227. var_attribution,
  228. new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.only_variable, null, null, null, false),
  229. expression1,
  230. expression2,
  231. expression3,
  232. commands_block);
  233. }
  234. function parseWhileTrue (command, function_obj) {
  235. var expression = CodeParser.expressionParserCodeVisual(command.expression, function_obj);
  236. var commands = parseCommands(command.commands, function_obj);
  237. return new Models.WhileTrue(
  238. expression,
  239. commands
  240. );
  241. }
  242. function parseDoWhileTrue (command, function_obj) {
  243. var expression = CodeParser.expressionParserCodeVisual(command.expression, function_obj);
  244. var commands = parseCommands(command.commands, function_obj);
  245. return new Models.DoWhileTrue(
  246. expression,
  247. commands
  248. );
  249. }
  250. function parseSwitch (command, function_obj) {
  251. var expression = CodeParser.expressionParserCodeVisual(command.expression, function_obj);
  252. var sc = [];
  253. if (command.cases) {
  254. command.cases.forEach(function(case_el) {
  255. var temp_exp = CodeParser.expressionParserCodeVisual(case_el.expression, function_obj);
  256. var temp_commands = parseCommands(case_el.commands, function_obj);
  257. var temp_case = new Models.SwitchCase(temp_exp[0], temp_commands);
  258. sc.push(temp_case);
  259. })
  260. }
  261. return new Models.Switch(
  262. expression[0],
  263. sc
  264. );
  265. }