diff options
| author | student <student@localhost> | 2018-03-05 16:48:38 -0500 |
|---|---|---|
| committer | student <student@localhost> | 2018-03-05 16:48:38 -0500 |
| commit | 806cba069576e48477d4660a019a49c405cb0195 (patch) | |
| tree | c1e718b18e660c02a12b5a466a222daf8a435bf4 /base/src/bjc | |
| parent | f9d9bd4bbf7dd6a297e1daf5ee7b4263d706d9cd (diff) | |
Update
Diffstat (limited to 'base/src/bjc')
| -rw-r--r-- | base/src/bjc/dicelang/DiceLangEngine.java | 182 | ||||
| -rw-r--r-- | base/src/bjc/dicelang/Errors.java | 4 | ||||
| -rw-r--r-- | base/src/bjc/dicelang/Node.java | 1 | ||||
| -rw-r--r-- | base/src/bjc/dicelang/cli/DiceLangConsole.java | 3 | ||||
| -rw-r--r-- | base/src/bjc/dicelang/eval/DiceEvaluatorResult.java | 29 | ||||
| -rw-r--r-- | base/src/bjc/dicelang/eval/Evaluator.java | 176 | ||||
| -rw-r--r-- | base/src/bjc/dicelang/eval/FailureEvaluatorResult.java | 46 | ||||
| -rw-r--r-- | base/src/bjc/dicelang/eval/FloatEvaluatorResult.java | 24 | ||||
| -rw-r--r-- | base/src/bjc/dicelang/eval/StringEvaluatorResult.java | 29 | ||||
| -rw-r--r-- | base/src/bjc/dicelang/expr/ExprREPL.java | 11 | ||||
| -rw-r--r-- | base/src/bjc/dicelang/expr/Ezpr.java | 41 | ||||
| -rw-r--r-- | base/src/bjc/dicelang/expr/Parser.java | 20 | ||||
| -rw-r--r-- | base/src/bjc/dicelang/tokens/DiceToken.java | 34 | ||||
| -rw-r--r-- | base/src/bjc/dicelang/tokens/FloatToken.java | 27 |
14 files changed, 383 insertions, 244 deletions
diff --git a/base/src/bjc/dicelang/DiceLangEngine.java b/base/src/bjc/dicelang/DiceLangEngine.java index 2aafcd5..2abf066 100644 --- a/base/src/bjc/dicelang/DiceLangEngine.java +++ b/base/src/bjc/dicelang/DiceLangEngine.java @@ -37,9 +37,8 @@ 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 */ @@ -48,7 +47,9 @@ public class DiceLangEngine { /* ID for generation. */ int nextLiteral; - /* Debug indicator. */ + /** + * Debug indicator. + */ public boolean debugMode; /* Should we do shunting? */ private boolean postfixMode; @@ -146,7 +147,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); @@ -158,7 +159,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); @@ -217,7 +218,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 */ @@ -226,19 +227,18 @@ 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; } @@ -246,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; } @@ -259,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); } @@ -274,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 { @@ -289,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); @@ -301,37 +301,35 @@ 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); @@ -339,8 +337,7 @@ 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 { @@ -348,7 +345,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); @@ -363,7 +360,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: @@ -384,7 +381,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(); } @@ -392,24 +389,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); @@ -419,23 +416,21 @@ 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; @@ -446,13 +441,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) -> { @@ -470,19 +465,18 @@ 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); @@ -491,21 +485,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); @@ -519,26 +513,25 @@ 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; @@ -546,18 +539,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); @@ -574,10 +567,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 + ")"); @@ -600,26 +593,24 @@ 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; @@ -632,36 +623,32 @@ 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); @@ -669,7 +656,7 @@ public class DiceLangEngine { } } - if(curBraceCount > 0) { + if (curBraceCount > 0) { /* There was an unclosed group. */ Errors.inst.printError(EK_ENG_NOCLOSING); return false; @@ -678,7 +665,14 @@ public class DiceLangEngine { return true; } - /* Get a string literal from the string literal table. */ + /** + * Get a string literal from the string literal table. + * + * @param key + * The key for the literal. + * @return The literal value. + * + */ public String getStringLiteral(final int key) { return stringLits.get(key); } @@ -687,8 +681,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 8d3c969..fe9df2a 100644 --- a/base/src/bjc/dicelang/Errors.java +++ b/base/src/bjc/dicelang/Errors.java @@ -246,7 +246,9 @@ public class Errors { EK_MISC_NOFILE, /* Dice errors. */ - /* Recieved the wrong sort of expression to a die. */ + /** + * Recieved the wrong sort of expression to a die. + */ EK_DICE_INVTYPE, } diff --git a/base/src/bjc/dicelang/Node.java b/base/src/bjc/dicelang/Node.java index a9e14db..50a5e59 100644 --- a/base/src/bjc/dicelang/Node.java +++ b/base/src/bjc/dicelang/Node.java @@ -16,6 +16,7 @@ import bjc.utils.data.ITree; * * @author Ben Culkin */ +@SuppressWarnings("javadoc") public class Node { public static enum Type { ROOT, TOKREF, UNARYOP, BINOP, GROUP, OGROUP, RESULT diff --git a/base/src/bjc/dicelang/cli/DiceLangConsole.java b/base/src/bjc/dicelang/cli/DiceLangConsole.java index c19793f..18b6f7f 100644 --- a/base/src/bjc/dicelang/cli/DiceLangConsole.java +++ b/base/src/bjc/dicelang/cli/DiceLangConsole.java @@ -3,15 +3,12 @@ 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; diff --git a/base/src/bjc/dicelang/eval/DiceEvaluatorResult.java b/base/src/bjc/dicelang/eval/DiceEvaluatorResult.java index 02d01ae..c0e9643 100644 --- a/base/src/bjc/dicelang/eval/DiceEvaluatorResult.java +++ b/base/src/bjc/dicelang/eval/DiceEvaluatorResult.java @@ -6,26 +6,55 @@ import bjc.dicelang.dice.DieList; import bjc.dicelang.dice.ListDiceExpression; import bjc.dicelang.dice.ScalarDiceExpression; +/** + * Represents a result containing a dice value. + * + * @author student + * + */ public class DiceEvaluatorResult extends EvaluatorResult { /** * The dice value of the result. */ public DiceExpression diceVal; + /** + * Create a new result from an expression. + * + * @param expr + * The expression to use. + */ public DiceEvaluatorResult(DiceExpression expr) { super(Type.DICE); diceVal = expr; } + /** + * Create a new result from a die. + * + * @param die + * The die to use. + */ public DiceEvaluatorResult(Die die) { this(new ScalarDiceExpression(die)); } + /** + * Create a new result from a die list. + * + * @param list + * The die list to use. + */ public DiceEvaluatorResult(DieList list) { this(new ListDiceExpression(list)); } + /** + * Check if the result is a list. + * + * @return If the result is a 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 4603e22..f754c59 100644 --- a/base/src/bjc/dicelang/eval/Evaluator.java +++ b/base/src/bjc/dicelang/eval/Evaluator.java @@ -63,7 +63,7 @@ public class Evaluator { * Create a new evaluator. * * @param en - * The engine. + * The engine. */ public Evaluator(final DiceLangEngine en) { eng = en; @@ -73,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. */ @@ -83,18 +83,16 @@ 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; } @@ -103,7 +101,10 @@ public class Evaluator { * @NOTE * * This is broken until stepwise top-down transforms are fixed. + * + * Make it public once we know it works again. */ + @SuppressWarnings("javadoc") public Iterator<ITree<Node>> stepDebug(final ITree<Node> comm) { final Context ctx = new Context(); @@ -118,9 +119,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; @@ -135,7 +136,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: @@ -155,23 +156,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); @@ -181,16 +182,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 { @@ -200,7 +201,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)); } @@ -209,23 +210,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) { + if (curLevel == CoerceSteps.DOUBLE) { nd.resultVal = new FloatEvaluatorResult((double) res.intVal); } default: @@ -240,7 +241,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()); } @@ -249,7 +250,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()); } @@ -266,9 +267,8 @@ 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)); } @@ -279,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: @@ -302,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)); } @@ -321,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)); } @@ -329,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; } @@ -345,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 * @@ -354,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()); @@ -386,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 { @@ -402,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 { @@ -428,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)); } @@ -473,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)); } @@ -496,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)); } @@ -519,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); @@ -549,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); @@ -583,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/FailureEvaluatorResult.java b/base/src/bjc/dicelang/eval/FailureEvaluatorResult.java index d3b2cbe..6e46a61 100644 --- a/base/src/bjc/dicelang/eval/FailureEvaluatorResult.java +++ b/base/src/bjc/dicelang/eval/FailureEvaluatorResult.java @@ -4,26 +4,53 @@ import bjc.dicelang.Node; import bjc.utils.data.ITree; import bjc.utils.data.Tree; +/** + * Represents an evaluation ending in failure. + * + * @author student + * + */ public class FailureEvaluatorResult extends EvaluatorResult { /** * Original node data */ public ITree<Node> origVal; + /** + * Create a new generic failure. + */ public FailureEvaluatorResult() { super(Type.FAILURE); } + /** + * Create a new failure result + * + * @param orig + * The tree that caused the failure. + */ public FailureEvaluatorResult(final ITree<Node> orig) { super(Type.FAILURE); origVal = orig; } + /** + * Create a new failure result + * + * @param orig + * The node that caused the failure. + */ public FailureEvaluatorResult(final Node orig) { this(new Tree<>(orig)); } - + + /** + * Create a new failure result + * + * @param right + * The result that caused the failure. + */ public FailureEvaluatorResult(EvaluatorResult right) { this(new Node(Node.Type.RESULT, right)); } @@ -38,13 +65,18 @@ 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 22f89f0..9a36502 100644 --- a/base/src/bjc/dicelang/eval/FloatEvaluatorResult.java +++ b/base/src/bjc/dicelang/eval/FloatEvaluatorResult.java @@ -1,11 +1,23 @@ package bjc.dicelang.eval; +/** + * Represents a floating-point result. + * + * @author student + * + */ public class FloatEvaluatorResult extends EvaluatorResult { /** * The float value of the result. */ public double floatVal; + /** + * Create a new floating-point result. + * + * @param val + * The value of the result. + */ public FloatEvaluatorResult(double val) { super(Type.FLOAT); @@ -29,11 +41,15 @@ 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 251b318..f7d79d6 100644 --- a/base/src/bjc/dicelang/eval/StringEvaluatorResult.java +++ b/base/src/bjc/dicelang/eval/StringEvaluatorResult.java @@ -1,11 +1,23 @@ package bjc.dicelang.eval; +/** + * String evaluation result. + * + * @author student + * + */ public class StringEvaluatorResult extends EvaluatorResult { /** * The string value of the result. */ public String stringVal; + /** + * Create a new string evaluation result. + * + * @param strang + * The value of the result. + */ public StringEvaluatorResult(String strang) { super(Type.STRING); @@ -27,13 +39,18 @@ 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 d763d67..6cc06e2 100644 --- a/base/src/bjc/dicelang/expr/ExprREPL.java +++ b/base/src/bjc/dicelang/expr/ExprREPL.java @@ -6,13 +6,12 @@ import bjc.utils.data.ITree; import bjc.utils.funcdata.IList; import bjc.utils.parserutils.TreeConstructor; +/** + * REPL for expressions. + * @author student + * + */ public class ExprREPL { - - /* - * @TODO 10/08/17 Ben Culkin :MainSeperation - * - * This main method should be moved to its own class. - */ /** * Main method. * diff --git a/base/src/bjc/dicelang/expr/Ezpr.java b/base/src/bjc/dicelang/expr/Ezpr.java index 2a54b6e..12a3413 100644 --- a/base/src/bjc/dicelang/expr/Ezpr.java +++ b/base/src/bjc/dicelang/expr/Ezpr.java @@ -11,6 +11,13 @@ import com.google.common.collect.Multiset; import static bjc.dicelang.expr.Ezpr.EzprNode.EzprNodeType.*; +/* + * @TODO 3/5/18 Ben Culkin :EzprFinished + * + * Finish the documentation and make sure this class works. + */ + +@SuppressWarnings("javadoc") public class Ezpr { public static enum EzprType { SUM, MUL @@ -42,7 +49,7 @@ public class Ezpr { @Override public String toString() { - if(typ == TOKEN) { + if (typ == TOKEN) { return tok.toString(); } return ezp.toString(); @@ -65,28 +72,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) { + if (typ == SUM) { /* * Add sum parts to corresponding bags. */ - 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); } } 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); } } @@ -95,25 +102,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); } } @@ -130,13 +137,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/Parser.java b/base/src/bjc/dicelang/expr/Parser.java index 020467a..db6d547 100644 --- a/base/src/bjc/dicelang/expr/Parser.java +++ b/base/src/bjc/dicelang/expr/Parser.java @@ -8,14 +8,18 @@ import bjc.utils.data.ITree; * @author Ben Culkin */ 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. + * + * @param ast + * The AST to canonicalize. + * @return The canonicalized AST. */ 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 +33,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/tokens/DiceToken.java b/base/src/bjc/dicelang/tokens/DiceToken.java index 4bf2068..2a0e75e 100644 --- a/base/src/bjc/dicelang/tokens/DiceToken.java +++ b/base/src/bjc/dicelang/tokens/DiceToken.java @@ -2,9 +2,24 @@ package bjc.dicelang.tokens; import bjc.dicelang.dice.DiceExpression; +/** + * A token that contains a dice value. + * + * @author student + * + */ public class DiceToken extends Token { + /** + * The value of th token. + */ public DiceExpression diceValue; + /** + * Create a new dice token. + * + * @param val + * The value of the token. + */ public DiceToken(DiceExpression val) { super(Type.DICE_LIT); @@ -26,13 +41,20 @@ 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/tokens/FloatToken.java b/base/src/bjc/dicelang/tokens/FloatToken.java index f56bef7..2b77a24 100644 --- a/base/src/bjc/dicelang/tokens/FloatToken.java +++ b/base/src/bjc/dicelang/tokens/FloatToken.java @@ -1,8 +1,23 @@ package bjc.dicelang.tokens; +/** + * Represents a floating point token. + * + * @author student + * + */ public class FloatToken extends Token { + /** + * The value of the token. + */ public double floatValue; + /** + * Create a new floating-point token. + * + * @param val + * The value of the token. + */ public FloatToken(double val) { super(Type.FLOAT_LIT); @@ -26,11 +41,15 @@ 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; } } |
