📄 queryimpl.cs
字号:
public override bool Equals(object o)
{
return o is BinOpNode
&& base.Equals(o)
&& ((BinOpNode) o).left.Equals(left)
&& ((BinOpNode) o).right.Equals(right);
}
internal override long evaluateInt(FilterIterator t)
{
long lval = left.evaluateInt(t);
long rval = right.evaluateInt(t);
long res;
switch (tag)
{
case NodeTag.opIntAdd:
return lval + rval;
case NodeTag.opIntSub:
return lval - rval;
case NodeTag.opIntMul:
return lval * rval;
case NodeTag.opIntDiv:
if (rval == 0)
{
throw new JSQLArithmeticException("Divided by zero");
}
return lval / rval;
case NodeTag.opIntAnd:
return lval & rval;
case NodeTag.opIntOr:
return lval | rval;
case NodeTag.opIntPow:
res = 1;
if (rval < 0)
{
lval = 1 / lval;
rval = - rval;
}
while (rval != 0)
{
if ((rval & 1) != 0)
{
res *= lval;
}
lval *= lval;
rval = (long)((ulong)rval >> 1);
}
return res;
default:
throw new System.ApplicationException("Invalid tag");
}
}
internal override double evaluateReal(FilterIterator t)
{
double lval = left.evaluateReal(t);
double rval = right.evaluateReal(t);
switch (tag)
{
case NodeTag.opRealAdd:
return lval + rval;
case NodeTag.opRealSub:
return lval - rval;
case NodeTag.opRealMul:
return lval * rval;
case NodeTag.opRealDiv:
return lval / rval;
case NodeTag.opRealPow:
return Math.Pow(lval, rval);
default:
throw new System.ApplicationException("Invalid tag");
}
}
internal override string evaluateStr(FilterIterator t)
{
string lval = left.evaluateStr(t);
string rval = right.evaluateStr(t);
return lval + rval;
}
internal override object evaluateObj(FilterIterator t)
{
object lval, rval;
try
{
lval = left.evaluateObj(t);
}
catch (JSQLRuntimeException x)
{
t.query.ReportRuntimeError(x);
rval = right.evaluateObj(t);
if (rval is bool)
{
return (bool)rval;
}
throw x;
}
if (lval is bool)
{
switch (tag)
{
case NodeTag.opAnyAnd:
return ((bool) lval) && ((bool) right.evaluateObj(t))?true:false;
case NodeTag.opAnyOr:
return ((bool) lval) || ((bool) right.evaluateObj(t))?true:false;
default:
throw new System.ApplicationException("Operation is not applicable to operands of boolean type");
}
}
rval = right.evaluateObj(t);
if (lval is double || lval is float)
{
double r1 = Convert.ToDouble(lval);
double r2 = Convert.ToDouble(rval);
switch (tag)
{
case NodeTag.opAnyAdd:
return r1 + r2;
case NodeTag.opAnySub:
return r1 - r2;
case NodeTag.opAnyMul:
return r1 * r2;
case NodeTag.opAnyDiv:
return r1 / r2;
case NodeTag.opAnyPow:
return Math.Pow(r1, r2);
default:
throw new System.ApplicationException("Operation is not applicable to operands of real type");
}
}
else if (lval is string && rval is string)
{
return (string) lval + (string) rval;
}
else
{
long i1 = Convert.ToInt64(lval);
long i2 = Convert.ToInt64(rval);
long res;
switch (tag)
{
case NodeTag.opAnyAdd:
return i1 + i2;
case NodeTag.opAnySub:
return i1 - i2;
case NodeTag.opAnyMul:
return i1 * i2;
case NodeTag.opAnyDiv:
if (i2 == 0)
{
throw new JSQLArithmeticException("Divided by zero");
}
return i1 / i2;
case NodeTag.opAnyAnd:
return i1 & i2;
case NodeTag.opAnyOr:
return i1 | i2;
case NodeTag.opAnyPow:
res = 1;
if (i1 < 0)
{
i2 = 1 / i2;
i1 = - i1;
}
while (i1 != 0)
{
if ((i1 & 1) != 0)
{
res *= i2;
}
i2 *= i2;
i1 = (long)((ulong)i1 >> 1);
}
return res;
default:
throw new System.ApplicationException("Operation is not applicable to operands of integer type");
}
}
}
internal static bool areEqual(object a, object b)
{
if (a == b)
{
return true;
}
if (a is double || a is float || b is double || b is float)
{
return Convert.ToDouble(a) == Convert.ToDouble(b);
}
else if (a != null)
{
if (typeof(long).IsAssignableFrom(a.GetType()))
{
return Convert.ToInt64(a) == Convert.ToInt64(b);
}
else
{
return a.Equals(b);
}
}
return false;
}
internal static int compare(object a, object b)
{
if (a is double || a is float || b is double || b is float)
{
double r1 = Convert.ToDouble(a);
double r2 = Convert.ToDouble(b);
return r1 < r2?- 1:r1 == r2?0:1;
}
else
{
if (typeof(long).IsAssignableFrom(a.GetType()))
{
long i1 = Convert.ToInt64(a);
long i2 = Convert.ToInt64(b);
return i1 < i2?- 1:i1 == i2?0:1;
}
else
{
return ((IComparable) a).CompareTo(b);
}
}
}
internal override bool evaluateBool(FilterIterator t)
{
switch (tag)
{
case NodeTag.opAnyEq:
return areEqual(left.evaluateObj(t), right.evaluateObj(t));
case NodeTag.opAnyNe:
return !areEqual(left.evaluateObj(t), right.evaluateObj(t));
case NodeTag.opAnyLt:
return compare(left.evaluateObj(t), right.evaluateObj(t)) < 0;
case NodeTag.opAnyLe:
return compare(left.evaluateObj(t), right.evaluateObj(t)) <= 0;
case NodeTag.opAnyGt:
return compare(left.evaluateObj(t), right.evaluateObj(t)) > 0;
case NodeTag.opAnyGe:
return compare(left.evaluateObj(t), right.evaluateObj(t)) >= 0;
case NodeTag.opInAny:
{
object elem = left.evaluateObj(t);
object set = right.evaluateObj(t);
if (set is string)
{
return ((string) set).IndexOf((string) elem) >= 0;
}
else
{
object[] arr = (object[]) set;
for (int i = arr.Length; --i >= 0; )
{
if (elem.Equals(arr[i]))
{
return true;
}
}
return false;
}
}
case NodeTag.opBoolAnd:
try
{
if (!left.evaluateBool(t))
{
return false;
}
}
catch (JSQLRuntimeException x)
{
t.query.ReportRuntimeError(x);
}
return right.evaluateBool(t);
case NodeTag.opBoolOr:
try
{
if (left.evaluateBool(t))
{
return true;
}
}
catch (JSQLRuntimeException x)
{
t.query.ReportRuntimeError(x);
}
return right.evaluateBool(t);
case NodeTag.opIntEq:
return left.evaluateInt(t) == right.evaluateInt(t);
case NodeTag.opIntNe:
return left.evaluateInt(t) != right.evaluateInt(t);
case NodeTag.opIntLt:
return left.evaluateInt(t) < right.evaluateInt(t);
case NodeTag.opIntLe:
return left.evaluateInt(t) <= right.evaluateInt(t);
case NodeTag.opIntGt:
return left.evaluateInt(t) > right.evaluateInt(t);
case NodeTag.opIntGe:
return left.evaluateInt(t) >= right.evaluateInt(t);
case NodeTag.opRealEq:
return left.evaluateReal(t) == right.evaluateReal(t);
case NodeTag.opRealNe:
return left.evaluateReal(t) != right.evaluateReal(t);
case NodeTag.opRealLt:
return left.evaluateReal(t) < right.evaluateReal(t);
case NodeTag.opRealLe:
return left.evaluateReal(t) <= right.evaluateReal(t);
case NodeTag.opRealGt:
return left.evaluateReal(t) > right.evaluateReal(t);
case NodeTag.opRealGe:
return left.evaluateReal(t) >= right.evaluateReal(t);
case NodeTag.opStrEq:
return left.evaluateStr(t).Equals(right.evaluateStr(t));
case NodeTag.opStrNe:
return !left.evaluateStr(t).Equals(right.evaluateStr(t));
case NodeTag.opStrLt:
return left.evaluateStr(t).CompareTo(right.evaluateStr(t)) < 0;
case NodeTag.opStrLe:
return left.evaluateStr(t).CompareTo(right.evaluateStr(t)) <= 0;
case NodeTag.opStrGt:
return left.evaluateStr(t).CompareTo(right.evaluateStr(t)) > 0;
case NodeTag.opStrGe:
return left.evaluateStr(t).CompareTo(right.evaluateStr(t)) >= 0;
case NodeTag.opBoolEq:
return left.evaluateBool(t) == right.evaluateBool(t);
case NodeTag.opBoolNe:
return left.evaluateBool(t) != right.evaluateBool(t);
case NodeTag.opObjEq:
return areEqual(left.evaluateObj(t), right.evaluateObj(t));
case NodeTag.opObjNe:
return areEqual(left.evaluateObj(t), right.evaluateObj(t));
case NodeTag.opScanCollection:
foreach (object o in (ICollection) right.evaluateObj(t))
{
if (o == left.evaluateObj(t))
{
return true;
}
}
return false;
case NodeTag.opScanArrayBool:
{
bool val = left.evaluateBool(t);
bool[] arr = (bool[]) right.evaluateObj(t);
for (int i = arr.Length; --i >= 0; )
{
if (arr[i] == val)
{
return true;
}
}
return false;
}
case NodeTag.opScanArrayChar:
{
long val = left.evaluateInt(t);
char[] arr = (char[]) right.evaluateObj(t);
for (int i = arr.Length; --i >= 0; )
{
if (arr[i] == val)
{
return true;
}
}
return false;
}
case NodeTag.opScanArrayInt1:
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -