Пример #1
0
 public Exception MisplacedThis(PParser.PrimitiveContext location)
 {
     return(IssueError(location, location.THIS().Symbol, "keyword THIS used outside machine"));
 }
Пример #2
0
        public override IPExpr VisitPrimitive(PParser.PrimitiveContext context)
        {
            if (context.iden() != null)
            {
                var symbolName = context.iden().GetText();
                if (table.Lookup(symbolName, out Variable variable))
                {
                    return(new VariableAccessExpr(context, variable));
                }
                if (table.Lookup(symbolName, out EnumElem enumElem))
                {
                    return(new EnumElemRefExpr(context, enumElem));
                }
                if (table.Lookup(symbolName, out PEvent evt))
                {
                    return(new EventRefExpr(context, evt));
                }
                throw handler.MissingDeclaration(context.iden(), "variable, enum element, or event", symbolName);
            }

            if (context.floatLiteral() != null)
            {
                return(Visit(context.floatLiteral()));
            }
            if (context.BoolLiteral() != null)
            {
                return(new BoolLiteralExpr(context, context.BoolLiteral().GetText().Equals("true")));
            }
            if (context.IntLiteral() != null)
            {
                return(new IntLiteralExpr(context, int.Parse(context.IntLiteral().GetText())));
            }
            if (context.NullLiteral() != null)
            {
                return(new NullLiteralExpr(context));
            }
            if (context.NONDET() != null)
            {
                if (method.Owner?.IsSpec == true)
                {
                    throw handler.IllegalMonitorOperation(context, context.NONDET().Symbol, method.Owner);
                }
                method.IsNondeterministic = true;
                return(new NondetExpr(context));
            }

            if (context.FAIRNONDET() != null)
            {
                if (method.Owner?.IsSpec == true)
                {
                    throw handler.IllegalMonitorOperation(context, context.FAIRNONDET().Symbol, method.Owner);
                }
                method.IsNondeterministic = true;
                return(new FairNondetExpr(context));
            }

            if (context.HALT() != null)
            {
                var success = table.Lookup("halt", out PEvent haltEvent);
                Debug.Assert(success);
                return(new EventRefExpr(context, haltEvent));
            }

            if (context.THIS() != null)
            {
                if (method.Owner == null)
                {
                    throw handler.MisplacedThis(context);
                }
                if (method.Owner.IsSpec)
                {
                    throw handler.IllegalMonitorOperation(context, context.THIS().Symbol, method.Owner);
                }
                return(new ThisRefExpr(context, method.Owner));
            }

            throw handler.InternalError(context, new ArgumentOutOfRangeException(nameof(context), "unknown primitive"));
        }