summaryrefslogtreecommitdiff
path: root/dice/src/example/java/bjc/dicelang/neodice/commands
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
parent2afb54eecd8e8b5d663a05131c07c6b8d15e65ba (diff)
Rudimentary CLI for new die implementation
Diffstat (limited to 'dice/src/example/java/bjc/dicelang/neodice/commands')
-rw-r--r--dice/src/example/java/bjc/dicelang/neodice/commands/BindCommand.java39
-rw-r--r--dice/src/example/java/bjc/dicelang/neodice/commands/Command.java88
-rw-r--r--dice/src/example/java/bjc/dicelang/neodice/commands/HelpCommand.java26
-rw-r--r--dice/src/example/java/bjc/dicelang/neodice/commands/LiteralCommand.java35
-rw-r--r--dice/src/example/java/bjc/dicelang/neodice/commands/PolyhedralDieCommand.java40
-rw-r--r--dice/src/example/java/bjc/dicelang/neodice/commands/RollCommand.java43
-rw-r--r--dice/src/example/java/bjc/dicelang/neodice/commands/ShowBindingsCommand.java30
7 files changed, 301 insertions, 0 deletions
diff --git a/dice/src/example/java/bjc/dicelang/neodice/commands/BindCommand.java b/dice/src/example/java/bjc/dicelang/neodice/commands/BindCommand.java
new file mode 100644
index 0000000..5091c4b
--- /dev/null
+++ b/dice/src/example/java/bjc/dicelang/neodice/commands/BindCommand.java
@@ -0,0 +1,39 @@
+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 BindCommand implements Command {
+ @Override
+ public StatementValue execute(Iterator<String> words, DieBoxCLI state) {
+ if (!words.hasNext()) {
+ throw new DieBoxException("bind requires a name to bind the value to");
+ }
+
+ String name = words.next();
+
+ StatementValue value = state.runStatement(words);
+
+ if (state.doWarn && value.type == VOID) {
+ state.output.printf("Warning: bound %s to the instance of void. Should you have provided a value?", name);
+ }
+
+ state.bindings.put(name, value);
+
+ return value;
+ }
+
+ @Override
+ public String shortHelp() {
+ return "bind a value to a name";
+ }
+
+ @Override
+ public String longHelp() {
+ return "Binds a value to a name, and returns that name. Currently, all variables go into a single global scope, but this will probably change";
+ }
+} \ No newline at end of file
diff --git a/dice/src/example/java/bjc/dicelang/neodice/commands/Command.java b/dice/src/example/java/bjc/dicelang/neodice/commands/Command.java
new file mode 100644
index 0000000..8460104
--- /dev/null
+++ b/dice/src/example/java/bjc/dicelang/neodice/commands/Command.java
@@ -0,0 +1,88 @@
+package bjc.dicelang.neodice.commands;
+
+import java.util.*;
+import java.util.function.*;
+
+import bjc.dicelang.neodice.*;
+import bjc.dicelang.neodice.statements.*;
+
+/**
+ * A single CLI command.
+ *
+ * @author Ben Culkin
+ *
+ */
+@FunctionalInterface
+public interface Command {
+ /**
+ * Execute this command.
+ *
+ * @param words The remaining input.
+ * @param state The current state.
+ *
+ * @return The result of executing this command.
+ */
+ public StatementValue execute(Iterator<String> words, DieBoxCLI state);
+
+ /**
+ * Get the 'short help' or usage summary for this command.
+ *
+ * @return The short help for this command.
+ */
+ default String shortHelp() {
+ return "no short help";
+ }
+
+ /**
+ * Get the 'long help' or detailed usage for this command.
+ *
+ * @return The long help for this command.
+ */
+ default String longHelp() {
+ return "no long help";
+ }
+
+ /**
+ * Create a new command, backed by a function.
+ *
+ * @param executor The function which backs the command.
+ * @param shortHelp The short help string.
+ * @param longHelp The long help string.
+ *
+ * @return A command backed by the function, with help.
+ */
+ static Command newCommand(
+ BiFunction<Iterator<String>, DieBoxCLI, StatementValue> executor,
+ String shortHelp, String longHelp) {
+ return new FunctionalCommand(executor, shortHelp, longHelp);
+ }
+}
+
+class FunctionalCommand implements Command {
+ private final BiFunction<Iterator<String>, DieBoxCLI, StatementValue> executor;
+ private final String shortHelp;
+ private final String longHelp;
+
+ public FunctionalCommand(
+ BiFunction<Iterator<String>, DieBoxCLI, StatementValue> executor, String shortHelp,
+ String longHelp) {
+ this.executor = executor;
+ this.shortHelp = shortHelp;
+ this.longHelp = longHelp;
+ }
+
+ @Override
+ public StatementValue execute(Iterator<String> words, DieBoxCLI state) {
+ return executor.apply(words, state);
+ }
+
+ @Override
+ public String shortHelp() {
+ return shortHelp;
+ }
+
+ @Override
+ public String longHelp() {
+ return longHelp;
+ }
+} \ No newline at end of file
diff --git a/dice/src/example/java/bjc/dicelang/neodice/commands/HelpCommand.java b/dice/src/example/java/bjc/dicelang/neodice/commands/HelpCommand.java
new file mode 100644
index 0000000..705745e
--- /dev/null
+++ b/dice/src/example/java/bjc/dicelang/neodice/commands/HelpCommand.java
@@ -0,0 +1,26 @@
+package bjc.dicelang.neodice.commands;
+
+import static bjc.dicelang.neodice.statements.VoidStatementValue.*;
+
+import java.util.*;
+
+import bjc.dicelang.neodice.*;
+import bjc.dicelang.neodice.statements.*;
+
+public class HelpCommand implements Command {
+ @Override
+ public StatementValue execute(Iterator<String> words, DieBoxCLI state) {
+ state.output.println("help has not yet been implemented. TODO");
+ return VOID_INST;
+ }
+
+ @Override
+ public String shortHelp() {
+ return "prints out help for commands";
+ }
+
+ @Override
+ public String longHelp() {
+ return "Invoke the help system. Unfortunately, not yet implemented";
+ }
+} \ No newline at end of file
diff --git a/dice/src/example/java/bjc/dicelang/neodice/commands/LiteralCommand.java b/dice/src/example/java/bjc/dicelang/neodice/commands/LiteralCommand.java
new file mode 100644
index 0000000..9b42b42
--- /dev/null
+++ b/dice/src/example/java/bjc/dicelang/neodice/commands/LiteralCommand.java
@@ -0,0 +1,35 @@
+package bjc.dicelang.neodice.commands;
+
+import java.util.*;
+
+import bjc.dicelang.neodice.*;
+import bjc.dicelang.neodice.statements.*;
+
+public class LiteralCommand implements Command {
+ private final StatementValue value;
+
+ private final String shortHelp;
+ private final String longHelp;
+
+ public LiteralCommand(StatementValue value, String shortHelp, String longHelp) {
+ this.value = value;
+
+ this.shortHelp = shortHelp;
+ this.longHelp = longHelp;
+ }
+
+ @Override
+ public StatementValue execute(Iterator<String> words, DieBoxCLI state) {
+ return value;
+ }
+
+ @Override
+ public String shortHelp() {
+ return shortHelp;
+ }
+
+ @Override
+ public String longHelp() {
+ return longHelp;
+ }
+} \ No newline at end of file
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
diff --git a/dice/src/example/java/bjc/dicelang/neodice/commands/RollCommand.java b/dice/src/example/java/bjc/dicelang/neodice/commands/RollCommand.java
new file mode 100644
index 0000000..eb7b597
--- /dev/null
+++ b/dice/src/example/java/bjc/dicelang/neodice/commands/RollCommand.java
@@ -0,0 +1,43 @@
+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 RollCommand implements Command {
+ @Override
+ public StatementValue execute(Iterator<String> words, DieBoxCLI state) {
+ if (!words.hasNext()) {
+ throw new DieBoxException("Roll must be provided an argument to roll");
+ } else {
+ StatementValue toRoll = state.runStatement(words);
+
+ if (toRoll.type == DIE) {
+ DieStatementValue die = (DieStatementValue) toRoll;
+
+ return new IntegerStatementValue(die.value.roll(state.rng));
+ } else if (toRoll.type == DIEPOOL) {
+ DiePoolStatementValue pool = (DiePoolStatementValue) toRoll;
+
+ return new IntArrayStatementValue(pool.value.roll(state.rng));
+ } else {
+ throw new DieBoxException("Roll was provided something that wasn't rollable (only DIE and DIEPOOL objects are rollable) (was %s, of type %s)",
+ toRoll, toRoll.type);
+ }
+ }
+ }
+
+ @Override
+ public String shortHelp() {
+ return "rolls a die-like object";
+ }
+
+ @Override
+ public String longHelp() {
+ return "Rolls a die-like object, and yields the result of rolling it."
+ + " What is returned can differ based on what is rolled";
+ }
+} \ No newline at end of file
diff --git a/dice/src/example/java/bjc/dicelang/neodice/commands/ShowBindingsCommand.java b/dice/src/example/java/bjc/dicelang/neodice/commands/ShowBindingsCommand.java
new file mode 100644
index 0000000..fe2ac89
--- /dev/null
+++ b/dice/src/example/java/bjc/dicelang/neodice/commands/ShowBindingsCommand.java
@@ -0,0 +1,30 @@
+package bjc.dicelang.neodice.commands;
+
+import static bjc.dicelang.neodice.statements.VoidStatementValue.*;
+
+import java.util.*;
+
+import bjc.dicelang.neodice.*;
+import bjc.dicelang.neodice.statements.*;
+
+public class ShowBindingsCommand implements Command {
+ @Override
+ public StatementValue execute(Iterator<String> words, DieBoxCLI state) {
+ state.output.printf("Showing all %d variables currently bound:", state.bindings.size());
+ state.bindings.forEach((name, bound) -> {
+ state.output.printf("\t%t\t%s\n", name, bound);
+ });
+
+ return VOID_INST;
+ }
+
+ @Override
+ public String shortHelp() {
+ return "print out all the variable bindings";
+ }
+
+ @Override
+ public String longHelp() {
+ return "Prints out all of the variable bindings that exist at the moment";
+ }
+} \ No newline at end of file