Example #1
0
        /// <summary>
        /// Crea la declaracio del metode 'start'
        /// </summary>
        /// <param name="machine">La maquina.</param>
        /// <returns>La declaracio del metode.</returns>
        ///
        private FunctionDeclaration MakeStartFunction(Machine machine)
        {
            StatementList bodyStatements = new StatementList();

            if (machine.InitializeAction != null)
            {
                var statements = MakeActionStatements(machine.InitializeAction);
                if (statements != null)
                {
                    bodyStatements.AddRange(statements);
                }
            }
            bodyStatements.Add(
                new InvokeStatement(
                    new InvokeExpression(
                        new IdentifierExpression("initialize"),
                        new InvokeExpression(
                            new IdentifierExpression("getStateInstance"),
                            new IdentifierExpression(
                                String.Format("StateID::{0}", machine.Start.FullName))))));

            return(new FunctionDeclaration(
                       "start",
                       AccessSpecifier.Public,
                       TypeIdentifier.FromName("void"),
                       null,
                       bodyStatements));
        }
Example #2
0
        /// <summary>
        /// Construeix la funcio de transicio.
        /// </summary>
        /// <param name="state">El estat.</param>
        /// <param name="transitionName">El nom de la transicio.</param>
        /// <returns>La funcio.</returns>
        ///
        private FunctionDeclaration MakeOnTransitionFunction(State state, string transitionName, string contextClassName, string ownerClassName)
        {
            StatementList bodyStatements = new StatementList();

            // Intruccio per recuperar el context.
            //
            bodyStatements.Add(
                new InlineStatement(
                    String.Format("{0}* ctx = static_cast<{0}*>(getContext())", contextClassName)));
            bodyStatements.Add(
                new InlineStatement(
                    String.Format("{0}* owner = ctx->getOwner()", ownerClassName)));

            foreach (Transition transition in state.Transitions)
            {
                if (transition.TransitionEvent.Name == transitionName)
                {
                    StatementList trueBodyStatements = new StatementList();

                    trueBodyStatements.Add(new InvokeStatement(
                                               new InvokeExpression(
                                                   new IdentifierExpression("ctx->beginTransition"))));

                    // Accio de transicio.
                    //
                    if (transition.Action != null)
                    {
                        trueBodyStatements.AddRange(MakeActionStatements(transition.Action));
                    }

                    trueBodyStatements.Add(new InvokeStatement(
                                               new InvokeExpression(
                                                   new IdentifierExpression("ctx->endTransition"),
                                                   new InvokeExpression(
                                                       new IdentifierExpression("ctx->getStateInstance"),
                                                       new IdentifierExpression(
                                                           String.Format("Context::StateID::{0}", transition.NextState.Name))))));

                    Expression conditionExpr = new InlineExpression(transition.Guard == null ? "true" : transition.Guard.Expression);
                    bodyStatements.Add(new IfThenElseStatement(
                                           conditionExpr,
                                           new BlockStatement {
                        Statements = trueBodyStatements
                    },
                                           null));
                }
            }

            return(new FunctionDeclaration {
                Access = AccessSpecifier.Public,
                Implementation = ImplementationSpecifier.Override,
                ReturnType = TypeIdentifier.FromName("void"),
                Name = String.Format("transition_{0}", transitionName),
                Body = new BlockStatement(bodyStatements),
            });
        }