protected OperatorImplementation AddUnary(ExpressionType op, Type commonType, UnaryOperatorMethod unaryMethod) { var key = new OperatorDispatchKey(op, commonType); var impl = new OperatorImplementation(key, commonType, null, unaryMethod, null, null); OperatorImplementations[key] = impl; return(impl); }
protected OperatorImplementation AddConverter(Type fromType, Type toType, UnaryOperatorMethod method) { var key = new OperatorDispatchKey(ExpressionType.ConvertChecked, fromType, toType); var impl = new OperatorImplementation(key, toType, method); OperatorImplementations[key] = impl; return(impl); }
protected OperatorImplementation AddBinary(ExpressionType op, Type commonType, BinaryOperatorMethod binaryMethod, UnaryOperatorMethod resultConverter) { var key = new OperatorDispatchKey(op, commonType, commonType); var impl = new OperatorImplementation(key, commonType, binaryMethod, null, null, resultConverter); OperatorImplementations[key] = impl; return(impl); }
protected OperatorImplementation CreateBinaryOperatorImplementation(ExpressionType op, Type arg1Type, Type arg2Type, Type commonType, BinaryOperatorMethod method, UnaryOperatorMethod resultConverter) { OperatorDispatchKey key = new OperatorDispatchKey(op, arg1Type, arg2Type); UnaryOperatorMethod arg1Converter = arg1Type == commonType ? null : GetConverter(arg1Type, commonType); UnaryOperatorMethod arg2Converter = arg2Type == commonType ? null : GetConverter(arg2Type, commonType); var impl = new OperatorImplementation(key, commonType, method, arg1Converter, arg2Converter, resultConverter); return(impl); }
private static bool CanOverflow(OperatorImplementation impl) { if (!CanOverflow(impl.Key.Op)) { return(false); } if (impl.CommonType == typeof(double) || impl.CommonType == typeof(float)) { return(false); } return(true); }
public object ExecuteUnaryOperator(ExpressionType op, object arg1, ref OperatorImplementation previousUsed) { Type arg1Type; try { arg1Type = arg1.GetType(); } catch (NullReferenceException) { CheckUnassigned(arg1); throw; } OperatorDispatchKey key; var currentImpl = previousUsed; if (currentImpl != null && arg1Type != currentImpl.Key.Arg1Type) { currentImpl = null; } if (currentImpl == null) { key = new OperatorDispatchKey(op, arg1Type); if (!OperatorImplementations.TryGetValue(key, out currentImpl)) { throw new ScriptException($"Op(<{op}> <{arg1Type.Name}>) not defined!"); } } try { previousUsed = currentImpl; return(currentImpl.Arg1Converter(arg1)); } catch (OverflowException) { if (currentImpl.OverflowHandler == null) { throw; } previousUsed = currentImpl.OverflowHandler; return(ExecuteUnaryOperator(op, arg1, ref previousUsed)); } }