summaryrefslogtreecommitdiff
path: root/dice-lang/src/bjc/dicelang/v1/ast/DiceASTUtils.java
diff options
context:
space:
mode:
authorbjculkin <bjculkin@WIT-136XG42.wvu-ad.wvu.edu>2017-02-27 10:08:50 -0500
committerbjculkin <bjculkin@WIT-136XG42.wvu-ad.wvu.edu>2017-02-27 10:08:50 -0500
commit79ee129fc0d36ad10bceb942262f2842419c030c (patch)
treed1298fdb8b81726f4b9012d7a29c3029a55a3aa7 /dice-lang/src/bjc/dicelang/v1/ast/DiceASTUtils.java
parentc50a0744269ce22604c0604cc69e6d5e5ce8a3fc (diff)
Pacakge reorganization
Diffstat (limited to 'dice-lang/src/bjc/dicelang/v1/ast/DiceASTUtils.java')
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/DiceASTUtils.java85
1 files changed, 85 insertions, 0 deletions
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTUtils.java b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTUtils.java
new file mode 100644
index 0000000..92658b6
--- /dev/null
+++ b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTUtils.java
@@ -0,0 +1,85 @@
+package bjc.dicelang.v1.ast;
+
+import bjc.dicelang.v1.IDiceExpression;
+import bjc.dicelang.v1.ScalarDie;
+import bjc.dicelang.v1.ast.nodes.DiceASTType;
+import bjc.dicelang.v1.ast.nodes.DiceLiteralNode;
+import bjc.dicelang.v1.ast.nodes.IDiceASTNode;
+import bjc.dicelang.v1.ast.nodes.ILiteralDiceNode;
+import bjc.dicelang.v1.ast.nodes.IntegerLiteralNode;
+import bjc.utils.data.ITree;
+
+/**
+ * Functions that are useful when dealing with dice ASTs
+ *
+ * @author ben
+ *
+ */
+public class DiceASTUtils {
+ /**
+ * Check if a dice AST contains a simple variable reference
+ *
+ * @param nameTree
+ * The tree to check for a reference in
+ * @return Whether or not a dice AST contains a simple variable
+ * reference
+ */
+ public static boolean containsSimpleVariable(
+ ITree<IDiceASTNode> nameTree) {
+ return nameTree.transformHead((nameNode) -> {
+ if (nameNode.getType() != DiceASTType.VARIABLE) {
+ return false;
+ }
+
+ return true;
+ });
+ }
+
+ /**
+ * Convert an literal AST node to a dice expression, if possible.
+ *
+ * @param tree
+ * The node to convert in tree form
+ * @return The tree as a dice expression
+ *
+ * @throws ClassCastException
+ * if the head of the tree is not a literal (implements
+ * {@link ILiteralDiceNode})
+ * @throws UnsupportedOperationException
+ * if the head of the tree is not optimizable
+ */
+ public static IDiceExpression literalToExpression(
+ ITree<IDiceASTNode> tree) {
+ ILiteralDiceNode literalNode = (ILiteralDiceNode) tree.getHead();
+
+ switch (literalNode.getLiteralType()) {
+ case DICE:
+ return ((DiceLiteralNode) literalNode).getValue();
+ case INTEGER:
+ return new ScalarDie(
+ ((IntegerLiteralNode) literalNode).getValue());
+ default:
+ throw new UnsupportedOperationException(
+ "This type of literal isn't convertable to an expression");
+ }
+ }
+
+ /**
+ * Convert an literal AST node to an integer, if possible.
+ *
+ * @param tree
+ * The literal node to convert, as a tree
+ * @return The node as an integer
+ *
+ * @throws ClassCastException
+ * if the head of the tree is not a literal (implements
+ * {@link ILiteralDiceNode})
+ * @throws UnsupportedOperationException
+ * if the head of the tree is not optimizable
+ */
+ public static int literalToInteger(ITree<IDiceASTNode> tree) {
+ return tree.transformHead((node) -> {
+ return ((ILiteralDiceNode) node).optimize();
+ });
+ }
+}