protected override Completion ExecuteImpl(ExecutionEnvironment enviroment)
        {
            if (Min == null)
            {
                return(Completion.Exception("parameter of random can not be null", Min));
            }
            if (Max == null)
            {
                return(Completion.Exception("parameter of random can not be null", Max));
            }
            var left = Min.Execute(enviroment);

            if (left.Type != CompletionType.Value)
            {
                return(left);
            }
            var right = Max.Execute(enviroment);

            if (right.Type != CompletionType.Value)
            {
                return(right);
            }
            if (right.Type != CompletionType.Value)
            {
                return(right);
            }
            if (!TypeConverters.IsNumber(left.ReturnValue))
            {
                return(Completion.Exception("value is not a number", Min));
            }
            if (!TypeConverters.IsNumber(right.ReturnValue))
            {
                return(Completion.Exception("value is not a number", Max));
            }
            Type T = TypeConverters.GetMaxTypes(left.ReturnValue, right.ReturnValue);

            if (T == null)
            {
                return(Completion.Exception("Only nuber can accepted", this));
            }

            try
            {
                var l = TypeConverters.GetValue <int>(left.ReturnValue);
                var r = TypeConverters.GetValue <int>(right.ReturnValue);
                return(new Completion(random.Next(l, r)));
            }
            catch (Exception e)
            {
                return(Completion.Exception(e.Message, this));
            }
        }
