diff options
| author | Ben Culkin <scorpress@gmail.com> | 2020-11-21 18:04:20 -0500 |
|---|---|---|
| committer | Ben Culkin <scorpress@gmail.com> | 2020-11-21 18:04:20 -0500 |
| commit | 3ddd062d60d621971af59b480ba70e8bf9e705f1 (patch) | |
| tree | 8d24f91e4f695a526777b5fe75fe268669b8c9a6 | |
| parent | 2afb54eecd8e8b5d663a05131c07c6b8d15e65ba (diff) | |
Rudimentary CLI for new die implementation
18 files changed, 880 insertions, 0 deletions
diff --git a/dice/src/example/java/bjc/dicelang/neodice/DieBoxCLI.java b/dice/src/example/java/bjc/dicelang/neodice/DieBoxCLI.java new file mode 100644 index 0000000..557fd51 --- /dev/null +++ b/dice/src/example/java/bjc/dicelang/neodice/DieBoxCLI.java @@ -0,0 +1,219 @@ +package bjc.dicelang.neodice; + +import static bjc.dicelang.neodice.statements.BooleanStatementValue.*; +import static bjc.dicelang.neodice.statements.VoidStatementValue.*; + +import java.io.*; +import java.util.*; +import java.util.regex.*; + +import bjc.data.*; +import bjc.dicelang.neodice.commands.*; +import bjc.dicelang.neodice.statements.*; +import bjc.funcdata.*; + +/** + * Command-line interface for testing the neodice implementation. + * + * @author Ben Culkin + * + */ +public class DieBoxCLI { + private static final Pattern INT_PATTERN = Pattern.compile("(?:\\+|-)?\\d+"); + 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.deepFreeze(); + + // 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()); + builtInCommands.put("roll", new RollCommand()); + 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. + */ + public DieBoxCLI(Scanner input, PrintStream output) { + 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. + */ + public DieBoxCLI(InputStream input, OutputStream output) { + this(new Scanner(input), new PrintStream(output)); + } + + /** + * Main method. + * + * @param args Currently unused CLI arguments. + */ + public static void main(String[] args) { + Scanner input = new Scanner(System.in); + PrintStream output = System.out; + + DieBoxCLI box = new DieBoxCLI(input, output); + box.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()) { + String nextLine = input.nextLine().trim(); + + numStatements += 1; + + if (nextLine.equals("")) continue; + // @FIXME Nov 15th, 2020 Ben Culkin :HardcodeExit + // Exit should not be hard-coded like this + 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); + } 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) { + output.printf("INTERNAL ERROR (in statement %d): %s\n", + numStatements, ex.getMessage()); + ex.printStackTrace(output); + } + + if (doPrompt) output.printf("diebox(%d)> ", numStatements); + } + + input.close(); + output.close(); + } + + public StatementValue runStatement(Iterator<String> words) { + if (!words.hasNext()) { + return VOID_INST; + } + + String command = words.next().trim(); + + 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); + } + } 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); + } + } + } 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); + } + } + } +}
\ No newline at end of file diff --git a/dice/src/example/java/bjc/dicelang/neodice/DieBoxException.java b/dice/src/example/java/bjc/dicelang/neodice/DieBoxException.java new file mode 100644 index 0000000..efa3d54 --- /dev/null +++ b/dice/src/example/java/bjc/dicelang/neodice/DieBoxException.java @@ -0,0 +1,29 @@ +package bjc.dicelang.neodice; + +public class DieBoxException extends RuntimeException { + private static final long serialVersionUID = 1851356458656622896L; + + public DieBoxException() { + super(); + } + + public DieBoxException(String message) { + super(message); + } + + public DieBoxException(String format, Object... args) { + super(String.format(format, args)); + } + + public DieBoxException(Throwable cause) { + super(cause); + } + + public DieBoxException(Throwable cause, String message) { + super(message, cause); + } + + public DieBoxException(Throwable cause, String format, Object... args) { + super(String.format(format, args), cause); + } +}
\ No newline at end of file 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 diff --git a/dice/src/example/java/bjc/dicelang/neodice/statements/ArrayStatementValue.java b/dice/src/example/java/bjc/dicelang/neodice/statements/ArrayStatementValue.java new file mode 100644 index 0000000..66e14ad --- /dev/null +++ b/dice/src/example/java/bjc/dicelang/neodice/statements/ArrayStatementValue.java @@ -0,0 +1,54 @@ +package bjc.dicelang.neodice.statements; + +import static bjc.dicelang.neodice.statements.StatementValue.Type.*; + +import java.util.*; + +public class ArrayStatementValue<ElementType extends StatementValue> extends StatementValue { + public final Type elementType; + public final ElementType[] values; + + @SafeVarargs + public ArrayStatementValue(Type elementType, ElementType... values) { + super(ARRAY); + + this.elementType = elementType; + this.values = values; + } + + @Override + public String toString() { + StringBuilder buffer = new StringBuilder(); + + buffer.append('('); + for (int index = 0; index < values.length; index++) { + ElementType 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); + result = prime * result + Objects.hash(elementType); + 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; + + ArrayStatementValue<?> other = (ArrayStatementValue<?>) obj; + + return elementType == other.elementType && Arrays.equals(values, other.values); + } +}
\ No newline at end of file diff --git a/dice/src/example/java/bjc/dicelang/neodice/statements/BooleanStatementValue.java b/dice/src/example/java/bjc/dicelang/neodice/statements/BooleanStatementValue.java new file mode 100644 index 0000000..ba89893 --- /dev/null +++ b/dice/src/example/java/bjc/dicelang/neodice/statements/BooleanStatementValue.java @@ -0,0 +1,39 @@ +package bjc.dicelang.neodice.statements; + +import static bjc.dicelang.neodice.statements.StatementValue.Type.*; + +import java.util.*; + +public class BooleanStatementValue extends StatementValue { + private boolean value; + + public static final BooleanStatementValue TRUE_INST = new BooleanStatementValue(true); + public static final BooleanStatementValue FALSE_INST = new BooleanStatementValue(false); + + private BooleanStatementValue(boolean value) { + super(BOOLEAN); + + this.value = value; + } + + @Override + public String toString() { + return value ? "(true)" : "(false)"; + } + + @Override + public int hashCode() { + return Objects.hash(value); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null) return false; + if (getClass() != obj.getClass()) return false; + + BooleanStatementValue other = (BooleanStatementValue) obj; + + return value == other.value; + } +}
\ No newline at end of file diff --git a/dice/src/example/java/bjc/dicelang/neodice/statements/DiePoolStatementValue.java b/dice/src/example/java/bjc/dicelang/neodice/statements/DiePoolStatementValue.java new file mode 100644 index 0000000..a4a9104 --- /dev/null +++ b/dice/src/example/java/bjc/dicelang/neodice/statements/DiePoolStatementValue.java @@ -0,0 +1,38 @@ +package bjc.dicelang.neodice.statements; + +import static bjc.dicelang.neodice.statements.StatementValue.Type.*; + +import java.util.*; + +import bjc.dicelang.neodice.*; + +public class DiePoolStatementValue extends StatementValue { + public final DiePool value; + + public DiePoolStatementValue(DiePool value) { + super(DIEPOOL); + + this.value = value; + } + + @Override + public String toString() { + return "(" + value.toString() + ")"; + } + + @Override + public int hashCode() { + return Objects.hash(value); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null) return false; + if (getClass() != obj.getClass()) return false; + + DiePoolStatementValue other = (DiePoolStatementValue) obj; + + return Objects.equals(value, other.value); + } +}
\ No newline at end of file diff --git a/dice/src/example/java/bjc/dicelang/neodice/statements/DieStatementValue.java b/dice/src/example/java/bjc/dicelang/neodice/statements/DieStatementValue.java new file mode 100644 index 0000000..55a6856 --- /dev/null +++ b/dice/src/example/java/bjc/dicelang/neodice/statements/DieStatementValue.java @@ -0,0 +1,38 @@ +package bjc.dicelang.neodice.statements; + +import static bjc.dicelang.neodice.statements.StatementValue.Type.*; + +import java.util.*; + +import bjc.dicelang.neodice.*; + +public class DieStatementValue extends StatementValue { + public final Die value; + + public DieStatementValue(Die value) { + super(DIE); + + this.value = value; + } + + @Override + public String toString() { + return "(" + value.toString() + ")"; + } + + @Override + public int hashCode() { + return Objects.hash(value); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null) return false; + if (getClass() != obj.getClass()) return false; + + DieStatementValue other = (DieStatementValue) obj; + + return Objects.equals(value, other.value); + } +}
\ No newline at end of file diff --git a/dice/src/example/java/bjc/dicelang/neodice/statements/IntArrayStatementValue.java b/dice/src/example/java/bjc/dicelang/neodice/statements/IntArrayStatementValue.java new file mode 100644 index 0000000..b313d42 --- /dev/null +++ b/dice/src/example/java/bjc/dicelang/neodice/statements/IntArrayStatementValue.java @@ -0,0 +1,50 @@ +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/IntegerStatementValue.java b/dice/src/example/java/bjc/dicelang/neodice/statements/IntegerStatementValue.java new file mode 100644 index 0000000..91e45b6 --- /dev/null +++ b/dice/src/example/java/bjc/dicelang/neodice/statements/IntegerStatementValue.java @@ -0,0 +1,18 @@ +package bjc.dicelang.neodice.statements; + +import static bjc.dicelang.neodice.statements.StatementValue.Type.*; + +public class IntegerStatementValue extends StatementValue { + public final int value; + + public IntegerStatementValue(int value) { + super(INTEGER); + + this.value = value; + } + + @Override + public String toString() { + return "(" + value + ")"; + } +}
\ 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 new file mode 100644 index 0000000..5090bc7 --- /dev/null +++ b/dice/src/example/java/bjc/dicelang/neodice/statements/StatementValue.java @@ -0,0 +1,40 @@ +package bjc.dicelang.neodice.statements; + +import java.util.*; + +public abstract class StatementValue { + public static enum Type { + VOID, + BOOLEAN, + INTEGER, + + INT_ARRAY, + + DIE, + DIEPOOL, + + ARRAY, + } + + public final Type type; + + protected StatementValue(Type type) { + this.type = type; + } + + @Override + public int hashCode() { + return Objects.hash(type); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null) return false; + if (getClass() != obj.getClass()) return false; + + StatementValue other = (StatementValue) obj; + + return type == other.type; + } +}
\ No newline at end of file diff --git a/dice/src/example/java/bjc/dicelang/neodice/statements/VoidStatementValue.java b/dice/src/example/java/bjc/dicelang/neodice/statements/VoidStatementValue.java new file mode 100644 index 0000000..7e437e7 --- /dev/null +++ b/dice/src/example/java/bjc/dicelang/neodice/statements/VoidStatementValue.java @@ -0,0 +1,20 @@ +package bjc.dicelang.neodice.statements; + +import static bjc.dicelang.neodice.statements.StatementValue.Type.*; + +/** + * @author Ben Culkin + * + */ +public class VoidStatementValue extends StatementValue { + public static final VoidStatementValue VOID_INST = new VoidStatementValue(); + + private VoidStatementValue() { + super(VOID); + } + + @Override + public String toString() { + return "(void)"; + } +} diff --git a/dice/src/test/java/bjc/dicelang/neodice/DieBoxCLITest.java b/dice/src/test/java/bjc/dicelang/neodice/DieBoxCLITest.java new file mode 100644 index 0000000..678194b --- /dev/null +++ b/dice/src/test/java/bjc/dicelang/neodice/DieBoxCLITest.java @@ -0,0 +1,34 @@ +package bjc.dicelang.neodice; + +import static org.junit.Assert.*; + +import java.io.*; + +import org.junit.*; + +@SuppressWarnings("javadoc") +public class DieBoxCLITest { + private DieBoxCLI diebox; + + private OutputStream dieBoxInput; + private InputStream dieBoxOutput; + + @Before + public void setUp() throws Exception { + PipedInputStream pipeInput = new PipedInputStream(); + dieBoxInput = new PipedOutputStream(pipeInput); + + PipedOutputStream pipeOutput = new PipedOutputStream(); + dieBoxOutput = new PipedInputStream(pipeOutput); + + diebox = new DieBoxCLI(pipeInput, pipeOutput); + } + + @After + public void tearDown() throws Exception { + dieBoxInput.close(); + dieBoxOutput.close(); + } + + // @TODO write some tests +} |
