codeParser.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. // iVProg - www.usp.br/line/ivprog
  2. // LInE - Free Education, Private Data
  3. import * as Models from '../visualUI/ivprog_elements';
  4. import { LocalizedStrings } from "./../services/localizedStringsService";
  5. import * as VariableValueMenuManagement from '../visualUI/commands/variable_value_menu';
  6. import * as CodeParser from '../visualUI/commands/generic_expression';
  7. function parseBoolean (var_value, dimensions) {
  8. if (dimensions == 0)
  9. return var_value == LocalizedStrings.getUI("logic_value_true");
  10. if (dimensions == 1) {
  11. var final_array = [];
  12. var_value.forEach(function(el){
  13. final_array.push(el == LocalizedStrings.getUI("logic_value_true"));
  14. });
  15. return final_array;
  16. }
  17. if (dimensions == 2) {
  18. var final_array = [];
  19. var_value.forEach(function(row){
  20. var temp = [];
  21. row.forEach(function(el){
  22. temp.push(el == LocalizedStrings.getUI("logic_value_true"));
  23. });
  24. final_array.push(temp);
  25. });
  26. return final_array;
  27. }
  28. }
  29. function parseGlobal (global_obj) {
  30. var new_global = new Models.Variable(global_obj.type, global_obj.name, global_obj.value);
  31. new_global.is_constant = global_obj.is_const;
  32. new_global.columns = global_obj.columns;
  33. new_global.dimensions = global_obj.dimension;
  34. new_global.rows = global_obj.rows;
  35. if (global_obj.type == "boolean")
  36. new_global.value = parseBoolean(global_obj.value, global_obj.dimension);
  37. window.program_obj.addGlobal(new_global);
  38. }
  39. function parseParameter (parameter_obj) {
  40. const new_parameter = new Models.Variable(
  41. parameter_obj.type,
  42. parameter_obj.name,
  43. null,
  44. parameter_obj.dimension,
  45. parameter_obj.is_const,
  46. parameter_obj.rows,
  47. parameter_obj.columns,
  48. parameter_obj.reference
  49. );
  50. new_parameter.value = parameter_obj.value;
  51. if (parameter_obj.type == "boolean" && parameter_obj.value)
  52. new_parameter.value = parseBoolean(parameter_obj.value, parameter_obj.dimension);
  53. return new_parameter;
  54. }
  55. function parseFunction (function_obj) {
  56. const new_function = new Models.Function(function_obj.name, function_obj.return_type, function_obj.return_dimensions, [], false, false, []);
  57. if (!new_function.name) {
  58. new_function.name = LocalizedStrings.getUI("start");
  59. new_function.is_main = true;
  60. }
  61. if (function_obj.parameters_list) {
  62. function_obj.parameters_list.forEach(function(el){
  63. new_function.parameters_list.push(parseParameter(el));
  64. });
  65. }
  66. if (function_obj.variables_list) {
  67. function_obj.variables_list.forEach(function(el){
  68. new_function.variables_list.push(parseParameter(el));
  69. });
  70. }
  71. window.program_obj.addFunction(new_function);
  72. }
  73. export function parserCodeVisual (code_obj = null) {
  74. console.log(".ivprog/js/util/codeParser.js: parserCodeVisual(.)");
  75. window.conteudo = code_obj
  76. // Globals:
  77. window.program_obj.globals = [];
  78. code_obj.globals.forEach(parseGlobal);
  79. // Functions:
  80. window.program_obj.functions = [];
  81. code_obj.functions.forEach(parseFunction);
  82. // Commands:
  83. window.program_obj.functions.forEach(function(preparedFunction) {
  84. code_obj.functions.forEach(function(rawFunction) {
  85. if ((preparedFunction.name == rawFunction.name) || (!rawFunction.name && preparedFunction.name == LocalizedStrings.getUI("start"))) {
  86. preparedFunction.commands = parseCommands(rawFunction.commands, preparedFunction);
  87. }
  88. })
  89. });
  90. }
  91. function parseCommands (commands_block, function_obj) {
  92. console.log(".ivprog/js/util/codeParser.js: parseCommands(.): commands_block=" + JSON.stringify(commands_block));
  93. if (Array.isArray(commands_block)) {
  94. var temp = [];
  95. commands_block.forEach(function(command) {
  96. temp.push(parseCommands(command, function_obj));
  97. });
  98. return temp;
  99. }
  100. else {
  101. switch (commands_block.type) {
  102. case Models.COMMAND_TYPES.reader:
  103. return parseReader(commands_block, function_obj);
  104. case Models.COMMAND_TYPES.return:
  105. return parseReturn(commands_block, function_obj);
  106. case Models.COMMAND_TYPES.writer:
  107. return parseWriter(commands_block, function_obj);
  108. case Models.COMMAND_TYPES.break:
  109. return parseBreak(commands_block, function_obj);
  110. case Models.COMMAND_TYPES.comment:
  111. return parseComment(commands_block, function_obj);
  112. case Models.COMMAND_TYPES.attribution:
  113. return parseAttribution(commands_block, function_obj);
  114. case Models.COMMAND_TYPES.functioncall:
  115. return parseFunctionCall(commands_block, function_obj);
  116. case Models.COMMAND_TYPES.iftrue:
  117. return parseIfTrue(commands_block, function_obj);
  118. case Models.COMMAND_TYPES.repeatNtimes:
  119. return parseRepeatNTimes(commands_block, function_obj);
  120. case Models.COMMAND_TYPES.whiletrue:
  121. return parseWhileTrue(commands_block, function_obj);
  122. case Models.COMMAND_TYPES.dowhiletrue:
  123. return parseDoWhileTrue(commands_block, function_obj);
  124. case Models.COMMAND_TYPES.switch:
  125. return parseSwitch(commands_block, function_obj);
  126. }
  127. return null;
  128. }
  129. }
  130. function parseReader (command, function_obj) {
  131. var temp = CodeParser.expressionParserCodeVisual(command.variable, function_obj);
  132. if (Array.isArray(temp) && temp[0])
  133. temp = temp[0];
  134. return new Models.Reader(temp);
  135. }
  136. function parseReturn (command, function_obj) {
  137. var temp = CodeParser.expressionParserCodeVisual(command.expression, function_obj);
  138. return new Models.Return(temp);
  139. }
  140. // Generate command "write(...)"
  141. // @calls js/visualUI/commands/attribution.js: renderExpression
  142. function parseWriter (command, function_obj) {
  143. //D var len_cmd = command.content.length; //leo
  144. //D var str = ""; // "#command.content=" + len_cmd + "\n"; //leo
  145. //D for (var i=0; i<len_cmd; i++) str += JSON.stringify(command.content[i]) + "; "; // "; " + command.content[i];
  146. //D console.log("./ivprog/js/util/codeParser.js: parseWriter(.): command=" + JSON.stringify(command)) // + "\nfunction_obj=||" + JSON.stringify(function_obj) + "||\n" + str); //leo
  147. console.log("./ivprog/js/util/codeParser.js: parseWriter(.): command=" + JSON.stringify(command));
  148. //#command=" + (command ? command.length : "<>") ); //leo + ";
  149. //leo
  150. //TODO It is missing to implement the visualUI construtor to "write_separator" in 'getVariable(.)'
  151. //TODO Incomplete solution: I change "Models.EXPRESSION_TYPES.write_sep" to "Models.ARITHMETIC_TYPES.plus" in ./ivprog/js/visualUI/commands/generic_expression.js
  152. //TODO See ./ivprog/js/visualUI/commands/generic_expression.js : expressionParserCodeVisual(.) : missing treat to 'write_sep' in 'getVariable(.)' final command
  153. // ATTENTION must pass all the vector to work with several variables in "write(.)"
  154. var temp = CodeParser.expressionParserCodeVisual(command.content, function_obj); // provide HTML elements
  155. //0 temp = CodeParser.expressionParserCodeVisual(command.content[0], function_obj);
  156. //D console.log("./ivprog/js/util/codeParser.js: parseWriter(.): #temp=" + temp.length + "; type=" + temp.type + ": temp=" + JSON.stringify(temp));
  157. return new Models.Writer(temp,command.newLine);
  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(variable, expression);
  168. }
  169. //TODO
  170. function parseComment (command, function_obj) {
  171. return new Models.Comment(null);
  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(temp, null);
  187. }
  188. function parseIfTrue(command, function_obj) {
  189. var expression = CodeParser.expressionParserCodeVisual(command.expression, function_obj);
  190. var ifTrueBlock = parseCommands(command.ifTrue, function_obj);
  191. var ifFalseBlock = parseCommands(command.ifFalse, function_obj);
  192. return new Models.IfTrue(expression, ifTrueBlock, ifFalseBlock);
  193. }
  194. function parseRepeatNTimes (command, function_obj) {
  195. console.log(".ivprog/js/util/codeParser.js: parseRepeatNTimes(.): command=" + JSON.stringify(command));
  196. var var_attribution = CodeParser.expressionParserCodeVisual(command.var_attribution, function_obj);
  197. var_attribution = var_attribution[0];
  198. var expression1 = CodeParser.expressionParserCodeVisual(command.var_initial, function_obj);
  199. expression1 = expression1[0];
  200. var expression2 = CodeParser.expressionParserCodeVisual(command.condition, function_obj);
  201. if (expression2[0].item == 'minus') {
  202. expression2 = expression2[1];
  203. expression2.content *= -1;
  204. } else
  205. expression2 = expression2[0];
  206. var var_step = CodeParser.expressionParserCodeVisual(command.step_expression, function_obj);
  207. var commands_block = parseCommands(command.commands, function_obj);
  208. var operator = command.step_expression[0].value == '+' ? Models.ARITHMETIC_TYPES.plus : Models.ARITHMETIC_TYPES.minus;
  209. var expression3 = new Models.ExpressionElement(Models.EXPRESSION_ELEMENTS.exp_op_exp, [ null, operator, var_step[1] ]);
  210. return new Models.RepeatNTimes(
  211. var_attribution,
  212. new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.only_variable, null, null, null, false),
  213. expression1,
  214. expression2,
  215. expression3,
  216. commands_block);
  217. } // function parseRepeatNTimes(command, function_obj)
  218. function parseWhileTrue (command, function_obj) {
  219. var expression = CodeParser.expressionParserCodeVisual(command.expression, function_obj);
  220. var commands = parseCommands(command.commands, function_obj);
  221. return new Models.WhileTrue(expression, commands);
  222. }
  223. function parseDoWhileTrue (command, function_obj) {
  224. var expression = CodeParser.expressionParserCodeVisual(command.expression, function_obj);
  225. var commands = parseCommands(command.commands, function_obj);
  226. return new Models.DoWhileTrue(expression, commands);
  227. }
  228. function parseSwitch (command, function_obj) {
  229. var expression = CodeParser.expressionParserCodeVisual(command.expression, function_obj);
  230. var sc = [];
  231. if (command.cases) {
  232. command.cases.forEach(function(case_el) {
  233. var temp_exp = CodeParser.expressionParserCodeVisual(case_el.expression, function_obj);
  234. var temp_commands = parseCommands(case_el.commands, function_obj);
  235. var temp_case = new Models.SwitchCase(temp_exp[0], temp_commands);
  236. sc.push(temp_case);
  237. })
  238. }
  239. return new Models.Switch(expression[0], sc);
  240. }