示例#2
0
 protected override Completion ExecuteImpl(ExecutionEnvironment enviroment)
 {
     try
     {
         List <object> values = new List <object>();
         for (int i = 0; i < Args.Count; i++)
         {
             if (Args[i] == null)
             {
                 return(Completion.Exception(Language.NullException, this));
             }
             var c = Args[i].Execute(enviroment);
             if (!c.IsValue)
             {
                 return(c);
             }
             if (!TypeConverters.IsNumber(c.ReturnValue))
             {
                 return(Completion.Exception(Language.NotNumberException, Args[i]));
             }
             values.Add(c.ReturnValue);
         }
         int        methodCount  = 0;
         var        ms           = typeof(Math).GetMethods();
         MethodInfo doubleMethod = null;
         foreach (var m in ms)
         {
             if (m.Name.Equals(MathFunction) && m.GetParameters().Length == Args.Count)
             {
                 methodCount++;
                 bool isDouble = true;
                 foreach (var p in m.GetParameters())
                 {
                     if (!p.ParameterType.Equals(typeof(double)))
                     {
                         isDouble = false;
                         break;
                     }
                 }
                 if (isDouble)
                 {
                     doubleMethod = m;
                 }
             }
         }
         if (methodCount < 0)
         {
             return(Completion.Exception("no function found", this));
         }
         else if (methodCount == 1)
         {
             if (doubleMethod != null)
             {
                 var           ps     = doubleMethod.GetParameters();
                 List <object> fparam = new List <object>();
                 for (int i = 0; i < Args.Count; i++)
                 {
                     fparam.Add(TypeConverters.GetValue(values[i], ps[i].ParameterType));
                 }
                 return(new Completion(doubleMethod.Invoke(null, fparam.ToArray())));
             }
             else
             {
                 MethodInfo    method = typeof(Math).GetMethod(MathFunction);
                 var           ps     = method.GetParameters();
                 List <object> fparam = new List <object>();
                 for (int i = 0; i < Args.Count; i++)
                 {
                     fparam.Add(TypeConverters.GetValue(values[i], ps[i].ParameterType));
                 }
                 return(new Completion(method.Invoke(null, fparam.ToArray())));
             }
         }
         else if (methodCount == 2)
         {
             if (doubleMethod != null)
             {
                 var           ps     = doubleMethod.GetParameters();
                 List <object> fparam = new List <object>();
                 for (int i = 0; i < Args.Count; i++)
                 {
                     fparam.Add(TypeConverters.GetValue(values[i], ps[i].ParameterType));
                 }
                 return(new Completion(doubleMethod.Invoke(null, fparam.ToArray())));
             }
             return(Completion.Exception(Language.UnknowException, this));
         }
         else
         {
             var T = TypeConverters.GetMaxTypes(values.ToArray());
             if (T.Equals(typeof(char)))
             {
                 return(CallMathFunc <int>(values));
             }
             else if (T.Equals(typeof(int)))
             {
                 return(CallMathFunc <int>(values));
             }
             else if (T.Equals(typeof(float)))
             {
                 return(CallMathFunc <float>(values));
             }
             else
             {
                 return(CallMathFunc <double>(values));
             }
         }
     }catch (Exception e)
     {
         Console.WriteLine(e.Message);
         Console.WriteLine(e.StackTrace);
         return(Completion.Exception(e.Message, this));
     }
 }
        protected override Completion ExecuteImpl(ExecutionEnvironment enviroment)
        {
            if (Left == null)
            {
                return(Completion.Exception(Properties.Language.NullException, this));
            }
            if (Right == null)
            {
                return(Completion.Exception(Properties.Language.NullException, this));
            }
            var left = Left.Execute(enviroment);

            if (left.Type != CompletionType.Value)
            {
                return(left);
            }
            var right = Right.Execute(enviroment);

            if (right.Type != CompletionType.Value)
            {
                return(right);
            }
            if (IsLogicalOperator(Operator))
            {
                return(ExecuteLogical(enviroment, left.ReturnValue, right.ReturnValue));
            }
            if (Operator == Operator.Add)
            {
                if ((left.ReturnValue is string) || right.ReturnValue is string)
                {
                    return(ExecuteStringAdd(enviroment, left.ReturnValue, right.ReturnValue));
                }
            }
            if (Operator == Operator.Equal)
            {
                if (left.ReturnValue is string)
                {
                    return(new Completion(left.ReturnValue is string && (left.ReturnValue as string).Equals(right.ReturnValue)));
                }
                if (right.ReturnValue is string)
                {
                    return(new Completion(right.ReturnValue is string && (right.ReturnValue as string).Equals(left.ReturnValue)));
                }
                if (left.ReturnValue is bool)
                {
                    return(new Completion(right.ReturnValue is bool && (bool)right.ReturnValue == (bool)left.ReturnValue));
                }
                if (right.ReturnValue is bool)
                {
                    return(new Completion(left.ReturnValue is bool && (bool)left.ReturnValue == (bool)right.ReturnValue));
                }
                if (left.ReturnValue == null)
                {
                    if (right.ReturnValue == null)
                    {
                        return(new Completion(true));
                    }
                    else
                    {
                        return(new Completion(false));
                    }
                }
                if (right.ReturnValue == null)
                {
                    if (left.ReturnValue == null)
                    {
                        return(new Completion(true));
                    }
                    return(new Completion(false));
                }
            }
            if (Operator == Operator.NotEqual)
            {
                if (left.ReturnValue is string)
                {
                    return(new Completion(!(left.ReturnValue as string).Equals(right.ReturnValue)));
                }
                if (right.ReturnValue is string)
                {
                    return(new Completion(!(right.ReturnValue as string).Equals(left.ReturnValue)));
                }
                if (left.ReturnValue is bool)
                {
                    return(new Completion(!(right.ReturnValue is bool) || (bool)right.ReturnValue != (bool)left.ReturnValue));
                }
                if (right.ReturnValue is bool)
                {
                    return(new Completion(!(left.ReturnValue is bool) || (bool)left.ReturnValue != (bool)right.ReturnValue));
                }
                if (left.ReturnValue == null)
                {
                    if (right.ReturnValue != null)
                    {
                        return(new Completion(true));
                    }
                    else
                    {
                        return(new Completion(false));
                    }
                }
                if (right.ReturnValue == null)
                {
                    if (left.ReturnValue != null)
                    {
                        return(new Completion(true));
                    }
                    return(new Completion(false));
                }
            }
            if (Operator == Operator.Add)
            {
                if ((left.ReturnValue is DateTime) && right.ReturnValue is DateTime)
                {
                    return(ExecuteStringAdd(enviroment, left.ReturnValue, right.ReturnValue));
                }
            }
            Type T = TypeConverters.GetMaxTypes(left.ReturnValue, right.ReturnValue);

            if (T == null)
            {
                return(Completion.Exception(Properties.Language.NotNumber, this));
            }
            if (Operator == Operator.Mod || Operator == Operator.BitAnd || Operator == Operator.BitOr ||
                Operator == Operator.BitLeftShift || Operator == Operator.BitRightShift || Operator == Operator.BitExclusiveOr)
            {
                T = typeof(int);
            }
            if (T.Equals(typeof(char)))
            {
                try
                {
                    var l = TypeConverters.GetValue <char>(left.ReturnValue);
                    var r = TypeConverters.GetValue <char>(right.ReturnValue);
                    switch (Operator)
                    {
                    case Operator.Add:
                        return(new Completion(l + r));

                    case Operator.Minus:
                        return(new Completion(l - r));

                    case Operator.Mulitiply:
                        return(new Completion(l * r));

                    case Operator.Divide:
                        return(new Completion(l / r));

                    case Operator.Mod:
                        return(new Completion(l % r));

                    case Operator.Great:
                        return(new Completion(l > r));

                    case Operator.GreatOrEqual:
                        return(new Completion(l >= r));

                    case Operator.Less:
                        return(new Completion(l < r));

                    case Operator.LessOrEqual:
                        return(new Completion(l <= r));

                    case Operator.Equal:
                        return(new Completion(l == r));

                    case Operator.NotEqual:
                        return(new Completion(l != r));

                    case Operator.BitAnd:
                        return(new Completion(l & r));

                    case Operator.BitOr:
                        return(new Completion(l | r));

                    case Operator.BitLeftShift:
                        return(new Completion(l << r));

                    case Operator.BitRightShift:
                        return(new Completion(l >> r));

                    case Operator.BitExclusiveOr:
                        return(new Completion(l ^ r));
                    }
                    return(Completion.Exception(Properties.Language.UnknowException, this));
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.StackTrace);
                    return(Completion.Exception(e.Message, this));
                }
            }
            if (T.Equals(typeof(int)))
            {
                try
                {
                    var l = TypeConverters.GetValue <int>(left.ReturnValue);
                    var r = TypeConverters.GetValue <int>(right.ReturnValue);
                    switch (Operator)
                    {
                    case Operator.Add:
                        return(new Completion(l + r));

                    case Operator.Minus:
                        return(new Completion(l - r));

                    case Operator.Mulitiply:
                        return(new Completion(l * r));

                    case Operator.Divide:
                        return(new Completion(l / r));

                    case Operator.Mod:
                        return(new Completion(l % r));

                    case Operator.Great:
                        return(new Completion(l > r));

                    case Operator.GreatOrEqual:
                        return(new Completion(l >= r));

                    case Operator.Less:
                        return(new Completion(l < r));

                    case Operator.LessOrEqual:
                        return(new Completion(l <= r));

                    case Operator.Equal:
                        return(new Completion(l == r));

                    case Operator.NotEqual:
                        return(new Completion(l != r));

                    case Operator.BitAnd:
                        return(new Completion(l & r));

                    case Operator.BitOr:
                        return(new Completion(l | r));

                    case Operator.BitLeftShift:
                        return(new Completion(l << r));

                    case Operator.BitRightShift:
                        return(new Completion(l >> r));

                    case Operator.BitExclusiveOr:
                        return(new Completion(l ^ r));
                    }
                    return(Completion.Exception(Properties.Language.UnknowException, this));
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.StackTrace);
                    return(Completion.Exception(e.Message, this));
                }
            }
            if (T.Equals(typeof(float)))
            {
                try
                {
                    var l = TypeConverters.GetValue <float>(left.ReturnValue);
                    var r = TypeConverters.GetValue <float>(right.ReturnValue);
                    switch (Operator)
                    {
                    case Operator.Add:
                        return(new Completion(l + r));

                    case Operator.Minus:
                        return(new Completion(l - r));

                    case Operator.Mulitiply:
                        return(new Completion(l * r));

                    case Operator.Divide:
                        return(new Completion(l / r));

                    case Operator.Great:
                        return(new Completion(l > r));

                    case Operator.GreatOrEqual:
                        return(new Completion(l >= r));

                    case Operator.Less:
                        return(new Completion(l < r));

                    case Operator.LessOrEqual:
                        return(new Completion(l <= r));

                    case Operator.Equal:
                        return(new Completion(l == r));

                    case Operator.NotEqual:
                        return(new Completion(l != r));
                    }
                    return(Completion.Exception(Properties.Language.UnknowException, this));
                }
                catch (Exception e)
                {
                    return(Completion.Exception(e.Message, this));
                }
            }
            if (T.Equals(typeof(long)))
            {
                try
                {
                    var l = TypeConverters.GetValue <long>(left.ReturnValue);
                    var r = TypeConverters.GetValue <long>(right.ReturnValue);
                    switch (Operator)
                    {
                    case Operator.Add:
                        return(new Completion(l + r));

                    case Operator.Minus:
                        return(new Completion(l - r));

                    case Operator.Mulitiply:
                        return(new Completion(l * r));

                    case Operator.Divide:
                        return(new Completion(l / r));

                    case Operator.Great:
                        return(new Completion(l > r));

                    case Operator.GreatOrEqual:
                        return(new Completion(l >= r));

                    case Operator.Less:
                        return(new Completion(l < r));

                    case Operator.LessOrEqual:
                        return(new Completion(l <= r));

                    case Operator.Equal:
                        return(new Completion(l == r));

                    case Operator.NotEqual:
                        return(new Completion(l != r));
                    }
                    return(Completion.Exception(Properties.Language.UnknowException, this));
                }
                catch (Exception e)
                {
                    return(Completion.Exception(e.Message, this));
                }
            }
            {
                try
                {
                    double l = TypeConverters.GetValue <double>(left.ReturnValue);
                    double r = TypeConverters.GetValue <double>(right.ReturnValue);
                    switch (Operator)
                    {
                    case Operator.Add:
                        return(new Completion(l + r));

                    case Operator.Minus:
                        return(new Completion(l - r));

                    case Operator.Mulitiply:
                        return(new Completion(l * r));

                    case Operator.Divide:
                        return(new Completion(l / r));

                    case Operator.Great:
                        return(new Completion(l > r));

                    case Operator.GreatOrEqual:
                        return(new Completion(l >= r));

                    case Operator.Less:
                        return(new Completion(l < r));

                    case Operator.LessOrEqual:
                        return(new Completion(l <= r));

                    case Operator.Equal:
                        return(new Completion(l == r));

                    case Operator.NotEqual:
                        return(new Completion(l != r));
                    }
                    return(Completion.Exception(Properties.Language.UnknowException, this));
                }
                catch (Exception e)
                {
                    return(Completion.Exception(e.Message, this));
                }
            }
        }
        protected override Completion ExecuteImpl(ExecutionEnvironment enviroment)
        {
            if (Right == null)
            {
                return(Completion.Exception(Properties.Language.NoValueAssigned, this));
            }
            var right = Right.Execute(enviroment);

            if (right.Type != CompletionType.Value)
            {
                return(right);
            }
            if (Left == null)
            {
                return(right);
            }
            if (Left is IAssignment)
            {
                try
                {
                    if (Operator == AssignmentOperator.Equal)
                    {
                        return((Left as IAssignment).Assign(enviroment, right.ReturnValue));
                    }
                    var l = Left.Execute(enviroment);
                    if (!l.IsValue)
                    {
                        return(l);
                    }
                    if (l.ReturnValue == null)
                    {
                        return(Completion.Exception(Properties.Language.VariableNullException, Left));
                    }
                    Type lt = l.ReturnValue.GetType();

                    if (l.ReturnValue is string || right.ReturnValue is string)
                    {
                        if (Operator != AssignmentOperator.AddEqual)
                        {
                            return(Completion.Exception(Properties.Language.StringOnlySupport, this));
                        }
                        return((Left as IAssignment).Assign(enviroment, l.ReturnValue + "" + right.ReturnValue));
                    }
                    if (!TypeConverters.IsNumber(l.ReturnValue))
                    {
                        return(Completion.Exception(Properties.Language.NotNumber, Left));
                    }
                    if (!TypeConverters.IsNumber(right.ReturnValue))
                    {
                        return(Completion.Exception(Properties.Language.NotNumber, Right));
                    }
                    Type maxType = TypeConverters.GetMaxTypes(l.ReturnValue, right.ReturnValue);
                    Console.Write(maxType);
                    if (maxType.Equals(typeof(char)))
                    {
                        var lint = TypeConverters.GetValue <char>(l.ReturnValue);
                        var rint = TypeConverters.GetValue <char>(right.ReturnValue);
                        if (Operator == AssignmentOperator.AddEqual)
                        {
                            return((Left as IAssignment).Assign(enviroment, lint + rint));
                        }
                        if (Operator == AssignmentOperator.MinusEqual)
                        {
                            return((Left as IAssignment).Assign(enviroment, lint - rint));
                        }
                        if (Operator == AssignmentOperator.MulitiplyEqual)
                        {
                            return((Left as IAssignment).Assign(enviroment, lint * rint));
                        }
                        if (Operator == AssignmentOperator.DivideEqual)
                        {
                            return((Left as IAssignment).Assign(enviroment, lint / rint));
                        }
                        if (Operator == AssignmentOperator.ModEqual)
                        {
                            return((Left as IAssignment).Assign(enviroment, lint % rint));
                        }
                        if (Operator == AssignmentOperator.BitAndEqual)
                        {
                            return((Left as IAssignment).Assign(enviroment, lint & rint));
                        }
                        if (Operator == AssignmentOperator.BitExclusiveOrEqual)
                        {
                            return((Left as IAssignment).Assign(enviroment, lint ^ rint));
                        }
                        if (Operator == AssignmentOperator.BitLeftShiftEqual)
                        {
                            return((Left as IAssignment).Assign(enviroment, lint << rint));
                        }
                        if (Operator == AssignmentOperator.BitOrEqual)
                        {
                            return((Left as IAssignment).Assign(enviroment, lint | rint));
                        }
                        if (Operator == AssignmentOperator.BitRightShiftEqual)
                        {
                            return((Left as IAssignment).Assign(enviroment, lint >> rint));
                        }
                        return(Completion.Exception(Properties.Language.UnknowException, this));
                    }
                    if (Operator == AssignmentOperator.BitAndEqual || Operator == AssignmentOperator.BitExclusiveOrEqual || Operator == AssignmentOperator.BitLeftShiftEqual ||
                        Operator == AssignmentOperator.BitOrEqual || Operator == AssignmentOperator.BitRightShiftEqual)
                    {
                        int lint = TypeConverters.GetValue <int>(l.ReturnValue);
                        int rint = TypeConverters.GetValue <int>(right.ReturnValue);
                        if (Operator == AssignmentOperator.BitAndEqual)
                        {
                            return((Left as IAssignment).Assign(enviroment, lint & rint));
                        }
                        if (Operator == AssignmentOperator.BitExclusiveOrEqual)
                        {
                            return((Left as IAssignment).Assign(enviroment, lint ^ rint));
                        }
                        if (Operator == AssignmentOperator.BitLeftShiftEqual)
                        {
                            return((Left as IAssignment).Assign(enviroment, lint << rint));
                        }
                        if (Operator == AssignmentOperator.BitOrEqual)
                        {
                            return((Left as IAssignment).Assign(enviroment, lint | rint));
                        }
                        if (Operator == AssignmentOperator.BitRightShiftEqual)
                        {
                            return((Left as IAssignment).Assign(enviroment, lint >> rint));
                        }
                        return(Completion.Exception(Properties.Language.UnknowException, this));
                    }
                    if (maxType.Equals(typeof(int)))
                    {
                        int lint = TypeConverters.GetValue <int>(l.ReturnValue);
                        int rint = TypeConverters.GetValue <int>(right.ReturnValue);
                        if (Operator == AssignmentOperator.AddEqual)
                        {
                            return((Left as IAssignment).Assign(enviroment, lint + rint));
                        }
                        if (Operator == AssignmentOperator.MinusEqual)
                        {
                            return((Left as IAssignment).Assign(enviroment, lint - rint));
                        }
                        if (Operator == AssignmentOperator.MulitiplyEqual)
                        {
                            return((Left as IAssignment).Assign(enviroment, lint * rint));
                        }
                        if (Operator == AssignmentOperator.DivideEqual)
                        {
                            return((Left as IAssignment).Assign(enviroment, lint / rint));
                        }
                        if (Operator == AssignmentOperator.ModEqual)
                        {
                            return((Left as IAssignment).Assign(enviroment, lint % rint));
                        }
                        return(Completion.Exception(Properties.Language.UnknowException, this));
                    }
                    else if (maxType.Equals(typeof(float)))
                    {
                        float lint = TypeConverters.GetValue <float>(l.ReturnValue);
                        float rint = TypeConverters.GetValue <int>(right.ReturnValue);
                        if (Operator == AssignmentOperator.AddEqual)
                        {
                            return((Left as IAssignment).Assign(enviroment, lint + rint));
                        }
                        if (Operator == AssignmentOperator.MinusEqual)
                        {
                            return((Left as IAssignment).Assign(enviroment, lint - rint));
                        }
                        if (Operator == AssignmentOperator.MulitiplyEqual)
                        {
                            return((Left as IAssignment).Assign(enviroment, lint * rint));
                        }
                        if (Operator == AssignmentOperator.DivideEqual)
                        {
                            return((Left as IAssignment).Assign(enviroment, lint / rint));
                        }
                        if (Operator == AssignmentOperator.ModEqual)
                        {
                            return((Left as IAssignment).Assign(enviroment, lint % rint));
                        }
                        return(Completion.Exception(Properties.Language.UnknowException, this));
                    }
                    else
                    {
                        double lint = TypeConverters.GetValue <double>(l.ReturnValue);
                        double rint = TypeConverters.GetValue <double>(right.ReturnValue);
                        if (Operator == AssignmentOperator.AddEqual)
                        {
                            return((Left as IAssignment).Assign(enviroment, lint + rint));
                        }
                        if (Operator == AssignmentOperator.MinusEqual)
                        {
                            return((Left as IAssignment).Assign(enviroment, lint - rint));
                        }
                        if (Operator == AssignmentOperator.MulitiplyEqual)
                        {
                            return((Left as IAssignment).Assign(enviroment, lint * rint));
                        }
                        if (Operator == AssignmentOperator.DivideEqual)
                        {
                            return((Left as IAssignment).Assign(enviroment, lint / rint));
                        }
                        if (Operator == AssignmentOperator.ModEqual)
                        {
                            return((Left as IAssignment).Assign(enviroment, lint % rint));
                        }
                        return(Completion.Exception(Properties.Language.UnknowException, this));
                    }
                }catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Console.WriteLine(e.StackTrace);
                    return(Completion.Exception(e.Message, this));
                }
            }
            else
            {
                return(Completion.Exception(Properties.Language.InvalidBeforeEqual, Left));
            }
        }