ToMaxType() public static method

public static ToMaxType ( Type leftType, Type rightType ) : Type
leftType System.Type
rightType System.Type
return System.Type
コード例 #1
0
ファイル: ASTDivNode.cs プロジェクト: zyj0021/NVelocity
        /// <summary>
        /// Computes the result of the division. Currently limited to Integers.
        /// </summary>
        /// <returns>Integer(value) or null</returns>
        public override Object Value(IInternalContextAdapter context)
        {
            // get the two args
            Object left  = GetChild(0).Value(context);
            Object right = GetChild(1).Value(context);

            // if either is null, lets log and bail
            if (left == null || right == null)
            {
                runtimeServices.Error(
                    string.Format(
                        "{0} side ({1}) of division operation has null value. Operation not possible. {2} [line {3}, column {4}]",
                        (left == null ? "Left" : "Right"), GetChild((left == null ? 0 : 1)).Literal, context.CurrentTemplateName, Line,
                        Column));
                return(null);
            }

            Type maxType = MathUtil.ToMaxType(left.GetType(), right.GetType());

            if (maxType == null)
            {
                return(null);
            }

            try
            {
                return(MathUtil.Div(maxType, left, right));
            }
            catch (DivideByZeroException)
            {
                runtimeServices.Error("Right side of division operation is zero. Must be non-zero. " + context.CurrentTemplateName + " [line " + Line + ", column " + Column + "]");
            }
            return(null);
        }
コード例 #2
0
ファイル: ASTAddNode.cs プロジェクト: zyj0021/NVelocity
        /// <summary>
        /// Computes the sum of the two nodes.
        /// Currently only integer operations are supported.
        /// </summary>
        /// <returns>Integer object with value, or null</returns>
        public override Object Value(IInternalContextAdapter context)
        {
            // get the two addends
            Object left  = GetChild(0).Value(context);
            Object right = GetChild(1).Value(context);

            // if either is null, lets log and bail
            if (left == null || right == null)
            {
                runtimeServices.Error(
                    string.Format(
                        "{0} side ({1}) of addition operation has null value. Operation not possible. {2} [line {3}, column {4}]",
                        (left == null ? "Left" : "Right"), GetChild((left == null ? 0 : 1)).Literal, context.CurrentTemplateName, Line,
                        Column));
                return(null);
            }

            Type maxType = MathUtil.ToMaxType(left.GetType(), right.GetType());

            if (maxType == null)
            {
                return(null);
            }

            return(MathUtil.Add(maxType, left, right));

            // if not an Integer, not much we can do either
//			if (!(left is Int32) || !(right is Int32))
//			{
//				runtimeServices.Error((!(left is Int32) ? "Left" : "Right") + " side of addition operation is not a valid type. " + "Currently only integers (1,2,3...) and Integer type is supported. " + context.CurrentTemplateName + " [line " + Line + ", column " + Column + "]");
//
//				return null;
//			}
//			return ((Int32) left) + ((Int32) right);
        }
コード例 #3
0
        public override object Value(IInternalContextAdapter context)
        {
            object obj  = base.GetChild(0).Value(context);
            object obj2 = base.GetChild(1).Value(context);
            object result;

            if (obj == null || obj2 == null)
            {
                this.rsvc.Error(string.Concat(new object[]
                {
                    (obj == null) ? "Left" : "Right",
                    " side (",
                    base.GetChild((obj == null) ? 0 : 1).Literal,
                    ") of subtraction operation has null value. Operation not possible. ",
                    context.CurrentTemplateName,
                    " [line ",
                    base.Line,
                    ", column ",
                    base.Column,
                    "]"
                }));
                result = null;
            }
            else
            {
                Type type = MathUtil.ToMaxType(obj.GetType(), obj2.GetType());
                if (type == null)
                {
                    result = null;
                }
                else
                {
                    result = MathUtil.Sub(type, obj, obj2);
                }
            }
            return(result);
        }