codeParser.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import * as Models from '../visualUI/ivprog_elements';
  2. import { LocalizedStrings } from "./../services/localizedStringsService";
  3. function parseBoolean (var_value, dimensions) {
  4. if (dimensions == 0)
  5. return var_value == LocalizedStrings.getUI("logic_value_true");
  6. if (dimensions == 1) {
  7. var final_array = [];
  8. var_value.forEach(function(el){
  9. final_array.push(el == LocalizedStrings.getUI("logic_value_true"));
  10. });
  11. return final_array;
  12. }
  13. if (dimensions == 2) {
  14. var final_array = [];
  15. var_value.forEach(function(row){
  16. var temp = [];
  17. row.forEach(function(el){
  18. temp.push(el == LocalizedStrings.getUI("logic_value_true"));
  19. });
  20. final_array.push(temp);
  21. });
  22. return final_array;
  23. }
  24. }
  25. function parseGlobal (global_obj) {
  26. var new_global = new Models.Variable(
  27. global_obj.type,
  28. global_obj.name,
  29. global_obj.value);
  30. new_global.is_constant = global_obj.is_const;
  31. new_global.columns = global_obj.columns;
  32. new_global.dimensions = global_obj.dimension;
  33. new_global.rows = global_obj.rows;
  34. if (global_obj.type == "boolean")
  35. new_global.value = parseBoolean(global_obj.value, global_obj.dimension);
  36. window.program_obj.addGlobal(new_global);
  37. }
  38. function parseParameter (parameter_obj) {
  39. const new_parameter = new Models.Variable(
  40. parameter_obj.type,
  41. parameter_obj.name,
  42. null,
  43. parameter_obj.dimension,
  44. parameter_obj.is_const,
  45. parameter_obj.rows,
  46. parameter_obj.columns,
  47. parameter_obj.reference
  48. );
  49. new_parameter.value = parameter_obj.value;
  50. if (parameter_obj.type == "boolean" && parameter_obj.value)
  51. new_parameter.value = parseBoolean(parameter_obj.value, parameter_obj.dimension);
  52. return new_parameter;
  53. }
  54. function parseFunction (function_obj) {
  55. const new_function = new Models.Function(
  56. function_obj.name,
  57. function_obj.return_type,
  58. function_obj.return_dimensions,
  59. [],
  60. false,
  61. false,
  62. []);
  63. if (!new_function.name) {
  64. new_function.name = LocalizedStrings.getUI("start");
  65. new_function.is_main = true;
  66. }
  67. if (function_obj.parameters_list) {
  68. function_obj.parameters_list.forEach(function(el){
  69. new_function.parameters_list.push(parseParameter(el));
  70. });
  71. }
  72. if (function_obj.variables_list) {
  73. function_obj.variables_list.forEach(function(el){
  74. new_function.variables_list.push(parseParameter(el));
  75. });
  76. }
  77. new_function.commands = parseCommands(function_obj.commands)
  78. window.program_obj.addFunction(new_function);
  79. }
  80. export function parserCodeVisual (code_obj = null) {
  81. console.log('chegou!', code_obj)
  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. }
  90. function parseCommands (commands_block) {
  91. console.log('bloco', commands_block)
  92. if (Array.isArray(commands_block)) {
  93. var temp = [];
  94. commands_block.forEach(function(command) {
  95. temp.push(parseCommands(command));
  96. });
  97. return temp;
  98. }
  99. else {
  100. return null;
  101. }
  102. }