summaryrefslogtreecommitdiff
path: root/dice-lang/src/bjc/dicelang/Shunter.java
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2017-04-11 21:48:50 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2017-04-11 21:48:50 -0400
commit57f9a3bfdad20bead5b35ee540e8790e80a6b9a4 (patch)
tree1e7184825eaa8d22077b505513df3e0d8502fb39 /dice-lang/src/bjc/dicelang/Shunter.java
parent675ae734dd7b7a47d93ee3527dd1eb7195be047b (diff)
Cleanup
Diffstat (limited to 'dice-lang/src/bjc/dicelang/Shunter.java')
-rw-r--r--dice-lang/src/bjc/dicelang/Shunter.java165
1 files changed, 94 insertions, 71 deletions
diff --git a/dice-lang/src/bjc/dicelang/Shunter.java b/dice-lang/src/bjc/dicelang/Shunter.java
index 5ac4614..ffe730b 100644
--- a/dice-lang/src/bjc/dicelang/Shunter.java
+++ b/dice-lang/src/bjc/dicelang/Shunter.java
@@ -1,17 +1,41 @@
package bjc.dicelang;
-import bjc.utils.funcdata.FunctionalList;
-import bjc.utils.funcdata.FunctionalMap;
-import bjc.utils.funcdata.IList;
-import bjc.utils.funcdata.IMap;
+import static bjc.dicelang.Errors.ErrorKey.EK_SHUNT_INVSEP;
+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.ADD;
+import static bjc.dicelang.Token.Type.BIND;
+import static bjc.dicelang.Token.Type.CBRACE;
+import static bjc.dicelang.Token.Type.COERCE;
+import static bjc.dicelang.Token.Type.CPAREN;
+import static bjc.dicelang.Token.Type.DICECONCAT;
+import static bjc.dicelang.Token.Type.DICEGROUP;
+import static bjc.dicelang.Token.Type.DICELIST;
+import static bjc.dicelang.Token.Type.DIVIDE;
+import static bjc.dicelang.Token.Type.GROUPSEP;
+import static bjc.dicelang.Token.Type.IDIVIDE;
+import static bjc.dicelang.Token.Type.LET;
+import static bjc.dicelang.Token.Type.MULTIPLY;
+import static bjc.dicelang.Token.Type.OBRACE;
+import static bjc.dicelang.Token.Type.OPAREN;
+import static bjc.dicelang.Token.Type.STRCAT;
+import static bjc.dicelang.Token.Type.STRREP;
+import static bjc.dicelang.Token.Type.SUBTRACT;
+import static bjc.dicelang.Token.Type.TAGOP;
+import static bjc.dicelang.Token.Type.TAGOPR;
+import static bjc.dicelang.Token.Type.TOKGROUP;
import java.util.Deque;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
-import static bjc.dicelang.Errors.ErrorKey.*;
-import static bjc.dicelang.Token.Type.*;
+import bjc.utils.funcdata.FunctionalList;
+import bjc.utils.funcdata.FunctionalMap;
+import bjc.utils.funcdata.IList;
+import bjc.utils.funcdata.IMap;
/**
* Shunt a set of infix tokens to postfix tokens.
@@ -110,61 +134,61 @@ public class Shunter {
*
* @return Whether or not the shunt succeeded.
*/
- public boolean shuntTokens(IList<Token> tks, IList<Token> returned) {
- Deque<Token> opStack = new LinkedList<>();
- Deque<Token> unaryOps = new LinkedList<>();
+ public boolean shuntTokens(final IList<Token> tks, final IList<Token> returned) {
+ final Deque<Token> opStack = new LinkedList<>();
+ final Deque<Token> unaryOps = new LinkedList<>();
- Deque<Token> currReturned = new LinkedList<>();
+ final Deque<Token> currReturned = new LinkedList<>();
- Deque<Token> feed = new LinkedList<>();
+ final Deque<Token> feed = new LinkedList<>();
- for(Token tk : tks.toIterable()) {
+ for (final Token tk : tks.toIterable()) {
boolean succ;
- while(feed.size() != 0) {
+ while (feed.size() != 0) {
succ = shuntToken(feed.poll(), opStack, unaryOps, currReturned, feed);
- if(!succ) return false;
+ if (!succ) return false;
}
succ = shuntToken(tk, opStack, unaryOps, currReturned, feed);
- if(!succ) return false;
+ if (!succ) return false;
}
// Flush leftover operators
- while(!opStack.isEmpty()) {
+ while (!opStack.isEmpty()) {
currReturned.addLast(opStack.pop());
}
- for(Token tk : currReturned) {
+ for (final Token tk : currReturned) {
returned.add(tk);
}
return true;
}
- private boolean shuntToken(Token tk, Deque<Token> opStack, Deque<Token> unaryStack, Deque<Token> currReturned,
- @SuppressWarnings("unused") Deque<Token> feed) {
- if(unaryStack.size() != 0) {
- if(isUnary(tk)) {
+ private boolean shuntToken(final Token tk, final Deque<Token> opStack, final Deque<Token> unaryStack,
+ final Deque<Token> currReturned, final Deque<Token> feed) {
+ if (unaryStack.size() != 0) {
+ if (isUnary(tk)) {
unaryStack.add(tk);
return true;
}
- Token unaryOp = unaryStack.pop();
+ final Token unaryOp = unaryStack.pop();
- Token.Type unaryType = unaryOp.type;
+ final Token.Type unaryType = unaryOp.type;
- if(unaryAdjectives.contains(unaryType)) {
- if(isOp(tk)) {
+ if (unaryAdjectives.contains(unaryType)) {
+ if (isOp(tk)) {
Errors.inst.printError(EK_SHUNT_NOTADV, unaryOp.toString(), tk.toString());
return false;
}
- Token newTok = new Token(TAGOPR);
+ final Token newTok = new Token(TAGOPR);
- if(tk.type == TAGOP) {
+ if (tk.type == TAGOP) {
newTok.tokenValues = tk.tokenValues;
} else {
newTok.tokenValues = new FunctionalList<>(tk);
@@ -174,15 +198,15 @@ public class Shunter {
opStack.push(newTok);
return true;
- } else if(unaryAdverbs.contains(unaryType)) {
- if(!isOp(tk)) {
+ } else if (unaryAdverbs.contains(unaryType)) {
+ if (!isOp(tk)) {
Errors.inst.printError(EK_SHUNT_NOTADJ, unaryOp.toString(), tk.toString());
return false;
}
- Token newTok = new Token(TAGOPR);
+ final Token newTok = new Token(TAGOPR);
- if(tk.type == TAGOP) {
+ if (tk.type == TAGOP) {
newTok.tokenValues = tk.tokenValues;
} else {
newTok.tokenValues = new FunctionalList<>(tk);
@@ -195,14 +219,14 @@ public class Shunter {
}
}
- if(isUnary(tk)) {
+ if (isUnary(tk)) {
unaryStack.add(tk);
return true;
- } else if(isOp(tk)) {
- while(!opStack.isEmpty() && isHigherPrec(tk, opStack.peek())) {
- Token newOp = opStack.pop();
+ } else if (isOp(tk)) {
+ 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());
}
@@ -210,16 +234,16 @@ 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) {
Token matching = null;
- switch(tk.type) {
+ switch (tk.type) {
case CPAREN:
matching = new Token(OPAREN, tk.intValue);
break;
@@ -231,32 +255,32 @@ 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) {
- IList<Token> group = new FunctionalList<>();
+ } else if (tk.type == GROUPSEP) {
+ 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;
}
@@ -269,58 +293,57 @@ public class Shunter {
return true;
}
- private boolean isHigherPrec(Token lft, Token rght) {
- Token.Type left = lft.type;
- Token.Type right = rght.type;
+ private boolean isHigherPrec(final Token lft, final Token rght) {
+ final Token.Type left = lft.type;
+ final Token.Type right = rght.type;
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) return false;
+ 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))
- return rightPrecedence > leftPrecedence;
- else
- return rightPrecedence >= leftPrecedence;
+ if (rightAssoc.contains(left)) return rightPrecedence > leftPrecedence;
+
+ return rightPrecedence >= leftPrecedence;
}
- private boolean isOp(Token tk) {
- Token.Type ty = tk.type;
+ private boolean isOp(final Token tk) {
+ final Token.Type ty = tk.type;
- if(ops.containsKey(ty)) return true;
- if(unaryAdjectives.contains(ty)) return true;
- if(unaryAdverbs.contains(ty)) return true;
- if(unaryGerunds.contains(ty)) return true;
- if(ty == TAGOPR) return true;
+ if (ops.containsKey(ty)) return true;
+ if (unaryAdjectives.contains(ty)) return true;
+ if (unaryAdverbs.contains(ty)) return true;
+ if (unaryGerunds.contains(ty)) return true;
+ if (ty == TAGOPR) return true;
return false;
}
- private boolean isUnary(Token tk) {
- Token.Type ty = tk.type;
+ private boolean isUnary(final Token tk) {
+ final Token.Type ty = tk.type;
- if(unaryAdjectives.contains(ty)) return true;
- if(unaryAdverbs.contains(ty)) return true;
- if(unaryGerunds.contains(ty)) return true;
+ if (unaryAdjectives.contains(ty)) return true;
+ if (unaryAdverbs.contains(ty)) return true;
+ if (unaryGerunds.contains(ty)) return true;
return false;
}