From 1cf770b4662fed523513a1c73b63c56d9552d8f1 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Wed, 27 Jul 2016 16:30:39 -0400 Subject: General cleanliness --- .../main/java/bjc/dicelang/IDiceExpression.java | 25 +++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) (limited to 'dice-lang/src/main/java/bjc/dicelang/IDiceExpression.java') diff --git a/dice-lang/src/main/java/bjc/dicelang/IDiceExpression.java b/dice-lang/src/main/java/bjc/dicelang/IDiceExpression.java index 4585f75..0ee2127 100644 --- a/dice-lang/src/main/java/bjc/dicelang/IDiceExpression.java +++ b/dice-lang/src/main/java/bjc/dicelang/IDiceExpression.java @@ -11,26 +11,41 @@ import bjc.utils.funcutils.StringUtils; @FunctionalInterface public interface IDiceExpression { /** - * Parse this node into an expression + * Parse a string into an expression. + * + * It can accept the following types of expressions + * + * + * Dice concatenation is like using 2 d10s to emulate a d100, so + * instead of adding them, it reads them side by side. * * @param expression * The string to convert to an expression * - * @return The string in expression form + * @return The string, converted into expression form */ static IDiceExpression toExpression(String expression) { String literalData = expression; if (StringUtils.containsInfixOperator(literalData, "c")) { + // Parse a compound die String[] strangs = literalData.split("c"); return new CompoundDice(strangs); } else if (StringUtils.containsInfixOperator(literalData, "d")) { - /* - * Handle dice groups - */ + // Handle groups of similiar dice return ComplexDice.fromString(literalData); + } else if (literalData.startsWith("d")) { + // Handle people who put 'd6' instead of '1d6' + return new Die(Integer.parseInt(literalData.substring(1))); } else { + // Parse a scalar number try { return new ScalarDie(Integer.parseInt(literalData)); } catch (NumberFormatException nfex) { -- cgit v1.2.3