Esempio n. 1
0
        public override object VisitUnaryExpression([NotNull] PigeonParser.UnaryExpressionContext context)
        {
            var operand = Visit(context.expr());
            var resType = analyser.Types.Get(context);

            switch (context.op.Text)
            {
            case "+":
                if (resType == PigeonType.Int)
                {
                    return((int)operand);
                }
                return((float)operand);

            case "-":
                if (resType == PigeonType.Int)
                {
                    return(-(int)operand);
                }
                return(-(float)operand);

            case "!":
                return(!(bool)operand);

            default:
                throw new InternalErrorException($"Unsupported unary operator {context.op.Text}");
            }
        }
Esempio n. 2
0
        public override void ExitUnaryExpression([NotNull] PigeonParser.UnaryExpressionContext context)
        {
            var operandType = Types.Get(context.expr());

            if (!UnaryOperator.TryGetResType(context.op.Text, operandType, out var type))
            {
                errorBag.ReportInvalidTypeUnaryOperator(context.op.GetTextSpan(), context.op.Text, type);
            }
            Types.Put(context, type);
        }