diff options
Diffstat (limited to 'dice-lang/src/main/java/bjc/dicelang/old/ast/nodes')
6 files changed, 466 insertions, 0 deletions
diff --git a/dice-lang/src/main/java/bjc/dicelang/old/ast/nodes/DiceASTType.java b/dice-lang/src/main/java/bjc/dicelang/old/ast/nodes/DiceASTType.java new file mode 100644 index 0000000..633d1d9 --- /dev/null +++ b/dice-lang/src/main/java/bjc/dicelang/old/ast/nodes/DiceASTType.java @@ -0,0 +1,27 @@ +package bjc.dicelang.old.ast.nodes; + +/** + * An enum to represent the type of node an AST node is + * + * @author ben + * + */ +public enum DiceASTType { + /** + * A node that contains a literal value + */ + LITERAL, + /** + * A node that contains an operator expression + */ + OPERATOR, + /** + * A node that contains a variable reference + */ + VARIABLE; + + @Override + public String toString() { + return this.name().toLowerCase(); + } +}
\ No newline at end of file diff --git a/dice-lang/src/main/java/bjc/dicelang/old/ast/nodes/IDiceASTNode.java b/dice-lang/src/main/java/bjc/dicelang/old/ast/nodes/IDiceASTNode.java new file mode 100644 index 0000000..579c595 --- /dev/null +++ b/dice-lang/src/main/java/bjc/dicelang/old/ast/nodes/IDiceASTNode.java @@ -0,0 +1,23 @@ +package bjc.dicelang.old.ast.nodes; + +/** + * The interface for a node in a dice AST + * + * @author ben + * + */ +public interface IDiceASTNode { + /** + * Check if this node represents an operator or not + * + * @return Whether or not this node represents an operator + */ + public boolean isOperator(); + + /** + * Get the type of AST node this node is + * + * @return The type of AST node this AST node is + */ + public DiceASTType getType(); +}
\ No newline at end of file diff --git a/dice-lang/src/main/java/bjc/dicelang/old/ast/nodes/LiteralDiceNode.java b/dice-lang/src/main/java/bjc/dicelang/old/ast/nodes/LiteralDiceNode.java new file mode 100644 index 0000000..46c84d0 --- /dev/null +++ b/dice-lang/src/main/java/bjc/dicelang/old/ast/nodes/LiteralDiceNode.java @@ -0,0 +1,217 @@ +package bjc.dicelang.old.ast.nodes; + +import org.apache.commons.lang3.StringUtils; + +import bjc.dicelang.ComplexDice; +import bjc.dicelang.CompoundDice; +import bjc.dicelang.IDiceExpression; +import bjc.dicelang.ScalarDie; +import bjc.utils.data.Pair; +import bjc.utils.parserutils.AST; + +/** + * A AST node that represents a literal value + * + * @author ben + * + */ +public class LiteralDiceNode implements IDiceASTNode { + private static boolean isValidInfixOperator(String dat, String op) { + return StringUtils.countMatches(dat, op) == 1 + && !dat.equalsIgnoreCase(op) && !dat.startsWith(op); + } + + /** + * The value contained by this node + */ + private String value; + + /** + * Create a new node with the given value + * + * @param data + * The value to be in this node + */ + public LiteralDiceNode(String data) { + this.value = data; + } + + /** + * Create a new node with the given value + * + * @param val + * The value for this node + */ + public LiteralDiceNode(int val) { + this(Integer.toString(val)); + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } else if (obj == null) { + return false; + } else if (getClass() != obj.getClass()) { + return false; + } else { + LiteralDiceNode other = (LiteralDiceNode) obj; + + if (value == null) { + if (other.value != null) { + return false; + } + } else if (!value.equals(other.value)) { + return false; + } + + return true; + } + } + + /** + * Get the data stored in this AST node + * + * @return the data stored in this AST node + */ + public String getData() { + return value; + } + + @Override + public DiceASTType getType() { + return DiceASTType.LITERAL; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((value == null) ? 0 : value.hashCode()); + return result; + } + + @Override + public boolean isOperator() { + return false; + } + + /** + * Parse this node into an expression + * + * @return The node in expression form + */ + public IDiceExpression toExpression() { + String literalData = this.getData(); + + if (LiteralDiceNode.isValidInfixOperator(literalData, "c")) { + String[] strangs = literalData.split("c"); + + return new CompoundDice(strangs); + } else if (LiteralDiceNode.isValidInfixOperator(literalData, + "d")) { + /* + * Handle dice groups + */ + return ComplexDice.fromString(literalData); + } else { + try { + return new ScalarDie(Integer.parseInt(literalData)); + } catch (NumberFormatException nfex) { + UnsupportedOperationException usex = new UnsupportedOperationException( + "Found malformed leaf token " + this); + + usex.initCause(nfex); + + throw usex; + } + } + } + + /** + * Parse this node into an expression + * + * @return The node as a pair of a sample value and the AST it + * represents + */ + public Pair<Integer, AST<IDiceASTNode>> toParseValue() { + AST<IDiceASTNode> returnedAST = new AST<>(this); + + IDiceExpression expression = toExpression(); + + return new Pair<>(expression.roll(), returnedAST); + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return value; + } + + /** + * Check if this node represents a constant value + * + * @return Whether or not this node represents a constant value + */ + public boolean isConstant() { + try { + Integer.parseInt(value); + return true; + } catch (@SuppressWarnings("unused") NumberFormatException nfex) { + // We don't care about details + return false; + } + } + + /** + * Return the constant value this node represents + * + * @return The constant value of this node + * + * @throws NumberFormatException + * if you call this on a node that doesn't represent a + * constant value + */ + public int toConstant() { + return Integer.parseInt(value); + } + + /** + * Check if a token represents a literal + * + * @param tok + * The token to check + * @return Whether or not the token represents a literal + */ + public static boolean isLiteral(String tok) { + if (StringUtils.countMatches(tok, 'c') == 1 + && !tok.equalsIgnoreCase("c")) { + return true; + } else if (StringUtils.countMatches(tok, 'd') == 1 + && !tok.equalsIgnoreCase("d")) { + return true; + } else { + try { + Integer.parseInt(tok); + return true; + } catch (@SuppressWarnings("unused") NumberFormatException nfex) { + // We don't care about details + return false; + } + } + } +}
\ No newline at end of file diff --git a/dice-lang/src/main/java/bjc/dicelang/old/ast/nodes/OperatorDiceNode.java b/dice-lang/src/main/java/bjc/dicelang/old/ast/nodes/OperatorDiceNode.java new file mode 100644 index 0000000..3c06553 --- /dev/null +++ b/dice-lang/src/main/java/bjc/dicelang/old/ast/nodes/OperatorDiceNode.java @@ -0,0 +1,92 @@ +package bjc.dicelang.old.ast.nodes; + +// The following classes need to be changed upon addition of a new operator +// 1. DiceASTExpression +// 2. DiceASTFlattener +// 3. DiceASTParser +/** + * A node that represents an operator + * + * @author ben + * + */ +public enum OperatorDiceNode implements IDiceASTNode { + /** + * Represents adding two nodes + */ + ADD, + /** + * Represents assigning one node to another + */ + ASSIGN, + /** + * Representings combining two node values together + */ + COMPOUND, + /** + * Represents dividing two nodes + */ + DIVIDE, + /** + * Represents using one node a variable number of times + */ + GROUP, + /** + * Represents multiplying two nodes + */ + MULTIPLY, + /** + * Represents subtracting two nodes + */ + SUBTRACT, + /** + * Represents executing one statement in the context of the other + */ + LET; + + /** + * Create a operator node from a string + * + * @param s + * The string to convert to a node + * @return The operator corresponding to the node + */ + public static OperatorDiceNode fromString(String s) { + switch (s) { + case ":=": + return ASSIGN; + case "+": + return ADD; + case "-": + return SUBTRACT; + case "*": + return MULTIPLY; + case "/": + return DIVIDE; + case "d": + return GROUP; + case "c": + return COMPOUND; + case "->": + return LET; + default: + throw new IllegalArgumentException( + s + " is not a valid operator node"); + } + } + + @Override + public DiceASTType getType() { + return DiceASTType.OPERATOR; + } + + /* + * (non-Javadoc) + * + * @see bjc.utils.dice.ast.IDiceASTNode#isOperator() + */ + @Override + public boolean isOperator() { + return true; + } +} diff --git a/dice-lang/src/main/java/bjc/dicelang/old/ast/nodes/VariableDiceNode.java b/dice-lang/src/main/java/bjc/dicelang/old/ast/nodes/VariableDiceNode.java new file mode 100644 index 0000000..262f99b --- /dev/null +++ b/dice-lang/src/main/java/bjc/dicelang/old/ast/nodes/VariableDiceNode.java @@ -0,0 +1,101 @@ +package bjc.dicelang.old.ast.nodes; + +/** + * A node that represents a variable reference + * + * @author ben + * + */ +public class VariableDiceNode implements IDiceASTNode { + /** + * The variable referenced by this node + */ + private String variableName; + + /** + * Create a new node representing the specified variable + * + * @param varName + * The name of the variable being referenced + */ + public VariableDiceNode(String varName) { + this.variableName = varName; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + // Handle special cases + if (this == obj) { + return true; + } else if (obj == null) { + return false; + } else if (getClass() != obj.getClass()) { + return false; + } else { + VariableDiceNode other = (VariableDiceNode) obj; + + if (variableName == null) { + if (other.variableName != null) { + return false; + } + } else if (!variableName.equals(other.variableName)) { + return false; + } + + return true; + } + } + + @Override + public DiceASTType getType() { + return DiceASTType.VARIABLE; + } + + /** + * Get the variable referenced by this AST node + * + * @return the variable referenced by this AST node + */ + public String getVariable() { + return variableName; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + + ((variableName == null) ? 0 : variableName.hashCode()); + return result; + } + + /* + * (non-Javadoc) + * + * @see bjc.utils.dice.ast.IDiceASTNode#isOperator() + */ + @Override + public boolean isOperator() { + return false; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return variableName; + } +}
\ No newline at end of file diff --git a/dice-lang/src/main/java/bjc/dicelang/old/ast/nodes/package-info.java b/dice-lang/src/main/java/bjc/dicelang/old/ast/nodes/package-info.java new file mode 100644 index 0000000..f99776f --- /dev/null +++ b/dice-lang/src/main/java/bjc/dicelang/old/ast/nodes/package-info.java @@ -0,0 +1,6 @@ +/** + * This package contains the various Node types in the Dice AST + * @author ben + * + */ +package bjc.dicelang.old.ast.nodes;
\ No newline at end of file |
