private void MkCreateSpecMachineMethod(string machineName) { var fields = new List <SyntaxNode>(); //create machine fields.Add( generator.LocalDeclarationStatement( generator.IdentifierName("var"), "machine", generator.ObjectCreationExpression( generator.IdentifierName(machineName), new List <SyntaxNode> { generator.IdentifierName("application") }))); //log fields.Add(CSharpHelper.MkCSharpTrace($"<CreateLog> Created spec Machine {machineName}")); fields.AddRange(allMachines[machineName].observesEvents.Select(x => CSharpHelper.MkCSharpInvocationExpression(CSharpHelper.MkCSharpDot("machine", "observes", "Add"), GetEventVar(x)))); var prtvalueNull = (ExpressionSyntax)generator.MemberAccessExpression(IdentifierName("PrtEventValue"), IdentifierName("@null")); //execute the entry function of the machine fields.Add( CSharpHelper.MkCSharpInvocationExpression( CSharpHelper.MkCSharpDot("machine", "PrtEnqueueEvent"), prtvalueNull, prtvalueNull, LiteralExpression(SyntaxKind.NullLiteralExpression))); //return machine; fields.Add(generator.ReturnStatement(generator.IdentifierName("machine"))); var methodPars = new[] { generator.ParameterDeclaration("application", generator.IdentifierName("StateImpl")) }; SyntaxNode makeCreateSpecDecl = generator.MethodDeclaration( $"CreateSpecMachine_{machineName}", methodPars, null, IdentifierName("PrtSpecMachine"), Accessibility.Public, DeclarationModifiers.Static, fields); members.Add(makeCreateSpecDecl); }
private void MkCreateRealMachineMethod(string machineName) { var fields = new List <SyntaxNode>(); //create machine MachineInfo machineInfo = allMachines[machineName]; //There are three cases: //- default (no constraint on queue size): maxQueueSizeAssumed == false; maxQueueSize = default (10?) //- assume <maxQueueSize>: maxQueueSize > 0, maxQueueSizeAssumed == true; //- assert <maxQueueSize>: maxQueueSize > 0, maxQueueSizeAssumed == false; if (machineInfo.maxQueueSize > 0) { fields.Add( generator.LocalDeclarationStatement( generator.IdentifierName("var"), "machine", generator.ObjectCreationExpression( generator.IdentifierName(machineName), new List <SyntaxNode> { generator.IdentifierName("application"), generator.LiteralExpression(machineInfo.maxQueueSize), generator.LiteralExpression(machineInfo.maxQueueSizeAssumed) }))); } else { fields.Add( generator.LocalDeclarationStatement( generator.IdentifierName("var"), "machine", generator.ObjectCreationExpression( generator.IdentifierName(machineName), new List <SyntaxNode> { generator.IdentifierName("application"), MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, IdentifierName("PrtImplMachine"), IdentifierName("DefaultMaxBufferSize")), generator.LiteralExpression(machineInfo.maxQueueSizeAssumed) }))); } // log fields.Add( CSharpHelper.MkCSharpTrace( $"<CreateLog> Created Machine {machineName}-{{0}}", CSharpHelper.MkCSharpDot("machine", "instanceNumber"))); //initialize the permission set for self if (allMachines[machineName].receiveSet == null) { fields.Add( CSharpHelper.MkCSharpSimpleAssignmentExpressionStatement( CSharpHelper.MkCSharpDot(IdentifierName("machine"), "self", "permissions"), LiteralExpression(SyntaxKind.NullLiteralExpression))); } else { fields.AddRange( allMachines[machineName].receiveSet.Select( ev => CSharpHelper.MkCSharpInvocationExpression( CSharpHelper.MkCSharpDot(IdentifierName("machine"), "self", "permissions", "Add"), GetEventVar(ev)))); } //initialize the send set if (allMachines[machineName].sendsSet == null) { fields.Add( CSharpHelper.MkCSharpSimpleAssignmentExpressionStatement( CSharpHelper.MkCSharpDot(IdentifierName("machine"), "sends"), LiteralExpression(SyntaxKind.NullLiteralExpression))); } else { fields.AddRange( allMachines[machineName].sendsSet.Select( ev => CSharpHelper.MkCSharpInvocationExpression( CSharpHelper.MkCSharpDot(IdentifierName("machine"), "sends", "Add"), GetEventVar(ev)))); } //machine.currentPayload = payload; fields.Add( CSharpHelper.MkCSharpSimpleAssignmentExpressionStatement( CSharpHelper.MkCSharpDot("machine", "currentPayload"), IdentifierName("payload"))); //return machine; fields.Add(generator.ReturnStatement(generator.IdentifierName("machine"))); var methodPars = new[] { generator.ParameterDeclaration("application", generator.IdentifierName("StateImpl")), generator.ParameterDeclaration("payload", generator.IdentifierName("PrtValue")) }; SyntaxNode makeCreateMachineDecl = generator.MethodDeclaration( $"CreateMachine_{machineName}", methodPars, null, // type parameters generator.IdentifierName("PrtImplMachine"), Accessibility.Public, DeclarationModifiers.Static, fields); members.Add(makeCreateMachineDecl); }
public static ExpressionSyntax MkCSharpDot(string first, params string[] names) { return(CSharpHelper.MkCSharpDot(SyntaxFactory.IdentifierName(first), names)); }
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); }
public SyntaxNode MkClass() { var generator = translator.generator; //StartState property (getter only, since there's no setter in the base class): string startState = machineInfo.initStateName; var startStateProperty = SyntaxFactory.PropertyDeclaration( SyntaxFactory.IdentifierName("PrtState"), SyntaxFactory.Identifier("StartState")) .WithModifiers( SyntaxFactory.TokenList( new[] { SyntaxFactory.Token(SyntaxKind.PublicKeyword), SyntaxFactory.Token(SyntaxKind.OverrideKeyword) })) .WithAccessorList( SyntaxFactory.AccessorList( SyntaxFactory.SingletonList <AccessorDeclarationSyntax>( SyntaxFactory.AccessorDeclaration( SyntaxKind.GetAccessorDeclaration, SyntaxFactory.Block( SyntaxFactory.SingletonList <StatementSyntax>( SyntaxFactory.ReturnStatement( SyntaxFactory.IdentifierName(startState)))))))) .NormalizeWhitespace(); machineMembers.Add(startStateProperty); //Add getters/setters for machine variables: int ind = 0; foreach (var pair in machineInfo.localVariableToVarInfo) { SyntaxTokenList modifiers = new SyntaxTokenList(); modifiers = modifiers.Add(SyntaxFactory.Token(SyntaxKind.PublicKeyword)); var getBody = SyntaxFactory.SingletonList <StatementSyntax>(SyntaxFactory.ReturnStatement( (ExpressionSyntax)CSharpHelper.MkCSharpElementAccessExpression( SyntaxFactory.IdentifierName("fields"), ind))); var setBody = SyntaxFactory.SingletonList <StatementSyntax>((StatementSyntax)CSharpHelper.MkCSharpSimpleAssignmentExpressionStatement( (ExpressionSyntax)CSharpHelper.MkCSharpElementAccessExpression( SyntaxFactory.IdentifierName("fields"), ind), SyntaxFactory.IdentifierName("value"))); AccessorDeclarationSyntax[] accessorList = new AccessorDeclarationSyntax[] { CSharpHelper.MkCSharpAccessor("get", getBody), CSharpHelper.MkCSharpAccessor("set", setBody) }; machineMembers.Add(CSharpHelper.MkCSharpPropertyDecl("PrtValue", VarName(pair.Key), modifiers, accessorList)); ind += 1; } var skeletonMethodBody = generator.ReturnStatement(generator.ObjectCreationExpression(generator.IdentifierName(machineName))); var skeletonMethodDecl = generator.MethodDeclaration("MakeSkeleton", null, null, generator.IdentifierName(machineInfo.IsReal ? "PrtImplMachine" : "PrtSpecMachine"), Accessibility.Public, DeclarationModifiers.Override, new SyntaxNode[] { skeletonMethodBody }); machineMembers.Add(skeletonMethodDecl); //(this.GetType()) var returnMethodPars = new SyntaxNode[] { generator.InvocationExpression(generator.MemberAccessExpression(generator.ThisExpression(), generator.IdentifierName("GetType"))) }; //return app.NextMachineInstanceNumber(this.GetType()); var nextInstanceNumberMethodBody = generator.ReturnStatement(generator.InvocationExpression(generator.MemberAccessExpression( generator.IdentifierName("app"), generator.IdentifierName("NextMachineInstanceNumber")), returnMethodPars)); //(StateImpl app) if (machineInfo.IsReal) { var methodPars = new SyntaxNode[] { generator.ParameterDeclaration("app", generator.IdentifierName("StateImpl")) }; var nextInstanceNumberMethodDecl = generator.MethodDeclaration("NextInstanceNumber", methodPars, //null, generator.IdentifierName("int"), null, generator.TypeExpression(SpecialType.System_Int32), Accessibility.Public, DeclarationModifiers.Override, new SyntaxNode[] { nextInstanceNumberMethodBody }); machineMembers.Add(nextInstanceNumberMethodDecl); } //Name property (getter only): var nameProperty = SyntaxFactory.PropertyDeclaration( SyntaxFactory.PredefinedType( SyntaxFactory.Token(SyntaxKind.StringKeyword)), SyntaxFactory.Identifier("Name")) .WithModifiers( SyntaxFactory.TokenList( new[] { SyntaxFactory.Token(SyntaxKind.PublicKeyword), SyntaxFactory.Token(SyntaxKind.OverrideKeyword) })) .WithAccessorList( SyntaxFactory.AccessorList( SyntaxFactory.SingletonList <AccessorDeclarationSyntax>( SyntaxFactory.AccessorDeclaration( SyntaxKind.GetAccessorDeclaration, SyntaxFactory.Block( SyntaxFactory.SingletonList <StatementSyntax>( SyntaxFactory.ReturnStatement( SyntaxFactory.IdentifierName("\"" + machineName + "\"")))))))) .NormalizeWhitespace(); machineMembers.Add(nameProperty); //constructor for cloning var constructor_1 = generator.ConstructorDeclaration(machineName, null, Accessibility.Public, baseConstructorArguments: new SyntaxNode[0]); machineMembers.Add(constructor_1); //Machine class constructor //public PONG(StateImpl app, int maxB, bool assume): base (app, maxB, assume) List <SyntaxNode> constructorStmtList = new List <SyntaxNode>(); foreach (var varInfo in machineInfo.localVariableToVarInfo.Values) { var defaultValue = CSharpHelper.MkCSharpInvocationExpression(SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName("PrtValue"), SyntaxFactory.IdentifierName("PrtMkDefaultValue")), translator.typeContext.PTypeToCSharpExpr(varInfo.type)); constructorStmtList.Add(SyntaxFactory.ExpressionStatement(CSharpHelper.MkCSharpInvocationExpression(CSharpHelper.MkCSharpDot("fields", "Add"), defaultValue))); } if (machineInfo.IsReal) { var constructorPars = new SyntaxNode[] { generator.ParameterDeclaration("app", generator.IdentifierName("StateImpl")), generator.ParameterDeclaration("maxB", generator.TypeExpression(SpecialType.System_Int32)), generator.ParameterDeclaration("assume", generator.TypeExpression(SpecialType.System_Boolean)) }; var baseConstructorArgs = new SyntaxNode[] { generator.IdentifierName("app"), generator.IdentifierName("maxB"), generator.IdentifierName("assume") }; var constructor_2 = generator.ConstructorDeclaration(machineName, constructorPars, Accessibility.Public, baseConstructorArguments: baseConstructorArgs, statements: constructorStmtList); machineMembers.Add(constructor_2); } else { var constructorPars = new SyntaxNode[] { generator.ParameterDeclaration("app", generator.IdentifierName("StateImpl")) }; var baseConstructorArgs = new SyntaxNode[] { generator.IdentifierName("app") }; var constructor_2 = generator.ConstructorDeclaration(machineName, constructorPars, Accessibility.Public, baseConstructorArguments: baseConstructorArgs, statements: constructorStmtList); machineMembers.Add(constructor_2); } // Fun classes for all functions of the machine: foreach (var pair in machineInfo.funNameToFunInfo) { MkFunctionDecl funDecl = new MkFunctionDecl(pair.Key, pair.Value, this, translator); funDecl.AddFunClass(); } foreach (var pair in machineInfo.stateNameToStateInfo) { string stateName = pair.Key; string stateType = stateName + "_Class"; //state instantiation for the machine class constructor: //Example: //PONG_Pong_WaitPing = new PONG_Pong_WaitPing_Class("PONG_Pong_WaitPing", AnonFun1, AnonFun0, false, StateTemperature.Warm); //Consider 6 cases (write a mmethod): for each bool value of hasNullTransition, there are 3 possible "temperatures" mainConstructorFields.Add(MkStateInstantiation(stateName, stateType, pair.Value.entryActionName, pair.Value.exitFunName, pair.Value.hasNullTransition, pair.Value.temperature)); } //State classes for all states of the machine: foreach (var pair in machineInfo.stateNameToStateInfo) { string stateName = pair.Key; string stateType = stateName + "_Class"; //state class declaration: var stateClassDecl = SyntaxFactory.ClassDeclaration(stateType) .WithModifiers( SyntaxFactory.TokenList( SyntaxFactory.Token(SyntaxKind.PublicKeyword))) .WithBaseList( SyntaxFactory.BaseList( SyntaxFactory.SingletonSeparatedList <BaseTypeSyntax>( SyntaxFactory.SimpleBaseType( SyntaxFactory.IdentifierName("PrtState"))))) .WithMembers( SyntaxFactory.SingletonList <MemberDeclarationSyntax>( SyntaxFactory.ConstructorDeclaration( SyntaxFactory.Identifier(stateType)) .WithModifiers( SyntaxFactory.TokenList( SyntaxFactory.Token(SyntaxKind.PublicKeyword))) .WithParameterList( SyntaxFactory.ParameterList( SyntaxFactory.SeparatedList <ParameterSyntax>( new SyntaxNodeOrToken[] { SyntaxFactory.Parameter( SyntaxFactory.Identifier("name")) .WithType( SyntaxFactory.PredefinedType( SyntaxFactory.Token(SyntaxKind.StringKeyword))), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.Parameter( SyntaxFactory.Identifier("entryFun")) .WithType( SyntaxFactory.IdentifierName("PrtFun")), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.Parameter( SyntaxFactory.Identifier("exitFun")) .WithType( SyntaxFactory.IdentifierName("PrtFun")), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.Parameter( SyntaxFactory.Identifier("hasNullTransition")) .WithType( SyntaxFactory.PredefinedType( SyntaxFactory.Token(SyntaxKind.BoolKeyword))), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.Parameter( SyntaxFactory.Identifier("temperature")) .WithType( SyntaxFactory.IdentifierName("StateTemperature")) }))) .WithInitializer( SyntaxFactory.ConstructorInitializer( SyntaxKind.BaseConstructorInitializer, SyntaxFactory.ArgumentList( SyntaxFactory.SeparatedList <ArgumentSyntax>( new SyntaxNodeOrToken[] { SyntaxFactory.Argument( SyntaxFactory.IdentifierName("name")), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.Argument( SyntaxFactory.IdentifierName("entryFun")), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.Argument( SyntaxFactory.IdentifierName("exitFun")), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.Argument( SyntaxFactory.IdentifierName("hasNullTransition")), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.Argument( SyntaxFactory.IdentifierName("temperature")) })))) .WithBody( SyntaxFactory.Block()))) .NormalizeWhitespace(); machineMembers.Add(stateClassDecl); //state declaration: //Example: public static Init Init_State; var stateDeclaration = SyntaxFactory.FieldDeclaration( SyntaxFactory.VariableDeclaration( SyntaxFactory.IdentifierName(stateType)) .WithVariables( SyntaxFactory.SingletonSeparatedList <VariableDeclaratorSyntax>( SyntaxFactory.VariableDeclarator( SyntaxFactory.Identifier(stateName))))) .WithModifiers( SyntaxFactory.TokenList( new[] { SyntaxFactory.Token(SyntaxKind.PublicKeyword), SyntaxFactory.Token(SyntaxKind.StaticKeyword) })) .NormalizeWhitespace(); machineMembers.Add(stateDeclaration); //Add DoDecls to the StateInfo: foreach (var doFun in pair.Value.dos) { ExpressionSyntax doFunExpr = SyntaxFactory.IdentifierName(doFun.Value); if (doFun.Value == "ignore") { doFunExpr = SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName("PrtFun"), SyntaxFactory.IdentifierName("IgnoreFun")); } mainConstructorFields.Add( SyntaxFactory.ExpressionStatement( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(stateName), SyntaxFactory.IdentifierName("dos")), SyntaxFactory.IdentifierName("Add"))) .WithArgumentList( SyntaxFactory.ArgumentList( SyntaxFactory.SeparatedList <ArgumentSyntax>( new SyntaxNodeOrToken[] { SyntaxFactory.Argument( translator.GetEventVar(doFun.Key)), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.Argument(doFunExpr) })))) .NormalizeWhitespace() ); } //Add deferred events (if any) to the state: foreach (var deferredEvent in pair.Value.deferredEvents) { mainConstructorFields.Add( SyntaxFactory.ExpressionStatement( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(stateName), SyntaxFactory.IdentifierName("deferredSet")), SyntaxFactory.IdentifierName("Add"))) .WithArgumentList( SyntaxFactory.ArgumentList( SyntaxFactory.SingletonSeparatedList <ArgumentSyntax>( SyntaxFactory.Argument( translator.GetEventVar(deferredEvent)))))) .NormalizeWhitespace() ); } //Transition instantiations for the state of the machine: //"transition" + "_" + transition_count foreach (var transition in pair.Value.transitions) { string trigger = transition.Key; string transition_name = "transition" + "_" + transition_count; if (transition.Value.IsPush) { //push transition: mainConstructorFields.Add( SyntaxFactory.LocalDeclarationStatement( SyntaxFactory.VariableDeclaration( SyntaxFactory.IdentifierName("PrtTransition")) .WithVariables( SyntaxFactory.SingletonSeparatedList <VariableDeclaratorSyntax>( SyntaxFactory.VariableDeclarator( SyntaxFactory.Identifier(transition_name)) .WithInitializer( SyntaxFactory.EqualsValueClause( SyntaxFactory.ObjectCreationExpression( SyntaxFactory.IdentifierName("PrtTransition")) .WithArgumentList( SyntaxFactory.ArgumentList( SyntaxFactory.SeparatedList <ArgumentSyntax>( new SyntaxNodeOrToken[] { SyntaxFactory.Argument( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName("PrtFun"), SyntaxFactory.IdentifierName("IgnoreFun"))), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.Argument( SyntaxFactory.IdentifierName(transition.Value.target)), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.Argument( SyntaxFactory.LiteralExpression( SyntaxKind.TrueLiteralExpression)) })))))))) .NormalizeWhitespace()); } else { //goto transition: mainConstructorFields.Add( SyntaxFactory.LocalDeclarationStatement( SyntaxFactory.VariableDeclaration( SyntaxFactory.IdentifierName("PrtTransition")) .WithVariables( SyntaxFactory.SingletonSeparatedList <VariableDeclaratorSyntax>( SyntaxFactory.VariableDeclarator( SyntaxFactory.Identifier("transition" + "_" + transition_count)) .WithInitializer( SyntaxFactory.EqualsValueClause( SyntaxFactory.ObjectCreationExpression( SyntaxFactory.IdentifierName("PrtTransition")) .WithArgumentList( SyntaxFactory.ArgumentList( SyntaxFactory.SeparatedList <ArgumentSyntax>( new SyntaxNodeOrToken[] { SyntaxFactory.Argument( SyntaxFactory.IdentifierName(transition.Value.transFunName)), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.Argument( SyntaxFactory.IdentifierName(transition.Value.target)), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.Argument( SyntaxFactory.LiteralExpression( SyntaxKind.FalseLiteralExpression)) })))))))) .NormalizeWhitespace()); } //Add transition to the state: mainConstructorFields.Add( SyntaxFactory.ExpressionStatement( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(stateName), SyntaxFactory.IdentifierName("transitions")), SyntaxFactory.IdentifierName("Add"))) .WithArgumentList( SyntaxFactory.ArgumentList( SyntaxFactory.SeparatedList <ArgumentSyntax>( new SyntaxNodeOrToken[] { SyntaxFactory.Argument( translator.GetEventVar(trigger)), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.Argument( SyntaxFactory.IdentifierName(transition_name)) })))) .NormalizeWhitespace() ); transition_count += 1; } } //Constructor for the machine class: //static Machine() var mainConstructor = SyntaxFactory.ConstructorDeclaration( SyntaxFactory.Identifier(machineName)) .WithModifiers( SyntaxFactory.TokenList( SyntaxFactory.Token(SyntaxKind.StaticKeyword))) .WithBody( SyntaxFactory.Block(mainConstructorFields )) .NormalizeWhitespace(); machineMembers.Add(mainConstructor); var mainMachineClassDecl = generator.ClassDeclaration( machineName, typeParameters: null, accessibility: Accessibility.Public, baseType: generator.IdentifierName(machineInfo.IsReal ? "PrtImplMachine" : "PrtSpecMachine"), members: machineMembers); return(mainMachineClassDecl); }