diff options
Diffstat (limited to 'dice-lang/src/bjc/dicelang/expr')
| -rw-r--r-- | dice-lang/src/bjc/dicelang/expr/Lexer.java | 20 | ||||
| -rw-r--r-- | dice-lang/src/bjc/dicelang/expr/Parser.java | 79 | ||||
| -rw-r--r-- | dice-lang/src/bjc/dicelang/expr/Shunter.java | 36 | ||||
| -rw-r--r-- | dice-lang/src/bjc/dicelang/expr/Token.java | 16 | ||||
| -rw-r--r-- | dice-lang/src/bjc/dicelang/expr/TokenType.java | 4 | ||||
| -rw-r--r-- | dice-lang/src/bjc/dicelang/expr/Tokens.java | 15 |
6 files changed, 84 insertions, 86 deletions
diff --git a/dice-lang/src/bjc/dicelang/expr/Lexer.java b/dice-lang/src/bjc/dicelang/expr/Lexer.java index 52be56c..bac866b 100644 --- a/dice-lang/src/bjc/dicelang/expr/Lexer.java +++ b/dice-lang/src/bjc/dicelang/expr/Lexer.java @@ -1,11 +1,11 @@ package bjc.dicelang.expr; -import bjc.utils.funcdata.IList; -import bjc.utils.parserutils.splitter.ConfigurableTokenSplitter; - import java.util.LinkedList; import java.util.List; +import bjc.utils.funcdata.IList; +import bjc.utils.parserutils.splitter.ConfigurableTokenSplitter; + /** * Implements the lexer for simple expression operations. * @@ -15,7 +15,7 @@ public class Lexer { /* * Splitter we use. */ - private ConfigurableTokenSplitter split; + private final ConfigurableTokenSplitter split; /** * Create a new expression lexer. @@ -38,14 +38,14 @@ public class Lexer { * * @return A series of infix tokens representing the command. */ - public Token[] lexString(String inp, Tokens tks) { - String[] spacedTokens = inp.split("[ \t]"); + public Token[] lexString(final String inp, final Tokens tks) { + final String[] spacedTokens = inp.split("[ \t]"); - List<Token> tokens = new LinkedList<>(); + final List<Token> tokens = new LinkedList<>(); - for(String spacedToken : spacedTokens) { - IList<String> splitTokens = split.split(spacedToken); - IList<Token> rawTokens = splitTokens.map(tok -> tks.lexToken(tok, spacedToken)); + for (final String spacedToken : spacedTokens) { + final IList<String> splitTokens = split.split(spacedToken); + final IList<Token> rawTokens = splitTokens.map(tok -> tks.lexToken(tok, spacedToken)); rawTokens.forEach(tokens::add); } diff --git a/dice-lang/src/bjc/dicelang/expr/Parser.java b/dice-lang/src/bjc/dicelang/expr/Parser.java index 71a88fc..90a6b38 100644 --- a/dice-lang/src/bjc/dicelang/expr/Parser.java +++ b/dice-lang/src/bjc/dicelang/expr/Parser.java @@ -1,12 +1,12 @@ package bjc.dicelang.expr; +import java.util.Arrays; +import java.util.Scanner; + import bjc.utils.data.ITree; import bjc.utils.funcdata.FunctionalList; import bjc.utils.parserutils.TreeConstructor; -import java.util.Arrays; -import java.util.Scanner; - /** * Parser for simple math expressions. * @@ -19,17 +19,17 @@ public class Parser { * @param args * Unused CLI args. */ - public static void main(String[] args) { + public static void main(final String[] args) { /* * Create our objects. */ - Tokens toks = new Tokens(); - Lexer lex = new Lexer(); + final Tokens toks = new Tokens(); + final Lexer lex = new Lexer(); /* * Prepare our input. */ - Scanner scan = new Scanner(System.in); + final Scanner scan = new Scanner(System.in); /* * Read initial command. @@ -40,7 +40,7 @@ public class Parser { /* * Enter REPL loop. */ - while(!ln.equals("")) { + while (!ln.equals("")) { /* * Print raw command. */ @@ -50,9 +50,9 @@ public class Parser { /* * Lex command to infix tokens. */ - Token[] infixTokens = lex.lexString(ln, toks); + final Token[] infixTokens = lex.lexString(ln, toks); System.out.println("Lexed tokens: "); - for(Token tok : infixTokens) { + for (final Token tok : infixTokens) { System.out.println("\t" + tok); } @@ -60,7 +60,7 @@ public class Parser { * Print out infix expression. */ System.out.print("Lexed expression: "); - for(Token tok : infixTokens) { + for (final Token tok : infixTokens) { System.out.print(tok.toExpr() + " "); } System.out.println(); @@ -69,9 +69,9 @@ public class Parser { /* * Shunt infix tokens to postfix tokens. */ - Token[] postfixTokens = Shunter.shuntTokens(infixTokens); + final Token[] postfixTokens = Shunter.shuntTokens(infixTokens); System.out.println("Lexed tokens: "); - for(Token tok : postfixTokens) { + for (final Token tok : postfixTokens) { System.out.println("\t" + tok); } @@ -79,14 +79,14 @@ public class Parser { * Print out postfix tokens. */ System.out.print("Shunted expression: "); - for(Token tok : postfixTokens) { + for (final Token tok : postfixTokens) { System.out.print(tok.toExpr() + " "); } System.out.println(); System.out.println(); - FunctionalList<Token> tokList = new FunctionalList<>(Arrays.asList(postfixTokens)); - ITree<Token> ast = TreeConstructor.constructTree(tokList, tok -> tok.type.isOperator); + final FunctionalList<Token> tokList = new FunctionalList<>(Arrays.asList(postfixTokens)); + final ITree<Token> ast = TreeConstructor.constructTree(tokList, tok -> tok.typ.isOperator); /* * Print the tree, then the canonical expression for it. @@ -114,40 +114,39 @@ public class Parser { * Convert an expression to one that uses the smallest necessary amount * of parens. */ - private static String toCanonicalExpr(ITree<Token> ast) { - Token data = ast.getHead(); + private 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(); - else { - ITree<Token> left = ast.getChild(0); - ITree<Token> right = ast.getChild(1); - String leftExpr = toCanonicalExpr(left); - String rightExpr = toCanonicalExpr(right); + final ITree<Token> left = ast.getChild(0); + final ITree<Token> right = ast.getChild(1); - /* - * Add parens if the left was higher priority. - */ - if(left.getChildrenCount() == 0) { - if(left.getHead().type.operatorPriority >= data.type.operatorPriority) { - leftExpr = "(" + leftExpr + ")"; - } - } + String leftExpr = toCanonicalExpr(left); + String rightExpr = toCanonicalExpr(right); - /* - * Add parens if the right was higher priority. - */ - if(right.getChildrenCount() == 0) { - if(right.getHead().type.operatorPriority >= data.type.operatorPriority) { - rightExpr = "(" + rightExpr + ")"; - } + /* + * Add parens if the left was higher priority. + */ + if (left.getChildrenCount() == 0) { + if (left.getHead().typ.operatorPriority >= data.typ.operatorPriority) { + leftExpr = "(" + leftExpr + ")"; } + } - return leftExpr + " " + data.toExpr() + " " + rightExpr; + /* + * Add parens if the right was higher priority. + */ + if (right.getChildrenCount() == 0) { + if (right.getHead().typ.operatorPriority >= data.typ.operatorPriority) { + rightExpr = "(" + rightExpr + ")"; + } } + + return leftExpr + " " + data.toExpr() + " " + rightExpr; } }
\ No newline at end of file diff --git a/dice-lang/src/bjc/dicelang/expr/Shunter.java b/dice-lang/src/bjc/dicelang/expr/Shunter.java index 3b2bee2..19b30c3 100644 --- a/dice-lang/src/bjc/dicelang/expr/Shunter.java +++ b/dice-lang/src/bjc/dicelang/expr/Shunter.java @@ -19,32 +19,32 @@ public class Shunter { * * @return The tokens in postfix order. */ - public static Token[] shuntTokens(Token[] infixTokens) { - List<Token> postfixTokens = new ArrayList<>(infixTokens.length); + public static Token[] shuntTokens(final Token[] infixTokens) { + final List<Token> postfixTokens = new ArrayList<>(infixTokens.length); - Deque<Token> opStack = new LinkedList<>(); + final Deque<Token> opStack = new LinkedList<>(); /* * Shunt each token. */ - for(Token tok : infixTokens) { + for (final Token tok : infixTokens) { /* * Handle operators. */ - if(tok.type.isOperator) { + if (tok.typ.isOperator) { Token curOp = opStack.peek(); /* * Check if an operator is higher priority, * respecting their left associativity. */ - int leftPriority = tok.type.operatorPriority; + int leftPriority = tok.typ.operatorPriority; int rightPriority; - if(curOp == null) { + if (curOp == null) { rightPriority = 0; } else { - rightPriority = curOp.type.operatorPriority; + rightPriority = curOp.typ.operatorPriority; } boolean isHigherPrec = leftPriority >= rightPriority; @@ -53,41 +53,41 @@ public class Shunter { * 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.type.operatorPriority; + leftPriority = tok.typ.operatorPriority; - if(curOp == null) { + if (curOp == null) { rightPriority = 0; } else { - rightPriority = curOp.type.operatorPriority; + rightPriority = curOp.typ.operatorPriority; } isHigherPrec = leftPriority >= rightPriority; } opStack.push(tok); - } else if(tok.type == TokenType.OPAREN) { + } else if (tok.typ == TokenType.OPAREN) { opStack.push(tok); - } else if(tok.type == TokenType.CPAREN) { + } else if (tok.typ == TokenType.CPAREN) { Token curOp = opStack.peek(); /* * Pop things until we find the matching * parenthesis. */ - while(curOp.type != TokenType.OPAREN) { - Token tk = opStack.pop(); + 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 { @@ -98,7 +98,7 @@ public class Shunter { /* * Flush remaining operators. */ - while(!opStack.isEmpty()) { + while (!opStack.isEmpty()) { postfixTokens.add(opStack.pop()); } diff --git a/dice-lang/src/bjc/dicelang/expr/Token.java b/dice-lang/src/bjc/dicelang/expr/Token.java index dac90aa..d7fc0e2 100644 --- a/dice-lang/src/bjc/dicelang/expr/Token.java +++ b/dice-lang/src/bjc/dicelang/expr/Token.java @@ -9,14 +9,14 @@ public class Token { /* * The state for this token. */ - private Tokens tks; + private final Tokens tks; /** * The type of the token. * * Determines which fields have a value. */ - public final TokenType type; + public final TokenType typ; /** * The integer value attached to this token. @@ -40,8 +40,8 @@ public class Token { * @param toks * The state for this token */ - public Token(TokenType type, String raw, Tokens toks) { - this.type = type; + public Token(final TokenType type, final String raw, final Tokens toks) { + this.typ = type; rawValue = raw; @@ -50,10 +50,10 @@ public class Token { @Override public String toString() { - String typeStr = type.toString(); - typeStr += " (" + type.name() + ")"; + String typeStr = typ.toString(); + typeStr += " (" + typ.name() + ")"; - if(type == 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(type) { + switch (typ) { case ADD: return "+"; case SUBTRACT: diff --git a/dice-lang/src/bjc/dicelang/expr/TokenType.java b/dice-lang/src/bjc/dicelang/expr/TokenType.java index 9e4bbc2..fa20813 100644 --- a/dice-lang/src/bjc/dicelang/expr/TokenType.java +++ b/dice-lang/src/bjc/dicelang/expr/TokenType.java @@ -50,7 +50,7 @@ public enum TokenType { */ public final int operatorPriority; - private TokenType(int num, boolean isOp, int priority) { + private TokenType(final int num, final boolean isOp, final int priority) { nVal = num; isOperator = isOp; @@ -58,7 +58,7 @@ public enum TokenType { operatorPriority = priority; } - private TokenType(int num) { + private TokenType(final int num) { this(num, false, -1); } diff --git a/dice-lang/src/bjc/dicelang/expr/Tokens.java b/dice-lang/src/bjc/dicelang/expr/Tokens.java index 88bd99e..6bdcde0 100644 --- a/dice-lang/src/bjc/dicelang/expr/Tokens.java +++ b/dice-lang/src/bjc/dicelang/expr/Tokens.java @@ -66,20 +66,19 @@ public class Tokens { * * @return The token the string represents. */ - public Token lexToken(String tok, String raw) { - if(litTokens.containsKey(tok)) - return new Token(litTokens.get(tok), raw, this); - else - return parseVRef(tok, raw); + public Token lexToken(final String tok, final String raw) { + if (litTokens.containsKey(tok)) return new Token(litTokens.get(tok), raw, this); + + return parseVRef(tok, raw); } /* * Parse a variable reference. */ - private Token parseVRef(String tok, String raw) { - Token tk = new Token(TokenType.VREF, raw, this); + 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. */ |
