public static bool ApplyUrnaryOperator(IEnumerable <Operator> ops, UrnaryOperatorType opType, object first, out object success, out ParseException failure) { Validate.NonNull(ops, nameof(ops)); Validate.NonNull(opType, nameof(opType)); Validate.NonNull(first, nameof(first)); bool worked = ApplyUrnaryOperators( FindUrnaryOperators( ops, opType, first.GetType() ), first, out success ); if (!worked) { failure = ParseException.UrnaryOperatorEvaluationFailed(opType.GetSymbolString(), first.ToString()); return(false); } else { failure = null; return(true); } }
public static string GetSymbolString(this UrnaryOperatorType type) { switch (type) { case UrnaryOperatorType.Negation: return("-"); case UrnaryOperatorType.Positation: return("+"); default: return(string.Empty); } }
private static IEnumerable <UrnaryOperator> FindUrnaryOperators(IEnumerable <Operator> operators, UrnaryOperatorType type, Type t1) { Validate.NonNull(operators, nameof(operators)); Validate.NonNull(type, nameof(type)); Validate.NonNull(t1, nameof(t1)); var ops = operators .Where(x => x is UrnaryOperator) .Select(x => (UrnaryOperator)x) .Where(x => x.Type == type && x.InputType == t1); return(ops); }
public UrnaryOperator(Type input, UrnaryOperatorType type, Func <object, object> eval) { this.Evaluate = eval; this.Type = type; this.InputType = input; }