Exemplo n.º 1
0
        private static void ValidateEventPayloadToTransitionTarget(ITranslationErrorHandler handler,
                                                                   ParserRuleContext sourceLocation,
                                                                   PLanguageType eventPayloadType,
                                                                   Function targetFunction)
        {
            IReadOnlyList <PLanguageType> entrySignature = targetFunction.Signature.ParameterTypes.ToList();

            if (entrySignature.Count == 0)
            {
                return;
            }

            if (entrySignature.Count == 1 && entrySignature[0].IsAssignableFrom(eventPayloadType))
            {
                return;
            }

            if (entrySignature.Count == 1 && eventPayloadType.Canonicalize() is TupleType tuple &&
                tuple.Types.Count == 1 && entrySignature[0].IsAssignableFrom(tuple.Types[0]))
            {
                return;
            }

            if (entrySignature.Count == 1)
            {
                throw handler.TypeMismatch(sourceLocation, eventPayloadType, entrySignature[0]);
            }

            PLanguageType entrySignatureType = new TupleType(entrySignature.ToArray());

            if (!entrySignatureType.IsAssignableFrom(eventPayloadType))
            {
                throw handler.TypeMismatch(sourceLocation, eventPayloadType, entrySignatureType);
            }
        }
Exemplo n.º 2
0
 public static void CheckArgument(ITranslationErrorHandler handler, ParserRuleContext context,
                                  PLanguageType argumentType, IPExpr arg)
 {
     if (arg is ILinearRef linearRef)
     {
         if (linearRef.LinearType.Equals(LinearType.Swap) && !arg.Type.IsSameTypeAs(argumentType))
         {
             throw handler.TypeMismatch(context, arg.Type, argumentType);
         }
     }
     if (!argumentType.IsAssignableFrom(arg.Type))
     {
         throw handler.TypeMismatch(context, arg.Type, argumentType);
     }
 }
Exemplo n.º 3
0
 public static void ValidatePayloadTypes(ITranslationErrorHandler handler, ParserRuleContext context,
                                         PLanguageType payloadType, IReadOnlyList <IPExpr> arguments)
 {
     if (arguments.Count == 0)
     {
         if (!payloadType.IsSameTypeAs(PrimitiveType.Null))
         {
             throw handler.TypeMismatch(context, PrimitiveType.Null, payloadType);
         }
     }
     else if (arguments.Count == 1)
     {
         CheckArgument(handler, context, payloadType, arguments[0]);
     }
     else if (payloadType.Canonicalize() is TupleType tuple)
     {
         foreach (Tuple <IPExpr, PLanguageType> pair in arguments.Zip(tuple.Types, Tuple.Create))
         {
             CheckArgument(handler, context, pair.Item2, pair.Item1);
         }
     }
     else
     {
         throw handler.IncorrectArgumentCount(context, arguments.Count, 1);
     }
 }
Exemplo n.º 4
0
        public static void ValidateMachine(ITranslationErrorHandler handler, Machine machine)
        {
            State         startState            = FindStartState(machine, handler);
            PLanguageType startStatePayloadType = GetStatePayload(startState, handler);

            if (!startStatePayloadType.IsSameTypeAs(machine.PayloadType))
            {
                throw handler.InternalError(machine.SourceLocation,
                                            "machine payload type is not the same as start state's entry payload type");
            }

            foreach (Interface machineInterface in machine.Interfaces)
            {
                if (!machine.PayloadType.IsAssignableFrom(machineInterface.PayloadType))
                {
                    // TODO: add special "invalid machine interface" error
                    throw handler.TypeMismatch(machine.StartState.Entry?.SourceLocation ?? machine.SourceLocation,
                                               machine.PayloadType,
                                               machineInterface.PayloadType);
                }
            }
        }
Exemplo n.º 5
0
        public override IPExpr VisitNamedTupleAccessExpr(PParser.NamedTupleAccessExprContext context)
        {
            var subExpr = Visit(context.expr());

            if (!(subExpr.Type.Canonicalize() is NamedTupleType tuple))
            {
                throw handler.TypeMismatch(subExpr, TypeKind.NamedTuple);
            }
            var fieldName = context.field.GetText();

            if (!tuple.LookupEntry(fieldName, out var entry))
            {
                throw handler.MissingNamedTupleEntry(context.field, tuple);
            }
            return(new NamedTupleAccessExpr(context, subExpr, entry));
        }
Exemplo n.º 6
0
        public override IPStmt VisitAssertStmt(PParser.AssertStmtContext context)
        {
            IPExpr assertion = exprVisitor.Visit(context.expr());

            if (!PrimitiveType.Bool.IsSameTypeAs(assertion.Type))
            {
                throw handler.TypeMismatch(context.expr(), assertion.Type, PrimitiveType.Bool);
            }
            string message = context.StringLiteral()?.GetText() ?? "";

            return(new AssertStmt(context, assertion, message));
        }
Exemplo n.º 7
0
        public override IPStmt VisitAssertStmt(PParser.AssertStmtContext context)
        {
            var assertion = exprVisitor.Visit(context.expr());

            if (!PrimitiveType.Bool.IsSameTypeAs(assertion.Type))
            {
                throw handler.TypeMismatch(context.expr(), assertion.Type, PrimitiveType.Bool);
            }

            var message = context.StringLiteral()?.GetText() ?? "";

            if (message.StartsWith("\""))
            {
                message = message.Substring(1, message.Length - 2);
            }
            return(new AssertStmt(context, assertion, message));
        }
Exemplo n.º 8
0
        public override IPStmt VisitAssertStmt(PParser.AssertStmtContext context)
        {
            IPExpr assertion = exprVisitor.Visit(context.assertion);

            if (!PrimitiveType.Bool.IsSameTypeAs(assertion.Type))
            {
                throw handler.TypeMismatch(context.assertion, assertion.Type, PrimitiveType.Bool);
            }
            IPExpr message;

            if (context.message == null)
            {
                message = new StringExpr(context, "", new List <IPExpr>());
            }
            else
            {
                message = exprVisitor.Visit(context.message);
                if (!message.Type.IsSameTypeAs(PrimitiveType.String))
                {
                    throw handler.TypeMismatch(context.message, message.Type, PrimitiveType.String);
                }
            }

            return(new AssertStmt(context, assertion, message));
        }