public override string ToSql(ExpressionSqlBuilder builder) { if (builder.DbType == DriverType.SqlServer) { string op = "-"; SimpleTypes t1 = Operand1.GetResultType(); SimpleTypes t2 = Operand2.GetResultType(); if (t2 == SimpleTypes.Time && t1 == SimpleTypes.Time) { return("CONVERT(time, CONVERT(datetime, " + Operand1.ToSql(builder) + ")" + op + "CONVERT(datetime, " + Operand2.ToSql(builder) + "))"); } if (t1 == SimpleTypes.Date && t2 == SimpleTypes.Time) { return("(CONVERT(datetime, " + Operand1.ToSql(builder) + ")" + op + Operand2.ToSql(builder) + ")"); } if (t2 == SimpleTypes.Date && t1 == SimpleTypes.Time) { return("(CONVERT(datetime, " + Operand2.ToSql(builder) + ")" + op + Operand1.ToSql(builder) + ")"); } if (t1 == SimpleTypes.Geometry && t2 == SimpleTypes.Geometry) { if (op == "+") { return("(" + Operand2.ToSql(builder) + ".STUnion(" + Operand1.ToSql(builder) + "))"); } if (op == "-") { return("(" + Operand2.ToSql(builder) + ".STDifference(" + Operand1.ToSql(builder) + "))"); } } return("(" + Operand1.ToSql(builder) + op + Operand2.ToSql(builder) + ")"); } if (builder.DbType == DriverType.PostgreSQL) { string op = "-"; SimpleTypes t1 = Operand1.GetResultType(); SimpleTypes t2 = Operand2.GetResultType(); if (t1 == SimpleTypes.String || t2 == SimpleTypes.String) { op = "||"; } if (t2 == SimpleTypes.Time && t1 == SimpleTypes.Time) { return("(" + Operand1.ToSql(builder) + op + "CAST(" + Operand2.ToSql(builder) + " as interval))"); } return("(" + Operand1.ToSql(builder) + op + Operand2.ToSql(builder) + ")"); } return(ToSqlException()); }
//parsing by the format operand-expression-expression public override void Parse(TokensStack sTokens) { Token t = sTokens.Pop(); //( if (sTokens.Count < 3 || !(sTokens.Peek(0) is Operator)) { throw new SyntaxErrorException("Expected Operator, recieved ", sTokens.Peek(0)); } Operator = ((Operator)sTokens.Pop()).Name.ToString(); Operand1 = Expression.Create(sTokens); Operand1.Parse(sTokens); Operand2 = Expression.Create(sTokens); Operand2.Parse(sTokens); t = sTokens.Pop(); //) }
public override string ToSql(ExpressionSqlBuilder builder) { if (builder.DbType == DriverType.SqlServer) { return("CHARINDEX(" + Operand1.ToSql(builder) + ", " + Operand2.ToSql(builder) + ")"); } else if (builder.DbType == DriverType.PostgreSQL) { return("position(" + Operand1.ToSql(builder) + " in " + Operand2.ToSql(builder) + ")"); } else { return(ToSqlException()); } }
public override object EvalValue(Func <string, object> context) { base.EvalValue(context); object value = Operand1.EvalValue(context); if (!value.ConvertToBoolean()) { return(false); } else { value = Operand2.EvalValue(context); return(value.ConvertToBoolean()); } }
public override void Parse(TokensStack sTokens) { if (sTokens.Peek() is Parentheses && ((Parentheses)sTokens.Peek()).Name.Equals('(')) { Token t1 = sTokens.Pop(); } else { throw new SyntaxErrorException("missing ( ", sTokens.Peek()); } if (sTokens.Peek() is Operator) { Operator = "" + ((Operator)sTokens.Pop()).Name; } else { throw new SyntaxErrorException("no operator", sTokens.Peek()); } Operand1 = Expression.Create(sTokens); if (Operand1 != null) { Operand1.Parse(sTokens); } else { throw new SyntaxErrorException("Bad Ope1", new Token()); } Operand2 = Expression.Create(sTokens); if (Operand2 != null) { Operand2.Parse(sTokens); } else { throw new SyntaxErrorException("Bad Ope2", new Token()); } if (sTokens.Peek() is Parentheses && ((Parentheses)sTokens.Peek()).Name.Equals(')')) { Token t2 = sTokens.Pop(); } else { throw new SyntaxErrorException("misssing (", sTokens.Pop()); } }
public override void Prepare() { base.Prepare(); SimpleTypes t1 = Operand1.GetResultType(); SimpleTypes t2 = Operand2.GetResultType(); //public enum ColumnSimpleTypes { Integer = 1, Float = 2, String = 3, Geometry = 4, Date = 5, DateTime = 6, Time = 7, Boolean = 8 } if (t1 == SimpleTypes.Integer && t2 == SimpleTypes.Integer) { GetIntResultOut = CalcAsInt; SetResultType(SimpleTypes.Integer); return; } if ((t1 == SimpleTypes.Integer || t1 == SimpleTypes.Float) && (t2 == SimpleTypes.Integer || t2 == SimpleTypes.Float)) { GetFloatResultOut = CalcAsFloat; SetResultType(SimpleTypes.Float); return; } if (t1 == SimpleTypes.DateTime && t2 == SimpleTypes.Time) { GetDateTimeResultOut = CalcAsDateTimeAndTime1; SetResultType(SimpleTypes.DateTime); return; } if (t2 == SimpleTypes.DateTime && t1 == SimpleTypes.Time) { GetDateTimeResultOut = CalcAsDateTimeAndTime2; SetResultType(SimpleTypes.DateTime); return; } if (t2 == SimpleTypes.Time && t1 == SimpleTypes.Time) { GetTimeResultOut = CalcAsTimeAndTime; SetResultType(SimpleTypes.Time); return; } /*if (t2 == ColumnSimpleTypes.Geometry && t1 == ColumnSimpleTypes.Geometry) * { * GetGeomResultOut = CalcAsGeom; * SetResultType(ColumnSimpleTypes.Geometry); * return; * }*/ TypesException(); }
public override object EvalValue(Func <string, object> context) { object a = Operand1.EvalValue(context); object b = Operand2.EvalValue(context); if (a == null || b == null) { return(null); } if (a is decimal && b is decimal) { return(Convert.ToDecimal(Math.Pow(Convert.ToDouble(a), Convert.ToDouble(b)))); } else { throw new Exceptions.UnexpectedTypeException(a.GetType()); } }
public override void Parse(TokensStack sTokens) { Token open = sTokens.Pop(); if (!(open is Parentheses) || ((Parentheses)open).Name != '(') { throw new SyntaxErrorException("$Expected (" + open, open); } Operand1 = Expression.Create(sTokens); Operand1.Parse(sTokens); // while (!(sTokens.Peek() is Operator)) // { // sTokens.Pop(); // } Token tOperator = sTokens.Pop(); if (!(tOperator is Operator)) { throw new SyntaxErrorException("$Expected operator ", tOperator); } else { Operator = ((Operator)tOperator).Name.ToString(); } Operand2 = Expression.Create(sTokens); Operand2.Parse(sTokens); //while (!(sTokens.Peek() is Parentheses)) // { // sTokens.Pop(); // } Token close = sTokens.Pop(); if (!(close is Parentheses) || ((Parentheses)close).Name != ')') { throw new SyntaxErrorException("$Expected )", close); } }
private EquationParts GetEquationPartToBeCorrected() { if (Operand1.Contains('?')) { return(EquationParts.Operand1); } if (Operand2.Contains('?')) { return(EquationParts.Operand2); } if (Result.Contains('?')) { return(EquationParts.Result); } else { return(EquationParts.Invalid); } }
public override object EvalValue(Func <string, object> context) { object a = Operand1.EvalValue(context); object b = Operand2.EvalValue(context); if (a == null || b == null) { return(null); } if (a is double && b is double) { return((double)a * (double)b); } else { throw new FormatException(); } }
public override object EvalValue(Func <string, object> context) { object a = Operand1.EvalValue(context); object b = Operand2.EvalValue(context); if (a == null || b == null) { return(null); } if (a is double && b is double) { return((double)a % (double)b); } else { throw new Exceptions.UnexpectedTypeException(a.GetType()); } }
/// <summary>Resolves the two operands and then compares them using the user specified operator</summary> protected override double Execute(IGameState gameState) { var op1 = Operand1.Evaluate(gameState); var op2 = Operand2.Evaluate(gameState); switch (Operator) { case MathsOperator.Add: return(op1 + op2); case MathsOperator.Sub: return(op1 - op2); case MathsOperator.Mul: return(op1 * op2); case MathsOperator.Div when op2 != 0: return(op1 / op2); // Return 0 if user tried to divide by zero. case MathsOperator.Mod when op2 != 0: return(op1 % op2); default: return(0); } }
/// <summary>Resolves the two operands and then compares them using the user specified operator</summary> public double Evaluate(IGameState gameState) { var op1 = Operand1.Evaluate(gameState); var op2 = Operand2.Evaluate(gameState); switch (Operator) { case MathsOperator.Add: return(op1 + op2); case MathsOperator.Sub: return(op1 - op2); case MathsOperator.Mul: return(op1 * op2); case MathsOperator.Div: return(op2 == 0 ? 0 : op1 / op2); // Return 0 if user tried to divide by zero. Easier than having to deal with Infinity (which C# returns). case MathsOperator.Mod: return(op2 == 0 ? 0 : op1 % op2); default: return(0); } }
public virtual string EvalText(string format) { if (Operand1 == null || Operand2 == null) { throw new OperandException(Name); } StringBuilder sb = new StringBuilder(); if (Operand1 != null) { if (Operand1 is InfixOperationToken && ((InfixOperationToken)Operand1).Precedence < Precedence) { sb.AppendFormat("({0})", Operand1.EvalText(format)); } else { sb.Append(Operand1.EvalText(format)); } } else { sb.Append("[Missing]"); } sb.Append(' ').Append(Name).Append(' '); if (Operand2 != null) { if (Operand2 is InfixOperationToken && ((InfixOperationToken)Operand2).Precedence <= Precedence) { sb.AppendFormat("({0})", Operand2.EvalText(format)); } else { sb.Append(Operand2.EvalText(format)); } } else { sb.Append("[Missing]"); } return(sb.ToString()); }
/// <summary>Resolves the two operands and then compares them with the user-specified operator.</summary> protected override bool Execute(IGameState gameState) { var op1 = Operand1.Evaluate(gameState); var op2 = Operand2.Evaluate(gameState); switch (Operator) { case ComparisonOperator.EQ: return(op1 == op2); case ComparisonOperator.NEQ: return(op1 != op2); case ComparisonOperator.GT: return(op1 > op2); case ComparisonOperator.GTE: return(op1 >= op2); case ComparisonOperator.LT: return(op1 < op2); case ComparisonOperator.LTE: return(op1 <= op2); default: return(false); } }
public override object EvalValue(Func <string, object> context) { object a = Operand1.EvalValue(context); object b = Operand2.EvalValue(context); if (a == null || b == null) { return(null); } else if (a is decimal && b is decimal) { return((decimal)a - (decimal)b); } else if (a is DateTime && b is decimal) { return(((DateTime)a).AddDays(-1 * Convert.ToDouble(b))); } else { throw new UnexpectedTypeException(a.GetType()); } }
public override string ToString() { var symbol = SymbolToken.OperatorStringOf(Type); var left = Operand1.ToString(); if (Operand1.Type.Order() < Type.Order()) { left = $"({left})"; } var right = Operand2.ToString(); if (Operand2.Type.Order() < Type.Order()) { right = $"({right})"; } // we reverse here since we want the more complex stuff on the left. // our transforms tend to push them to the right, and we can do this // since multiplication is commutative. return($"{right} {symbol} {left}"); }
public override object EvalValue(Func <string, object> context) { object a = Operand1.EvalValue(context); object b = Operand2.EvalValue(context); if (a == null || b == null) { return(false); } Type type = a.GetType(); if (type != b.GetType()) { throw new Exceptions.UnexpectedTypeException(type, b.GetType()); } int result = 0; if (type == typeof(decimal)) { result = Comparer <decimal> .Default.Compare((decimal)a, (decimal)b); } else if (type == typeof(DateTime)) { result = Comparer <DateTime> .Default.Compare((DateTime)a, (DateTime)b); } else if (type == typeof(String)) { result = Comparer <String> .Default.Compare((String)a, (String)b); } else if (type == typeof(bool)) { result = Comparer <bool> .Default.Compare((bool)a, (bool)b); } else { throw new NotSupportedException(); } return(interpret(result)); }
public override void Render(MachineInstructionWriter writer, MachineInstructionWriterOptions options) { writer.WriteOpcode(Opcode.ToString()); if (Operand1 == null) { return; } writer.Tab(); Operand1.Write(writer, options); if (Operand2 == null) { return; } writer.WriteString(","); Operand2.Write(writer, options); if (Operand3 == null) { return; } writer.WriteString(","); Operand3.Write(writer, options); }
public string ToString(bool includeValue, bool reduceParentheses) { if (Operator == Operator.Create) { return(Value.ToString()); } StringBuilder stringBuilder = new StringBuilder(); if (includeValue) { stringBuilder.Append(Value); stringBuilder.Append("="); } if (Operand1.Operator == Operator.Create || !includeValue && reduceParentheses && (Operator == Operator.Add || Operator == Operator.Subtract || (Operator == Operator.Multiply || Operator == Operator.Divide) && (Operand1.Operator == Operator.Multiply || Operand1.Operator == Operator.Divide))) { stringBuilder.Append(Operand1.ToString(includeValue, reduceParentheses)); } else { stringBuilder.Append(String.Format("({0})", Operand1.ToString(includeValue, reduceParentheses))); } stringBuilder.Append(GetOperatorString(Operator)); if (Operand2.Operator == Operator.Create || !includeValue && reduceParentheses && (Operator == Operator.Add || (Operator == Operator.Subtract || Operator == Operator.Multiply || Operator == Operator.Divide) && (Operand2.Operator == Operator.Multiply || Operand2.Operator == Operator.Divide))) { stringBuilder.Append(Operand2.ToString(includeValue, reduceParentheses)); } else { stringBuilder.Append(String.Format("({0})", Operand2.ToString(includeValue, reduceParentheses))); } return(stringBuilder.ToString()); }
public override void Prepare() { base.Prepare(); SimpleTypes t1 = Operand1.GetResultType(); SimpleTypes t2 = Operand2.GetResultType(); //public enum ColumnSimpleTypes { Integer = 1, Float = 2, String = 3, Geometry = 4, Date = 5, DateTime = 6, Time = 7, Boolean = 8 } if (t1 == SimpleTypes.Integer && t2 == SimpleTypes.Integer) { GetIntResultOut = CalcAsInt; SetResultType(SimpleTypes.Integer); return; } if ((t1 == SimpleTypes.Integer || t1 == SimpleTypes.Float) && (t2 == SimpleTypes.Integer || t2 == SimpleTypes.Float)) { GetFloatResultOut = CalcAsFloat; SetResultType(SimpleTypes.Float); return; } TypesException(); }
public override object EvalValue(Func <string, object> context) { object a = Operand1.EvalValue(context); object b = Operand2.EvalValue(context); if (a == null || b == null) { return(null); } else if (double.TryParse(a.ToString(), out double aNumber) && double.TryParse(b.ToString(), out double bNumber)) { return(aNumber - bNumber); } else if (a is DateTime && b is double) { return(((DateTime)a).AddDays(-1 * Convert.ToDouble(b))); } else { throw new UnexpectedTypeException(a.GetType()); } }
public override void Prepare() { base.Prepare(); SimpleTypes st = SimpleTypes.DateTime; if (Operand1.GetResultType() == SimpleTypes.DateTime) { st = SimpleTypes.DateTime; GetDateTimeResultOut = GetResultDate; } if (Operand1.GetResultType() == SimpleTypes.Date) { st = SimpleTypes.Date; GetDateTimeResultOut = GetResultDate; } if (Operand1.GetResultType() == SimpleTypes.Time) { st = SimpleTypes.Time; GetTimeResultOut = GetResultTime; } SetResultType(st); }
public override GremlinExpression Simplify() { var simplifiedOperand1 = Operand1.Simplify(); var simplifiedOperand2 = Operand2.Simplify(); if (simplifiedOperand1 is TerminalGremlinExpression terminal1 && simplifiedOperand2 is TerminalGremlinExpression terminal2) { if (terminal1.Parameter == terminal2.Parameter) { if (terminal1.Key == terminal2.Key || terminal1.Key is MemberExpression memberExpression1 && terminal2.Key is MemberExpression memberExpression2 && memberExpression1.Member == memberExpression2.Member) { return(new TerminalGremlinExpression(Parameter, terminal1.Key, Fuse(terminal1.Predicate, terminal2.Predicate))); } } } if (simplifiedOperand1 == Operand1 && simplifiedOperand2 == Operand2) { return(this); } return(CreateSimplified(Parameter, simplifiedOperand1, simplifiedOperand2)); }
/// <summary>Compares the two strings with the given operator</summary> public bool Evaluate(IGameState gameState) { var op1 = Operand1.Evaluate(gameState); var op2 = Operand2.Evaluate(gameState); if (CaseInsensitive) { op1 = op1.ToLower(); op2 = op2.ToLower(); } switch (Operator) { case StringComparisonOperator.Equal: return(op1 == op2); case StringComparisonOperator.NotEqual: return(op1 != op2); case StringComparisonOperator.Before: return(string.Compare(op1, op2) < 0); case StringComparisonOperator.After: return(string.Compare(op1, op2) > 0); case StringComparisonOperator.EqualLength: return(op1.Length == op2.Length); case StringComparisonOperator.ShorterThan: return(op1.Length < op2.Length); case StringComparisonOperator.LongerThan: return(op1.Length > op2.Length); case StringComparisonOperator.StartsWith: return(op1.StartsWith(op2)); case StringComparisonOperator.EndsWith: return(op1.EndsWith(op2)); case StringComparisonOperator.Contains: return(op1.Contains(op2)); default: return(false); } }
public override string ToString() { // In an ideal world we would convert this into subtraction when // applicable. This is the lazy way without doing re-tokenization. var symbol = SymbolToken.OperatorStringOf(Type); var left = Operand1.ToString(); if (Operand1.Type.Order() < Type.Order()) { left = $"({left})"; } var right = Operand2.ToString(); if (Operand2.Type.Order() < Type.Order()) { right = $"({right})"; } // we reverse here since we want the more complex stuff on the left. // our transforms tend to push them to the right, and we can do this // since multiplication is commutative. return($"{right} {symbol} {left}"); }
/// <summary> /// Write instruction operands into bytecode stream. /// </summary> /// <param name="writer">Bytecode writer.</param> public override void WriteOperands(WordWriter writer) { Operand1.Write(writer); Operand2.Write(writer); }
public override string ToSql(ExpressionSqlBuilder builder) { return("(" + Operand1.ToSql(builder) + " < " + Operand2.ToSql(builder) + ")"); }
internal override bool ExistEnum(object enumVal) { bool exist = Operand1.ExistEnum(enumVal); return(exist); }
public override string ToStr() { return(Operand1.ToStr() + " < " + Operand2.ToStr()); }