public override object VisitOnEventPushState(PParser.OnEventPushStateContext context)
        {
            //annotationSet?
            if (context.annotationSet() != null)
            {
                throw new NotImplementedException("push state annotations");
            }

            // PUSH stateName
            var targetState = FindState(context.stateName());

            // ON eventList
            var actions = new List <IStateAction>();

            foreach (var token in context.eventList().eventId())
            {
                if (!CurrentScope.Lookup(token.GetText(), out PEvent evt))
                {
                    throw Handler.MissingDeclaration(token, "event", token.GetText());
                }

                actions.Add(new EventPushState(token, evt, targetState));
            }
            return(actions.ToArray());
        }
Exemple #2
0
        public override object VisitForeignFunDecl(PParser.ForeignFunDeclContext context)
        {
            // FUN name=Iden
            var fun = (Function)nodesToDeclarations.Get(context);

            // LPAREN funParamList? RPAREN
            var paramList = context.funParamList() != null
                ? (Variable[])Visit(context.funParamList())
                : new Variable[0];

            fun.Signature.Parameters.AddRange(paramList);

            // (COLON type)?
            fun.Signature.ReturnType = ResolveType(context.type());

            // SEMI
            // no function body
            fun.Role |= FunctionRole.Foreign;

            // Creates
            foreach (var createdInterface in context._interfaces)
            {
                if (CurrentScope.Lookup(createdInterface.GetText(), out Interface @interface))
                {
                    fun.AddCreatesInterface(@interface);
                }
                else
                {
                    throw Handler.MissingDeclaration(createdInterface, "interface", createdInterface.GetText());
                }
            }
            return(fun);
        }
        public override object VisitNonDefaultEvent(PParser.NonDefaultEventContext context)
        {
            // HALT | iden
            var eventName = context.GetText();

            if (!CurrentScope.Lookup(eventName, out PEvent pEvent))
            {
                throw Handler.MissingDeclaration(context, "event", eventName);
            }
            return(pEvent);
        }
        public override object VisitOnEventGotoState(PParser.OnEventGotoStateContext context)
        {
            // annotationSet?
            if (context.annotationSet() != null)
            {
                throw new NotImplementedException("state transition annotations");
            }

            Function transitionFunction;

            if (context.funName != null)
            {
                // WITH funName=Iden
                var funName = context.funName.GetText();
                if (!CurrentScope.Lookup(funName, out transitionFunction))
                {
                    throw Handler.MissingDeclaration(context.funName, "function", funName);
                }
                transitionFunction.Role |= FunctionRole.TransitionFunction;
            }
            else if (context.anonEventHandler() != null)
            {
                // WITH anonEventHandler
                transitionFunction       = CreateAnonFunction(context.anonEventHandler());
                transitionFunction.Role |= FunctionRole.TransitionFunction;
            }
            else
            {
                // SEMI
                transitionFunction = null;
            }

            // GOTO stateName
            var target = FindState(context.stateName());

            // ON eventList
            var actions = new List <IStateAction>();

            foreach (var eventIdContext in context.eventList().eventId())
            {
                if (!CurrentScope.Lookup(eventIdContext.GetText(), out PEvent evt))
                {
                    throw Handler.MissingDeclaration(eventIdContext, "event", eventIdContext.GetText());
                }

                actions.Add(new EventGotoState(eventIdContext, evt, target, transitionFunction));
            }
            return(actions.ToArray());
        }
Exemple #5
0
        public override object VisitStateIgnore(PParser.StateIgnoreContext context)
        {
            // IGNORE nonDefaultEventList
            var actions = new List <IStateAction>();

            foreach (var token in context.nonDefaultEventList()._events)
            {
                if (!CurrentScope.Lookup(token.GetText(), out PEvent evt))
                {
                    throw Handler.MissingDeclaration(token, "event", token.GetText());
                }
                actions.Add(new EventIgnore(token, evt));
            }

            return(actions.ToArray());
        }
        public override object VisitStateExit(PParser.StateExitContext context)
        {
            Function fun;

            if (context.noParamAnonEventHandler() != null)
            {
                fun = CreateAnonFunction(context.noParamAnonEventHandler());
            }
            else
            {
                var funName = context.funName.GetText();
                if (!CurrentScope.Lookup(funName, out fun))
                {
                    throw Handler.MissingDeclaration(context.funName, "function", funName);
                }
            }
            fun.Role |= FunctionRole.ExitHandler;
            return(Tuple.Create("EXIT", fun));
        }
