/// FACTORIAL_FACTOR: PRIMARY '!'? Factor /// 5! private ASTNode ParseFactorialFactor( ) { var node = ParsePrimary(); if (IsNext(Token.TokenType.Factorial)) { node = new FactorialUnaryOperatorASTNode(Accept( ), node); } return(node); }
protected double Evaluate(FactorialUnaryOperatorASTNode node) { int fact(int x) => x == 1 ? 1 : x *fact(x - 1); var value = ( int )Evaluate(node.Target as dynamic); if (value < 0) { throw new Exception("Factorial only supports Positive Integers"); } return(fact(value)); }
/// FACTORIAL_FACTOR: PRIMARY '!'? private bool TryParseFactorialFactor(out ASTNode node) { if (TryParsePrimary(out node)) { if (IsNext(Token.TokenType.Factorial)) { node = new FactorialUnaryOperatorASTNode(Accept( ), node); } } return(node != null); }