You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.3 KiB
48 lines
1.3 KiB
2 years ago
|
using EC.Helper.RabbitFunc.Runtime;
|
||
|
|
||
|
namespace EC.Helper.RabbitFunc.Expressions;
|
||
|
|
||
|
public class UnaryExpression : Expression
|
||
|
{
|
||
|
internal UnaryExpression(ExpressionType type, Expression operand)
|
||
|
: base(type)
|
||
|
{
|
||
|
Operand = operand;
|
||
|
}
|
||
|
|
||
|
public Expression Operand { get; }
|
||
|
|
||
|
public override object Eval(RuntimeContext context)
|
||
|
{
|
||
|
if (context == null)
|
||
|
throw new ArgumentNullException(nameof(context));
|
||
|
|
||
|
object value = Operand.Eval(context);
|
||
|
switch (NodeType)
|
||
|
{
|
||
|
case ExpressionType.Negate:
|
||
|
return (-(double)value);
|
||
|
|
||
|
case ExpressionType.Not:
|
||
|
return (!(bool)value);
|
||
|
|
||
|
default:
|
||
|
throw new RuntimeException("unknown unary:" + NodeType.ToString());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public override string ToString()
|
||
|
{
|
||
|
switch (NodeType)
|
||
|
{
|
||
|
case ExpressionType.Negate:
|
||
|
return Operand is BinaryExpression ? "-(" + Operand.ToString() + ")" : "-" + Operand.ToString();
|
||
|
|
||
|
case ExpressionType.Not:
|
||
|
return Operand is BinaryExpression ? "!(" + Operand.ToString() + ")" : "!" + Operand.ToString();
|
||
|
|
||
|
default:
|
||
|
throw new RuntimeException("unknown operator:" + NodeType.ToString());
|
||
|
}
|
||
|
}
|
||
|
}
|