diff options
| author | bjculkin <bjculkin@WIT-136XG42.wvu-ad.wvu.edu> | 2017-02-27 10:08:50 -0500 |
|---|---|---|
| committer | bjculkin <bjculkin@WIT-136XG42.wvu-ad.wvu.edu> | 2017-02-27 10:08:50 -0500 |
| commit | 79ee129fc0d36ad10bceb942262f2842419c030c (patch) | |
| tree | d1298fdb8b81726f4b9012d7a29c3029a55a3aa7 /dice-lang/src/bjc/dicelang/v1/ast/nodes/ILiteralDiceNode.java | |
| parent | c50a0744269ce22604c0604cc69e6d5e5ce8a3fc (diff) | |
Pacakge reorganization
Diffstat (limited to 'dice-lang/src/bjc/dicelang/v1/ast/nodes/ILiteralDiceNode.java')
| -rw-r--r-- | dice-lang/src/bjc/dicelang/v1/ast/nodes/ILiteralDiceNode.java | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/nodes/ILiteralDiceNode.java b/dice-lang/src/bjc/dicelang/v1/ast/nodes/ILiteralDiceNode.java new file mode 100644 index 0000000..11d0d90 --- /dev/null +++ b/dice-lang/src/bjc/dicelang/v1/ast/nodes/ILiteralDiceNode.java @@ -0,0 +1,75 @@ +package bjc.dicelang.v1.ast.nodes; + +/** + * Represents a literal of some type in the AST + * + * @author ben + * + */ +public interface ILiteralDiceNode extends IDiceASTNode { + /** + * Check if a token represents a literal, and if so, what type + * + * @param tok + * The token to check + * @return The type the literal would be if it is one, or null + * otherwise + */ + static DiceLiteralType getLiteralType(String tok) { + String diceGroup = "\\d*d\\d+\\"; + + String diceGroupOrNumber = "[(?:" + diceGroup + ")(?:\\d+)]"; + + if (tok.matches("\\A" + diceGroupOrNumber + "?" + "c" + + diceGroupOrNumber + "\\Z")) { + return DiceLiteralType.DICE; + } + + if (tok.matches("\\A" + diceGroup + "Z")) { + return DiceLiteralType.DICE; + } + + try { + Integer.parseInt(tok); + return DiceLiteralType.INTEGER; + } catch (NumberFormatException nfex) { + // We don't care about details + // This probably shouldn't return null, but I believe it does so + // because where its called checks that. @FIXME + return null; + } + } + + /** + * Check if this node can be optimized to a constant + * + * @return Whether or not this node can be optimized to a constant + * @see bjc.dicelang.v1.IDiceExpression#canOptimize() + */ + boolean canOptimize(); + + /** + * Get the type of literal this node represents + * + * @return The type of literal this node represents + */ + DiceLiteralType getLiteralType(); + + @Override + default DiceASTType getType() { + return DiceASTType.LITERAL; + } + + @Override + default boolean isOperator() { + return false; + } + + /** + * Optimize this node to a constant if possible + * + * @return This node in constant form if possible + * @see bjc.dicelang.v1.IDiceExpression#optimize() + */ + int optimize(); +} |
