diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-04-18 08:34:32 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-04-18 08:34:32 -0400 |
| commit | 9ce39956fa1702f157c347dc4b8807d9b5dd2185 (patch) | |
| tree | d981c0010a92660a1f0501431c4a3bc02d94e56d /dice-lang/src/main/java/bjc/dicelang/ast/DiceASTUtils.java | |
| parent | 7c222f25d4b2d9f3b149d880f0e1acf8d673e4f5 (diff) | |
Reimplemented basic optimization.
Diffstat (limited to 'dice-lang/src/main/java/bjc/dicelang/ast/DiceASTUtils.java')
| -rw-r--r-- | dice-lang/src/main/java/bjc/dicelang/ast/DiceASTUtils.java | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTUtils.java b/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTUtils.java new file mode 100644 index 0000000..20a46c7 --- /dev/null +++ b/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTUtils.java @@ -0,0 +1,83 @@ +package bjc.dicelang.ast; + +import bjc.dicelang.IDiceExpression; +import bjc.dicelang.ScalarDie; +import bjc.dicelang.ast.nodes.DiceASTType; +import bjc.dicelang.ast.nodes.DiceLiteralNode; +import bjc.dicelang.ast.nodes.IDiceASTNode; +import bjc.dicelang.ast.nodes.ILiteralDiceNode; +import bjc.dicelang.ast.nodes.IntegerLiteralNode; +import bjc.utils.funcdata.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 + * @return Whether or not a dice AST contains a simple variable + * reference + */ + public static boolean + containsSimpleVariable(ITree<IDiceASTNode> nameTree) { + return nameTree.transformHead((nameNod) -> { + if (nameNod.getType() != DiceASTType.VARIABLE) { + return false; + } + + return true; + }); + } + + /** + * Convert an AST tree to an integer, if possible. + * + * @param tree + * The tree to convert + * @return The tree 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 toInt(ITree<IDiceASTNode> tree) { + return tree.transformHead((node) -> { + return ((ILiteralDiceNode) node).optimize(); + }); + } + + /** + * Convert an AST tree to a dice expression, if possible. + * + * @param tree + * The tree to convert + * @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 toExpression(ITree<IDiceASTNode> tree) { + ILiteralDiceNode litNode = (ILiteralDiceNode) tree.getHead(); + + switch (litNode.getLiteralType()) { + case DICE: + return ((DiceLiteralNode) litNode).getValue(); + case INTEGER: + return new ScalarDie( + ((IntegerLiteralNode) litNode).getValue()); + default: + throw new UnsupportedOperationException( + "This type of literal isn't convertable to an expression"); + } + } +} |
