1 Star 1 Fork 0

子小川页月月鸟/PatternMatchingExpr

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Expr.cs 3.78 KB
一键复制 编辑 原始数据 按行查看 历史
hez2010 提交于 2022-03-04 11:00 . Expression calculator
using System.Globalization;
namespace PatternMatchingExpr;
public abstract partial class Expr<T> where T : IBinaryNumber<T>
{
public abstract T Eval(params (string Name, T Value)[] args);
public T InteractiveEval()
{
var names = Array.Empty<string>();
return Eval(GetArgs(this, ref names, ref names));
}
private static T GetArg(string name, ref string[] names)
{
Console.Write($"Parameter {name}: ");
string? str;
do { str = Console.ReadLine(); }
while (str is null);
names = names.Append(name).ToArray();
return T.Parse(str, NumberStyles.Number, null);
}
private static (string Name, T Value)[] GetArgs(Expr<T> expr, ref string[] assigned, ref string[] filter) => expr switch
{
TernaryExpr(var cond, var left, var right) => GetArgs(cond, ref assigned, ref assigned).Concat(GetArgs(left, ref assigned, ref assigned)).Concat(GetArgs(right, ref assigned, ref assigned)).ToArray(),
BinaryExpr(_, var left, var right) => GetArgs(left, ref assigned, ref assigned).Concat(GetArgs(right, ref assigned, ref assigned)).ToArray(),
UnaryExpr(_, var uexpr) => GetArgs(uexpr, ref assigned, ref assigned),
ParameterExpr(var name) => filter switch
{
[var head, ..] when head == name => Array.Empty<(string Name, T Value)>(),
[_, .. var tail] => GetArgs(expr, ref assigned, ref tail),
[] => new[] { (name, GetArg(name, ref assigned)) }
},
_ => Array.Empty<(string Name, T Value)>()
};
public static Expr<T> operator ~(Expr<T> operand) => new UnaryExpr(new(Operators.Inv), operand);
public static Expr<T> operator !(Expr<T> operand) => new UnaryExpr(new(Operators.LogicalNot), operand);
public static Expr<T> operator -(Expr<T> operand) => new UnaryExpr(new(Operators.Min), operand);
public static Expr<T> operator +(Expr<T> left, Expr<T> right) => new BinaryExpr(new(Operators.Add), left, right);
public static Expr<T> operator -(Expr<T> left, Expr<T> right) => new BinaryExpr(new(Operators.Sub), left, right);
public static Expr<T> operator *(Expr<T> left, Expr<T> right) => new BinaryExpr(new(Operators.Mul), left, right);
public static Expr<T> operator /(Expr<T> left, Expr<T> right) => new BinaryExpr(new(Operators.Div), left, right);
public static Expr<T> operator &(Expr<T> left, Expr<T> right) => new BinaryExpr(new(Operators.And), left, right);
public static Expr<T> operator |(Expr<T> left, Expr<T> right) => new BinaryExpr(new(Operators.Or), left, right);
public static Expr<T> operator ^(Expr<T> left, Expr<T> right) => new BinaryExpr(new(Operators.Xor), left, right);
public static Expr<T> operator >(Expr<T> left, Expr<T> right) => new BinaryExpr(new(Operators.Gt), left, right);
public static Expr<T> operator <(Expr<T> left, Expr<T> right) => new BinaryExpr(new(Operators.Lt), left, right);
public static Expr<T> operator >=(Expr<T> left, Expr<T> right) => new BinaryExpr(new(Operators.Ge), left, right);
public static Expr<T> operator <=(Expr<T> left, Expr<T> right) => new BinaryExpr(new(Operators.Le), left, right);
public static Expr<T> operator ==(Expr<T> left, Expr<T> right) => new BinaryExpr(new(Operators.Eq), left, right);
public static Expr<T> operator !=(Expr<T> left, Expr<T> right) => new BinaryExpr(new(Operators.Ne), left, right);
public static implicit operator Expr<T>(T value) => new ConstantExpr(value);
public static implicit operator Expr<T>(string name) => new ParameterExpr(name);
public static implicit operator Expr<T>(bool value) => new ConstantExpr(value ? T.One : T.Zero);
public override bool Equals(object? obj) => base.Equals(obj);
public override int GetHashCode() => base.GetHashCode();
}
马建仓 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