diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-10-27 21:56:18 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-10-27 22:12:47 -0400 |
| commit | e7413128ff4e376997de6e94e4bea5eca14811ef (patch) | |
| tree | 0749e270fdb754d04dc223abd95d47436508047f /dice-lang/src/bjc/dicelang/ast/nodes | |
| parent | e13a6981bd278c2cfc3b5ecb2517367b117f7a52 (diff) | |
Moved examples
Diffstat (limited to 'dice-lang/src/bjc/dicelang/ast/nodes')
10 files changed, 490 insertions, 0 deletions
diff --git a/dice-lang/src/bjc/dicelang/ast/nodes/DiceASTType.java b/dice-lang/src/bjc/dicelang/ast/nodes/DiceASTType.java new file mode 100644 index 0000000..9feb461 --- /dev/null +++ b/dice-lang/src/bjc/dicelang/ast/nodes/DiceASTType.java @@ -0,0 +1,27 @@ +package bjc.dicelang.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/bjc/dicelang/ast/nodes/DiceLiteralNode.java b/dice-lang/src/bjc/dicelang/ast/nodes/DiceLiteralNode.java new file mode 100644 index 0000000..b398ac6 --- /dev/null +++ b/dice-lang/src/bjc/dicelang/ast/nodes/DiceLiteralNode.java @@ -0,0 +1,52 @@ +package bjc.dicelang.ast.nodes; + +import bjc.dicelang.IDiceExpression; + +/** + * Represents a literal backed by a dice expression + * + * @author ben + * + */ +public class DiceLiteralNode implements ILiteralDiceNode { + private IDiceExpression expression; + + /** + * Create a new literal from an expression + * + * @param exp + * The expression to attempt to create a literal from + */ + public DiceLiteralNode(IDiceExpression exp) { + expression = exp; + } + + @Override + public boolean canOptimize() { + return expression.canOptimize(); + } + + @Override + public DiceLiteralType getLiteralType() { + return DiceLiteralType.DICE; + } + + /** + * Return the expression being represented + * + * @return The expression being represented + */ + public IDiceExpression getValue() { + return expression; + } + + @Override + public int optimize() { + return expression.optimize(); + } + + @Override + public String toString() { + return expression.toString(); + } +} diff --git a/dice-lang/src/bjc/dicelang/ast/nodes/DiceLiteralType.java b/dice-lang/src/bjc/dicelang/ast/nodes/DiceLiteralType.java new file mode 100644 index 0000000..41c6b05 --- /dev/null +++ b/dice-lang/src/bjc/dicelang/ast/nodes/DiceLiteralType.java @@ -0,0 +1,18 @@ +package bjc.dicelang.ast.nodes; + +/** + * Represents the type of literals that can be in an AST + * + * @author ben + * + */ +public enum DiceLiteralType { + /** + * Represents a integral constant + */ + INTEGER, + /** + * Represents a dice literal + */ + DICE; +} diff --git a/dice-lang/src/bjc/dicelang/ast/nodes/DiceOperatorType.java b/dice-lang/src/bjc/dicelang/ast/nodes/DiceOperatorType.java new file mode 100644 index 0000000..7cc1e42 --- /dev/null +++ b/dice-lang/src/bjc/dicelang/ast/nodes/DiceOperatorType.java @@ -0,0 +1,29 @@ +package bjc.dicelang.ast.nodes; + +/** + * Represents the different type of operators. + * + * Mostly, what distinguishes groups is that all the operators in a group + * have similiar precedence, and operate on similiar things + * + * @author ben + * + */ +public enum DiceOperatorType { + /** + * Represents operators that do math operations + */ + MATH, + /** + * Represents operators that do things with arrays + */ + ARRAY, + /** + * Represents operators that do things with dice + */ + DICE, + /** + * Represents operators that do things with expressions + */ + EXPRESSION; +} diff --git a/dice-lang/src/bjc/dicelang/ast/nodes/IDiceASTNode.java b/dice-lang/src/bjc/dicelang/ast/nodes/IDiceASTNode.java new file mode 100644 index 0000000..b7bf9a6 --- /dev/null +++ b/dice-lang/src/bjc/dicelang/ast/nodes/IDiceASTNode.java @@ -0,0 +1,23 @@ +package bjc.dicelang.ast.nodes; + +/** + * The interface for a node in a dice AST + * + * @author ben + * + */ +public interface IDiceASTNode { + /** + * Get the type of AST node this node is + * + * @return The type of AST node this AST node is + */ + public DiceASTType getType(); + + /** + * Check if this node represents an operator or not + * + * @return Whether or not this node represents an operator + */ + public boolean isOperator(); +}
\ No newline at end of file diff --git a/dice-lang/src/bjc/dicelang/ast/nodes/ILiteralDiceNode.java b/dice-lang/src/bjc/dicelang/ast/nodes/ILiteralDiceNode.java new file mode 100644 index 0000000..b94bcc8 --- /dev/null +++ b/dice-lang/src/bjc/dicelang/ast/nodes/ILiteralDiceNode.java @@ -0,0 +1,73 @@ +package bjc.dicelang.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 diceGroupOrNumber = "[(?:\\d*d\\d+)(?:\\d+)]"; + + if (tok.matches("\\A" + diceGroupOrNumber + "?" + "c" + + diceGroupOrNumber + "\\Z")) { + return DiceLiteralType.DICE; + } + + String diceGroup = "\\d*d\\d+\\"; + + if (tok.matches("\\A" + diceGroup + "Z")) { + return DiceLiteralType.DICE; + } + + try { + Integer.parseInt(tok); + return DiceLiteralType.INTEGER; + } catch (@SuppressWarnings("unused") NumberFormatException nfex) { + // We don't care about details + 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.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.IDiceExpression#optimize() + */ + int optimize(); +} diff --git a/dice-lang/src/bjc/dicelang/ast/nodes/IntegerLiteralNode.java b/dice-lang/src/bjc/dicelang/ast/nodes/IntegerLiteralNode.java new file mode 100644 index 0000000..3d43bb1 --- /dev/null +++ b/dice-lang/src/bjc/dicelang/ast/nodes/IntegerLiteralNode.java @@ -0,0 +1,50 @@ +package bjc.dicelang.ast.nodes; + +/** + * Represents an integer literal of some kind + * + * @author ben + * + */ +public class IntegerLiteralNode implements ILiteralDiceNode { + private int value; + + /** + * Create a new integer literal from the given number + * + * @param val + * The value this node represents + */ + public IntegerLiteralNode(int val) { + value = val; + } + + @Override + public boolean canOptimize() { + return true; + } + + @Override + public DiceLiteralType getLiteralType() { + return DiceLiteralType.INTEGER; + } + + /** + * Get the value this node represents + * + * @return The integer value of this node + */ + public int getValue() { + return value; + } + + @Override + public int optimize() { + return value; + } + + @Override + public String toString() { + return Integer.toString(value); + } +} diff --git a/dice-lang/src/bjc/dicelang/ast/nodes/OperatorDiceNode.java b/dice-lang/src/bjc/dicelang/ast/nodes/OperatorDiceNode.java new file mode 100644 index 0000000..7c0a29d --- /dev/null +++ b/dice-lang/src/bjc/dicelang/ast/nodes/OperatorDiceNode.java @@ -0,0 +1,110 @@ +package bjc.dicelang.ast.nodes; + +import static bjc.dicelang.ast.nodes.DiceOperatorType.DICE; +import static bjc.dicelang.ast.nodes.DiceOperatorType.EXPRESSION; +import static bjc.dicelang.ast.nodes.DiceOperatorType.MATH; + +/** + * A node that represents an operator + * + * @author ben + * + */ +public enum OperatorDiceNode implements IDiceASTNode { + /** + * Represents adding two nodes + */ + ADD(MATH), + /** + * Represents dividing two nodes + */ + DIVIDE(MATH), + /** + * Represents multiplying two nodes + */ + MULTIPLY(MATH), + /** + * Represents subtracting two nodes + */ + SUBTRACT(MATH), + /** + * Representings combining two node values together + */ + COMPOUND(DICE), + /** + * Represents using one node a variable number of times + */ + GROUP(DICE), + /** + * Represents constructing an array from a sequence of expressions + */ + ARRAY(DiceOperatorType.ARRAY), + /** + * Represents assigning one node to another + */ + ASSIGN(EXPRESSION), + /** + * Represents evaluating one expression in the context of another + */ + LET(EXPRESSION); + + /** + * 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": + case "group": + return GROUP; + case "c": + case "compound": + return COMPOUND; + case "=>": + return LET; + case "[]": + return ARRAY; + default: + throw new IllegalArgumentException( + s + " is not a valid operator node"); + } + } + + /** + * Represents the group of operator this operator is sorted into. + * + */ + public final DiceOperatorType type; + + private OperatorDiceNode(DiceOperatorType ty) { + type = ty; + } + + @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/bjc/dicelang/ast/nodes/VariableDiceNode.java b/dice-lang/src/bjc/dicelang/ast/nodes/VariableDiceNode.java new file mode 100644 index 0000000..da66608 --- /dev/null +++ b/dice-lang/src/bjc/dicelang/ast/nodes/VariableDiceNode.java @@ -0,0 +1,101 @@ +package bjc.dicelang.ast.nodes; + +/** + * A node that represents a reference to a variable + * + * @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/bjc/dicelang/ast/nodes/package-info.java b/dice-lang/src/bjc/dicelang/ast/nodes/package-info.java new file mode 100644 index 0000000..f0f7366 --- /dev/null +++ b/dice-lang/src/bjc/dicelang/ast/nodes/package-info.java @@ -0,0 +1,7 @@ +/** + * Classes for nodes in the dice-lang AST + * + * @author ben + * + */ +package bjc.dicelang.ast.nodes;
\ No newline at end of file |
