|
@@ -4,6 +4,7 @@ import { LocalizedStrings } from "../../services/localizedStringsService";
|
|
|
import * as VariableValueMenuManagement from "./variable_value_menu";
|
|
|
import { registerUserEvent, ActionTypes } from "../../services/userLog";
|
|
|
import WatchJS from "melanke-watchjs";
|
|
|
+import * as Utils from '../utils';
|
|
|
|
|
|
window.timer = false;
|
|
|
|
|
@@ -1302,3 +1303,332 @@ function getSeparator () {
|
|
|
"write_seprator_menu_text"
|
|
|
)}</div>`;
|
|
|
}
|
|
|
+
|
|
|
+function getVariable (function_obj, search) {
|
|
|
+ if (search == ")" || search == "(") {
|
|
|
+ return search;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (search.instance == "operator") {
|
|
|
+ var obj;
|
|
|
+ switch (search.value) {
|
|
|
+ case '+':
|
|
|
+ obj = new Models.ExpressionOperator(
|
|
|
+ Models.EXPRESSION_TYPES.exp_arithmetic,
|
|
|
+ Models.ARITHMETIC_TYPES.plus);
|
|
|
+ break;
|
|
|
+ case '-':
|
|
|
+ obj = new Models.ExpressionOperator(
|
|
|
+ Models.EXPRESSION_TYPES.exp_arithmetic,
|
|
|
+ Models.ARITHMETIC_TYPES.minus);
|
|
|
+ break;
|
|
|
+ case '*':
|
|
|
+ obj = new Models.ExpressionOperator(
|
|
|
+ Models.EXPRESSION_TYPES.exp_arithmetic,
|
|
|
+ Models.ARITHMETIC_TYPES.multiplication);
|
|
|
+ break;
|
|
|
+ case '/':
|
|
|
+ obj = new Models.ExpressionOperator(
|
|
|
+ Models.EXPRESSION_TYPES.exp_arithmetic,
|
|
|
+ Models.ARITHMETIC_TYPES.division);
|
|
|
+ break;
|
|
|
+ case '%':
|
|
|
+ obj = new Models.ExpressionOperator(
|
|
|
+ Models.EXPRESSION_TYPES.exp_arithmetic,
|
|
|
+ Models.ARITHMETIC_TYPES.module);
|
|
|
+ break;
|
|
|
+ case '>':
|
|
|
+ obj = new Models.ExpressionOperator(
|
|
|
+ Models.EXPRESSION_TYPES.exp_conditional,
|
|
|
+ Models.ARITHMETIC_COMPARISON.greater_than);
|
|
|
+ break;
|
|
|
+ case '<':
|
|
|
+ obj = new Models.ExpressionOperator(
|
|
|
+ Models.EXPRESSION_TYPES.exp_conditional,
|
|
|
+ Models.ARITHMETIC_COMPARISON.less_than);
|
|
|
+ break;
|
|
|
+ case '>=':
|
|
|
+ obj = new Models.ExpressionOperator(
|
|
|
+ Models.EXPRESSION_TYPES.exp_conditional,
|
|
|
+ Models.ARITHMETIC_COMPARISON.greater_than_or_equals_to);
|
|
|
+ break;
|
|
|
+ case '<=':
|
|
|
+ obj = new Models.ExpressionOperator(
|
|
|
+ Models.EXPRESSION_TYPES.exp_conditional,
|
|
|
+ Models.ARITHMETIC_COMPARISON.less_than_or_equals_to);
|
|
|
+ break;
|
|
|
+ case '==':
|
|
|
+ obj = new Models.ExpressionOperator(
|
|
|
+ Models.EXPRESSION_TYPES.exp_conditional,
|
|
|
+ Models.ARITHMETIC_COMPARISON.equals_to);
|
|
|
+ break;
|
|
|
+ case '!=':
|
|
|
+ obj = new Models.ExpressionOperator(
|
|
|
+ Models.EXPRESSION_TYPES.exp_conditional,
|
|
|
+ Models.ARITHMETIC_COMPARISON.not_equals_to);
|
|
|
+ break;
|
|
|
+ case 'and':
|
|
|
+ obj = new Models.ExpressionOperator(
|
|
|
+ Models.EXPRESSION_TYPES.exp_logic,
|
|
|
+ Models.LOGIC_COMPARISON.and);
|
|
|
+ break;
|
|
|
+ case 'or':
|
|
|
+ obj = new Models.ExpressionOperator(
|
|
|
+ Models.EXPRESSION_TYPES.exp_logic,
|
|
|
+ Models.LOGIC_COMPARISON.or);
|
|
|
+ break;
|
|
|
+ case 'not':
|
|
|
+ obj = new Models.ExpressionOperator(
|
|
|
+ Models.EXPRESSION_TYPES.exp_logic,
|
|
|
+ Models.LOGIC_COMPARISON.not);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ return obj;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (search.instance == "expression"
|
|
|
+ && search.type == "const") {
|
|
|
+ if (search.value === true) {
|
|
|
+ var obj = new Models.VariableValueMenu(
|
|
|
+ VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all,
|
|
|
+ LocalizedStrings.getUI('logic_value_true'),
|
|
|
+ null,
|
|
|
+ null,
|
|
|
+ true
|
|
|
+ );
|
|
|
+ return obj;
|
|
|
+ }
|
|
|
+ if (search.value === false) {
|
|
|
+ var obj = new Models.VariableValueMenu(
|
|
|
+ VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all,
|
|
|
+ LocalizedStrings.getUI('logic_value_false'),
|
|
|
+ null,
|
|
|
+ null,
|
|
|
+ true
|
|
|
+ );
|
|
|
+ return obj;
|
|
|
+ }
|
|
|
+ var obj = new Models.VariableValueMenu(
|
|
|
+ VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all,
|
|
|
+ search.value,
|
|
|
+ null,
|
|
|
+ null,
|
|
|
+ true
|
|
|
+ );
|
|
|
+ return obj;
|
|
|
+ }
|
|
|
+
|
|
|
+ var variavel = null;
|
|
|
+ if (search.instance == "expression"
|
|
|
+ && search.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 == search.value) {
|
|
|
+ variavel = function_obj.variables_list[j];
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. Procurar a variável nas gloais:
|
|
|
+ if (!variavel)
|
|
|
+ for (var j = 0; j < program_obj.globals.length; j++) {
|
|
|
+ if (program_obj.globals[j].name == search.value) {
|
|
|
+ variavel = program_obj.globals[j];
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. Procurar na lista de parâmetros:
|
|
|
+ if (!variavel)
|
|
|
+ for (var j = 0; j < function_obj.parameters_list.length; j++) {
|
|
|
+ if (function_obj.parameters_list[j].name == search.value) {
|
|
|
+ variavel = function_obj.parameters_list[j];
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (search.instance == "expression"
|
|
|
+ && search.type == "var"
|
|
|
+ && !search.line) {
|
|
|
+
|
|
|
+ var obj = new Models.VariableValueMenu(
|
|
|
+ VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all,
|
|
|
+ variavel,
|
|
|
+ null,
|
|
|
+ null,
|
|
|
+ true
|
|
|
+ );
|
|
|
+ return obj;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (search.instance == "expression"
|
|
|
+ && search.type == "var"
|
|
|
+ && search.class == "vector") {
|
|
|
+
|
|
|
+ var obj = new Models.VariableValueMenu(
|
|
|
+ VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all,
|
|
|
+ variavel,
|
|
|
+ null,
|
|
|
+ getVariable(function_obj, search.line[0]),
|
|
|
+ true
|
|
|
+ );
|
|
|
+ return obj;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (search.instance == "expression"
|
|
|
+ && search.type == "var"
|
|
|
+ && search.class == "matrix") {
|
|
|
+ var obj = new Models.VariableValueMenu(
|
|
|
+ VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all,
|
|
|
+ variavel,
|
|
|
+ getVariable(function_obj, search.line[0]),
|
|
|
+ getVariable(function_obj, search.column[0]),
|
|
|
+ true
|
|
|
+ );
|
|
|
+ return obj;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (search.instance == "expression"
|
|
|
+ && search.type == "function") {
|
|
|
+ var obj = new Models.VariableValueMenu(
|
|
|
+ VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all,
|
|
|
+ variavel,
|
|
|
+ null,
|
|
|
+ null,
|
|
|
+ true
|
|
|
+ );
|
|
|
+ // Procurar a função para referência:
|
|
|
+ for (var i = 0; i < program_obj.functions.length; i++) {
|
|
|
+ if (program_obj.functions[i].name == search.value) {
|
|
|
+ obj.function_called = program_obj.functions[i];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Se ainda não foi encontrada, procurar na biblioteca do iVProg:
|
|
|
+ if (!obj.function_called) {
|
|
|
+ for (var i = 0; i < window.system_functions.length; i++) {
|
|
|
+ if (search.value.split(".")[1] && search.value.split(".")[1]
|
|
|
+ == window.system_functions[i].identifier) {
|
|
|
+ obj.function_called = window.system_functions[i];
|
|
|
+ } else if (search.value == window.system_functions[i].identifier) {
|
|
|
+ obj.function_called = window.system_functions[i];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ obj.parameters_list = [];
|
|
|
+
|
|
|
+ for (var i = 0; i < search.params.length; i++) {
|
|
|
+ obj.parameters_list.push(getVariable(function_obj,search.params[i][0]));
|
|
|
+ }
|
|
|
+ return obj;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+export function expressionParserToVisual (text, function_obj, input_field) {
|
|
|
+
|
|
|
+ if (text.trim().length == 0) {
|
|
|
+ return [new Models.VariableValueMenu(
|
|
|
+ VariableValueMenuManagement.VAR_OR_VALUE_TYPES.all,
|
|
|
+ null,
|
|
|
+ null,
|
|
|
+ null,
|
|
|
+ true
|
|
|
+ )];
|
|
|
+ }
|
|
|
+
|
|
|
+ var var_not_found = [];
|
|
|
+ var fun_not_found = [];
|
|
|
+ var parsed;
|
|
|
+ try {
|
|
|
+ parsed = ivprogCore.parseExpression(text);
|
|
|
+ } catch (ex) {
|
|
|
+ Utils.renderErrorMessage(input_field, LocalizedStrings.getUI('expression_invalid'));
|
|
|
+ }
|
|
|
+ if (!parsed) return null;
|
|
|
+
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+ if (fun_not_found.length > 0) {
|
|
|
+ let uniqueWords = [...new Set(fun_not_found)];
|
|
|
+ Utils.renderErrorMessage(input_field, LocalizedStrings.getUI('expression_undeclared_function') + " " + uniqueWords.join(", "));
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (var_not_found.length > 0) {
|
|
|
+ let uniqueWords = [...new Set(var_not_found)];
|
|
|
+ Utils.renderErrorMessage(input_field, LocalizedStrings.getUI('expression_undelcared_variable') + " " + uniqueWords.join(", "));
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ var exp_obj = [];
|
|
|
+ for (var i = 0; i < parsed.length; i++) {
|
|
|
+ exp_obj.push(getVariable(function_obj, parsed[i]));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ return exp_obj;
|
|
|
+}
|