Expr Logical(DynamicMetaObject left, DynamicMetaObject right) { // Assign left operand to a temp variable for single evaluation var tempLeft = Expr.Variable(left.LimitType); var compareExpr = (Expr)tempLeft; Expr ifExpr = null; switch (Operation) { case ExprType.AndAlso: if (left.LimitType != typeof(bool)) { compareExpr = Expr.Equal(tempLeft, Expr.Constant(null)); } ifExpr = Expr.IfThenElse(compareExpr, right.Expression, tempLeft); break; case ExprType.OrElse: if (left.LimitType != typeof(bool)) { compareExpr = Expr.NotEqual(tempLeft, Expr.Constant(null)); } ifExpr = Expr.IfThenElse(compareExpr, tempLeft, right.Expression); break; } return (Expr.Block( new[] { tempLeft }, Expr.Assign(tempLeft, left.Expression), ifExpr)); }
Expr NotOp(DynamicMetaObject target) { if (target.LimitType == typeof(bool)) { return(Expr.MakeUnary(Operation, target.Expression, null)); } return(Expr.Equal(target.Expression, Expr.Constant(null))); }
public override System.Linq.Expressions.Expression GenerateLinqExpression(IASTContext context) { GenerateLinqExpresions(context, left, right, out LinqExpression expLeft, out LinqExpression expRight); if (IsNullable(expLeft.Type) && !IsNullable(expRight.Type)) { expRight = LinqExpression.MakeUnary(System.Linq.Expressions.ExpressionType.Convert, expRight, expLeft.Type); } else if (IsNullable(expRight.Type) && !IsNullable(expLeft.Type)) { expLeft = LinqExpression.MakeUnary(System.Linq.Expressions.ExpressionType.Convert, expLeft, expRight.Type); } switch (op) { case Operator.OP_ADD: return(LinqExpression.Add(expLeft, expRight)); case Operator.OP_SUB: return(LinqExpression.Subtract(expLeft, expRight)); case Operator.OP_DIV: return(LinqExpression.Divide(expLeft, expRight)); case Operator.OP_MUL: return(LinqExpression.Multiply(expLeft, expRight)); case Operator.OP_AND: return(LinqExpression.AndAlso(expLeft, expRight)); case Operator.OP_OR: return(LinqExpression.OrElse(expLeft, expRight)); case Operator.OP_EQUAL: return(LinqExpression.Equal(expLeft, expRight)); case Operator.OP_DISTINT: return(LinqExpression.NotEqual(expLeft, expRight)); case Operator.OP_LIKE: return(CreateLike(expLeft, expRight)); case Operator.OP_LESS: return(LinqExpression.LessThan(expLeft, expRight)); case Operator.OP_GREATHER: return(LinqExpression.GreaterThan(expLeft, expRight)); case Operator.OP_GREATHER_EQ: return(LinqExpression.GreaterThanOrEqual(expLeft, expRight)); case Operator.OP_LESS_EQ: return(LinqExpression.LessThanOrEqual(expLeft, expRight)); default: throw new InvalidOperationException("operator not supported"); } }
internal void SetRuleForCall(MetaObjectBuilder /*!*/ metaBuilder, CallArguments /*!*/ args) { Assert.NotNull(metaBuilder, args); Debug.Assert(args.Target == this); // TODO: we could compare infos here: // first argument must be this method: metaBuilder.AddRestriction(Ast.Equal(args.TargetExpression, Ast.Constant(this))); // set the target (becomes self in the called method): args.SetTarget(Ast.Constant(_target), _target); _info.BuildCall(metaBuilder, args, _name); }
private static LinqExpression GetPathExpression(AttributePath path, LinqExpression representationAttrExpr) { if (path == null) { throw new ArgumentNullException(nameof(path)); } if (representationAttrExpr == null) { throw new ArgumentNullException(nameof(representationAttrExpr)); } var schemaAttributeParameter = LinqExpression.Property(representationAttrExpr, "SchemaAttribute"); var propertyName = LinqExpression.Property(schemaAttributeParameter, "Name"); var notNull = LinqExpression.NotEqual(schemaAttributeParameter, LinqExpression.Constant(null)); var equal = LinqExpression.Equal(propertyName, LinqExpression.Constant(path.Name)); return(LinqExpression.AndAlso(notNull, equal)); }
public override DynamicMetaObject BindGetIndex(GetIndexBinder binder, DynamicMetaObject[] indexes) { var valueVar = Expr.Variable(typeof(object)); var getValue = Expr.Call( Expr.Convert(Expression, typeof(LuaTable)), MemberInfos.LuaTableGetValue, Expr.Convert(indexes[0].Expression, typeof(object))); var valueAssign = Expr.Assign(valueVar, getValue); var expression = Expr.Block( valueVar, Expr.Condition( Expr.Equal(valueVar, Expr.Constant(null)), MetamethodFallbacks.Index(null, this, indexes), valueVar)); return(new DynamicMetaObject(expression, RuntimeHelpers.MergeTypeRestrictions(this))); }
public void Query_Where_Negation_NotContains_EqualsCastedFalse() { //Construct an expression like s => !s.Contains("Vol") == (bool)false var parameter = Expr.Parameter(typeof(Sensor), "s"); var member = Expr.MakeMemberAccess(parameter, typeof(Sensor).GetProperty("Name")); var method = typeof(string).GetMethod("Contains", new[] { typeof(string) }); var methodCall = Expr.Call(member, method, Expr.Constant("Vol")); var notMethodCall = Expr.Not(methodCall); var boolVal = Expr.Constant(false); var cast = Expr.Convert(boolVal, typeof(bool)); var equals = Expr.Equal(notMethodCall, cast); var lambda = Expr.Lambda <Func <Sensor, bool> >( equals, parameter ); //Expression will be replaced by partial evaluator ExecuteFilter(lambda, "filter_name=@sub(Vol)", s => Assert.AreEqual(3, s.Count)); }
public override DynamicMetaObject BindSetIndex(SetIndexBinder binder, DynamicMetaObject[] indexes, DynamicMetaObject value) { var getValue = Expr.Call( Expr.Convert(Expression, typeof(LuaTable)), MemberInfos.LuaTableGetValue, Expr.Convert(indexes[0].Expression, typeof(object))); var setValue = Expr.Call( Expr.Convert(Expression, typeof(LuaTable)), MemberInfos.LuaTableSetValue, Expr.Convert(indexes[0].Expression, typeof(object)), Expr.Convert(value.Expression, typeof(object))); var expression = Expr.Condition( Expr.Equal(getValue, Expr.Constant(null)), MetamethodFallbacks.NewIndex(null, this, indexes, value), setValue); return(new DynamicMetaObject(expression, RuntimeHelpers.MergeTypeRestrictions(this))); }
private static LinqExpression BuildComparisonExpression(CompAttributeExpression compAttributeExpression, LinqExpression representationAttrExpr) { var argSchemaAttrValue = LinqExpression.Parameter(typeof(RepresentationAttributeValue), "rav"); var propertyValue = LinqExpression.Property(representationAttrExpr, "Value"); var propertySchemaAttribute = LinqExpression.Property(representationAttrExpr, "SchemaAttribute"); var propertySchemaType = LinqExpression.Property(propertySchemaAttribute, "Type"); var propertyMultiValued = LinqExpression.Property(propertySchemaAttribute, "MultiValued"); var propertyValues = LinqExpression.Property(representationAttrExpr, "Values"); var propertySchemaAttrValue = LinqExpression.Property(argSchemaAttrValue, "Value"); var equality = LinqExpression.Equal(propertySchemaAttrValue, LinqExpression.Constant(compAttributeExpression.Value)); var callEqualValue = LinqExpression.Lambda <Func <RepresentationAttributeValue, bool> >(equality, argSchemaAttrValue); var anyExpr = LinqExpression.Call(typeof(Enumerable), _anyMethodName, new[] { typeof(RepresentationAttributeValue) }, propertyValues, callEqualValue); var itemCount = LinqExpression.Call(typeof(Enumerable), "Count", new[] { typeof(RepresentationAttributeValue) }, propertyValues); var moreThanZero = LinqExpression.GreaterThan(itemCount, LinqExpression.Constant(0)); var isMultiValued = LinqExpression.Equal(propertyMultiValued, LinqExpression.Constant(true)); var isNotMultiValued = LinqExpression.Equal(propertyMultiValued, LinqExpression.Constant(false)); var equalBoolean = LinqExpression.Equal(propertySchemaType, LinqExpression.Constant(Common.Constants.SchemaAttributeTypes.Boolean)); var equalBinary = LinqExpression.Equal(propertySchemaType, LinqExpression.Constant(Common.Constants.SchemaAttributeTypes.Binary)); var equalString = LinqExpression.Equal(propertySchemaType, LinqExpression.Constant(Common.Constants.SchemaAttributeTypes.String)); var equalDatetime = LinqExpression.Equal(propertySchemaType, LinqExpression.Constant(Common.Constants.SchemaAttributeTypes.DateTime)); var equalDecimal = LinqExpression.Equal(propertySchemaType, LinqExpression.Constant(Common.Constants.SchemaAttributeTypes.Decimal)); var equalInteger = LinqExpression.Equal(propertySchemaType, LinqExpression.Constant(Common.Constants.SchemaAttributeTypes.Integer)); var firstEqualityStr = LinqExpression.OrElse(equalBoolean, equalBinary); var secondEqualityStr = LinqExpression.OrElse(firstEqualityStr, equalString); var firstEqualityFloat = LinqExpression.OrElse(equalDatetime, equalDecimal); var equalityFloat = LinqExpression.OrElse(firstEqualityFloat, equalInteger); var finalEqualityStr = LinqExpression.OrElse(equalityFloat, secondEqualityStr); var containsStr = LinqExpression.AndAlso(moreThanZero, anyExpr); var equalityMultipleStr = LinqExpression.AndAlso(isMultiValued, containsStr); var contains = typeof(string).GetMethod("Contains"); var containsEqualValue = LinqExpression.Call(propertyValue, contains, LinqExpression.Constant(compAttributeExpression.Value)); var equalityStr = LinqExpression.AndAlso(isNotMultiValued, containsEqualValue); var checkStrMultiValued = LinqExpression.AndAlso(finalEqualityStr, equalityMultipleStr); var checkContainsStr = LinqExpression.AndAlso(finalEqualityStr, equalityStr); return(LinqExpression.OrElse(checkStrMultiValued, checkContainsStr)); }
static ReadDelegate GenerateTestFlagsMethod() { ////////////////////////////////////////////////////////////////////////// // Define the generated method's parameters and return constructs var param_v = GenerateParamValue(false); var param_f = GenerateParamFlags(); ////////////////////////////////////////////////////////////////////////// // return (value & flags) == flags var param_v_member = Expr.PropertyOrField(param_v, EnumUtils.kMemberName); var param_f_member = Expr.PropertyOrField(param_f, EnumUtils.kMemberName); var and = Expr.And(param_v_member, param_f_member); var equ = Expr.Equal(and, param_f_member); ////////////////////////////////////////////////////////////////////////// // Generate a method based on the expression tree we've built var lambda = Expr.Lambda <ReadDelegate>(equ, param_v, param_f); return(lambda.Compile()); }
static ReadDelegate GenerateTestFlagsMethod() { ////////////////////////////////////////////////////////////////////////// // Define the generated method's parameters and return constructs var param_v = GenerateParamValue(false); var param_f = GenerateParamFlags(); ////////////////////////////////////////////////////////////////////////// // return (value & flags) == flags var v_as_int = Expr.Convert(param_v, kUnderlyingType); var f_as_int = Expr.Convert(param_f, kUnderlyingType); var and = Expr.Convert(Expr.And(v_as_int, f_as_int), kEnumType); var equ = Expr.Equal(and, param_f); ////////////////////////////////////////////////////////////////////////// // Generate a method based on the expression tree we've built var lambda = Expr.Lambda <ReadDelegate>(equ, param_v, param_f); return(lambda.Compile()); }
public LinqExpression CreateLike(LinqExpression expleft, LinqExpression expright) { LiteralExpression lit = (LiteralExpression)right; var value = lit.Value; if (string.IsNullOrWhiteSpace(value)) { return(LinqExpression.Equal(expleft, expright)); } MethodInfo method = null; string compareValue = value; if (value[0] == '%' && value[value.Length - 1] == '%') { method = typeof(string).GetMethod("Contains", new Type[] { typeof(string) }); compareValue = value.Substring(1, value.Length - 2); } else if (value[0] == '%') { method = typeof(String).GetMethod("EndsWith", new Type[] { typeof(String) }); compareValue = value.Substring(1, value.Length - 1); } else if (value[value.Length - 1] == '%') { method = typeof(String).GetMethod("StartsWith", new Type[] { typeof(String) }); compareValue = value.Substring(0, value.Length - 1); } if (method != null) { return(LinqExpression.Call(expleft, method, LinqExpression.Constant(compareValue, typeof(String)))); } return(LinqExpression.Equal(expleft, expright)); }
private static LinqExpression GetComparisonExpression(CompAttributeExpression compAttributeExpression, LinqExpression representationAttrExpr) { if (compAttributeExpression == null) { throw new ArgumentNullException(nameof(compAttributeExpression)); } if (representationAttrExpr == null) { throw new ArgumentNullException(nameof(representationAttrExpr)); } var propertySchemaAttribute = LinqExpression.Property(representationAttrExpr, "SchemaAttribute"); var propertySchemaType = LinqExpression.Property(propertySchemaAttribute, "Type"); var propertyValue = LinqExpression.Property(representationAttrExpr, "Value"); var propertyValueNumber = LinqExpression.Property(representationAttrExpr, "ValueNumber"); LinqExpression equalValue = null; switch (compAttributeExpression.Operator) { case ComparisonOperators.eq: equalValue = LinqExpression.Equal(propertyValue, LinqExpression.Constant(compAttributeExpression.Value)); break; case ComparisonOperators.pr: equalValue = LinqExpression.NotEqual(propertyValue, LinqExpression.Constant(null)); break; case ComparisonOperators.co: equalValue = BuildComparisonExpression(compAttributeExpression, representationAttrExpr); break; case ComparisonOperators.gt: equalValue = LinqExpression.GreaterThan(propertyValueNumber, LinqExpression.Constant(double.Parse(compAttributeExpression.Value))); break; case ComparisonOperators.ge: equalValue = LinqExpression.GreaterThanOrEqual(propertyValueNumber, LinqExpression.Constant(double.Parse(compAttributeExpression.Value))); break; case ComparisonOperators.le: equalValue = LinqExpression.LessThanOrEqual(propertyValueNumber, LinqExpression.Constant(double.Parse(compAttributeExpression.Value))); break; case ComparisonOperators.lt: equalValue = LinqExpression.LessThan(propertyValueNumber, LinqExpression.Constant(double.Parse(compAttributeExpression.Value))); break; case ComparisonOperators.sw: var startWith = typeof(string).GetMethod("StartsWith"); equalValue = LinqExpression.Call(propertyValue, startWith, LinqExpression.Constant(compAttributeExpression.Value)); break; case ComparisonOperators.ew: var endsWith = typeof(string).GetMethod("EndsWith"); equalValue = LinqExpression.Call(propertyValue, endsWith, LinqExpression.Constant(compAttributeExpression.Value)); break; } return(equalValue); }
protected override LExpression GetExpressionTreeIfPossible( LExpression contextExpression, LExpression evalContext) { var leftExpression = GetExpressionTreeIfPossible(Left, contextExpression, evalContext); var rightExpression = GetExpressionTreeIfPossible(Right, contextExpression, evalContext); if (leftExpression == null || rightExpression == null) { return(null); } var res = CreateBinaryExpressionForAllNumericTypesForNotNullChildren( leftExpression, rightExpression, LExpression.Equal); if (res != null) { return(res); } if (leftExpression.Type == typeof(bool) && rightExpression.Type == typeof(bool)) { return(LExpression.Equal(leftExpression, rightExpression)); } if (leftExpression.Type == typeof(string) || rightExpression.Type == typeof(string)) { return(LExpression.Equal(leftExpression, rightExpression)); } if (leftExpression.Type == typeof(DateTime) && rightExpression.Type == typeof(DateTime)) { return(LExpression.Equal(leftExpression, rightExpression)); } // TODO: upewniæ siê, ¿e to dzia³a (dla wybranych typów) tak samo jak interpretacja! //TODO: brak obs³ugi .. czy charów... czy innych takich! to samo przy Less i innych operatorach! // todo: g³upie jest to, i¿ mo¿e to nie zadzia³aæ dla boxowanych typów... oto jest pytanie... // todo: mo¿e nigdy nie powiniœmy eqlals jednak u¿ywaæ... do zastanowienia siê... if (leftExpression.Type.IsValueType) { leftExpression = LExpression.Convert(leftExpression, typeof(object)); } if (rightExpression.Type.IsValueType) { rightExpression = LExpression.Convert(rightExpression, typeof(object)); } return(LExpression.Condition( LExpression.Equal(leftExpression, LExpression.Constant(null, typeof(object))), // left is null - emitting (rigth == null) LExpression.Equal(rightExpression, LExpression.Constant(null, typeof(object))), // left is not null - checking right LExpression.Condition( LExpression.Equal(rightExpression, LExpression.Constant(null, typeof(object))), // left not null; right is null => false LExpression.Constant(false, typeof(bool)), // left not null; right not null => emitting left.Equals(right) LExpression.Call(leftExpression, objEqualsMi, rightExpression) ) )); }
private static LinqExpression EvaluateChildren(AttributePath path, System.Linq.Expressions.ParameterExpression arg, Func <System.Linq.Expressions.ParameterExpression, LinqExpression> callback = null) { if (path == null) { throw new ArgumentNullException(nameof(path)); } if (arg == null) { throw new ArgumentNullException(nameof(arg)); } var subArg = LinqExpression.Parameter(typeof(RepresentationAttribute), GetSc()); var subChild = LinqExpression.Parameter(typeof(RepresentationAttribute), GetSc()); var childrenProperty = LinqExpression.Property(arg, "Children"); var argChildrenProperty = LinqExpression.Property(subArg, "Children"); var schemaAttributeProperty = LinqExpression.Property(arg, "SchemaAttribute"); var schemaTypeProperty = LinqExpression.Property(schemaAttributeProperty, "Type"); var schemaMultiValuedProperty = LinqExpression.Property(schemaAttributeProperty, "MultiValued"); var isMultiValued = LinqExpression.Equal(schemaMultiValuedProperty, LinqExpression.Constant(true)); // true var isNotMultiValued = LinqExpression.Equal(schemaMultiValuedProperty, LinqExpression.Constant(false)); var isTypeComplex = LinqExpression.Equal(schemaTypeProperty, LinqExpression.Constant(Common.Constants.SchemaAttributeTypes.Complex)); var isNotTypeComplex = LinqExpression.NotEqual(schemaTypeProperty, LinqExpression.Constant(Common.Constants.SchemaAttributeTypes.Complex)); var isComplexMultiValued = LinqExpression.AndAlso(isMultiValued, isTypeComplex); var isNotComplexMultiValued = LinqExpression.OrElse(isNotTypeComplex, isNotMultiValued); var itemCount = LinqExpression.Call(typeof(Enumerable), "Count", new[] { typeof(RepresentationAttribute) }, childrenProperty); var moreThanZero = LinqExpression.GreaterThan(itemCount, LinqExpression.Constant(0)); LinqExpression result = null; if (callback != null) { result = callback(subChild); } if (path.Next != null) { var subCond = path.Next.Evaluate(subChild); if (result != null) { result = LinqExpression.AndAlso(result, subCond); } else { result = subCond; } } // a => aaze = azeaze var callEqualValue = LinqExpression.Lambda <Func <RepresentationAttribute, bool> >(result, subChild); // c => c.value = <value> var anyComplexMultiValuedSubAnyExpr = LinqExpression.Call(typeof(Enumerable), _anyMethodName, new[] { typeof(RepresentationAttribute) }, argChildrenProperty, callEqualValue); var lambdaMultiValuedSubAnyExpr = LinqExpression.Lambda <Func <RepresentationAttribute, bool> >(anyComplexMultiValuedSubAnyExpr, subArg); var anyComplexMultiValuedAnyExpr = LinqExpression.Call(typeof(Enumerable), _anyMethodName, new[] { typeof(RepresentationAttribute) }, childrenProperty, lambdaMultiValuedSubAnyExpr); var anyNotComplexMultiValuedExpr = LinqExpression.Call(typeof(Enumerable), _anyMethodName, new[] { typeof(RepresentationAttribute) }, childrenProperty, callEqualValue); var firstNotComplexMultiValued = LinqExpression.AndAlso(isNotComplexMultiValued, moreThanZero); var notComplexMultiValued = LinqExpression.AndAlso(firstNotComplexMultiValued, anyNotComplexMultiValuedExpr); var firstComplexMultiValued = LinqExpression.AndAlso(isComplexMultiValued, moreThanZero); var complexMultiValued = LinqExpression.AndAlso(firstComplexMultiValued, anyComplexMultiValuedAnyExpr); return(LinqExpression.OrElse(complexMultiValued, notComplexMultiValued)); }
public static tbool Eq(tfloat b1, tfloat b2) => Ex.Equal(b1, b2);
private static Expression ScanOperatorToExpression(ScanOperator scanOperator, Expression operand1, Expression operand2) { switch (scanOperator) { case ScanOperator.Equal: return(Expression.Equal(operand1, operand2)); case ScanOperator.NotEqual: return(Expression.NotEqual(operand1, operand2)); case ScanOperator.LessThan: if (operand1.Type == typeof(string)) { return(Expression.LessThan(Expression.Call(StringCompareMethodInfo, operand1, operand2, Expression.Constant(StringComparison.Ordinal)), Expression.Constant(0))); } return(Expression.LessThan(operand1, operand2)); case ScanOperator.LessThanOrEqual: if (operand1.Type == typeof(string)) { return(Expression.LessThanOrEqual(Expression.Call(StringCompareMethodInfo, operand1, operand2, Expression.Constant(StringComparison.Ordinal)), Expression.Constant(0))); } return(Expression.LessThanOrEqual(operand1, operand2)); case ScanOperator.GreaterThan: if (operand1.Type == typeof(string)) { return(Expression.GreaterThan(Expression.Call(StringCompareMethodInfo, operand1, operand2, Expression.Constant(StringComparison.Ordinal)), Expression.Constant(0))); } return(Expression.GreaterThan(operand1, operand2)); case ScanOperator.GreaterThanOrEqual: if (operand1.Type == typeof(string)) { return(Expression.GreaterThanOrEqual(Expression.Call(StringCompareMethodInfo, operand1, operand2, Expression.Constant(StringComparison.Ordinal)), Expression.Constant(0))); } return(Expression.GreaterThanOrEqual(operand1, operand2)); case ScanOperator.BeginsWith: if (operand1.Type == typeof(string)) { return(Expression.Call(operand1, StartsWithMethodInfo, operand2)); } break; case ScanOperator.Contains: if (operand1.Type == typeof(string)) { return(Expression.Call(operand1, ContainsMethodInfo, operand2)); } break; case ScanOperator.NotContains: if (operand1.Type == typeof(string)) { return(Expression.Not(Expression.Call(operand1, ContainsMethodInfo, operand2))); } break; } throw new NotSupportedException(string.Format("Condition operator {0} is not supported", scanOperator)); }
public static Expression Equal(Expression arg0, Expression arg1) { return(new Expression( LinqExpression.Equal(arg0, arg1))); }
/// <summary> /// If the switcher is 1, return the result, otherwise the default value. /// </summary> public static TEx <T> If1 <T>(tfloat switcher, TEx <T> result) => Ex.Condition(Ex.Equal(switcher, E1), result, Ex.Default(typeof(T)));