/// <summary> /// Checks an expression associated to a model element /// </summary> /// <param name="model">The model element on which the expression is defined</param> /// <param name="expression">The expression to check</param> /// <returns>the expression parse tree</returns> private Interpreter.Expression checkExpression(ModelElement model, string expression) { Interpreter.Expression retVal = null; Interpreter.Parser parser = model.EFSSystem.Parser; try { retVal = parser.Expression(model, expression); if (retVal != null) { retVal.checkExpression(); Types.Type type = retVal.GetExpressionType(); if (type == null) { model.AddError("Cannot determine expression type (5) for " + retVal.ToString()); } } else { model.AddError("Cannot parse expression"); } } catch (Exception exception) { model.AddException(exception); } return(retVal); }
private void CheckActualAgainstFormal(Dictionary <string, Expression> actuals, Expression expression, Parameter parameter) { actuals[parameter.Name] = expression; expression.checkExpression(); Types.Type argumentType = expression.GetExpressionType(); if (argumentType == null) { AddError("Cannot evaluate argument type for argument " + expression.ToString()); } else { if (parameter.Type == null) { AddError("Cannot evaluate formal parameter type for " + parameter.Name); } else { if (!parameter.Type.Match(argumentType)) { AddError("Invalid argument " + expression.ToString() + " type, expected " + parameter.Type.FullName + ", actual " + argumentType.FullName); } } } }
public override void visit(Generated.Expectation obj, bool subNodes) { Tests.Expectation expect = obj as Tests.Expectation; if (expect != null) { try { expect.Messages.Clear(); if (!expect.Expression.Contains("%")) { Interpreter.Expression expression = checkExpression(expect, expect.Expression); if (!expect.EFSSystem.BoolType.Match(expression.GetExpressionType())) { expect.AddError("Expression type should be Boolean"); } } if (expect.getCondition() != null && !expect.getCondition().Contains("%")) { Interpreter.Expression expression = checkExpression(expect, expect.getCondition()); if (!expect.EFSSystem.BoolType.Match(expression.GetExpressionType())) { expect.AddError("Condition type should be Boolean"); } } } catch (Exception exception) { expect.AddException(exception); } } base.visit(obj, subNodes); }
/// <summary> /// Checks the expression and appends errors to the root tree node when inconsistencies are found /// </summary> public override void CheckExpression() { Structure structureType = Structure.GetExpressionType() as Structure; if (structureType != null) { if (structureType.IsAbstract) { AddError("Instantiation of abstract types is forbidden", RuleChecksEnum.SyntaxError); } foreach (KeyValuePair <Designator, Expression> pair in Associations) { Designator name = pair.Key; Expression expression = pair.Value; List <INamable> targets = new List <INamable>(); structureType.Find(name.Image, targets); if (targets.Count > 0) { expression.CheckExpression(); Type type = expression.GetExpressionType(); if (type != null) { if (type.IsAbstract) { AddError("Instantiation of abstract types is forbidden", RuleChecksEnum.SyntaxError); } else { foreach (INamable namable in targets) { ITypedElement element = namable as ITypedElement; if (element != null && element.Type != null) { if (!element.Type.Match(type)) { AddError("Expression " + expression + " type (" + type.FullName + ") does not match the target element " + element.Name + " type (" + element.Type.FullName + ")", RuleChecksEnum.SyntaxError); } } } } } else { AddError("Expression " + expression + " type cannot be found", RuleChecksEnum.SyntaxError); } } else { Root.AddError("Cannot find " + name + " in structure " + Structure); } } } else { AddError("Cannot find structure type " + Structure, RuleChecksEnum.SyntaxError); } }
/// <summary> /// Provides the type of this expression /// </summary> /// <param name="context">The interpretation context</param> /// <returns></returns> public override Types.Type GetExpressionType() { Functions.Function retVal = (Functions.Function)Generated.acceptor.getFactory().createFunction(); retVal.Name = ToString(); retVal.ReturnType = Expression.GetExpressionType(); foreach (Parameter parameter in Parameters) { Parameter param = (Parameter)Generated.acceptor.getFactory().createParameter(); param.Name = parameter.Name; param.Type = parameter.Type; retVal.appendParameters(param); } retVal.Enclosing = Root; return(retVal); }
/// <summary> /// Checks the expression and appends errors to the root tree node when inconsistencies are found /// </summary> public override void checkExpression() { Types.Structure structureType = Structure.GetExpressionType() as Types.Structure; if (structureType != null) { foreach (KeyValuePair <string, Expression> pair in Associations) { string name = pair.Key; Expression expression = pair.Value; List <Utils.INamable> targets = new List <Utils.INamable>(); structureType.Find(name, targets); if (targets.Count > 0) { expression.checkExpression(); Types.Type type = expression.GetExpressionType(); if (type != null) { foreach (Utils.INamable namable in targets) { Types.ITypedElement element = namable as Types.ITypedElement; if (element != null && element.Type != null) { if (!element.Type.Match(type)) { AddError("Expression " + expression.ToString() + " type (" + type.FullName + ") does not match the target element " + element.Name + " type (" + element.Type.FullName + ")"); } } } } else { AddError("Expression " + expression.ToString() + " type cannot be found"); } } else { Root.AddError("Cannot find " + name + " in structure " + Structure.ToString()); } } } else { AddError("Cannot find structure type " + Structure.ToString()); } }
public override void visit(Generated.PreCondition obj, bool subNodes) { Rules.PreCondition preCondition = obj as Rules.PreCondition; if (preCondition != null) { try { // Check whether the expression is valid Interpreter.Expression expression = checkExpression(preCondition, preCondition.Condition); if (!preCondition.Dictionary.EFSSystem.BoolType.Match(expression.GetExpressionType())) { preCondition.AddError("Expression type should be Boolean"); } Types.ITypedElement element = OverallTypedElementFinder.INSTANCE.findByName(preCondition, preCondition.findVariable()); if (element != null) { if (element.Type is Types.StateMachine) { if (preCondition.findOperator() != null) { if (preCondition.findOperator().CompareTo("==") == 0) { preCondition.AddWarning("Operator == should not be used for state machines"); } else if (preCondition.findOperator().CompareTo("!=") == 0) { preCondition.AddWarning("Operator != should not be used for state machines"); } } } } } catch (Exception exception) { preCondition.AddException(exception); } } base.visit(obj, subNodes); }
/// <summary> /// Provides the type of this expression /// </summary> /// <returns></returns> public override Type GetExpressionType() { Type retVal = null; if (Term != null) { retVal = Term.GetExpressionType(); } else if (Expression != null) { if (Not.Equals(UnaryOp)) { Type type = Expression.GetExpressionType(); if (type is BoolType) { retVal = type; } else { AddError("Cannot apply NOT on non boolean types", RuleChecksEnum.SemanticAnalysisError); } } else if (Minus.Equals(UnaryOp)) { Type type = Expression.GetExpressionType(); if (type == EfsSystem.Instance.IntegerType || type == EfsSystem.Instance.DoubleType || type is Range) { retVal = type; } else { AddError("Cannot apply - on non integral types", RuleChecksEnum.SemanticAnalysisError); } } else { retVal = Expression.GetExpressionType(); } } return(retVal); }
/// <summary> /// Checks the expression and appends errors to the root tree node when inconsistencies are found /// </summary> public override void CheckExpression() { base.CheckExpression(); InitialValue.CheckExpression(); Type initialValueType = InitialValue.GetExpressionType(); if (initialValueType != null) { Expression.CheckExpression(); Type expressionType = Expression.GetExpressionType(); if (expressionType != null) { if (expressionType != initialValueType) { AddError("Expression " + Expression + " has not the same type (" + expressionType.FullName + " than initial value " + InitialValue + " type " + initialValueType.FullName, RuleChecksEnum.SemanticAnalysisError); } } else { AddError("Cannot determine type of expression " + Expression, RuleChecksEnum.SemanticAnalysisError); } Type conditionType = Condition.GetExpressionType(); if (conditionType != null) { if (!(conditionType is BoolType)) { AddError("Condition " + Condition + " does not evaluate to a boolean", RuleChecksEnum.SemanticAnalysisError); } } else { AddError("Cannot determine type of condition " + Condition, RuleChecksEnum.SyntaxError); } } else { AddError("Cannot determine type of the initial value " + InitialValue, RuleChecksEnum.SemanticAnalysisError); } }
/// <summary> /// Provides the type of this expression /// </summary> /// <param name="context">The interpretation context</param> /// <returns></returns> public override Types.Type GetExpressionType() { Types.Type retVal = null; if (Term != null) { retVal = Term.GetExpressionType(); } else if (Expression != null) { if (NOT.CompareTo(UnaryOp) == 0) { Types.Type type = Expression.GetExpressionType(); if (type is Types.BoolType) { retVal = type; } else { AddError("Cannot apply NOT on non boolean types"); } } else if (MINUS.CompareTo(UnaryOp) == 0) { Types.Type type = Expression.GetExpressionType(); if (type == EFSSystem.IntegerType || type == EFSSystem.DoubleType || type is Types.Range) { retVal = type; } else { AddError("Cannot apply - on non integral types"); } } else { retVal = Expression.GetExpressionType(); } } return(retVal); }
/// <summary> /// Checks the expression and appends errors to the root tree node when inconsistencies are found /// </summary> /// <param name="context">The interpretation context</param> public override void checkExpression() { base.checkExpression(); InitialValue.checkExpression(); Types.Type initialValueType = InitialValue.GetExpressionType(); if (initialValueType != null) { Expression.checkExpression(); Types.Type expressionType = Expression.GetExpressionType(); if (expressionType != null) { if (expressionType != initialValueType) { AddError("Expression " + Expression + " has not the same type (" + expressionType.FullName + " than initial value " + InitialValue + " type " + initialValueType.FullName); } } else { AddError("Cannot determine type of expression " + Expression); } Types.Type conditionType = Condition.GetExpressionType(); if (conditionType != null) { if (!(conditionType is Types.BoolType)) { AddError("Condition " + Condition + " does not evaluate to a boolean"); } } else { AddError("Cannot determine type of condition " + Condition); } } else { AddError("Cannot determine type of the initial value " + InitialValue); } }
private void CheckActualAgainstFormal(Dictionary<string, Expression> actuals, Expression expression, Parameter parameter) { actuals[parameter.Name] = expression; expression.checkExpression(); Types.Type argumentType = expression.GetExpressionType(); if (argumentType == null) { AddError("Cannot evaluate argument type for argument " + expression.ToString()); } else { if (parameter.Type == null) { AddError("Cannot evaluate formal parameter type for " + parameter.Name); } else { if (!parameter.Type.Match(argumentType)) { AddError("Invalid argument " + expression.ToString() + " type, expected " + parameter.Type.FullName + ", actual " + argumentType.FullName); } } } }
/// <summary> /// Provides the type of this expression /// </summary> /// <returns></returns> public override Type GetExpressionType() { return(Expression.GetExpressionType()); }
/// <summary> /// Ensures that the parameter provided corresponds to a function double->double /// </summary> /// <param name="root">Element on which the errors shall be attached</param> /// <param name="context">The context used to evaluate the expression</param> /// <param name="expression">The expression which references the function</param> /// <param name="count">the expected number of parameters</param> protected virtual void CheckFunctionalParameter(ModelElement root, InterpretationContext context, Expression expression, int count) { Type type = expression.GetExpressionType(); Function function = type as Function; if (function != null) { if (function.FormalParameters.Count == count) { foreach (Parameter parameter in function.FormalParameters) { if (!parameter.Type.IsDouble()) { root.AddError(expression.ToString() + " does not takes a double for parameter " + parameter.Name); } } } else { root.AddError(expression.ToString() + " does not take " + count + "parameter(s) as input"); } if (!function.ReturnType.IsDouble()) { root.AddError(expression.ToString() + " does not return a double"); } } else { if (!type.IsDouble()) { root.AddError(expression.ToString() + " type is not double"); } } }