public Formula BuildFormula(ParsedElement value, Dictionary <string, Entity> declaredVariables) { if (value == null) { throw new ArgumentNullException("value"); } if (value.GetType() == typeof(ParsedForallExpression)) { var a = new Forall(); var d2 = new Dictionary <string, KAOSTools.Core.Entity> (declaredVariables); foreach (var arg in (value as ParsedForallExpression).arguments) { var name = arg.VariableName; var type = GetOrCreateEntity(arg.Type); if (declaredVariables.ContainsKey(name)) { throw new BuilderException(string.Format("'{0}' is already defined", name), value.Filename, value.Line, value.Col); } a.Declarations.Add(new KAOSTools.Core.ArgumentDeclaration() { Name = name, Type = type.Identifier }); d2.Add(name, type); } a.Enclosed = BuildFormula((value as ParsedForallExpression).Enclosed, d2); return(a); } else if (value.GetType() == typeof(ParsedExistsExpression)) { var a = new Exists(); var d2 = new Dictionary <string, KAOSTools.Core.Entity> (declaredVariables); foreach (var arg in (value as ParsedExistsExpression).arguments) { var name = arg.VariableName; var type = GetOrCreateEntity(arg.Type); if (declaredVariables.ContainsKey(name)) { throw new BuilderException(string.Format("'{0}' is already defined", name), value.Filename, value.Line, value.Col); } a.Declarations.Add(new KAOSTools.Core.ArgumentDeclaration() { Name = name, Type = type.Identifier }); d2.Add(name, type); } a.Enclosed = BuildFormula((value as ParsedExistsExpression).Enclosed, d2); return(a); } else if (value.GetType() == typeof(ParsedStrongImplyExpression)) { return(new StrongImply() { Left = BuildFormula((value as ParsedStrongImplyExpression).Left, declaredVariables), Right = BuildFormula((value as ParsedStrongImplyExpression).Right, declaredVariables) }); } else if (value.GetType() == typeof(ParsedImplyExpression)) { return(new Imply() { Left = BuildFormula((value as ParsedImplyExpression).Left, declaredVariables), Right = BuildFormula((value as ParsedImplyExpression).Right, declaredVariables) }); } else if (value.GetType() == typeof(ParsedEquivalenceExpression)) { return(new Equivalence() { Left = BuildFormula((value as ParsedEquivalenceExpression).Left, declaredVariables), Right = BuildFormula((value as ParsedEquivalenceExpression).Right, declaredVariables) }); } else if (value.GetType() == typeof(ParsedUntilExpression)) { return(new Until() { Left = BuildFormula((value as ParsedUntilExpression).Left, declaredVariables), Right = BuildFormula((value as ParsedUntilExpression).Right, declaredVariables) }); } else if (value.GetType() == typeof(ParsedUnlessExpression)) { return(new Unless() { Left = BuildFormula((value as ParsedUnlessExpression).Left, declaredVariables), Right = BuildFormula((value as ParsedUnlessExpression).Right, declaredVariables) }); } else if (value.GetType() == typeof(ParsedReleaseExpression)) { return(new Release() { Left = BuildFormula((value as ParsedReleaseExpression).Left, declaredVariables), Right = BuildFormula((value as ParsedReleaseExpression).Right, declaredVariables) }); } else if (value.GetType() == typeof(ParsedAndExpression)) { return(new And() { Left = BuildFormula((value as ParsedAndExpression).Left, declaredVariables), Right = BuildFormula((value as ParsedAndExpression).Right, declaredVariables) }); } else if (value.GetType() == typeof(ParsedOrExpression)) { return(new Or() { Left = BuildFormula((value as ParsedOrExpression).Left, declaredVariables), Right = BuildFormula((value as ParsedOrExpression).Right, declaredVariables) }); } else if (value.GetType() == typeof(ParsedNotExpression)) { return(new Not() { Enclosed = BuildFormula((value as ParsedNotExpression).Enclosed, declaredVariables) }); } else if (value.GetType() == typeof(ParsedNextExpression)) { return(new Next() { Enclosed = BuildFormula((value as ParsedNextExpression).Enclosed, declaredVariables) }); } else if (value.GetType() == typeof(ParsedEventuallyExpression)) { return(new Eventually() { Enclosed = BuildFormula((value as ParsedEventuallyExpression).Enclosed, declaredVariables), TimeBound = BuildTimeBound((value as ParsedEventuallyExpression).TimeBound) }); } else if (value.GetType() == typeof(ParsedGloballyExpression)) { return(new Globally() { Enclosed = BuildFormula((value as ParsedGloballyExpression).Enclosed, declaredVariables), TimeBound = BuildTimeBound((value as ParsedGloballyExpression).TimeBound) }); } else if (value.GetType() == typeof(ParsedPredicateReferenceExpression)) { var prel = value as ParsedPredicateReferenceExpression; // Check if arguments are all defined foreach (var arg in prel.ActualArguments) { if (!declaredVariables.ContainsKey(arg)) { throw new BuilderException(string.Format("'{0}' is not declared"), value.Filename, value.Line, value.Col); } } return(new PredicateReference() { PredicateIdentifier = GetOrCreatePredicate(prel, declaredVariables).Identifier, ActualArguments = prel.ActualArguments }); } else if (value.GetType() == typeof(ParsedInRelationExpression)) { var prel = value as ParsedInRelationExpression; foreach (var arg in prel.Variables) { if (!declaredVariables.ContainsKey(arg)) { throw new BuilderException(string.Format("'{0}' is not declared", arg), value.Filename, value.Line, value.Col); } } return(new RelationReference() { Relation = GetOrCreateRelation(value as ParsedInRelationExpression, declaredVariables).Identifier, ActualArguments = prel.Variables }); } else if (value.GetType() == typeof(ParsedAttributeReferenceExpression)) { var pref = value as ParsedAttributeReferenceExpression; if (declaredVariables.ContainsKey(pref.Variable)) { var boolType = GetOrCreateGivenType("boolean"); return(new AttributeReference() { Variable = pref.Variable, Entity = declaredVariables [pref.Variable].Identifier, Attribute = GetOrCreateAttribute(value as ParsedAttributeReferenceExpression, declaredVariables [pref.Variable], boolType).Identifier }); } else { throw new BuilderException(string.Format("Variable '{0}' is not declared", pref.Variable), value.Filename, value.Line, value.Col); } } else if (value.GetType() == typeof(ParsedComparisonExpression)) { var pref = value as ParsedComparisonExpression; ComparisonCriteria criteria; if (pref.criteria == ParsedComparisonCriteria.Equals) { criteria = ComparisonCriteria.Equals; } else if (pref.criteria == ParsedComparisonCriteria.NotEquals) { criteria = ComparisonCriteria.NotEquals; } else if (pref.criteria == ParsedComparisonCriteria.BiggerThan) { criteria = ComparisonCriteria.BiggerThan; } else if (pref.criteria == ParsedComparisonCriteria.BiggerThanOrEquals) { criteria = ComparisonCriteria.BiggerThanOrEquals; } else if (pref.criteria == ParsedComparisonCriteria.LessThan) { criteria = ComparisonCriteria.LessThan; } else if (pref.criteria == ParsedComparisonCriteria.LessThanOrEquals) { criteria = ComparisonCriteria.LessThanOrEquals; } else { throw new NotImplementedException(); } return(new ComparisonPredicate() { Criteria = criteria, Left = BuildFormula(pref.Left, declaredVariables), Right = BuildFormula(pref.Right, declaredVariables) }); } else if (value.GetType() == typeof(ParsedStringConstantExpression)) { return(new StringConstant { Value = Sanitize((value as ParsedStringConstantExpression).Value) }); } else if (value.GetType() == typeof(ParsedNumericConstantExpression)) { return(new NumericConstant { Value = (value as ParsedNumericConstantExpression).Value }); } else if (value.GetType() == typeof(ParsedBoolConstantExpression)) { return(new BoolConstant { Value = (value as ParsedBoolConstantExpression).Value }); } else if (value.GetType() == typeof(ParsedVariableReference)) { if (!declaredVariables.ContainsKey((value as ParsedVariableReference).Value)) { throw new BuilderException(string.Format("Variable '{0}' is not declared", (value as ParsedVariableReference).Value), value.Filename, value.Line, value.Col); } return(new VariableReference { Name = (value as ParsedVariableReference).Value }); } throw new NotImplementedException(string.Format("{0} is not yet supported", value.GetType().Name)); }
protected override Expression ParseQuantifier(TokenSet followers) { Token kind = this.currentToken; SourceLocationBuilder slb = this.GetSourceLocationBuilderForLastScannedToken(); this.GetNextToken(); TokenSet followersOrLeftBraceOrSemicolonOrUnaryStart = followers | TS.LeftBraceOrRightParenthesisOrSemicolonOrUnaryStart | TS.PrimaryStart; List<LocalDeclarationsStatement> boundVariables = this.ParseQuantifierBoundVariables(followersOrLeftBraceOrSemicolonOrUnaryStart); IEnumerable<IEnumerable<Expression>> triggers = this.ParseQuantifierTriggers(followers | TS.UnaryStart | TS.PrimaryStart); Expression condition = this.ParseExpression(followers); slb.UpdateToSpan(this.scanner.SourceLocationOfLastScannedToken); Expression result; if (kind == Token.Exists) result = new Exists(boundVariables, condition, slb); else if (kind == Token.Forall) result = new Forall(boundVariables, condition, slb); else if (kind == Token.Lambda) result = new VccLambda(boundVariables, new CompileTimeConstant(true, SourceDummy.SourceLocation), condition, slb); else throw new InvalidOperationException(); this.compilation.ContractProvider.AssociateTriggersWithQuantifier(result, triggers); return result; }