/////////////////////////////////////////////////////////////////////////////////////////////// #region IExecuteArgument Members public override ReturnCode Execute( Interpreter interpreter, IClientData clientData, ArgumentList arguments, ref Argument value, ref Result error ) { ReturnCode code = ReturnCode.Ok; if (interpreter != null) { if (arguments != null) { if (arguments.Count == (this.Arguments + 1)) { Variant variant1 = null; code = Value.GetVariant(interpreter, (IGetValue)arguments[1], ValueFlags.AnyVariant, interpreter.CultureInfo, ref variant1, ref error); if (code == ReturnCode.Ok) { try { if (variant1.IsDateTime()) { value = ConversionOps.ToInt((DateTime)variant1.Value); /* LOSSY */ } else if (variant1.IsDouble()) { variant1.Value = Math.Truncate((double)variant1.Value); if (variant1.ConvertTo(typeof(int))) { value = (int)variant1.Value; } else { error = "integer value too large to represent"; code = ReturnCode.Error; } } else if (variant1.IsDecimal()) { variant1.Value = Math.Truncate((decimal)variant1.Value); if (variant1.ConvertTo(typeof(int))) { value = (int)variant1.Value; } else { error = "integer value too large to represent"; code = ReturnCode.Error; } } else if (variant1.IsWideInteger()) { value = ConversionOps.ToInt((long)variant1.Value); /* LOSSY */ } else if (variant1.IsInteger()) { value = (int)variant1.Value; /* NOP */ } else if (variant1.IsBoolean()) { value = ConversionOps.ToInt((bool)variant1.Value); } else { error = String.Format( "expected integer but got \"{0}\"", arguments[1]); code = ReturnCode.Error; } } catch (Exception e) { Engine.SetExceptionErrorCode(interpreter, e); error = String.Format( "caught math exception: {0}", e); code = ReturnCode.Error; } } } else { if (arguments.Count > (this.Arguments + 1)) { error = String.Format( "too many arguments for math function \"{0}\"", base.Name); } else { error = String.Format( "too few arguments for math function \"{0}\"", base.Name); } code = ReturnCode.Error; } } else { error = "invalid argument list"; code = ReturnCode.Error; } } else { error = "invalid interpreter"; code = ReturnCode.Error; } return(code); }
/////////////////////////////////////////////////////////////////////////////////////////////// #region IExecuteArgument Members public override ReturnCode Execute( Interpreter interpreter, IClientData clientData, ArgumentList arguments, ref Argument value, ref Result error ) { ReturnCode code = ReturnCode.Ok; if (interpreter != null) { if (arguments != null) { try { string name = null; Variant operand1 = null; Variant operand2 = null; code = Value.GetOperandsFromArguments(interpreter, this, arguments, ValueFlags.AnyVariant, interpreter.CultureInfo, ref name, ref operand1, ref operand2, ref error); if (code == ReturnCode.Ok) { if (operand1 != null) { if (operand1.ConvertTo(typeof(bool))) { if (operand1.IsBoolean()) { value = LogicOps.Not((bool)operand1.Value); } else { error = String.Format( "unsupported operand type for operator {0}", FormatOps.OperatorName(name, this.Lexeme)); code = ReturnCode.Error; } } else { error = String.Format( "failed to convert operand to type \"{0}\"", typeof(bool)); code = ReturnCode.Error; } } else { error = String.Format( "operand for operator {0} is invalid", FormatOps.OperatorName(name, this.Lexeme)); code = ReturnCode.Error; } } } catch (Exception e) { Engine.SetExceptionErrorCode(interpreter, e); error = String.Format( "caught math exception: {0}", e); code = ReturnCode.Error; } } else { error = "invalid argument list"; code = ReturnCode.Error; } } else { error = "invalid interpreter"; code = ReturnCode.Error; } return(code); }
/////////////////////////////////////////////////////////////////////////////////////////////// #region IExecuteArgument Members public override ReturnCode Execute( Interpreter interpreter, IClientData clientData, ArgumentList arguments, ref Argument value, ref Result error ) { ReturnCode code = ReturnCode.Ok; if (interpreter != null) { if (arguments != null) { if (arguments.Count == (this.Arguments + 1)) { Variant variant1 = null; code = Value.GetVariant(interpreter, (IGetValue)arguments[1], ValueFlags.AnyVariant, interpreter.CultureInfo, ref variant1, ref error); if (code == ReturnCode.Ok) { try { if (variant1.IsList()) { value = (StringList)variant1.Value; /* NOP */ } else if (variant1.ConvertTo(typeof(StringList))) { value = (StringList)variant1.Value; } else { error = String.Format( "expected list but got \"{0}\"", arguments[1]); code = ReturnCode.Error; } } catch (Exception e) { Engine.SetExceptionErrorCode(interpreter, e); error = String.Format( "caught math exception: {0}", e); code = ReturnCode.Error; } } } else { if (arguments.Count > (this.Arguments + 1)) { error = String.Format( "too many arguments for math function \"{0}\"", base.Name); } else { error = String.Format( "too few arguments for math function \"{0}\"", base.Name); } code = ReturnCode.Error; } } else { error = "invalid argument list"; code = ReturnCode.Error; } } else { error = "invalid interpreter"; code = ReturnCode.Error; } return(code); }
/////////////////////////////////////////////////////////////////////////////////////////////// #region IExecuteArgument Members public override ReturnCode Execute( Interpreter interpreter, IClientData clientData, ArgumentList arguments, ref Argument value, ref Result error ) { ReturnCode code = ReturnCode.Ok; if (interpreter != null) { if (arguments != null) { if (arguments.Count == (this.Arguments + 1)) { Variant variant1 = null; code = Value.GetVariant(interpreter, (IGetValue)arguments[1], ValueFlags.AnyVariant, interpreter.CultureInfo, ref variant1, ref error); if (code == ReturnCode.Ok) { try { if (variant1.IsDouble()) { value = Interpreter.FixIntermediatePrecision( Math.Ceiling((double)variant1.Value)); } else if (variant1.IsDecimal() || variant1.ConvertTo(typeof(decimal))) { value = Interpreter.FixIntermediatePrecision( Math.Ceiling((decimal)variant1.Value)); } else { error = String.Format( "unsupported variant type for function \"{0}\"", base.Name); code = ReturnCode.Error; } } catch (Exception e) { Engine.SetExceptionErrorCode(interpreter, e); error = String.Format( "caught math exception: {0}", e); code = ReturnCode.Error; } } } else { if (arguments.Count > (this.Arguments + 1)) { error = String.Format( "too many arguments for math function \"{0}\"", base.Name); } else { error = String.Format( "too few arguments for math function \"{0}\"", base.Name); } code = ReturnCode.Error; } } else { error = "invalid argument list"; code = ReturnCode.Error; } } else { error = "invalid interpreter"; code = ReturnCode.Error; } return(code); }
/////////////////////////////////////////////////////////////////////////////////////////////// #region IExecuteArgument Members public override ReturnCode Execute( Interpreter interpreter, IClientData clientData, ArgumentList arguments, ref Argument value, ref Result error ) { ReturnCode code = ReturnCode.Ok; if (interpreter != null) { if (arguments != null) { try { string name = null; Variant operand1 = null; Variant operand2 = null; code = Value.GetOperandsFromArguments(interpreter, this, arguments, ValueFlags.AnyVariant, interpreter.CultureInfo, ref name, ref operand1, ref operand2, ref error); if (code == ReturnCode.Ok) { code = Value.FixupVariants( this, operand1, operand2, null, null, false, false, ref error); if (code == ReturnCode.Ok) { if (operand1.IsDouble()) { value = Interpreter.FixIntermediatePrecision( Math.Pow((double)operand1.Value, (double)operand2.Value)); } else if (operand1.IsDecimal()) { if (operand1.ConvertTo(typeof(double))) { if (operand2.ConvertTo(typeof(double))) { value = Interpreter.FixIntermediatePrecision( Math.Pow((double)operand1.Value, (double)operand2.Value)); } else { error = String.Format( "could not convert \"{0}\" to double", operand2.Value); code = ReturnCode.Error; } } else { error = String.Format( "could not convert \"{0}\" to double", operand1.Value); code = ReturnCode.Error; } } else if (operand1.IsWideInteger()) { value = MathOps.Pow((long)operand1.Value, (long)operand2.Value); } else if (operand1.IsInteger()) { value = MathOps.Pow((int)operand1.Value, (int)operand2.Value); } else if (operand1.IsBoolean()) { value = MathOps.Pow(ConversionOps.ToInt((bool)operand1.Value), ConversionOps.ToInt((bool)operand2.Value)); } else { error = String.Format( "unsupported operand type for operator {0}", FormatOps.OperatorName(name, this.Lexeme)); code = ReturnCode.Error; } } } } catch (Exception e) { Engine.SetExceptionErrorCode(interpreter, e); error = String.Format( "caught math exception: {0}", e); code = ReturnCode.Error; } } else { error = "invalid argument list"; code = ReturnCode.Error; } } else { error = "invalid interpreter"; code = ReturnCode.Error; } return(code); }