From 05c17c6e0e8e5e9015da4d1396587c4af0ea09d3 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sun, 10 Apr 2016 21:39:05 -0400 Subject: Removed old code that wasn't being used --- .../dicelang/old/ast/DiceASTDefinedChecker.java | 61 ---- .../bjc/dicelang/old/ast/DiceASTExpression.java | 332 --------------------- .../bjc/dicelang/old/ast/DiceASTFlattener.java | 145 --------- .../java/bjc/dicelang/old/ast/DiceASTInliner.java | 138 --------- .../java/bjc/dicelang/old/ast/DiceASTParser.java | 119 -------- .../dicelang/old/ast/DiceASTReferenceChecker.java | 60 ---- .../bjc/dicelang/old/ast/IOperatorCollapser.java | 18 -- .../bjc/dicelang/old/ast/nodes/DiceASTType.java | 27 -- .../bjc/dicelang/old/ast/nodes/IDiceASTNode.java | 23 -- .../dicelang/old/ast/nodes/LiteralDiceNode.java | 217 -------------- .../dicelang/old/ast/nodes/OperatorDiceNode.java | 92 ------ .../dicelang/old/ast/nodes/VariableDiceNode.java | 101 ------- .../bjc/dicelang/old/ast/nodes/package-info.java | 6 - .../old/ast/optimization/DiceASTOptimizer.java | 209 ------------- .../old/ast/optimization/package-info.java | 6 - 15 files changed, 1554 deletions(-) delete mode 100644 dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTDefinedChecker.java delete mode 100644 dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTExpression.java delete mode 100644 dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTFlattener.java delete mode 100644 dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTInliner.java delete mode 100644 dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTParser.java delete mode 100644 dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTReferenceChecker.java delete mode 100644 dice-lang/src/main/java/bjc/dicelang/old/ast/IOperatorCollapser.java delete mode 100644 dice-lang/src/main/java/bjc/dicelang/old/ast/nodes/DiceASTType.java delete mode 100644 dice-lang/src/main/java/bjc/dicelang/old/ast/nodes/IDiceASTNode.java delete mode 100644 dice-lang/src/main/java/bjc/dicelang/old/ast/nodes/LiteralDiceNode.java delete mode 100644 dice-lang/src/main/java/bjc/dicelang/old/ast/nodes/OperatorDiceNode.java delete mode 100644 dice-lang/src/main/java/bjc/dicelang/old/ast/nodes/VariableDiceNode.java delete mode 100644 dice-lang/src/main/java/bjc/dicelang/old/ast/nodes/package-info.java delete mode 100644 dice-lang/src/main/java/bjc/dicelang/old/ast/optimization/DiceASTOptimizer.java delete mode 100644 dice-lang/src/main/java/bjc/dicelang/old/ast/optimization/package-info.java (limited to 'dice-lang/src/main/java/bjc/dicelang/old') diff --git a/dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTDefinedChecker.java b/dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTDefinedChecker.java deleted file mode 100644 index e279d8e..0000000 --- a/dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTDefinedChecker.java +++ /dev/null @@ -1,61 +0,0 @@ -package bjc.dicelang.old.ast; - -import java.util.Map; -import java.util.function.Consumer; - -import bjc.dicelang.old.ast.nodes.DiceASTType; -import bjc.dicelang.old.ast.nodes.IDiceASTNode; -import bjc.dicelang.old.ast.nodes.VariableDiceNode; -import bjc.utils.data.IHolder; - -/** - * Check if the specified node references a particular variable - * - * @author ben - * - */ -public final class DiceASTDefinedChecker - implements Consumer { - /** - * This is true if the specified node references the set variable - */ - private IHolder referencesVariable; - - private Map enviroment; - - /** - * Create a new reference checker - * - * @param referencesVar - * The holder of whether the variable is referenced or not - * @param env - * The enviroment to check undefinedness against - */ - public DiceASTDefinedChecker(IHolder referencesVar, - Map env) { - this.referencesVariable = referencesVar; - this.enviroment = env; - } - - @Override - public void accept(IDiceASTNode astNode) { - referencesVariable.transform((bool) -> checkUndefined(astNode)); - } - - /** - * Check if a given AST node references an undefined variable - * - * @param astNode - * The node to check - * @return Whether or not the node directly the variable - */ - private boolean checkUndefined(IDiceASTNode astNode) { - if (astNode.getType() == DiceASTType.VARIABLE) { - VariableDiceNode node = (VariableDiceNode) astNode; - - return !enviroment.containsKey(node.getVariable()); - } - - return false; - } -} \ No newline at end of file diff --git a/dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTExpression.java b/dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTExpression.java deleted file mode 100644 index e6dca9e..0000000 --- a/dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTExpression.java +++ /dev/null @@ -1,332 +0,0 @@ -package bjc.dicelang.old.ast; - -import java.util.HashMap; -import java.util.Map; -import java.util.function.BinaryOperator; -import java.util.function.Function; - -import bjc.dicelang.ComplexDice; -import bjc.dicelang.IDiceExpression; -import bjc.dicelang.old.ast.nodes.DiceASTType; -import bjc.dicelang.old.ast.nodes.IDiceASTNode; -import bjc.dicelang.old.ast.nodes.LiteralDiceNode; -import bjc.dicelang.old.ast.nodes.OperatorDiceNode; -import bjc.dicelang.old.ast.nodes.VariableDiceNode; -import bjc.utils.data.GenHolder; -import bjc.utils.data.Pair; -import bjc.utils.funcdata.bst.ITreePart.TreeLinearizationMethod; -import bjc.utils.parserutils.AST; - -/** - * An implementation of {@link IDiceExpression} backed by an AST of - * {@link IDiceASTNode}s - * - * @author ben - * - */ -public class DiceASTExpression implements IDiceExpression { - private static final class ArithmeticCollapser - implements IOperatorCollapser { - private OperatorDiceNode type; - - private BinaryOperator valueOp; - - public ArithmeticCollapser(OperatorDiceNode type, - BinaryOperator valueOp) { - this.type = type; - this.valueOp = valueOp; - } - - @Override - public Pair> apply( - Pair> leftNode, - Pair> rightNode) { - return leftNode.merge((leftValue, leftAST) -> { - return rightNode.merge((rightValue, rightAST) -> { - if (type == OperatorDiceNode.DIVIDE - && rightValue == 0) { - throw new ArithmeticException( - "Attempted to divide by zero. The AST of the problem expression is " - + rightAST); - } - - return new Pair<>(valueOp.apply(leftValue, rightValue), - new AST<>(type, leftAST, rightAST)); - }); - }); - } - } - - private static final class VariableRetriever - implements Function { - @Override - public String apply(IDiceASTNode node) { - if (node.getType() != DiceASTType.VARIABLE) { - throw new UnsupportedOperationException( - "Attempted to assign to something that isn't a variable." - + " This isn't supported yet. The problem node is " - + node); - } - - return ((VariableDiceNode) node).getVariable(); - } - } - - /** - * Build the map of operations to use when collapsing the AST - * - * @param enviroment - * The enviroment to evaluate bindings and such against - * @return The operations to use when collapsing the AST - */ - private static Map buildOperations( - Map enviroment) { - Map operatorCollapsers = - new HashMap<>(); - - operatorCollapsers.put(OperatorDiceNode.ADD, - new ArithmeticCollapser(OperatorDiceNode.ADD, - (left, right) -> left + right)); - - operatorCollapsers.put(OperatorDiceNode.SUBTRACT, - new ArithmeticCollapser(OperatorDiceNode.SUBTRACT, - (left, right) -> left - right)); - - operatorCollapsers.put(OperatorDiceNode.MULTIPLY, - new ArithmeticCollapser(OperatorDiceNode.MULTIPLY, - (left, right) -> left * right)); - - operatorCollapsers.put(OperatorDiceNode.DIVIDE, - new ArithmeticCollapser(OperatorDiceNode.DIVIDE, - (left, right) -> left / right)); - - operatorCollapsers.put(OperatorDiceNode.ASSIGN, (left, right) -> { - return parseBinding(enviroment, left, right); - }); - - operatorCollapsers.put(OperatorDiceNode.COMPOUND, - DiceASTExpression::parseCompound); - - operatorCollapsers.put(OperatorDiceNode.GROUP, - DiceASTExpression::parseGroup); - - operatorCollapsers.put(OperatorDiceNode.LET, (left, right) -> { - return doLet(left, right); - }); - - return operatorCollapsers; - } - - private static Pair> doLet( - Pair> left, - Pair> right) { - return left.merge((leftValue, leftAST) -> { - return right.merge((rightValue, rightAST) -> { - if (!leftAST - .applyToHead(DiceASTExpression::isAssignNode)) { - // Just ignore the left block then - return new Pair<>(rightValue, rightAST); - } - - return null; - }); - }); - } - - private static Boolean isAssignNode(IDiceASTNode node) { - return node.getType() == DiceASTType.OPERATOR - && node == OperatorDiceNode.ASSIGN; - } - - private static Pair> parseBinding( - Map enviroment, - Pair> left, - Pair> right) { - return left.merge((leftValue, leftAST) -> { - return right.merge((rightValue, rightAST) -> { - String variableName = leftAST - .collapse(new VariableRetriever(), (operator) -> { - throw new UnsupportedOperationException( - "Can only assign to plain variable names. The problem operator is " - + operator); - }, (returnedAST) -> returnedAST); - - GenHolder selfReference = new GenHolder<>(false); - - DiceASTReferenceChecker refChecker = - new DiceASTReferenceChecker(selfReference, - variableName); - - rightAST.traverse(TreeLinearizationMethod.PREORDER, - refChecker); - - // Ignore meta-variable that'll be auto-frozen to restore - // definition sanity - if (selfReference.unwrap((bool) -> bool) - && !variableName.equals("last")) { - throw new UnsupportedOperationException( - "Variable '" + variableName - + "' references itself. Problematic definition: \n\t" - + rightAST); - } - - if (!variableName.equals("last")) { - enviroment.put(variableName, - new DiceASTExpression(rightAST, enviroment)); - } else { - // Do nothing, last is a auto-handled meta-variable - } - - return new Pair<>(rightValue, new AST<>( - OperatorDiceNode.ASSIGN, leftAST, rightAST)); - }); - }); - } - - private static Pair> parseCompound( - Pair> leftNode, - Pair> rightNode) { - return leftNode.merge((leftValue, leftAST) -> { - return rightNode.merge((rightValue, rightAST) -> { - int compoundValue = - Integer.parseInt(Integer.toString(leftValue) - + Integer.toString(rightValue)); - - return new Pair<>(compoundValue, new AST<>( - OperatorDiceNode.COMPOUND, leftAST, rightAST)); - }); - }); - } - - private static Pair> parseGroup( - Pair> leftNode, - Pair> rightNode) { - return leftNode.merge((leftValue, leftAST) -> { - return rightNode.merge((rightValue, rightAST) -> { - if (leftValue < 0) { - throw new UnsupportedOperationException( - "Can't attempt to roll a negative number of dice." - + " The problematic AST is " - + leftAST); - } else if (rightValue < 1) { - throw new UnsupportedOperationException( - "Can't roll dice with less than one side." - + " The problematic AST is " - + rightAST); - } - - int rolledValue = - new ComplexDice(leftValue, rightValue).roll(); - - return new Pair<>(rolledValue, new AST<>( - OperatorDiceNode.GROUP, leftAST, rightAST)); - }); - }); - } - - /** - * The AST this expression will evaluate - */ - private AST ast; - - /** - * The enviroment to evaluate bindings and such against - */ - private Map env; - - /** - * Create a new dice expression backed by an AST - * - * @param ast - * The AST backing this expression - * @param env - * The enviroment to evaluate bindings against - */ - public DiceASTExpression(AST ast, - Map env) { - this.ast = ast; - this.env = env; - } - - /** - * Expand a leaf AST token into a pair for evaluation - * - * @param leafNode - * The token to evaluate - * @return A pair consisting of the token's value and the AST it - * represents - */ - private Pair> evaluateLeaf( - IDiceASTNode leafNode) { - if (leafNode.getType() == DiceASTType.VARIABLE) { - VariableDiceNode node = (VariableDiceNode) leafNode; - - return parseVariable(node); - } else if (leafNode.getType() == DiceASTType.LITERAL) { - LiteralDiceNode node = (LiteralDiceNode) leafNode; - - return node.toParseValue(); - } else { - throw new UnsupportedOperationException("Found leaf operator " - + leafNode + ". These aren't supported."); - } - } - - private Pair> parseVariable( - VariableDiceNode leafNode) { - String varName = leafNode.getVariable(); - - if (env.containsKey(varName)) { - return new Pair<>(env.get(varName).roll(), - new AST<>(leafNode)); - } - - // Handle special case for defining variables - return new Pair<>(0, new AST<>(leafNode)); - } - - /** - * Get the AST bound to this expression - * - * @return the ast - */ - public AST getAst() { - return ast; - } - - /* - * (non-Javadoc) - * - * @see bjc.utils.dice.IDiceExpression#roll() - */ - @Override - public int roll() { - Map operations = - buildOperations(env); - - return ast.collapse(this::evaluateLeaf, operations::get, - (returnedValue) -> returnedValue - .merge((left, right) -> left)); - } - - /* - * (non-Javadoc) - * - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - return ast.toString(); - } - - @Override - public int optimize() { - throw new UnsupportedOperationException( - "Use DiceASTOptimizer for optimizing these"); - } - - @Override - public boolean canOptimize() { - return false; - } -} \ No newline at end of file diff --git a/dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTFlattener.java b/dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTFlattener.java deleted file mode 100644 index 39c0797..0000000 --- a/dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTFlattener.java +++ /dev/null @@ -1,145 +0,0 @@ -package bjc.dicelang.old.ast; - -import java.util.HashMap; -import java.util.Map; -import java.util.function.BinaryOperator; -import java.util.function.Function; - -import org.apache.commons.lang3.StringUtils; - -import bjc.dicelang.BindingDiceExpression; -import bjc.dicelang.ComplexDice; -import bjc.dicelang.CompoundDice; -import bjc.dicelang.OperatorDiceExpression; -import bjc.dicelang.DiceExpressionType; -import bjc.dicelang.IDiceExpression; -import bjc.dicelang.ReferenceDiceExpression; -import bjc.dicelang.ScalarDie; -import bjc.dicelang.old.ast.nodes.DiceASTType; -import bjc.dicelang.old.ast.nodes.IDiceASTNode; -import bjc.dicelang.old.ast.nodes.LiteralDiceNode; -import bjc.dicelang.old.ast.nodes.OperatorDiceNode; -import bjc.dicelang.old.ast.nodes.VariableDiceNode; -import bjc.utils.parserutils.AST; - -/** - * Flatten an {@link AST} of {@link IDiceASTNode} into a - * {@link IDiceExpression} - * - * @author ben - * - */ -public class DiceASTFlattener { - private static final class NodeCollapser - implements Function { - private Map enviroment; - - public NodeCollapser(Map env) { - this.enviroment = env; - } - - @Override - public IDiceExpression apply(IDiceASTNode nod) { - if (nod.getType() == DiceASTType.LITERAL) { - return expFromLiteral((LiteralDiceNode) nod); - } else if (nod.getType() == DiceASTType.VARIABLE) { - String varName = ((VariableDiceNode) nod).getVariable(); - - return new ReferenceDiceExpression(varName, enviroment); - } else { - throw new UnsupportedOperationException( - "Attempted to flatten something that can't be" - + " flattened. The culprit is " + nod); - } - } - - /** - * Create a dice expression from a literal token - * - * @param tok - * The token to convert to an expression - * @return The dice expression represented by the token - */ - private static IDiceExpression - expFromLiteral(LiteralDiceNode tok) { - String data = tok.getData(); - - if (data.equals("")) { - throw new UnsupportedOperationException( - "Can't convert a blank token into a literal"); - } - - if (StringUtils.countMatches(data, 'c') == 1 - && !data.equalsIgnoreCase("c")) { - String[] strangs = data.split("c"); - - return new CompoundDice(ComplexDice.fromString(strangs[0]), - ComplexDice.fromString(strangs[1])); - } else if (StringUtils.countMatches(data, 'd') == 1 - && !data.equalsIgnoreCase("d")) { - return ComplexDice.fromString(data); - } else { - return new ScalarDie(Integer.parseInt(data)); - } - } - } - - /** - * Build the operations to use for tree flattening - * - * @param env - * The enviroment the tree will be flattened against - * @return The operations needed for tree flattening - */ - private static Map> - buildOperations(Map env) { - Map> opCollapsers = - new HashMap<>(); - - opCollapsers.put(OperatorDiceNode.ADD, (left, right) -> { - return new OperatorDiceExpression(right, left, - DiceExpressionType.ADD); - }); - opCollapsers.put(OperatorDiceNode.SUBTRACT, (left, right) -> { - return new OperatorDiceExpression(right, left, - DiceExpressionType.SUBTRACT); - }); - opCollapsers.put(OperatorDiceNode.MULTIPLY, (left, right) -> { - return new OperatorDiceExpression(right, left, - DiceExpressionType.MULTIPLY); - }); - opCollapsers.put(OperatorDiceNode.DIVIDE, (left, right) -> { - return new OperatorDiceExpression(right, left, - DiceExpressionType.DIVIDE); - }); - opCollapsers.put(OperatorDiceNode.ASSIGN, (left, right) -> { - return new BindingDiceExpression(left, right, env); - }); - opCollapsers.put(OperatorDiceNode.COMPOUND, (left, right) -> { - return new CompoundDice(left, right); - }); - opCollapsers.put(OperatorDiceNode.GROUP, (left, right) -> { - return new ComplexDice(left, right); - }); - - return opCollapsers; - } - - /** - * Flatten a AST into a dice expression - * - * @param ast - * The AST to flatten - * @param env - * The enviroment to flatten against - * @return The AST, flattened into a dice expression - */ - public static IDiceExpression flatten(AST ast, - Map env) { - Map> opCollapsers = - buildOperations(env); - - return ast.collapse(new NodeCollapser(env), opCollapsers::get, - (r) -> r); - } -} diff --git a/dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTInliner.java b/dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTInliner.java deleted file mode 100644 index 802741f..0000000 --- a/dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTInliner.java +++ /dev/null @@ -1,138 +0,0 @@ -package bjc.dicelang.old.ast; - -import java.util.function.Function; - -import bjc.dicelang.old.ast.nodes.DiceASTType; -import bjc.dicelang.old.ast.nodes.IDiceASTNode; -import bjc.dicelang.old.ast.nodes.VariableDiceNode; -import bjc.utils.funcdata.FunctionalList; -import bjc.utils.funcdata.FunctionalMap; -import bjc.utils.funcdata.IFunctionalList; -import bjc.utils.funcdata.IFunctionalMap; -import bjc.utils.parserutils.AST; - -/** - * Inline references in a dice AST, replacing variable references with what - * the variables refer to - * - * @author ben - * - */ -public class DiceASTInliner { - private static class NodeInliner - implements Function> { - private IFunctionalMap> enviroment; - - public NodeInliner(IFunctionalMap> env) { - enviroment = env; - } - - @Override - public AST apply(IDiceASTNode nod) { - if (nod.getType() == DiceASTType.VARIABLE) { - return expandNode((VariableDiceNode) nod); - } - - return new AST<>(nod); - } - - protected AST expandNode( - VariableDiceNode variableNode) { - String varName = variableNode.getVariable(); - - if (!enviroment.containsKey(varName)) { - throw new IllegalArgumentException( - "Attempted to freeze reference" - + " to an undefined variable " + varName); - } - - return enviroment.get(varName); - } - } - - private static final class SelectiveInliner extends NodeInliner { - - private IFunctionalList variableNames; - - public SelectiveInliner( - IFunctionalMap> env, - IFunctionalList varNames) { - super(env); - - variableNames = varNames; - } - - @Override - protected AST expandNode( - VariableDiceNode variableNode) { - if (variableNames.contains(variableNode.getVariable())) { - return super.expandNode(variableNode); - } - - return new AST<>(variableNode); - } - } - - /** - * Inline the references in an AST - * - * @param tree - * The tree to inline references in - * @param env - * The enviroment to get reference values from - * @return The tree with references inlined - */ - public static AST inlineAST(AST tree, - IFunctionalMap> env) { - return selectiveInline(tree, env); - } - - /** - * Inline the references in an expression backed by an AST - * - * @param tree - * The tree-backed expression to inline references in - * @param env - * The enviroment to get reference values from - * @return The tree with references inlined - */ - public static AST inlineAST(DiceASTExpression tree, - FunctionalMap env) { - return inlineAST(tree.getAst(), - env.mapValues(expression -> expression.getAst())); - } - - /** - * Inline references to specified variables - * - * @param tree - * The tree-backed expression to inline references in - * @param env - * The enviroment to resolve variables against - * @param varNames - * The names of the variables to inline - * @return An AST with the specified variables inlined - */ - public static AST selectiveInline(AST tree, - IFunctionalMap> env, - String... varNames) { - return selectiveInline(tree, env, new FunctionalList<>(varNames)); - } - - /** - * Inline references to specified variables - * - * @param tree - * The tree-backed expression to inline references in - * @param env - * The enviroment to resolve variables against - * @param varNames - * The names of the variables to inline - * @return An AST with the specified variables inline - */ - public static AST selectiveInline(AST tree, - IFunctionalMap> env, - IFunctionalList varNames) { - return tree.flatMapTree(new SelectiveInliner(env, varNames)); - } -} \ No newline at end of file diff --git a/dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTParser.java b/dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTParser.java deleted file mode 100644 index c9b48c8..0000000 --- a/dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTParser.java +++ /dev/null @@ -1,119 +0,0 @@ -package bjc.dicelang.old.ast; - -import java.util.Deque; -import java.util.LinkedList; -import java.util.function.Function; - -import bjc.dicelang.old.ast.nodes.IDiceASTNode; -import bjc.dicelang.old.ast.nodes.LiteralDiceNode; -import bjc.dicelang.old.ast.nodes.OperatorDiceNode; -import bjc.dicelang.old.ast.nodes.VariableDiceNode; -import bjc.utils.data.IPair; -import bjc.utils.data.Pair; -import bjc.utils.funcdata.FunctionalStringTokenizer; -import bjc.utils.funcdata.IFunctionalList; -import bjc.utils.funcutils.ListUtils; -import bjc.utils.parserutils.AST; -import bjc.utils.parserutils.ShuntingYard; -import bjc.utils.parserutils.TreeConstructor; - -/** - * Create an AST from a string expression - * - * @author ben - * - */ -public class DiceASTParser { - private static final class NodeBaker - implements Function { - @Override - public IDiceASTNode apply(String tok) { - if (isOperator(tok)) { - return OperatorDiceNode.fromString(tok); - } else if (LiteralDiceNode.isLiteral(tok)) { - return new LiteralDiceNode(tok); - } else { - return new VariableDiceNode(tok); - } - } - } - - /** - * The yard to use for shunting expressions - */ - private static ShuntingYard yard; - - static { - yard = new ShuntingYard<>(); - - yard.addOp("d", 5); // dice operator: use for creating variable - // size dice groups - yard.addOp("c", 6); // compound operator: use for creating compound - // dice from expressions - yard.addOp(":=", 0); // binding operator: Bind a name to a variable - // expression - } - - /** - * Build an AST from a string expression - * - * @param exp - * The string to build from - * @return An AST built from the passed in string - */ - public static AST buildAST(String exp) { - IFunctionalList tokens = FunctionalStringTokenizer - .fromString(exp).toList(); - - Deque> ops = new LinkedList<>(); - - ops.add(new Pair<>("+", "\\+")); - ops.add(new Pair<>("-", "-")); - ops.add(new Pair<>("*", "\\*")); - ops.add(new Pair<>("/", "/")); - ops.add(new Pair<>(":=", ":=")); - - IFunctionalList semiExpandedTokens = ListUtils - .splitTokens(tokens, ops); - - ops = new LinkedList<>(); - - ops.add(new Pair<>("(", "\\(")); - ops.add(new Pair<>(")", "\\)")); - - IFunctionalList fullyExpandedTokens = ListUtils - .deAffixTokens(semiExpandedTokens, ops); - - IFunctionalList shunted = yard.postfix(fullyExpandedTokens, - (s) -> s); - - AST rawAST = TreeConstructor.constructTree(shunted, - DiceASTParser::isOperator); - - AST bakedAST = rawAST.transmuteAST(new NodeBaker()); - - return bakedAST; - } - - /** - * Check if a token represents an operator - * - * @param tok - * The token to check if it represents an operator - * @return Whether or not the token represents an operator - */ - private static boolean isOperator(String tok) { - switch (tok) { - case ":=": - case "+": - case "-": - case "*": - case "/": - case "c": - case "d": - return true; - default: - return false; - } - } -} diff --git a/dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTReferenceChecker.java b/dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTReferenceChecker.java deleted file mode 100644 index 3e0ceec..0000000 --- a/dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTReferenceChecker.java +++ /dev/null @@ -1,60 +0,0 @@ -package bjc.dicelang.old.ast; - -import java.util.function.Consumer; - -import bjc.dicelang.old.ast.nodes.DiceASTType; -import bjc.dicelang.old.ast.nodes.IDiceASTNode; -import bjc.dicelang.old.ast.nodes.VariableDiceNode; -import bjc.utils.data.IHolder; - -/** - * Check if the specified node references a particular variable - * - * @author ben - * - */ -public final class DiceASTReferenceChecker - implements Consumer { - /** - * This is true if the specified node references the set variable - */ - private IHolder referencesVariable; - - private String varName; - - /** - * Create a new reference checker - * - * @param referencesVar - * The holder of whether the variable is referenced or not - * @param varName - * The variable to check for references in - */ - public DiceASTReferenceChecker(IHolder referencesVar, - String varName) { - this.referencesVariable = referencesVar; - this.varName = varName; - } - - @Override - public void accept(IDiceASTNode astNode) { - referencesVariable.transform((bool) -> isDirectReference(astNode)); - } - - /** - * Check if a given AST node directly references the specified variable - * - * @param astNode - * The node to check - * @return Whether or not the node directly the variable - */ - private boolean isDirectReference(IDiceASTNode astNode) { - if (astNode.getType() == DiceASTType.VARIABLE) { - VariableDiceNode node = (VariableDiceNode) astNode; - - return node.getVariable().equals(varName); - } - - return false; - } -} \ No newline at end of file diff --git a/dice-lang/src/main/java/bjc/dicelang/old/ast/IOperatorCollapser.java b/dice-lang/src/main/java/bjc/dicelang/old/ast/IOperatorCollapser.java deleted file mode 100644 index 36243a6..0000000 --- a/dice-lang/src/main/java/bjc/dicelang/old/ast/IOperatorCollapser.java +++ /dev/null @@ -1,18 +0,0 @@ -package bjc.dicelang.old.ast; - -import java.util.function.BinaryOperator; - -import bjc.dicelang.old.ast.nodes.IDiceASTNode; -import bjc.utils.data.Pair; -import bjc.utils.parserutils.AST; - -/** - * Alias for operator collapsers. Because 68-char types are too long - * - * @author ben - * - */ -public interface IOperatorCollapser - extends BinaryOperator>> { - // Just an alias -} 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 deleted file mode 100644 index 633d1d9..0000000 --- a/dice-lang/src/main/java/bjc/dicelang/old/ast/nodes/DiceASTType.java +++ /dev/null @@ -1,27 +0,0 @@ -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 deleted file mode 100644 index 579c595..0000000 --- a/dice-lang/src/main/java/bjc/dicelang/old/ast/nodes/IDiceASTNode.java +++ /dev/null @@ -1,23 +0,0 @@ -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 deleted file mode 100644 index 46c84d0..0000000 --- a/dice-lang/src/main/java/bjc/dicelang/old/ast/nodes/LiteralDiceNode.java +++ /dev/null @@ -1,217 +0,0 @@ -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> toParseValue() { - AST 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 deleted file mode 100644 index 3c06553..0000000 --- a/dice-lang/src/main/java/bjc/dicelang/old/ast/nodes/OperatorDiceNode.java +++ /dev/null @@ -1,92 +0,0 @@ -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 deleted file mode 100644 index 262f99b..0000000 --- a/dice-lang/src/main/java/bjc/dicelang/old/ast/nodes/VariableDiceNode.java +++ /dev/null @@ -1,101 +0,0 @@ -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 deleted file mode 100644 index f99776f..0000000 --- a/dice-lang/src/main/java/bjc/dicelang/old/ast/nodes/package-info.java +++ /dev/null @@ -1,6 +0,0 @@ -/** - * 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 diff --git a/dice-lang/src/main/java/bjc/dicelang/old/ast/optimization/DiceASTOptimizer.java b/dice-lang/src/main/java/bjc/dicelang/old/ast/optimization/DiceASTOptimizer.java deleted file mode 100644 index dead812..0000000 --- a/dice-lang/src/main/java/bjc/dicelang/old/ast/optimization/DiceASTOptimizer.java +++ /dev/null @@ -1,209 +0,0 @@ -package bjc.dicelang.old.ast.optimization; - -import static bjc.dicelang.old.ast.nodes.DiceASTType.*; - -import java.util.HashMap; -import java.util.Map; -import java.util.function.BiFunction; -import java.util.function.BinaryOperator; - -import bjc.dicelang.IDiceExpression; -import bjc.dicelang.old.ast.nodes.DiceASTType; -import bjc.dicelang.old.ast.nodes.IDiceASTNode; -import bjc.dicelang.old.ast.nodes.LiteralDiceNode; -import bjc.dicelang.old.ast.nodes.OperatorDiceNode; -import bjc.utils.parserutils.AST; - -/** - * Optimize an AST - * - * @author ben - * - */ -public class DiceASTOptimizer { - private static final class NestedArithmeticOperationCollapser - implements BinaryOperator> { - private IDiceASTNode type; - private BiFunction valueCollapser; - - public NestedArithmeticOperationCollapser(IDiceASTNode type, - BiFunction valueCollapser) { - this.type = type; - this.valueCollapser = valueCollapser; - } - - @Override - public AST apply(AST leftAST, - AST rightAST) { - AST rightBranchOfLeftAST = leftAST - .applyToRight((rightSideAST) -> rightSideAST); - AST leftBranchOfLeftAST = leftAST - .applyToRight((rightSideAST) -> rightSideAST); - - boolean leftContainsNestedConstant = DiceASTOptimizer - .checkNodeType(rightBranchOfLeftAST, LITERAL) - && DiceASTOptimizer.isNodeConstant(leftAST); - - boolean isRightConstant = DiceASTOptimizer - .checkNodeType(rightAST, LITERAL) - && DiceASTOptimizer.isNodeConstant(leftAST); - - if (leftContainsNestedConstant && isRightConstant) { - int combinedValue = valueCollapser.apply( - getNodeValue(rightBranchOfLeftAST), - getNodeValue(rightAST)); - - AST newRightBranch = new AST<>( - new LiteralDiceNode(combinedValue)); - - return new AST<>(type, leftBranchOfLeftAST, - newRightBranch); - } - - return new AST<>(type, leftAST, rightAST); - } - } - - private static final class ArithmeticOperationCollapser - implements BinaryOperator> { - private IDiceASTNode type; - private BiFunction valueCollapser; - private boolean doSwap; - - public ArithmeticOperationCollapser(IDiceASTNode type, - BiFunction valueCollapser, - boolean doSwap) { - this.type = type; - this.valueCollapser = valueCollapser; - this.doSwap = doSwap; - } - - @Override - public AST apply(AST leftAST, - AST rightAST) { - boolean isLeftConstant = DiceASTOptimizer - .checkNodeType(leftAST, LITERAL) - && DiceASTOptimizer.isNodeConstant(leftAST); - - boolean isRightConstant = DiceASTOptimizer - .checkNodeType(rightAST, LITERAL) - && DiceASTOptimizer.isNodeConstant(leftAST); - - if (isLeftConstant) { - if (isRightConstant) { - int combinedValue = valueCollapser.apply( - getNodeValue(leftAST), getNodeValue(rightAST)); - - return new AST<>(new LiteralDiceNode(combinedValue)); - } - - if (doSwap) { - return new AST<>(type, rightAST, leftAST); - } - } - - return new AST<>(type, leftAST, rightAST); - } - } - - private static Map>> buildConstantCollapsers() { - Map>> operatorCollapsers = new HashMap<>(); - - operatorCollapsers.put(OperatorDiceNode.ADD, - new ArithmeticOperationCollapser(OperatorDiceNode.ADD, - (leftVal, rightVal) -> leftVal + rightVal, true)); - - operatorCollapsers.put(OperatorDiceNode.MULTIPLY, - new ArithmeticOperationCollapser(OperatorDiceNode.MULTIPLY, - (leftVal, rightVal) -> leftVal * rightVal, true)); - - operatorCollapsers.put(OperatorDiceNode.SUBTRACT, - new ArithmeticOperationCollapser(OperatorDiceNode.SUBTRACT, - (leftVal, rightVal) -> leftVal - rightVal, false)); - - operatorCollapsers.put(OperatorDiceNode.DIVIDE, - new ArithmeticOperationCollapser(OperatorDiceNode.DIVIDE, - (leftVal, rightVal) -> leftVal / rightVal, false)); - - return operatorCollapsers; - } - - private static Map>> buildNestedConstantCollapsers() { - Map>> operatorCollapsers = new HashMap<>(); - - operatorCollapsers.put(OperatorDiceNode.ADD, - new NestedArithmeticOperationCollapser( - OperatorDiceNode.ADD, - (leftVal, rightVal) -> leftVal + rightVal)); - - operatorCollapsers.put(OperatorDiceNode.MULTIPLY, - new NestedArithmeticOperationCollapser( - OperatorDiceNode.MULTIPLY, - (leftVal, rightVal) -> leftVal * rightVal)); - - return operatorCollapsers; - } - - private static AST collapseLeaf(IDiceASTNode leaf) { - // Can't optimize a simple reference - if (leaf.getType() == VARIABLE) { - return new AST<>(leaf); - } else if (leaf.getType() == LITERAL) { - LiteralDiceNode node = (LiteralDiceNode) leaf; - - return new AST<>(optimizeLiteral(node, node.toExpression())); - } else { - throw new UnsupportedOperationException( - "Found leaf operator. This isn't supported"); - } - } - - private static IDiceASTNode optimizeLiteral(LiteralDiceNode node, - IDiceExpression leaf) { - if (leaf.canOptimize()) { - return new LiteralDiceNode(Integer.toString(leaf.optimize())); - } - - return node; - } - - private static AST finishTree(AST tree) { - return tree; - } - - /** - * Optimize a tree of expressions into a simpler form - * - * @param tree - * The tree to optimize - * @return The optimized tree - */ - public static AST optimizeTree(AST tree) { - AST astWithConstantsFolded = tree.collapse( - DiceASTOptimizer::collapseLeaf, - buildConstantCollapsers()::get, - DiceASTOptimizer::finishTree); - - AST astWithNestedConstantsFolded = astWithConstantsFolded - .collapse(DiceASTOptimizer::collapseLeaf, - buildNestedConstantCollapsers()::get, - DiceASTOptimizer::finishTree); - - return astWithNestedConstantsFolded; - } - - private static boolean checkNodeType(AST ast, - DiceASTType type) { - return ast.applyToHead((node) -> node.getType()) == type; - } - - private static boolean isNodeConstant(AST ast) { - return ast.applyToHead( - (node) -> ((LiteralDiceNode) node).isConstant()); - } - - private static int getNodeValue(AST ast) { - return ast.applyToHead( - (node) -> ((LiteralDiceNode) node).toConstant()); - } -} diff --git a/dice-lang/src/main/java/bjc/dicelang/old/ast/optimization/package-info.java b/dice-lang/src/main/java/bjc/dicelang/old/ast/optimization/package-info.java deleted file mode 100644 index ef39522..0000000 --- a/dice-lang/src/main/java/bjc/dicelang/old/ast/optimization/package-info.java +++ /dev/null @@ -1,6 +0,0 @@ -/** - * Contains classes for optimizing ASTs - * @author ben - * - */ -package bjc.dicelang.old.ast.optimization; \ No newline at end of file -- cgit v1.2.3