From a7e84eea087a35721a971e827149f0ca5fba4676 Mon Sep 17 00:00:00 2001 From: bjculkin Date: Tue, 21 Mar 2017 14:08:50 -0400 Subject: Remove version 1 files Remove the old, not used version 1 files from the repository. Check the history if you care about them. --- dice-lang/src/bjc/dicelang/v1/ast/.gitignore | 1 - .../bjc/dicelang/v1/ast/ArithmeticCollapser.java | 149 ----------- dice-lang/src/bjc/dicelang/v1/ast/ArrayResult.java | 43 ---- .../src/bjc/dicelang/v1/ast/DiceASTEvaluator.java | 276 --------------------- .../src/bjc/dicelang/v1/ast/DiceASTInliner.java | 112 --------- .../src/bjc/dicelang/v1/ast/DiceASTOptimizer.java | 55 ---- .../src/bjc/dicelang/v1/ast/DiceASTParser.java | 147 ----------- .../dicelang/v1/ast/DiceASTReferenceChecker.java | 59 ----- .../dicelang/v1/ast/DiceASTReferenceSanitizer.java | 176 ------------- .../src/bjc/dicelang/v1/ast/DiceASTUtils.java | 80 ------ dice-lang/src/bjc/dicelang/v1/ast/DummyResult.java | 43 ---- .../bjc/dicelang/v1/ast/IOperatorCollapser.java | 19 -- dice-lang/src/bjc/dicelang/v1/ast/IResult.java | 16 -- .../src/bjc/dicelang/v1/ast/IntegerResult.java | 40 --- dice-lang/src/bjc/dicelang/v1/ast/ResultType.java | 22 -- .../src/bjc/dicelang/v1/ast/nodes/DiceASTType.java | 27 -- .../bjc/dicelang/v1/ast/nodes/DiceLiteralNode.java | 52 ---- .../bjc/dicelang/v1/ast/nodes/DiceLiteralType.java | 18 -- .../dicelang/v1/ast/nodes/DiceOperatorType.java | 29 --- .../bjc/dicelang/v1/ast/nodes/IDiceASTNode.java | 23 -- .../dicelang/v1/ast/nodes/ILiteralDiceNode.java | 71 ------ .../dicelang/v1/ast/nodes/IntegerLiteralNode.java | 50 ---- .../dicelang/v1/ast/nodes/OperatorDiceNode.java | 104 -------- .../dicelang/v1/ast/nodes/VariableDiceNode.java | 96 ------- .../bjc/dicelang/v1/ast/nodes/package-info.java | 7 - .../v1/ast/optimization/ArithmeticCollapser.java | 42 ---- .../v1/ast/optimization/ConstantCollapser.java | 76 ------ .../v1/ast/optimization/IOptimizationPass.java | 33 --- .../v1/ast/optimization/OperationCondenser.java | 96 ------- .../dicelang/v1/ast/optimization/package-info.java | 7 - .../src/bjc/dicelang/v1/ast/package-info.java | 7 - 31 files changed, 1976 deletions(-) delete mode 100644 dice-lang/src/bjc/dicelang/v1/ast/.gitignore delete mode 100644 dice-lang/src/bjc/dicelang/v1/ast/ArithmeticCollapser.java delete mode 100644 dice-lang/src/bjc/dicelang/v1/ast/ArrayResult.java delete mode 100644 dice-lang/src/bjc/dicelang/v1/ast/DiceASTEvaluator.java delete mode 100644 dice-lang/src/bjc/dicelang/v1/ast/DiceASTInliner.java delete mode 100644 dice-lang/src/bjc/dicelang/v1/ast/DiceASTOptimizer.java delete mode 100644 dice-lang/src/bjc/dicelang/v1/ast/DiceASTParser.java delete mode 100644 dice-lang/src/bjc/dicelang/v1/ast/DiceASTReferenceChecker.java delete mode 100644 dice-lang/src/bjc/dicelang/v1/ast/DiceASTReferenceSanitizer.java delete mode 100644 dice-lang/src/bjc/dicelang/v1/ast/DiceASTUtils.java delete mode 100644 dice-lang/src/bjc/dicelang/v1/ast/DummyResult.java delete mode 100644 dice-lang/src/bjc/dicelang/v1/ast/IOperatorCollapser.java delete mode 100644 dice-lang/src/bjc/dicelang/v1/ast/IResult.java delete mode 100644 dice-lang/src/bjc/dicelang/v1/ast/IntegerResult.java delete mode 100644 dice-lang/src/bjc/dicelang/v1/ast/ResultType.java delete mode 100644 dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceASTType.java delete mode 100644 dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceLiteralNode.java delete mode 100644 dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceLiteralType.java delete mode 100644 dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceOperatorType.java delete mode 100644 dice-lang/src/bjc/dicelang/v1/ast/nodes/IDiceASTNode.java delete mode 100644 dice-lang/src/bjc/dicelang/v1/ast/nodes/ILiteralDiceNode.java delete mode 100644 dice-lang/src/bjc/dicelang/v1/ast/nodes/IntegerLiteralNode.java delete mode 100644 dice-lang/src/bjc/dicelang/v1/ast/nodes/OperatorDiceNode.java delete mode 100644 dice-lang/src/bjc/dicelang/v1/ast/nodes/VariableDiceNode.java delete mode 100644 dice-lang/src/bjc/dicelang/v1/ast/nodes/package-info.java delete mode 100644 dice-lang/src/bjc/dicelang/v1/ast/optimization/ArithmeticCollapser.java delete mode 100644 dice-lang/src/bjc/dicelang/v1/ast/optimization/ConstantCollapser.java delete mode 100644 dice-lang/src/bjc/dicelang/v1/ast/optimization/IOptimizationPass.java delete mode 100644 dice-lang/src/bjc/dicelang/v1/ast/optimization/OperationCondenser.java delete mode 100644 dice-lang/src/bjc/dicelang/v1/ast/optimization/package-info.java delete mode 100644 dice-lang/src/bjc/dicelang/v1/ast/package-info.java (limited to 'dice-lang/src/bjc/dicelang/v1/ast') diff --git a/dice-lang/src/bjc/dicelang/v1/ast/.gitignore b/dice-lang/src/bjc/dicelang/v1/ast/.gitignore deleted file mode 100644 index 67877fe..0000000 --- a/dice-lang/src/bjc/dicelang/v1/ast/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/.DiceASTParser.java.swp diff --git a/dice-lang/src/bjc/dicelang/v1/ast/ArithmeticCollapser.java b/dice-lang/src/bjc/dicelang/v1/ast/ArithmeticCollapser.java deleted file mode 100644 index 28f29f0..0000000 --- a/dice-lang/src/bjc/dicelang/v1/ast/ArithmeticCollapser.java +++ /dev/null @@ -1,149 +0,0 @@ -package bjc.dicelang.v1.ast; - -import bjc.dicelang.v1.ast.nodes.IDiceASTNode; -import bjc.dicelang.v1.ast.nodes.OperatorDiceNode; -import bjc.utils.data.IPair; -import bjc.utils.data.ITree; -import bjc.utils.data.Pair; -import bjc.utils.data.Tree; -import bjc.utils.funcdata.IList; - -import java.util.function.BinaryOperator; - -/** - * Responsible for collapsing arithmetic operators - * - * @author ben - * - */ -final class ArithmeticCollapser implements IOperatorCollapser { - // The type of operator we're collapsing - private OperatorDiceNode type; - - // The operator to use to collapse operators - private BinaryOperator valueOp; - - private int initialValue; - - public ArithmeticCollapser(OperatorDiceNode type, BinaryOperator valueOp, int initVal) { - this.type = type; - this.valueOp = valueOp; - this.initialValue = initVal; - } - - @Override - public IPair> apply(IList>> nodes) { - IPair> initialState = new Pair<>(new IntegerResult(initialValue), - new Tree<>(type)); - - BinaryOperator>> reducer = (currentState, accumulatedState) -> { - // Force evaluation of accumulated state to prevent - // certain bugs from occuring - // accumulatedState.merge((l, r) -> null); - - return reduceStates(accumulatedState, currentState); - }; - - IPair> reducedState = nodes.reduceAux(initialState, reducer, - (state) -> state); - - return reducedState; - } - - private IList combineArrayResults(IResult accumulatedValue, IResult currentValue) { - IList currentList = ((ArrayResult) currentValue).getValue(); - IList accumulatedList = ((ArrayResult) accumulatedValue).getValue(); - - if(currentList.getSize() != accumulatedList.getSize()) - throw new UnsupportedOperationException("Can only apply operations to equal-length arrays"); - - IList resultList = currentList.combineWith(accumulatedList, (currentNode, accumulatedNode) -> { - boolean currentNotInt = currentNode.getType() != ResultType.INTEGER; - boolean accumulatedNotInt = accumulatedNode.getType() != ResultType.INTEGER; - - if(currentNotInt || accumulatedNotInt) - throw new UnsupportedOperationException("Nesting of array operations isn't allowed"); - - int accumulatedInt = ((IntegerResult) accumulatedNode).getValue(); - int currentInt = ((IntegerResult) currentNode).getValue(); - - IResult combinedValue = new IntegerResult(valueOp.apply(accumulatedInt, currentInt)); - return combinedValue; - }); - return resultList; - } - - private IPair> doArithmeticCollapse(IResult accumulatedValue, - ITree accumulatedTree, IResult currentValue) { - if(accumulatedValue.getType() == ResultType.DUMMY || currentValue.getType() == ResultType.DUMMY) { - DummyResult result = new DummyResult("Found dummy result with either accumulated dummy (" - + ((DummyResult) accumulatedValue).getData() + ") or current dummy (" - + ((DummyResult) currentValue).getData() + ")."); - - return new Pair<>(result, accumulatedTree); - } - - boolean currentIsInt = currentValue.getType() == ResultType.INTEGER; - boolean accumulatedIsInt = accumulatedValue.getType() == ResultType.INTEGER; - - if(!currentIsInt) { - if(!accumulatedIsInt) { - IList resultList = combineArrayResults(accumulatedValue, currentValue); - - return new Pair<>(new ArrayResult(resultList), accumulatedTree); - } - - IList resultList = halfCombineLists(((ArrayResult) currentValue).getValue(), - accumulatedValue, true); - - return new Pair<>(new ArrayResult(resultList), accumulatedTree); - } else if(!accumulatedIsInt) { - IList resultList = halfCombineLists(((ArrayResult) accumulatedValue).getValue(), - currentValue, false); - - return new Pair<>(new ArrayResult(resultList), accumulatedTree); - } - - int accumulatedInt = ((IntegerResult) accumulatedValue).getValue(); - int currentInt = ((IntegerResult) currentValue).getValue(); - - int combinedValue = valueOp.apply(accumulatedInt, currentInt); - - return new Pair<>(new IntegerResult(combinedValue), accumulatedTree); - } - - private IList halfCombineLists(IList list, IResult scalar, boolean scalarLeft) { - if(scalar.getType() != ResultType.INTEGER) - throw new UnsupportedOperationException("Nested array operations not supported"); - - int scalarInt = ((IntegerResult) scalar).getValue(); - - return list.map((element) -> { - if(element.getType() != ResultType.INTEGER) - throw new UnsupportedOperationException("Nested array operations not supported"); - - int elementInt = ((IntegerResult) element).getValue(); - - IResult combinedValue; - - if(scalarLeft) { - combinedValue = new IntegerResult(valueOp.apply(scalarInt, elementInt)); - } else { - combinedValue = new IntegerResult(valueOp.apply(elementInt, scalarInt)); - } - - return combinedValue; - }); - } - - private IPair> reduceStates(IPair> accumulatedState, - IPair> currentState) { - return accumulatedState.bind((accumulatedValue, accumulatedTree) -> { - return currentState.bind((currentValue, currentTree) -> { - accumulatedTree.addChild(currentTree); - - return doArithmeticCollapse(accumulatedValue, accumulatedTree, currentValue); - }); - }); - } -} \ No newline at end of file diff --git a/dice-lang/src/bjc/dicelang/v1/ast/ArrayResult.java b/dice-lang/src/bjc/dicelang/v1/ast/ArrayResult.java deleted file mode 100644 index 1053360..0000000 --- a/dice-lang/src/bjc/dicelang/v1/ast/ArrayResult.java +++ /dev/null @@ -1,43 +0,0 @@ -package bjc.dicelang.v1.ast; - -import bjc.utils.funcdata.IList; - -/** - * Represents a result that is an array of other results - * - * @author ben - * - * TODO finish implementing me - */ -public class ArrayResult implements IResult { - private IList arrayContents; - - /** - * Create a new array-valued result - * - * @param results - * The results in the array - */ - public ArrayResult(IList results) { - this.arrayContents = results; - } - - @Override - public ResultType getType() { - return ResultType.ARRAY; - } - - /** - * Get the value of this result - * - * @return The value of this result - */ - public IList getValue() { - return arrayContents; - } - - @Override - public String toString() { - return arrayContents.toString(); - } -} diff --git a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTEvaluator.java b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTEvaluator.java deleted file mode 100644 index 8273525..0000000 --- a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTEvaluator.java +++ /dev/null @@ -1,276 +0,0 @@ -package bjc.dicelang.v1.ast; - -import bjc.dicelang.v1.ComplexDice; -import bjc.dicelang.v1.ast.nodes.DiceASTType; -import bjc.dicelang.v1.ast.nodes.DiceLiteralNode; -import bjc.dicelang.v1.ast.nodes.DiceLiteralType; -import bjc.dicelang.v1.ast.nodes.IDiceASTNode; -import bjc.dicelang.v1.ast.nodes.ILiteralDiceNode; -import bjc.dicelang.v1.ast.nodes.IntegerLiteralNode; -import bjc.dicelang.v1.ast.nodes.OperatorDiceNode; -import bjc.dicelang.v1.ast.nodes.VariableDiceNode; -import bjc.utils.data.IHolder; -import bjc.utils.data.IPair; -import bjc.utils.data.ITree; -import bjc.utils.data.Identity; -import bjc.utils.data.LazyPair; -import bjc.utils.data.Pair; -import bjc.utils.data.Tree; -import bjc.utils.funcdata.FunctionalList; -import bjc.utils.funcdata.FunctionalMap; -import bjc.utils.funcdata.IList; -import bjc.utils.funcdata.IMap; - -import java.util.function.Supplier; - -/** - * Evaluate a dice AST to an integer value - * - * @author ben - * - */ -public class DiceASTEvaluator { - private static IResult bindLiteralValue(IDiceASTNode leafNode, IMap> enviroment) { - String variableName = ((VariableDiceNode) leafNode).getVariable(); - - if(enviroment.containsKey(variableName)) { - IResult result = evaluateAST(enviroment.get(variableName), enviroment); - - return result; - } - - // Return a DummyResult to handle lets properly - return new DummyResult("Attempted to deref unbound variable " + variableName); - } - - /** - * 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 IMap buildOperations( - IMap> enviroment) { - IMap operatorCollapsers = new FunctionalMap<>(); - - operatorCollapsers.put(OperatorDiceNode.ADD, - new ArithmeticCollapser(OperatorDiceNode.ADD, (left, right) -> left + right, 0)); - - operatorCollapsers.put(OperatorDiceNode.SUBTRACT, - new ArithmeticCollapser(OperatorDiceNode.SUBTRACT, (left, right) -> left - right, 0)); - - operatorCollapsers.put(OperatorDiceNode.MULTIPLY, - new ArithmeticCollapser(OperatorDiceNode.MULTIPLY, (left, right) -> left * right, 1)); - - operatorCollapsers.put(OperatorDiceNode.DIVIDE, - new ArithmeticCollapser(OperatorDiceNode.DIVIDE, (left, right) -> left / right, 1)); - - operatorCollapsers.put(OperatorDiceNode.ASSIGN, (nodes) -> { - return parseBinding(enviroment, nodes); - }); - - operatorCollapsers.put(OperatorDiceNode.COMPOUND, - new ArithmeticCollapser(OperatorDiceNode.COMPOUND, (left, right) -> { - return Integer.parseInt(Integer.toString(left) + Integer.toString(right)); - }, 0)); - - operatorCollapsers.put(OperatorDiceNode.GROUP, DiceASTEvaluator::parseGroup); - - operatorCollapsers.put(OperatorDiceNode.LET, (nodes) -> { - // @TODO Fix lets prematurely evaluating things - return parseLet(enviroment, nodes); - }); - - operatorCollapsers.put(OperatorDiceNode.ARRAY, (nodes) -> { - - // This is so that arrays respect lazy results properly - Supplier resultSupplier = () -> { - IList resultList = new FunctionalList<>(); - - nodes.forEach((node) -> { - resultList.add(node.getLeft()); - }); - - return new ArrayResult(resultList); - }; - - Supplier> treeSupplier = () -> { - ITree returnedTree = new Tree<>(OperatorDiceNode.ARRAY); - - nodes.forEach((element) -> { - returnedTree.addChild(element.getRight()); - }); - - return returnedTree; - }; - - return new LazyPair<>(resultSupplier, treeSupplier); - }); - - return operatorCollapsers; - } - - private static void doArrayAssign(IMap> enviroment, - IPair> nameNode, ITree nameTree, - ITree valueTree, IHolder childCount, ITree child) { - if(nameTree.getHead().getType() != DiceASTType.VARIABLE) throw new UnsupportedOperationException( - "Assigning to complex variables isn't supported. Problem node is " - + nameNode.getRight()); - - String varName = child.transformHead((nameNod) -> { - return ((VariableDiceNode) nameNod).getVariable(); - }); - - enviroment.put(varName, valueTree.getChild(childCount.getValue())); - - childCount.transform(val -> val + 1); - } - - /** - * Evaluate the provided AST to a numeric value - * - * @param expression - * The expression to evaluate - * @param enviroment - * The enviroment to look up variables in - * @return The integer value of the expression - */ - public static IResult evaluateAST(ITree expression, - IMap> enviroment) { - IMap collapsers = buildOperations(enviroment); - - return expression.collapse((node) -> evaluateLeaf(node, enviroment), collapsers::get, - (pair) -> pair.getLeft()); - } - - private static IPair> evaluateLeaf(IDiceASTNode leafNode, - IMap> enviroment) { - ITree returnedAST = new Tree<>(leafNode); - - switch(leafNode.getType()) { - case LITERAL: - return new Pair<>(evaluateLiteral(leafNode), returnedAST); - - case VARIABLE: - return new LazyPair<>(() -> { - return bindLiteralValue(leafNode, enviroment); - }, () -> returnedAST); - - case OPERATOR: - default: - throw new UnsupportedOperationException("Node '" + leafNode + "' cannot be a leaf."); - } - } - - private static IResult evaluateLiteral(IDiceASTNode leafNode) { - DiceLiteralType literalType = ((ILiteralDiceNode) leafNode).getLiteralType(); - - switch(literalType) { - case DICE: - int diceRoll = ((DiceLiteralNode) leafNode).getValue().roll(); - - return new IntegerResult(diceRoll); - case INTEGER: - int val = ((IntegerLiteralNode) leafNode).getValue(); - - return new IntegerResult(val); - default: - throw new UnsupportedOperationException("Literal value '" + leafNode + "' is of a type (" - + literalType + ") not currently supported."); - } - } - - private static IPair> parseBinding(IMap> enviroment, - IList>> nodes) { - if(nodes.getSize() != 2) throw new UnsupportedOperationException( - "Can only bind nodes with two children. Problem children are " + nodes); - - IPair> nameNode = nodes.getByIndex(0); - IPair> valueNode = nodes.getByIndex(1); - - return nameNode.bindRight((nameTree) -> { - return valueNode.bind((valueValue, valueTree) -> { - if(DiceASTUtils.containsSimpleVariable(nameTree)) { - String varName = nameTree.transformHead((nameNod) -> { - return ((VariableDiceNode) nameNod).getVariable(); - }); - - enviroment.put(varName, valueTree); - - return new Pair<>(valueValue, nameTree); - } else if(nameTree.getHead() == OperatorDiceNode.ARRAY) { - if(valueTree.getHead() == OperatorDiceNode.ARRAY) { - if(nameTree.getChildrenCount() != valueTree.getChildrenCount()) - throw new UnsupportedOperationException( - "Array assignment must be between two equal length arrays"); - - IHolder childCount = new Identity<>(0); - - nameTree.doForChildren((child) -> { - doArrayAssign(enviroment, nameNode, nameTree, valueTree, - childCount, child); - - childCount.transform(val -> val + 1); - }); - - return new Pair<>(valueValue, nameTree); - } - - nameTree.doForChildren((child) -> { - String varName = child.transformHead((nameNod) -> { - return ((VariableDiceNode) nameNod).getVariable(); - }); - - enviroment.put(varName, valueTree); - }); - - return new Pair<>(valueValue, nameTree); - } - - throw new UnsupportedOperationException( - "Assigning to complex variables isn't supported. Problem node is " - + nameNode.getRight()); - }); - }); - } - - private static IPair> parseGroup( - IList>> nodes) { - if(nodes.getSize() != 2) throw new UnsupportedOperationException("Can only form a group from two dice"); - - IPair> numberDiceNode = nodes.getByIndex(0); - IPair> diceTypeNode = nodes.getByIndex(1); - - return numberDiceNode.bind((numberDiceValue, numberDiceTree) -> { - return diceTypeNode.bind((diceTypeValue, diceTypeTree) -> { - ComplexDice cDice = new ComplexDice(((IntegerResult) numberDiceValue).getValue(), - ((IntegerResult) diceTypeValue).getValue()); - - return new Pair<>(new IntegerResult(cDice.roll()), - new Tree<>(OperatorDiceNode.GROUP, numberDiceTree, diceTypeTree)); - }); - }); - } - - private static IPair> parseLet(IMap> enviroment, - IList>> nodes) { - if(nodes.getSize() != 2) - throw new UnsupportedOperationException("Can only use let with two expressions."); - - ITree bindTree = nodes.getByIndex(0).getRight(); - ITree expressionTree = nodes.getByIndex(1).getRight(); - - IMap> letEnviroment = enviroment.extend(); - - System.out.println("Evaluating tree for bound values"); - - evaluateAST(bindTree, letEnviroment); - - IResult exprResult = evaluateAST(expressionTree, letEnviroment); - - IList> childrn = nodes.map((pair) -> pair.getRight()); - - return new Pair<>(exprResult, new Tree<>(OperatorDiceNode.LET, childrn)); - } -} diff --git a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTInliner.java b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTInliner.java deleted file mode 100644 index 74e59c6..0000000 --- a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTInliner.java +++ /dev/null @@ -1,112 +0,0 @@ -package bjc.dicelang.v1.ast; - -import bjc.dicelang.v1.ast.nodes.DiceASTType; -import bjc.dicelang.v1.ast.nodes.IDiceASTNode; -import bjc.dicelang.v1.ast.nodes.VariableDiceNode; -import bjc.utils.data.ITree; -import bjc.utils.data.Tree; -import bjc.utils.funcdata.FunctionalList; -import bjc.utils.funcdata.IList; -import bjc.utils.funcdata.IMap; - -/** - * Inline variables in a dice AST - * - * @author ben - * - */ -public class DiceASTInliner { - /** - * Inline all the variables in the AST - * - * @param ast - * The AST to inline variables into - * @param enviroment - * The enviroment to inline from - * @return The inlined AST - */ - public static ITree inlineAll(ITree ast, - IMap> enviroment) { - // Tell the compiler that the null is for the entire varargs - // parameter, not a single one with a null value - return selectiveInline(ast, enviroment, (String[]) null); - } - - private static ITree inlineNode(IDiceASTNode node, IMap> enviroment, - boolean specificInline, IList variableNames) { - // Only variables get inlined - if(node.getType() != DiceASTType.VARIABLE) return new Tree<>(node); - - // Get the name of what we're inlining - String variableName = ((VariableDiceNode) node).getVariable(); - - // If we're inlining only certain variables, do so - if(specificInline) { - // Only inline the variable if we're supposed to - if(variableNames.contains(variableName)) { - // You can't inline non-existent variables - if(!enviroment.containsKey(variableName)) throw new UnsupportedOperationException( - "Attempted to inline non-existant variable " + variableName); - - // Return the tree for the variable - return enviroment.get(variableName); - } - - // We're not inlining this particular variable - return new Tree<>(node); - } - - // You can't inline non-existent variables - if(!enviroment.containsKey(variableName)) throw new UnsupportedOperationException( - "Attempted to inline non-existant variable " + variableName); - - // Return the tree for the variable - return enviroment.get(variableName); - } - - /** - * Inline the specified variables in the AST - * - * @param ast - * The AST to inline variables into - * @param enviroment - * The enviroment to inline from - * @param variables - * The variables to inline - * @return The inlined AST - */ - public static ITree selectiveInline(ITree ast, - IMap> enviroment, IList variables) { - // Inline the specified variables - return selectiveInline(ast, enviroment, variables.toArray(new String[0])); - } - - /** - * Inline the specified variables in the AST - * - * @param ast - * The AST to inline variables into - * @param enviroment - * The enviroment to inline from - * @param variables - * The variables to inline - * @return The inlined AST - */ - public static ITree selectiveInline(ITree ast, - IMap> enviroment, String... variables) { - // If we're selectively inlining, do so - if(variables != null && variables.length > 0) { - IList variableNames = new FunctionalList<>(variables); - - // Selectively inline each tree node - return ast.flatMapTree((node) -> { - return inlineNode(node, enviroment, true, variableNames); - }); - } - - // Inline everything in each node - return ast.flatMapTree((node) -> { - return inlineNode(node, enviroment, false, null); - }); - } -} diff --git a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTOptimizer.java b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTOptimizer.java deleted file mode 100644 index fa4f0ca..0000000 --- a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTOptimizer.java +++ /dev/null @@ -1,55 +0,0 @@ -package bjc.dicelang.v1.ast; - -import bjc.dicelang.v1.ast.nodes.IDiceASTNode; -import bjc.dicelang.v1.ast.optimization.IOptimizationPass; -import bjc.utils.data.ITree; -import bjc.utils.funcdata.FunctionalList; -import bjc.utils.funcdata.IList; -import bjc.utils.funcdata.IMap; - -/** - * Contains optimizations appliable to a dice AST - * - * @author ben - * - */ -public class DiceASTOptimizer { - private IList passes; - - /** - * Create a new optimizer - */ - public DiceASTOptimizer() { - passes = new FunctionalList<>(); - } - - /** - * Add a pass to the list of optimization passes - * - * @param pass - * The pass to add - */ - public void addPass(IOptimizationPass pass) { - passes.add(pass); - } - - /** - * Optimize the passed in tree - * - * @param ast - * The tree to optimize - * @param enviroment - * The enviroment for variable references - * @return The optimized tree - */ - public ITree optimizeTree(ITree ast, IMap> enviroment) { - ITree optimizedTree = passes.reduceAux(ast, (currentPass, currentTree) -> { - return currentTree.collapse(currentPass::optimizeLeaf, (operator) -> { - return (nodes) -> { - return currentPass.optimizeOperator(operator, nodes); - }; - }, (tree) -> tree); - }, (tree) -> tree); - return optimizedTree; - } -} diff --git a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTParser.java b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTParser.java deleted file mode 100644 index 4a2822c..0000000 --- a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTParser.java +++ /dev/null @@ -1,147 +0,0 @@ -package bjc.dicelang.v1.ast; - -import bjc.dicelang.v1.IDiceExpression; -import bjc.dicelang.v1.ast.nodes.DiceLiteralNode; -import bjc.dicelang.v1.ast.nodes.DiceLiteralType; -import bjc.dicelang.v1.ast.nodes.IDiceASTNode; -import bjc.dicelang.v1.ast.nodes.ILiteralDiceNode; -import bjc.dicelang.v1.ast.nodes.IntegerLiteralNode; -import bjc.dicelang.v1.ast.nodes.OperatorDiceNode; -import bjc.dicelang.v1.ast.nodes.VariableDiceNode; -import bjc.utils.data.ITree; -import bjc.utils.data.Tree; -import bjc.utils.funcdata.FunctionalList; -import bjc.utils.funcdata.FunctionalMap; -import bjc.utils.funcdata.IList; -import bjc.utils.funcdata.IMap; -import bjc.utils.funcutils.StringUtils; -import bjc.utils.parserutils.TreeConstructor; - -import java.util.Deque; -import java.util.InputMismatchException; -import java.util.function.Function; -import java.util.function.Predicate; - -/** - * Parse a string expression into AST form. Doesn't do anything else - * - * @author ben - * - */ -public class DiceASTParser { - private static IDiceASTNode convertLeafNode(String leafNode) { - DiceLiteralType literalType = ILiteralDiceNode.getLiteralType(leafNode); - - if(literalType != null) { - switch(literalType) { - case DICE: - return new DiceLiteralNode(IDiceExpression.toExpression(leafNode)); - case INTEGER: - return new IntegerLiteralNode(Integer.parseInt(leafNode)); - default: - throw new InputMismatchException( - "Cannot convert string '" + leafNode + "' into a literal."); - } - } - - if(leafNode.matches("[+-]?\\d*\\.\\d+")) - throw new InputMismatchException("Floating point literals are not supported"); - - return new VariableDiceNode(leafNode); - } - - private static IDiceASTNode convertOperatorNode(String operatorNode) { - try { - return OperatorDiceNode.fromString(operatorNode); - } catch(IllegalArgumentException iaex) { - InputMismatchException imex = new InputMismatchException( - "Attempted to parse invalid operator " + operatorNode); - - imex.initCause(iaex); - - throw imex; - } - } - - /** - * Create an AST from a list of tokens - * - * @param tokens - * The list of tokens to convert - * @return An AST built from the tokens - */ - public static ITree createFromString(IList tokens) { - // Mark arrays as special operators - Predicate specialPicker = (operator) -> { - if(StringUtils.containsOnly(operator, "\\[") || StringUtils.containsOnly(operator, "\\]")) - return true; - - return false; - }; - - // Here is the map for holding special operators - IMap>, ITree>> operators = new FunctionalMap<>(); - - // Handle open [ - operators.put("[", (queuedTrees) -> { - // Just put in a [ - Tree openArray = new Tree<>("["); - - return openArray; - }); - - operators.put("]", (queuedTrees) -> { - // Parse closing an array - return parseCloseArray(queuedTrees); - }); - - ITree rawTokens = TreeConstructor.constructTree(tokens, (token) -> { - return isOperatorNode(token); - }, specialPicker, operators::get); - - ITree tokenizedTree = rawTokens.rebuildTree(DiceASTParser::convertLeafNode, - DiceASTParser::convertOperatorNode); - - return tokenizedTree; - } - - private static boolean isOperatorNode(String token) { - if(StringUtils.containsOnly(token, "\\[")) - return true; - else if(StringUtils.containsOnly(token, "\\]")) return true; - - if(token.equals("[]")) // This is a synthetic operator, - // constructed by [ and ] - return true; - - try { - OperatorDiceNode.fromString(token); - return true; - } catch(IllegalArgumentException iaex) { - // We don't care about details - return false; - } - } - - private static ITree parseCloseArray(Deque> queuedTrees) { - IList> children = new FunctionalList<>(); - - while(shouldContinuePopping(queuedTrees)) { - children.add(queuedTrees.pop()); - } - - queuedTrees.pop(); - - children.reverse(); - - ITree arrayTree = new Tree<>("[]", children); - - return arrayTree; - } - - private static boolean shouldContinuePopping(Deque> queuedTrees) { - String peekToken = queuedTrees.peek().getHead(); - - return !peekToken.equals("["); - } -} diff --git a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTReferenceChecker.java b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTReferenceChecker.java deleted file mode 100644 index f668c72..0000000 --- a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTReferenceChecker.java +++ /dev/null @@ -1,59 +0,0 @@ -package bjc.dicelang.v1.ast; - -import bjc.dicelang.v1.ast.nodes.DiceASTType; -import bjc.dicelang.v1.ast.nodes.IDiceASTNode; -import bjc.dicelang.v1.ast.nodes.VariableDiceNode; -import bjc.utils.data.IHolder; - -import java.util.function.Consumer; - -/** - * 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/bjc/dicelang/v1/ast/DiceASTReferenceSanitizer.java b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTReferenceSanitizer.java deleted file mode 100644 index ec595ea..0000000 --- a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTReferenceSanitizer.java +++ /dev/null @@ -1,176 +0,0 @@ -package bjc.dicelang.v1.ast; - -import bjc.dicelang.v1.ast.nodes.IDiceASTNode; -import bjc.dicelang.v1.ast.nodes.OperatorDiceNode; -import bjc.dicelang.v1.ast.nodes.VariableDiceNode; -import bjc.utils.data.IHolder; -import bjc.utils.data.ITree; -import bjc.utils.data.Identity; -import bjc.utils.data.TopDownTransformResult; -import bjc.utils.data.Tree; -import bjc.utils.funcdata.IMap; - -/** - * Sanitize the references in an AST so that a variable that refers to itself in - * its definition has the occurance of it replaced with its previous definition - * - * @author ben - * - */ -public class DiceASTReferenceSanitizer { - private static ITree doSanitize(ITree ast, - IMap> enviroment) { - if(ast.getChildrenCount() != 2) - throw new UnsupportedOperationException("Assignment must have two arguments."); - - ITree nameTree = ast.getChild(0); - ITree valueTree = ast.getChild(1); - - if(!DiceASTUtils.containsSimpleVariable(nameTree)) { - if(nameTree.getHead() == OperatorDiceNode.ARRAY) { - IHolder allSimpleVariables = new Identity<>(true); - - nameTree.doForChildren((child) -> { - if(allSimpleVariables.getValue()) { - boolean isSimple = DiceASTUtils.containsSimpleVariable(child); - - allSimpleVariables.replace(isSimple); - } - }); - - if(!allSimpleVariables.getValue()) throw new UnsupportedOperationException( - "Array assignment must be between variables and" - + " a expression/array of expressions"); - - if(valueTree.getHead() == OperatorDiceNode.ARRAY) { - if(nameTree.getChildrenCount() != valueTree.getChildrenCount()) - throw new UnsupportedOperationException( - "Array assignment between arrays must be" - + " between two arrays of equal length"); - } - } else - throw new UnsupportedOperationException( - "Assignment must be between a variable and a expression"); - } - - if(nameTree.getHead() == OperatorDiceNode.ARRAY) { - if(valueTree.getHead() == OperatorDiceNode.ARRAY) { - IHolder childCounter = new Identity<>(0); - - ITree returnTree = new Tree<>(OperatorDiceNode.ARRAY); - - nameTree.doForChildren((child) -> { - String variableName = child.transformHead((node) -> { - return ((VariableDiceNode) node).getVariable(); - }); - - ITree currentValue = valueTree.getChild(childCounter.getValue()); - - ITree sanitizedSubtree = doSingleSanitize(ast, enviroment, child, - currentValue, variableName); - - if(sanitizedSubtree == null) { - ITree oldTree = new Tree<>(ast.getHead(), child, - currentValue); - - returnTree.addChild(oldTree); - } else { - returnTree.addChild(sanitizedSubtree); - } - - childCounter.transform((count) -> count + 1); - }); - - return returnTree; - } - - ITree returnTree = new Tree<>(OperatorDiceNode.ARRAY); - - nameTree.doForChildren((child) -> { - String variableName = child - .transformHead((node) -> ((VariableDiceNode) node).getVariable()); - - ITree sanitizedChild = doSingleSanitize(ast, enviroment, child, valueTree, - variableName); - if(sanitizedChild == null) { - ITree oldTree = new Tree<>(ast.getHead(), child, valueTree); - - returnTree.addChild(oldTree); - } else { - returnTree.addChild(sanitizedChild); - } - }); - - return returnTree; - } - - String variableName = nameTree.transformHead((node) -> ((VariableDiceNode) node).getVariable()); - - ITree sanitizedTree = doSingleSanitize(ast, enviroment, nameTree, valueTree, - variableName); - - if(sanitizedTree == null) return ast; - - return sanitizedTree; - } - - private static ITree doSingleSanitize(ITree ast, - IMap> enviroment, ITree nameTree, - ITree valueTree, String variableName) { - if(enviroment.containsKey(variableName)) { - // @ is a meta-variable standing for the left side of an - // assignment - ITree oldVal = enviroment.put("@", enviroment.get(variableName)); - - // We should always inline out references to last, - // because it - // will always change - ITree inlinedValue = DiceASTInliner.selectiveInline(valueTree, enviroment, - variableName, "last", "@"); - - if(oldVal != null) { - enviroment.put("@", oldVal); - } else { - enviroment.remove("@"); - } - - return new Tree<>(ast.getHead(), nameTree, inlinedValue); - } - - return null; - } - - /** - * Sanitize the references in an AST - * - * @param ast - * @param enviroment - * @return The sanitized AST - */ - public static ITree sanitize(ITree ast, - IMap> enviroment) { - return ast.topDownTransform(DiceASTReferenceSanitizer::shouldSanitize, (subTree) -> { - return doSanitize(subTree, enviroment); - }); - } - - private static TopDownTransformResult shouldSanitize(IDiceASTNode node) { - if(!node.isOperator()) return TopDownTransformResult.SKIP; - - switch((OperatorDiceNode) node) { - case ASSIGN: - return TopDownTransformResult.TRANSFORM; - case ARRAY: - case LET: - return TopDownTransformResult.PASSTHROUGH; - case ADD: - case COMPOUND: - case DIVIDE: - case GROUP: - case MULTIPLY: - case SUBTRACT: - default: - return TopDownTransformResult.SKIP; - } - } -} diff --git a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTUtils.java b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTUtils.java deleted file mode 100644 index e37d3c3..0000000 --- a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTUtils.java +++ /dev/null @@ -1,80 +0,0 @@ -package bjc.dicelang.v1.ast; - -import bjc.dicelang.v1.IDiceExpression; -import bjc.dicelang.v1.ScalarDie; -import bjc.dicelang.v1.ast.nodes.DiceASTType; -import bjc.dicelang.v1.ast.nodes.DiceLiteralNode; -import bjc.dicelang.v1.ast.nodes.IDiceASTNode; -import bjc.dicelang.v1.ast.nodes.ILiteralDiceNode; -import bjc.dicelang.v1.ast.nodes.IntegerLiteralNode; -import bjc.utils.data.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 - * The tree to check for a reference in - * @return Whether or not a dice AST contains a simple variable - * reference - */ - public static boolean containsSimpleVariable(ITree nameTree) { - return nameTree.transformHead((nameNode) -> { - if(nameNode.getType() != DiceASTType.VARIABLE) return false; - - return true; - }); - } - - /** - * Convert an literal AST node to a dice expression, if possible. - * - * @param tree - * The node to convert in tree form - * @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 literalToExpression(ITree tree) { - ILiteralDiceNode literalNode = (ILiteralDiceNode) tree.getHead(); - - switch(literalNode.getLiteralType()) { - case DICE: - return ((DiceLiteralNode) literalNode).getValue(); - case INTEGER: - return new ScalarDie(((IntegerLiteralNode) literalNode).getValue()); - default: - throw new UnsupportedOperationException( - "This type of literal isn't convertable to an expression"); - } - } - - /** - * Convert an literal AST node to an integer, if possible. - * - * @param tree - * The literal node to convert, as a tree - * @return The node 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 literalToInteger(ITree tree) { - return tree.transformHead((node) -> { - return ((ILiteralDiceNode) node).optimize(); - }); - } -} diff --git a/dice-lang/src/bjc/dicelang/v1/ast/DummyResult.java b/dice-lang/src/bjc/dicelang/v1/ast/DummyResult.java deleted file mode 100644 index 2328d3c..0000000 --- a/dice-lang/src/bjc/dicelang/v1/ast/DummyResult.java +++ /dev/null @@ -1,43 +0,0 @@ -package bjc.dicelang.v1.ast; - -/** - * A dummy result - * - * @author ben - * - */ -public class DummyResult implements IResult { - /* - * The reason why this result is a dummy - */ - private String dummyData; - - /** - * Create a new dummy result with a reason - * - * @param data - * The reason why the result is a dummy - */ - public DummyResult(String data) { - dummyData = data; - } - - /** - * Get the data in this dummy - * - * @return The reason why this result is a dummy - */ - public String getData() { - return dummyData; - } - - @Override - public ResultType getType() { - return ResultType.DUMMY; - } - - @Override - public String toString() { - return "Dummy with reason " + dummyData; - } -} diff --git a/dice-lang/src/bjc/dicelang/v1/ast/IOperatorCollapser.java b/dice-lang/src/bjc/dicelang/v1/ast/IOperatorCollapser.java deleted file mode 100644 index ab097db..0000000 --- a/dice-lang/src/bjc/dicelang/v1/ast/IOperatorCollapser.java +++ /dev/null @@ -1,19 +0,0 @@ -package bjc.dicelang.v1.ast; - -import bjc.dicelang.v1.ast.nodes.IDiceASTNode; -import bjc.utils.data.IPair; -import bjc.utils.data.ITree; -import bjc.utils.funcdata.IList; - -import java.util.function.Function; - -/** - * Alias for operator collapsers. Because 68-char types are too long - * - * @author ben - * - */ -public interface IOperatorCollapser - extends Function>>, IPair>> { - // Just an alias -} diff --git a/dice-lang/src/bjc/dicelang/v1/ast/IResult.java b/dice-lang/src/bjc/dicelang/v1/ast/IResult.java deleted file mode 100644 index df67061..0000000 --- a/dice-lang/src/bjc/dicelang/v1/ast/IResult.java +++ /dev/null @@ -1,16 +0,0 @@ -package bjc.dicelang.v1.ast; - -/** - * Represents a result from an expression evaluation - * - * @author ben - * - */ -public interface IResult { - /** - * Get the type of this result - * - * @return The type of this result - */ - public ResultType getType(); -} diff --git a/dice-lang/src/bjc/dicelang/v1/ast/IntegerResult.java b/dice-lang/src/bjc/dicelang/v1/ast/IntegerResult.java deleted file mode 100644 index 61d57b7..0000000 --- a/dice-lang/src/bjc/dicelang/v1/ast/IntegerResult.java +++ /dev/null @@ -1,40 +0,0 @@ -package bjc.dicelang.v1.ast; - -/** - * Represents a integer-valued result - * - * @author ben - * - */ -public class IntegerResult implements IResult { - private int value; - - /** - * Create a new integer valued result - * - * @param val - * The value of the result - */ - public IntegerResult(int val) { - value = val; - } - - @Override - public ResultType getType() { - return ResultType.INTEGER; - } - - /** - * Get the value of this result - * - * @return The value of this result - */ - public int getValue() { - return value; - } - - @Override - public String toString() { - return Integer.toString(value); - } -} diff --git a/dice-lang/src/bjc/dicelang/v1/ast/ResultType.java b/dice-lang/src/bjc/dicelang/v1/ast/ResultType.java deleted file mode 100644 index c5afee8..0000000 --- a/dice-lang/src/bjc/dicelang/v1/ast/ResultType.java +++ /dev/null @@ -1,22 +0,0 @@ -package bjc.dicelang.v1.ast; - -/** - * Represents the result of a computation - * - * @author ben - * - */ -public enum ResultType { - /** - * Represents a result that is equivalent to a single integer - */ - INTEGER, - /** - * Represents a result that is an array - */ - ARRAY, - /** - * Represents something not to poke at - */ - DUMMY -} diff --git a/dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceASTType.java b/dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceASTType.java deleted file mode 100644 index 47e8b39..0000000 --- a/dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceASTType.java +++ /dev/null @@ -1,27 +0,0 @@ -package bjc.dicelang.v1.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/v1/ast/nodes/DiceLiteralNode.java b/dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceLiteralNode.java deleted file mode 100644 index 4241463..0000000 --- a/dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceLiteralNode.java +++ /dev/null @@ -1,52 +0,0 @@ -package bjc.dicelang.v1.ast.nodes; - -import bjc.dicelang.v1.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/v1/ast/nodes/DiceLiteralType.java b/dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceLiteralType.java deleted file mode 100644 index d48104c..0000000 --- a/dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceLiteralType.java +++ /dev/null @@ -1,18 +0,0 @@ -package bjc.dicelang.v1.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/v1/ast/nodes/DiceOperatorType.java b/dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceOperatorType.java deleted file mode 100644 index b842604..0000000 --- a/dice-lang/src/bjc/dicelang/v1/ast/nodes/DiceOperatorType.java +++ /dev/null @@ -1,29 +0,0 @@ -package bjc.dicelang.v1.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/v1/ast/nodes/IDiceASTNode.java b/dice-lang/src/bjc/dicelang/v1/ast/nodes/IDiceASTNode.java deleted file mode 100644 index 7e8bb81..0000000 --- a/dice-lang/src/bjc/dicelang/v1/ast/nodes/IDiceASTNode.java +++ /dev/null @@ -1,23 +0,0 @@ -package bjc.dicelang.v1.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/v1/ast/nodes/ILiteralDiceNode.java b/dice-lang/src/bjc/dicelang/v1/ast/nodes/ILiteralDiceNode.java deleted file mode 100644 index 5a3f5b3..0000000 --- a/dice-lang/src/bjc/dicelang/v1/ast/nodes/ILiteralDiceNode.java +++ /dev/null @@ -1,71 +0,0 @@ -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(); -} diff --git a/dice-lang/src/bjc/dicelang/v1/ast/nodes/IntegerLiteralNode.java b/dice-lang/src/bjc/dicelang/v1/ast/nodes/IntegerLiteralNode.java deleted file mode 100644 index fd1a1e6..0000000 --- a/dice-lang/src/bjc/dicelang/v1/ast/nodes/IntegerLiteralNode.java +++ /dev/null @@ -1,50 +0,0 @@ -package bjc.dicelang.v1.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/v1/ast/nodes/OperatorDiceNode.java b/dice-lang/src/bjc/dicelang/v1/ast/nodes/OperatorDiceNode.java deleted file mode 100644 index 0181314..0000000 --- a/dice-lang/src/bjc/dicelang/v1/ast/nodes/OperatorDiceNode.java +++ /dev/null @@ -1,104 +0,0 @@ -package bjc.dicelang.v1.ast.nodes; - -import static bjc.dicelang.v1.ast.nodes.DiceOperatorType.DICE; -import static bjc.dicelang.v1.ast.nodes.DiceOperatorType.EXPRESSION; -import static bjc.dicelang.v1.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; - } - - @Override - public boolean isOperator() { - return true; - } -} diff --git a/dice-lang/src/bjc/dicelang/v1/ast/nodes/VariableDiceNode.java b/dice-lang/src/bjc/dicelang/v1/ast/nodes/VariableDiceNode.java deleted file mode 100644 index e44f2ab..0000000 --- a/dice-lang/src/bjc/dicelang/v1/ast/nodes/VariableDiceNode.java +++ /dev/null @@ -1,96 +0,0 @@ -package bjc.dicelang.v1.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/v1/ast/nodes/package-info.java b/dice-lang/src/bjc/dicelang/v1/ast/nodes/package-info.java deleted file mode 100644 index cdd63e9..0000000 --- a/dice-lang/src/bjc/dicelang/v1/ast/nodes/package-info.java +++ /dev/null @@ -1,7 +0,0 @@ -/** - * Classes for nodes in the dice-lang AST - * - * @author ben - * - */ -package bjc.dicelang.v1.ast.nodes; \ No newline at end of file diff --git a/dice-lang/src/bjc/dicelang/v1/ast/optimization/ArithmeticCollapser.java b/dice-lang/src/bjc/dicelang/v1/ast/optimization/ArithmeticCollapser.java deleted file mode 100644 index acc1afa..0000000 --- a/dice-lang/src/bjc/dicelang/v1/ast/optimization/ArithmeticCollapser.java +++ /dev/null @@ -1,42 +0,0 @@ -package bjc.dicelang.v1.ast.optimization; - -import bjc.dicelang.v1.ast.DiceASTUtils; -import bjc.dicelang.v1.ast.nodes.DiceASTType; -import bjc.dicelang.v1.ast.nodes.IDiceASTNode; -import bjc.dicelang.v1.ast.nodes.ILiteralDiceNode; -import bjc.dicelang.v1.ast.nodes.IntegerLiteralNode; -import bjc.dicelang.v1.ast.nodes.OperatorDiceNode; -import bjc.utils.data.ITree; -import bjc.utils.data.Tree; -import bjc.utils.funcdata.IList; - -import java.util.function.BinaryOperator; - -class ArithmeticCollapser { - private BinaryOperator reducer; - private OperatorDiceNode type; - - public ArithmeticCollapser(BinaryOperator reducr, OperatorDiceNode typ) { - reducer = reducr; - this.type = typ; - } - - public ITree collapse(IList> children) { - boolean allConstant = children.allMatch((subtree) -> { - return subtree.transformHead((node) -> { - if(node.getType() == DiceASTType.LITERAL) - return ((ILiteralDiceNode) node).canOptimize(); - - return false; - }); - }); - - if(!allConstant) return new Tree<>(type, children); - - int initState = DiceASTUtils.literalToInteger(children.first()); - - return children.tail().reduceAux(initState, (currentNode, state) -> { - return reducer.apply(state, DiceASTUtils.literalToInteger(currentNode)); - }, (state) -> new Tree<>(new IntegerLiteralNode(state))); - } -} diff --git a/dice-lang/src/bjc/dicelang/v1/ast/optimization/ConstantCollapser.java b/dice-lang/src/bjc/dicelang/v1/ast/optimization/ConstantCollapser.java deleted file mode 100644 index bf44953..0000000 --- a/dice-lang/src/bjc/dicelang/v1/ast/optimization/ConstantCollapser.java +++ /dev/null @@ -1,76 +0,0 @@ -package bjc.dicelang.v1.ast.optimization; - -import bjc.dicelang.v1.ComplexDice; -import bjc.dicelang.v1.ast.DiceASTUtils; -import bjc.dicelang.v1.ast.nodes.IDiceASTNode; -import bjc.dicelang.v1.ast.nodes.IntegerLiteralNode; -import bjc.dicelang.v1.ast.nodes.OperatorDiceNode; -import bjc.utils.data.ITree; -import bjc.utils.data.Tree; -import bjc.utils.funcdata.IList; - -/** - * Collapses operations with constants to constants - * - * @author ben - * - */ -public class ConstantCollapser implements IOptimizationPass { - private static final ArithmeticCollapser additionCollapser = new ArithmeticCollapser( - (left, right) -> left + right, OperatorDiceNode.ADD); - - private static final ArithmeticCollapser divideCollapser = new ArithmeticCollapser( - (left, right) -> left / right, OperatorDiceNode.DIVIDE); - - private static final ArithmeticCollapser multiplyCollapser = new ArithmeticCollapser( - (left, right) -> left * right, OperatorDiceNode.MULTIPLY); - - private static final ArithmeticCollapser subtractCollapser = new ArithmeticCollapser( - (left, right) -> left - right, OperatorDiceNode.SUBTRACT); - - private static final ArithmeticCollapser compoundCollapser = new ArithmeticCollapser( - (left, right) -> Integer.parseInt(Integer.toString(left) + Integer.toString(left)), - OperatorDiceNode.COMPOUND); - - @Override - public ITree optimizeLeaf(IDiceASTNode leafNode) { - // We don't do anything special here - return new Tree<>(leafNode); - } - - @Override - public ITree optimizeOperator(IDiceASTNode operator, IList> children) { - if(!operator.isOperator()) return new Tree<>(operator, children); - - switch((OperatorDiceNode) operator) { - case ADD: - return additionCollapser.collapse(children); - case DIVIDE: - return divideCollapser.collapse(children); - case MULTIPLY: - return multiplyCollapser.collapse(children); - case SUBTRACT: - return subtractCollapser.collapse(children); - case COMPOUND: - return compoundCollapser.collapse(children); - case GROUP: - if(children.getSize() != 2) return new Tree<>(operator, children); - - ComplexDice dice = new ComplexDice(DiceASTUtils.literalToExpression(children.getByIndex(0)), - DiceASTUtils.literalToExpression(children.getByIndex(1))); - - if(dice.canOptimize()) return new Tree<>(new IntegerLiteralNode(dice.optimize())); - - return new Tree<>(operator, children); - case ARRAY: - if(children.getSize() != 1) return new Tree<>(operator, children); - - return children.first(); - case ASSIGN: - case LET: - default: - // We don't optimize these operators - return new Tree<>(operator, children); - } - } -} diff --git a/dice-lang/src/bjc/dicelang/v1/ast/optimization/IOptimizationPass.java b/dice-lang/src/bjc/dicelang/v1/ast/optimization/IOptimizationPass.java deleted file mode 100644 index 082f042..0000000 --- a/dice-lang/src/bjc/dicelang/v1/ast/optimization/IOptimizationPass.java +++ /dev/null @@ -1,33 +0,0 @@ -package bjc.dicelang.v1.ast.optimization; - -import bjc.dicelang.v1.ast.nodes.IDiceASTNode; -import bjc.utils.data.ITree; -import bjc.utils.funcdata.IList; - -/** - * Represents a pass of optimizations over a dice AST - * - * @author ben - * - */ -public interface IOptimizationPass { - /** - * Optimize a leaf in the tree - * - * @param leafNode - * The node to optimize - * @return The optimized node - */ - public ITree optimizeLeaf(IDiceASTNode leafNode); - - /** - * Optimize an operator in an AST node - * - * @param operator - * The operator being optimized - * @param children - * The children of the operator being optimized - * @return The optimized node - */ - public ITree optimizeOperator(IDiceASTNode operator, IList> children); -} diff --git a/dice-lang/src/bjc/dicelang/v1/ast/optimization/OperationCondenser.java b/dice-lang/src/bjc/dicelang/v1/ast/optimization/OperationCondenser.java deleted file mode 100644 index 7ce6f5d..0000000 --- a/dice-lang/src/bjc/dicelang/v1/ast/optimization/OperationCondenser.java +++ /dev/null @@ -1,96 +0,0 @@ -package bjc.dicelang.v1.ast.optimization; - -import bjc.dicelang.v1.ast.nodes.DiceASTType; -import bjc.dicelang.v1.ast.nodes.IDiceASTNode; -import bjc.dicelang.v1.ast.nodes.OperatorDiceNode; -import bjc.utils.data.IHolder; -import bjc.utils.data.ITree; -import bjc.utils.data.Identity; -import bjc.utils.data.TopDownTransformResult; -import bjc.utils.data.Tree; - -/** - * Condenses chained operations into a single level - * - * @author ben - * - */ -public class OperationCondenser { - /** - * Condense chained similiar operations into a single level - * - * @param ast - * The AST to condense - * @return The condensed AST - */ - public static ITree condense(ITree ast) { - return ast.topDownTransform(OperationCondenser::pickNode, OperationCondenser::doCondense); - } - - private static ITree doCondense(ITree ast) { - OperatorDiceNode operation = ast.transformHead((node) -> (OperatorDiceNode) node); - - IHolder canCondense = new Identity<>(true); - - ast.doForChildren((child) -> { - if(canCondense.getValue()) { - canCondense.replace(child.transformHead((node) -> { - if(node.getType() == DiceASTType.OPERATOR) { - if(operation.equals(node)) return true; - - return false; - } - - return true; - })); - } - }); - - if(!canCondense.getValue()) return ast; - - ITree condensedAST = new Tree<>(operation); - - ast.doForChildren((child) -> { - if(child.getHead().getType() == DiceASTType.OPERATOR) { - child.doForChildren((subChild) -> { - condensedAST.addChild(subChild); - }); - } else { - condensedAST.addChild(child); - } - }); - - return condensedAST; - } - - private static TopDownTransformResult pickNode(IDiceASTNode node) { - switch(node.getType()) { - case LITERAL: - return TopDownTransformResult.SKIP; - case OPERATOR: - return pickOperator((OperatorDiceNode) node); - case VARIABLE: - return TopDownTransformResult.SKIP; - default: - throw new UnsupportedOperationException("Attempted to traverse unknown node type " + node); - } - } - - private static TopDownTransformResult pickOperator(OperatorDiceNode node) { - switch(node) { - case ADD: - case MULTIPLY: - case SUBTRACT: - case DIVIDE: - case COMPOUND: - return TopDownTransformResult.PUSHDOWN; - case ARRAY: - case ASSIGN: - case GROUP: - case LET: - return TopDownTransformResult.PASSTHROUGH; - default: - throw new UnsupportedOperationException("Attempted to traverse unknown operator " + node); - } - } -} diff --git a/dice-lang/src/bjc/dicelang/v1/ast/optimization/package-info.java b/dice-lang/src/bjc/dicelang/v1/ast/optimization/package-info.java deleted file mode 100644 index 3c74a21..0000000 --- a/dice-lang/src/bjc/dicelang/v1/ast/optimization/package-info.java +++ /dev/null @@ -1,7 +0,0 @@ -/** - * Contains optimizations on dice ASTs - * - * @author ben - * - */ -package bjc.dicelang.v1.ast.optimization; \ No newline at end of file diff --git a/dice-lang/src/bjc/dicelang/v1/ast/package-info.java b/dice-lang/src/bjc/dicelang/v1/ast/package-info.java deleted file mode 100644 index f1de4f7..0000000 --- a/dice-lang/src/bjc/dicelang/v1/ast/package-info.java +++ /dev/null @@ -1,7 +0,0 @@ -/** - * New implementation of AST for dice-lang - * - * @author ben - * - */ -package bjc.dicelang.v1.ast; \ No newline at end of file -- cgit v1.2.3