Exemple #1
0
 private static void AssignmentMissmatch(ExpressionNode left, ExpressionNode right, SequencePoint point)
 {
     ErrorCode.TypeMismatch.ReportAndThrow(point,
                                           "Cannot assign {0} to {1}", right.ExpressionReturnType, left.ExpressionReturnType);
 }
Exemple #2
0
        public static ExpressionNode Create(ContextNode context, AssignmentOperatorType op, ExpressionNode left, ExpressionNode right, SequencePoint point)
        {
            if (!left.IsSettable)
            {
                ErrorCode.NotAnLValue.ReportAndThrow(left.SequencePoint, "Left of assignment operator must be settable");
            }

            if (!right.IsGettable)
            {
                ErrorCode.NotAnRValue.ReportAndThrow(right.SequencePoint, "Right of assignment operator must be gettable");
            }

            var type = left.ExpressionReturnType;

            var overloaded = AsOverloadedMethod(context, op, left, right, point);

            if (overloaded != null)
            {
                return(overloaded);
            }

            if (op != AssignmentOperatorType.Assignment)
            {
                //only transform primitives
                if (!(context.Parser.IsPrimitive(left.ExpressionReturnType) && context.Parser.IsPrimitive(right.ExpressionReturnType)))
                {
                    AssignmentMissmatch(left, right, point);
                }

                if (!left.IsGettable)
                {
                    ErrorCode.NotAnRValue.ReportAndThrow(right.SequencePoint, "Left of this type of assignment operator must be gettable");
                }

                right = BinaryOperatorNode.Create(context, AssignmentToBinary[op], left, right, point);
            }

            if (!right.ExpressionReturnType.IsAssignableTo(left.ExpressionReturnType))
            {
                ErrorCode.TypeMismatch.ReportAndThrow(point,
                                                      "Cannot assign {0} to {1}", right.ExpressionReturnType, left.ExpressionReturnType);
            }

            var instance = new AssignmentOperatorNode(point);

            instance.left  = left;
            instance.right = right;
            instance.type  = left.ExpressionReturnType;

            return(instance);
        }
Exemple #3
0
        private static ExpressionNode AsOverloadedMethod(ContextNode context, AssignmentOperatorType op, ExpressionNode left, ExpressionNode right, SequencePoint point)
        {
            if (!OperatorMethods.ContainsKey(op))
            {
                return(null);
            }

            string name = OperatorMethods[op];

            return(BinaryOperatorNode.AsOverload(context, name, left, right, point));
        }