private double And(AstNode node) { var result = Interpret(node.GetChild(1)) == 1 && Interpret(node.GetChild(2)) == 1 ? 1 : 0; return(result); }
private static string getStringSubTree(AstNode node, string indent, bool root) { if (node == null) { return(""); } string result = indent; if (!root) { if (node.Index < node.Parent.ChildCount - 1) { result += MiddleNodeChar + " "; indent += ConnectChar + " "; } else { result += LastNodeChar + " "; indent += " "; } } result += node + "\n"; for (int i = 0; i < node.ChildCount; i++) { result += getStringSubTree(node.GetChild(i), indent, false); } return(result); }
/// <summary> /// Performs the specified action on an expression tree. /// </summary> /// <returns>The top node of the results.</returns> /// <exception>ApplicationException</exception> /// <param name="node">The top node of the tree.</param> /// <param name="data">Data passed to the visitor (Typically not used).</param> /// <param name="pv">Visitor object that can be used to descend the tree.</param> /// <param name="stack">The stack to operate on.</param> /// <exception cref="ApplicationException">If operator must have 3 arguments.</exception> public virtual object Evaluate( AstNode node, object data, IAstNodeVisitor pv, Stack< double? > stack ) { if ( node.NumChildren != 3 ) throw new ApplicationException( "If operator must have 3 arguments." ); AstNode condition = node.GetChild( 0 ); condition.Accept( pv, data ); CheckStack( stack ); double? condVal = stack.Pop(); if ( condVal.HasValue && ( condVal > 0.0 ) ) { AstNode trueNode = node.GetChild( 1 ); trueNode.Accept( pv, data ); return data; } AstNode falseNode = node.GetChild( 2 ); falseNode.Accept( pv, data ); return data; }
private double GreaterThan(AstNode node) { var result = Interpret(node.GetChild(1)) > Interpret(node.GetChild(2)) ? 1 : 0; return(result); }
private double Not(AstNode node) { var result = Interpret(node.GetChild(1)) == 1 ? 0 : 1; return(result); }
private double LessThanOrEqual(AstNode node) { var result = Interpret(node.GetChild(1)) <= Interpret(node.GetChild(2)) ? 1 : 0; return(result); }
private double Equality(AstNode node) { var result = Interpret(node.GetChild(1)) == Interpret(node.GetChild(2)) ? 1 : 0; return(result); }
private double Plus(AstNode node) { var result = Interpret(node.GetChild(1)) + Interpret(node.GetChild(2)); return(result); }
private double Multiply(AstNode node) { var result = Interpret(node.GetChild(1)) * Interpret(node.GetChild(2)); return(result); }
private double Divide(AstNode node) { var result = Interpret(node.GetChild(1)) / Interpret(node.GetChild(2)); return(result); }
private double Exponent(AstNode node) { var result = Math.Pow(Interpret(node.GetChild(1)), Interpret(node.GetChild(2))); return(result); }
private double Modulo(AstNode node) { var result = Interpret(node.GetChild(1)) % Interpret(node.GetChild(2)); return(result); }