summaryrefslogtreecommitdiff
path: root/dice-lang/src/bjc/dicelang/DiceLangEngine.java
diff options
context:
space:
mode:
authorEVE <EVE@EVE-PC>2017-03-14 12:08:11 -0400
committerEVE <EVE@EVE-PC>2017-03-14 12:08:11 -0400
commit635d3150e3e85c01b777ff165e21fa8965d58440 (patch)
tree3389128f83a5a79f8d0eec0a0e19f54b9d117b66 /dice-lang/src/bjc/dicelang/DiceLangEngine.java
parente59e2a97773f93bdd25bd4680809c10699f0feb3 (diff)
Cleanup
Diffstat (limited to 'dice-lang/src/bjc/dicelang/DiceLangEngine.java')
-rw-r--r--dice-lang/src/bjc/dicelang/DiceLangEngine.java147
1 files changed, 65 insertions, 82 deletions
diff --git a/dice-lang/src/bjc/dicelang/DiceLangEngine.java b/dice-lang/src/bjc/dicelang/DiceLangEngine.java
index f65f5dd..d16e9f4 100644
--- a/dice-lang/src/bjc/dicelang/DiceLangEngine.java
+++ b/dice-lang/src/bjc/dicelang/DiceLangEngine.java
@@ -1,7 +1,6 @@
package bjc.dicelang;
import bjc.dicelang.scl.StreamEngine;
-
import bjc.utils.data.ITree;
import bjc.utils.funcdata.FunctionalList;
import bjc.utils.funcdata.FunctionalMap;
@@ -14,12 +13,13 @@ import bjc.utils.funcutils.StringUtils;
import java.util.Deque;
import java.util.Iterator;
-import java.util.List;
import java.util.LinkedList;
+import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-import static bjc.dicelang.Errors.ErrorKey.*;
+import static bjc.dicelang.Errors.ErrorKey.EK_ENG_NOCLOSING;
+import static bjc.dicelang.Errors.ErrorKey.EK_ENG_NOOPENING;
import static bjc.dicelang.Token.Type.*;
/**
@@ -83,14 +83,14 @@ public class DiceLangEngine {
*/
public final IMap<Integer, String> symTable;
- private IMap<Integer, String> stringLits;
- private IMap<String, String> stringLiterals;
+ private IMap<Integer, String> stringLits;
+ private IMap<String, String> stringLiterals;
/*
* Lists of defns.
*/
- private IList<Define> lineDefns;
- private IList<Define> tokenDefns;
+ private IList<Define> lineDefns;
+ private IList<Define> tokenDefns;
/*
* Are defns sorted by priority?
@@ -249,7 +249,7 @@ public class DiceLangEngine {
*
* @param command
* The command to run
- *
+ *
* @return Whether or not the command ran successfully
*/
public boolean runCommand(String command) {
@@ -258,18 +258,14 @@ public class DiceLangEngine {
*/
IList<String> preprocessedTokens = preprocessCommand(command);
- if (preprocessedTokens == null) {
- return false;
- }
+ if(preprocessedTokens == null) return false;
/*
* Lex the string tokens into token-tokens
*/
IList<Token> lexedTokens = lexTokens(preprocessedTokens);
- if (lexedTokens == null) {
- return false;
- }
+ if(lexedTokens == null) return false;
/*
* Parse the tokens into an AST forest
@@ -277,9 +273,7 @@ public class DiceLangEngine {
IList<ITree<Node>> astForest = new FunctionalList<>();
boolean succ = parsr.parseTokens(lexedTokens, astForest);
- if (!succ) {
- return false;
- }
+ if(!succ) return false;
/*
* Evaluate the AST forest
@@ -295,13 +289,13 @@ public class DiceLangEngine {
private IList<Token> lexTokens(IList<String> preprocessedTokens) {
IList<Token> lexedTokens = new FunctionalList<>();
- for (String token : preprocessedTokens) {
+ for(String token : preprocessedTokens) {
String newTok = token;
/*
* Apply token defns
*/
- for (Define dfn : tokenDefns.toIterable()) {
+ for(Define dfn : tokenDefns.toIterable()) {
newTok = dfn.apply(newTok);
}
@@ -310,22 +304,22 @@ public class DiceLangEngine {
*/
Token tk = tokenzer.lexToken(token, stringLiterals);
- 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 {
+ else {
lexedTokens.add(tk);
}
}
- if (debugMode) {
+ if(debugMode) {
System.out.printf("\tCommand after tokenization: %s\n", lexedTokens.toString());
}
@@ -337,25 +331,21 @@ public class DiceLangEngine {
boolean succ = removePreshuntTokens(lexedTokens, preparedTokens);
- if (!succ) {
- return null;
- }
+ if(!succ) return null;
- if (debugMode && !postfixMode) {
+ if(debugMode && !postfixMode) {
System.out.printf("\tCommand after pre-shunter removal: %s\n", preparedTokens.toString());
}
- if (!postfixMode && !prefixMode) {
+ if(!postfixMode && !prefixMode) {
/*
* Shunt the tokens
*/
shuntedTokens = new FunctionalList<>();
succ = shunt.shuntTokens(preparedTokens, shuntedTokens);
- if (!succ) {
- return null;
- }
- } else if (prefixMode) {
+ if(!succ) return null;
+ } else if(prefixMode) {
/*
* Reverse directional tokens
*/
@@ -363,7 +353,7 @@ public class DiceLangEngine {
shuntedTokens = preparedTokens.map(this::reverseToken);
}
- if (debugMode && !postfixMode) {
+ if(debugMode && !postfixMode) {
System.out.printf("\tCommand after shunting: %s\n", shuntedTokens.toString());
}
@@ -371,16 +361,15 @@ public class DiceLangEngine {
* Expand token groups
*/
IList<Token> readyTokens = shuntedTokens.flatMap(tk -> {
- if (tk.type == Token.Type.TOKGROUP) {
+ if(tk.type == Token.Type.TOKGROUP)
return tk.tokenValues;
- } else if (tk.type == Token.Type.TAGOP || tk.type == Token.Type.TAGOPR) {
+ else if(tk.type == Token.Type.TAGOP || tk.type == Token.Type.TAGOPR)
return tk.tokenValues;
- } else {
+ else
return new FunctionalList<>(tk);
- }
});
- if (debugMode && !postfixMode) {
+ if(debugMode && !postfixMode) {
System.out.printf("\tCommand after re-preshunting: %s\n", readyTokens.toString());
}
@@ -389,11 +378,11 @@ public class DiceLangEngine {
/*
* Reverse orientation-sensitive tokens.
- *
+ *
* These are mostly just things like (, {, and [
*/
private Token reverseToken(Token tk) {
- switch (tk.type) {
+ switch(tk.type) {
case OBRACE:
return new Token(CBRACE, tk.intValue);
case OPAREN:
@@ -418,7 +407,7 @@ public class DiceLangEngine {
/*
* Sort the defines if they aren't sorted
*/
- if (!defnsSorted) {
+ if(!defnsSorted) {
sortDefns();
}
@@ -428,24 +417,22 @@ public class DiceLangEngine {
IList<String> streamToks = new FunctionalList<>();
boolean succ = streamEng.doStreams(command.split(" "), streamToks);
- if (!succ) {
- return null;
- }
+ if(!succ) return null;
/*
* Apply line defns
*/
String newComm = ListUtils.collapseTokens(streamToks, " ");
- if (debugMode) {
+ if(debugMode) {
System.out.println("\tCommand after stream commands: " + newComm);
}
- for (Define dfn : lineDefns.toIterable()) {
+ for(Define dfn : lineDefns.toIterable()) {
newComm = dfn.apply(newComm);
}
- if (debugMode) {
+ if(debugMode) {
System.out.println("\tCommand after line defines: " + newComm);
}
@@ -455,11 +442,11 @@ public class DiceLangEngine {
List<String> destringedParts = StringUtils.removeDQuotedStrings(newComm);
StringBuffer destringedCommand = new StringBuffer();
- for (String part : destringedParts) {
+ for(String part : destringedParts) {
/*
* Handle string literals
*/
- if (part.startsWith("\"") && part.endsWith("\"")) {
+ if(part.startsWith("\"") && part.endsWith("\"")) {
/*
* Get the actual string.
*/
@@ -482,13 +469,13 @@ public class DiceLangEngine {
}
}
- if (debugMode) {
+ if(debugMode) {
System.out.println("\tCommand after destringing: " + destringedCommand);
/*
* Print the string table if it exists.
*/
- if (stringLiterals.getSize() > 0) {
+ if(stringLiterals.getSize() > 0) {
System.out.println("\tString literals in table");
stringLiterals.forEach((key, val) -> {
@@ -510,17 +497,16 @@ public class DiceLangEngine {
tokens = tokens.map(tk -> {
Matcher nonExpandMatcher = nonExpandPattern.matcher(tk);
- if (nonExpandMatcher.matches()) {
+ if(nonExpandMatcher.matches()) {
String tkName = "nonExpandToken" + nextLiteral++;
nonExpandedTokens.put(tkName, nonExpandMatcher.group(1));
return tkName;
- } else {
+ } else
return tk;
- }
});
- if (debugMode) {
+ if(debugMode) {
System.out.printf("\tCommand after removal of non-expanders: %s\n", tokens.toString());
}
@@ -535,14 +521,13 @@ public class DiceLangEngine {
* Reinsert non-expanded tokens
*/
fullyExpandedTokens = fullyExpandedTokens.map(tk -> {
- if (tk.startsWith("nonExpandToken")) {
+ if(tk.startsWith("nonExpandToken"))
return nonExpandedTokens.get(tk);
- } else {
+ else
return tk;
- }
});
- if (debugMode) {
+ if(debugMode) {
System.out.printf("\tCommand after non-expander reinsertion: %s\n",
fullyExpandedTokens.toString());
}
@@ -551,24 +536,24 @@ public class DiceLangEngine {
}
private void evaluateForest(IList<ITree<Node>> astForest) {
- if (debugMode) {
+ if(debugMode) {
System.out.println("\tParsed forest of asts");
}
int treeNo = 1;
- for (ITree<Node> ast : astForest) {
- if (debugMode) {
+ for(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) {
int step = 1;
/*
* Evaluate it step by step
*/
- for (Iterator<ITree<Node>> itr = eval.stepDebug(ast); itr.hasNext();) {
+ for(Iterator<ITree<Node>> itr = eval.stepDebug(ast); itr.hasNext();) {
ITree<Node> nodeStep = itr.next();
System.out.printf("\t\tStep %d: Node is %s", step, nodeStep);
@@ -576,7 +561,7 @@ public class DiceLangEngine {
/*
* Don't evaluate null steps
*/
- if (nodeStep == null) {
+ if(nodeStep == null) {
System.out.println();
step += 1;
@@ -586,16 +571,16 @@ public class DiceLangEngine {
/*
* Print out details for results
*/
- if (nodeStep.getHead().type == Node.Type.RESULT) {
+ if(nodeStep.getHead().type == Node.Type.RESULT) {
EvaluatorResult res = nodeStep.getHead().resultVal;
System.out.printf(" (result is %s", res);
- if (res.type == EvaluatorResult.Type.DICE) {
+ if(res.type == EvaluatorResult.Type.DICE) {
System.out.printf(" (sample roll %s)", res.diceVal.value());
}
- if (res.origVal != null) {
+ if(res.origVal != null) {
System.out.printf(" (original tree is %s)", res.origVal);
}
@@ -614,10 +599,10 @@ public class DiceLangEngine {
*/
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) {
System.out.println("\t\t (sample roll " + res.diceVal.value() + ")");
}
}
@@ -644,14 +629,14 @@ public class DiceLangEngine {
Deque<IList<Token>> bracedTokens = new LinkedList<>();
IList<Token> curBracedTokens = null;
- for (Token tk : lexedTokens) {
- if (tk.type == Token.Type.OBRACE && tk.intValue == 2) {
+ for(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.
@@ -660,11 +645,11 @@ public class DiceLangEngine {
}
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.
@@ -682,16 +667,14 @@ public class DiceLangEngine {
*/
boolean success = shunt.shuntTokens(curBracedTokens, preshuntTokens);
- if (debugMode) {
+ if(debugMode) {
System.out.println("\t\tPreshunted " + curBracedTokens + " into "
+ preshuntTokens);
}
- if (!success) {
- return false;
- }
+ if(!success) return false;
- if (curBraceCount >= 1) {
+ if(curBraceCount >= 1) {
/*
* Add the preshunt group to the
* previous group.
@@ -711,7 +694,7 @@ public class DiceLangEngine {
* 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);
@@ -719,7 +702,7 @@ public class DiceLangEngine {
}
}
- if (curBraceCount > 0) {
+ if(curBraceCount > 0) {
/*
* There was an unclosed group.
*/