diff options
| author | EVE <EVE@EVE-PC> | 2017-03-13 16:41:45 -0400 |
|---|---|---|
| committer | EVE <EVE@EVE-PC> | 2017-03-13 16:41:45 -0400 |
| commit | 01136c6796e21f023713e026674576d8e623462d (patch) | |
| tree | e77886fe0e0adaf3c0430fba9ce248ef83f74fe4 /dice-lang/src/bjc/dicelang/Shunter.java | |
| parent | 870d769cfc152171d27b2331a7c590d0b307ad48 (diff) | |
Formatting
Diffstat (limited to 'dice-lang/src/bjc/dicelang/Shunter.java')
| -rw-r--r-- | dice-lang/src/bjc/dicelang/Shunter.java | 233 |
1 files changed, 121 insertions, 112 deletions
diff --git a/dice-lang/src/bjc/dicelang/Shunter.java b/dice-lang/src/bjc/dicelang/Shunter.java index 900f72c..48c5c67 100644 --- a/dice-lang/src/bjc/dicelang/Shunter.java +++ b/dice-lang/src/bjc/dicelang/Shunter.java @@ -22,16 +22,16 @@ public class Shunter { * Operators that are right-associative */ Set<Token.Type> rightAssoc; - + /* * Operators that aren't associative */ Set<Token.Type> notAssoc; - + // Unary operators that can only be // applied to non-operator tokens and yield operator tokens Set<Token.Type> unaryAdjectives; - + // Unary operators that can only be // applied to operator tokens and yield operator tokens Set<Token.Type> unaryAdverbs; @@ -42,210 +42,211 @@ public class Shunter { public final int MATH_PREC = 30; public final int DICE_PREC = 20; - public final int STR_PREC = 10; + public final int STR_PREC = 10; public final int EXPR_PREC = 0; public Shunter() { ops = new FunctionalMap<>(); - + rightAssoc = new HashSet<>(); - notAssoc = new HashSet<>(); + notAssoc = new HashSet<>(); unaryAdjectives = new HashSet<>(); - unaryAdverbs = new HashSet<>(); - unaryGerunds = new HashSet<>(); + unaryAdverbs = new HashSet<>(); + unaryGerunds = new HashSet<>(); unaryAdverbs.add(COERCE); - ops.put(ADD, 0 + MATH_PREC); - ops.put(SUBTRACT, 0 + MATH_PREC); + ops.put(ADD, 0 + MATH_PREC); + ops.put(SUBTRACT, 0 + MATH_PREC); - ops.put(MULTIPLY, 1 + MATH_PREC); - ops.put(IDIVIDE, 1 + MATH_PREC); - ops.put(DIVIDE, 1 + MATH_PREC); + ops.put(MULTIPLY, 1 + MATH_PREC); + ops.put(IDIVIDE, 1 + MATH_PREC); + ops.put(DIVIDE, 1 + MATH_PREC); - ops.put(DICEGROUP, 0 + DICE_PREC); + ops.put(DICEGROUP, 0 + DICE_PREC); ops.put(DICECONCAT, 1 + DICE_PREC); - ops.put(DICELIST, 2 + DICE_PREC); + ops.put(DICELIST, 2 + DICE_PREC); - ops.put(STRCAT, 0 + STR_PREC); - - ops.put(STRREP, 1 + STR_PREC); - - ops.put(LET, 0 + EXPR_PREC); - ops.put(BIND, 1 + EXPR_PREC); + ops.put(STRCAT, 0 + STR_PREC); + + ops.put(STRREP, 1 + STR_PREC); + + ops.put(LET, 0 + EXPR_PREC); + ops.put(BIND, 1 + EXPR_PREC); } public boolean shuntTokens(IList<Token> tks, IList<Token> returned) { - Deque<Token> opStack = new LinkedList<>(); + Deque<Token> opStack = new LinkedList<>(); Deque<Token> unaryOps = new LinkedList<>(); Deque<Token> currReturned = new LinkedList<>(); Deque<Token> feed = new LinkedList<>(); - for(Token tk : tks.toIterable()) { + for (Token tk : tks.toIterable()) { boolean succ; - - 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()); } - for(Token tk : currReturned) { + for (Token tk : currReturned) { returned.add(tk); } return true; } - private boolean shuntToken(Token tk, Deque<Token> opStack, - Deque<Token> unaryStack, Deque<Token> currReturned, + private boolean shuntToken(Token tk, Deque<Token> opStack, Deque<Token> unaryStack, Deque<Token> currReturned, Deque<Token> feed) { - if(unaryStack.size() != 0) { - if(isUnary(tk)) { + if (unaryStack.size() != 0) { + if (isUnary(tk)) { unaryStack.add(tk); return true; } - + Token unaryOp = unaryStack.pop(); - + 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); - - if(tk.type == TAGOP) { + + if (tk.type == TAGOP) { newTok.tokenValues = tk.tokenValues; } else { newTok.tokenValues = new FunctionalList<>(tk); } - + newTok.tokenValues.add(unaryOp); 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); - - if(tk.type == TAGOP) { + + if (tk.type == TAGOP) { newTok.tokenValues = tk.tokenValues; } else { newTok.tokenValues = new FunctionalList<>(tk); } - + newTok.tokenValues.add(unaryOp); opStack.push(newTok); - + return true; } } - - if(isUnary(tk)) { + + if (isUnary(tk)) { unaryStack.add(tk); return true; - } else if(isOp(tk)) { - while(!opStack.isEmpty() && isHigherPrec(tk, opStack.peek())) { + } else if (isOp(tk)) { + while (!opStack.isEmpty() && isHigherPrec(tk, opStack.peek())) { 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()); } - + currReturned.addLast(newOp); } - + 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) currReturned.addLast(tk); - } else if(tk.type == CPAREN || tk.type == CBRACE) { + + if (tk.type == OBRACE) + currReturned.addLast(tk); + } else if (tk.type == CPAREN || tk.type == CBRACE) { Token matching = null; - - switch(tk.type) { - case CPAREN: - matching = new Token(OPAREN, tk.intValue); - break; - case CBRACE: - matching = new Token(OBRACE, tk.intValue); - break; - default: - break; + + switch (tk.type) { + case CPAREN: + matching = new Token(OPAREN, tk.intValue); + break; + case CBRACE: + matching = new Token(OBRACE, tk.intValue); + break; + default: + break; } - - 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) { 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; } - + currReturned.addLast(new Token(TOKGROUP, group)); } else { currReturned.addLast(tk); } - + return true; } private boolean isHigherPrec(Token lft, Token rght) { - Token.Type left = lft.type; + Token.Type left = lft.type; Token.Type right = rght.type; boolean exists = ops.containsKey(right); - if(rght.type == TAGOPR) exists = true; + if (rght.type == TAGOPR) + exists = true; // If it doesn't, the left is higher precedence. if (!exists) { @@ -255,19 +256,19 @@ public class Shunter { int rightPrecedence; int leftPrecedence; - if(rght.type == TAGOPR) { - rightPrecedence = (int)rght.intValue; + if (rght.type == TAGOPR) { + rightPrecedence = (int) rght.intValue; } else { rightPrecedence = ops.get(right); } - if(lft.type == TAGOPR) { - leftPrecedence = (int)lft.intValue; + if (lft.type == TAGOPR) { + leftPrecedence = (int) lft.intValue; } else { - leftPrecedence = ops.get(left); + leftPrecedence = ops.get(left); } - - if(rightAssoc.contains(left)) { + + if (rightAssoc.contains(left)) { return rightPrecedence > leftPrecedence; } else { return rightPrecedence >= leftPrecedence; @@ -276,23 +277,31 @@ public class Shunter { private boolean isOp(Token tk) { 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; - - 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; } } |
