Ejemplo n.º 1
0
        public override IPStmt VisitSendStmt(PParser.SendStmtContext context)
        {
            IPExpr machineExpr = exprVisitor.Visit(context.machine);

            if (!PrimitiveType.Machine.IsAssignableFrom(machineExpr.Type))
            {
                throw handler.TypeMismatch(context.machine, machineExpr.Type, PrimitiveType.Machine);
            }

            IPExpr evtExpr = exprVisitor.Visit(context.@event);

            if (IsDefinitelyNullEvent(evtExpr))
            {
                throw handler.EmittedNullEvent(evtExpr);
            }

            if (!PrimitiveType.Event.IsAssignableFrom(evtExpr.Type))
            {
                throw handler.TypeMismatch(context.@event, evtExpr.Type, PrimitiveType.Event);
            }

            IPExpr[] args = (context.rvalueList()?.rvalue().Select(rv => exprVisitor.Visit(rv)) ??
                             Enumerable.Empty <IPExpr>()).ToArray();

            if (evtExpr is EventRefExpr eventRef)
            {
                TypeCheckingUtils.ValidatePayloadTypes(handler, context, eventRef.PEvent.PayloadType, args);
            }

            return(new SendStmt(context, machineExpr, evtExpr, args));
        }
Ejemplo n.º 2
0
        public override IPStmt VisitRaiseStmt(PParser.RaiseStmtContext context)
        {
            IPExpr evtExpr = exprVisitor.Visit(context.expr());

            if (IsDefinitelyNullEvent(evtExpr))
            {
                throw handler.EmittedNullEvent(evtExpr);
            }

            if (!PrimitiveType.Event.IsAssignableFrom(evtExpr.Type))
            {
                throw handler.TypeMismatch(context.expr(), evtExpr.Type, PrimitiveType.Event);
            }

            method.CanCommunicate = true;
            method.CanChangeState = true;

            IPExpr[] args = (context.rvalueList()?.rvalue().Select(rv => exprVisitor.Visit(rv)) ??
                             Enumerable.Empty <IPExpr>()).ToArray();

            if (evtExpr is EventRefExpr eventRef)
            {
                TypeCheckingUtils.ValidatePayloadTypes(handler, context, eventRef.PEvent.PayloadType, args);
            }

            return(new RaiseStmt(context, evtExpr, args));
        }
Ejemplo n.º 3
0
        public override IPStmt VisitFunCallStmt(PParser.FunCallStmtContext context)
        {
            string funName            = context.fun.GetText();
            IEnumerable <IPExpr> args = context.rvalueList()?.rvalue().Select(rv => exprVisitor.Visit(rv)) ??
                                        Enumerable.Empty <IPExpr>();
            List <IPExpr> argsList = args.ToList();

            if (!table.Lookup(funName, out Function fun))
            {
                throw handler.MissingDeclaration(context.fun, "function or function prototype", funName);
            }

            if (fun.Signature.Parameters.Count != argsList.Count)
            {
                throw handler.IncorrectArgumentCount((ParserRuleContext)context.rvalueList() ?? context,
                                                     argsList.Count,
                                                     fun.Signature.Parameters.Count);
            }

            foreach (Tuple <Variable, IPExpr> pair in fun.Signature.Parameters.Zip(argsList, Tuple.Create))
            {
                TypeCheckingUtils.CheckArgument(handler, context, pair.Item1.Type, pair.Item2);
            }

            method.AddCallee(fun);
            return(new FunCallStmt(context, fun, argsList));
        }
Ejemplo n.º 4
0
        public override IPStmt VisitCtorStmt(PParser.CtorStmtContext context)
        {
            string machineName = context.iden().GetText();

            if (!table.Lookup(machineName, out Machine targetMachine))
            {
                throw handler.MissingDeclaration(context.iden(), "machine", machineName);
            }
            var args = (from arg in context.rvalueList()?.rvalue() ?? Enumerable.Empty <PParser.RvalueContext>() select exprVisitor.Visit(arg)).ToList();

            TypeCheckingUtils.ValidatePayloadTypes(handler, context, targetMachine.PayloadType, args);
            return(new CtorStmt(context, targetMachine, args));
        }
Ejemplo n.º 5
0
        public override IPExpr VisitCtorExpr(PParser.CtorExprContext context)
        {
            string machineName = context.machineName.GetText();

            if (!table.Lookup(machineName, out Machine machine))
            {
                throw handler.MissingDeclaration(context.machineName, "machine", machineName);
            }

            IPExpr[] arguments = (context.rvalueList()?.rvalue().Select(Visit) ?? Enumerable.Empty <IPExpr>()).ToArray();
            TypeCheckingUtils.ValidatePayloadTypes(handler, context, machine.PayloadType, arguments);
            return(new CtorExpr(context, machine, arguments));
        }