summaryrefslogtreecommitdiff
path: root/dice/src/example/java/bjc/dicelang/neodice
diff options
context:
space:
mode:
authorBen Culkin <scorpress@gmail.com>2020-11-21 23:11:43 -0500
committerBen Culkin <scorpress@gmail.com>2020-11-21 23:11:43 -0500
commiteda9a86d8d48758e9982cfffd470c3b38a0a4b0b (patch)
treefbe073650de751486725844ed41dfe70986a914d /dice/src/example/java/bjc/dicelang/neodice
parentb5c2fb1ed923d43412694729b4445a66fa9f47fc (diff)
Make dice generic
Convert dice from dealing exclusively with ints, to deal with objects of arbitrary types
Diffstat (limited to 'dice/src/example/java/bjc/dicelang/neodice')
-rw-r--r--dice/src/example/java/bjc/dicelang/neodice/DieBoxCLI.java190
-rw-r--r--dice/src/example/java/bjc/dicelang/neodice/commands/PolyhedralDieCommand.java6
-rw-r--r--dice/src/example/java/bjc/dicelang/neodice/commands/RollCommand.java9
-rw-r--r--dice/src/example/java/bjc/dicelang/neodice/statements/DiePoolStatementValue.java6
-rw-r--r--dice/src/example/java/bjc/dicelang/neodice/statements/DieStatementValue.java6
-rw-r--r--dice/src/example/java/bjc/dicelang/neodice/statements/IntArrayStatementValue.java50
-rw-r--r--dice/src/example/java/bjc/dicelang/neodice/statements/StatementValue.java2
7 files changed, 120 insertions, 149 deletions
diff --git a/dice/src/example/java/bjc/dicelang/neodice/DieBoxCLI.java b/dice/src/example/java/bjc/dicelang/neodice/DieBoxCLI.java
index 557fd51..1d6e808 100644
--- a/dice/src/example/java/bjc/dicelang/neodice/DieBoxCLI.java
+++ b/dice/src/example/java/bjc/dicelang/neodice/DieBoxCLI.java
@@ -20,61 +20,55 @@ import bjc.funcdata.*;
*/
public class DieBoxCLI {
private static final Pattern INT_PATTERN = Pattern.compile("(?:\\+|-)?\\d+");
- Scanner input;
+ Scanner input;
public PrintStream output;
-
+
public IMap<String, StatementValue> bindings = new FunctionalMap<>();
public Random rng = new Random();
-
+
static final IMap<String, Command> builtInCommands;
static final IMap<String, Command> builtInliterals;
-
+
final IMap<String, Command> commands;
final IMap<String, Command> literals;
-
+
private int numStatements = 0;
/**
* Whether or not to print out a prompt before asking for input
*/
public boolean doPrompt = true;
-
+
/**
* Whether or not to output the results of each command.
*/
public boolean doOutput = true;
-
+
/**
* Should warning messages be printed?
*/
public boolean doWarn = true;
-
+
static {
// Initialize all of our literal-formers
builtInliterals = new FunctionalMap<>();
-
- builtInliterals.put("void",
- new LiteralCommand(
- VOID_INST,
- "the unique instance of type VOID",
- "Returns a reference to the unique instance of type VOID."));
- builtInliterals.put("true",
- new LiteralCommand(
- TRUE_INST,
- "the unique true value of type BOOLEAN",
- "Returns a reference to the unique true instance of type BOOLEAN"));
- builtInliterals.put("false",
- new LiteralCommand(
- FALSE_INST,
- "the unique false value of type BOOLEAN",
- "Returns a reference to the unique false instance of type BOOLEAN"));
-
+
+ builtInliterals.put("void", new LiteralCommand(VOID_INST,
+ "the unique instance of type VOID",
+ "Returns a reference to the unique instance of type VOID."));
+ builtInliterals.put("true", new LiteralCommand(TRUE_INST,
+ "the unique true value of type BOOLEAN",
+ "Returns a reference to the unique true instance of type BOOLEAN"));
+ builtInliterals.put("false", new LiteralCommand(FALSE_INST,
+ "the unique false value of type BOOLEAN",
+ "Returns a reference to the unique false instance of type BOOLEAN"));
+
builtInliterals.deepFreeze();
-
- // Initialize all of our built-in commands
+
+ // Initialize all of our built-in commands
builtInCommands = new FunctionalMap<>();
-
+
builtInCommands.put("show-bindings", new ShowBindingsCommand());
builtInCommands.put("bind", new BindCommand());
builtInCommands.put("polyhedral-die", new PolyhedralDieCommand());
@@ -82,26 +76,30 @@ public class DieBoxCLI {
builtInCommands.put("help", new HelpCommand());
builtInCommands.deepFreeze();
}
-
+
/**
* Create a new CLI for interacting with dice.
*
- * @param input The place to read input from.
- * @param output The place to read output from.
+ * @param input
+ * The place to read input from.
+ * @param output
+ * The place to read output from.
*/
public DieBoxCLI(Scanner input, PrintStream output) {
- this.input = input;
+ this.input = input;
this.output = output;
-
+
this.commands = builtInCommands.extend();
this.literals = builtInliterals.extend();
}
-
+
/**
* Create a new CLI for interacting with dice.
*
- * @param input The place to read input from.
- * @param output The place to read output from.
+ * @param input
+ * The place to read input from.
+ * @param output
+ * The place to read output from.
*/
public DieBoxCLI(InputStream input, OutputStream output) {
this(new Scanner(input), new PrintStream(output));
@@ -110,45 +108,51 @@ public class DieBoxCLI {
/**
* Main method.
*
- * @param args Currently unused CLI arguments.
+ * @param args
+ * Currently unused CLI arguments.
*/
public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
+ Scanner input = new Scanner(System.in);
PrintStream output = System.out;
-
+
DieBoxCLI box = new DieBoxCLI(input, output);
box.run();
}
- private void run() {
+ private void run() {
if (doPrompt) {
output.println("diebox CLI - enter 'help' for help, 'exit' to exit");
}
-
- if (doPrompt) output.printf("diebox(%d)> ", numStatements);
- while(input.hasNextLine()) {
+
+ if (doPrompt)
+ output.printf("diebox(%d)> ", numStatements);
+ while (input.hasNextLine()) {
String nextLine = input.nextLine().trim();
-
+
numStatements += 1;
-
- if (nextLine.equals("")) continue;
+
+ if (nextLine.equals(""))
+ continue;
// @FIXME Nov 15th, 2020 Ben Culkin :HardcodeExit
// Exit should not be hard-coded like this
- if (nextLine.equals("exit")) break;
-
+ if (nextLine.equals("exit"))
+ break;
+
String[] lineWords = nextLine.split("\\s+");
Iterator<String> wordIter = new ArrayIterator<>(lineWords);
try {
StatementValue val = runStatement(wordIter);
-
- if (doOutput) output.printf("%s%s\n", doPrompt ? "==> " : "", val);
+
+ if (doOutput)
+ output.printf("%s%s\n", doPrompt ? "==> " : "",
+ val);
} catch (DieBoxException dbex) {
output.printf("ERROR (in statement %d): %s\n",
numStatements, dbex.getMessage());
Throwable curEx = dbex.getCause();
while (curEx != null) {
output.printf("...caused by: %s\n", curEx);
-
+
curEx = dbex.getCause();
}
} catch (Exception ex) {
@@ -156,10 +160,11 @@ public class DieBoxCLI {
numStatements, ex.getMessage());
ex.printStackTrace(output);
}
-
- if (doPrompt) output.printf("diebox(%d)> ", numStatements);
+
+ if (doPrompt)
+ output.printf("diebox(%d)> ", numStatements);
}
-
+
input.close();
output.close();
}
@@ -168,52 +173,57 @@ public class DieBoxCLI {
if (!words.hasNext()) {
return VOID_INST;
}
-
+
String command = words.next().trim();
-
+ DieBoxCLI state = this;
+
if (command.startsWith("$")) {
// All variable refs start with $
String varName = command.substring(1);
-
- if (bindings.containsKey(varName)) {
- return bindings.get(varName);
- } else {
- // @TODO Nov 15th, 2020 Ben Culkin :Autovars
- // Perhaps something along the lines of 'auto-variables' (here
- // called 'spring-loaded variables') should be created? These
- // would be essentially values which invoke a given expression
- // whenever they are referenced.
- throw new DieBoxException("Attempted to reference non-existing variable %s", varName);
- }
+
+ return bindings.get(varName)
+ .orElseThrow(() -> new DieBoxException(
+ "Attempted to reference non-existing variable %s",
+ varName));
+ // @TODO Nov 15th, 2020 Ben Culkin :Autovars
+ // Perhaps something along the lines of 'auto-variables' (here
+ // called 'spring-loaded variables') should be created? These
+ // would be essentially values which invoke a given expression
+ // whenever they are referenced.
} else if (command.startsWith("#")) {
// All literals/literal-formers start with #
String actualCommand = command.substring(1);
-
+
// Attempt to use a mapped literal/literal-former
- Command literalCommand = literals.get(actualCommand);
- if (literalCommand != null) {
- return literalCommand.execute(words, this);
- } else {
- if (INT_PATTERN.matcher(actualCommand).matches()) {
- try {
- int val = Integer.parseInt(actualCommand);
-
- return new IntegerStatementValue(val);
- } catch (NumberFormatException nfex) {
- throw new DieBoxException(nfex, "Improper integer literal (%s)", actualCommand);
- }
- } else {
- throw new DieBoxException("Unknown literal format (%s)", actualCommand);
- }
+ StatementValue val = literals.get(actualCommand)
+ .map((com) -> com.execute(words, state))
+ .orElseGet(() -> parseActualLiteral(actualCommand));
+
+ return val;
+ } else {
+ // Attempt to use a mapped command first
+ StatementValue val = commands.get(command)
+ .map((com) -> com.execute(words, state))
+ .orElseThrow(() -> new DieBoxException(
+ "Unknown command %s", command));
+ return val;
+ }
+ }
+
+ private StatementValue parseActualLiteral(String litText) {
+ if (INT_PATTERN.matcher(litText).matches()) {
+ try {
+ int val = Integer.parseInt(litText);
+
+ return new IntegerStatementValue(val);
+ } catch (NumberFormatException nfex) {
+ throw new DieBoxException(nfex,
+ "Improper integer literal (%s)",
+ litText);
}
} else {
- // Attempt to use a mapped command first
- Command mapCommand = commands.get(command);
- if (mapCommand != null) {
- return mapCommand.execute(words, this);
- } else {
- throw new DieBoxException("Unknown command %s", command);
- }
+ throw new DieBoxException("Unknown literal format (%s)",
+ litText);
}
}
} \ 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
index 95e9dff..6af495e 100644
--- a/dice/src/example/java/bjc/dicelang/neodice/commands/PolyhedralDieCommand.java
+++ b/dice/src/example/java/bjc/dicelang/neodice/commands/PolyhedralDieCommand.java
@@ -20,7 +20,11 @@ public class PolyhedralDieCommand implements Command {
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(Die.polyhedral(numSides));
+ IDie<StatementValue> die = IDie
+ .polyhedral(numSides)
+ .transform(IntegerStatementValue::new);
+
+ return new DieStatementValue(INTEGER, die);
} else {
throw new DieBoxException("Number of sides to polyhedral-die wasn't an integer (was %s, of type %s)",
sideValue, sideValue.type);
diff --git a/dice/src/example/java/bjc/dicelang/neodice/commands/RollCommand.java b/dice/src/example/java/bjc/dicelang/neodice/commands/RollCommand.java
index eb7b597..eb8beda 100644
--- a/dice/src/example/java/bjc/dicelang/neodice/commands/RollCommand.java
+++ b/dice/src/example/java/bjc/dicelang/neodice/commands/RollCommand.java
@@ -18,11 +18,16 @@ public class RollCommand implements Command {
if (toRoll.type == DIE) {
DieStatementValue die = (DieStatementValue) toRoll;
- return new IntegerStatementValue(die.value.roll(state.rng));
+ return die.value.roll(state.rng);
} else if (toRoll.type == DIEPOOL) {
DiePoolStatementValue pool = (DiePoolStatementValue) toRoll;
- return new IntArrayStatementValue(pool.value.roll(state.rng));
+ StatementValue[] values = pool.value
+ .roll(state.rng)
+ .toArray((sz) -> new StatementValue[sz]);
+
+ return new ArrayStatementValue<>(pool.elementType,
+ values);
} 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);
diff --git a/dice/src/example/java/bjc/dicelang/neodice/statements/DiePoolStatementValue.java b/dice/src/example/java/bjc/dicelang/neodice/statements/DiePoolStatementValue.java
index a4a9104..551c0e9 100644
--- a/dice/src/example/java/bjc/dicelang/neodice/statements/DiePoolStatementValue.java
+++ b/dice/src/example/java/bjc/dicelang/neodice/statements/DiePoolStatementValue.java
@@ -7,11 +7,13 @@ import java.util.*;
import bjc.dicelang.neodice.*;
public class DiePoolStatementValue extends StatementValue {
- public final DiePool value;
+ public final Type elementType;
+ public final IDiePool<StatementValue> value;
- public DiePoolStatementValue(DiePool value) {
+ public DiePoolStatementValue(Type elementType, IDiePool<StatementValue> value) {
super(DIEPOOL);
+ this.elementType = elementType;
this.value = value;
}
diff --git a/dice/src/example/java/bjc/dicelang/neodice/statements/DieStatementValue.java b/dice/src/example/java/bjc/dicelang/neodice/statements/DieStatementValue.java
index 55a6856..e1dbde8 100644
--- a/dice/src/example/java/bjc/dicelang/neodice/statements/DieStatementValue.java
+++ b/dice/src/example/java/bjc/dicelang/neodice/statements/DieStatementValue.java
@@ -7,11 +7,13 @@ import java.util.*;
import bjc.dicelang.neodice.*;
public class DieStatementValue extends StatementValue {
- public final Die value;
+ public final Type sideType;
+ public final IDie<StatementValue> value;
- public DieStatementValue(Die value) {
+ public DieStatementValue(Type sideType, IDie<StatementValue> value) {
super(DIE);
+ this.sideType = sideType;
this.value = value;
}
diff --git a/dice/src/example/java/bjc/dicelang/neodice/statements/IntArrayStatementValue.java b/dice/src/example/java/bjc/dicelang/neodice/statements/IntArrayStatementValue.java
deleted file mode 100644
index b313d42..0000000
--- a/dice/src/example/java/bjc/dicelang/neodice/statements/IntArrayStatementValue.java
+++ /dev/null
@@ -1,50 +0,0 @@
-package bjc.dicelang.neodice.statements;
-
-import static bjc.dicelang.neodice.statements.StatementValue.Type.*;
-
-import java.util.*;
-
-public class IntArrayStatementValue extends StatementValue {
- public final int[] values;
-
- public IntArrayStatementValue(int... values) {
- super(INT_ARRAY);
-
- this.values = values;
- }
-
- @Override
- public String toString() {
- StringBuilder buffer = new StringBuilder();
-
- buffer.append('(');
- for (int index = 0; index < values.length; index++) {
- int value = values[index];
-
- buffer.append(value);
- if (index < values.length - 1) buffer.append(", ");
- }
- buffer.append(')');
-
- return buffer.toString();
- }
-
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = super.hashCode();
- result = prime * result + Arrays.hashCode(values);
- return result;
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj) return true;
- if (!super.equals(obj)) return false;
- if (getClass() != obj.getClass()) return false;
-
- IntArrayStatementValue other = (IntArrayStatementValue) obj;
-
- return Arrays.equals(values, other.values);
- }
-} \ No newline at end of file
diff --git a/dice/src/example/java/bjc/dicelang/neodice/statements/StatementValue.java b/dice/src/example/java/bjc/dicelang/neodice/statements/StatementValue.java
index 5090bc7..de796e5 100644
--- a/dice/src/example/java/bjc/dicelang/neodice/statements/StatementValue.java
+++ b/dice/src/example/java/bjc/dicelang/neodice/statements/StatementValue.java
@@ -8,8 +8,6 @@ public abstract class StatementValue {
BOOLEAN,
INTEGER,
- INT_ARRAY,
-
DIE,
DIEPOOL,