/// <summary> /// Retrieve real expression of varexpression /// </summary> /// <param name="expression">VarExpression to get real expression from</param> /// <returns>Real expression value</returns> private Expression GetReferenceExpression(VarExpression expression) { String identifier = expression.GetVariableIdentifier(); Expression referenceExpr = null; do { if (SymbolTable.ContainsVariable(identifier)) { referenceExpr = SymbolTable.GetVariableDefinition(identifier); if (referenceExpr == expression && SymbolTable.GetParentSymbolTable() != null) { //Get reference from parent symboltable referenceExpr = SymbolTable.GetParentSymbolTable().GetVariableDefinition(identifier); } } } while (referenceExpr is VarExpression); return(referenceExpr); }
/// <summary> /// Interpret VarExpression /// </summary> /// <param name="expression">VarExpression to interpret</param> public override void Visit(VarExpression expression) { //Check if variable does exist String varName = expression.GetVariableIdentifier(); Expression check = SymbolTable.GetVariableDefinition(varName); //If reference has been made to parent variable with same name, take parent reference if (check == expression && SymbolTable.GetParentSymbolTable() != null) { check = SymbolTable.GetParentSymbolTable().GetVariableDefinition(varName); } if (check != null) { //Visit variable to retrieve current value check.AcceptVisitor(this); } else { TextValue = "undef"; } }
/// <summary> /// Method which evaluates an predicate and returns true or false /// </summary> /// <param name="predicate">Predicate to evaluate</param> /// <returns>True if predicate is true, otherwise false</returns> private bool EvaluatePredicate(Predicate predicate) { if (predicate is IsPredicate) { //Evaluate Expression Predicate IsPredicate isPredicate = (IsPredicate)predicate; Expression expression = isPredicate.GetExpression(); //In case of VarExpression, get expression from symboltable if (expression is VarExpression) { expression = GetReferenceExpression((VarExpression)expression); if (expression == null) { return(false); } } //If right type return true, otherwise false if (isPredicate.GetType() is StringType) { return(expression is TextExpression); } else if (isPredicate.GetType() is ListType) { return(expression is ListExpression); } else if (isPredicate.GetType() is RecordType) { return(expression is RecordExpression); } else { //No match between types which could be checked, so false return(false); } } else if (predicate is ExpressionPredicate) { //Evaluate Expression Predicate ExpressionPredicate expressionPredicate = (ExpressionPredicate)predicate; Expression expression = expressionPredicate.GetExpression(); if (expression is FieldExpression) { //Check if specific field exists in record (not null) Expression expr = GetExpression((FieldExpression)expression); return(expr != null); } else if (expression is VarExpression) { //Check if specific variable is defined VarExpression varExpr = (VarExpression)expression; return(SymbolTable.ContainsVariable(varExpr.GetVariableIdentifier())); } else { //Other expressions are always true, because they doesn't refer to something return(true); } } else if (predicate is AndPredicate) { //Evaluate And Predicate AndPredicate andPredicate = (AndPredicate)predicate; return(EvaluatePredicate(andPredicate.GetLeftPredicate()) && EvaluatePredicate(andPredicate.GetLeftPredicate())); } else if (predicate is OrPredicate) { //Evaluate Or Predicate OrPredicate orPredicate = (OrPredicate)predicate; return(EvaluatePredicate(orPredicate.GetLeftPredicate()) || EvaluatePredicate(orPredicate.GetRightPredicate())); } else if (predicate is NotPredicate) { //Evaluate Not Predicate NotPredicate notPredicate = (NotPredicate)predicate; return(!EvaluatePredicate(notPredicate.GetPredicate())); } return(false); }