I want to overload some mathematical operators. How do I specify the priority? #4095
-
using System; namespace Util.Mathematics { /// 二叉树遍历方式 /// public enum TravelType { Foreword, Middle, Back } abstract public class Expr { protected Expr left, right, parent; public IEnumerable Travel(TravelType tt = TravelType.Back) { switch (tt) { case TravelType.Foreword: yield return this; if (left != null) foreach (var item in left.Travel(tt)) yield return item; if (right != null) foreach (var item in right.Travel(tt)) yield return item; break; case TravelType.Middle: if (left != null) foreach (var item in left.Travel(tt)) yield return item; yield return this; if (right != null) foreach (var item in right.Travel(tt)) yield return item; break; case TravelType.Back: if (left != null) foreach (var item in left.Travel(tt)) yield return item; if (right != null) foreach (var item in right.Travel(tt)) yield return item; yield return this; break; default: break; } } public struct Type { internal string value; public Type(string value) { this.value = value; } static public Type None = nameof(None); static public Type Add = "+"; static public Type Subtract = "-"; static public Type Multiply = "*"; static public Type Divide = "/"; static public Type Pow = "^"; static public Type Log = "log"; static public Type Lg = "lg"; static public Type Sin = "sin"; static public Type Cos = "cos"; static public Type Tan = "tan"; static public Type Ctan = "ctan"; static public Type Abs = "abs"; static public Type Exp = "exp"; static public Type Ln = "ln"; static public Type Round = "round"; static public Type Floor = "floor"; static public Type Max = "max"; static public Type Min = "min"; static public Type Ceiling = "ceiling"; static public Type Asin = "asin"; static public Type Acos = "acos"; static public Type Atan = "atan"; static public implicit operator Type(string value) => new Type(value); static public implicit operator string(Type con) => con.value; public int Priority => this switch { Type x when x == Add => 0, Type x when x == Subtract => 0, Type x when x == Multiply => 1, Type x when x == Divide => 1, Type x when x == Pow => 2, Type x when x == Log => 2, Type x when x == Lg => 2, Type x when x == Ln => 2, _ => 3 }; public override string ToString() => this.value; } public Expr.Type OperType { get; set; }
} namespace Netlibs.Test { So I hope C# can support more complete mathematical symbols |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You can not change parser precedence. Sorry! |
Beta Was this translation helpful? Give feedback.
You can not change parser precedence. Sorry!