Exemplo n.º 1
0
 // computes and returns the value of an expression in parentheses.
 // The scanner cursor should be pointing at the opening paren when this is called.
 // When this method returns, the scanner cursor will be pointing at the character directly after the closing paren.
 // Returns the expression in parentheses
 private string ExtractParentheses(StringScanner scanner)
 {
     StringBuilder steve = new StringBuilder ();
     int nestCount = 1; // track nested parentheses
     scanner.TrySkip (); // move past open-paren
     if (!scanner.HasNext ())
         throw new InvalidExpressionException ("mismatched parentheses");
     while (!(scanner.Peek () == ')' && nestCount == 1)) {
         if (scanner.Peek () == '(') {
             nestCount++;
         } else if (scanner.Peek () == ')') {
             nestCount--;
         }
         steve.Append (scanner.Read ());
         if (!scanner.HasNext ())
             throw new InvalidExpressionException ("mismatched parentheses");
     }
     scanner.Read (); // move past close-paren
     DebugMessage ("evaluating \"" + steve.ToString () + "\" in parentheses");
     return steve.ToString ();
 }
Exemplo n.º 2
0
        private Token ProcessDieDef(
				StringScanner steve, 
				Stack<string> opStack, 
				List<Token> output,
				bool lastTokenWasNumber)
        {
            steve.TrySkip(); // move past the d\
            if (!steve.HasNext())
                throw new InvalidExpressionException("no die type given");
            if (Char.IsDigit (steve.Peek ())) { // check that the syntax is valid before just trying to read it
                Token dieCount = new NumToken(1);
                if (lastTokenWasNumber) {
                    // the last number was the die count, because it was followed by a 'd'
                    dieCount = output.Last();
                    output.RemoveAt (output.Count - 1);
                }
                BigInteger dieType = steve.ReadLong(); // this is safe because we checked that the next char is a digit
                // we now know that die type and the die count, now we need to see if there are extra instructions for the roll
                long keepCount = 1;
                KeepStrategy keepstrat = KeepStrategy.ALL;
                if (steve.HasNext () && char.IsLetter (steve.Peek ()) && Char.ToLower (steve.Peek ()) != 'd') {
                    char extension = Char.ToLower (steve.Read ());
                    if (extension == 'h') {
                        keepstrat = KeepStrategy.HIGHEST;
                        BigInteger temp = null;
                        if (steve.TryReadLong (ref temp)) {
                            if (temp > MAX_DIECOUNT) {
                                throw new InvalidExpressionException (temp.ToString () + " is too many dice");
                            } else {
                                keepCount = temp.LongValue ();
                            }
                        }
                    } else if (extension == 'l') {
                        keepstrat = KeepStrategy.LOWEST;
                        BigInteger temp = null;
                        if (steve.TryReadLong (ref temp)) {
                            if (temp > MAX_DIECOUNT) {
                                throw new InvalidExpressionException (temp.ToString () + " is too many dice");
                            } else {
                                keepCount = temp.LongValue ();
                            }
                        }
                    } else {
                        throw new InvalidExpressionException("invalid die extension " + extension);
                    }
                }
                var countList = new List<Token> ();
                countList.Add (dieCount);
                return new DiceToken (countList, dieType, keepCount, keepstrat, this);
            } else {
                throw new InvalidExpressionException("no die type given");
            }
        }
Exemplo n.º 3
0
 // reads the name of a formula, evaluates its expression, and returns the result.
 // the scanner cursor should be pointing at the opening bracket when this is called.
 // When this method returns, the cursor will be pointing at the character after the closing bracket.
 private Formula ExtractFormula(StringScanner scanner)
 {
     StringBuilder steve = new StringBuilder ();
     scanner.TrySkip();
     if (!scanner.HasNext ())
         throw new InvalidExpressionException ("mismatched brackets");
     while (scanner.Peek () != ']') {
         steve.Append (scanner.Read ());
         if (!scanner.HasNext ())
             throw new InvalidExpressionException ("mismatched brackets");
     }
     scanner.Read ();
     Formula f = formulas [steve.ToString ()];
     if (f == null)
         throw new InvalidExpressionException ("No formula " + steve.ToString ());
     return f;
 }