summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/dice/DiceExpressionParser.java
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2016-02-21 15:40:30 -0500
committerbculkin2442 <bjculkin@mix.wvu.edu>2016-02-21 15:40:30 -0500
commit77a797089a2e065cc8cf2a83ae8356b16591aebe (patch)
treee88f80b126cbb6de08881beb0a8c97111966a2b7 /BJC-Utils2/src/main/java/bjc/utils/dice/DiceExpressionParser.java
parentd8b3b3c5e4441cecec98c06a36fc81570008c888 (diff)
Revamping of the way dice work
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/dice/DiceExpressionParser.java')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/dice/DiceExpressionParser.java80
1 files changed, 73 insertions, 7 deletions
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<String> 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<String> ls = yard.postfix(fst.toList(s -> s),
s -> s);
- Stack<DiceExpression> dexps = new Stack<>();
+ /*
+ * Create a stack for building an expression from parts
+ */
+ Stack<IDiceExpression> 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();
}
}