コード例 #1
0
        public UnitDeclaration Generate(Machine machine, Variant variant)
        {
            UnitBuilder ub = new UnitBuilder();

            TypeIdentifier voidTypeIdentifier       = TypeIdentifier.FromName("void");
            TypeIdentifier contextTypePtrIdentifier = TypeIdentifier.FromName(String.Format("{0}*", contextClassName));

            // Obra un espai de noms si cal.
            //
            if (!String.IsNullOrEmpty(nsName))
            {
                ub.BeginNamespace(nsName);
            }

            // Declara la clase de context
            //
            if (variant == Variant.Header)
            {
                ub.AddForwardClassDeclaration(contextClassName);
            }

            // Declara la clase d'estat base
            //
            ub.BeginClass(stateClassName, stateBaseClassName, AccessSpecifier.Public);

            // Declara el constructor
            //
            ub.AddMemberDeclaration(new ConstructorDeclaration {
                Access    = AccessSpecifier.Protected,
                Arguments = new ArgumentDeclarationList {
                    new ArgumentDeclaration("context", contextTypePtrIdentifier)
                },
                Initializers = new ConstructorInitializerList {
                    new ConstructorInitializer(
                        stateBaseClassName,
                        new IdentifierExpression("context"))
                }
            });

            // Declara la functio 'enter'
            //
            ub.AddMemberDeclaration(new FunctionDeclaration {
                Access         = AccessSpecifier.Public,
                Implementation = ImplementationSpecifier.Virtual,
                ReturnType     = voidTypeIdentifier,
                Name           = "enter"
            });

            // Declara la fuincio 'exit'
            //
            ub.AddMemberDeclaration(new FunctionDeclaration {
                Access         = AccessSpecifier.Public,
                Implementation = ImplementationSpecifier.Virtual,
                ReturnType     = voidTypeIdentifier,
                Name           = "exit"
            });

            foreach (var transitionName in machine.GetTransitionNames())
            {
                ub.AddMemberDeclaration(new FunctionDeclaration {
                    Access         = AccessSpecifier.Public,
                    Implementation = ImplementationSpecifier.Virtual,
                    ReturnType     = voidTypeIdentifier,
                    Name           = String.Format("transition_{0}", transitionName)
                });
            }

            // Finalitza la clase
            //
            ub.EndClass();

            // Crea les clases d'estat derivades
            //
            foreach (var state in machine.States)
            {
                ub.BeginClass(String.Format("{0}", state.Name), stateClassName, AccessSpecifier.Public);

                // Declara el constructor.
                //
                ub.AddMemberDeclaration(new ConstructorDeclaration {
                    Access    = AccessSpecifier.Public,
                    Arguments = new ArgumentDeclarationList {
                        new ArgumentDeclaration("context", contextTypePtrIdentifier)
                    },
                    Initializers = new ConstructorInitializerList {
                        new ConstructorInitializer(
                            stateClassName,
                            new IdentifierExpression("context"))
                    }
                });

                // Declara la fuincio 'enter'.
                //
                if (state.EnterAction != null)
                {
                    ub.AddMemberDeclaration(MakeOnEnterFunction(state, contextClassName, ownerClassName));
                }

                // Declara la funcio 'exit'
                //
                if (state.ExitAction != null)
                {
                    ub.AddMemberDeclaration(MakeOnExitFunction(state, contextClassName, ownerClassName));
                }

                // Declara les funcions de transicio
                //
                foreach (var transitionName in state.GetTransitionNames())
                {
                    ub.AddMemberDeclaration(MakeOnTransitionFunction(state, transitionName, contextClassName, ownerClassName));
                }

                ub.EndClass();
            }

            return(ub.ToUnit());
        }
コード例 #2
0
        /// <summary>
        /// Genera la unitat de compilacio.
        /// </summary>
        /// <param name="machine">La maquina</param>
        /// <returns>La unitat de compilacio.</returns>
        ///
        public UnitDeclaration Generate(Machine machine)
        {
            UnitBuilder ub = new UnitBuilder();

            bool useNamespace = !String.IsNullOrEmpty(nsName);

            // Obre un nou espai de noms, si cal
            //
            if (useNamespace)
            {
                ub.BeginNamespace(nsName);
            }

            ub.AddForwardClassDeclaration(stateClassName);
            ub.AddForwardClassDeclaration(ownerClassName);

            // Obra la declaracio de clase 'Context'
            //
            ub.BeginClass(contextClassName, contextBaseClassName, AccessSpecifier.Public);

            // Declara un enumerador pels estats
            //
            List <string> elements = new List <string>(machine.GetStateNames());

            ub.AddMemberDeclaration(new EnumerationDeclaration {
                Name     = "StateID",
                Elements = elements
            });

            // Declara la variable per les instancies dels estats.
            //
            ub.AddMemberDeclaration(new VariableDeclaration {
                Name      = String.Format("states[{0}]", elements.Count),
                ValueType = TypeIdentifier.FromName(String.Format("{0}*", stateClassName))
            });

            // Afegeix la variable pel punter al objecte propietari
            //
            ub.AddMemberDeclaration(MakeOwnerVariable(machine));

            // Afegeix el constructor
            //
            ub.AddMemberDeclaration(MakeConstructor(machine));

            // Afegeix les funcions membre
            //
            ub.AddMemberDeclaration(MakeGetStateInstanceFunction(machine));
            ub.AddMemberDeclaration(MakeGetOwnerFunction(machine));
            ub.AddMemberDeclaration(MakeStartFunction(machine));
            ub.AddMemberDeclaration(MakeEndFunction(machine));
            foreach (var transitionName in machine.GetTransitionNames())
            {
                ub.AddMemberDeclaration(MakeTransitionFunction(transitionName));
            }

            // Tanca la declaracio de la clase 'Context'
            //
            ub.EndClass();

            // Tanca la declaracio del espai de noms, si cal.
            //
            if (useNamespace)
            {
                ub.EndNamespace();
            }

            // Retorna l'unitat.
            //
            return(ub.ToUnit());
        }