summaryrefslogtreecommitdiff
path: root/dice/src/example/java/bjc/dicelang/neodice/commands/PolyhedralDieCommand.java
diff options
context:
space:
mode:
authorBen Culkin <scorpress@gmail.com>2020-11-21 18:04:20 -0500
committerBen Culkin <scorpress@gmail.com>2020-11-21 18:04:20 -0500
commit3ddd062d60d621971af59b480ba70e8bf9e705f1 (patch)
tree8d24f91e4f695a526777b5fe75fe268669b8c9a6 /dice/src/example/java/bjc/dicelang/neodice/commands/PolyhedralDieCommand.java
parent2afb54eecd8e8b5d663a05131c07c6b8d15e65ba (diff)
Rudimentary CLI for new die implementation
Diffstat (limited to 'dice/src/example/java/bjc/dicelang/neodice/commands/PolyhedralDieCommand.java')
-rw-r--r--dice/src/example/java/bjc/dicelang/neodice/commands/PolyhedralDieCommand.java40
1 files changed, 40 insertions, 0 deletions
diff --git a/dice/src/example/java/bjc/dicelang/neodice/commands/PolyhedralDieCommand.java b/dice/src/example/java/bjc/dicelang/neodice/commands/PolyhedralDieCommand.java
new file mode 100644
index 0000000..02fc9cf
--- /dev/null
+++ b/dice/src/example/java/bjc/dicelang/neodice/commands/PolyhedralDieCommand.java
@@ -0,0 +1,40 @@
+package bjc.dicelang.neodice.commands;
+
+import static bjc.dicelang.neodice.statements.StatementValue.Type.*;
+
+import java.util.*;
+
+import bjc.dicelang.neodice.*;
+import bjc.dicelang.neodice.statements.*;
+
+public class PolyhedralDieCommand implements Command {
+ @Override
+ public StatementValue execute(Iterator<String> words, DieBoxCLI state) {
+ if (!words.hasNext()) {
+ throw new DieBoxException("Number of sides to polyhedral-die must be provided");
+ } else {
+ StatementValue sideValue = state.runStatement(words);
+
+ if (sideValue.type == INTEGER) {
+ int numSides = ((IntegerStatementValue)sideValue).value;
+
+ if (numSides < 0) throw new DieBoxException("Number of sides to polyhedral-die was not valid (must be less than 0, was %d)", numSides);
+
+ return new DieStatementValue(DieFactory.polyhedral(numSides));
+ } else {
+ throw new DieBoxException("Number of sides to polyhedral-die wasn't an integer (was %s, of type %s)",
+ sideValue, sideValue.type);
+ }
+ }
+ }
+
+ @Override
+ public String shortHelp() {
+ return "create a single polyhedral die";
+ }
+
+ @Override
+ public String longHelp() {
+ return "Creates a single polyhedral die with a fixed number of sides";
+ }
+} \ No newline at end of file