Exemple #7
0
        public override object VisitOnEventPushState(PParser.OnEventPushStateContext context)
        {
            // PUSH stateName
            State targetState = FindState(context.stateName());

            // ON eventList
            List <IStateAction> actions = new List <IStateAction>();

            foreach (PParser.EventIdContext token in context.eventList().eventId())
            {
                if (!CurrentScope.Lookup(token.GetText(), out PEvent evt))
                {
                    throw Handler.MissingDeclaration(token, "event", token.GetText());
                }

                actions.Add(new EventPushState(token, evt, targetState));
            }

            return(actions.ToArray());
        }
        public override object VisitOnEventDoAction(PParser.OnEventDoActionContext context)
        {
            // annotationSet?
            if (context.annotationSet() != null)
            {
                throw new NotImplementedException("state action annotations");
            }

            Function fun;

            if (context.anonEventHandler() is PParser.AnonEventHandlerContext anonEventHandler)
            {
                // DO [...] anonEventHandler
                fun = CreateAnonFunction(anonEventHandler);
            }
            else
            {
                // DO funName=Iden
                var funName = context.funName.GetText();
                if (!CurrentScope.Lookup(funName, out fun))
                {
                    throw Handler.MissingDeclaration(context.funName, "function", funName);
                }
            }
            // TODO: is this correct?
            fun.Role |= FunctionRole.EventHandler;

            // ON eventList
            var actions = new List <IStateAction>();

            foreach (var eventIdContext in context.eventList().eventId())
            {
                if (!CurrentScope.Lookup(eventIdContext.GetText(), out PEvent evt))
                {
                    throw Handler.MissingDeclaration(eventIdContext, "event", eventIdContext.GetText());
                }
                actions.Add(new EventDoAction(eventIdContext, evt, fun));
            }
            return(actions.ToArray());
        }
        public override object VisitStateDefer(PParser.StateDeferContext context)
        {
            // annotationSet? SEMI
            if (context.annotationSet() != null)
            {
                throw new NotImplementedException("event defer annotations");
            }

            // DEFER nonDefaultEventList
            var eventContexts = context.nonDefaultEventList()._events;
            var actions       = new IStateAction[eventContexts.Count];

            for (var i = 0; i < eventContexts.Count; i++)
            {
                var token = eventContexts[i];
                if (!CurrentScope.Lookup(token.GetText(), out PEvent evt))
                {
                    throw Handler.MissingDeclaration(token, "event", token.GetText());
                }
                actions[i] = new EventDefer(token, evt);
            }
            return(actions);
        }
Exemple #10
0
        public override object VisitStateDefer(PParser.StateDeferContext context)
        {
            if (CurrentMachine.IsSpec)
            {
                throw Handler.DeferredEventInMonitor(context, CurrentMachine);
            }

            // DEFER nonDefaultEventList
            var eventContexts = context.nonDefaultEventList()._events;
            var actions       = new IStateAction[eventContexts.Count];

            for (var i = 0; i < eventContexts.Count; i++)
            {
                var token = eventContexts[i];
                if (!CurrentScope.Lookup(token.GetText(), out PEvent evt))
                {
                    throw Handler.MissingDeclaration(token, "event", token.GetText());
                }
                actions[i] = new EventDefer(token, evt);
            }

            return(actions);
        }
        public override object VisitImplMachineDecl(PParser.ImplMachineDeclContext context)
        {
            // MACHINE name=iden
            var machine = (Machine)nodesToDeclarations.Get(context);

            // cardinality?
            var hasAssume   = context.cardinality()?.ASSUME() != null;
            var hasAssert   = context.cardinality()?.ASSERT() != null;
            var cardinality = int.Parse(context.cardinality()?.IntLiteral().GetText() ?? "-1");

            machine.Assume = hasAssume ? cardinality : -1;
            machine.Assert = hasAssert ? cardinality : -1;

            // annotationSet?
            if (context.annotationSet() != null)
            {
                throw new NotImplementedException("impl machine annotations");
            }

            // (COLON idenList)?
            if (context.idenList() is PParser.IdenListContext interfaces)
            {
                foreach (var pInterfaceNameCtx in interfaces._names)
                {
                    var pInterfaceName = pInterfaceNameCtx.GetText();
                    if (!CurrentScope.Lookup(pInterfaceName, out Interface pInterface))
                    {
                        throw Handler.MissingDeclaration(pInterfaceNameCtx, "interface", pInterfaceName);
                    }

                    machine.AddInterface(pInterface);
                }
            }

            // receivesSends*
            foreach (var receivesSends in context.receivesSends())
            {
                var recvSendTuple = (Tuple <string, PEvent[]>)Visit(receivesSends);
                var eventSetType  = recvSendTuple.Item1;
                if (eventSetType.Equals("RECV", StringComparison.InvariantCulture))
                {
                    if (machine.Receives == null)
                    {
                        machine.Receives = new EventSet();
                    }
                    foreach (PEvent @event in recvSendTuple.Item2)
                    {
                        machine.Receives.AddEvent(@event);
                    }
                }
                else if (eventSetType.Equals("SEND", StringComparison.InvariantCulture))
                {
                    if (machine.Sends == null)
                    {
                        machine.Sends = new EventSet();
                    }
                    foreach (PEvent @event in recvSendTuple.Item2)
                    {
                        machine.Sends.AddEvent(@event);
                    }
                }
                else
                {
                    throw Handler.InternalError(receivesSends, "grammar changed surrounding receives/sends.");
                }
            }

            // machineBody
            using (currentScope.NewContext(machine.Scope))
                using (currentMachine.NewContext(machine))
                {
                    Visit(context.machineBody());
                }

            return(machine);
        }