Ejemplo n.º 1
0
        private BinaryEqualExpression Eq(Expression lhs, Expression rhs, ParserRuleContext context)
        {
            if (rhs == null || lhs == null)
            {
                return(null);
            }

            if (lhs.Type != rhs.Type)
            {
                this.AddSyntaxError("Types mismatch. Are you missing a cast?", context);
                return(null);
            }

            if (!ArithmeticTypeHelper.IsArithmetic(lhs.Type.ClrType))
            {
                var eqType = typeof(IEquatable <>).MakeGenericType(lhs.Type.ClrType);

                if (lhs.Type.ClrType.GetInterfaces().Contains(eqType))
                {
                    this.AddSyntaxError($"There is no defined equality operator for the type {lhs.Type.ClrType.Name}.", context);
                    return(null);
                }

                var meth = lhs.Type.ClrType.GetMethod("Equals", new[] { lhs.Type.ClrType });

                return(new BinaryEqualExpression(lhs, rhs, meth, context));
            }

            return(new BinaryEqualExpression(lhs, rhs, context));
        }
Ejemplo n.º 2
0
        private T BinaryCompare <T>(Expression lhs, Expression rhs, ParserRuleContext context)
            where T : BinaryExpression
        {
            if (lhs == null || rhs == null)
            {
                return(null);
            }

            if (lhs.Type != rhs.Type)
            {
                this.AddSyntaxError("Types mismatch. Are you missing a cast?", context);
                return(null);
            }

            var t = lhs.Type.ClrType;

            if (!ArithmeticTypeHelper.IsArithmetic(t))
            {
                var comparableInterface = typeof(IComparable <>).MakeGenericType(t);
                if (!t.GetInterfaces().Contains(comparableInterface))
                {
                    this.AddSyntaxError($"The type {t.Name} is not comparable.", context);
                    return(null);
                }
                else
                {
                    return((T)Activator.CreateInstance(typeof(T), lhs, rhs, t.GetMethod("CompareTo", new Type[] { t }), context));
                }
            }

            return((T)Activator.CreateInstance(typeof(T), lhs, rhs, context));
        }
Ejemplo n.º 3
0
        private T Binary <T>(Expression lhs, Expression rhs, string opName, ParserRuleContext context)
            where T : BinaryExpression
        {
            if (lhs == null || rhs == null)
            {
                return(null);
            }

            if (lhs.Type != rhs.Type)
            {
                this.AddSyntaxError("Types mismatch. Are you missing a cast?", context);
                return(null);
            }

            if (ArithmeticTypeHelper.IsArithmetic(lhs.Type.ClrType))
            {
                return((T)Activator.CreateInstance(typeof(T), lhs, rhs, context));
            }
            else
            {
                var op = this.FindOperator(lhs.Type.ClrType, "Add", context);
                if (op == null)
                {
                    return(null);
                }

                return((T)Activator.CreateInstance(typeof(T), lhs, rhs, op, context));
            }
        }
Ejemplo n.º 4
0
        private bool IsComparable(Type t, ParserRuleContext context)
        {
            if (!ArithmeticTypeHelper.IsArithmetic(t))
            {
                var comparableInterface = typeof(IComparable <>).MakeGenericType(t);
                if (!t.GetInterfaces().Contains(comparableInterface))
                {
                    this.AddSyntaxError($"The type {t.Name} is not comparable.", context);
                    return(false);
                }
            }

            return(true);
        }