/// <summary> /// Visits the children of the <see cref="T:System.Linq.Expressions.BinaryExpression"/>. /// </summary> /// <returns> /// The modified expression, if it or any subexpression was modified; otherwise, returns the original expression. /// </returns> /// <param name="node">The expression to visit.</param> protected override Expression VisitBinary(BinaryExpression node) { var op = node.GetOperator(); Expression left = node.Left; Expression right = node.Right; _serverWriter.OpenBrace(); if (left.Type.IsBoolean()) { Visit(left); _serverWriter.WhiteSpace(); _serverWriter.Write(op); _serverWriter.WhiteSpace(); Visit(right); } else { VisitValue(left); _serverWriter.WhiteSpace(); _serverWriter.Write(op); _serverWriter.WhiteSpace(); VisitValue(right); } _serverWriter.CloseBrace(); return(node); }
BinaryOperator getBinaryOperator(Token t) { if (t.Type != TokenType.Operator) { return(BinaryOperator.None); } return(BinaryExpression.GetOperator(t.Content)); }
private static void GetConditional(this BinaryExpression binaryExpression, StringBuilder builder, string tableName) { var memberExpression = (MemberExpression)binaryExpression.Left; var constantExpression = (ConstantExpression)binaryExpression.Right; var propertyName = memberExpression.GetPropertyName(tableName); var operatorFilter = binaryExpression.GetOperator(); var filter = constantExpression.GetConstant(); builder.Append($"{propertyName} {operatorFilter} {filter}"); }
public IntExpression(IntAttribute property, ValueOperator op, PropertyType value) { if (value == null) { throw new ArgumentNullException("value"); } if (value.DataType != DataType.Int) { throw GetWrongPropertyDataTypeException("value", DataType.Int); } _binExp = new BinaryExpression((NodeAttribute)property, BinaryExpression.GetOperator(op), value); }
public IntExpression(PropertyType property, ValueOperator op, IntAttribute value) { if (property == null) { throw new ArgumentNullException("property"); } if (property.DataType != DataType.Int) { throw GetWrongPropertyDataTypeException("property", DataType.Int); } _binExp = new BinaryExpression(property, BinaryExpression.GetOperator(op), BinaryExpression.GetNodeAttribute(value)); }
//-- There are not any NodeAttribute with Currency datatype public CurrencyExpression(PropertyType property, ValueOperator op, decimal?value) { if (property == null) { throw new ArgumentNullException("property"); } if (property.DataType != DataType.Currency) { throw GetWrongPropertyDataTypeException("property", DataType.Currency); } _binExp = new BinaryExpression(property, BinaryExpression.GetOperator(op), value); }
public DateTimeExpression(PropertyType property, ValueOperator op, DateTimeAttribute value) { if (property == null) { throw new ArgumentNullException("property"); } if (property.DataType != DataType.DateTime) { throw new ArgumentOutOfRangeException("property", "The DataType of 'property' must be DataType.DateTime"); } _binExp = new BinaryExpression(property, BinaryExpression.GetOperator(op), BinaryExpression.GetNodeAttribute(value)); }
public StringExpression(PropertyType property, StringOperator op, string value) { if (property == null) { throw new ArgumentNullException("property"); } if (property.DataType != DataType.String) { throw GetWrongPropertyDataTypeException("property", DataType.String); } _binExp = new BinaryExpression(property, BinaryExpression.GetOperator(op), EscapeValue(value)); }
public DateTimeExpression(PropertyType property, ValueOperator op, PropertyType value) { if (property == null) { throw new ArgumentNullException("property"); } if (property.DataType != DataType.DateTime) { throw GetWrongPropertyDataTypeException("property", DataType.DateTime); } if (value == null) { throw new ArgumentNullException("value"); } if (value.DataType != DataType.DateTime) { throw GetWrongPropertyDataTypeException("value", DataType.DateTime); } _binExp = new BinaryExpression(property, BinaryExpression.GetOperator(op), value); }
public IntExpression(IntAttribute property, ValueOperator op, IntAttribute value) { _binExp = new BinaryExpression((NodeAttribute)property, BinaryExpression.GetOperator(op), BinaryExpression.GetNodeAttribute(value)); }
private static string ExpressionTransform(this Expression expression) { StringBuilder result = new StringBuilder(); var candidates = new Queue <Operation>(new[] { new Operation { Expression = expression } }); while (candidates.Count > 0) { MemberExpression member = null; ConstantExpression constant = null; BinaryExpression binary = null; var ope = candidates.Dequeue(); // Console.WriteLine(ope.Expression); if (ope.Expression is BinaryExpression) { binary = ope.Expression as BinaryExpression; if (binary.Left is MemberExpression && binary.Right is ConstantExpression) { // like n => n.PartitionKey == "42" member = binary.Left as MemberExpression; constant = binary.Right as ConstantExpression; result.Insert(0, string.Format("{0} ({1} {2} {3}) ", ope.TableOperand.GetTableOperand(), member.Member.Name, binary.GetOperator(), constant.FormatConstantExpression()).TrimStart()); } else if (binary.Left is ConstantExpression && binary.Right is MemberExpression) { // like n => "42" == n.PartitionKey member = binary.Right as MemberExpression; constant = binary.Left as ConstantExpression; result.Insert(0, string.Format("{0} ({1} {2} {3}) ", ope.TableOperand.GetTableOperand(), member.Member.Name, binary.GetOperator(true), constant.FormatConstantExpression()).TrimStart()); } else if (binary.Left is BinaryExpression && binary.Right is BinaryExpression) { // like n => n.PartitionKey == "42" && n.RowKey == 42 candidates.Enqueue(new Operation() { Expression = binary.Right, TableOperand = binary.NodeType }); candidates.Enqueue(new Operation() { Expression = binary.Left }); } else if (binary.Left is MemberExpression && binary.Right is MemberExpression) { try { // like n => n.Date = DateTime.Now member = binary.Left as MemberExpression; result.Insert(0, string.Format("{0} ({1} {2} {3}) ", ope.TableOperand.GetTableOperand(), member.Member.Name, binary.GetOperator(), binary.Right.EvaluateExpression()).TrimStart()); } catch { // like n => DateTime.Now = n.Date member = binary.Right as MemberExpression; result.Insert(0, string.Format("{0} ({1} {2} {3}) ", ope.TableOperand.GetTableOperand(), member.Member.Name, binary.GetOperator(true), binary.Left.EvaluateExpression()).TrimStart()); } } else if (binary.Left is MemberExpression && binary.Right is NewExpression) { // like n => n.Date = new DateTime(2012, 12, 21); member = binary.Left as MemberExpression; result.Insert(0, string.Format("{0} ({1} {2} {3}) ", ope.TableOperand.GetTableOperand(), member.Member.Name, binary.GetOperator(), binary.Right.EvaluateExpression()).TrimStart()); } else if (binary.Left is NewExpression && binary.Right is MemberExpression) { // like n => new DateTime(2012, 12, 21) == n.Date; member = binary.Right as MemberExpression; result.Insert(0, string.Format("{0} ({1} {2} {3}) ", ope.TableOperand.GetTableOperand(), member.Member.Name, binary.GetOperator(true), binary.Left.EvaluateExpression()).TrimStart()); } else if (binary.Left is MemberExpression && binary.Right is MethodCallExpression) { // like n => n.UniqueIdentifier == Guid.Parse("52317684-641D-40C0-86C7-9B57DF97AC7F") member = binary.Left as MemberExpression; result.Insert(0, string.Format("{0} ({1} {2} {3}) ", ope.TableOperand.GetTableOperand(), member.Member.Name, binary.GetOperator(), binary.Right.EvaluateExpression()).TrimStart()); } else if (binary.Left is MethodCallExpression && binary.Right is MemberExpression) { // like n => n.UniqueIdentifier == Guid.Parse("52317684-641D-40C0-86C7-9B57DF97AC7F") member = binary.Right as MemberExpression; result.Insert(0, string.Format("{0} ({1} {2} {3}) ", ope.TableOperand.GetTableOperand(), member.Member.Name, binary.GetOperator(true), binary.Left.EvaluateExpression()).TrimStart()); } } else if (ope.Expression is MemberExpression) { // like n => n.IsEnabled member = ope.Expression as MemberExpression; result.Insert(0, string.Format("{0} ({1} eq true) ", ope.TableOperand.GetTableOperand(), member.Member.Name).TrimStart()); } else if (ope.Expression is UnaryExpression) { var unary = ope.Expression as UnaryExpression; // like n => !n.IsEnabled if (unary.Operand is MemberExpression) { member = unary.Operand as MemberExpression; result.Insert(0, string.Format("{0} ({1} eq false) ", ope.TableOperand.GetTableOperand(), member.Member.Name).TrimStart()); } } } return(result.ToString().Trim()); }
public DateTimeExpression(DateTimeAttribute property, ValueOperator op, DateTimeAttribute value) { _binExp = new BinaryExpression((NodeAttribute)property, BinaryExpression.GetOperator(op), BinaryExpression.GetNodeAttribute(value)); }
// given a left and right side of an equation, a Steps object, and if the unknown is time (for special cases) public static void solveEquation(bool isTime, Steps showYourWork, Expression l, Expression r, int unknownQuantityIndex) { //write down current step of equation showYourWork.AddStep(l.ToString() + " = " + r.ToString()); //if the unknown is on the right side, then flip them Expression rightSide = null; Expression leftSide = null; if (l.GetIsKnown()) { // leftSide is known, rightSide is NOT known leftSide = r; rightSide = l; } else if (r.GetIsKnown()) { // leftSide is unknown, right is (leave the equation as is) leftSide = l; rightSide = r; } else { //if there are variables on both sides, then something went wrong throw new System.ArgumentException("ERROR: Both sides of equation contain an unknown value"); } //add step to work showYourWork.AddStep(leftSide + " = " + rightSide.Evaluate()); //essentially does algebra until the variable is isolated on one side while (leftSide is BinaryExpression) { BinaryExpression binaryLeftSide = (BinaryExpression)leftSide; char expressionOperator = binaryLeftSide.GetOperator(); bool isOperand1Known; Expression knownExpression; // change the left side by taking the unknown branch and isolating it if (binaryLeftSide.GetOperand1().GetIsKnown()) { knownExpression = binaryLeftSide.GetOperand1(); isOperand1Known = true; leftSide = ((BinaryExpression)leftSide).GetOperand2(); } else if (binaryLeftSide.GetOperand2().GetIsKnown()) { knownExpression = binaryLeftSide.GetOperand2(); isOperand1Known = false; leftSide = ((BinaryExpression)leftSide).GetOperand1(); } else { throw new System.ArgumentException("ERROR: Neither expression in the leftside contains a known value"); } // Change right side in accordance with what was removed from the left side if (expressionOperator == '+') { rightSide = new BinaryExpression(rightSide, knownExpression, '-'); } else if (expressionOperator == '-') { if (isOperand1Known) { rightSide = new BinaryExpression(knownExpression, rightSide, '-'); } else { rightSide = new BinaryExpression(rightSide, knownExpression, '+'); } } else if (expressionOperator == '*') { rightSide = new BinaryExpression(rightSide, knownExpression, '/'); } else if (expressionOperator == '/') { if (isOperand1Known) { rightSide = new BinaryExpression(knownExpression, rightSide, '/'); } else { rightSide = new BinaryExpression(rightSide, knownExpression, '*'); } } else if (expressionOperator == '^' && !isOperand1Known) { float exponent = Mathf.Pow(knownExpression.Evaluate(), -1); UnaryExpression exponentExpression = new UnaryExpression(exponent.ToString()); rightSide = new BinaryExpression(rightSide, exponentExpression, '^'); } else { throw new System.ArgumentException("ERROR: Invalid operator " + expressionOperator); } showYourWork.AddStep(leftSide + " = " + rightSide.Evaluate()); showYourWork.SetAnswer(rightSide.Evaluate()); } //If we are solving for time, the answer cannot be negative if (rightSide.Evaluate() < 0 && isTime) { throw new System.ArgumentException("ERROR: Time cannot be negative"); } showYourWork.ReplaceLastValue(leftSide + " = " + rightSide.Evaluate() + " " + standardUnits[unknownQuantityIndex]); }
public StringExpression(StringAttribute property, StringOperator op, StringAttribute value) { _binExp = new BinaryExpression((NodeAttribute)property, BinaryExpression.GetOperator(op), BinaryExpression.GetNodeAttribute(value)); }