summaryrefslogtreecommitdiff
path: root/base/src/bjc/dicelang/expr
diff options
context:
space:
mode:
authorstudent <student@localhost>2018-03-05 16:48:38 -0500
committerstudent <student@localhost>2018-03-05 16:48:38 -0500
commit806cba069576e48477d4660a019a49c405cb0195 (patch)
treec1e718b18e660c02a12b5a466a222daf8a435bf4 /base/src/bjc/dicelang/expr
parentf9d9bd4bbf7dd6a297e1daf5ee7b4263d706d9cd (diff)
Update
Diffstat (limited to 'base/src/bjc/dicelang/expr')
-rw-r--r--base/src/bjc/dicelang/expr/ExprREPL.java11
-rw-r--r--base/src/bjc/dicelang/expr/Ezpr.java41
-rw-r--r--base/src/bjc/dicelang/expr/Parser.java20
3 files changed, 41 insertions, 31 deletions
diff --git a/base/src/bjc/dicelang/expr/ExprREPL.java b/base/src/bjc/dicelang/expr/ExprREPL.java
index d763d67..6cc06e2 100644
--- a/base/src/bjc/dicelang/expr/ExprREPL.java
+++ b/base/src/bjc/dicelang/expr/ExprREPL.java
@@ -6,13 +6,12 @@ import bjc.utils.data.ITree;
import bjc.utils.funcdata.IList;
import bjc.utils.parserutils.TreeConstructor;
+/**
+ * REPL for expressions.
+ * @author student
+ *
+ */
public class ExprREPL {
-
- /*
- * @TODO 10/08/17 Ben Culkin :MainSeperation
- *
- * This main method should be moved to its own class.
- */
/**
* Main method.
*
diff --git a/base/src/bjc/dicelang/expr/Ezpr.java b/base/src/bjc/dicelang/expr/Ezpr.java
index 2a54b6e..12a3413 100644
--- a/base/src/bjc/dicelang/expr/Ezpr.java
+++ b/base/src/bjc/dicelang/expr/Ezpr.java
@@ -11,6 +11,13 @@ import com.google.common.collect.Multiset;
import static bjc.dicelang.expr.Ezpr.EzprNode.EzprNodeType.*;
+/*
+ * @TODO 3/5/18 Ben Culkin :EzprFinished
+ *
+ * Finish the documentation and make sure this class works.
+ */
+
+@SuppressWarnings("javadoc")
public class Ezpr {
public static enum EzprType {
SUM, MUL
@@ -42,7 +49,7 @@ public class Ezpr {
@Override
public String toString() {
- if(typ == TOKEN) {
+ if (typ == TOKEN) {
return tok.toString();
}
return ezp.toString();
@@ -65,28 +72,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) {
+ if (typ == SUM) {
/*
* Add sum parts to corresponding bags.
*/
- 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);
}
} 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);
}
}
@@ -95,25 +102,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);
}
}
@@ -130,13 +137,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/Parser.java b/base/src/bjc/dicelang/expr/Parser.java
index 020467a..db6d547 100644
--- a/base/src/bjc/dicelang/expr/Parser.java
+++ b/base/src/bjc/dicelang/expr/Parser.java
@@ -8,14 +8,18 @@ import bjc.utils.data.ITree;
* @author Ben Culkin
*/
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.
+ *
+ * @param ast
+ * The AST to canonicalize.
+ * @return The canonicalized AST.
*/
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 +33,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);
}
}