/// <summary> /// Adds a new function for the given types. /// </summary> /// <param name="param">The type of the operand.</param> /// <param name="func">The function that handles this operand.</param> /// <param name="replaceChildren"> /// Iff <c>true</c>, the functions that handle types more derived than /// <c>param</c> should be removed from this operator. /// </param> public void AddFunction(Type param, CLUnaryOperatorFunc func, bool replaceChildren = true) { Functions[param] = func; // Now replace all the child types if necessary. if (replaceChildren) { foreach (Type paramTest in Functions.Keys) { if (paramTest.IsSubclassOf(param)) { Functions.Remove(param); } } } }
/// <summary>Runs the Operator on two operands.</summary> /// <param name="param">The right operand.</param> /// <param name="vars">The local variable storage.</param> /// <param name="context">An object representing context.</param> public CalcValue Run(CalcObject param, CLLocalStore vars = null, CLContextProvider context = null) { // If the operator is value-based, we'll automatically convert expressions. if (ValueBased) { param = param.GetValue(vars, context); } // Now get the func. CLUnaryOperatorFunc func = this[param.GetType()]; // If it's null, we'll throw an exception. if (func == null) { throw new CLException( "Binary operator " + Symbol + " doesn't support parameter " + param.GetType().Name ); } // Now let's run it. return(func(param, vars, context)); }