diff options
| author | bjculkin <bjculkin@mix.wvu.edu> | 2018-03-01 19:13:48 -0500 |
|---|---|---|
| committer | bjculkin <bjculkin@mix.wvu.edu> | 2018-03-01 19:13:48 -0500 |
| commit | f9d9bd4bbf7dd6a297e1daf5ee7b4263d706d9cd (patch) | |
| tree | 75ade2ca798bcdbdd7daf867480378299598581a | |
| parent | b14a399d05fc90d8532cd08d1546e6bf197db10e (diff) | |
Update
55 files changed, 1415 insertions, 1345 deletions
diff --git a/base/src/bjc/dicelang/CompilerTweaker.java b/base/src/bjc/dicelang/CompilerTweaker.java index b11caa9..5f2b9f6 100644 --- a/base/src/bjc/dicelang/CompilerTweaker.java +++ b/base/src/bjc/dicelang/CompilerTweaker.java @@ -21,7 +21,7 @@ public class CompilerTweaker { * Create a new compiler tweaker. * * @param engine - * The engine to tweak. + * The engine to tweak. */ public CompilerTweaker(final DiceLangEngine engine) { eng = engine; @@ -33,7 +33,7 @@ public class CompilerTweaker { * Add a string literal to the compiler's internal banks. * * @param val - * The string literal to add. + * The string literal to add. * * @return The key into the string literal table for this string. */ @@ -48,7 +48,7 @@ public class CompilerTweaker { * Add a line defn to the compiler. * * @param dfn - * The defn to add. + * The defn to add. */ public void addLineDefine(final Define dfn) { eng.addLineDefine(dfn); @@ -58,7 +58,7 @@ public class CompilerTweaker { * Add a token defn to the compiler. * * @param dfn - * The defn to add. + * The defn to add. */ public void addTokenDefine(final Define dfn) { eng.addTokenDefine(dfn); @@ -68,7 +68,7 @@ public class CompilerTweaker { * Adds delimiters that are expanded from tokens. * * @param delims - * The delimiters to expand on. + * The delimiters to expand on. */ public void addDelimiter(final String... delims) { opExpander.addSimpleDelimiters(delims); @@ -78,7 +78,7 @@ public class CompilerTweaker { * Adds multi-character delimiters that are expanded from tokens. * * @param delims - * The multi-character delimiters to expand on. + * The multi-character delimiters to expand on. */ public void addMultiDelimiter(final String... delims) { opExpander.addMultiDelimiters(delims); @@ -95,7 +95,7 @@ public class CompilerTweaker { * Change the max no. of times defines are allowed to recur. * * @param times - * The number of times to allow defines to recur. + * The number of times to allow defines to recur. */ public static void setDefineRecurLimit(final int times) { Define.MAX_RECURS = times; diff --git a/base/src/bjc/dicelang/Define.java b/base/src/bjc/dicelang/Define.java index b11e10b..b0e8a21 100644 --- a/base/src/bjc/dicelang/Define.java +++ b/base/src/bjc/dicelang/Define.java @@ -63,25 +63,25 @@ public class Define implements UnaryOperator<String>, Comparable<Define> { * Create a new define. * * @param priorty - * The priority of the define. + * The priority of the define. * * @param isSub - * Whether or not this is a 'sub-define' + * Whether or not this is a 'sub-define' * * @param recur - * Whether this define is recursive or not. + * Whether this define is recursive or not. * * @param isCircular - * Whether this define is circular or not. + * Whether this define is circular or not. * * @param predicte - * The string to use as a predicate. + * The string to use as a predicate. * * @param searchr - * The string to use as a search. + * The string to use as a search. * * @param replacrs - * The source for replacement strings. + * The source for replacement strings. */ public Define(final int priorty, final boolean isSub, final boolean recur, final boolean isCircular, final String predicte, final String searchr, final Iterable<String> replacrs) { @@ -90,10 +90,10 @@ public class Define implements UnaryOperator<String>, Comparable<Define> { subType = isSub; /* Only try to compile non-null predicates */ - if (predicte != null) { + if(predicte != null) { try { predicate = Pattern.compile(predicte); - } catch (final PatternSyntaxException psex) { + } catch(final PatternSyntaxException psex) { Errors.inst.printError(EK_DFN_PREDSYN, psex.getMessage()); inError = true; return; @@ -103,7 +103,7 @@ public class Define implements UnaryOperator<String>, Comparable<Define> { /* Compile the search pattern */ try { searcher = Pattern.compile(searchr); - } catch (final PatternSyntaxException psex) { + } catch(final PatternSyntaxException psex) { Errors.inst.printError(EK_DFN_SRCSYN, psex.getMessage()); inError = true; return; @@ -112,8 +112,8 @@ public class Define implements UnaryOperator<String>, Comparable<Define> { inError = false; /* Check whether or not we do sub-replacements */ - if (subType) { - if (replacrs.iterator().hasNext()) { + if(subType) { + if(replacrs.iterator().hasNext()) { replacers = new CircularIterator<>(replacrs, isCircular); } else { replacers = null; @@ -121,7 +121,7 @@ public class Define implements UnaryOperator<String>, Comparable<Define> { } else { final Iterator<String> itr = replacrs.iterator(); - if (itr.hasNext()) { + if(itr.hasNext()) { replacer = itr.next(); } else { replacer = ""; @@ -131,22 +131,22 @@ public class Define implements UnaryOperator<String>, Comparable<Define> { @Override public String apply(final String tok) { - if (inError) { + if(inError) { return tok; } - if (predicate != null) { - if (!predicate.matcher(tok).matches()) { + if(predicate != null) { + if(!predicate.matcher(tok).matches()) { return tok; } } String strang = doPass(tok); - if (doRecur) { + if(doRecur) { int recurCount = 0; - if (strang.equals(tok)) { + if(strang.equals(tok)) { return strang; } @@ -155,9 +155,9 @@ public class Define implements UnaryOperator<String>, Comparable<Define> { do { strang = doPass(tok); recurCount += 1; - } while (!strang.equals(oldStrang) && recurCount < MAX_RECURS); + } while(!strang.equals(oldStrang) && recurCount < MAX_RECURS); - if (recurCount >= MAX_RECURS) { + if(recurCount >= MAX_RECURS) { Errors.inst.printError(EK_DFN_RECUR, Integer.toString(MAX_RECURS), tok, strang); return strang; } @@ -170,11 +170,11 @@ public class Define implements UnaryOperator<String>, Comparable<Define> { private String doPass(final String tok) { final Matcher searcherMatcher = searcher.matcher(tok); - if (subType) { + if(subType) { final StringBuffer sb = new StringBuffer(); - while (searcherMatcher.find()) { - if (replacers == null) { + while(searcherMatcher.find()) { + if(replacers == null) { searcherMatcher.appendReplacement(sb, ""); } else { final String replac = replacers.next(); diff --git a/base/src/bjc/dicelang/DiceLangEngine.java b/base/src/bjc/dicelang/DiceLangEngine.java index f8fc5c6..2aafcd5 100644 --- a/base/src/bjc/dicelang/DiceLangEngine.java +++ b/base/src/bjc/dicelang/DiceLangEngine.java @@ -13,6 +13,7 @@ import bjc.dicelang.eval.Evaluator; import bjc.dicelang.eval.EvaluatorResult; import bjc.dicelang.eval.FailureEvaluatorResult; import bjc.dicelang.scl.StreamEngine; +import bjc.dicelang.tokens.Token; import bjc.utils.data.ITree; import bjc.utils.funcdata.FunctionalList; import bjc.utils.funcdata.FunctionalMap; @@ -24,7 +25,7 @@ import bjc.utils.parserutils.TokenUtils; import bjc.utils.parserutils.splitter.ConfigurableTokenSplitter; import static bjc.dicelang.Errors.ErrorKey.*; -import static bjc.dicelang.Token.Type.*; +import static bjc.dicelang.tokens.Token.Type.*; /** * Implements the orchestration necessary for processing DiceLang commands. @@ -36,8 +37,9 @@ public class DiceLangEngine { private static final Logger LOG = Logger.getLogger(DiceLangEngine.class.getName()); /* - * The random fields that are package private instead of private-private are for - * the benefit of the tweaker, so that it can mess around with them. + * The random fields that are package private instead of private-private + * are for the benefit of the tweaker, so that it can mess around with + * them. */ /* Split tokens around operators with regex */ @@ -144,7 +146,7 @@ public class DiceLangEngine { * Add a defn that's applied to lines. * * @param dfn - * The defn to add. + * The defn to add. */ public void addLineDefine(final Define dfn) { lineDefns.add(dfn); @@ -156,7 +158,7 @@ public class DiceLangEngine { * Add a defn that's applied to tokens. * * @param dfn - * The defn to add. + * The defn to add. */ public void addTokenDefine(final Define dfn) { tokenDefns.add(dfn); @@ -215,7 +217,7 @@ public class DiceLangEngine { * Run a command to completion. * * @param command - * The command to run + * The command to run * * @return Whether or not the command ran successfully */ @@ -224,18 +226,19 @@ public class DiceLangEngine { /* * @NOTE * - * Instead of strings, this should maybe use a RawToken class or something. + * Instead of strings, this should maybe use a RawToken class or + * something. */ final IList<String> preprocessedTokens = preprocessCommand(command); - if (preprocessedTokens == null) { + if(preprocessedTokens == null) { return false; } /* Lex the string tokens into token-tokens */ final IList<Token> lexedTokens = lexTokens(preprocessedTokens); - if (lexedTokens == null) { + if(lexedTokens == null) { return false; } @@ -243,7 +246,7 @@ public class DiceLangEngine { final IList<ITree<Node>> astForest = new FunctionalList<>(); final boolean succ = Parser.parseTokens(lexedTokens, astForest); - if (!succ) { + if(!succ) { return false; } @@ -256,14 +259,14 @@ public class DiceLangEngine { private IList<Token> lexTokens(final IList<String> preprocessedTokens) { final IList<Token> lexedTokens = new FunctionalList<>(); - for (final String token : preprocessedTokens) { + for(final String token : preprocessedTokens) { String newTok = token; /* Apply token defns */ - for (final Define dfn : tokenDefns.toIterable()) { + for(final Define dfn : tokenDefns.toIterable()) { /* - * @NOTE What happens with a define that produces multiple tokens from one - * token? + * @NOTE What happens with a define that + * produces multiple tokens from one token? */ newTok = dfn.apply(newTok); } @@ -271,14 +274,14 @@ public class DiceLangEngine { /* Lex the token */ final Token tk = tokenzer.lexToken(token, stringLiterals); - if (debugMode) { + if(debugMode) { LOG.finer(String.format("lexed token: %s\n", tk)); } - if (tk == null) { + if(tk == null) { /* Ignore blank tokens */ continue; - } else if (tk == Token.NIL_TOKEN) { + } else if(tk == Token.NIL_TOKEN) { /* Fail on bad tokens */ return null; } else { @@ -286,7 +289,7 @@ public class DiceLangEngine { } } - if (debugMode) { + if(debugMode) { String msg = String.format("\tCommand after tokenization: %s\n", lexedTokens.toString()); LOG.fine(msg); System.out.print(msg); @@ -298,35 +301,37 @@ public class DiceLangEngine { boolean succ = removePreshuntTokens(lexedTokens, preparedTokens); - if (!succ) { + if(!succ) { return null; } - if (debugMode && !postfixMode) { - String msg = String.format("\tCommand after pre-shunter removal: %s\n", preparedTokens.toString()); + if(debugMode && !postfixMode) { + String msg = String.format("\tCommand after pre-shunter removal: %s\n", + preparedTokens.toString()); LOG.fine(msg); System.out.print(msg); } /* Only shunt if we're not in a special mode. */ - if (!postfixMode && !prefixMode) { + if(!postfixMode && !prefixMode) { /* Shunt the tokens */ shuntedTokens = new FunctionalList<>(); succ = shunt.shuntTokens(preparedTokens, shuntedTokens); - if (!succ) { + if(!succ) { return null; } - } else if (prefixMode) { + } else if(prefixMode) { /* Reverse directional tokens */ /* - * @NOTE Merge these two operations into one iteration over the list? + * @NOTE Merge these two operations into one iteration + * over the list? */ preparedTokens.reverse(); shuntedTokens = preparedTokens.map(this::reverseToken); } - if (debugMode && !postfixMode) { + if(debugMode && !postfixMode) { String msg = String.format("\tCommand after shunting: %s\n", shuntedTokens.toString()); LOG.fine(msg); System.out.print(msg); @@ -334,7 +339,8 @@ public class DiceLangEngine { /* Expand token groups */ final IList<Token> readyTokens = shuntedTokens.flatMap(tk -> { - if (tk.type == Token.Type.TOKGROUP || tk.type == Token.Type.TAGOP || tk.type == Token.Type.TAGOPR) { + if(tk.type == Token.Type.TOKGROUP || tk.type == Token.Type.TAGOP + || tk.type == Token.Type.TAGOPR) { LOG.finer(String.format("Expanding token group to: %s\n", tk.tokenValues.toString())); return tk.tokenValues; } else { @@ -342,7 +348,7 @@ public class DiceLangEngine { } }); - if (debugMode && !postfixMode) { + if(debugMode && !postfixMode) { String msg = String.format("\tCommand after re-preshunting: %s\n", readyTokens.toString()); LOG.fine(msg); System.out.print(msg); @@ -357,7 +363,7 @@ public class DiceLangEngine { * These are things like (, {, and [ */ private Token reverseToken(final Token tk) { - switch (tk.type) { + switch(tk.type) { case OBRACE: return new Token(CBRACE, tk.intValue); case OPAREN: @@ -378,7 +384,7 @@ public class DiceLangEngine { /* Preprocess a command into a list of string tokens. */ private IList<String> preprocessCommand(final String command) { /* Sort the defines if they aren't sorted */ - if (!defnsSorted) { + if(!defnsSorted) { sortDefns(); } @@ -386,24 +392,24 @@ public class DiceLangEngine { final IList<String> streamToks = new FunctionalList<>(); final boolean succ = streamEng.doStreams(command.split(" "), streamToks); - if (!succ) { + if(!succ) { return null; } String newComm = ListUtils.collapseTokens(streamToks, " "); - if (debugMode) { + if(debugMode) { String msg = String.format("\tCommand after stream commands: %s\n", newComm); LOG.fine(msg); System.out.print(msg); } /* Apply line defns */ - for (final Define dfn : lineDefns.toIterable()) { + for(final Define dfn : lineDefns.toIterable()) { newComm = dfn.apply(newComm); } - if (debugMode) { + if(debugMode) { String msg = String.format("\tCommand after line defines: %s\n", newComm); LOG.fine(msg); System.out.print(msg); @@ -413,21 +419,23 @@ public class DiceLangEngine { final List<String> destringedParts = TokenUtils.removeDQuotedStrings(newComm); final StringBuffer destringedCommand = new StringBuffer(); - for (final String part : destringedParts) { + for(final String part : destringedParts) { /* Handle string literals */ - if (part.startsWith("\"") && part.endsWith("\"")) { + if(part.startsWith("\"") && part.endsWith("\"")) { /* Get the actual string. */ final String litName = "stringLiteral" + nextLiteral; final String litVal = part.substring(1, part.length() - 1); /* - * Insert the string with its escape sequences interpreted. + * Insert the string with its escape sequences + * interpreted. */ final String descVal = TokenUtils.descapeString(litVal); stringLiterals.put(litName, descVal); - if (debugMode) - LOG.finer(String.format("Replaced string literal '%s' with literal no. %d", descVal, nextLiteral)); + if(debugMode) + LOG.finer(String.format("Replaced string literal '%s' with literal no. %d", + descVal, nextLiteral)); nextLiteral += 1; @@ -438,13 +446,13 @@ public class DiceLangEngine { } } - if (debugMode) { + if(debugMode) { String msg = String.format("\tCommand after destringing: %s\n", destringedCommand); LOG.fine(msg); System.out.print(msg); /* Print the string table if it exists. */ - if (stringLiterals.size() > 0) { + if(stringLiterals.size() > 0) { System.out.println("\tString literals in table"); stringLiterals.forEach((key, val) -> { @@ -462,18 +470,19 @@ public class DiceLangEngine { tokens = tokens.map(tk -> { final Matcher nonExpandMatcher = nonExpandPattern.matcher(tk); - if (nonExpandMatcher.matches()) { + if(nonExpandMatcher.matches()) { final String tkName = "nonExpandToken" + nextLiteral++; nonExpandedTokens.put(tkName, nonExpandMatcher.group(1)); - LOG.finer(String.format("Pulled non-expander '%s' to '%s'", nonExpandMatcher.group(1), tkName)); + LOG.finer(String.format("Pulled non-expander '%s' to '%s'", nonExpandMatcher.group(1), + tkName)); return tkName; } return tk; }); - if (debugMode) { + if(debugMode) { String msg = String.format("\tCommand after removal of non-expanders: %s\n", tokens.toString()); LOG.fine(msg); System.out.print(msg); @@ -482,21 +491,21 @@ public class DiceLangEngine { /* Expand tokens */ IList<String> fullyExpandedTokens = tokens.flatMap(opExpander::split); - if (debugMode) { - String msg = String.format("\tCommand after token expansion: %s\n", fullyExpandedTokens.toString()); + if(debugMode) { + String msg = String.format("\tCommand after token expansion: %s\n", + fullyExpandedTokens.toString()); LOG.fine(msg); System.out.print(msg); } /* Reinsert non-expanded tokens */ fullyExpandedTokens = fullyExpandedTokens.map(tk -> { - if (tk.startsWith("nonExpandToken")) - return nonExpandedTokens.get(tk); + if(tk.startsWith("nonExpandToken")) return nonExpandedTokens.get(tk); return tk; }); - if (debugMode) { + if(debugMode) { String msg = String.format("\tCommand after non-expander reinsertion: %s\n", fullyExpandedTokens.toString()); LOG.fine(msg); @@ -510,25 +519,26 @@ public class DiceLangEngine { private void evaluateForest(final IList<ITree<Node>> astForest) { int treeNo = 1; - for (final ITree<Node> ast : astForest) { - if (debugMode) { + for(final ITree<Node> ast : astForest) { + if(debugMode) { System.out.printf("\t\tTree %d in forest:\n%s\n", treeNo, ast.toString()); } - if (debugMode && stepEval) { + if(debugMode && stepEval) { /* - * @NOTE This is broken until stepwise top-down tree transforms are fixed. + * @NOTE This is broken until stepwise top-down + * tree transforms are fixed. */ int step = 1; /* Evaluate it step by step */ - for (final Iterator<ITree<Node>> itr = eval.stepDebug(ast); itr.hasNext();) { + for(final Iterator<ITree<Node>> itr = eval.stepDebug(ast); itr.hasNext();) { final ITree<Node> nodeStep = itr.next(); System.out.printf("\t\tStep %d: Node is %s", step, nodeStep); /* Don't evaluate null steps */ - if (nodeStep == null) { + if(nodeStep == null) { System.out.println(); step += 1; @@ -536,18 +546,18 @@ public class DiceLangEngine { } /* Print out details for results */ - if (nodeStep.getHead().type == Node.Type.RESULT) { + if(nodeStep.getHead().type == Node.Type.RESULT) { final EvaluatorResult res = nodeStep.getHead().resultVal; System.out.printf(" (result is %s", res); - if (res.type == EvaluatorResult.Type.DICE) { + if(res.type == EvaluatorResult.Type.DICE) { String value = ((DiceEvaluatorResult) res).diceVal.value(); System.out.printf(" (sample roll %s)", value); } - if (res.type == EvaluatorResult.Type.FAILURE) { + if(res.type == EvaluatorResult.Type.FAILURE) { ITree<Node> otree = ((FailureEvaluatorResult) res).origVal; System.out.printf(" (original tree is %s)", otree); @@ -564,10 +574,10 @@ public class DiceLangEngine { /* Evaluate it normally */ final EvaluatorResult res = eval.evaluate(ast); - if (debugMode) { + if(debugMode) { System.out.printf("\t\tEvaluates to %s", res); - if (res.type == EvaluatorResult.Type.DICE) { + if(res.type == EvaluatorResult.Type.DICE) { String value = ((DiceEvaluatorResult) res).diceVal.value(); System.out.println("\t\t (sample roll " + value + ")"); @@ -590,24 +600,26 @@ public class DiceLangEngine { final Deque<IList<Token>> bracedTokens = new LinkedList<>(); IList<Token> curBracedTokens = new FunctionalList<>(); - for (final Token tk : lexedTokens) { - if (tk.type == Token.Type.OBRACE && tk.intValue == 2) { + for(final Token tk : lexedTokens) { + if(tk.type == Token.Type.OBRACE && tk.intValue == 2) { /* Open a preshunt group. */ curBraceCount += 1; - if (curBraceCount != 1) { + if(curBraceCount != 1) { /* - * Push the old group onto the group stack. + * Push the old group onto the group + * stack. */ bracedTokens.push(curBracedTokens); } curBracedTokens = new FunctionalList<>(); - } else if (tk.type == Token.Type.CBRACE && tk.intValue == 2) { + } else if(tk.type == Token.Type.CBRACE && tk.intValue == 2) { /* Close a preshunt group. */ - if (curBraceCount == 0) { + if(curBraceCount == 0) { /* - * Error if there couldn't have been an opening. + * Error if there couldn't have been an + * opening. */ Errors.inst.printError(EK_ENG_NOOPENING); return false; @@ -620,32 +632,36 @@ public class DiceLangEngine { /* Shunt preshunt group. */ final boolean success = shunt.shuntTokens(curBracedTokens, preshuntTokens); - if (debugMode) { - System.out.println("\t\tPreshunted " + curBracedTokens + " into " + preshuntTokens); + if(debugMode) { + System.out.println("\t\tPreshunted " + curBracedTokens + " into " + + preshuntTokens); } - if (!success) { + if(!success) { return false; } - if (curBraceCount >= 1) { + if(curBraceCount >= 1) { /* - * Add the preshunt group to the previous group. + * Add the preshunt group to the + * previous group. */ curBracedTokens = bracedTokens.pop(); curBracedTokens.add(new Token(Token.Type.TOKGROUP, preshuntTokens)); } else { /* - * Add the preshunt group to the token stream. + * Add the preshunt group to the token + * stream. */ preparedTokens.add(new Token(Token.Type.TOKGROUP, preshuntTokens)); } } else { /* - * Add the token to the active preshunt group, if there is one.. + * Add the token to the active preshunt group, + * if there is one.. */ - if (curBraceCount >= 1) { + if(curBraceCount >= 1) { curBracedTokens.add(tk); } else { preparedTokens.add(tk); @@ -653,7 +669,7 @@ public class DiceLangEngine { } } - if (curBraceCount > 0) { + if(curBraceCount > 0) { /* There was an unclosed group. */ Errors.inst.printError(EK_ENG_NOCLOSING); return false; @@ -671,8 +687,8 @@ public class DiceLangEngine { /* * @NOTE * - * The string literal table should be abstracted into some kind of auto-numbered - * map thing. + * The string literal table should be abstracted into some kind of + * auto-numbered map thing. */ void addStringLiteral(final int key, final String val) { stringLits.put(key, val); diff --git a/base/src/bjc/dicelang/Errors.java b/base/src/bjc/dicelang/Errors.java index 2577a32..8d3c969 100644 --- a/base/src/bjc/dicelang/Errors.java +++ b/base/src/bjc/dicelang/Errors.java @@ -273,15 +273,15 @@ public class Errors { * Print an error. * * @param key - * The key of the error. + * The key of the error. * * @param args - * The arguments for the error. + * The arguments for the error. */ public void printError(final ErrorKey key, final String... args) { - switch (mode) { + switch(mode) { case WIZARD: - if (key == ErrorKey.EK_MISC_NOFILE) { + if(key == ErrorKey.EK_MISC_NOFILE) { System.out.println("\t? 404"); } else { System.out.println("\t? " + key.ordinal()); @@ -299,7 +299,7 @@ public class Errors { } private static void devError(final ErrorKey key, final String[] args) { - switch (key) { + switch(key) { case EK_DFN_PREDSYN: System.out.printf("\tERROR: Incorrect define guard syntax %s\n", args[0]); break; @@ -309,8 +309,10 @@ public class Errors { break; case EK_DFN_RECUR: - System.out.printf("\tERROR: Recursive define didn't converge after %s iterations." - + " Original string was %s, last iteration was %s\n", args[0], args[1], args[2]); + System.out.printf( + "\tERROR: Recursive define didn't converge after %s iterations." + + " Original string was %s, last iteration was %s\n", + args[0], args[1], args[2]); break; case EK_CONS_INVPRAG: @@ -322,7 +324,8 @@ public class Errors { break; case EK_ENG_NOOPENING: - System.out.printf("\tERROR: Encountered closing doublebrace without" + " matching opening doublebrace\n"); + System.out.printf("\tERROR: Encountered closing doublebrace without" + + " matching opening doublebrace\n"); break; case EK_ENG_NOCLOSING: @@ -346,8 +349,8 @@ public class Errors { break; case EK_EVAL_INVBIN: - System.out.printf("\tERROR: Binary operators take 2 operands, not %s\n" + "\tProblem node is %s\n", args[0], - args[1]); + System.out.printf("\tERROR: Binary operators take 2 operands, not %s\n" + + "\tProblem node is %s\n", args[0], args[1]); break; case EK_EVAL_UNBIN: @@ -383,7 +386,8 @@ public class Errors { break; case EK_EVAL_INVDGROUP: - System.out.printf("\tERROR: Dice group operator expects scalar dice or integers," + " not %s\n", args[0]); + System.out.printf("\tERROR: Dice group operator expects scalar dice or integers," + " not %s\n", + args[0]); break; case EK_EVAL_INVDICE: @@ -407,8 +411,8 @@ public class Errors { break; case EK_PARSE_UNCLOSE: - System.out.printf("\tERROR: Found group closer without opener: (closing was %s" + ", expected %s)\n", - args[0], args[1]); + System.out.printf("\tERROR: Found group closer without opener: (closing was %s" + + ", expected %s)\n", args[0], args[1]); break; case EK_PARSE_BINARY: @@ -434,12 +438,13 @@ public class Errors { break; case EK_SHUNT_NOOP: - System.out.printf("\tERROR: Unary operator %s is an adverb, but there is no operator" + " to apply it to\n", - args[0]); + System.out.printf("\tERROR: Unary operator %s is an adverb, but there is no operator" + + " to apply it to\n", args[0]); break; case EK_SHUNT_NOGROUP: - System.out.printf("\tERROR: Couldn't find matching grouping %s (expected %s)\n", args[0], args[1]); + System.out.printf("\tERROR: Couldn't find matching grouping %s (expected %s)\n", args[0], + args[1]); break; case EK_SHUNT_NOTASSOC: diff --git a/base/src/bjc/dicelang/Node.java b/base/src/bjc/dicelang/Node.java index 7e97f9c..a9e14db 100644 --- a/base/src/bjc/dicelang/Node.java +++ b/base/src/bjc/dicelang/Node.java @@ -2,6 +2,7 @@ package bjc.dicelang; import bjc.dicelang.eval.EvaluatorResult; import bjc.dicelang.eval.FailureEvaluatorResult; +import bjc.dicelang.tokens.Token; import bjc.utils.data.ITree; /* @@ -62,7 +63,7 @@ public class Node { @Override public String toString() { - switch (type) { + switch(type) { case UNARYOP: case BINOP: return "(" + type.name() + " : " + operatorType + ")"; @@ -84,17 +85,17 @@ public class Node { @Override public boolean equals(final Object other) { - if (!(other instanceof Node)) { + if(!(other instanceof Node)) { return false; } final Node otk = (Node) other; - if (otk.type != type) { + if(otk.type != type) { return false; } - switch (type) { + switch(type) { case OGROUP: return tokenVal.equals(otk.tokenVal); diff --git a/base/src/bjc/dicelang/Parser.java b/base/src/bjc/dicelang/Parser.java index 76730b9..ffbfb9d 100644 --- a/base/src/bjc/dicelang/Parser.java +++ b/base/src/bjc/dicelang/Parser.java @@ -10,12 +10,13 @@ import static bjc.dicelang.Node.Type.GROUP; import static bjc.dicelang.Node.Type.OGROUP; import static bjc.dicelang.Node.Type.TOKREF; import static bjc.dicelang.Node.Type.UNARYOP; -import static bjc.dicelang.Token.Type.CBRACE; -import static bjc.dicelang.Token.Type.CBRACKET; +import static bjc.dicelang.tokens.Token.Type.CBRACE; +import static bjc.dicelang.tokens.Token.Type.CBRACKET; import java.util.Deque; import java.util.LinkedList; +import bjc.dicelang.tokens.Token; import bjc.utils.data.ITree; import bjc.utils.data.Tree; import bjc.utils.funcdata.IList; @@ -36,18 +37,18 @@ public class Parser { * Parse a series of tokens to a forest of ASTs. * * @param tokens - * The list of tokens to parse. + * The list of tokens to parse. * * @param results - * The place to set results. + * The place to set results. * * @return Whether or not the parse was successful. */ public static boolean parseTokens(final IList<Token> tokens, final IList<ITree<Node>> results) { final Deque<ITree<Node>> working = new LinkedList<>(); - for (final Token tk : tokens) { - switch (tk.type) { + for(final Token tk : tokens) { + switch(tk.type) { case OBRACKET: case OBRACE: /* Parse opening delims. */ @@ -59,7 +60,7 @@ public class Parser { /* Parse closing delims. */ final boolean sc = parseClosingGrouper(working, tk); - if (!sc) { + if(!sc) { return false; } @@ -75,7 +76,7 @@ public class Parser { case LET: case BIND: /* Parse binary operator. */ - if (working.size() < 2) { + if(working.size() < 2) { Errors.inst.printError(EK_PARSE_BINARY); return false; } @@ -85,10 +86,10 @@ public class Parser { case ADD: case SUBTRACT: /* Handle binary/unary operators. */ - if (working.size() == 0) { + if(working.size() == 0) { Errors.inst.printError(EK_PARSE_UNOPERAND, tk.toString()); return false; - } else if (working.size() == 1) { + } else if(working.size() == 1) { final ITree<Node> operand = working.pop(); final ITree<Node> opNode = new Tree<>(new Node(UNARYOP, tk.type)); @@ -104,7 +105,7 @@ public class Parser { case DICESCALAR: case DICEFUDGE: /* Handle unary operators. */ - if (working.size() == 0) { + if(working.size() == 0) { Errors.inst.printError(EK_PARSE_UNOPERAND, tk.toString()); } else { final ITree<Node> operand = working.pop(); @@ -131,9 +132,10 @@ public class Parser { } /* - * Collect the remaining nodes as the roots of the trees in the AST forest. + * Collect the remaining nodes as the roots of the trees in the + * AST forest. */ - for (final ITree<Node> ast : working) { + for(final ITree<Node> ast : working) { results.add(ast); } @@ -155,14 +157,14 @@ public class Parser { /* Parse a closing delimiter. */ private static boolean parseClosingGrouper(final Deque<ITree<Node>> working, final Token tk) { - if (working.size() == 0) { + if(working.size() == 0) { Errors.inst.printError(EK_PARSE_NOCLOSE); return false; } ITree<Node> groupNode = null; - switch (tk.type) { + switch(tk.type) { case CBRACE: groupNode = new Tree<>(new Node(GROUP, Node.GroupType.CODE)); break; @@ -176,22 +178,22 @@ public class Parser { Token matching = null; - if (tk.type == CBRACKET) { + if(tk.type == CBRACKET) { matching = new Token(Token.Type.OBRACKET, tk.intValue); - } else if (tk.type == CBRACE) { + } else if(tk.type == CBRACE) { matching = new Token(Token.Type.OBRACE, tk.intValue); } final ITree<Node> matchNode = new Tree<>(new Node(OGROUP, matching)); - if (!working.contains(matchNode)) { + if(!working.contains(matchNode)) { Errors.inst.printError(EK_PARSE_UNCLOSE, tk.toString(), matchNode.toString()); System.out.println("\tCurrent forest is: "); int treeNo = 1; - for (final ITree<Node> ast : working) { + for(final ITree<Node> ast : working) { System.out.println("Tree " + treeNo++ + ": " + ast.toString()); } @@ -200,14 +202,14 @@ public class Parser { final Deque<ITree<Node>> childs = new LinkedList<>(); - while (!working.peek().equals(matchNode)) { + while(!working.peek().equals(matchNode)) { childs.push(working.pop()); } /* Discard opener */ working.pop(); - for (final ITree<Node> child : childs) { + for(final ITree<Node> child : childs) { groupNode.addChild(child); } diff --git a/base/src/bjc/dicelang/Shunter.java b/base/src/bjc/dicelang/Shunter.java index 01029cd..79ab97a 100644 --- a/base/src/bjc/dicelang/Shunter.java +++ b/base/src/bjc/dicelang/Shunter.java @@ -5,6 +5,7 @@ import java.util.HashSet; import java.util.LinkedList; import java.util.Set; +import bjc.dicelang.tokens.Token; import bjc.utils.funcdata.FunctionalList; import bjc.utils.funcdata.FunctionalMap; import bjc.utils.funcdata.IList; @@ -15,7 +16,7 @@ import static bjc.dicelang.Errors.ErrorKey.EK_SHUNT_NOGROUP; import static bjc.dicelang.Errors.ErrorKey.EK_SHUNT_NOTADJ; import static bjc.dicelang.Errors.ErrorKey.EK_SHUNT_NOTADV; import static bjc.dicelang.Errors.ErrorKey.EK_SHUNT_NOTASSOC; -import static bjc.dicelang.Token.Type.*; +import static bjc.dicelang.tokens.Token.Type.*; /** * Shunt a set of infix tokens to postfix tokens. @@ -34,8 +35,8 @@ public class Shunter { Set<Token.Type> notAssoc; /* - * Unary operators that can only be applied to non-operator tokens and yield - * operator tokens. + * Unary operators that can only be applied to non-operator tokens and + * yield operator tokens. */ Set<Token.Type> unaryAdjectives; @@ -46,8 +47,8 @@ public class Shunter { Set<Token.Type> unaryAdverbs; /* - * Unary operators that can only be applied to operator tokens and yield data - * tokens + * Unary operators that can only be applied to operator tokens and yield + * data tokens */ Set<Token.Type> unaryGerunds; @@ -108,10 +109,10 @@ public class Shunter { * Shunt a set of tokens from infix to postfix. * * @param tks - * The tokens to input. + * The tokens to input. * * @param returned - * The postfix tokens. + * The postfix tokens. * * @return Whether or not the shunt succeeded. */ @@ -126,32 +127,32 @@ public class Shunter { /* Tokens to feed ahead of the current one. */ final Deque<Token> feed = new LinkedList<>(); - for (final Token tk : tks.toIterable()) { + for(final Token tk : tks.toIterable()) { boolean succ; /* Drain the feed queue. */ - while (feed.size() != 0) { + while(feed.size() != 0) { succ = shuntToken(feed.poll(), opStack, unaryOps, currReturned, feed); - if (!succ) { + if(!succ) { return false; } } succ = shuntToken(tk, opStack, unaryOps, currReturned, feed); - if (!succ) { + if(!succ) { return false; } } /* Flush leftover operators. */ - while (!opStack.isEmpty()) { + while(!opStack.isEmpty()) { currReturned.addLast(opStack.pop()); } /* Add the tokens to the returned list. */ - for (final Token tk : currReturned) { + for(final Token tk : currReturned) { returned.add(tk); } @@ -162,8 +163,8 @@ public class Shunter { private boolean shuntToken(final Token tk, final Deque<Token> opStack, final Deque<Token> unaryStack, final Deque<Token> currReturned, final Deque<Token> feed) { /* Handle unary operators. */ - if (unaryStack.size() != 0) { - if (isUnary(tk)) { + if(unaryStack.size() != 0) { + if(isUnary(tk)) { unaryStack.add(tk); return true; } @@ -172,18 +173,19 @@ public class Shunter { final Token.Type unaryType = unaryOp.type; - if (unaryAdjectives.contains(unaryType)) { + if(unaryAdjectives.contains(unaryType)) { /* - * Handle unary adjectives that take a non-operator. + * Handle unary adjectives that take a + * non-operator. */ - if (isOp(tk)) { + if(isOp(tk)) { Errors.inst.printError(EK_SHUNT_NOTADV, unaryOp.toString(), tk.toString()); return false; } final Token newTok = new Token(TAGOPR); - if (tk.type == TAGOP) { + if(tk.type == TAGOP) { newTok.tokenValues = tk.tokenValues; } else { newTok.tokenValues = new FunctionalList<>(tk); @@ -193,16 +195,18 @@ public class Shunter { opStack.push(newTok); return true; - } else if (unaryAdverbs.contains(unaryType)) { - /* Handle unary adverbs that take an operator. */ - if (!isOp(tk)) { + } else if(unaryAdverbs.contains(unaryType)) { + /* + * Handle unary adverbs that take an operator. + */ + if(!isOp(tk)) { Errors.inst.printError(EK_SHUNT_NOTADJ, unaryOp.toString(), tk.toString()); return false; } final Token newTok = new Token(TAGOPR); - if (tk.type == TAGOP) { + if(tk.type == TAGOP) { newTok.tokenValues = tk.tokenValues; } else { newTok.tokenValues = new FunctionalList<>(tk); @@ -215,15 +219,15 @@ public class Shunter { } } - if (isUnary(tk)) { + if(isUnary(tk)) { unaryStack.add(tk); return true; - } else if (isOp(tk)) { + } else if(isOp(tk)) { /* Drain higher precedence operators. */ - while (!opStack.isEmpty() && isHigherPrec(tk, opStack.peek())) { + while(!opStack.isEmpty() && isHigherPrec(tk, opStack.peek())) { final Token newOp = opStack.pop(); - if (tk.type == newOp.type && notAssoc.contains(tk.type)) { + if(tk.type == newOp.type && notAssoc.contains(tk.type)) { Errors.inst.printError(EK_SHUNT_NOTASSOC, tk.type.toString()); } @@ -231,17 +235,17 @@ public class Shunter { } opStack.push(tk); - } else if (tk.type == OPAREN || tk.type == OBRACE) { + } else if(tk.type == OPAREN || tk.type == OBRACE) { opStack.push(tk); - if (tk.type == OBRACE) { + if(tk.type == OBRACE) { currReturned.addLast(tk); } - } else if (tk.type == CPAREN || tk.type == CBRACE) { + } else if(tk.type == CPAREN || tk.type == CBRACE) { /* Handle closing delimiter. */ Token matching = null; - switch (tk.type) { + switch(tk.type) { case CPAREN: matching = new Token(OPAREN, tk.intValue); break; @@ -253,33 +257,33 @@ public class Shunter { return false; } - if (!opStack.contains(matching)) { + if(!opStack.contains(matching)) { Errors.inst.printError(EK_SHUNT_NOGROUP, tk.toString(), matching.toString()); return false; } - while (!opStack.peek().equals(matching)) { + while(!opStack.peek().equals(matching)) { currReturned.addLast(opStack.pop()); } - if (tk.type == CBRACE) { + if(tk.type == CBRACE) { currReturned.addLast(tk); } opStack.pop(); - } else if (tk.type == GROUPSEP) { + } else if(tk.type == GROUPSEP) { /* Add a grouped token. */ final IList<Token> group = new FunctionalList<>(); - while (currReturned.size() != 0 && !currReturned.peek().isGrouper()) { + while(currReturned.size() != 0 && !currReturned.peek().isGrouper()) { group.add(currReturned.pop()); } - while (opStack.size() != 0 && !opStack.peek().isGrouper()) { + while(opStack.size() != 0 && !opStack.peek().isGrouper()) { group.add(opStack.pop()); } - if (currReturned.size() == 0) { + if(currReturned.size() == 0) { Errors.inst.printError(EK_SHUNT_INVSEP); return false; } @@ -299,31 +303,31 @@ public class Shunter { boolean exists = ops.containsKey(right); - if (rght.type == TAGOPR) { + if(rght.type == TAGOPR) { exists = true; } /* If it doesn't, the left is higher precedence. */ - if (!exists) { + if(!exists) { return false; } int rightPrecedence; int leftPrecedence; - if (rght.type == TAGOPR) { + if(rght.type == TAGOPR) { rightPrecedence = (int) rght.intValue; } else { rightPrecedence = ops.get(right); } - if (lft.type == TAGOPR) { + if(lft.type == TAGOPR) { leftPrecedence = (int) lft.intValue; } else { leftPrecedence = ops.get(left); } - if (rightAssoc.contains(left)) { + if(rightAssoc.contains(left)) { return rightPrecedence > leftPrecedence; } @@ -334,23 +338,23 @@ public class Shunter { private boolean isOp(final Token tk) { final Token.Type ty = tk.type; - if (ops.containsKey(ty)) { + if(ops.containsKey(ty)) { return true; } - if (unaryAdjectives.contains(ty)) { + if(unaryAdjectives.contains(ty)) { return true; } - if (unaryAdverbs.contains(ty)) { + if(unaryAdverbs.contains(ty)) { return true; } - if (unaryGerunds.contains(ty)) { + if(unaryGerunds.contains(ty)) { return true; } - if (ty == TAGOPR) { + if(ty == TAGOPR) { return true; } @@ -361,15 +365,15 @@ public class Shunter { private boolean isUnary(final Token tk) { final Token.Type ty = tk.type; - if (unaryAdjectives.contains(ty)) { + if(unaryAdjectives.contains(ty)) { return true; } - if (unaryAdverbs.contains(ty)) { + if(unaryAdverbs.contains(ty)) { return true; } - if (unaryGerunds.contains(ty)) { + if(unaryGerunds.contains(ty)) { return true; } diff --git a/base/src/bjc/dicelang/Tokenizer.java b/base/src/bjc/dicelang/Tokenizer.java index 3a6db22..cd732ee 100644 --- a/base/src/bjc/dicelang/Tokenizer.java +++ b/base/src/bjc/dicelang/Tokenizer.java @@ -4,13 +4,16 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import bjc.dicelang.dice.DiceBox; +import bjc.dicelang.tokens.DiceToken; +import bjc.dicelang.tokens.FloatToken; +import bjc.dicelang.tokens.Token; import bjc.utils.funcdata.FunctionalMap; import bjc.utils.funcdata.IMap; import bjc.utils.funcutils.StringUtils; import bjc.utils.parserutils.TokenUtils; import static bjc.dicelang.Errors.ErrorKey.*; -import static bjc.dicelang.Token.Type.*; +import static bjc.dicelang.tokens.Token.Type.*; /** * Converts strings into tokens. @@ -49,16 +52,16 @@ public class Tokenizer { } public Token lexToken(final String token, final IMap<String, String> stringLts) { - if (token.equals("")) { + if(token.equals("")) { return null; } Token tk = Token.NIL_TOKEN; - if (litTokens.containsKey(token)) { + if(litTokens.containsKey(token)) { tk = new Token(litTokens.get(token)); } else { - switch (token.charAt(0)) { + switch(token.charAt(0)) { case '(': case ')': case '[': @@ -78,9 +81,9 @@ public class Tokenizer { private static Token tokenizeGrouping(final String token) { Token tk = Token.NIL_TOKEN; - if (StringUtils.containsOnly(token, "\\" + token.charAt(0))) { + if(StringUtils.containsOnly(token, "\\" + token.charAt(0))) { /* Handle multiple-grouped delimiters. */ - switch (token.charAt(0)) { + switch(token.charAt(0)) { case '(': tk = new Token(OPAREN, token.length()); break; @@ -125,15 +128,15 @@ public class Tokenizer { String token = rtoken.trim(); - if (TokenUtils.isInt(token)) { + if(TokenUtils.isInt(token)) { tk = new Token(INT_LIT, Long.parseLong(token)); - } else if (hexadecimalMatcher.matcher(token).matches()) { + } else if(hexadecimalMatcher.matcher(token).matches()) { final String newToken = token.substring(0, 1) + token.substring(token.indexOf('x')); tk = new Token(INT_LIT, Long.parseLong(newToken.substring(2).toUpperCase(), 16)); - } else if (flexadecimalMatcher.matcher(token).matches()) { + } else if(flexadecimalMatcher.matcher(token).matches()) { final int parseBase = Integer.parseInt(token.substring(token.lastIndexOf('B') + 1)); - if (parseBase < Character.MIN_RADIX || parseBase > Character.MAX_RADIX) { + if(parseBase < Character.MIN_RADIX || parseBase > Character.MAX_RADIX) { Errors.inst.printError(EK_TOK_INVBASE, Integer.toString(parseBase)); return Token.NIL_TOKEN; } @@ -142,18 +145,18 @@ public class Tokenizer { try { tk = new Token(INT_LIT, Long.parseLong(flexNum, parseBase)); - } catch (final NumberFormatException nfex) { + } catch(final NumberFormatException nfex) { Errors.inst.printError(EK_TOK_INVFLEX, flexNum, Integer.toString(parseBase)); return Token.NIL_TOKEN; } - } else if (TokenUtils.isDouble(token)) { + } else if(TokenUtils.isDouble(token)) { tk = new FloatToken(Double.parseDouble(token)); - } else if (DiceBox.isValidExpression(token)) { + } else if(DiceBox.isValidExpression(token)) { tk = new DiceToken(DiceBox.parseExpression(token)); } else { final Matcher stringLit = stringLitMatcher.matcher(token); - if (stringLit.matches()) { + if(stringLit.matches()) { final int litNum = Integer.parseInt(stringLit.group(1)); eng.addStringLiteral(litNum, stringLts.get(token)); diff --git a/base/src/bjc/dicelang/CLIArgsParser.java b/base/src/bjc/dicelang/cli/CLIArgsParser.java index 553692b..b4c0c7c 100644 --- a/base/src/bjc/dicelang/CLIArgsParser.java +++ b/base/src/bjc/dicelang/cli/CLIArgsParser.java @@ -1,4 +1,4 @@ -package bjc.dicelang; +package bjc.dicelang.cli; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -6,6 +6,9 @@ import java.io.IOException; import java.util.Arrays; import java.util.Scanner; +import bjc.dicelang.Define; +import bjc.dicelang.DiceLangEngine; +import bjc.dicelang.Errors; import bjc.dicelang.util.ResourceLoader; import static bjc.dicelang.Errors.ErrorKey.*; @@ -21,94 +24,94 @@ public class CLIArgsParser { * Parse the provided set of CLI arguments. * * @param args - * The CLI arguments to parse. + * The CLI arguments to parse. * @param eng - * The engine to affect with parsing. + * The engine to affect with parsing. * * @return Whether or not to continue to the DiceLang repl. */ public static boolean parseArgs(final String[] args, final DiceLangEngine eng) { - if (args.length < 0) { + if(args.length < 0) { return true; } - if (args.length == 1 && (args[0].equals("--help") || args[0].equals("-h"))) { - for (final String lne : ResourceLoader.loadHelpFile("cli")) { + if(args.length == 1 && (args[0].equals("--help") || args[0].equals("-h"))) { + for(final String lne : ResourceLoader.loadHelpFile("cli")) { System.out.println(lne); } System.exit(0); } - for (int i = 0; i < args.length; i++) { + for(int i = 0; i < args.length; i++) { final String arg = args[i]; /* * @TODO 10/08/17 Ben Culkin :CLIArgRefactor * - * Use whatever library gets added to BJC-Utils for this, and extend these to do - * more things. + * Use whatever library gets added to BJC-Utils for + * this, and extend these to do more things. */ - switch (arg) { + switch(arg) { case "-d": case "--debug": - if (!eng.toggleDebug()) { + if(!eng.toggleDebug()) { eng.toggleDebug(); } break; case "-nd": case "--no-debug": - if (eng.toggleDebug()) { + if(eng.toggleDebug()) { eng.toggleDebug(); } break; case "-po": case "--postfix": - if (!eng.togglePostfix()) { + if(!eng.togglePostfix()) { eng.togglePostfix(); } break; case "-npo": case "--no-postfix": - if (eng.togglePostfix()) { + if(eng.togglePostfix()) { eng.togglePostfix(); } break; case "-pr": case "--prefix": - if (!eng.togglePrefix()) { + if(!eng.togglePrefix()) { eng.togglePrefix(); } break; case "-npr": case "--no-prefix": - if (eng.togglePrefix()) { + if(eng.togglePrefix()) { eng.togglePrefix(); } break; case "-se": case "--stepeval": - if (!eng.toggleStepEval()) { + if(!eng.toggleStepEval()) { eng.toggleStepEval(); } break; case "-nse": case "--no-stepeval": - if (eng.toggleStepEval()) { + if(eng.toggleStepEval()) { eng.toggleStepEval(); } break; case "-D": case "--define": i = simpleDefine(i, args, eng); - if (i == -1) { + if(i == -1) { return false; } break; case "-df": case "--define-file": i = defineFile(i, args, eng); - if (i == -1) { + if(i == -1) { return false; } break; @@ -130,15 +133,15 @@ public class CLIArgsParser { private static int simpleDefine(final int i, final String[] args, final DiceLangEngine eng) { /* :DefineRefactor */ - if (i >= args.length - 1) { + if(i >= args.length - 1) { Errors.inst.printError(EK_CLI_MISARG, "define"); return -1; } - if (i >= args.length - 2) { + if(i >= args.length - 2) { final Define dfn = new Define(5, false, false, false, null, args[i + 1], Arrays.asList("")); - if (dfn.inError) { + if(dfn.inError) { return -1; } @@ -148,7 +151,7 @@ public class CLIArgsParser { final Define dfn = new Define(5, false, false, false, null, args[i + 1], Arrays.asList(args[i + 2])); - if (dfn.inError) { + if(dfn.inError) { return -1; } @@ -158,27 +161,27 @@ public class CLIArgsParser { /* Load a series of defines from a file. */ private static int defineFile(final int i, final String[] args, final DiceLangEngine eng) { - if (i >= args.length - 1) { + if(i >= args.length - 1) { Errors.inst.printError(EK_CLI_MISARG, "define-file"); return -1; } final String fName = args[i + 1]; - try (FileInputStream fis = new FileInputStream(fName)) { - try (Scanner scan = new Scanner(fis)) { - while (scan.hasNextLine()) { + try(FileInputStream fis = new FileInputStream(fName)) { + try(Scanner scan = new Scanner(fis)) { + while(scan.hasNextLine()) { final String ln = scan.nextLine(); final Define dfn = parseDefine(ln.substring(ln.indexOf(' '))); - if (dfn == null || dfn.inError) { + if(dfn == null || dfn.inError) { return -1; } - if (ln.startsWith("line")) { + if(ln.startsWith("line")) { eng.addLineDefine(dfn); - } else if (ln.startsWith("token")) { + } else if(ln.startsWith("token")) { eng.addTokenDefine(dfn); } else { final String defnType = ln.substring(0, ln.indexOf(' ')); @@ -188,10 +191,10 @@ public class CLIArgsParser { } } } - } catch (final FileNotFoundException fnfex) { + } catch(final FileNotFoundException fnfex) { Errors.inst.printError(EK_MISC_NOFILE, fName); return -1; - } catch (final IOException ioex) { + } catch(final IOException ioex) { Errors.inst.printError(EK_MISC_IOEX, fName); return -1; } diff --git a/base/src/bjc/dicelang/DiceLangConsole.java b/base/src/bjc/dicelang/cli/DiceLangConsole.java index a43c1da..c19793f 100644 --- a/base/src/bjc/dicelang/DiceLangConsole.java +++ b/base/src/bjc/dicelang/cli/DiceLangConsole.java @@ -1,8 +1,17 @@ -package bjc.dicelang; +package bjc.dicelang.cli; + +import bjc.dicelang.Define; +import bjc.dicelang.DiceLangEngine; +import bjc.dicelang.Errors; +import bjc.dicelang.Define.Type; +import bjc.utils.funcdata.FunctionalMap; +import bjc.utils.funcdata.IMap; import java.io.IOException; import java.util.LinkedList; import java.util.List; +import java.util.function.BiPredicate; +import java.util.function.Predicate; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -25,17 +34,45 @@ public class DiceLangConsole { /* The place to read input from. */ private ConsoleReader read; + /* Are we in multi-line mode? */ + private boolean multiLine; + + private static IMap<String, DiceLangPragma> pragmas; + + static { + pragmas = new FunctionalMap<>(); + + pragmas.put("debug", new DiceLangPragma() { + + @Override + public String getDescription() { + return "Toggle debug mode, which includes a bunch more output during various stages of compilation and interpretation"; + } + + @Override + public String getBrief() { + return "Toggle Debug mode"; + } + + @Override + public boolean execute(String lne, DiceLangEngine eng) { + System.out.println("\tDebug mode is now " + eng.toggleDebug()); + return true; + } + }); + } + /** * Create a new console. * * @param args - * The CLI args for the console. + * The CLI args for the console. */ public DiceLangConsole(final String[] args) { commandNumber = 0; eng = new DiceLangEngine(); - if (!CLIArgsParser.parseArgs(args, eng)) { + if(!CLIArgsParser.parseArgs(args, eng)) { System.exit(1); } @@ -47,46 +84,62 @@ public class DiceLangConsole { /* Set up console. */ try { read = new ConsoleReader(); - } catch (final IOException ioex) { + } catch(final IOException ioex) { System.out.println("ERROR: Console init failed"); return; } /* Print greeting. */ System.out.println("dice-lang v0.2"); - String comm = null; - /* Read initial command. */ - try { - comm = read.readLine(String.format("(%d) dice-lang> ", commandNumber)); - } catch (final IOException ioex) { - System.out.println("ERROR: I/O failed"); - return; - } + do { + String comm = null; - /* Run commands. */ - /* - * @NOTE Should switch this to a do-while loop to reduce code duplication. - */ - while (!comm.equals("quit") && !comm.equals("exit")) { - if (comm.startsWith("pragma")) { + /* Read initial command. */ + try { + comm = read.readLine(String.format("(%d) dice-lang> ", commandNumber)); + } catch(final IOException ioex) { + System.out.println("ERROR: I/O failed"); + return; + } + /* Run commands. */ + if(comm.equals("quit") || comm.equals("exit")) { + break; + } + + if(comm.startsWith("pragma")) { /* Run pragmas. */ final boolean success = handlePragma(comm.substring(7)); - if (success) { + if(success) { System.out.println("Pragma completed succesfully"); } else { System.out.println("Pragma execution failed"); } } else { + if(multiLine) { + try { + do { + String nLine = read.readLine( + String.format("(%d)\t...> ", commandNumber)); + + if(nLine.trim().equals("")) break; + + comm = String.format("%s %s", comm, nLine); + } while(true); + } catch(IOException ioex) { + System.out.println("ERROR: I/O failed"); + return; + } + } /* Run commands. */ - if (eng.debugMode) { + if(eng.debugMode) { System.out.printf("\tRaw command: %s\n", comm); } final boolean success = eng.runCommand(comm); - if (success) { + if(success) { System.out.println("Command completed succesfully"); } else { System.out.println("Command execution failed"); @@ -98,16 +151,17 @@ public class DiceLangConsole { /* Read the next command. */ try { comm = read.readLine(String.format("(%d) dice-lang> ", commandNumber)); - } catch (final IOException ioex) { + } catch(final IOException ioex) { System.out.println("ERROR: I/O failed"); return; } - } + } while(true); + } /* Handle running pragmas. */ private boolean handlePragma(final String pragma) { - if (eng.debugMode) { + if(eng.debugMode) { System.out.println("\tRaw pragma: " + pragma); } @@ -116,7 +170,7 @@ public class DiceLangConsole { final int firstIndex = pragma.indexOf(' '); /* Handle argless pragmas. */ - if (firstIndex == -1) { + if(firstIndex == -1) { pragmaName = pragma; } else { pragmaName = pragma.substring(0, firstIndex); @@ -124,10 +178,11 @@ public class DiceLangConsole { /* Run pragmas. */ /* - * @TODO 10/09/17 Ben Culkin :PragmaRefactor Swap to using something that makes - * it easier to add pragmas. + * @TODO 10/09/17 Ben Culkin :PragmaRefactor + * + * Swap to using something that makes it easier to add pragmas. */ - switch (pragmaName) { + switch(pragmaName) { case "debug": System.out.println("\tDebug mode is now " + eng.toggleDebug()); break; @@ -144,6 +199,9 @@ public class DiceLangConsole { return defineMode(pragma.substring(7)); case "help": return helpMode(pragma.substring(5)); + case "multi-line": + multiLine = !multiLine; + break; default: Errors.inst.printError(EK_CONS_INVPRAG, pragma); return false; @@ -155,7 +213,7 @@ public class DiceLangConsole { /* Run a help mode. */ private static boolean helpMode(final String pragma) { /* Get the help topic. */ - switch (pragma.trim()) { + switch(pragma.trim()) { case "help": System.out.println("\tGet help on pragmas"); break; @@ -175,6 +233,9 @@ public class DiceLangConsole { System.out.println("\tAdd a macro rewrite directive."); System.out.println("\tdefine <priority> <type> <recursion> <guard> <circular> <patterns>..."); break; + case "multi-line": + System.out.println("\tToggle multi-line input mode."); + break; default: System.out.println("\tNo help available for pragma " + pragma); } @@ -196,24 +257,25 @@ public class DiceLangConsole { final int sixthIndex = defineText.indexOf(' ', fifthIndex + 1); /* - * Error if we got something we didn't need, or didn't get something we need. + * Error if we got something we didn't need, or didn't get + * something we need. */ - if (firstIndex == -1) { + if(firstIndex == -1) { Errors.inst.printError(EK_CONS_INVDEFINE, "(no priority)"); return false; - } else if (secondIndex == -1) { + } else if(secondIndex == -1) { Errors.inst.printError(EK_CONS_INVDEFINE, "(no define type)"); return false; - } else if (thirdIndex == -1) { + } else if(thirdIndex == -1) { Errors.inst.printError(EK_CONS_INVDEFINE, "(no recursion type)"); return false; - } else if (fourthIndex == -1) { + } else if(fourthIndex == -1) { Errors.inst.printError(EK_CONS_INVDEFINE, "(no guard type)"); return false; - } else if (fifthIndex == -1) { + } else if(fifthIndex == -1) { Errors.inst.printError(EK_CONS_INVDEFINE, "(no circularity)"); return false; - } else if (sixthIndex == -1) { + } else if(sixthIndex == -1) { Errors.inst.printError(EK_CONS_INVDEFINE, "(no patterns)"); return false; } @@ -226,7 +288,7 @@ public class DiceLangConsole { boolean subMode = false; /* Parse the define type. */ - switch (defineType) { + switch(defineType) { case "line": type = Define.Type.LINE; break; @@ -258,9 +320,9 @@ public class DiceLangConsole { final Matcher patMatcher = slashPattern.matcher(pats); String guardPattern = null; - if (hasGuard) { + if(hasGuard) { /* Grab the guard pattern. */ - if (!patMatcher.find()) { + if(!patMatcher.find()) { Errors.inst.printError(EK_CONS_INVDEFINE, "(no guard pattern)"); return false; } @@ -268,7 +330,7 @@ public class DiceLangConsole { guardPattern = patMatcher.group(1); } - if (!patMatcher.find()) { + if(!patMatcher.find()) { /* Grab the search pattern. */ Errors.inst.printError(EK_CONS_INVDEFINE, "(no search pattern)"); return false; @@ -277,7 +339,7 @@ public class DiceLangConsole { final String searchPattern = patMatcher.group(1); final List<String> replacePatterns = new LinkedList<>(); - while (patMatcher.find()) { + while(patMatcher.find()) { /* Grab the replacer patterns. */ replacePatterns.add(patMatcher.group(1)); } @@ -285,12 +347,12 @@ public class DiceLangConsole { final Define dfn = new Define(priority, subMode, doRecur, isCircular, guardPattern, searchPattern, replacePatterns); - if (dfn.inError) { + if(dfn.inError) { return false; } /* Add the define to the proper place. */ - if (type == Define.Type.LINE) { + if(type == Define.Type.LINE) { eng.addLineDefine(dfn); } else { eng.addTokenDefine(dfn); @@ -303,7 +365,7 @@ public class DiceLangConsole { * Main method. * * @param args - * CLI arguments. + * CLI arguments. */ public static void main(final String[] args) { final DiceLangConsole console = new DiceLangConsole(args); diff --git a/base/src/bjc/dicelang/cli/DiceLangPragma.java b/base/src/bjc/dicelang/cli/DiceLangPragma.java new file mode 100644 index 0000000..9bbdf49 --- /dev/null +++ b/base/src/bjc/dicelang/cli/DiceLangPragma.java @@ -0,0 +1,36 @@ +package bjc.dicelang.cli; + +import bjc.dicelang.DiceLangEngine; + +/** + * Represents a pragma for the command interface. + * + * @author EVE + * + */ +public interface DiceLangPragma { + /** + * Execute the pragma. + * + * @param lne + * The command line the pragma came from. + * @param eng + * The engine we are attached to. + * @return Whether or not the pragma succeeded. + */ + public boolean execute(String lne, DiceLangEngine eng); + + /** + * Get a description on how to use this pragma + * + * @return The description on how to use the pragma + */ + public String getDescription(); + + /** + * Get a brief idea on what this pragma does + * + * @return A brief description of what this pragma does. + */ + public String getBrief(); +} diff --git a/base/src/bjc/dicelang/eval/DiceEvaluatorResult.java b/base/src/bjc/dicelang/eval/DiceEvaluatorResult.java index 8e50333..02d01ae 100644 --- a/base/src/bjc/dicelang/eval/DiceEvaluatorResult.java +++ b/base/src/bjc/dicelang/eval/DiceEvaluatorResult.java @@ -25,7 +25,7 @@ public class DiceEvaluatorResult extends EvaluatorResult { public DiceEvaluatorResult(DieList list) { this(new ListDiceExpression(list)); } - + public boolean isList() { return diceVal.isList(); } diff --git a/base/src/bjc/dicelang/eval/Evaluator.java b/base/src/bjc/dicelang/eval/Evaluator.java index 4cc2a1e..4603e22 100644 --- a/base/src/bjc/dicelang/eval/Evaluator.java +++ b/base/src/bjc/dicelang/eval/Evaluator.java @@ -6,11 +6,8 @@ import java.util.LinkedList; import java.util.function.Consumer; import bjc.dicelang.DiceLangEngine; -import bjc.dicelang.DiceToken; import bjc.dicelang.Errors; -import bjc.dicelang.FloatToken; import bjc.dicelang.Node; -import bjc.dicelang.Token; import bjc.dicelang.dice.CompoundDie; import bjc.dicelang.dice.Die; import bjc.dicelang.dice.MathDie; @@ -18,7 +15,9 @@ import bjc.dicelang.dice.ScalarDiceExpression; import bjc.dicelang.dice.ScalarDie; import bjc.dicelang.dice.SimpleDie; import bjc.dicelang.dice.SimpleDieList; - +import bjc.dicelang.tokens.DiceToken; +import bjc.dicelang.tokens.FloatToken; +import bjc.dicelang.tokens.Token; import bjc.utils.data.ITree; import bjc.utils.data.SingleIterator; import bjc.utils.data.TopDownTransformIterator; @@ -64,7 +63,7 @@ public class Evaluator { * Create a new evaluator. * * @param en - * The engine. + * The engine. */ public Evaluator(final DiceLangEngine en) { eng = en; @@ -74,7 +73,7 @@ public class Evaluator { * Evaluate a AST. * * @param comm - * The AST to evaluate. + * The AST to evaluate. * * @return The result of the tree. */ @@ -84,16 +83,18 @@ public class Evaluator { ctx.isDebug = false; ctx.thunk = itr -> { /* - * Deliberately finish the iterator, but ignore results. It's only for stepwise - * evaluation, but we don't know if stepping the iterator has side effects. + * Deliberately finish the iterator, but ignore results. + * It's only for stepwise evaluation, but we don't know + * if stepping the iterator has side effects. */ - while (itr.hasNext()) { + while(itr.hasNext()) { itr.next(); } }; /* The result. */ - final ITree<Node> res = comm.topDownTransform(this::pickEvaluationType, node -> this.evaluateNode(node, ctx)); + final ITree<Node> res = comm.topDownTransform(this::pickEvaluationType, + node -> this.evaluateNode(node, ctx)); return res.getHead().resultVal; } @@ -117,9 +118,9 @@ public class Evaluator { /* Pick the way to evaluate a node. */ private TopDownTransformResult pickEvaluationType(final Node nd) { - switch (nd.type) { + switch(nd.type) { case UNARYOP: - switch (nd.operatorType) { + switch(nd.operatorType) { case COERCE: /* Coerce does special things to the tree. */ return TopDownTransformResult.RTRANSFORM; @@ -134,7 +135,7 @@ public class Evaluator { /* Evaluate a node. */ private ITree<Node> evaluateNode(final ITree<Node> ast, final Context ctx) { - switch (ast.getHead().type) { + switch(ast.getHead().type) { case UNARYOP: return evaluateUnaryOp(ast, ctx); case BINOP: @@ -154,23 +155,23 @@ public class Evaluator { /* Evaluate a unary operator. */ private ITree<Node> evaluateUnaryOp(final ITree<Node> ast, final Context ctx) { /* Unary operators only take one operand. */ - if (ast.getChildrenCount() != 1) { + if(ast.getChildrenCount() != 1) { Errors.inst.printError(EK_EVAL_UNUNARY, Integer.toString(ast.getChildrenCount())); return new Tree<>(Node.FAIL(ast)); } - switch (ast.getHead().operatorType) { + switch(ast.getHead().operatorType) { /* * @TODO 10/09/17 Ben Culkin :CoerceRefactor * * :EvaluatorSplit * - * Coercing should be moved to its own class, or at the very least its own - * method. When the evaluator splits, this node type'll be handled exclusively - * by the type-checker. + * Coercing should be moved to its own class, or at the very + * least its own method. When the evaluator splits, this node + * type'll be handled exclusively by the type-checker. * - * Coerce also needs to be able to coerce things to dice and ratios (whenever - * they get added). + * Coerce also needs to be able to coerce things to dice and + * ratios (whenever they get added). */ case COERCE: final ITree<Node> toCoerce = ast.getChild(0); @@ -180,16 +181,16 @@ public class Evaluator { /* The current type we are coercing to. */ CoerceSteps curLevel = CoerceSteps.INTEGER; - for (int i = 0; i < toCoerce.getChildrenCount(); i++) { + for(int i = 0; i < toCoerce.getChildrenCount(); i++) { final ITree<Node> child = toCoerce.getChild(i); ITree<Node> nChild = null; /* Tell our thunk we processed a node. */ - if (ctx.isDebug) { + if(ctx.isDebug) { /* Evaluate each step of the child. */ final Iterator<ITree<Node>> nd = stepDebug(child); - for (; nd.hasNext(); nChild = nd.next()) { + for(; nd.hasNext(); nChild = nd.next()) { ctx.thunk.accept(new SingleIterator<>(child)); } } else { @@ -199,7 +200,7 @@ public class Evaluator { ctx.thunk.accept(new SingleIterator<>(nChild)); } - if (nChild == null) { + if(nChild == null) { Errors.inst.printError(EK_EVAL_INVNODE); return new Tree<>(Node.FAIL(ast)); } @@ -208,21 +209,23 @@ public class Evaluator { final EvaluatorResult res = childNode.resultVal; /* Move up to coercing to a float. */ - if (res.type == FLOAT) { + if(res.type == FLOAT) { curLevel = CoerceSteps.DOUBLE; } children.add(nChild); } - for (final ITree<Node> child : children) { + for(final ITree<Node> child : children) { final Node nd = child.getHead(); final EvaluatorResult res = nd.resultVal; - switch (res.type) { + switch(res.type) { case INT: - /* Coerce ints to doubles if we need to. */ - if (curLevel == CoerceSteps.DOUBLE) { + /* + * Coerce ints to doubles if we need to. + */ + if(curLevel == CoerceSteps.DOUBLE) { nd.resultVal = new FloatEvaluatorResult((double) res.intVal); } default: @@ -237,7 +240,7 @@ public class Evaluator { case DICESCALAR: final EvaluatorResult opr = ast.getChild(0).getHead().resultVal; - if (opr.type != INT) { + if(opr.type != INT) { Errors.inst.printError(EK_EVAL_INVDCREATE, opr.type.toString()); } @@ -246,7 +249,7 @@ public class Evaluator { case DICEFUDGE: final EvaluatorResult oprn = ast.getChild(0).getHead().resultVal; - if (oprn.type != INT) { + if(oprn.type != INT) { Errors.inst.printError(EK_EVAL_INVDCREATE, oprn.type.toString()); } @@ -263,8 +266,9 @@ public class Evaluator { final Token.Type binOp = ast.getHead().operatorType; /* Binary operators always have two children. */ - if (ast.getChildrenCount() != 2) { - Errors.inst.printError(EK_EVAL_INVBIN, Integer.toString(ast.getChildrenCount()), ast.toString()); + if(ast.getChildrenCount() != 2) { + Errors.inst.printError(EK_EVAL_INVBIN, Integer.toString(ast.getChildrenCount()), + ast.toString()); return new Tree<>(Node.FAIL(ast)); } @@ -275,7 +279,7 @@ public class Evaluator { final EvaluatorResult leftRes = left.getHead().resultVal; final EvaluatorResult rightRes = right.getHead().resultVal; - switch (binOp) { + switch(binOp) { case ADD: case SUBTRACT: case MULTIPLY: @@ -298,16 +302,16 @@ public class Evaluator { /* Evaluate a binary operator on strings. */ private static ITree<Node> evaluateStringBinary(final Token.Type op, final EvaluatorResult left, final EvaluatorResult right, final Context ctx) { - if (left.type != STRING) { + if(left.type != STRING) { Errors.inst.printError(EK_EVAL_INVSTRING, left.type.toString()); return new Tree<>(Node.FAIL(left)); } final String strang = ((StringEvaluatorResult) left).stringVal; - switch (op) { + switch(op) { case STRCAT: - if (right.type != STRING) { + if(right.type != STRING) { Errors.inst.printError(EK_EVAL_UNSTRING, right.type.toString()); return new Tree<>(Node.FAIL(right)); } @@ -317,7 +321,7 @@ public class Evaluator { return new Tree<>(new Node(Node.Type.RESULT, cres)); case STRREP: - if (right.type != INT) { + if(right.type != INT) { Errors.inst.printError(EK_EVAL_INVSTRING, right.type.toString()); return new Tree<>(Node.FAIL(right)); } @@ -325,7 +329,7 @@ public class Evaluator { String res = strang; final long count = right.intVal; - for (long i = 1; i < count; i++) { + for(long i = 1; i < count; i++) { res += strang; } @@ -341,7 +345,7 @@ public class Evaluator { final EvaluatorResult right, final Context ctx) { EvaluatorResult res = null; - switch (op) { + switch(op) { /* * @TODO 10/09/17 Ben Culkin :DiceSimplify * @@ -350,27 +354,27 @@ public class Evaluator { * ADDENDA: Replace the .diceVal.isList() with .isList() */ case DICEGROUP: - if (left.type == DICE && !((DiceEvaluatorResult) left).diceVal.isList()) { + if(left.type == DICE && !((DiceEvaluatorResult) left).diceVal.isList()) { Die lhs = ((ScalarDiceExpression) ((DiceEvaluatorResult) left).diceVal).scalar; - if (right.type == DICE && !((DiceEvaluatorResult) right).diceVal.isList()) { + if(right.type == DICE && !((DiceEvaluatorResult) right).diceVal.isList()) { Die rhs = ((ScalarDiceExpression) ((DiceEvaluatorResult) right).diceVal).scalar; Die simple = new SimpleDie(lhs, rhs); res = new DiceEvaluatorResult(simple); - } else if (right.type == INT) { + } else if(right.type == INT) { res = new DiceEvaluatorResult(new SimpleDie(lhs, right.intVal)); } else { Errors.inst.printError(EK_EVAL_INVDGROUP, right.type.toString()); return new Tree<>(Node.FAIL(right)); } - } else if (left.type == INT) { - if (right.type == DICE && !((DiceEvaluatorResult) right).diceVal.isList()) { + } else if(left.type == INT) { + if(right.type == DICE && !((DiceEvaluatorResult) right).diceVal.isList()) { Die rhs = ((ScalarDiceExpression) ((DiceEvaluatorResult) right).diceVal).scalar; res = new DiceEvaluatorResult(new SimpleDie(left.intVal, rhs)); - } else if (right.type == INT) { + } else if(right.type == INT) { res = new DiceEvaluatorResult(new SimpleDie(left.intVal, right.intVal)); } else { Errors.inst.printError(EK_EVAL_INVDGROUP, right.type.toString()); @@ -382,10 +386,10 @@ public class Evaluator { } case DICECONCAT: - if (left.type != DICE || ((DiceEvaluatorResult) left).diceVal.isList()) { + if(left.type != DICE || ((DiceEvaluatorResult) left).diceVal.isList()) { Errors.inst.printError(EK_EVAL_INVDICE, left.type.toString()); return new Tree<>(Node.FAIL(left)); - } else if (right.type != DICE || ((DiceEvaluatorResult) right).diceVal.isList()) { + } else if(right.type != DICE || ((DiceEvaluatorResult) right).diceVal.isList()) { Errors.inst.printError(EK_EVAL_INVDICE, right.type.toString()); return new Tree<>(Node.FAIL(right)); } else { @@ -398,10 +402,10 @@ public class Evaluator { break; case DICELIST: - if (left.type != DICE || ((DiceEvaluatorResult) left).diceVal.isList()) { + if(left.type != DICE || ((DiceEvaluatorResult) left).diceVal.isList()) { Errors.inst.printError(EK_EVAL_INVDICE, left.type.toString()); return new Tree<>(Node.FAIL(left)); - } else if (right.type != DICE || ((DiceEvaluatorResult) right).diceVal.isList()) { + } else if(right.type != DICE || ((DiceEvaluatorResult) right).diceVal.isList()) { Errors.inst.printError(EK_EVAL_INVDICE, right.type.toString()); return new Tree<>(Node.FAIL(right)); } else { @@ -424,42 +428,42 @@ public class Evaluator { /* Evaluate a binary math operator. */ private static ITree<Node> evaluateMathBinary(final Token.Type op, final EvaluatorResult left, final EvaluatorResult right, final Context ctx) { - if (left.type == STRING || right.type == STRING) { + if(left.type == STRING || right.type == STRING) { Errors.inst.printError(EK_EVAL_STRINGMATH); return new Tree<>(Node.FAIL()); - } else if (left.type == FAILURE || right.type == FAILURE) { + } else if(left.type == FAILURE || right.type == FAILURE) { return new Tree<>(Node.FAIL()); - } else if (left.type == INT && right.type != INT) { + } else if(left.type == INT && right.type != INT) { Errors.inst.printError(EK_EVAL_MISMATH); return new Tree<>(Node.FAIL(right)); - } else if (left.type == FLOAT && right.type != FLOAT) { + } else if(left.type == FLOAT && right.type != FLOAT) { Errors.inst.printError(EK_EVAL_MISMATH); return new Tree<>(Node.FAIL(right)); - } else if (left.type == DICE && right.type != DICE) { + } else if(left.type == DICE && right.type != DICE) { Errors.inst.printError(EK_EVAL_MISMATH); return new Tree<>(Node.FAIL(right)); - } else if (right.type == INT && left.type != INT) { + } else if(right.type == INT && left.type != INT) { Errors.inst.printError(EK_EVAL_MISMATH); return new Tree<>(Node.FAIL(left)); - } else if (right.type == FLOAT && left.type != FLOAT) { + } else if(right.type == FLOAT && left.type != FLOAT) { Errors.inst.printError(EK_EVAL_MISMATH); return new Tree<>(Node.FAIL(left)); - } else if (right.type == DICE && left.type != DICE) { + } else if(right.type == DICE && left.type != DICE) { Errors.inst.printError(EK_EVAL_MISMATH); return new Tree<>(Node.FAIL(left)); } EvaluatorResult res = null; - switch (op) { + switch(op) { case ADD: - if (left.type == INT) { + if(left.type == INT) { res = new EvaluatorResult(INT, left.intVal + right.intVal); - } else if (left.type == DICE) { - if (((DiceEvaluatorResult) left).diceVal.isList()) { + } else if(left.type == DICE) { + if(((DiceEvaluatorResult) left).diceVal.isList()) { Errors.inst.printError(EK_EVAL_INVDICE, left.toString()); return new Tree<>(Node.FAIL(left)); - } else if (((DiceEvaluatorResult) right).diceVal.isList()) { + } else if(((DiceEvaluatorResult) right).diceVal.isList()) { Errors.inst.printError(EK_EVAL_INVDICE, right.toString()); return new Tree<>(Node.FAIL(right)); } @@ -469,20 +473,20 @@ public class Evaluator { res = new DiceEvaluatorResult(new MathDie(MathDie.MathOp.ADD, lhs, rhs)); } else { - res = new FloatEvaluatorResult( - ((FloatEvaluatorResult) left).floatVal + ((FloatEvaluatorResult) right).floatVal); + res = new FloatEvaluatorResult(((FloatEvaluatorResult) left).floatVal + + ((FloatEvaluatorResult) right).floatVal); } break; case SUBTRACT: - if (left.type == INT) { + if(left.type == INT) { res = new EvaluatorResult(INT, left.intVal - right.intVal); - } else if (left.type == DICE) { - if (((DiceEvaluatorResult) left).diceVal.isList()) { + } else if(left.type == DICE) { + if(((DiceEvaluatorResult) left).diceVal.isList()) { Errors.inst.printError(EK_EVAL_INVDICE, left.toString()); return new Tree<>(Node.FAIL(left)); - } else if (((DiceEvaluatorResult) right).diceVal.isList()) { + } else if(((DiceEvaluatorResult) right).diceVal.isList()) { Errors.inst.printError(EK_EVAL_INVDICE, right.toString()); return new Tree<>(Node.FAIL(right)); } @@ -492,20 +496,20 @@ public class Evaluator { res = new DiceEvaluatorResult(new MathDie(MathDie.MathOp.SUBTRACT, lhs, rhs)); } else { - res = new FloatEvaluatorResult( - ((FloatEvaluatorResult) left).floatVal - ((FloatEvaluatorResult) right).floatVal); + res = new FloatEvaluatorResult(((FloatEvaluatorResult) left).floatVal + - ((FloatEvaluatorResult) right).floatVal); } break; case MULTIPLY: - if (left.type == INT) { + if(left.type == INT) { res = new EvaluatorResult(INT, left.intVal * right.intVal); - } else if (left.type == DICE) { - if (((DiceEvaluatorResult) left).diceVal.isList()) { + } else if(left.type == DICE) { + if(((DiceEvaluatorResult) left).diceVal.isList()) { Errors.inst.printError(EK_EVAL_INVDICE, left.toString()); return new Tree<>(Node.FAIL(left)); - } else if (((DiceEvaluatorResult) right).diceVal.isList()) { + } else if(((DiceEvaluatorResult) right).diceVal.isList()) { Errors.inst.printError(EK_EVAL_INVDICE, right.toString()); return new Tree<>(Node.FAIL(right)); } @@ -515,27 +519,27 @@ public class Evaluator { res = new DiceEvaluatorResult(new MathDie(MathDie.MathOp.MULTIPLY, lhs, rhs)); } else { - res = new FloatEvaluatorResult( - ((FloatEvaluatorResult) left).floatVal * ((FloatEvaluatorResult) right).floatVal); + res = new FloatEvaluatorResult(((FloatEvaluatorResult) left).floatVal + * ((FloatEvaluatorResult) right).floatVal); } break; case DIVIDE: - if (left.type == INT) { - if (right.intVal == 0) { + if(left.type == INT) { + if(right.intVal == 0) { Errors.inst.printError(EK_EVAL_DIVZERO); res = new FailureEvaluatorResult(right); } else { res = new EvaluatorResult(FLOAT, left.intVal / right.intVal); } - } else if (left.type == FLOAT) { - if (((FloatEvaluatorResult) right).floatVal == 0) { + } else if(left.type == FLOAT) { + if(((FloatEvaluatorResult) right).floatVal == 0) { Errors.inst.printError(EK_EVAL_DIVZERO); res = new FailureEvaluatorResult(right); } else { - res = new FloatEvaluatorResult( - ((FloatEvaluatorResult) left).floatVal / ((FloatEvaluatorResult) right).floatVal); + res = new FloatEvaluatorResult(((FloatEvaluatorResult) left).floatVal + / ((FloatEvaluatorResult) right).floatVal); } } else { Errors.inst.printError(EK_EVAL_DIVDICE); @@ -545,20 +549,20 @@ public class Evaluator { break; case IDIVIDE: - if (left.type == INT) { - if (right.intVal == 0) { + if(left.type == INT) { + if(right.intVal == 0) { Errors.inst.printError(EK_EVAL_DIVZERO); res = new FailureEvaluatorResult(right); } else { res = new EvaluatorResult(INT, (int) (left.intVal / right.intVal)); } - } else if (left.type == FLOAT) { - if (((FloatEvaluatorResult) right).floatVal == 0) { + } else if(left.type == FLOAT) { + if(((FloatEvaluatorResult) right).floatVal == 0) { Errors.inst.printError(EK_EVAL_DIVZERO); res = new FailureEvaluatorResult(right); } else { - res = new EvaluatorResult(INT, - (int) (((FloatEvaluatorResult) left).floatVal / ((FloatEvaluatorResult) right).floatVal)); + res = new EvaluatorResult(INT, (int) (((FloatEvaluatorResult) left).floatVal + / ((FloatEvaluatorResult) right).floatVal)); } } else { Errors.inst.printError(EK_EVAL_DIVDICE); @@ -579,7 +583,7 @@ public class Evaluator { private ITree<Node> evaluateTokenRef(final Token tk, final Context ctx) { EvaluatorResult res = null; - switch (tk.type) { + switch(tk.type) { case INT_LIT: res = new EvaluatorResult(INT, tk.intValue); break; diff --git a/base/src/bjc/dicelang/eval/EvaluatorResult.java b/base/src/bjc/dicelang/eval/EvaluatorResult.java index 3ac0202..1a783ad 100644 --- a/base/src/bjc/dicelang/eval/EvaluatorResult.java +++ b/base/src/bjc/dicelang/eval/EvaluatorResult.java @@ -57,7 +57,7 @@ public class EvaluatorResult { * Create a new result. * * @param typ - * The type of the result. + * The type of the result. */ protected EvaluatorResult(final EvaluatorResult.Type typ) { type = typ; diff --git a/base/src/bjc/dicelang/eval/FailureEvaluatorResult.java b/base/src/bjc/dicelang/eval/FailureEvaluatorResult.java index 3b641db..d3b2cbe 100644 --- a/base/src/bjc/dicelang/eval/FailureEvaluatorResult.java +++ b/base/src/bjc/dicelang/eval/FailureEvaluatorResult.java @@ -9,14 +9,14 @@ public class FailureEvaluatorResult extends EvaluatorResult { * Original node data */ public ITree<Node> origVal; - + public FailureEvaluatorResult() { super(Type.FAILURE); } - + public FailureEvaluatorResult(final ITree<Node> orig) { super(Type.FAILURE); - + origVal = orig; } @@ -38,18 +38,13 @@ public class FailureEvaluatorResult extends EvaluatorResult { @Override public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; + if(this == obj) return true; + if(obj == null) return false; + if(getClass() != obj.getClass()) return false; FailureEvaluatorResult other = (FailureEvaluatorResult) obj; - if (origVal == null) { - if (other.origVal != null) - return false; - } else if (!origVal.equals(other.origVal)) - return false; + if(origVal == null) { + if(other.origVal != null) return false; + } else if(!origVal.equals(other.origVal)) return false; return true; } } diff --git a/base/src/bjc/dicelang/eval/FloatEvaluatorResult.java b/base/src/bjc/dicelang/eval/FloatEvaluatorResult.java index 7fbbcdc..22f89f0 100644 --- a/base/src/bjc/dicelang/eval/FloatEvaluatorResult.java +++ b/base/src/bjc/dicelang/eval/FloatEvaluatorResult.java @@ -5,13 +5,13 @@ public class FloatEvaluatorResult extends EvaluatorResult { * The float value of the result. */ public double floatVal; - + public FloatEvaluatorResult(double val) { super(Type.FLOAT); - + floatVal = val; } - + @Override public String toString() { return super.toString() + "(" + floatVal + ")"; @@ -29,15 +29,11 @@ public class FloatEvaluatorResult extends EvaluatorResult { @Override public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; + if(this == obj) return true; + if(obj == null) return false; + if(getClass() != obj.getClass()) return false; FloatEvaluatorResult other = (FloatEvaluatorResult) obj; - if (Double.doubleToLongBits(floatVal) != Double.doubleToLongBits(other.floatVal)) - return false; + if(Double.doubleToLongBits(floatVal) != Double.doubleToLongBits(other.floatVal)) return false; return true; } } diff --git a/base/src/bjc/dicelang/eval/StringEvaluatorResult.java b/base/src/bjc/dicelang/eval/StringEvaluatorResult.java index 870fd01..251b318 100644 --- a/base/src/bjc/dicelang/eval/StringEvaluatorResult.java +++ b/base/src/bjc/dicelang/eval/StringEvaluatorResult.java @@ -8,10 +8,10 @@ public class StringEvaluatorResult extends EvaluatorResult { public StringEvaluatorResult(String strang) { super(Type.STRING); - + stringVal = strang; } - + @Override public String toString() { return super.toString() + "(" + stringVal + ")"; @@ -27,18 +27,13 @@ public class StringEvaluatorResult extends EvaluatorResult { @Override public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; + if(this == obj) return true; + if(obj == null) return false; + if(getClass() != obj.getClass()) return false; StringEvaluatorResult other = (StringEvaluatorResult) obj; - if (stringVal == null) { - if (other.stringVal != null) - return false; - } else if (!stringVal.equals(other.stringVal)) - return false; + if(stringVal == null) { + if(other.stringVal != null) return false; + } else if(!stringVal.equals(other.stringVal)) return false; return true; } } diff --git a/base/src/bjc/dicelang/expr/ExprREPL.java b/base/src/bjc/dicelang/expr/ExprREPL.java index 49b9575..d763d67 100644 --- a/base/src/bjc/dicelang/expr/ExprREPL.java +++ b/base/src/bjc/dicelang/expr/ExprREPL.java @@ -17,7 +17,7 @@ public class ExprREPL { * Main method. * * @param args - * Unused CLI args. + * Unused CLI args. */ public static void main(final String[] args) { /* Create our objects. */ @@ -32,7 +32,7 @@ public class ExprREPL { String ln = scan.nextLine().trim(); /* Enter REPL loop. */ - while (!ln.equals("")) { + while(!ln.equals("")) { /* Print raw command. */ System.out.println("Raw command: " + ln); System.out.println(); @@ -40,13 +40,13 @@ public class ExprREPL { /* Lex command to infix tokens. */ final Token[] infixTokens = lex.lexString(ln, toks); System.out.println("Lexed tokens: "); - for (final Token tok : infixTokens) { + for(final Token tok : infixTokens) { System.out.println("\t" + tok); } /* Print out infix expression. */ System.out.print("Lexed expression: "); - for (final Token tok : infixTokens) { + for(final Token tok : infixTokens) { System.out.print(tok.toExpr() + " "); } @@ -57,13 +57,13 @@ public class ExprREPL { /* Shunt infix tokens to postfix tokens. */ final IList<Token> postfixTokens = Shunter.shuntTokens(infixTokens); System.out.println("Lexed tokens: "); - for (final Token tok : postfixTokens) { + for(final Token tok : postfixTokens) { System.out.println("\t" + tok); } /* Print out postfix tokens. */ System.out.print("Shunted expression: "); - for (final Token tok : postfixTokens) { + for(final Token tok : postfixTokens) { System.out.print(tok.toExpr() + " "); } @@ -71,10 +71,15 @@ public class ExprREPL { System.out.println(); System.out.println(); - /* Construct a tree from the list of postfixed tokens. */ - final ITree<Token> ast = TreeConstructor.constructTree(postfixTokens, tok -> tok.typ.isOperator); + /* + * Construct a tree from the list of postfixed tokens. + */ + final ITree<Token> ast = TreeConstructor.constructTree(postfixTokens, + tok -> tok.typ.isOperator); - /* Print the tree, then the canonical expression for it. */ + /* + * Print the tree, then the canonical expression for it. + */ System.out.println("Parsed tree"); System.out.println(ast.toString()); System.out.println("\nCanonical expr: " + Parser.toCanonicalExpr(ast)); diff --git a/base/src/bjc/dicelang/expr/Ezpr.java b/base/src/bjc/dicelang/expr/Ezpr.java index 4919c81..2a54b6e 100644 --- a/base/src/bjc/dicelang/expr/Ezpr.java +++ b/base/src/bjc/dicelang/expr/Ezpr.java @@ -42,7 +42,7 @@ public class Ezpr { @Override public String toString() { - if (typ == TOKEN) { + if(typ == TOKEN) { return tok.toString(); } return ezp.toString(); @@ -65,26 +65,28 @@ public class Ezpr { HashMultiset<EzprNode> newPositive = HashMultiset.create(); HashMultiset<EzprNode> newNegative = HashMultiset.create(); - for (EzprNode nd : positive) { + for(EzprNode nd : positive) { /* Flatten enclosed ezprs of the same type. */ - if (nd.typ == EZPR && (nd.ezp.typ == typ)) { + if(nd.typ == EZPR && (nd.ezp.typ == typ)) { /* Recursively flatten kids. */ Ezpr kid = nd.ezp.flatten(); - if (typ == SUM) { - /* Add sum parts to corresponding bags. */ - for (EzprNode knd : kid.positive) { + if(typ == SUM) { + /* + * Add sum parts to corresponding bags. + */ + for(EzprNode knd : kid.positive) { newPositive.add(knd); } - for (EzprNode knd : kid.negative) { + for(EzprNode knd : kid.negative) { newNegative.add(knd); } } else { /* @TODO ensure that this is correct. */ - for (EzprNode knd : kid.positive) { + for(EzprNode knd : kid.positive) { newPositive.add(knd); } - for (EzprNode knd : kid.negative) { + for(EzprNode knd : kid.negative) { newNegative.add(knd); } } @@ -93,25 +95,25 @@ public class Ezpr { } } - for (EzprNode nd : negative) { + for(EzprNode nd : negative) { /* Flatten enclosed ezprs of the same type. */ - if (nd.typ == EZPR && (nd.ezp.typ == typ)) { + if(nd.typ == EZPR && (nd.ezp.typ == typ)) { /* Recursively flatten kids. */ Ezpr kid = nd.ezp.flatten(); /* @TODO ensure that this is correct. */ - if (typ == SUM) { - for (EzprNode knd : kid.positive) { + if(typ == SUM) { + for(EzprNode knd : kid.positive) { newNegative.add(knd); } - for (EzprNode knd : kid.negative) { + for(EzprNode knd : kid.negative) { newPositive.add(knd); } } else { - for (EzprNode knd : kid.positive) { + for(EzprNode knd : kid.positive) { newNegative.add(knd); } - for (EzprNode knd : kid.negative) { + for(EzprNode knd : kid.negative) { newPositive.add(knd); } } @@ -128,13 +130,13 @@ public class Ezpr { StringBuilder sb = new StringBuilder(typ.toString()); sb.append(" [ "); - for (EzprNode nd : positive) { + for(EzprNode nd : positive) { sb.append(nd.toString()); sb.append(" "); } sb.append("# "); - for (EzprNode nd : negative) { + for(EzprNode nd : negative) { sb.append(nd.toString()); sb.append(" "); } diff --git a/base/src/bjc/dicelang/expr/Lexer.java b/base/src/bjc/dicelang/expr/Lexer.java index c7dd92f..c43050b 100644 --- a/base/src/bjc/dicelang/expr/Lexer.java +++ b/base/src/bjc/dicelang/expr/Lexer.java @@ -34,10 +34,10 @@ public class Lexer { * Convert a string from a input command to a series of infix tokens. * * @param inp - * The input command. + * The input command. * * @param tks - * The token state. + * The token state. * * @return A series of infix tokens representing the command. */ @@ -48,7 +48,7 @@ public class Lexer { final List<Token> tokens = new LinkedList<>(); /* Process each token. */ - for (final String spacedToken : spacedTokens) { + for(final String spacedToken : spacedTokens) { /* Split on operators. */ final IList<String> splitTokens = split.split(spacedToken); diff --git a/base/src/bjc/dicelang/expr/Parser.java b/base/src/bjc/dicelang/expr/Parser.java index 1156334..020467a 100644 --- a/base/src/bjc/dicelang/expr/Parser.java +++ b/base/src/bjc/dicelang/expr/Parser.java @@ -9,13 +9,13 @@ import bjc.utils.data.ITree; */ public class Parser { /* - * Convert an expression to one that uses the smallest necessary amount of - * parens. + * Convert an expression to one that uses the smallest necessary amount + * of parens. */ public static String toCanonicalExpr(final ITree<Token> ast) { final Token data = ast.getHead(); - if (ast.getChildrenCount() == 0) { + if(ast.getChildrenCount() == 0) { /* Handle leaf nodes. */ return data.toExpr(); } @@ -29,21 +29,21 @@ public class Parser { String rightExpr = toCanonicalExpr(right); /* Add parens if the left was higher priority. */ - if (left.getChildrenCount() == 0) { + if(left.getChildrenCount() == 0) { int leftPriority = left.getHead().typ.operatorPriority; int dataPriority = data.typ.operatorPriority; - if (leftPriority >= dataPriority) { + if(leftPriority >= dataPriority) { leftExpr = String.format("(%s)", leftExpr); } } /* Add parens if the right was higher priority. */ - if (right.getChildrenCount() == 0) { + if(right.getChildrenCount() == 0) { int rightPriority = right.getHead().typ.operatorPriority; int dataPriority = data.typ.operatorPriority; - if (rightPriority >= dataPriority) { + if(rightPriority >= dataPriority) { rightExpr = String.format("(%s)", rightExpr); } } diff --git a/base/src/bjc/dicelang/expr/Shunter.java b/base/src/bjc/dicelang/expr/Shunter.java index 53c2298..3d2a523 100644 --- a/base/src/bjc/dicelang/expr/Shunter.java +++ b/base/src/bjc/dicelang/expr/Shunter.java @@ -16,7 +16,7 @@ public class Shunter { * Convert a infix series of tokens to a postfix series of tokens. * * @param infixTokens - * The tokens in infix order. + * The tokens in infix order. * * @return The tokens in postfix order. */ @@ -28,20 +28,22 @@ public class Shunter { final Deque<Token> opStack = new LinkedList<>(); /* Shunt each token. */ - for (final Token tok : infixTokens) { + for(final Token tok : infixTokens) { /* Handle operators. */ - if (tok.typ.isOperator) { + if(tok.typ.isOperator) { Token curOp = opStack.peek(); /* - * Check if an operator is higher priority, respecting their left associativity. + * Check if an operator is higher priority, + * respecting their left associativity. * - * @NOTE Should this be factored out into a method? + * @NOTE Should this be factored out into a + * method? */ int leftPriority = tok.typ.operatorPriority; int rightPriority; - if (curOp == null) { + if(curOp == null) { rightPriority = 0; } else { rightPriority = curOp.typ.operatorPriority; @@ -50,14 +52,15 @@ public class Shunter { boolean isHigherPrec = leftPriority >= rightPriority; /* - * Pop all operators that are lower precedence than us. + * Pop all operators that are lower precedence + * than us. */ - while (!opStack.isEmpty() && isHigherPrec) { + while(!opStack.isEmpty() && isHigherPrec) { postfixTokens.add(opStack.pop()); curOp = opStack.peek(); leftPriority = tok.typ.operatorPriority; - if (curOp == null) { + if(curOp == null) { rightPriority = 0; } else { rightPriority = curOp.typ.operatorPriority; @@ -67,21 +70,22 @@ public class Shunter { } opStack.push(tok); - } else if (tok.typ == TokenType.OPAREN) { + } else if(tok.typ == TokenType.OPAREN) { opStack.push(tok); - } else if (tok.typ == TokenType.CPAREN) { + } else if(tok.typ == TokenType.CPAREN) { Token curOp = opStack.peek(); /* - * Pop things until we find the matching parenthesis. + * Pop things until we find the matching + * parenthesis. */ - while (curOp.typ != TokenType.OPAREN) { + while(curOp.typ != TokenType.OPAREN) { final Token tk = opStack.pop(); postfixTokens.add(tk); curOp = opStack.peek(); } - if (!opStack.isEmpty()) { + if(!opStack.isEmpty()) { opStack.pop(); } } else { @@ -92,7 +96,7 @@ public class Shunter { /* * Flush remaining operators. */ - while (!opStack.isEmpty()) { + while(!opStack.isEmpty()) { postfixTokens.add(opStack.pop()); } diff --git a/base/src/bjc/dicelang/expr/Token.java b/base/src/bjc/dicelang/expr/Token.java index 750362d..f083577 100644 --- a/base/src/bjc/dicelang/expr/Token.java +++ b/base/src/bjc/dicelang/expr/Token.java @@ -33,13 +33,13 @@ public class Token { * Create a new token. * * @param type - * The type of this token. + * The type of this token. * * @param raw - * The string this token came from. + * The string this token came from. * * @param toks - * The state for this token + * The state for this token */ public Token(final TokenType type, final String raw, final Tokens toks) { this.typ = type; @@ -53,7 +53,7 @@ public class Token { String typeStr = typ.toString(); typeStr = String.format("%s (%s)", typeStr, typ.name()); - if (typ == TokenType.VREF) { + if(typ == TokenType.VREF) { typeStr += " (ind. " + intValue; typeStr += ", sym. \"" + tks.symbolTable.get(intValue) + "\")"; } @@ -67,7 +67,7 @@ public class Token { * @return The string representation of it. */ public String toExpr() { - switch (typ) { + switch(typ) { case ADD: return "+"; case SUBTRACT: diff --git a/base/src/bjc/dicelang/expr/TokenType.java b/base/src/bjc/dicelang/expr/TokenType.java index 71dd597..93de6d9 100644 --- a/base/src/bjc/dicelang/expr/TokenType.java +++ b/base/src/bjc/dicelang/expr/TokenType.java @@ -10,8 +10,8 @@ public enum TokenType { * @NOTE * * Do we want to switch to auto-numbering the tokens? They were manually - * numbered because this was an assignment for PoPL and that was what Dr. Naz - * wanted. + * numbered because this was an assignment for PoPL and that was what + * Dr. Naz wanted. */ /** Represents + */ diff --git a/base/src/bjc/dicelang/expr/Tokens.java b/base/src/bjc/dicelang/expr/Tokens.java index 687a414..c307391 100644 --- a/base/src/bjc/dicelang/expr/Tokens.java +++ b/base/src/bjc/dicelang/expr/Tokens.java @@ -58,14 +58,14 @@ public class Tokens { * Convert the string representation of a token into a token. * * @param tok - * The string representation of the token. + * The string representation of the token. * @param raw - * The original string the token came from. + * The original string the token came from. * * @return The token the string represents. */ public Token lexToken(final String tok, final String raw) { - if (litTokens.containsKey(tok)) { + if(litTokens.containsKey(tok)) { /* Return matching literal token. */ return new Token(litTokens.get(tok), raw, this); } @@ -78,7 +78,7 @@ public class Tokens { private Token parseVRef(final String tok, final String raw) { final Token tk = new Token(TokenType.VREF, raw, this); - if (revSymTab.containsKey(tok)) { + if(revSymTab.containsKey(tok)) { /* Reuse the entry if it exists. */ tk.intValue = revSymTab.get(tok); } else { diff --git a/base/src/bjc/dicelang/DiceToken.java b/base/src/bjc/dicelang/tokens/DiceToken.java index fcb8a0c..4bf2068 100644 --- a/base/src/bjc/dicelang/DiceToken.java +++ b/base/src/bjc/dicelang/tokens/DiceToken.java @@ -1,4 +1,4 @@ -package bjc.dicelang; +package bjc.dicelang.tokens; import bjc.dicelang.dice.DiceExpression; @@ -26,18 +26,13 @@ public class DiceToken extends Token { @Override public boolean equals(Object obj) { - if (this == obj) - return true; - if (!super.equals(obj)) - return false; - if (getClass() != obj.getClass()) - return false; + if(this == obj) return true; + if(!super.equals(obj)) return false; + if(getClass() != obj.getClass()) return false; DiceToken other = (DiceToken) obj; - if (diceValue == null) { - if (other.diceValue != null) - return false; - } else if (!diceValue.equals(other.diceValue)) - return false; + if(diceValue == null) { + if(other.diceValue != null) return false; + } else if(!diceValue.equals(other.diceValue)) return false; return true; } } diff --git a/base/src/bjc/dicelang/FloatToken.java b/base/src/bjc/dicelang/tokens/FloatToken.java index 786df6e..f56bef7 100644 --- a/base/src/bjc/dicelang/FloatToken.java +++ b/base/src/bjc/dicelang/tokens/FloatToken.java @@ -1,4 +1,4 @@ -package bjc.dicelang; +package bjc.dicelang.tokens; public class FloatToken extends Token { public double floatValue; @@ -26,15 +26,11 @@ public class FloatToken extends Token { @Override public boolean equals(Object obj) { - if (this == obj) - return true; - if (!super.equals(obj)) - return false; - if (getClass() != obj.getClass()) - return false; + if(this == obj) return true; + if(!super.equals(obj)) return false; + if(getClass() != obj.getClass()) return false; FloatToken other = (FloatToken) obj; - if (Double.doubleToLongBits(floatValue) != Double.doubleToLongBits(other.floatValue)) - return false; + if(Double.doubleToLongBits(floatValue) != Double.doubleToLongBits(other.floatValue)) return false; return true; } } diff --git a/base/src/bjc/dicelang/tokens/IntToken.java b/base/src/bjc/dicelang/tokens/IntToken.java new file mode 100644 index 0000000..5ff4b7c --- /dev/null +++ b/base/src/bjc/dicelang/tokens/IntToken.java @@ -0,0 +1,26 @@ +package bjc.dicelang.tokens; + +/** + * Represents an integer literal. + * + * @author EVE + * + */ +public class IntToken extends Token { + /** + * The literal value. + */ + public final long value; + + /** + * Create a new integer literal. + * + * @param val + * The integer to use. + */ + public IntToken(long val) { + super(Type.INT_LIT); + + value = val; + } +} diff --git a/base/src/bjc/dicelang/Token.java b/base/src/bjc/dicelang/tokens/Token.java index b940925..1a11ccd 100644 --- a/base/src/bjc/dicelang/Token.java +++ b/base/src/bjc/dicelang/tokens/Token.java @@ -1,4 +1,4 @@ -package bjc.dicelang; +package bjc.dicelang.tokens; import bjc.utils.funcdata.IList; @@ -105,7 +105,7 @@ public class Token { @Override public String toString() { - switch (type) { + switch(type) { case INT_LIT: case STRING_LIT: case VREF: @@ -129,17 +129,17 @@ public class Token { @Override public boolean equals(final Object other) { - if (!(other instanceof Token)) { + if(!(other instanceof Token)) { return false; } final Token otk = (Token) other; - if (otk.type != type) { + if(otk.type != type) { return false; } - switch (type) { + switch(type) { case OBRACE: case OBRACKET: return intValue == otk.intValue; @@ -149,7 +149,7 @@ public class Token { } public boolean isGrouper() { - switch (type) { + switch(type) { case OPAREN: case OBRACE: case OBRACKET: @@ -159,4 +159,10 @@ public class Token { return false; } } + + @Override + public int hashCode() { + // TODO Auto-generated method stub + return super.hashCode(); + } } diff --git a/base/src/bjc/dicelang/util/ResourceLoader.java b/base/src/bjc/dicelang/util/ResourceLoader.java index 085081f..6777443 100644 --- a/base/src/bjc/dicelang/util/ResourceLoader.java +++ b/base/src/bjc/dicelang/util/ResourceLoader.java @@ -22,9 +22,10 @@ public class ResourceLoader { * Loads a .help file from the data/help directory. * * @param name - * The name of the help file to load. + * The name of the help file to load. * - * @return The contents of the help file, or null if it could not be opened. + * @return The contents of the help file, or null if it could not be + * opened. */ public static String[] loadHelpFile(final String name) { final URL fle = ResourceLoader.class.getResource("/data/help/" + name + ".help"); @@ -33,7 +34,7 @@ public class ResourceLoader { Path pth = Paths.get(fle.toURI()); return Files.lines(pth).toArray(sze -> new String[sze]); - } catch (IOException | URISyntaxException ioex) { + } catch(IOException | URISyntaxException ioex) { Errors.inst.printError(EK_MISC_IOEX, fle.toString()); } diff --git a/dice/pom.xml b/dice/pom.xml index cf53f8a..da9d618 100644 --- a/dice/pom.xml +++ b/dice/pom.xml @@ -1,6 +1,7 @@ <?xml version="1.0"?> -<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> +<project + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" + xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> @@ -26,5 +27,10 @@ <version>3.8.1</version> <scope>test</scope> </dependency> + <dependency> + <groupId>bjc</groupId> + <artifactId>BJC-Utils2</artifactId> + <version>1.0.0</version> + </dependency> </dependencies> </project> diff --git a/dice/src/main/java/bjc/dicelang/dice/CompoundDie.java b/dice/src/main/java/bjc/dicelang/dice/CompoundDie.java index b66022c..2c6cec2 100644 --- a/dice/src/main/java/bjc/dicelang/dice/CompoundDie.java +++ b/dice/src/main/java/bjc/dicelang/dice/CompoundDie.java @@ -1,91 +1,84 @@ -package bjc.dicelang.dice;
-
-/**
- * A die whose rolls result from concatenating two other rolls together.
- *
- * @author Ben Culkin
- */
-public class CompoundDie implements Die {
- /* The dice that form this die */
- private final Die left;
- private final Die right;
-
- /**
- * Create a new compound die.
- *
- * @param lft
- * The left die
- * @param rght
- * The right die
- */
- public CompoundDie(final Die lft, final Die rght) {
- left = lft;
- right = rght;
- }
-
- @Override
- public boolean canOptimize() {
- return left.canOptimize() && right.canOptimize();
- }
-
- @Override
- public long optimize() {
- long leftOpt = left.optimize();
- long rightOpt = right.optimize();
-
- return Long.parseLong(String.format("%d%d", leftOpt, rightOpt));
- }
-
- @Override
- public long roll() {
- long leftRoll = left.optimize();
- long rightRoll = right.optimize();
-
- return Long.parseLong(String.format("%d%d", leftRoll, rightRoll));
- }
-
- @Override
- public long rollSingle() {
- /* Actually one dice built using two, can't be split. */
- return roll();
- }
-
- @Override
- public String toString() {
- String leftString = left.toString();
- String rightString = right.toString();
-
- return String.format("%sc%s", leftString, rightString);
- }
-
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((left == null) ? 0 : left.hashCode());
- result = prime * result + ((right == null) ? 0 : right.hashCode());
- return result;
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- CompoundDie other = (CompoundDie) obj;
- if (left == null) {
- if (other.left != null)
- return false;
- } else if (!left.equals(other.left))
- return false;
- if (right == null) {
- if (other.right != null)
- return false;
- } else if (!right.equals(other.right))
- return false;
- return true;
- }
-}
+package bjc.dicelang.dice; + +/** + * A die whose rolls result from concatenating two other rolls together. + * + * @author Ben Culkin + */ +public class CompoundDie implements Die { + /* The dice that form this die */ + private final Die left; + private final Die right; + + /** + * Create a new compound die. + * + * @param lft + * The left die + * @param rght + * The right die + */ + public CompoundDie(final Die lft, final Die rght) { + left = lft; + right = rght; + } + + @Override + public boolean canOptimize() { + return left.canOptimize() && right.canOptimize(); + } + + @Override + public long optimize() { + long leftOpt = left.optimize(); + long rightOpt = right.optimize(); + + return Long.parseLong(String.format("%d%d", leftOpt, rightOpt)); + } + + @Override + public long roll() { + long leftRoll = left.optimize(); + long rightRoll = right.optimize(); + + return Long.parseLong(String.format("%d%d", leftRoll, rightRoll)); + } + + @Override + public long rollSingle() { + /* Actually one dice built using two, can't be split. */ + return roll(); + } + + @Override + public String toString() { + String leftString = left.toString(); + String rightString = right.toString(); + + return String.format("%sc%s", leftString, rightString); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((left == null) ? 0 : left.hashCode()); + result = prime * result + ((right == null) ? 0 : right.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if(this == obj) return true; + if(obj == null) return false; + if(getClass() != obj.getClass()) return false; + CompoundDie other = (CompoundDie) obj; + if(left == null) { + if(other.left != null) return false; + } else if(!left.equals(other.left)) return false; + if(right == null) { + if(other.right != null) return false; + } else if(!right.equals(other.right)) return false; + return true; + } +} diff --git a/dice/src/main/java/bjc/dicelang/dice/CompoundingDie.java b/dice/src/main/java/bjc/dicelang/dice/CompoundingDie.java index f4ab510..99a8fb0 100644 --- a/dice/src/main/java/bjc/dicelang/dice/CompoundingDie.java +++ b/dice/src/main/java/bjc/dicelang/dice/CompoundingDie.java @@ -1,145 +1,138 @@ -package bjc.dicelang.dice;
-
-import java.util.function.Predicate;
-
-/**
- * Implements a compounding die.
- *
- * This means that the source will be rolled, and then more single rolls will be
- * added while it meets a qualification.
- *
- * @author Ben Culkin
- */
-public class CompoundingDie implements Die {
- /* The source die to compound. */
- private final Die source;
-
- /* The predicate that marks when to compound. */
- private final Predicate<Long> compoundOn;
- /* The string version of the predicate, if one exists. */
- private final String compoundPattern;
-
- /**
- * Create a new compounding die with no pattern.
- *
- * @param src
- * The die to compound from
- * @param compound
- * The conditions to compound on
- */
- public CompoundingDie(final Die src, final Predicate<Long> compound) {
- this(src, compound, null);
- }
-
- /**
- * Create a new compounding die with a specified pattern.
- *
- * @param src
- * The die to compound from
- * @param compound
- * The conditions to compound on
- * @param patt
- * The string pattern the condition came from, for printing
- */
- public CompoundingDie(final Die src, final Predicate<Long> compound, final String patt) {
- source = src;
-
- compoundOn = compound;
- compoundPattern = patt;
- }
-
- @Override
- public boolean canOptimize() {
- if (source.canOptimize()) {
- /* We can only be optimized for a result of zero. */
- return source.optimize() == 0;
- }
-
- return false;
- }
-
- @Override
- public long optimize() {
- /* If we can be optimized, its to zero. */
- return 0;
- }
-
- @Override
- public long roll() {
- /* The current result. */
- long res = source.roll();
- /* The last result. */
- long oldRes = res;
-
- while (compoundOn.test(oldRes)) {
- /* Compound while the result should be compounded. */
- oldRes = source.rollSingle();
-
- /* Accumulate. */
- res += oldRes;
- }
-
- return res;
- }
-
- @Override
- public long rollSingle() {
- /* Just compound on an initial single role. */
- long res = source.rollSingle();
- /* The last result. */
- long oldRes = res;
-
- while (compoundOn.test(oldRes)) {
- /* Compound while the result should be compounded. */
- oldRes = source.rollSingle();
-
- /* Accumulate. */
- res += oldRes;
- }
-
- return res;
- }
-
- @Override
- public String toString() {
- String sourceString = source.toString();
-
- /* Can't print a parseable version. */
- if (compoundPattern == null) {
- return String.format("%s!!<complex-pattern>", sourceString);
- }
-
- return String.format("%s!!%s", sourceString, compoundPattern);
- }
-
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((compoundPattern == null) ? 0 : compoundPattern.hashCode());
- result = prime * result + ((source == null) ? 0 : source.hashCode());
- return result;
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- CompoundingDie other = (CompoundingDie) obj;
- if (compoundPattern == null) {
- if (other.compoundPattern != null)
- return false;
- } else if (!compoundPattern.equals(other.compoundPattern))
- return false;
- if (source == null) {
- if (other.source != null)
- return false;
- } else if (!source.equals(other.source))
- return false;
- return true;
- }
-}
+package bjc.dicelang.dice; + +import java.util.function.Predicate; + +/** + * Implements a compounding die. + * + * This means that the source will be rolled, and then more single rolls will be + * added while it meets a qualification. + * + * @author Ben Culkin + */ +public class CompoundingDie implements Die { + /* The source die to compound. */ + private final Die source; + + /* The predicate that marks when to compound. */ + private final Predicate<Long> compoundOn; + /* The string version of the predicate, if one exists. */ + private final String compoundPattern; + + /** + * Create a new compounding die with no pattern. + * + * @param src + * The die to compound from + * @param compound + * The conditions to compound on + */ + public CompoundingDie(final Die src, final Predicate<Long> compound) { + this(src, compound, null); + } + + /** + * Create a new compounding die with a specified pattern. + * + * @param src + * The die to compound from + * @param compound + * The conditions to compound on + * @param patt + * The string pattern the condition came from, for printing + */ + public CompoundingDie(final Die src, final Predicate<Long> compound, final String patt) { + source = src; + + compoundOn = compound; + compoundPattern = patt; + } + + @Override + public boolean canOptimize() { + if(source.canOptimize()) { + /* We can only be optimized for a result of zero. */ + return source.optimize() == 0; + } + + return false; + } + + @Override + public long optimize() { + /* If we can be optimized, its to zero. */ + return 0; + } + + @Override + public long roll() { + /* The current result. */ + long res = source.roll(); + /* The last result. */ + long oldRes = res; + + while(compoundOn.test(oldRes)) { + /* Compound while the result should be compounded. */ + oldRes = source.rollSingle(); + + /* Accumulate. */ + res += oldRes; + } + + return res; + } + + @Override + public long rollSingle() { + /* Just compound on an initial single role. */ + long res = source.rollSingle(); + /* The last result. */ + long oldRes = res; + + while(compoundOn.test(oldRes)) { + /* Compound while the result should be compounded. */ + oldRes = source.rollSingle(); + + /* Accumulate. */ + res += oldRes; + } + + return res; + } + + @Override + public String toString() { + String sourceString = source.toString(); + + /* Can't print a parseable version. */ + if(compoundPattern == null) { + return String.format("%s!!<complex-pattern>", sourceString); + } + + return String.format("%s!!%s", sourceString, compoundPattern); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((compoundPattern == null) ? 0 : compoundPattern.hashCode()); + result = prime * result + ((source == null) ? 0 : source.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if(this == obj) return true; + if(obj == null) return false; + if(getClass() != obj.getClass()) return false; + CompoundingDie other = (CompoundingDie) obj; + if(compoundPattern == null) { + if(other.compoundPattern != null) return false; + } else if(!compoundPattern.equals(other.compoundPattern)) return false; + if(source == null) { + if(other.source != null) return false; + } else if(!source.equals(other.source)) return false; + return true; + } +} diff --git a/dice/src/main/java/bjc/dicelang/dice/DiceBox.java b/dice/src/main/java/bjc/dicelang/dice/DiceBox.java index b8e1b3d..1bf2f63 100644 --- a/dice/src/main/java/bjc/dicelang/dice/DiceBox.java +++ b/dice/src/main/java/bjc/dicelang/dice/DiceBox.java @@ -1,5 +1,7 @@ package bjc.dicelang.dice; +import bjc.utils.funcutils.StringUtils; + import java.util.Random; import java.util.function.Predicate; import java.util.regex.Pattern; @@ -16,20 +18,21 @@ public class DiceBox { * Parse a die expression from a string. * * @param expString - * The string to parse. + * The string to parse. * * @return The die expression from the string, or null if it wasn't one */ public static DiceExpression parseExpression(final String expString) { try { return doParseExpression(expString); - } catch (Exception ex) { + } catch(Exception ex) { /* * @TODO 10/08/17 Ben Culkin :DieErrors * * :ErrorRefactor * - * Use different types of exceptions to provide better error messages. + * Use different types of exceptions to provide better + * error messages. */ String exMessage = ex.getMessage(); @@ -42,34 +45,27 @@ public class DiceBox { private static DiceExpression doParseExpression(final String expString) { /* Only bother with valid expressions. */ - if (!isValidExpression(expString)) { + if(!isValidExpression(expString)) { return null; } - if (scalarDiePattern.matcher(expString).matches()) { + if(scalarDiePattern.matcher(expString).matches()) { /* Parse scalar die. */ - /* - * @TODO 10/08/17 Ben Culkin :SubstringIndexOf - * - * This substring/index of call should be abstracted into its own method so as - * to make the code more explanatory and ensure that things like the return code - * of indexOf are correctly checked. - */ - final String dieString = expString.substring(0, expString.indexOf('s')); + final String dieString = StringUtils.substringTo(expString, "s"); final long lar = Long.parseLong(dieString); final Die scal = new ScalarDie(lar); return new ScalarDiceExpression(scal); - } else if (simpleDiePattern.matcher(expString).matches()) { + } else if(simpleDiePattern.matcher(expString).matches()) { /* Parse simple die groups. */ final String[] dieParts = expString.split("d"); final long right = Long.parseLong(dieParts[1]); final long left; - if (dieParts[0].equals("")) { + if(dieParts[0].equals("")) { /* Handle short-form expressions. */ left = 1; } else { @@ -79,33 +75,33 @@ public class DiceBox { final Die scal = new SimpleDie(left, right); return new ScalarDiceExpression(scal); - } else if (fudgeDiePattern.matcher(expString).matches()) { + } else if(fudgeDiePattern.matcher(expString).matches()) { /* Parse fudge dice. */ - /* :SubstringIndexOf */ - final String nDice = expString.substring(0, expString.indexOf('d')); + final String nDice = StringUtils.substringTo(expString, "d"); final Die fudge = new FudgeDie(Long.parseLong(nDice)); return new ScalarDiceExpression(fudge); - } else if (compoundDiePattern.matcher(expString).matches()) { + } else if(compoundDiePattern.matcher(expString).matches()) { /* Parse compound die expressions. */ final String[] dieParts = expString.split("c"); /* * @TODO 10/08/17 :SplitParse * - * Should this split string/parse split parts be abstracted into something else - * that handles doing the splitting correctly, as well as making sure that the + * Should this split string/parse split parts be + * abstracted into something else that handles doing the + * splitting correctly, as well as making sure that the * resulting DieExpressions are of the right type? */ final DiceExpression left = parseExpression(dieParts[0]); final DiceExpression right = parseExpression(dieParts[1]); /* :ErrorRefactor */ - if (left.isList()) { + if(left.isList()) { System.out.printf( "ERROR: Expected a scalar dice expression for lhs of compound die, got a list expression instead (%s)\n", left); - } else if (right.isList()) { + } else if(right.isList()) { System.out.printf( "ERROR: Expected a scalar dice expression for rhs of compound die, got a list expression instead (%s)\n", right); @@ -117,7 +113,7 @@ public class DiceBox { final Die compound = new CompoundDie(lhs, rhs); return new ScalarDiceExpression(compound); - } else if (compoundingDiePattern.matcher(expString).matches()) { + } else if(compoundingDiePattern.matcher(expString).matches()) { /* Parse compounding die expressions. */ final String[] dieParts = expString.split("!!"); @@ -129,7 +125,7 @@ public class DiceBox { final Die scal = new CompoundingDie(die, right, dieParts[1]); return new ScalarDiceExpression(scal); - } else if (explodingDiePattern.matcher(expString).matches()) { + } else if(explodingDiePattern.matcher(expString).matches()) { /* Parse exploding die expressions. */ final String[] dieParts = expString.split("!"); @@ -141,7 +137,7 @@ public class DiceBox { final DieList lst = new ExplodingDice(lhs, right, dieParts[1], false); return new ListDiceExpression(lst); - } else if (penetratingDiePattern.matcher(expString).matches()) { + } else if(penetratingDiePattern.matcher(expString).matches()) { /* Parse penetrating die expressions. */ final String[] dieParts = expString.split("p!"); @@ -153,7 +149,7 @@ public class DiceBox { final DieList lst = new ExplodingDice(lhs, right, dieParts[1], true); return new ListDiceExpression(lst); - } else if (diceListPattern.matcher(expString).matches()) { + } else if(diceListPattern.matcher(expString).matches()) { /* Parse simple die lists. */ final String[] dieParts = expString.split("dl"); @@ -177,8 +173,9 @@ public class DiceBox { /* * @TODO 10/08/17 Ben Culkin :RegexResource * - * These regexes and patterns should be moved to something external, probably - * using the SimpleProperties-based system that BJC-Utils2 uses. + * These regexes and patterns should be moved to something external, + * probably using the SimpleProperties-based system that BJC-Utils2 + * uses. */ /* Defines a comparison predicate. */ private static final String comparePoint = "[<>=]\\d+"; @@ -202,7 +199,8 @@ public class DiceBox { /* * Defines a fudge die. * - * This is like a simple die, but all the die give -1, 0, or 1 as results. + * This is like a simple die, but all the die give -1, 0, or 1 as + * results. */ private static final String fudgeDie = "(?:\\d+)?dF"; private static final Pattern fudgeDiePattern = Pattern.compile("\\A" + fudgeDie + "\\Z"); @@ -226,7 +224,8 @@ public class DiceBox { /* * Defines a compounding die. * - * This is like an exploding die, but is a single die, not a group of them. + * This is like an exploding die, but is a single die, not a group of + * them. */ private static final String compoundingDie = compoundGroup + "!!" + comparePoint; private static final Pattern compoundingDiePattern = Pattern.compile("\\A" + compoundingDie + "\\Z"); @@ -243,7 +242,8 @@ public class DiceBox { /* * Defines a penetrating die. * - * This is like an exploding die, but the exploded result gets a -1 penalty. + * This is like an exploding die, but the exploded result gets a -1 + * penalty. */ private static final String penetratingDie = compoundGroup + "!" + comparePoint; private static final Pattern penetratingDiePattern = Pattern.compile("\\A" + penetratingDie + "\\Z"); @@ -260,29 +260,30 @@ public class DiceBox { * Check if a given string is a valid die expression. * * @param exp - * The string to check validity of. + * The string to check validity of. * * @return Whether or not the string is a valid command. */ public static boolean isValidExpression(final String exp) { /* - * @NOTE Should this matcher/matches expression be abstracted in some way? + * @NOTE Should this matcher/matches expression be abstracted in + * some way? */ - if (scalarDiePattern.matcher(exp).matches()) { + if(scalarDiePattern.matcher(exp).matches()) { return true; - } else if (simpleDiePattern.matcher(exp).matches()) { + } else if(simpleDiePattern.matcher(exp).matches()) { return true; - } else if (fudgeDiePattern.matcher(exp).matches()) { + } else if(fudgeDiePattern.matcher(exp).matches()) { return true; - } else if (compoundDiePattern.matcher(exp).matches()) { + } else if(compoundDiePattern.matcher(exp).matches()) { return true; - } else if (compoundingDiePattern.matcher(exp).matches()) { + } else if(compoundingDiePattern.matcher(exp).matches()) { return true; - } else if (explodingDiePattern.matcher(exp).matches()) { + } else if(explodingDiePattern.matcher(exp).matches()) { return true; - } else if (penetratingDiePattern.matcher(exp).matches()) { + } else if(penetratingDiePattern.matcher(exp).matches()) { return true; - } else if (diceListPattern.matcher(exp).matches()) { + } else if(diceListPattern.matcher(exp).matches()) { return true; } else { return false; @@ -294,9 +295,10 @@ public class DiceBox { final long num = Long.parseLong(patt.substring(1)); /* - * @NOTE Should this be extended in some way, to provide other operators? + * @NOTE Should this be extended in some way, to provide other + * operators? */ - switch (patt.charAt(0)) { + switch(patt.charAt(0)) { case '<': return (roll) -> (roll < num); case '=': diff --git a/dice/src/main/java/bjc/dicelang/dice/Die.java b/dice/src/main/java/bjc/dicelang/dice/Die.java index 80d9458..31a5903 100644 --- a/dice/src/main/java/bjc/dicelang/dice/Die.java +++ b/dice/src/main/java/bjc/dicelang/dice/Die.java @@ -1,39 +1,39 @@ -package bjc.dicelang.dice;
-
-/**
- * Represents one or more dice that produce a scalar result.
- *
- * @author Ben Culkin
- */
-public interface Die {
- /**
- * Can this die be optimized to a single number?
- *
- * @return Whether this die can be optimized or not.
- */
- boolean canOptimize();
-
- /**
- * Optimize this die to a single number.
- *
- * Calling optimize on dice that return false for canOptimize produces undefined
- * behavior
- *
- * @return The optimized form of this die
- */
- long optimize();
-
- /**
- * Roll this die.
- *
- * @return A possible roll of this die
- */
- long roll();
-
- /**
- * Roll only a single portion of this die.
- *
- * @return A possible roll of a single portion of this die.
- */
- long rollSingle();
-}
+package bjc.dicelang.dice; + +/** + * Represents one or more dice that produce a scalar result. + * + * @author Ben Culkin + */ +public interface Die { + /** + * Can this die be optimized to a single number? + * + * @return Whether this die can be optimized or not. + */ + boolean canOptimize(); + + /** + * Optimize this die to a single number. + * + * Calling optimize on dice that return false for canOptimize produces + * undefined behavior + * + * @return The optimized form of this die + */ + long optimize(); + + /** + * Roll this die. + * + * @return A possible roll of this die + */ + long roll(); + + /** + * Roll only a single portion of this die. + * + * @return A possible roll of a single portion of this die. + */ + long rollSingle(); +} diff --git a/dice/src/main/java/bjc/dicelang/dice/ExplodingDice.java b/dice/src/main/java/bjc/dicelang/dice/ExplodingDice.java index 2dad73a..6893eb6 100644 --- a/dice/src/main/java/bjc/dicelang/dice/ExplodingDice.java +++ b/dice/src/main/java/bjc/dicelang/dice/ExplodingDice.java @@ -1,154 +1,148 @@ -package bjc.dicelang.dice;
-
-import java.util.LinkedList;
-import java.util.List;
-import java.util.function.Predicate;
-
-/**
- * An exploding die.
- *
- * Represents a die list that keeps getting another added die as long as a
- * condition is met.
- *
- * @author Ben Culkin
- */
-public class ExplodingDice implements DieList {
- /* The source die to use. */
- private final Die source;
-
- /* The conditions for exploding. */
- private final Predicate<Long> explodeOn;
- private final String explodePattern;
- /* Whether or not to apply a -1 penalty to explosions. */
- private final boolean explodePenetrates;
-
- /**
- * Create a new exploding die.
- *
- * @param src
- * The source die for exploding.
- * @param explode
- * The condition to explode on.
- */
- public ExplodingDice(final Die src, final Predicate<Long> explode) {
- this(src, explode, null, false);
- }
-
- /**
- * Create a new exploding die that may penetrate.
- *
- * @param src
- * The source die for exploding.
- * @param explode
- * The condition to explode on.
- * @param penetrate
- * Whether or not for explosions to penetrate (-1 to exploded die).
- */
- public ExplodingDice(final Die src, final Predicate<Long> explode, final boolean penetrate) {
- this(src, explode, null, penetrate);
- }
-
- /**
- * Create a new exploding die that may penetrate.
- *
- * @param src
- * The source die for exploding.
- * @param explode
- * The condition to explode on.
- * @param penetrate
- * Whether or not for explosions to penetrate (-1 to exploded die).
- * @param patt
- * The string the condition came from, for printing.
- */
- public ExplodingDice(final Die src, final Predicate<Long> explode, final String patt, final boolean penetrate) {
- source = src;
- explodeOn = explode;
- explodePattern = patt;
- explodePenetrates = penetrate;
- }
-
- @Override
- public boolean canOptimize() {
- return false;
- }
-
- @Override
- public long[] optimize() {
- return new long[0];
- }
-
- @Override
- public long[] roll() {
- final long res = source.roll();
- long oldRes = res;
-
- final List<Long> resList = new LinkedList<>();
- resList.add(res);
-
- while (explodeOn.test(oldRes)) {
- oldRes = source.rollSingle();
-
- if (explodePenetrates) {
- oldRes -= 1;
- }
-
- resList.add(oldRes);
- }
-
- final long resArr[] = new long[resList.size()];
-
- int i = 0;
- for (long rll : resList) {
- resArr[i] = rll;
- i += 1;
- }
-
- return resArr;
- }
-
- @Override
- public String toString() {
- String penString = explodePenetrates ? "p" : "";
- String sourceString = source.toString();
-
- if (explodePattern == null) {
- return String.format("%s%s!<complex-pred>", sourceString, penString);
- }
-
- return String.format("%s%s!%s", sourceString, penString, explodePattern);
- }
-
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((explodePattern == null) ? 0 : explodePattern.hashCode());
- result = prime * result + (explodePenetrates ? 1231 : 1237);
- result = prime * result + ((source == null) ? 0 : source.hashCode());
- return result;
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- ExplodingDice other = (ExplodingDice) obj;
- if (explodePattern == null) {
- if (other.explodePattern != null)
- return false;
- } else if (!explodePattern.equals(other.explodePattern))
- return false;
- if (explodePenetrates != other.explodePenetrates)
- return false;
- if (source == null) {
- if (other.source != null)
- return false;
- } else if (!source.equals(other.source))
- return false;
- return true;
- }
-}
+package bjc.dicelang.dice; + +import java.util.LinkedList; +import java.util.List; +import java.util.function.Predicate; + +/** + * An exploding die. + * + * Represents a die list that keeps getting another added die as long as a + * condition is met. + * + * @author Ben Culkin + */ +public class ExplodingDice implements DieList { + /* The source die to use. */ + private final Die source; + + /* The conditions for exploding. */ + private final Predicate<Long> explodeOn; + private final String explodePattern; + /* Whether or not to apply a -1 penalty to explosions. */ + private final boolean explodePenetrates; + + /** + * Create a new exploding die. + * + * @param src + * The source die for exploding. + * @param explode + * The condition to explode on. + */ + public ExplodingDice(final Die src, final Predicate<Long> explode) { + this(src, explode, null, false); + } + + /** + * Create a new exploding die that may penetrate. + * + * @param src + * The source die for exploding. + * @param explode + * The condition to explode on. + * @param penetrate + * Whether or not for explosions to penetrate (-1 to exploded + * die). + */ + public ExplodingDice(final Die src, final Predicate<Long> explode, final boolean penetrate) { + this(src, explode, null, penetrate); + } + + /** + * Create a new exploding die that may penetrate. + * + * @param src + * The source die for exploding. + * @param explode + * The condition to explode on. + * @param penetrate + * Whether or not for explosions to penetrate (-1 to exploded + * die). + * @param patt + * The string the condition came from, for printing. + */ + public ExplodingDice(final Die src, final Predicate<Long> explode, final String patt, final boolean penetrate) { + source = src; + explodeOn = explode; + explodePattern = patt; + explodePenetrates = penetrate; + } + + @Override + public boolean canOptimize() { + return false; + } + + @Override + public long[] optimize() { + return new long[0]; + } + + @Override + public long[] roll() { + final long res = source.roll(); + long oldRes = res; + + final List<Long> resList = new LinkedList<>(); + resList.add(res); + + while(explodeOn.test(oldRes)) { + oldRes = source.rollSingle(); + + if(explodePenetrates) { + oldRes -= 1; + } + + resList.add(oldRes); + } + + final long resArr[] = new long[resList.size()]; + + int i = 0; + for(long rll : resList) { + resArr[i] = rll; + i += 1; + } + + return resArr; + } + + @Override + public String toString() { + String penString = explodePenetrates ? "p" : ""; + String sourceString = source.toString(); + + if(explodePattern == null) { + return String.format("%s%s!<complex-pred>", sourceString, penString); + } + + return String.format("%s%s!%s", sourceString, penString, explodePattern); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((explodePattern == null) ? 0 : explodePattern.hashCode()); + result = prime * result + (explodePenetrates ? 1231 : 1237); + result = prime * result + ((source == null) ? 0 : source.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if(this == obj) return true; + if(obj == null) return false; + if(getClass() != obj.getClass()) return false; + ExplodingDice other = (ExplodingDice) obj; + if(explodePattern == null) { + if(other.explodePattern != null) return false; + } else if(!explodePattern.equals(other.explodePattern)) return false; + if(explodePenetrates != other.explodePenetrates) return false; + if(source == null) { + if(other.source != null) return false; + } else if(!source.equals(other.source)) return false; + return true; + } +} diff --git a/dice/src/main/java/bjc/dicelang/dice/FudgeDie.java b/dice/src/main/java/bjc/dicelang/dice/FudgeDie.java index 9ebbee1..088ce2e 100644 --- a/dice/src/main/java/bjc/dicelang/dice/FudgeDie.java +++ b/dice/src/main/java/bjc/dicelang/dice/FudgeDie.java @@ -14,7 +14,7 @@ public class FudgeDie implements Die { * Create a new fudge die. * * @param nDice - * The number of dice to roll. + * The number of dice to roll. */ public FudgeDie(final long nDice) { numDice = new ScalarDie(nDice); @@ -24,7 +24,7 @@ public class FudgeDie implements Die { * Create a new fudge die. * * @param nDice - * The number of dice to roll. + * The number of dice to roll. */ public FudgeDie(final Die nDice) { numDice = nDice; @@ -46,7 +46,7 @@ public class FudgeDie implements Die { final long nDice = numDice.roll(); - for (int i = 0; i < nDice; i++) { + for(int i = 0; i < nDice; i++) { res += rollSingle(); } @@ -75,18 +75,13 @@ public class FudgeDie implements Die { @Override public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; + if(this == obj) return true; + if(obj == null) return false; + if(getClass() != obj.getClass()) return false; FudgeDie other = (FudgeDie) obj; - if (numDice == null) { - if (other.numDice != null) - return false; - } else if (!numDice.equals(other.numDice)) - return false; + if(numDice == null) { + if(other.numDice != null) return false; + } else if(!numDice.equals(other.numDice)) return false; return true; } } diff --git a/dice/src/main/java/bjc/dicelang/dice/ListDiceExpression.java b/dice/src/main/java/bjc/dicelang/dice/ListDiceExpression.java index f8ff588..33c1e51 100644 --- a/dice/src/main/java/bjc/dicelang/dice/ListDiceExpression.java +++ b/dice/src/main/java/bjc/dicelang/dice/ListDiceExpression.java @@ -11,7 +11,7 @@ public class ListDiceExpression implements DiceExpression { * Create a list die expression. * * @param lst - * The list value of this expression. + * The list value of this expression. */ public ListDiceExpression(final DieList lst) { list = lst; @@ -42,18 +42,13 @@ public class ListDiceExpression implements DiceExpression { @Override public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; + if(this == obj) return true; + if(obj == null) return false; + if(getClass() != obj.getClass()) return false; ListDiceExpression other = (ListDiceExpression) obj; - if (list == null) { - if (other.list != null) - return false; - } else if (!list.equals(other.list)) - return false; + if(list == null) { + if(other.list != null) return false; + } else if(!list.equals(other.list)) return false; return true; } } diff --git a/dice/src/main/java/bjc/dicelang/dice/MathDie.java b/dice/src/main/java/bjc/dicelang/dice/MathDie.java index 348c2d3..e78dc3c 100644 --- a/dice/src/main/java/bjc/dicelang/dice/MathDie.java +++ b/dice/src/main/java/bjc/dicelang/dice/MathDie.java @@ -8,9 +8,11 @@ package bjc.dicelang.dice; */ public class MathDie implements Die { /* - * @TODO 10/08/17 Ben Culkin :MathGeneralize Why do we have the operator types - * hardcoded, instead of just having a general thing for applying a binary - * operator to dice? Fix this by changing it to the more general form. + * @TODO 10/08/17 Ben Culkin :MathGeneralize + * + * Why do we have the operator types hardcoded, instead of just having a + * general thing for applying a binary operator to dice? Fix this by + * changing it to the more general form. */ /** * The types of a math operator. @@ -28,7 +30,7 @@ public class MathDie implements Die { @Override public String toString() { - switch (this) { + switch(this) { case ADD: return "+"; @@ -53,13 +55,13 @@ public class MathDie implements Die { * Create a new math die. * * @param op - * The operator to apply. + * The operator to apply. * * @param lft - * The left operand. + * The left operand. * * @param rght - * The right operand. + * The right operand. */ public MathDie(final MathDie.MathOp op, final Die lft, final Die rght) { type = op; @@ -74,7 +76,7 @@ public class MathDie implements Die { } private long performOp(final long lft, final long rght) { - switch (type) { + switch(type) { case ADD: return lft + rght; @@ -130,25 +132,17 @@ public class MathDie implements Die { @Override public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; + if(this == obj) return true; + if(obj == null) return false; + if(getClass() != obj.getClass()) return false; MathDie other = (MathDie) obj; - if (left == null) { - if (other.left != null) - return false; - } else if (!left.equals(other.left)) - return false; - if (right == null) { - if (other.right != null) - return false; - } else if (!right.equals(other.right)) - return false; - if (type != other.type) - return false; + if(left == null) { + if(other.left != null) return false; + } else if(!left.equals(other.left)) return false; + if(right == null) { + if(other.right != null) return false; + } else if(!right.equals(other.right)) return false; + if(type != other.type) return false; return true; } } diff --git a/dice/src/main/java/bjc/dicelang/dice/ScalarDiceExpression.java b/dice/src/main/java/bjc/dicelang/dice/ScalarDiceExpression.java index 6951f49..5f57979 100644 --- a/dice/src/main/java/bjc/dicelang/dice/ScalarDiceExpression.java +++ b/dice/src/main/java/bjc/dicelang/dice/ScalarDiceExpression.java @@ -25,18 +25,13 @@ public class ScalarDiceExpression implements DiceExpression { @Override public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; + if(this == obj) return true; + if(obj == null) return false; + if(getClass() != obj.getClass()) return false; ScalarDiceExpression other = (ScalarDiceExpression) obj; - if (scalar == null) { - if (other.scalar != null) - return false; - } else if (!scalar.equals(other.scalar)) - return false; + if(scalar == null) { + if(other.scalar != null) return false; + } else if(!scalar.equals(other.scalar)) return false; return true; } diff --git a/dice/src/main/java/bjc/dicelang/dice/ScalarDie.java b/dice/src/main/java/bjc/dicelang/dice/ScalarDie.java index af6fd60..de15507 100644 --- a/dice/src/main/java/bjc/dicelang/dice/ScalarDie.java +++ b/dice/src/main/java/bjc/dicelang/dice/ScalarDie.java @@ -14,7 +14,7 @@ public class ScalarDie implements Die { * Create a new scalar die with a set value. * * @param vl - * The value to use. + * The value to use. */ public ScalarDie(final long vl) { val = vl; @@ -55,15 +55,11 @@ public class ScalarDie implements Die { @Override public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; + if(this == obj) return true; + if(obj == null) return false; + if(getClass() != obj.getClass()) return false; ScalarDie other = (ScalarDie) obj; - if (val != other.val) - return false; + if(val != other.val) return false; return true; } } diff --git a/dice/src/main/java/bjc/dicelang/dice/SimpleDie.java b/dice/src/main/java/bjc/dicelang/dice/SimpleDie.java index de31a6d..34ec059 100644 --- a/dice/src/main/java/bjc/dicelang/dice/SimpleDie.java +++ b/dice/src/main/java/bjc/dicelang/dice/SimpleDie.java @@ -14,8 +14,8 @@ public class SimpleDie implements Die { * * Rolled once per role, not once for each dice rolled. * - * @NOTE Would having some way to roll it once for each dice rolled be useful in - * any sort of case? + * @NOTE Would having some way to roll it once for each dice rolled be + * useful in any sort of case? */ private final Die diceSize; @@ -23,10 +23,10 @@ public class SimpleDie implements Die { * Create a new dice group. * * @param nDice - * The number of dice. + * The number of dice. * * @param size - * The size of the dice. + * The size of the dice. */ public SimpleDie(final long nDice, final long size) { this(new ScalarDie(nDice), new ScalarDie(size)); @@ -36,10 +36,10 @@ public class SimpleDie implements Die { * Create a new dice group. * * @param nDice - * The number of dice. + * The number of dice. * * @param size - * The size of the dice. + * The size of the dice. */ public SimpleDie(final Die nDice, final long size) { this(nDice, new ScalarDie(size)); @@ -49,10 +49,10 @@ public class SimpleDie implements Die { * Create a new dice group. * * @param nDice - * The number of dice. + * The number of dice. * * @param size - * The size of the dice. + * The size of the dice. */ public SimpleDie(final long nDice, final Die size) { this(new ScalarDie(nDice), size); @@ -62,10 +62,10 @@ public class SimpleDie implements Die { * Create a new dice group. * * @param nDice - * The number of dice. + * The number of dice. * * @param size - * The size of the dice. + * The size of the dice. */ public SimpleDie(final Die nDice, final Die size) { numDice = nDice; @@ -74,7 +74,7 @@ public class SimpleDie implements Die { @Override public boolean canOptimize() { - if (diceSize.canOptimize() && diceSize.optimize() <= 1) { + if(diceSize.canOptimize() && diceSize.optimize() <= 1) { return numDice.canOptimize(); } @@ -85,7 +85,7 @@ public class SimpleDie implements Die { public long optimize() { final long optSize = diceSize.optimize(); - if (optSize == 0) { + if(optSize == 0) { return 0; } @@ -99,7 +99,7 @@ public class SimpleDie implements Die { final long nDice = numDice.roll(); final long dSize = diceSize.roll(); - for (int i = 0; i < nDice; i++) { + for(int i = 0; i < nDice; i++) { total += Math.abs(DiceBox.rng.nextLong()) % dSize + 1; } @@ -127,23 +127,16 @@ public class SimpleDie implements Die { @Override public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; + if(this == obj) return true; + if(obj == null) return false; + if(getClass() != obj.getClass()) return false; SimpleDie other = (SimpleDie) obj; - if (diceSize == null) { - if (other.diceSize != null) - return false; - } else if (!diceSize.equals(other.diceSize)) - return false; - if (numDice == null) { - if (other.numDice != null) - return false; - } else if (!numDice.equals(other.numDice)) - return false; + if(diceSize == null) { + if(other.diceSize != null) return false; + } else if(!diceSize.equals(other.diceSize)) return false; + if(numDice == null) { + if(other.numDice != null) return false; + } else if(!numDice.equals(other.numDice)) return false; return true; } } diff --git a/dice/src/main/java/bjc/dicelang/dice/SimpleDieList.java b/dice/src/main/java/bjc/dicelang/dice/SimpleDieList.java index 4e8c51a..f3af08b 100644 --- a/dice/src/main/java/bjc/dicelang/dice/SimpleDieList.java +++ b/dice/src/main/java/bjc/dicelang/dice/SimpleDieList.java @@ -5,10 +5,13 @@ package bjc.dicelang.dice; * * @author EVE * + */ + +/* * @TODO 10/08/17 Ben Culkin :DieListGeneralize * - * DieList in general should be changed to be able to be constructed from - * an arbitrary die using rollSingle and things like that. + * DieList in general should be changed to be able to be constructed from an + * arbitrary die using rollSingle and things like that. */ public class SimpleDieList implements DieList { /* The number of dice to roll. */ @@ -24,10 +27,10 @@ public class SimpleDieList implements DieList { * Create a new list of dice. * * @param nDice - * The number of dice in the list. + * The number of dice in the list. * * @param sze - * The size of dice in the list. + * The size of dice in the list. */ public SimpleDieList(final Die nDice, final Die sze) { numDice = nDice; @@ -36,7 +39,7 @@ public class SimpleDieList implements DieList { @Override public boolean canOptimize() { - if (size.canOptimize() && size.optimize() <= 1) { + if(size.canOptimize() && size.optimize() <= 1) { return numDice.canOptimize(); } @@ -50,7 +53,7 @@ public class SimpleDieList implements DieList { final long[] ret = new long[sze]; - for (int i = 0; i < sze; i++) { + for(int i = 0; i < sze; i++) { ret[i] = res; } @@ -63,7 +66,7 @@ public class SimpleDieList implements DieList { final long[] ret = new long[num]; - for (int i = 0; i < num; i++) { + for(int i = 0; i < num; i++) { ret[i] = size.roll(); } @@ -86,23 +89,16 @@ public class SimpleDieList implements DieList { @Override public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; + if(this == obj) return true; + if(obj == null) return false; + if(getClass() != obj.getClass()) return false; SimpleDieList other = (SimpleDieList) obj; - if (numDice == null) { - if (other.numDice != null) - return false; - } else if (!numDice.equals(other.numDice)) - return false; - if (size == null) { - if (other.size != null) - return false; - } else if (!size.equals(other.size)) - return false; + if(numDice == null) { + if(other.numDice != null) return false; + } else if(!numDice.equals(other.numDice)) return false; + if(size == null) { + if(other.size != null) return false; + } else if(!size.equals(other.size)) return false; return true; } } diff --git a/dice/src/test/java/bjc/dicelang/AppTest.java b/dice/src/test/java/bjc/dicelang/AppTest.java index 6ffe98a..210aec7 100644 --- a/dice/src/test/java/bjc/dicelang/AppTest.java +++ b/dice/src/test/java/bjc/dicelang/AppTest.java @@ -7,32 +7,28 @@ import junit.framework.TestSuite; /** * Unit test for simple App. */ -public class AppTest - extends TestCase -{ - /** - * Create the test case - * - * @param testName name of the test case - */ - public AppTest( String testName ) - { - super( testName ); - } +public class AppTest extends TestCase { + /** + * Create the test case + * + * @param testName + * name of the test case + */ + public AppTest(String testName) { + super(testName); + } - /** - * @return the suite of tests being tested - */ - public static Test suite() - { - return new TestSuite( AppTest.class ); - } + /** + * @return the suite of tests being tested + */ + public static Test suite() { + return new TestSuite(AppTest.class); + } - /** - * Rigourous Test :-) - */ - public void testApp() - { - assertTrue( true ); - } + /** + * Rigourous Test :-) + */ + public void testApp() { + assertTrue(true); + } } diff --git a/scl/src/main/java/bjc/dicelang/scl/Errors.java b/scl/src/main/java/bjc/dicelang/scl/Errors.java index d372850..8d3faff 100644 --- a/scl/src/main/java/bjc/dicelang/scl/Errors.java +++ b/scl/src/main/java/bjc/dicelang/scl/Errors.java @@ -3,12 +3,15 @@ package bjc.dicelang.scl; /** * Repository for error messages. * + * + * @author EVE + */ +/* * @TODO 10/08/17 Ben Culkin :ErrorRefactor * - * This way of handling error messages is not easy to deal with. Something - * else needs to be done, but I'm not sure what at the moment. + * This way of handling error messages is not easy to deal with. Something else + * needs to be done, but I'm not sure what at the moment. * - * @author EVE * */ public class Errors { @@ -78,13 +81,13 @@ public class Errors { * Print an error. * * @param key - * The key of the error. + * The key of the error. * * @param args - * The arguments for the error. + * The arguments for the error. */ public void printError(final ErrorKey key, final String... args) { - switch (mode) { + switch(mode) { case WIZARD: System.out.println("\t? " + key.ordinal()); break; @@ -99,7 +102,7 @@ public class Errors { } private static void devError(final ErrorKey key, final String[] args) { - switch (key) { + switch(key) { case EK_STRM_NONEX: System.out.printf("\tERROR: Attempted to switch to non-existent stream\n"); break; @@ -111,7 +114,7 @@ public class Errors { case EK_STRM_INVCOM: System.out.printf("\tERROR: Unknown stream control command %s\n", args[0]); break; - + case EK_SCL_INVTOKEN: System.out.printf("\tERROR: Unknown SCL token %s\n", args[0]); break; diff --git a/scl/src/main/java/bjc/dicelang/scl/StreamControlConsole.java b/scl/src/main/java/bjc/dicelang/scl/StreamControlConsole.java index 26266ff..78c8c5e 100644 --- a/scl/src/main/java/bjc/dicelang/scl/StreamControlConsole.java +++ b/scl/src/main/java/bjc/dicelang/scl/StreamControlConsole.java @@ -21,7 +21,7 @@ public class StreamControlConsole { * Main method * * @param args - * Unused CLI args. + * Unused CLI args. */ public static void main(String[] args) { /* @@ -36,10 +36,10 @@ public class StreamControlConsole { System.out.print("Enter a SCL command string (blank to exit): "); /* Process it. */ - while (scn.hasNextLine()) { + while(scn.hasNextLine()) { String ln = scn.nextLine().trim(); - if (ln.equals("")) { + if(ln.equals("")) { /* Ignore empty lines. */ break; } @@ -50,7 +50,7 @@ public class StreamControlConsole { /* Run the stream engine on the tokens. */ boolean succ = sengine.doStreams(tokens, res); - if (!succ) { + if(!succ) { System.out.printf("ERROR: Stream engine failed for line '%s'\n", ln); continue; } @@ -58,7 +58,7 @@ public class StreamControlConsole { /* Run the command through SCL. */ tokens = res.toArray(new String[res.getSize()]); succ = sclengine.runProgram(tokens); - if (!succ) { + if(!succ) { System.out.printf("ERROR: SCL engine failed for line '%s'\n", ln); continue; } diff --git a/scl/src/main/java/bjc/dicelang/scl/StreamControlEngine.java b/scl/src/main/java/bjc/dicelang/scl/StreamControlEngine.java index fe4fcda..a590bb3 100644 --- a/scl/src/main/java/bjc/dicelang/scl/StreamControlEngine.java +++ b/scl/src/main/java/bjc/dicelang/scl/StreamControlEngine.java @@ -48,7 +48,7 @@ public class StreamControlEngine { * Create a new stream control engine. * * @param engine - * The engine to control. + * The engine to control. */ public StreamControlEngine(final StreamEngine engine) { eng = engine; @@ -61,35 +61,35 @@ public class StreamControlEngine { * Run a SCL program. * * @param tokens - * The program to run. + * The program to run. * * @return Whether the program executed successfully. */ public boolean runProgram(final String[] tokens) { - for (int i = 0; i < tokens.length; i++) { + for(int i = 0; i < tokens.length; i++) { /* Tokenize each token. */ final String token = tokens[i]; final SCLToken tok = SCLToken.tokenizeString(token); - if (tok == null) { + if(tok == null) { System.out.printf("ERROR: Tokenization failed for '%s'\n", token); return false; } /* Handle token types. */ - switch (tok.type) { + switch(tok.type) { case SQUOTE: /* Handle single-quotes. */ i = handleSingleQuote(i, tokens); - if (i == -1) { + if(i == -1) { return false; } break; - + case OBRACKET: /* Handle delimited brackets. */ i = handleDelim(i, tokens, "]"); - if (i == -1) { + if(i == -1) { return false; } break; @@ -97,7 +97,7 @@ public class StreamControlEngine { case OBRACE: /* Handle delimited braces. */ i = handleDelim(i, tokens, "}"); - if (i == -1) { + if(i == -1) { return false; } final SCLToken brak = curStack.pop(); @@ -106,7 +106,7 @@ public class StreamControlEngine { case WORD: /* Handle words. */ - if (!handleWord((WordSCLToken) tok)) { + if(!handleWord((WordSCLToken) tok)) { System.out.printf("WARNING: Execution of word '%s' failed\n", tok); } break; @@ -126,51 +126,52 @@ public class StreamControlEngine { /* Handle each type of word. */ /* - * @NOTE This should probably use something other than a switch statement. + * @NOTE This should probably use something other than a switch + * statement. */ - switch (tk.wordVal) { + switch(tk.wordVal) { case NEWSTREAM: eng.newStream(); break; case LEFTSTREAM: succ = eng.leftStream(); - if (!succ) { + if(!succ) { return false; } break; case RIGHTSTREAM: succ = eng.rightStream(); - if (!succ) { + if(!succ) { return false; } break; case DELETESTREAM: succ = eng.deleteStream(); - if (!succ) { + if(!succ) { return false; } break; case MERGESTREAM: succ = eng.mergeStream(); - if (!succ) { + if(!succ) { return false; } break; case MAKEARRAY: succ = makeArray(); - if (!succ) { + if(!succ) { return false; } break; case MAKEEXEC: succ = toggleExec(true); - if (!succ) { + if(!succ) { return false; } break; case MAKEUNEXEC: succ = toggleExec(false); - if (!succ) { + if(!succ) { return false; } break; @@ -181,7 +182,7 @@ public class StreamControlEngine { curStack.push(new BooleanSCLToken(curStack.empty())); break; case DROP: - if (curStack.size() == 0) { + if(curStack.size() == 0) { Errors.inst.printError(EK_SCL_SUNDERFLOW, tk.toString()); return false; } @@ -189,12 +190,12 @@ public class StreamControlEngine { break; case NDROP: succ = handleNDrop(); - if (!succ) { + if(!succ) { return false; } break; case NIP: - if (curStack.size() < 2) { + if(curStack.size() < 2) { Errors.inst.printError(EK_SCL_SUNDERFLOW, tk.toString()); return false; } @@ -202,7 +203,7 @@ public class StreamControlEngine { break; case NNIP: succ = handleNNip(); - if (!succ) { + if(!succ) { return false; } break; @@ -218,14 +219,14 @@ public class StreamControlEngine { private boolean handleNNip() { final SCLToken num = curStack.pop(); - if (num.type != ILIT) { + if(num.type != ILIT) { Errors.inst.printError(EK_SCL_INVARG, num.type.toString()); return false; } final int n = (int) ((IntSCLToken) num).intVal; - if (curStack.size() < n) { + if(curStack.size() < n) { Errors.inst.printError(EK_SCL_SUNDERFLOW, NNIP.toString()); return false; } @@ -238,14 +239,14 @@ public class StreamControlEngine { private boolean handleNDrop() { final SCLToken num = curStack.pop(); - if (num.type != ILIT) { + if(num.type != ILIT) { Errors.inst.printError(EK_SCL_INVARG, num.type.toString()); return false; } final int n = (int) ((IntSCLToken) num).intVal; - if (curStack.size() < n) { + if(curStack.size() < n) { Errors.inst.printError(EK_SCL_SUNDERFLOW, NDROP.toString()); return false; } @@ -258,15 +259,15 @@ public class StreamControlEngine { private boolean toggleExec(final boolean exec) { final SCLToken top = curStack.top(); - if (exec) { - if (top.type != ARRAY) { + if(exec) { + if(top.type != ARRAY) { Errors.inst.printError(EK_SCL_INVARG, top.toString()); return false; } top.type = WORDS; } else { - if (top.type != WORDS) { + if(top.type != WORDS) { Errors.inst.printError(EK_SCL_INVARG, top.toString()); return false; } @@ -281,13 +282,13 @@ public class StreamControlEngine { private boolean makeArray() { final SCLToken num = curStack.pop(); - if (num.type != ILIT) { + if(num.type != ILIT) { Errors.inst.printError(EK_SCL_INVARG, num.type.toString()); } final IList<SCLToken> arr = new FunctionalList<>(); - for (int i = 0; i < ((IntSCLToken) num).intVal; i++) { + for(int i = 0; i < ((IntSCLToken) num).intVal; i++) { arr.add(curStack.pop()); } @@ -302,34 +303,34 @@ public class StreamControlEngine { int n = i + 1; - if (n >= tokens.length) { + if(n >= tokens.length) { Errors.inst.printError(EK_SCL_MMQUOTE); return -1; } String tok = tokens[n]; - while (!tok.equals(delim)) { + while(!tok.equals(delim)) { final SCLToken ntok = SCLToken.tokenizeString(tok); - switch (ntok.type) { + switch(ntok.type) { case SQUOTE: n = handleSingleQuote(n, tokens); - if (n == -1) { + if(n == -1) { return -1; } toks.add(curStack.pop()); break; case OBRACKET: n = handleDelim(n, tokens, "]"); - if (n == -1) { + if(n == -1) { return -1; } toks.add(curStack.pop()); break; case OBRACE: n = handleDelim(i, tokens, "}"); - if (n == -1) { + if(n == -1) { return -1; } final SCLToken brak = curStack.pop(); @@ -342,7 +343,7 @@ public class StreamControlEngine { /* Move to the next token */ n += 1; - if (n >= tokens.length) { + if(n >= tokens.length) { Errors.inst.printError(EK_SCL_MMQUOTE); return -1; } @@ -367,15 +368,15 @@ public class StreamControlEngine { int n = i + 1; - if (n >= tokens.length) { + if(n >= tokens.length) { Errors.inst.printError(EK_SCL_MMQUOTE); return -1; } String tok = tokens[n]; - while (!tok.equals("'")) { - if (tok.matches("\\\\+'")) { + while(!tok.equals("'")) { + if(tok.matches("\\\\+'")) { /* Handle escaped quotes. */ sb.append(tok.substring(1)); } else { @@ -385,7 +386,7 @@ public class StreamControlEngine { /* Move to the next token */ n += 1; - if (n >= tokens.length) { + if(n >= tokens.length) { Errors.inst.printError(EK_SCL_MMQUOTE); return -1; } diff --git a/scl/src/main/java/bjc/dicelang/scl/StreamEngine.java b/scl/src/main/java/bjc/dicelang/scl/StreamEngine.java index e22cae1..7d7c39e 100644 --- a/scl/src/main/java/bjc/dicelang/scl/StreamEngine.java +++ b/scl/src/main/java/bjc/dicelang/scl/StreamEngine.java @@ -4,10 +4,14 @@ import bjc.utils.esodata.SingleTape; import bjc.utils.esodata.Tape; import bjc.utils.esodata.TapeLibrary; import bjc.utils.funcdata.FunctionalList; +import bjc.utils.funcdata.FunctionalMap; import bjc.utils.funcdata.IList; +import bjc.utils.funcdata.IMap; import bjc.utils.funcutils.ListUtils; import java.util.Arrays; +import java.util.function.BooleanSupplier; +import java.util.function.Predicate; import static bjc.dicelang.scl.Errors.ErrorKey.*; @@ -21,7 +25,9 @@ import static bjc.dicelang.scl.Errors.ErrorKey.*; * @author Ben Culkin */ public class StreamEngine { - /* Whether or not we're doing debugging. */ + /** + * Whether or not we're doing debugging. + */ public final boolean debug = true; /* Our streams. */ @@ -34,6 +40,29 @@ public class StreamEngine { /* Handler for SCL programs */ private final StreamControlEngine scleng; + private static IMap<Character, Predicate<StreamEngine>> commands; + + static { + commands = new FunctionalMap<>(); + + commands.put('+', (eng) -> { + eng.newStream(); + return true; + }); + + commands.put('>', (eng) -> eng.rightStream()); + commands.put('<', (eng) -> eng.leftStream()); + commands.put('-', (eng) -> eng.deleteStream()); + commands.put('M', (eng) -> eng.mergeStream()); + commands.put('L', (eng) -> { + String[] arr = eng.currStream.toArray(new String[0]); + + boolean succ = eng.scleng.runProgram(arr); + + return succ; + }); + } + /** * Create a new stream engine. * @@ -57,10 +86,10 @@ public class StreamEngine { * Process a possibly interleaved set of streams. * * @param toks - * The raw token to read streams from. + * The raw token to read streams from. * * @param dest - * The list to write the final stream to. + * The list to write the final stream to. * * @return Whether or not the streams were successfully processed. */ @@ -72,10 +101,10 @@ public class StreamEngine { * Process a possibly interleaved set of streams. * * @param toks - * The raw token to read streams from. + * The raw token to read streams from. * * @param dest - * The list to write the final stream to. + * The list to write the final stream to. * * @return Whether or not the streams were successfully processed. */ @@ -87,20 +116,20 @@ public class StreamEngine { boolean quoteMode = false; /* Process each token. */ - for (final String tk : toks) { + for(final String tk : toks) { /* Process stream commands. */ - if (tk.startsWith("{@S") && !quoteMode) { - if (tk.equals("{@SQ}")) { + if(tk.startsWith("{@S") && !quoteMode) { + if(tk.equals("{@SQ}")) { /* Start quoting. */ quoteMode = true; - } else if (!processCommand(tk)) { + } else if(!processCommand(tk)) { return false; } } else { - if (tk.equals("{@SU}")) { + if(tk.equals("{@SU}")) { /* Stop quoting. */ quoteMode = false; - } else if (tk.startsWith("\\") && tk.endsWith("{@SU}")) { + } else if(tk.startsWith("\\") && tk.endsWith("{@SU}")) { /* Unquote quoted end. */ currStream.add(tk.substring(1)); } else { @@ -109,7 +138,7 @@ public class StreamEngine { } } - for (final String tk : currStream) { + for(final String tk : currStream) { /* Collect tokens from the current stream. */ dest.add(tk); } @@ -128,7 +157,7 @@ public class StreamEngine { * @return Whether or not the move was successful. */ public boolean rightStream() { - if (!streams.right()) { + if(!streams.right()) { Errors.inst.printError(EK_STRM_NONEX); return false; } @@ -143,7 +172,7 @@ public class StreamEngine { * @return Whether or not the move was successful. */ public boolean leftStream() { - if (!streams.left()) { + if(!streams.left()) { Errors.inst.printError(EK_STRM_NONEX); return false; } @@ -158,7 +187,7 @@ public class StreamEngine { * @return Whether or not the delete succeeded. */ public boolean deleteStream() { - if (streams.size() == 1) { + if(streams.size() == 1) { Errors.inst.printError(EK_STRM_LAST); return false; } @@ -175,7 +204,7 @@ public class StreamEngine { * @return Whether or not the merge succeded. */ public boolean mergeStream() { - if (streams.size() == 1) { + if(streams.size() == 1) { Errors.inst.printError(EK_STRM_LAST); return false; } @@ -190,7 +219,7 @@ public class StreamEngine { private boolean processCommand(final String tk) { char[] comms = null; - if (tk.length() > 5) { + if(tk.length() > 5) { /* Pull off {@S and closing } */ comms = tk.substring(3, tk.length() - 1).toCharArray(); } else { @@ -199,54 +228,16 @@ public class StreamEngine { comms[0] = tk.charAt(3); } - boolean succ; - /* Process each command. */ - /* - * @TODO 10/09/17 Ben Culkin :StreamCommands This should probably be refactored - * in some way, so as to make it easier to add new commands. - */ - for (final char comm : comms) { - switch (comm) { - case '+': - newStream(); - break; - case '>': - succ = rightStream(); - if (!succ) { - return false; - } - break; - case '<': - succ = leftStream(); - if (!succ) { - return false; - } - break; - case '-': - succ = deleteStream(); - if (!succ) { - return false; - } - break; - case 'M': - succ = mergeStream(); - if (!succ) { - return false; - } - break; - case 'L': - succ = scleng.runProgram(currStream.toArray(new String[0])); - if (!succ) { - return false; - } - break; - default: + for(final char comm : comms) { + boolean succ = commands.getOrDefault(comm, (eng) -> { Errors.inst.printError(EK_STRM_INVCOM, tk); return false; - } + }).test(this); + + if(!succ) return false; } return true; } -} +}
\ No newline at end of file diff --git a/scl/src/main/java/bjc/dicelang/scl/tokens/BooleanSCLToken.java b/scl/src/main/java/bjc/dicelang/scl/tokens/BooleanSCLToken.java index bb6d1a2..c62e4a6 100644 --- a/scl/src/main/java/bjc/dicelang/scl/tokens/BooleanSCLToken.java +++ b/scl/src/main/java/bjc/dicelang/scl/tokens/BooleanSCLToken.java @@ -20,15 +20,11 @@ public class BooleanSCLToken extends SCLToken { @Override public boolean equals(Object obj) { - if (this == obj) - return true; - if (!super.equals(obj)) - return false; - if (getClass() != obj.getClass()) - return false; + if(this == obj) return true; + if(!super.equals(obj)) return false; + if(getClass() != obj.getClass()) return false; BooleanSCLToken other = (BooleanSCLToken) obj; - if (boolVal != other.boolVal) - return false; + if(boolVal != other.boolVal) return false; return true; } diff --git a/scl/src/main/java/bjc/dicelang/scl/tokens/FloatSCLToken.java b/scl/src/main/java/bjc/dicelang/scl/tokens/FloatSCLToken.java index 835e881..6c07c4d 100644 --- a/scl/src/main/java/bjc/dicelang/scl/tokens/FloatSCLToken.java +++ b/scl/src/main/java/bjc/dicelang/scl/tokens/FloatSCLToken.java @@ -22,15 +22,11 @@ public class FloatSCLToken extends SCLToken { @Override public boolean equals(Object obj) { - if (this == obj) - return true; - if (!super.equals(obj)) - return false; - if (getClass() != obj.getClass()) - return false; + if(this == obj) return true; + if(!super.equals(obj)) return false; + if(getClass() != obj.getClass()) return false; FloatSCLToken other = (FloatSCLToken) obj; - if (Double.doubleToLongBits(floatVal) != Double.doubleToLongBits(other.floatVal)) - return false; + if(Double.doubleToLongBits(floatVal) != Double.doubleToLongBits(other.floatVal)) return false; return true; } diff --git a/scl/src/main/java/bjc/dicelang/scl/tokens/SCLToken.java b/scl/src/main/java/bjc/dicelang/scl/tokens/SCLToken.java index c0158d0..d729705 100644 --- a/scl/src/main/java/bjc/dicelang/scl/tokens/SCLToken.java +++ b/scl/src/main/java/bjc/dicelang/scl/tokens/SCLToken.java @@ -22,19 +22,19 @@ public class SCLToken { public SCLToken.Type type; public static SCLToken tokenizeString(final String token) { - if (litTokens.containsKey(token)) { + if(litTokens.containsKey(token)) { return new IntSCLToken(litTokens.get(token)); - } else if (token.startsWith("\\")) { + } else if(token.startsWith("\\")) { return new SymbolSCLToken(token.substring(1)); - } else if (WordSCLToken.isBuiltinWord(token)) { + } else if(WordSCLToken.isBuiltinWord(token)) { return new WordSCLToken(token); - } else if (token.equals("true")) { + } else if(token.equals("true")) { return new BooleanSCLToken(true); - } else if (token.equals("false")) { + } else if(token.equals("false")) { return new BooleanSCLToken(false); - } else if (TokenUtils.isInt(token)) { + } else if(TokenUtils.isInt(token)) { return new IntSCLToken(Long.parseLong(token)); - } else if (TokenUtils.isDouble(token)) { + } else if(TokenUtils.isDouble(token)) { return new FloatSCLToken(Double.parseDouble(token)); } else { Errors.inst.printError(EK_SCL_INVTOKEN, token); @@ -72,15 +72,11 @@ public class SCLToken { @Override public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; + if(this == obj) return true; + if(obj == null) return false; + if(getClass() != obj.getClass()) return false; SCLToken other = (SCLToken) obj; - if (type != other.type) - return false; + if(type != other.type) return false; return true; } diff --git a/scl/src/main/java/bjc/dicelang/scl/tokens/StringSCLToken.java b/scl/src/main/java/bjc/dicelang/scl/tokens/StringSCLToken.java index fcd339c..d4ed8ec 100644 --- a/scl/src/main/java/bjc/dicelang/scl/tokens/StringSCLToken.java +++ b/scl/src/main/java/bjc/dicelang/scl/tokens/StringSCLToken.java @@ -5,7 +5,7 @@ public class StringSCLToken extends SCLToken { public String stringVal; protected StringSCLToken(boolean isSymbol, String val) { - if (isSymbol) { + if(isSymbol) { type = Type.SYMBOL; } else { type = Type.SLIT; @@ -24,18 +24,13 @@ public class StringSCLToken extends SCLToken { @Override public boolean equals(Object obj) { - if (this == obj) - return true; - if (!super.equals(obj)) - return false; - if (getClass() != obj.getClass()) - return false; + if(this == obj) return true; + if(!super.equals(obj)) return false; + if(getClass() != obj.getClass()) return false; StringSCLToken other = (StringSCLToken) obj; - if (stringVal == null) { - if (other.stringVal != null) - return false; - } else if (!stringVal.equals(other.stringVal)) - return false; + if(stringVal == null) { + if(other.stringVal != null) return false; + } else if(!stringVal.equals(other.stringVal)) return false; return true; } diff --git a/scl/src/main/java/bjc/dicelang/scl/tokens/WordListSCLToken.java b/scl/src/main/java/bjc/dicelang/scl/tokens/WordListSCLToken.java index f6ae63b..8463b66 100644 --- a/scl/src/main/java/bjc/dicelang/scl/tokens/WordListSCLToken.java +++ b/scl/src/main/java/bjc/dicelang/scl/tokens/WordListSCLToken.java @@ -7,7 +7,7 @@ public class WordListSCLToken extends SCLToken { public IList<SCLToken> tokenVals; protected WordListSCLToken(boolean isArray, IList<SCLToken> tokens) { - if (isArray) { + if(isArray) { type = Type.ARRAY; } else { type = Type.WORDS; @@ -26,18 +26,13 @@ public class WordListSCLToken extends SCLToken { @Override public boolean equals(Object obj) { - if (this == obj) - return true; - if (!super.equals(obj)) - return false; - if (getClass() != obj.getClass()) - return false; + if(this == obj) return true; + if(!super.equals(obj)) return false; + if(getClass() != obj.getClass()) return false; WordListSCLToken other = (WordListSCLToken) obj; - if (tokenVals == null) { - if (other.tokenVals != null) - return false; - } else if (!tokenVals.equals(other.tokenVals)) - return false; + if(tokenVals == null) { + if(other.tokenVals != null) return false; + } else if(!tokenVals.equals(other.tokenVals)) return false; return true; } diff --git a/scl/src/main/java/bjc/dicelang/scl/tokens/WordSCLToken.java b/scl/src/main/java/bjc/dicelang/scl/tokens/WordSCLToken.java index dcbef72..5c3c8cb 100644 --- a/scl/src/main/java/bjc/dicelang/scl/tokens/WordSCLToken.java +++ b/scl/src/main/java/bjc/dicelang/scl/tokens/WordSCLToken.java @@ -42,15 +42,11 @@ public class WordSCLToken extends SCLToken { @Override public boolean equals(Object obj) { - if (this == obj) - return true; - if (!super.equals(obj)) - return false; - if (getClass() != obj.getClass()) - return false; + if(this == obj) return true; + if(!super.equals(obj)) return false; + if(getClass() != obj.getClass()) return false; WordSCLToken other = (WordSCLToken) obj; - if (wordVal != other.wordVal) - return false; + if(wordVal != other.wordVal) return false; return true; } diff --git a/scl/src/test/java/bjc/AppTest.java b/scl/src/test/java/bjc/AppTest.java index 8d8ca91..21becec 100644 --- a/scl/src/test/java/bjc/AppTest.java +++ b/scl/src/test/java/bjc/AppTest.java @@ -7,32 +7,28 @@ import junit.framework.TestSuite; /** * Unit test for simple App. */ -public class AppTest - extends TestCase -{ - /** - * Create the test case - * - * @param testName name of the test case - */ - public AppTest( String testName ) - { - super( testName ); - } +public class AppTest extends TestCase { + /** + * Create the test case + * + * @param testName + * name of the test case + */ + public AppTest(String testName) { + super(testName); + } - /** - * @return the suite of tests being tested - */ - public static Test suite() - { - return new TestSuite( AppTest.class ); - } + /** + * @return the suite of tests being tested + */ + public static Test suite() { + return new TestSuite(AppTest.class); + } - /** - * Rigourous Test :-) - */ - public void testApp() - { - assertTrue( true ); - } + /** + * Rigourous Test :-) + */ + public void testApp() { + assertTrue(true); + } } |
