From 77a797089a2e065cc8cf2a83ae8356b16591aebe Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sun, 21 Feb 2016 15:40:30 -0500 Subject: Revamping of the way dice work --- .../java/bjc/utils/dice/DiceExpressionParser.java | 80 ++++++++++++++++++++-- 1 file changed, 73 insertions(+), 7 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/dice/DiceExpressionParser.java') diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/DiceExpressionParser.java b/BJC-Utils2/src/main/java/bjc/utils/dice/DiceExpressionParser.java index 3be56e4..71801f8 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/DiceExpressionParser.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/DiceExpressionParser.java @@ -6,27 +6,80 @@ import bjc.utils.funcdata.FunctionalList; import bjc.utils.funcdata.FunctionalStringTokenizer; import bjc.utils.parserutils.ShuntingYard; +/** + * Parse a dice expression from a string + * + * @author ben + * + */ public class DiceExpressionParser { - public DiceExpression parse(String exp) { + /** + * Parse a dice expression from a string + * + * @param exp + * The string to parse an expression from + * @return The parsed dice expression + */ + public IDiceExpression parse(String exp) { + /* + * Create a tokenizer over the strings + */ FunctionalStringTokenizer fst = new FunctionalStringTokenizer(exp); + /* + * Create a shunter to rewrite the expression + */ ShuntingYard yard = new ShuntingYard<>(); + /* + * Add our custom operators to the yard + */ + yard.addOp("d", 5); // dice operator: use for creating variable + // size dice groups + yard.addOp("c", 6); // compound operator: use for creating compound + // dice from expressions + + /* + * Shunt the expression to postfix form + */ FunctionalList ls = yard.postfix(fst.toList(s -> s), s -> s); - Stack dexps = new Stack<>(); + /* + * Create a stack for building an expression from parts + */ + Stack dexps = new Stack<>(); + /* + * Create the expression from parts + */ ls.forEach((tok) -> { - if (tok.contains("d")) { - dexps.push(Dice.fromString(tok)); + /* + * Handle compound dice + */ + if (tok.contains("c") && !tok.equalsIgnoreCase("c")) { + String[] strangs = tok.split("c"); + + dexps.push(new CompoundDice(LazyDice.fromString(strangs[0]), + LazyDice.fromString(strangs[1]))); + } else if (tok.contains("d") && !tok.equalsIgnoreCase("d")) { + /* + * Handle dice groups + */ + dexps.push(LazyDice.fromString(tok)); } else { try { + /* + * Handle scalar numbers + */ dexps.push(new ScalarDie(Integer.parseInt(tok))); } catch (NumberFormatException nfex) { - DiceExpression l = dexps.pop(); - DiceExpression r = dexps.pop(); + /* + * Apply an operation to two dice + */ + IDiceExpression l = dexps.pop(); + IDiceExpression r = dexps.pop(); switch (tok) { case "+": @@ -45,13 +98,26 @@ public class DiceExpressionParser { dexps.push(new CompoundDiceExpression(l, r, DiceExpressionType.DIVIDE)); break; + case "c": + dexps.push(new CompoundDice(l, r)); + break; + case "d": + dexps.push(new LazyDice(l, r)); + break; default: - throw new IllegalStateException("Detected invalid operator " + tok); + /* + * Tell the user the operator is invalid + */ + throw new IllegalStateException( + "Detected invalid operator " + tok); } } } }); + /* + * Return the built expression + */ return dexps.pop(); } } -- cgit v1.2.3