private void MkEvents() { var evDeclarations = new List <SyntaxNode>(); var eventInitializationStmts = new List <StatementSyntax>(); const string eventsClassName = "Events"; foreach (string evName in exportedEvents) { //add declaration evDeclarations.Add( CSharpHelper.MkCSharpFieldDeclaration( IdentifierName("PrtEventValue"), EventName(evName), Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword))); //add initialization SyntaxNode payloadType = typeContext.PTypeToCSharpExpr(allEvents[evName].payloadType); SyntaxNode maxInstances; if (allEvents[evName].maxInstances == -1) { var s = "PrtEvent.DefaultMaxInstances"; maxInstances = IdentifierName(s); } else { maxInstances = CSharpHelper.MkCSharpNumericLiteralExpression(allEvents[evName].maxInstances); } SyntaxNode doAssume = allEvents[evName].maxInstancesAssumed ? CSharpHelper.MkCSharpTrueLiteralExpression() : CSharpHelper.MkCSharpFalseLiteralExpression(); SyntaxNode lhs = IdentifierName(EventName(evName)); SyntaxNode rhs = CSharpHelper.MkCSharpObjectCreationExpression( IdentifierName("PrtEventValue"), CSharpHelper.MkCSharpObjectCreationExpression( IdentifierName("PrtEvent"), CSharpHelper.MkCSharpStringLiteralExpression(evName), payloadType, maxInstances, doAssume)); eventInitializationStmts.Add(CSharpHelper.MkCSharpSimpleAssignmentExpressionStatement(lhs, rhs)); } string staticMethodName = "Events_" + Path.GetFileNameWithoutExtension(cSharpFileName.ToLower()); MethodDeclarationSyntax staticInitializer = MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier(staticMethodName)) .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword))) .WithBody(Block(eventInitializationStmts)).NormalizeWhitespace(); evDeclarations.Add(staticInitializer); SyntaxNode eventsClass = generator.ClassDeclaration( eventsClassName, null, Accessibility.Public, DeclarationModifiers.Partial, members: evDeclarations); members.Add(eventsClass); }
public static StatementSyntax MkCSharpTrace(string msg, params ExpressionSyntax[] pars) { var allPars = new List <ExpressionSyntax>(pars); allPars.Insert(0, CSharpHelper.MkCSharpStringLiteralExpression(msg)); return(SyntaxFactory.ExpressionStatement(MkCSharpInvocationExpression(MkCSharpDot("application", "TraceLine"), allPars.ToArray()))); }
public static StatementSyntax MkCSharpPrint(string msg, List <ExpressionSyntax> pars) { msg = "<PrintLog>" + " " + msg; var allPars = new List <ExpressionSyntax>(pars); allPars.Insert(0, CSharpHelper.MkCSharpStringLiteralExpression(msg)); return(SyntaxFactory.ExpressionStatement(MkCSharpInvocationExpression( MkCSharpDot("application", "Trace"), allPars.ToArray()))); }
private ExpressionSyntax ConstructType(FuncTerm type) { string typeKind = ((Id)type.Function).Name; ExpressionSyntax typeExpr; string typeName; if (ImportedTypes.ContainsKey(Factory.Instance.ToAST(type))) { typeName = GetNextTypeName(ImportedTypes[Factory.Instance.ToAST(type)]); return(GetTypeExpr(typeName)); } var originalName = "Interface"; if (ExportedTypes.ContainsKey(Factory.Instance.ToAST(type))) { originalName = ExportedTypes[Factory.Instance.ToAST(type)]; typeName = GetNextTypeName(ExportedTypes[Factory.Instance.ToAST(type)]); typeExpr = GetTypeExpr(typeName); } else { typeName = GetNextTypeName(); typeExpr = GetTypeExpr(typeName); } // add declaration and initialization if (typeKind == "BaseType") { string primitiveType = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(((Id)GetArgByIndex(type, 0)).Name.ToLowerInvariant()); Debug.Assert( primitiveType == "Any" || primitiveType == "Null" || primitiveType == "Bool" || primitiveType == "Int" || primitiveType == "Float" || primitiveType == "Event" || primitiveType == "Machine", $"Illegal BaseType: {primitiveType}"); if (primitiveType != "Any") { AddTypeInitialization( typeExpr, CSharpHelper.MkCSharpObjectCreationExpression(SyntaxFactory.IdentifierName($"Prt{primitiveType}Type"))); AddTypeDeclaration(typeName); } } else if (typeKind == "AnyType") { AddTypeDeclaration(typeName); AddTypeInitialization( typeExpr, CSharpHelper.MkCSharpObjectCreationExpression(SyntaxFactory.IdentifierName("PrtAnyType"))); } else if (typeKind == "NameType") { string enumTypeName = (GetArgByIndex(type, 0) as Cnst).GetStringValue(); if (pToCSharp.allEnums.ContainsKey(enumTypeName)) { var args = new List <ExpressionSyntax> { CSharpHelper.MkCSharpStringLiteralExpression(enumTypeName) }; foreach (KeyValuePair <string, int> x in pToCSharp.allEnums[enumTypeName]) { args.Add(CSharpHelper.MkCSharpStringLiteralExpression(x.Key)); args.Add(CSharpHelper.MkCSharpNumericLiteralExpression(x.Value)); } AddTypeDeclaration(typeName); AddTypeInitialization( typeExpr, CSharpHelper.MkCSharpObjectCreationExpression(SyntaxFactory.IdentifierName("PrtEnumType"), args.ToArray())); } else { var args = new List <ExpressionSyntax>(); AddTypeDeclaration(typeName); AddTypeInitialization( typeExpr, CSharpHelper.MkCSharpObjectCreationExpression(SyntaxFactory.IdentifierName($"Foreign_{enumTypeName}"), args.ToArray())); } } else if (typeKind == "TupType") { var memberTypes = new List <SyntaxNodeOrToken>(); while (type != null) { memberTypes.Add(PTypeToCSharpExpr((FuncTerm)GetArgByIndex(type, 0))); type = GetArgByIndex(type, 1) as FuncTerm; } var initializer = CSharpHelper.Intersperse(memberTypes, SyntaxFactory.Token(SyntaxKind.CommaToken)); AddTypeDeclaration(typeName); AddTypeInitialization( typeExpr, CSharpHelper.MkCSharpObjectCreationExpression( SyntaxFactory.IdentifierName("PrtTupleType"), CSharpHelper.MkCSharpArrayCreationExpression("PrtType", initializer.ToArray()))); } else if (typeKind == "NmdTupType") { var memberNames = new List <SyntaxNode>(); var memberTypes = new List <SyntaxNode>(); while (type != null) { var typeField = (FuncTerm)GetArgByIndex(type, 0); string nameField = ((Cnst)GetArgByIndex(typeField, 0)).GetStringValue(); memberNames.Add(CSharpHelper.MkCSharpStringLiteralExpression(nameField)); memberTypes.Add(PTypeToCSharpExpr((FuncTerm)GetArgByIndex(typeField, 1))); type = GetArgByIndex(type, 1) as FuncTerm; } var initializer = new List <SyntaxNodeOrToken>(); var ind = 0; foreach (SyntaxNode memberName in memberNames) { initializer.Add(memberName); initializer.Add(SyntaxFactory.Token(SyntaxKind.CommaToken)); initializer.Add(memberTypes[ind++]); initializer.Add(SyntaxFactory.Token(SyntaxKind.CommaToken)); } initializer.RemoveAt(initializer.Count - 1); AddTypeDeclaration(typeName); AddTypeInitialization( typeExpr, CSharpHelper.MkCSharpObjectCreationExpression( SyntaxFactory.IdentifierName("PrtNamedTupleType"), CSharpHelper.MkCSharpArrayCreationExpression("object", initializer.ToArray()))); } else if (typeKind == "SeqType") { SyntaxNode innerType = PTypeToCSharpExpr((FuncTerm)GetArgByIndex(type, 0)); AddTypeDeclaration(typeName); AddTypeInitialization( typeExpr, CSharpHelper.MkCSharpObjectCreationExpression(SyntaxFactory.IdentifierName("PrtSeqType"), innerType)); } else if (typeKind == "MapType") { SyntaxNode keyType = PTypeToCSharpExpr((FuncTerm)GetArgByIndex(type, 0)); SyntaxNode valType = PTypeToCSharpExpr((FuncTerm)GetArgByIndex(type, 1)); AddTypeDeclaration(typeName); AddTypeInitialization( typeExpr, CSharpHelper.MkCSharpObjectCreationExpression(SyntaxFactory.IdentifierName("PrtMapType"), keyType, valType)); } else { // typekind == "InterfaceType" ObjectCreationExpressionSyntax initializer = CSharpHelper.MkCSharpObjectCreationExpression( SyntaxFactory.IdentifierName("PrtInterfaceType"), CSharpHelper.MkCSharpStringLiteralExpression(originalName)); AddTypeDeclaration(typeName); AddTypeInitialization(typeExpr, initializer); } return(typeExpr); }
private SyntaxNode MkStaticAppConstructor(string testName) { var stmtList = new List <StatementSyntax>(); //Initialize types and events var nodes = dependsOn.Keys.Select(s => s.ToLower()).ToList(); foreach (var files in dependsOn.Values) { foreach (var f in files) { if (!nodes.Contains(f)) { nodes.Add(f); } } } var edges = new List <Tuple <string, string> >(); foreach (var file in dependsOn) { foreach (var dep in file.Value) { if (file.Key != dep) { edges.Add(new Tuple <string, string>(dep.ToLower(), file.Key.ToLower())); } } } List <string> topoOrder = TopologicalSortFiles <string>(nodes, edges); //make invocations to the initialization of all types foreach (var fName in topoOrder) { stmtList.Add( SyntaxFactory.ExpressionStatement(CSharpHelper.MkCSharpInvocationExpression( SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName("Types"), SyntaxFactory.IdentifierName("Types_" + fName)) ))); } //make invocation to the initialization of all events foreach (var fName in topoOrder) { stmtList.Add( SyntaxFactory.ExpressionStatement(CSharpHelper.MkCSharpInvocationExpression( SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName("Events"), SyntaxFactory.IdentifierName("Events_" + fName)) ))); } //Initialize all the maps //safe map foreach (var sm in allTests[testName].isSafeMap) { var safeadd = CSharpHelper.MkCSharpInvocationExpression( CSharpHelper.MkCSharpDot("isSafeMap", "Add"), CSharpHelper.MkCSharpStringLiteralExpression(sm.Key), sm.Value ? CSharpHelper.MkCSharpTrueLiteralExpression() : CSharpHelper.MkCSharpFalseLiteralExpression() ); stmtList.Add(SyntaxFactory.ExpressionStatement(safeadd)); } //rename map foreach (var rename in allTests[testName].renameMap) { var renameadd = CSharpHelper.MkCSharpInvocationExpression( CSharpHelper.MkCSharpDot("renameMap", "Add"), CSharpHelper.MkCSharpStringLiteralExpression(rename.Key), CSharpHelper.MkCSharpStringLiteralExpression(rename.Value) ); stmtList.Add(SyntaxFactory.ExpressionStatement(renameadd)); } //create map var listOfSpecMachineNames = allTests[testName].renameMap.Where(item => allTests[testName].specMachineMap.ContainsKey(item.Key)).Select(item => item.Value).Distinct(); var listOfMachineNames = allTests[testName].renameMap.Where(item => !allTests[testName].specMachineMap.ContainsKey(item.Key)).Select(item => item.Value).Distinct(); foreach (var specMachine in listOfSpecMachineNames) { var createadd = CSharpHelper.MkCSharpInvocationExpression( CSharpHelper.MkCSharpDot("createSpecMap", "Add"), CSharpHelper.MkCSharpStringLiteralExpression(specMachine), SyntaxFactory.IdentifierName(string.Format("CreateSpecMachine_{0}", specMachine))); stmtList.Add(SyntaxFactory.ExpressionStatement(createadd)); } foreach (var machine in listOfMachineNames) { //its a machine var createadd = CSharpHelper.MkCSharpInvocationExpression( CSharpHelper.MkCSharpDot("createMachineMap", "Add"), CSharpHelper.MkCSharpStringLiteralExpression(machine), SyntaxFactory.IdentifierName(string.Format("CreateMachine_{0}", machine)) ); stmtList.Add(SyntaxFactory.ExpressionStatement(createadd)); } //interface map foreach (var it in allTests[testName].interfaceMap) { var eventsParams = new SeparatedSyntaxList <ExpressionSyntax>(); foreach (var ev in it.Value) { eventsParams = eventsParams.Add(GetEventVar(ev)); } var initStmt = CSharpHelper.MkCSharpSimpleAssignmentExpressionStatement( SyntaxFactory.ElementAccessExpression( SyntaxFactory.IdentifierName("interfaceMap"), SyntaxFactory.BracketedArgumentList(SyntaxFactory.SingletonSeparatedList <ArgumentSyntax>(SyntaxFactory.Argument(CSharpHelper.MkCSharpStringLiteralExpression(it.Key)))) ), SyntaxFactory.ObjectCreationExpression( SyntaxFactory.GenericName(SyntaxFactory.Identifier("List"), SyntaxFactory.TypeArgumentList(SyntaxFactory.SingletonSeparatedList <TypeSyntax>(SyntaxFactory.IdentifierName("PrtEventValue")))), SyntaxFactory.ArgumentList(), SyntaxFactory.InitializerExpression(SyntaxKind.CollectionInitializerExpression, eventsParams)) ); stmtList.Add(initStmt); } //send actions { var eventsParams = new SeparatedSyntaxList <ExpressionSyntax>(); foreach (var ev in allTests[testName].sendActions) { eventsParams = eventsParams.Add(CSharpHelper.MkCSharpStringLiteralExpression(ev)); } var initStmt = CSharpHelper.MkCSharpSimpleAssignmentExpressionStatement( SyntaxFactory.IdentifierName("visibleEvents"), SyntaxFactory.ObjectCreationExpression( SyntaxFactory.GenericName(SyntaxFactory.Identifier("List"), SyntaxFactory.TypeArgumentList(SyntaxFactory.SingletonSeparatedList <TypeSyntax>(SyntaxFactory.IdentifierName("string")))), SyntaxFactory.ArgumentList(), SyntaxFactory.InitializerExpression(SyntaxKind.CollectionInitializerExpression, eventsParams)) ); stmtList.Add(initStmt); } //interface actions { var interfaceParams = new SeparatedSyntaxList <ExpressionSyntax>(); foreach (var it in allTests[testName].createActions) { interfaceParams = interfaceParams.Add(CSharpHelper.MkCSharpStringLiteralExpression(it)); } var initStmt = CSharpHelper.MkCSharpSimpleAssignmentExpressionStatement( SyntaxFactory.IdentifierName("visibleInterfaces"), SyntaxFactory.ObjectCreationExpression( SyntaxFactory.GenericName(SyntaxFactory.Identifier("List"), SyntaxFactory.TypeArgumentList(SyntaxFactory.SingletonSeparatedList <TypeSyntax>(SyntaxFactory.IdentifierName("string")))), SyntaxFactory.ArgumentList(), SyntaxFactory.InitializerExpression(SyntaxKind.CollectionInitializerExpression, interfaceParams)) ); stmtList.Add(initStmt); } //initialize the interface types foreach (var it in allTests[testName].interfaceMap) { var initStmt = CSharpHelper.MkCSharpSimpleAssignmentExpressionStatement( SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.ParenthesizedExpression( CSharpHelper.MkCSharpCastExpression("PrtInterfaceType", SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName("Types"), SyntaxFactory.IdentifierName(String.Format("type_{0}", it.Key))))), SyntaxFactory.IdentifierName("permissions")), SyntaxFactory.ElementAccessExpression( SyntaxFactory.IdentifierName("interfaceMap"), SyntaxFactory.BracketedArgumentList(SyntaxFactory.SingletonSeparatedList <ArgumentSyntax>(SyntaxFactory.Argument(CSharpHelper.MkCSharpStringLiteralExpression(it.Key)))) ) ); stmtList.Add(initStmt); } //spec machine map foreach (var specMachine in allTests[testName].specMachineMap) { SeparatedSyntaxList <ExpressionSyntax> machines = new SeparatedSyntaxList <ExpressionSyntax>(); machines = machines.AddRange(specMachine.Value.Select(x => CSharpHelper.MkCSharpStringLiteralExpression(x))); var addstmt = CSharpHelper.MkCSharpInvocationExpression( CSharpHelper.MkCSharpDot("specMachineMap", "Add"), CSharpHelper.MkCSharpStringLiteralExpression(specMachine.Key), SyntaxFactory.ObjectCreationExpression( CSharpHelper.MkCSharpGenericListType(SyntaxFactory.IdentifierName("string")), SyntaxFactory.ArgumentList(), SyntaxFactory.InitializerExpression(SyntaxKind.CollectionInitializerExpression, machines)) ); stmtList.Add(SyntaxFactory.ExpressionStatement(addstmt)); } //link map //declare temp variable var dictStringString = SyntaxFactory.GenericName( SyntaxFactory.Identifier("Dictionary"), SyntaxFactory.TypeArgumentList(SyntaxFactory.SeparatedList <TypeSyntax>( new SyntaxNodeOrToken[] { SyntaxFactory.PredefinedType( SyntaxFactory.Token(SyntaxKind.StringKeyword)), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.PredefinedType( SyntaxFactory.Token(SyntaxKind.StringKeyword)) })) ); stmtList.Add( SyntaxFactory.LocalDeclarationStatement( SyntaxFactory.VariableDeclaration( dictStringString, SyntaxFactory.SingletonSeparatedList <VariableDeclaratorSyntax>( SyntaxFactory.VariableDeclarator( SyntaxFactory.Identifier("_temp")) ) ) ) ); foreach (var creatorM in allTests[testName].linkMap) { var tempInit = CSharpHelper.MkCSharpSimpleAssignmentExpressionStatement( SyntaxFactory.IdentifierName("_temp"), CSharpHelper.MkCSharpObjectCreationExpression(dictStringString) ); stmtList.Add(tempInit); foreach (var linkmapitem in creatorM.Value) { stmtList.Add( SyntaxFactory.ExpressionStatement( CSharpHelper.MkCSharpInvocationExpression( CSharpHelper.MkCSharpDot("_temp", "Add"), CSharpHelper.MkCSharpStringLiteralExpression(linkmapitem.Key), CSharpHelper.MkCSharpStringLiteralExpression(linkmapitem.Value) )) ); } var addLinkItem = SyntaxFactory.ExpressionStatement( CSharpHelper.MkCSharpInvocationExpression( CSharpHelper.MkCSharpDot("linkMap", "Add"), CSharpHelper.MkCSharpStringLiteralExpression(creatorM.Key), SyntaxFactory.IdentifierName("_temp")) ); stmtList.Add(addLinkItem); } var staticConstrutor = SyntaxFactory.ConstructorDeclaration( SyntaxFactory.Identifier("Application")) .WithModifiers( SyntaxFactory.TokenList( SyntaxFactory.Token(SyntaxKind.StaticKeyword))) .WithBody(SyntaxFactory.Block(stmtList)) .NormalizeWhitespace(); return(staticConstrutor); }
private List <SyntaxNode> MkAppConstructors(string testName) { //parameterless constructor List <SyntaxNode> constructorList = new List <SyntaxNode>(); var constructor_1 = generator.ConstructorDeclaration("Application", null, Accessibility.Public, baseConstructorArguments: new SyntaxNode[0]); constructorList.Add(constructor_1); //constructor with the initialize paramenter which is called by Ptester at the start. var constructorParameters = new SyntaxNode[] { generator.ParameterDeclaration("initialize", generator.TypeExpression(SpecialType.System_Boolean)) }; List <SyntaxNode> stmtList = new List <SyntaxNode>(); foreach (var monName in allTests[testName].specMachineMap.Keys) { stmtList.Add(SyntaxFactory.ExpressionStatement( CSharpHelper.MkCSharpInvocationExpression(SyntaxFactory.IdentifierName("CreateSpecMachine"), CSharpHelper.MkCSharpStringLiteralExpression(monName)))); } var constructorBody = generator.ExpressionStatement( generator.InvocationExpression( generator.IdentifierName("CreateMainMachine"))); stmtList.Add(constructorBody); var constructor_2 = generator.ConstructorDeclaration("Application", constructorParameters, Accessibility.Public, baseConstructorArguments: new SyntaxNode[0], statements: stmtList.ToArray()); constructorList.Add(constructor_2); //skeleton constructor used by the clone function. var makeSkeletonMethodBody = generator.ReturnStatement(generator.ObjectCreationExpression(generator.IdentifierName("Application"))); var makeSkeletonMethodDecl = generator.MethodDeclaration("MakeSkeleton", null, null, generator.IdentifierName("StateImpl"), Accessibility.Public, DeclarationModifiers.Override, new SyntaxNode[] { makeSkeletonMethodBody }); constructorList.Add(makeSkeletonMethodDecl); return(constructorList); }
public static StatementSyntax MkCSharpAssert(ExpressionSyntax expr, string errorMsg) { return(SyntaxFactory.IfStatement( CSharpHelper.MkCSharpNot(expr), SyntaxFactory.ThrowStatement(MkCSharpObjectCreationExpression(SyntaxFactory.IdentifierName("PrtAssertFailureException"), CSharpHelper.MkCSharpStringLiteralExpression(errorMsg))))); }