/// <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); } } }
/// <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()); } } }
/// <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); } }
/// <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())); } }
/// <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); }
/// <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())); }
/// <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); }
/// <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); }
private void Assign() { Variable var = GetVariable(Reader.GetNextValue()); bool result = EvalExpression(); Debug.Assert(result); var.SetValue(VarStack.Pop()); }
/// <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)); }
/// <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()); }
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()); }
/// <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")); }
/// <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())); }
/// <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()); }
/// <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)); }
/// <summary> /// Converts the given value to a floating point number. /// </summary> private static void Float(Variable[] parameters, Variable returnValue) { returnValue.SetValue(parameters[0].ToFloat()); }
/// <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); }
/// <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()); }
/// <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")); }
/// <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()); }
/// <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)); }