/// <summary> /// Converts a parameter to an integer /// </summary> /// <param name="index">Index of parameter to convert</param> /// <returns></returns> public int ToInt(int index) { double?v = ConConverter.ToDouble(this[index]); if (!v.HasValue) { Command.ThrowNaNError(this[index], ErrorCode.NOT_A_NUMBER); throw new InvalidCastException("NaN"); } if (Math.Round(v.Value) != v.Value) { Command.ThrowNoFloatsAllowedError(v.Value, ErrorCode.NUMBER_NOT_INTEGER); throw new InvalidCastException("Not an integer"); } try { return((int)Math.Round(v.Value)); } catch (Exception) { Command.ThrowNoFloatsAllowedError(v.Value, ErrorCode.NUMBER_NOT_INTEGER); throw new InvalidCastException("Conversion not possible"); } }
/// <summary> /// Converts a parameter to a double /// </summary> /// <param name="index">Index of parameter to convert</param> /// <returns></returns> public double ToDouble(int index) { double?v = ConConverter.ToDouble(this[index]); if (!v.HasValue) { Command.ThrowNaNError(this[index], ErrorCode.NOT_A_NUMBER); throw new InvalidCastException("NaN"); } return(v.Value); }
/// <summary> /// Converts a parameter to an boolean /// </summary> /// <param name="index">Index of parameter to convert</param> /// <returns></returns> public bool ToBoolean(int index) { bool?v = ConConverter.ToBool(this[index]); if (!v.HasValue) { Command.ThrowArgumentError(this[index], new string[] { "true", "false" }, ErrorCode.NOT_BOOLEAN); throw new InvalidCastException("NaN"); } return(v.Value); }
/// <summary> /// Gets a list with the given string id /// </summary> /// <param name="idstr">ID of list enclosed in double square brackets</param> /// <returns></returns> public static List <string> GetList(string idstr) { //Decodes the ID of the list int?v = ConConverter.ToInt(idstr.TrimStart('[').TrimEnd(']')); //If the ID was invalid if (v == null) { Command.ThrowNaNError(idstr, ErrorCode.NOT_A_NUMBER); } //If the ID is unclaimed if (!StandardLib.Variables.List.Registry.ContainsKey(v.Value)) { Command.ThrowGenericError("Attempted access of undefined list", ErrorCode.INVALID_CONTEXT); } return(StandardLib.Variables.List.Registry[v.Value]); }
/// <summary> /// Checks whether an argument can be represented as a boolean /// </summary> /// <param name="index">Index of parameter to check</param> /// <returns></returns> public bool IsBoolean(int index) { return(ConConverter.ToBool(this[index]).HasValue); }
/// <summary> /// Checks whether an argument can be represented as an integer /// </summary> /// <param name="index">Index of parameter to check</param> /// <returns></returns> public bool IsInteger(int index) { return(ConConverter.ToInt(this[index]).HasValue); }
/// <summary> /// Checks whether an argument can be represented as a double /// </summary> /// <param name="index">Index of parameter to check</param> /// <returns></returns> public bool IsDouble(int index) { return(ConConverter.ToDouble(this[index]).HasValue); }
/// <summary> /// Assigns a value to a variable /// </summary> /// <param name="name">Name of variable to set</param> /// <param name="value">Value to assign</param> public void Set(string name, int value) { Set(name, ConConverter.ToString(value)); }