1 Star 1 Fork 0

子小川页月月鸟/PatternMatchingExpr

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Expr.BinaryExpr.cs 2.52 KB
一键复制 编辑 原始数据 按行查看 历史
hez2010 提交于 2022-03-04 11:00 . Expression calculator
using static PatternMatchingExpr.Operator;
namespace PatternMatchingExpr;
public abstract partial class Expr<T> where T : IBinaryNumber<T>
{
public class BinaryExpr : Expr<T>
{
public BinaryExpr(BinaryOperator op, Expr<T> left, Expr<T> right) => (Op, Left, Right) = (op, left, right);
public BinaryOperator Op { get; }
public Expr<T> Left { get; }
public Expr<T> Right { get; }
public void Deconstruct(out BinaryOperator op, out Expr<T> left, out Expr<T> right) => (op, left, right) = (Op, Left, Right);
public override T Eval(params (string Name, T Value)[] args) => Op switch
{
BinaryOperator(var op) => op switch
{
Operators.Add => Left.Eval(args) + Right.Eval(args),
Operators.Sub => Left.Eval(args) - Right.Eval(args),
Operators.Mul => Left.Eval(args) * Right.Eval(args),
Operators.Div => Left.Eval(args) / Right.Eval(args),
Operators.And => Left.Eval(args) & Right.Eval(args),
Operators.Or => Left.Eval(args) | Right.Eval(args),
Operators.Xor => Left.Eval(args) ^ Right.Eval(args),
Operators.Eq => Left.Eval(args) == Right.Eval(args) ? T.One : T.Zero,
Operators.Ne => Left.Eval(args) != Right.Eval(args) ? T.One : T.Zero,
Operators.Gt => Left.Eval(args) > Right.Eval(args) ? T.One : T.Zero,
Operators.Lt => Left.Eval(args) < Right.Eval(args) ? T.One : T.Zero,
Operators.Ge => Left.Eval(args) >= Right.Eval(args) ? T.One : T.Zero,
Operators.Le => Left.Eval(args) <= Right.Eval(args) ? T.One : T.Zero,
Operators.LogicalAnd => Left.Eval(args) == T.Zero || Right.Eval(args) == T.Zero ? T.Zero : T.One,
Operators.LogicalOr => Left.Eval(args) == T.Zero && Right.Eval(args) == T.Zero ? T.Zero : T.One,
< Operators.Add or > Operators.LogicalOr => throw new InvalidOperationException($"Unexpected a binary operator, but got {op}.")
},
_ => throw new InvalidOperationException("Unexpected a binary operator.")
};
public override string ToString() => $"({Left}) {Op.Operator.GetName()} ({Right})";
public override bool Equals(object? obj) => obj is BinaryExpr({ Operator: var op }, var left, var right) && (op, left, right).Equals((Op.Operator, Left, Right));
public override int GetHashCode() => (Op, Left, Right).GetHashCode();
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/xp_zz/PatternMatchingExpr.git
git@gitee.com:xp_zz/PatternMatchingExpr.git
xp_zz
PatternMatchingExpr
PatternMatchingExpr
main

搜索帮助

0d507c66 1850385 C8b1a773 1850385