public override bool Combine(Operator op, string a, object b, out StoryVar result) { result = default(StoryVar); // Logical operators, treat as bool if (op == Operator.LogicalAnd || op == Operator.LogicalOr) { bool aBool; return(ConvertTo <bool>(a, out aBool) && StoryVar.GetTypeService <bool>(true).Combine(op, aBool, b, out result)); } // To comply with Javascript comparison, concat if b is a string if (op == Operator.Add && b is string) { result = a + (string)b; return(true); } // For other operators, try numeric comabinations if possible (in strict mode this won't work) double aDouble; if (ConvertTo <double>(a, out aDouble)) { return(StoryVar.GetTypeService <double>(true).Combine(op, aDouble, b, out result)); } else { return(false); } }
public override bool Combine(Operator op, int a, object b, out StoryVar result) { result = default(StoryVar); // Logical operators, treat as bool if (op == Operator.LogicalAnd || op == Operator.LogicalOr) { bool aBool; return(ConvertTo <bool>(a, out aBool) && StoryVar.GetTypeService <bool>(true).Combine(op, aBool, b, out result)); } // Always use double combinations double aDouble; if (!ConvertTo <double>(a, out aDouble) || !StoryVar.GetTypeService <double>(true).Combine(op, aDouble, b, out result)) { return(false); } // Convert result back to int if it is whole number (or close to it in precision) double resultDouble = (double)result.Value; if (Math.Round(resultDouble) == Math.Round(resultDouble, DecimalPrecision)) { result.Value = Convert.ToInt32(resultDouble); } return(true); }
public override bool Combine(Operator op, bool a, object b, out StoryVar result) { result = default(StoryVar); if (op == Operator.LogicalAnd || op == Operator.LogicalOr) { bool bBool; if (!StoryVar.TryConvertTo <bool>(b, out bBool)) { return(false); } switch (op) { case Operator.LogicalAnd: result = a && bBool; break; case Operator.LogicalOr: result = a || bBool; break; default: break; } return(true); } double aDouble; return(ConvertTo <double>(a, out aDouble) && StoryVar.GetTypeService <double>(true).Combine(op, aDouble, b, out result)); }
// ............................ public override bool Compare(Operator op, int a, object b, out bool result) { // Always use double comparison result = false; double aDouble; return(ConvertTo(a, out aDouble) && StoryVar.GetTypeService <double>(true).Compare(op, aDouble, b, out result)); }
public override bool Unary(Operator op, bool a, out StoryVar result) { result = default(StoryVar); double aDouble; return(ConvertTo <double>(a, out aDouble) && StoryVar.GetTypeService <double>(true).Unary(op, aDouble, out result)); }
// ............................ public override bool Compare(Operator op, string a, object b, out bool result) { result = false; // Logical operators, treat as bool if (op == Operator.LogicalAnd || op == Operator.LogicalOr) { bool aBool; return(ConvertTo <bool>(a, out aBool) && StoryVar.GetTypeService <bool>(true).Compare(op, aBool, b, out result)); } // Try numeric comparison (in strict mode this won't work) double aDouble; if (ConvertTo <double>(a, out aDouble) && StoryVar.GetTypeService <double>(true).Compare(op, aDouble, b, out result)) { return(true); } string bString; if (!StoryVar.TryConvertTo <string>(b, out bString)) { return(false); } switch (op) { case Operator.Equals: result = a == bString; break; case Operator.GreaterThan: result = String.Compare(a, bString) > 0; break; case Operator.GreaterThanOrEquals: result = String.Compare(a, bString) >= 0; break; case Operator.LessThan: result = String.Compare(a, bString) < 0; break; case Operator.LessThanOrEquals: result = String.Compare(a, bString) <= 0; break; case Operator.Contains: result = a.Contains(bString); break; default: return(false); } return(true); }
public override bool Unary(Operator op, string a, out StoryVar result) { result = default(StoryVar); // Try numeric unary if possible (in strict mode this won't work) double aDouble; if (ConvertTo(a, out aDouble)) { return(StoryVar.GetTypeService <double>(true).Unary(op, aDouble, out result)); } else { return(false); } }
public override bool Combine(Operator op, double a, object b, out StoryVar result) { result = default(StoryVar); // Logical operators, treat as bool if (op == Operator.LogicalAnd || op == Operator.LogicalOr) { bool aBool; return(ConvertTo <bool>(a, out aBool) && StoryVar.GetTypeService <bool>(true).Combine(op, aBool, b, out result)); } double bDouble; if (!StoryVar.TryConvertTo <double>(b, out bDouble)) { return(false); } switch (op) { case Operator.Add: result = a + bDouble; break; case Operator.Subtract: result = a - bDouble; break; case Operator.Multiply: result = a * bDouble; break; case Operator.Divide: result = a / bDouble; break; case Operator.Modulo: result = a % bDouble; break; default: return(false); // combination not possible } return(true); }
public override bool Unary(Operator op, int a, out StoryVar result) { result = default(StoryVar); // Always use double operations double aDouble; if (!ConvertTo(a, out aDouble) || !StoryVar.GetTypeService <double>(true).Unary(op, aDouble, out result)) { return(false); } // Convert result back to int if it is whole number (or close to it in precision) double resultDouble = (double)result.Value; if (Math.Round(resultDouble) == Math.Round(resultDouble, DecimalPrecision)) { result.Value = Convert.ToInt32(resultDouble); } return(true); }
// ............................ public override bool Compare(Operator op, bool a, object b, out bool result) { result = false; if (op == Operator.Equals) { // Equaliy, evaluate as bools bool bBool; if (!StoryVar.TryConvertTo <bool>(b, out bBool)) { return(false); } result = a == bBool; return(true); } else { // Evaluate as numbers for other operators double aDouble; return(ConvertTo <double>(a, out aDouble) && StoryVar.GetTypeService <double>(true).Compare(op, aDouble, b, out result)); } }