public IValue Middle(IValue before, IValue after) { var beforeType = before.ValueType; var afterType = after.ValueType; if (beforeType == ValueTypes.String || afterType == ValueTypes.String) { return(new Constant(before.GetString() + after.GetString())); } if (beforeType == ValueTypes.Number || afterType == ValueTypes.Number) { return(new Constant(before.GetNumber() + after.GetNumber())); } if (beforeType == ValueTypes.Float || afterType == ValueTypes.Float) { return(new Constant(before.GetFloat() + after.GetFloat())); } if (beforeType == ValueTypes.Integer || afterType == ValueTypes.Integer) { return(new Constant(before.GetInteger() + after.GetInteger())); } throw new Exception(StringHelper.Format("{0} and {1} cannot {2}.", beforeType.ToString(), afterType.ToString(), Code)); }
public override long GetInteger() { try { return(m_pVal.GetInteger()); } catch (ParserError exc) { exc.GetContext().Ident = GetIdent(); throw; } }
public override void Eval(ref IValue ret, IValue[] a_pArg) { Global.MUP_VERIFY(a_pArg.Length == 2); IValue arg1 = a_pArg[0]; IValue arg2 = a_pArg[1]; if (arg1.IsNonComplexScalar() || arg2.IsNonComplexScalar()) { if (!arg1.IsNonComplexScalar()) { throw new ParserError(new ErrorContext(EErrorCodes.ecTYPE_CONFLICT_FUN, -1, GetIdent(), arg1.GetValueType(), 'f', 1)); } if (!arg2.IsNonComplexScalar()) { throw new ParserError(new ErrorContext(EErrorCodes.ecTYPE_CONFLICT_FUN, -1, GetIdent(), arg2.GetValueType(), 'f', 2)); } if (arg1.IsInteger() && arg2.IsInteger()) { ret = arg1.GetInteger() - arg2.GetInteger(); } else { ret = arg1.GetFloat() - arg2.GetFloat(); } } else //if (arg1.IsMatrix() && arg2.IsMatrix()) { // Vector + Vector //Matrix a1 = arg1.GetArray(), a2 = arg2.GetArray(); //if (a1.GetRows() != a2.GetRows()) // throw new ParserError(new ErrorContext(EErrorCodes.ecARRAY_SIZE_MISMATCH, -1, GetIdent(), 'm', 'm', 2)); //var rv = new Matrix(a1.GetRows()); //for (int i = 0; i < a1.GetRows(); ++i) //{ // if (!a1.At(i).IsNonComplexScalar()) // throw new ParserError(new ErrorContext(EErrorCodes.ecTYPE_CONFLICT_FUN, -1, GetIdent(), a1.At(i).GetValueType(), 'f', 1)); // if (!a2.At(i).IsNonComplexScalar()) // throw new ParserError(new ErrorContext(EErrorCodes.ecTYPE_CONFLICT_FUN, -1, GetIdent(), a2.At(i).GetValueType(), 'f', 1)); // if (a1.At(i).IsInteger() && a2.At(i).IsInteger()) // rv.At(i) = a1.At(i).GetInteger() - a2.At(i).GetInteger(); // else // rv.At(i) = a1.At(i).GetFloat() - a2.At(i).GetFloat(); //} ret = arg1 - arg2; } }
public IValue Before(IValue after) { switch (after.ValueType) { case ValueTypes.Integer: return(new Constant(-after.GetInteger())); case ValueTypes.Float: return(new Constant(-after.GetFloat())); case ValueTypes.Number: return(new Constant(-after.GetNumber())); default: throw new Exception("- 无法应用于" + after.ValueType); } }
public long GetInteger() { return(value.GetInteger()); }
public override void Eval(ref IValue ret, IValue[] a_pArg) { Global.MUP_VERIFY(a_pArg.Length == 2); IValue arg1 = a_pArg[0]; IValue arg2 = a_pArg[1]; if (!arg1.IsNonComplexScalar()) { throw new ParserError(new ErrorContext(EErrorCodes.ecTYPE_CONFLICT_FUN, -1, GetIdent(), arg1.GetValueType(), 'f', 1)); } if (!arg2.IsNonComplexScalar()) { throw new ParserError(new ErrorContext(EErrorCodes.ecTYPE_CONFLICT_FUN, -1, GetIdent(), arg2.GetValueType(), 'f', 2)); } if (arg1.IsInteger() && arg2.IsInteger()) { var i1 = arg1.GetInteger(); var i2 = arg2.GetInteger(); switch (i2) { case 1: ret = i1; break; case 2: ret = i1 * i1; break; case 3: ret = i1 * i1 * i1; break; case 4: ret = i1 * i1 * i1 * i1; break; case 5: ret = i1 * i1 * i1 * i1 * i1; break; default: ret = (long)Math.Pow(i1, i2); break; } } else { var i1 = arg1.GetFloat(); var i2 = arg2.GetFloat(); switch (i2) { case 1: ret = i1; break; case 2: ret = i1 * i1; break; case 3: ret = i1 * i1 * i1; break; case 4: ret = i1 * i1 + i1 * i1; break; case 5: ret = i1 * i1 * i1 * i1 * i1; break; default: ret = Math.Pow(i1, i2); break; } } }