summaryrefslogtreecommitdiff
path: root/base/src/bjc/dicelang/expr
diff options
context:
space:
mode:
Diffstat (limited to 'base/src/bjc/dicelang/expr')
-rw-r--r--base/src/bjc/dicelang/expr/ExprREPL.java23
-rw-r--r--base/src/bjc/dicelang/expr/Ezpr.java38
-rw-r--r--base/src/bjc/dicelang/expr/Lexer.java6
-rw-r--r--base/src/bjc/dicelang/expr/Parser.java14
-rw-r--r--base/src/bjc/dicelang/expr/Shunter.java34
-rw-r--r--base/src/bjc/dicelang/expr/Token.java10
-rw-r--r--base/src/bjc/dicelang/expr/TokenType.java4
-rw-r--r--base/src/bjc/dicelang/expr/Tokens.java8
8 files changed, 74 insertions, 63 deletions
diff --git a/base/src/bjc/dicelang/expr/ExprREPL.java b/base/src/bjc/dicelang/expr/ExprREPL.java
index 49b9575..d763d67 100644
--- a/base/src/bjc/dicelang/expr/ExprREPL.java
+++ b/base/src/bjc/dicelang/expr/ExprREPL.java
@@ -17,7 +17,7 @@ public class ExprREPL {
* Main method.
*
* @param args
- * Unused CLI args.
+ * Unused CLI args.
*/
public static void main(final String[] args) {
/* Create our objects. */
@@ -32,7 +32,7 @@ public class ExprREPL {
String ln = scan.nextLine().trim();
/* Enter REPL loop. */
- while (!ln.equals("")) {
+ while(!ln.equals("")) {
/* Print raw command. */
System.out.println("Raw command: " + ln);
System.out.println();
@@ -40,13 +40,13 @@ public class ExprREPL {
/* Lex command to infix tokens. */
final Token[] infixTokens = lex.lexString(ln, toks);
System.out.println("Lexed tokens: ");
- for (final Token tok : infixTokens) {
+ for(final Token tok : infixTokens) {
System.out.println("\t" + tok);
}
/* Print out infix expression. */
System.out.print("Lexed expression: ");
- for (final Token tok : infixTokens) {
+ for(final Token tok : infixTokens) {
System.out.print(tok.toExpr() + " ");
}
@@ -57,13 +57,13 @@ public class ExprREPL {
/* Shunt infix tokens to postfix tokens. */
final IList<Token> postfixTokens = Shunter.shuntTokens(infixTokens);
System.out.println("Lexed tokens: ");
- for (final Token tok : postfixTokens) {
+ for(final Token tok : postfixTokens) {
System.out.println("\t" + tok);
}
/* Print out postfix tokens. */
System.out.print("Shunted expression: ");
- for (final Token tok : postfixTokens) {
+ for(final Token tok : postfixTokens) {
System.out.print(tok.toExpr() + " ");
}
@@ -71,10 +71,15 @@ public class ExprREPL {
System.out.println();
System.out.println();
- /* Construct a tree from the list of postfixed tokens. */
- final ITree<Token> ast = TreeConstructor.constructTree(postfixTokens, tok -> tok.typ.isOperator);
+ /*
+ * Construct a tree from the list of postfixed tokens.
+ */
+ final ITree<Token> ast = TreeConstructor.constructTree(postfixTokens,
+ tok -> tok.typ.isOperator);
- /* Print the tree, then the canonical expression for it. */
+ /*
+ * Print the tree, then the canonical expression for it.
+ */
System.out.println("Parsed tree");
System.out.println(ast.toString());
System.out.println("\nCanonical expr: " + Parser.toCanonicalExpr(ast));
diff --git a/base/src/bjc/dicelang/expr/Ezpr.java b/base/src/bjc/dicelang/expr/Ezpr.java
index 4919c81..2a54b6e 100644
--- a/base/src/bjc/dicelang/expr/Ezpr.java
+++ b/base/src/bjc/dicelang/expr/Ezpr.java
@@ -42,7 +42,7 @@ public class Ezpr {
@Override
public String toString() {
- if (typ == TOKEN) {
+ if(typ == TOKEN) {
return tok.toString();
}
return ezp.toString();
@@ -65,26 +65,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) {
- /* Add sum parts to corresponding bags. */
- for (EzprNode knd : kid.positive) {
+ if(typ == SUM) {
+ /*
+ * Add sum parts to corresponding bags.
+ */
+ 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);
}
}
@@ -93,25 +95,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);
}
}
@@ -128,13 +130,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/Lexer.java b/base/src/bjc/dicelang/expr/Lexer.java
index c7dd92f..c43050b 100644
--- a/base/src/bjc/dicelang/expr/Lexer.java
+++ b/base/src/bjc/dicelang/expr/Lexer.java
@@ -34,10 +34,10 @@ public class Lexer {
* Convert a string from a input command to a series of infix tokens.
*
* @param inp
- * The input command.
+ * The input command.
*
* @param tks
- * The token state.
+ * The token state.
*
* @return A series of infix tokens representing the command.
*/
@@ -48,7 +48,7 @@ public class Lexer {
final List<Token> tokens = new LinkedList<>();
/* Process each token. */
- for (final String spacedToken : spacedTokens) {
+ for(final String spacedToken : spacedTokens) {
/* Split on operators. */
final IList<String> splitTokens = split.split(spacedToken);
diff --git a/base/src/bjc/dicelang/expr/Parser.java b/base/src/bjc/dicelang/expr/Parser.java
index 1156334..020467a 100644
--- a/base/src/bjc/dicelang/expr/Parser.java
+++ b/base/src/bjc/dicelang/expr/Parser.java
@@ -9,13 +9,13 @@ import bjc.utils.data.ITree;
*/
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.
*/
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 +29,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/expr/Shunter.java b/base/src/bjc/dicelang/expr/Shunter.java
index 53c2298..3d2a523 100644
--- a/base/src/bjc/dicelang/expr/Shunter.java
+++ b/base/src/bjc/dicelang/expr/Shunter.java
@@ -16,7 +16,7 @@ public class Shunter {
* Convert a infix series of tokens to a postfix series of tokens.
*
* @param infixTokens
- * The tokens in infix order.
+ * The tokens in infix order.
*
* @return The tokens in postfix order.
*/
@@ -28,20 +28,22 @@ public class Shunter {
final Deque<Token> opStack = new LinkedList<>();
/* Shunt each token. */
- for (final Token tok : infixTokens) {
+ for(final Token tok : infixTokens) {
/* Handle operators. */
- if (tok.typ.isOperator) {
+ if(tok.typ.isOperator) {
Token curOp = opStack.peek();
/*
- * Check if an operator is higher priority, respecting their left associativity.
+ * Check if an operator is higher priority,
+ * respecting their left associativity.
*
- * @NOTE Should this be factored out into a method?
+ * @NOTE Should this be factored out into a
+ * method?
*/
int leftPriority = tok.typ.operatorPriority;
int rightPriority;
- if (curOp == null) {
+ if(curOp == null) {
rightPriority = 0;
} else {
rightPriority = curOp.typ.operatorPriority;
@@ -50,14 +52,15 @@ public class Shunter {
boolean isHigherPrec = leftPriority >= rightPriority;
/*
- * Pop all operators that are lower precedence than us.
+ * 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.typ.operatorPriority;
- if (curOp == null) {
+ if(curOp == null) {
rightPriority = 0;
} else {
rightPriority = curOp.typ.operatorPriority;
@@ -67,21 +70,22 @@ public class Shunter {
}
opStack.push(tok);
- } else if (tok.typ == TokenType.OPAREN) {
+ } else if(tok.typ == TokenType.OPAREN) {
opStack.push(tok);
- } else if (tok.typ == TokenType.CPAREN) {
+ } else if(tok.typ == TokenType.CPAREN) {
Token curOp = opStack.peek();
/*
- * Pop things until we find the matching parenthesis.
+ * Pop things until we find the matching
+ * parenthesis.
*/
- while (curOp.typ != TokenType.OPAREN) {
+ 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 {
@@ -92,7 +96,7 @@ public class Shunter {
/*
* Flush remaining operators.
*/
- while (!opStack.isEmpty()) {
+ while(!opStack.isEmpty()) {
postfixTokens.add(opStack.pop());
}
diff --git a/base/src/bjc/dicelang/expr/Token.java b/base/src/bjc/dicelang/expr/Token.java
index 750362d..f083577 100644
--- a/base/src/bjc/dicelang/expr/Token.java
+++ b/base/src/bjc/dicelang/expr/Token.java
@@ -33,13 +33,13 @@ public class Token {
* Create a new token.
*
* @param type
- * The type of this token.
+ * The type of this token.
*
* @param raw
- * The string this token came from.
+ * The string this token came from.
*
* @param toks
- * The state for this token
+ * The state for this token
*/
public Token(final TokenType type, final String raw, final Tokens toks) {
this.typ = type;
@@ -53,7 +53,7 @@ public class Token {
String typeStr = typ.toString();
typeStr = String.format("%s (%s)", typeStr, typ.name());
- if (typ == 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 (typ) {
+ switch(typ) {
case ADD:
return "+";
case SUBTRACT:
diff --git a/base/src/bjc/dicelang/expr/TokenType.java b/base/src/bjc/dicelang/expr/TokenType.java
index 71dd597..93de6d9 100644
--- a/base/src/bjc/dicelang/expr/TokenType.java
+++ b/base/src/bjc/dicelang/expr/TokenType.java
@@ -10,8 +10,8 @@ public enum TokenType {
* @NOTE
*
* Do we want to switch to auto-numbering the tokens? They were manually
- * numbered because this was an assignment for PoPL and that was what Dr. Naz
- * wanted.
+ * numbered because this was an assignment for PoPL and that was what
+ * Dr. Naz wanted.
*/
/** Represents + */
diff --git a/base/src/bjc/dicelang/expr/Tokens.java b/base/src/bjc/dicelang/expr/Tokens.java
index 687a414..c307391 100644
--- a/base/src/bjc/dicelang/expr/Tokens.java
+++ b/base/src/bjc/dicelang/expr/Tokens.java
@@ -58,14 +58,14 @@ public class Tokens {
* Convert the string representation of a token into a token.
*
* @param tok
- * The string representation of the token.
+ * The string representation of the token.
* @param raw
- * The original string the token came from.
+ * The original string the token came from.
*
* @return The token the string represents.
*/
public Token lexToken(final String tok, final String raw) {
- if (litTokens.containsKey(tok)) {
+ if(litTokens.containsKey(tok)) {
/* Return matching literal token. */
return new Token(litTokens.get(tok), raw, this);
}
@@ -78,7 +78,7 @@ public class Tokens {
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. */
tk.intValue = revSymTab.get(tok);
} else {