public UnitUniLeafMathNode(IMathNode inner, UnitTypes unitType, UnitConverter converter, Enum labelledUnit) : base(null) { this.Inner = inner; this.unitType = unitType; this.converter = converter; this.labelledUnit = labelledUnit; }
public EvaluationTree(IMathNode root) { this.Root = root; if (this.Root == null) { this.Root = new NumericMathNode(0); } this.Root.Evaluate(); // I guess I need to do this once? }
public void Run() { while (true) { //ITokenizer tokenizer = new Tokenizer(); Parser parser = new Parser(); List <FPTToken> tokens = new List <FPTToken>(); Console.WriteLine("Enter code homie:"); string userInput = Console.ReadLine(); var found = userInput.Contains("plus"); IMathNode result = parser.MathParse(userInput); Console.WriteLine(result.Evaluate()); } }
/// <summary> /// Create an operator node based on a character /// </summary> /// <param name="c">The operator being + - * / or ^</param> /// <param name="left">The left part of the operator</param> /// <param name="right">The right part of the operator</param> /// <returns>Valid operators return IMathNode, null if no operator</returns> public IMathNode CreateOperatorNode(string c, IMathNode left, IMathNode right) { switch (c) { case "+": return(new AdditionBiLeafMathNode(left, right)); case "-": return(new SubtractionBiLeafMathNode(left, right)); case "*": return(new MultiplicationBiLeafMathNode(left, right)); case "/": return(new DivisionBiLeafMathNode(left, right)); default: return(null); } }
/// <summary> /// Create an operator node based on a character /// </summary> /// <param name="c">The operator being + - * / or ^</param> /// <param name="left">The left part of the operator</param> /// <param name="right">The right part of the operator</param> /// <returns>Valid operators return IMathNode, null if no operator</returns> public IMathNode CreateOperatorNode(string c, IMathNode left, IMathNode right) { switch (c) { case "+": return(new AdditionBiLeafMathNode(left, right)); case "-": return(new SubtractionBiLeafMathNode(left, right)); case "*": return(new MultiplicationBiLeafMathNode(left, right)); case "/": return(new DivisionBiLeafMathNode(left, right)); case "^": return(new ExponentBiLeafMathNode(left, right)); case "&": return(new BitwiseAndBiLeafMathNode(left, right)); case "|": return(new BitwiseOrBiLeafMathNode(left, right)); case "<<": return(new BitwiseShiftLeftBiLeafMathNode(left, right)); case ">>": return(new BitwiseShiftRightBiLeafMathNode(left, right)); case "%": return(new ModulusBiLeafMathNode(left, right)); default: return(null); } }
private string read(string text, bool reporting, out IMathNode ast) { ast = null; IEnumerable <ITokenMatch <TokenEnum> > tokens = lexer.ScanText(text); ITokenMatch <TokenEnum> err_token = tokens.FirstOrDefault(it => it.Token == TokenEnum.Error); if (err_token != null) { return("Incorrect symbol in input: " + err_token.Text); } else { object root = parser.Parse(tokens, new ParserOptions() { Trace = reporting }).FirstOrDefault(); if (reporting) { foreach (var s in parser.ParseLog) { Console.WriteLine(s + Environment.NewLine); } } if (!parser.IsSuccessfulParse) { return("There were errors while parsing." + Environment.NewLine + String.Join(Environment.NewLine, parser.ErrorMessages)); } else { ast = (IMathNode)root; return(null); } } }
public SymbolMathNode(IMathNode left, IMathNode right, string symbol) : base(left, right, symbol) { }
public BitwiseAndBiLeafMathNode(IMathNode left, IMathNode right) : base(left, right, '&') { }
public BitwiseShiftLeftBiLeafMathNode(IMathNode left, IMathNode right) : base(left, right, "<<") { }
public LogUniLeafMathNode(IMathNode logbase, IMathNode inner) : base(null) { this.Base = logbase; this.Inner = inner; }
internal CosNode(IMathNode s) { this.sub = s; }
public SymbolMathNode(IMathNode left, IMathNode right, char symbol) : base(left, right, symbol) { }
internal SqrtNode(IMathNode s) { this.sub = s; }
public BitwiseShiftRightBiLeafMathNode(IMathNode left, IMathNode right) : base(left, right, ">>") { }
public SubtractionBiLeafMathNode(IMathNode left, IMathNode right) : base(left, right, '-') { }
public ModulusBiLeafMathNode(IMathNode left, IMathNode right) : base(left, right, '%') { }
internal SinNode(IMathNode s) { this.sub = s; }
public Add(IMathNode m1, IMathNode m2) { this.m1 = m1; this.m2 = m2; }
public NegateUniLeafMathNode(IMathNode inner) : base(null) { this.Inner = inner; }
public DivisionBiLeafMathNode(IMathNode left, IMathNode right) : base(left, right, '/') { }
public BitwiseOrBiLeafMathNode(IMathNode left, IMathNode right) : base(left, right, '|') { }
internal PowNode(IMathNode s1, IMathNode s2) { this.sub1 = s1; this.sub2 = s2; }
public BiLeafMathNode(IMathNode left, IMathNode right, object value) { this.left = left; this.right = right; this.value = value; }
public MultiplicationBiLeafMathNode(IMathNode left, IMathNode right) : base(left, right, '*') { }
public ExponentBiLeafMathNode(IMathNode left, IMathNode right) : base(left, right, '^') { }
public AdditionBiLeafMathNode(IMathNode left, IMathNode right) : base(left, right, '+') { }
public StringMathNode(IMathNode inner) { this.value = inner; }
public SinhUniLeafMathNode(IMathNode inner) : base(null) { this.Inner = inner; }
/// <summary> /// Creates a new OperationNode, which represents a mathematical expression. /// The OperationNode contains the operation, and its branches are the operands. /// Each operand can be any node which inherits from MathNode and thus has an evaluate() function. /// </summary> /// <param name="op"></param> /// <param name="left"></param> /// <param name="right"></param> public OperatorNode(Operation op, IMathNode left, IMathNode right) { this.op = op; this.left = left ?? throw new NullReferenceException("Left branch of OperatorNode cannot be null."); this.right = right ?? throw new NullReferenceException("Right branch of OperatorNode cannot be null."); }