/// <summary> /// Busca recursivamente el valor de una variable /// </summary> private VariableModel Search(ContextModel context, ExpressionVariableIdentifier expressionVariable, out string error) { VariableModel variable = null; int index = 0; // Inicializa las variables de salida error = string.Empty; // Obtiene el índice asociado a la variable if (expressionVariable.IndexExpressionsRPN?.Count > 0) { VariableModel indexVariable = Compute(context, expressionVariable.IndexExpressionsRPN, out error); if (indexVariable.Type != VariableModel.VariableType.Numeric) { error = "Index expression is not a numeric value"; } else { index = (int)indexVariable.Value; } } // Si no hay ningún error, obtiene la variable if (string.IsNullOrWhiteSpace(error)) { variable = context.VariablesTable.Get(expressionVariable.Name, index); } // Devuelve la variable return(variable); }
public override void VisitExpressionVariableIdentifier(ExpressionVariableIdentifier node, CloningAstVisitorContext context) { context.Result = new ExpressionVariableIdentifier() { Identifier = this.CloneNode(node.Identifier, context) }; }
/// <summary> /// Obtiene el valor contenido en una variable /// </summary> private ValueBase GetValueVariable(ExpressionVariableIdentifier variableIdentifier) { Variable variable = Search(variableIdentifier, out string error); if (!string.IsNullOrWhiteSpace(error)) { return(ValueBase.GetError(error)); } else { return(variable.Value); } }
/// <summary> /// Busca recursivamente una variable y su miembro /// </summary> private Variable Search(ExpressionVariableIdentifier expressionVariable, VariablesCollection variables, out string error) { Variable variable = null; int index = 0; // Inicializa las variables de salida error = null; // Obtiene el índice asociado a la variable if (expressionVariable.IndexExpressionsRPN != null && expressionVariable.IndexExpressionsRPN.Count > 0) { ValueBase indexValue = Compute(expressionVariable.IndexExpressionsRPN); if (indexValue.HasError) { error = indexValue.Error; } else if (!(indexValue is ValueNumeric)) { error = "La expresión del índice no tiene un valor numérico"; } else { index = (int)(indexValue as ValueNumeric).Value; } } // Si no hay ningún error, obtiene la variable if (string.IsNullOrWhiteSpace(error)) { // Obtiene la variable variable = variables.Search(expressionVariable.Name, index); // Si tiene algún miembro, busca ese miembro if (expressionVariable.Member != null && !string.IsNullOrWhiteSpace(expressionVariable.Member.Name)) { variable = Search(expressionVariable.Member, variable.Members, out error); } } // Devuelve la variable return(variable); }
/// <summary> /// Obtiene el valor de una variable /// </summary> private string TransformExpressionVariable(ExpressionVariableIdentifier expression) { return(expression.Name); }
/// <summary> /// Busca recursivamente el valor de una variable /// </summary> private async Task <(string error, VariableModel variable)> SearchAsync(ContextModel context, ExpressionVariableIdentifier expressionVariable, CancellationToken cancellationToken) { string error = string.Empty; VariableModel variable = null; int index = 0; // Obtiene el índice asociado a la variable if (expressionVariable.IndexExpressionsRPN?.Count > 0) { (string errorCompute, VariableModel indexVariable) = await ComputeAsync(context, expressionVariable.IndexExpressionsRPN, cancellationToken); if (!string.IsNullOrWhiteSpace(errorCompute)) { error = errorCompute; } else if (indexVariable.Type != VariableModel.VariableType.Numeric) { error = "Index expression is not a numeric value"; } else { index = (int)indexVariable.Value; } } // Si no hay ningún error, obtiene la variable if (string.IsNullOrWhiteSpace(error)) { variable = context.VariablesTable.Get(expressionVariable.Name, index); } // Devuelve la variable return(error, variable); }
/// <summary> /// Lee un identificador de variable /// </summary> private ExpressionVariableIdentifier ReadVariableIdentifier(Token token, out string error) { ExpressionVariableIdentifier expression = new ExpressionVariableIdentifier(token); Token nextToken = GetToken(true); // Inicializa los argumentos de salida error = ""; // Asigna el nombre de la variable expression.Name = token.Content; // Comprueba si hay un corchete, es decir, es una variable de array if (nextToken.Type == Token.TokenType.LeftCorchete) { // Quita el token GetToken(); // Asigna la expresión expression.IndexExpressions = ReadExpression(ExpressionReaderMode.AtVariable, out error); expression.IndexExpressionsRPN = _expressionEvaluator.ConvertToRPN(expression.IndexExpressions); // Si no ha habido ningún error ... if (string.IsNullOrWhiteSpace(error)) { // Obtiene el siguiente token nextToken = GetToken(true); // Si no es un corchete, añade un error, si es un corchete, quita el token if (nextToken.Type != Token.TokenType.RightCorchete) { error = "Falta el corchete final en la definición de variable"; } else { // Quita el token GetToken(); // ... y deja leído el siguiente para comprobar si es un miembro de variable nextToken = GetToken(true); } } } // Comprueba si hay un signo ->, es decir, tenemos una variable miembro if (nextToken.Type == Token.TokenType.VariablePointer) { // Quita el token GetToken(); // Obtiene el siguiente token sin sacarlo del buffer nextToken = GetToken(true); // Si es un literal, lo considera una variable (para los casos en $a->b en lugar de $a->$b) if (nextToken.Type == Token.TokenType.Literal) { nextToken.Content = "$" + nextToken.Content; nextToken.Type = Token.TokenType.Variable; } // Comprueba que sea una variable if (nextToken.Type == Token.TokenType.Variable) // ... si es así, obtiene el identificador del miembro { expression.Member = ReadVariableIdentifier(GetToken(), out error); } else // ... si no es así, indica el error { error = "Falta el identificador de la variable miembro"; } } // Devuelve la expresión return(expression); }
public virtual void VisitExpressionVariableIdentifier(ExpressionVariableIdentifier node, TContext context) { this.Visit(node.Identifier, context); }
public override void VisitExpressionVariableIdentifier(ExpressionVariableIdentifier node, SemanticModelBuilderContext context) { this.SafeAddSymbolReference(node.Identifier, node, context); base.VisitExpressionVariableIdentifier(node, context); }
/// <summary> /// Busca una variable en la colección /// </summary> internal Variable Search(ExpressionVariableIdentifier expressionVariable, out string error) { return(Search(expressionVariable, Variables, out error)); }
public override void VisitExpressionVariableIdentifier(ExpressionVariableIdentifier node, AstPrinterContext context) { base.VisitExpressionVariableIdentifier(node, context); }