/// <summary> /// Parses the specified list of tokens with appropriate logic to convert /// it into a Dice instance to evaluate oprations on. /// </summary> /// <param name="expression">dice expression rolled</param> /// <param name="tokens">String expression to parse</param> /// <param name="dieRoller">Die roller to use</param> /// <returns>Dice result</returns> private DiceResult ParseLogic(string expression, List <string> tokens, IDieRoller dieRoller) { List <TermResult> results = new List <TermResult>(); while (tokens.IndexOf(this.GroupStartOperator) != -1) { // getting data between grouping symbols: "(" and ")" int open = tokens.LastIndexOf(this.GroupStartOperator); int close = tokens.IndexOf(this.GroupEndOperator, open); if (open >= close) { throw new ArithmeticException("No matching close-open parenthesis."); } // get a subexpression list for elements within the grouping symbols List <string> subExpression = new List <string>(); for (var i = open + 1; i < close; i++) { subExpression.Add(tokens[i]); } // run the operations on the subexpression int subValue = this.HandleBasicOperation(results, subExpression, dieRoller); // when subexpression calculation is done, replace the grouping start symbol // and removing the tokens for the subexpression from the token list tokens[open] = subValue.ToString(); tokens.RemoveRange(open + 1, close - open); } // at this point, we should have replaced all groups in the expression // with the appropriate values, so need to calculate last simple expression int value = this.HandleBasicOperation(results, tokens, dieRoller); // now return the dice result from the final value and TermResults list return(new DiceResult(expression, value, results, dieRoller.GetType().ToString(), this.config)); }
/// <inheritdoc/> public DiceResult Roll(IDieRoller dieRoller) { List <TermResult> termResults = this.terms.SelectMany(t => t.CalculateResults(dieRoller)).ToList(); return(new DiceResult(this.ToString(), termResults, dieRoller.GetType().ToString(), this.Configuration)); }