summaryrefslogtreecommitdiff
path: root/dice-lang/src/main/java/bjc/dicelang/ast/nodes
diff options
context:
space:
mode:
Diffstat (limited to 'dice-lang/src/main/java/bjc/dicelang/ast/nodes')
-rw-r--r--dice-lang/src/main/java/bjc/dicelang/ast/nodes/LiteralDiceNode.java91
1 files changed, 91 insertions, 0 deletions
diff --git a/dice-lang/src/main/java/bjc/dicelang/ast/nodes/LiteralDiceNode.java b/dice-lang/src/main/java/bjc/dicelang/ast/nodes/LiteralDiceNode.java
index fe4c402..e689c7f 100644
--- a/dice-lang/src/main/java/bjc/dicelang/ast/nodes/LiteralDiceNode.java
+++ b/dice-lang/src/main/java/bjc/dicelang/ast/nodes/LiteralDiceNode.java
@@ -1,5 +1,14 @@
package bjc.dicelang.ast.nodes;
+import org.apache.commons.lang3.StringUtils;
+
+import bjc.dicelang.ComplexDice;
+import bjc.dicelang.CompoundDice;
+import bjc.dicelang.IDiceExpression;
+import bjc.dicelang.ScalarDie;
+import bjc.utils.data.Pair;
+import bjc.utils.parserutils.AST;
+
/**
* A AST node that represents a literal value
*
@@ -7,6 +16,11 @@ package bjc.dicelang.ast.nodes;
*
*/
public class LiteralDiceNode implements IDiceASTNode {
+ private static boolean isValidInfixOperator(String dat, String op) {
+ return StringUtils.countMatches(dat, op) == 1
+ && !dat.equalsIgnoreCase(op) && !dat.startsWith(op);
+ }
+
/**
* The value contained by this node
*/
@@ -22,6 +36,14 @@ public class LiteralDiceNode implements IDiceASTNode {
this.value = data;
}
+ /**
+ * Create a new node with the given value
+ * @param val The value for this node
+ */
+ public LiteralDiceNode(int val) {
+ this(Integer.toString(val));
+ }
+
/*
* (non-Javadoc)
*
@@ -82,6 +104,48 @@ public class LiteralDiceNode implements IDiceASTNode {
return false;
}
+ /**
+ * Parse this node into an expression
+ *
+ * @return The node in expression form
+ */
+ public IDiceExpression toExpression() {
+ String literalData = this.getData();
+
+ if (LiteralDiceNode.isValidInfixOperator(literalData, "c")) {
+ String[] strangs = literalData.split("c");
+
+ return new CompoundDice(strangs);
+ } else if (LiteralDiceNode.isValidInfixOperator(literalData,
+ "d")) {
+ /*
+ * Handle dice groups
+ */
+ return ComplexDice.fromString(literalData);
+ } else {
+ try {
+ return new ScalarDie(Integer.parseInt(literalData));
+ } catch (NumberFormatException nfex) {
+ throw new UnsupportedOperationException(
+ "Found malformed leaf token " + this);
+ }
+ }
+ }
+
+ /**
+ * Parse this node into an expression
+ *
+ * @return The node as a pair of a sample value and the AST it
+ * represents
+ */
+ public Pair<Integer, AST<IDiceASTNode>> toParseValue() {
+ AST<IDiceASTNode> returnedAST = new AST<>(this);
+
+ IDiceExpression expression = toExpression();
+
+ return new Pair<>(expression.roll(), returnedAST);
+ }
+
/*
* (non-Javadoc)
*
@@ -91,4 +155,31 @@ public class LiteralDiceNode implements IDiceASTNode {
public String toString() {
return value;
}
+
+ /**
+ * Check if this node represents a constant value
+ *
+ * @return Whether or not this node represents a constant value
+ */
+ public boolean isConstant() {
+ try {
+ Integer.parseInt(value);
+ return true;
+ } catch (NumberFormatException nfex) {
+ return false;
+ }
+ }
+
+ /**
+ * Return the constant value this node represents
+ *
+ * @return The constant value of this node
+ *
+ * @throws NumberFormatException
+ * if you call this on a node that doesn't represent a
+ * constant value
+ */
+ public int toConstant() {
+ return Integer.parseInt(value);
+ }
} \ No newline at end of file