Browse Source

adding parsefunctioncall

Igor 2 years ago
parent
commit
504c384e37
2 changed files with 215 additions and 8 deletions
  1. 114 7
      js/util/codeParser.js
  2. 101 1
      js/visualUI/commands/generic_expression.js

+ 114 - 7
js/util/codeParser.js

@@ -1,6 +1,8 @@
 
 import * as Models from '../visualUI/ivprog_elements';
 import { LocalizedStrings } from "./../services/localizedStringsService";
+import * as VariableValueMenuManagement from '../visualUI/commands/variable_value_menu';
+import * as CodeParser from '../visualUI/commands/generic_expression';
 
 function parseBoolean (var_value, dimensions) {
   if (dimensions == 0)
@@ -92,15 +94,12 @@ function parseFunction (function_obj) {
     });
   }
 
-  new_function.commands = parseCommands(function_obj.commands)
-
   window.program_obj.addFunction(new_function);
   
 }
 
 export function parserCodeVisual (code_obj = null) {
 
-  console.log('chegou!', code_obj)
   window.conteudo = code_obj
 
   // Globals:
@@ -111,21 +110,129 @@ export function parserCodeVisual (code_obj = null) {
   window.program_obj.functions = [];
   code_obj.functions.forEach(parseFunction);
 
-}
+  // Commands: 
+  window.program_obj.functions.forEach(function(preparedFunction) {
+    code_obj.functions.forEach(function(rawFunction) {
+      if ((preparedFunction.name == rawFunction.name) 
+          || 
+          (!rawFunction.name && preparedFunction.name == LocalizedStrings.getUI("start"))) {
+            preparedFunction.commands = parseCommands(rawFunction.commands, preparedFunction);
+          }
+    })
+  });
 
-function parseCommands (commands_block) {
+}
 
-  console.log('bloco', commands_block)
+function parseCommands (commands_block, function_obj) {
 
   if (Array.isArray(commands_block)) {
     var temp = [];
     commands_block.forEach(function(command) {
-      temp.push(parseCommands(command));
+      temp.push(parseCommands(command, function_obj));
     });
     return temp;
   }
   else {
+    switch (commands_block.type) {
+      case Models.COMMAND_TYPES.reader:
+        return parseReader(commands_block, function_obj);
+
+      case Models.COMMAND_TYPES.return:
+        return parseReturn(commands_block, function_obj);
+
+      case Models.COMMAND_TYPES.writer:
+        return parseWriter(commands_block, function_obj);
+
+      case Models.COMMAND_TYPES.break:
+        return parseBreak(commands_block, function_obj);
+
+      case Models.COMMAND_TYPES.comment:
+        return parseComment(commands_block, function_obj);
+
+      case Models.COMMAND_TYPES.attribution:
+        return parseAttribution(commands_block, function_obj);
+
+      case Models.COMMAND_TYPES.functioncall:
+        return parseFunctionCall(commands_block, function_obj);
+    }
     return null;
   }
   
+}
+
+function parseReader (command, function_obj) {
+  var temp = CodeParser.expressionParserCodeVisual(command.variable, function_obj);
+
+  if (Array.isArray(temp) && temp[0])
+    temp = temp[0];
+
+  return new Models.Reader(
+    temp
+  );
+}
+
+function parseReturn (command, function_obj) {
+
+  var temp = CodeParser.expressionParserCodeVisual(command.expression, function_obj);
+
+  return new Models.Return(
+    temp
+  );
+
+}
+
+function parseWriter (command, function_obj) {
+
+  var temp = CodeParser.expressionParserCodeVisual(command.content[0], function_obj);
+
+  return new Models.Writer(
+    temp,
+    command.newLine
+  );
+}
+
+function parseBreak (command, function_obj) {
+    return new Models.Break();
+}
+
+function parseAttribution (command, function_obj) {
+
+  var variable = CodeParser.expressionParserCodeVisual(command.variable, function_obj);
+  var expression = CodeParser.expressionParserCodeVisual(command.expression, function_obj);
+
+  if (Array.isArray(variable)) 
+    variable = variable[0];
+
+  return new Models.Attribution(
+    variable,
+    expression
+  );
+}
+
+function parseComment(command, function_obj) {
+  // TODO
+  return new Models.Comment(
+    null
+  );
+}
+
+function parseFunctionCall (command, function_obj) {
+
+  var parameters = CodeParser.expressionParserCodeVisual(command.parameters_list[0], function_obj);
+  var function_called = CodeParser.searchFunction(command.name);
+
+  //function_called.parameters_list = parameters;
+
+  console.log('atenção: ', parameters, command.parameters_list);
+  
+  var temp = new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.only_function, null, null, null, false);
+  temp.function_called = function_called;
+  temp.parameters_list = parameters;
+  
+  return new Models.FunctionCall(
+    temp, 
+    parameters
+  );
+
+  //new Models.FunctionCall(new Models.VariableValueMenu(VariableValueMenuManagement.VAR_OR_VALUE_TYPES.only_function, null, null, null, false), null
 }

