Пример #1
0
        /// <summary>
        /// Returns the minimum of the given arguments.
        /// </summary>
        private static void Min(Variable[] parameters, Variable returnValue)
        {
            Debug.Assert(parameters.Length >= 1);
            var variables = FlattenVariableList(parameters);

            if (variables.Count() > 0)
            {
                if (variables.Any(p => p.IsFloat()))
                {
                    double min = int.MaxValue;
                    foreach (Variable variable in variables)
                    {
                        double value = variable.ToFloat();
                        if (value < min)
                        {
                            min = value;
                        }
                    }
                    returnValue.SetValue(min);
                }
                else
                {
                    int min = int.MaxValue;
                    foreach (Variable variable in variables)
                    {
                        int value = variable.ToInteger();
                        if (value < min)
                        {
                            min = value;
                        }
                    }
                    returnValue.SetValue(min);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Returns the average of the given arguments.
        /// </summary>
        private static void Avg(Variable[] parameters, Variable returnValue)
        {
            Debug.Assert(parameters.Length >= 1);
            var variables = FlattenVariableList(parameters);

            if (variables.Count() > 0)
            {
                if (variables.Any(p => p.IsFloat()))
                {
                    double total = 0;
                    foreach (Variable variable in variables)
                    {
                        total += variable.ToFloat();
                    }
                    returnValue.SetValue(total / variables.Count());
                }
                else
                {
                    int total = 0;
                    foreach (Variable variable in variables)
                    {
                        total += variable.ToInteger();
                    }
                    returnValue.SetValue(total / variables.Count());
                }
            }
        }
Пример #3
0
 /// <summary>
 /// Returns the string length of the given value. If the argument is a list,
 /// the number of items in the list is returned. Otherwise, the number of
 /// characters in the value converted to a string is returned.
 /// </summary>
 private static void Len(Variable[] parameters, Variable returnValue)
 {
     Debug.Assert(parameters.Length == 1);
     if (parameters[0].IsList)
     {
         returnValue.SetValue(parameters[0].ListCount);
     }
     else
     {
         returnValue.SetValue(parameters[0].ToString().Length);
     }
 }
Пример #4
0
 /// <summary>
 /// Returns the absolute value of the given number.
 /// </summary>
 private static void Abs(Variable[] parameters, Variable returnValue)
 {
     Debug.Assert(parameters.Length == 1);
     if (parameters[0].IsFloat())
     {
         returnValue.SetValue(Math.Abs(parameters[0].ToFloat()));
     }
     else
     {
         returnValue.SetValue(Math.Abs(parameters[0].ToInteger()));
     }
 }
Пример #5
0
        /// <summary>
        /// Returns the left-most number of characters specified by the second argument
        /// from the string specified by the first argument.
        /// </summary>
        private static void Left(Variable[] parameters, Variable returnValue)
        {
            Debug.Assert(parameters.Length == 2);
            string s   = parameters[0].ToString();
            int    len = parameters[1].ToInteger();

            if (s.Length > len)
            {
                returnValue.SetValue(s.Substring(0, len));
            }
            returnValue.SetValue(s);
        }
Пример #6
0
        /// <summary>
        /// Returns the right-most number of characters specified by the second argument
        /// from the string specified by the first argument.
        /// </summary>
        private static void Right(Variable[] parameters, Variable returnValue)
        {
            Debug.Assert(parameters.Length == 2);
            string s = parameters[0].ToString();

            returnValue.SetValue(s.Substring(s.Length - parameters[1].ToInteger()));
        }
Пример #7
0
        /// <summary>
        /// Returns the ASCII/Unicode value of the first character in
        /// the given string.
        /// </summary>
        private static void Asc(Variable[] parameters, Variable returnValue)
        {
            Debug.Assert(parameters.Length == 1);
            string s = parameters[0].ToString();

            returnValue.SetValue(s.Length > 0 ? s[0] : 0);
        }
Пример #8
0
        /// <summary>
        /// Returns the first 1-based position where the second string appears within the first
        /// string. An optional 3rd argument specifies the 1-based position to begin the search.
        /// Returns 0 if the string is not found.
        /// </summary>
        private static void InStr(Variable[] parameters, Variable returnValue)
        {
            Debug.Assert(parameters.Length >= 2);
            int start = (parameters.Length >= 3) ? Math.Max(parameters[2].ToInteger() - 1, 0) : 0;
            int match = parameters[0].ToString().IndexOf(parameters[1].ToString(), start);

            returnValue.SetValue(match + 1);
        }
Пример #9
0
        private void Assign()
        {
            Variable var    = GetVariable(Reader.GetNextValue());
            bool     result = EvalExpression();

            Debug.Assert(result);
            var.SetValue(VarStack.Pop());
        }
Пример #10
0
        /// <summary>
        /// Returns a section of the string given as the first argument. The second
        /// argument is the 1-based position where the string should be extracted. If
        /// a third argument is given, it specifies the maximum number of string to
        /// return.
        /// </summary>
        private static void Mid(Variable[] parameters, Variable returnValue)
        {
            Debug.Assert(parameters.Length >= 2);
            string s     = parameters[0].ToString();
            int    start = Math.Max(parameters[1].ToInteger() - 1, 0);
            int    count = (parameters.Length > 2) ? Math.Min(parameters[2].ToInteger(), s.Length - start) : (s.Length - start);

            returnValue.SetValue(s.Substring(start, count));
        }
Пример #11
0
        /// <summary>
        /// Returns a string consisting of the first argument repeated the number of times
        /// specified by the second argument. If the first argument is not a string, the
        /// result will consist of characters with the ASCII/Unicode value of this argument.
        /// </summary>
        private static void String(Variable[] parameters, Variable returnValue)
        {
            Debug.Assert(parameters.Length == 2);
            string s = (parameters[0].Type == ValueType.String) ?
                       parameters[0].ToString() :
                       ((char)parameters[0].ToInteger()).ToString();
            StringBuilder builder = new StringBuilder(s.Length * Math.Max(0, parameters[1].ToInteger()));

            for (int i = 0; i < parameters[1].ToInteger(); i++)
            {
                builder.Append(s);
            }
            returnValue.SetValue(builder.ToString());
        }
Пример #12
0
        private void AssignListVariable()
        {
            Variable array = GetVariable(Reader.GetNextValue());
            // Evaluate index
            bool result = EvalExpression();

            Debug.Assert(result);
            Variable index = VarStack.Pop();
            Variable var   = array.GetAt(index.ToInteger() - 1);

            // Evaluate expression
            result = EvalExpression();
            Debug.Assert(result);
            var.SetValue(VarStack.Pop());
        }
Пример #13
0
 /// <summary>
 /// Returns a string with the current time.
 /// </summary>
 private static void Time(Variable[] parameters, Variable returnValue)
 {
     Debug.Assert(parameters.Length == 0);
     returnValue.SetValue(DateTime.Now.ToString("T"));
 }
Пример #14
0
 /// <summary>
 /// Returns the tanget of the specified angle.
 /// </summary>
 private static void Tan(Variable[] parameters, Variable returnValue)
 {
     Debug.Assert(parameters.Length == 1);
     returnValue.SetValue(Math.Tan(parameters[0].ToFloat()));
 }
Пример #15
0
 /// <summary>
 /// Rounds the given value to the nearest integer.
 /// </summary>
 private static void Round(Variable[] parameters, Variable returnValue)
 {
     Debug.Assert(parameters.Length == 1);
     returnValue.SetValue(parameters[0].ToInteger());
 }
Пример #16
0
 /// <summary>
 /// Converts the given value to an octal string.
 /// </summary>
 private static void Oct(Variable[] parameters, Variable returnValue)
 {
     Debug.Assert(parameters.Length == 1);
     returnValue.SetValue(Convert.ToString(parameters[0].ToInteger(), 8));
 }
Пример #17
0
 /// <summary>
 /// Converts the given value to a floating point number.
 /// </summary>
 private static void Float(Variable[] parameters, Variable returnValue)
 {
     returnValue.SetValue(parameters[0].ToFloat());
 }
Пример #18
0
 /// <summary>
 /// Returns true if the given variable is a list.
 /// </summary>
 private static void IsList(Variable[] parameters, Variable returnValue)
 {
     Debug.Assert(parameters.Length == 1);
     returnValue.SetValue(parameters[0].IsList ? Boolean.True : Boolean.False);
 }
Пример #19
0
 /// <summary>
 /// Converts the given value to an integer, truncating any fractional portion.
 /// </summary>
 private static void Int(Variable[] parameters, Variable returnValue)
 {
     Debug.Assert(parameters.Length == 1);
     returnValue.SetValue((int)parameters[0].ToFloat());
 }
Пример #20
0
 /// <summary>
 /// Returns a hexedecimal string equal to the given value.
 /// </summary>
 private static void Hex(Variable[] parameters, Variable returnValue)
 {
     Debug.Assert(parameters.Length == 1);
     returnValue.SetValue(parameters[0].ToInteger().ToString("X"));
 }
Пример #21
0
 /// <summary>
 /// Creates a string with one character with the specified ASCII/
 /// Unicode value.
 /// </summary>
 private static void Chr(Variable[] parameters, Variable returnValue)
 {
     Debug.Assert(parameters.Length == 1);
     returnValue.SetValue(((char)parameters[0].ToInteger()).ToString());
 }
Пример #22
0
 /// <summary>
 /// Returns the value of the specified environment variable.
 /// </summary>
 private static void Environ(Variable[] parameters, Variable returnValue)
 {
     Debug.Assert(parameters.Length == 1);
     returnValue.SetValue(Environment.GetEnvironmentVariable(parameters[0].ToString() ?? string.Empty));
 }