+ 101 - 1
js/visualUI/commands/generic_expression.js

@@ -1628,7 +1628,107 @@ export function expressionParserToVisual (text, function_obj, input_field) {
     exp_obj.push(getVariable(function_obj, parsed[i]));
   }
 
+  return exp_obj;
+}
+
+export function searchFunction (function_name) {
+
+  for (var j = 0; j < program_obj.functions.length; j++) {
+    if (program_obj.functions[j].name == function_name) {
+      return program_obj.functions[j];
+    }
+  }
+
+  // Se ainda não encontrou, procurar na biblioteca do iVProg:
+  for (var j = 0; j < window.system_functions.length; j++) {
+    if (function_name.split(".")[1] && function_name.split(".")[1] 
+        == window.system_functions[j].identifier) {
+      return window.system_functions[j];
+    } else if (function_name == window.system_functions[j].identifier) {
+      return window.system_functions[j];
+    }
+  }
+
+}
+
+
+export function expressionParserCodeVisual (parsed, function_obj) {
+
+  if (!parsed) return null;
+
+  var var_not_found = [];
+  var fun_not_found = [];
+
+  for (var i = 0; i < parsed.length; i++) {
+    var variavel = null;
+    if (parsed[i].instance == "expression"
+          && parsed[i].type == "var") {
+      // 1. Procurar a variável na função:
+      for (var j = 0; j < function_obj.variables_list.length; j++) {
+        if (function_obj.variables_list[j].name == parsed[i].value) {
+          variavel = function_obj.variables_list[j];
+          break;
+        }
+      }
+
+      // 2. Procurar a variável nas globais: 
+      if (!variavel)
+        for (var j = 0; j < program_obj.globals.length; j++) {
+          if (program_obj.globals[j].name == parsed[i].value) {
+            variavel = program_obj.globals[j];
+            break;
+          }
+        }
+
+      // 3. Procurar a variável nos parâmetros:
+      if (!variavel)
+        for (var j = 0; j < function_obj.parameters_list.length; j++) {
+          if (function_obj.parameters_list[j].name == parsed[i].value) {
+            variavel = function_obj.parameters_list[j];
+            break;
+          }
+        }
+      
+      if (!variavel)
+        var_not_found.push(parsed[i].value);
+    }
+
+    var funcao;
+    if (parsed[i].instance == "expression"
+      && parsed[i].type == "function") {
+    
+      // Procurar a função para referência:
+      for (var j = 0; j < program_obj.functions.length; j++) {
+        if (program_obj.functions[j].name == parsed[i].value) {
+          funcao = program_obj.functions[j];
+        }
+      }
+
+      // Se ainda não encontrou, procurar na biblioteca do iVProg:
+      if (!funcao) {
+        for (var j = 0; j < window.system_functions.length; j++) {
+          if (parsed[i].value.split(".")[1] && parsed[i].value.split(".")[1] 
+              == window.system_functions[j].identifier) {
+            funcao = window.system_functions[j];
+          } else if (parsed[i].value == window.system_functions[j].identifier) {
+            funcao = window.system_functions[j];
+          }
+        }
+      }
+
+    if (!funcao) {
+      fun_not_found.push(parsed[i].value);
+    }
+  }
+}
+
+  var exp_obj = [];
+  for (var i = 0; i < parsed.length; i++) {
+    exp_obj.push(getVariable(function_obj, parsed[i]));
+  }
+
   
 
   return exp_obj;
-}
+}
+