diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-04-08 13:29:48 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-04-08 13:29:48 -0400 |
| commit | 90d1cc6c9f47f1b6f74fb57e07865795a46c23b8 (patch) | |
| tree | b74cd5b9989c9f5a1bbde1e1b8c751faf9cb7086 /dice-lang/src/main | |
| parent | b1df3ff8c890bf6d4cc16fb4f28ddb7833512d71 (diff) | |
Change to data interfaces, as well as prepare to rewrite parser
Diffstat (limited to 'dice-lang/src/main')
12 files changed, 249 insertions, 137 deletions
diff --git a/dice-lang/src/main/java/bjc/dicelang/DiceExpressionParser.java b/dice-lang/src/main/java/bjc/dicelang/DiceExpressionParser.java index 05e8942..8a8ded3 100644 --- a/dice-lang/src/main/java/bjc/dicelang/DiceExpressionParser.java +++ b/dice-lang/src/main/java/bjc/dicelang/DiceExpressionParser.java @@ -5,8 +5,8 @@ import java.util.Stack; import org.apache.commons.lang3.StringUtils; -import bjc.utils.funcdata.FunctionalList; import bjc.utils.funcdata.FunctionalStringTokenizer; +import bjc.utils.funcdata.IFunctionalList; import bjc.utils.parserutils.ShuntingYard; /** @@ -25,7 +25,7 @@ public class DiceExpressionParser { * The enviroment to use when parsing expressions * @return The parsed dice expression */ - public IDiceExpression parse(String exp, + public static IDiceExpression parse(String exp, Map<String, IDiceExpression> env) { /* * Create a tokenizer over the strings @@ -50,8 +50,8 @@ public class DiceExpressionParser { /* * Shunt the expression to postfix form */ - FunctionalList<String> ls = - yard.postfix(fst.toList(s -> s), s -> s); + IFunctionalList<String> ls = yard.postfix(fst.toList(s -> s), + s -> s); /* * Create a stack for building an expression from parts @@ -82,8 +82,8 @@ public class DiceExpressionParser { * Handle scalar numbers */ dexps.push(new ScalarDie(Integer.parseInt(tok))); - } catch (NumberFormatException nfex) { - + } catch (@SuppressWarnings("unused") NumberFormatException nfex) { + // We don't care about details, just that it failed if (dexps.size() >= 2) { /* * Apply an operation to two dice diff --git a/dice-lang/src/main/java/bjc/dicelang/PolyhedralDice.java b/dice-lang/src/main/java/bjc/dicelang/PolyhedralDice.java index e0bef64..9f609b4 100644 --- a/dice-lang/src/main/java/bjc/dicelang/PolyhedralDice.java +++ b/dice-lang/src/main/java/bjc/dicelang/PolyhedralDice.java @@ -19,6 +19,15 @@ public class PolyhedralDice { } /** + * Produce a single d10 + * + * @return A single d10 + */ + public static IDiceExpression d10() { + return d10(1); + } + + /** * Produce the specified number of 100-sided dice * * @param nDice @@ -30,6 +39,15 @@ public class PolyhedralDice { } /** + * Produce a single d100 + * + * @return A single d100 + */ + public static IDiceExpression d100() { + return d100(1); + } + + /** * Produce the specified number of 12-sided dice * * @param nDice @@ -41,6 +59,15 @@ public class PolyhedralDice { } /** + * Produce a single d12 + * + * @return A single d12 + */ + public static IDiceExpression d12() { + return d12(1); + } + + /** * Produce the specified number of 20-sided dice * * @param nDice @@ -52,6 +79,15 @@ public class PolyhedralDice { } /** + * Produce a single d20 + * + * @return A single d20 + */ + public static IDiceExpression d20() { + return d20(1); + } + + /** * Produce the specified number of 4-sided dice * * @param nDice @@ -63,6 +99,15 @@ public class PolyhedralDice { } /** + * Produce a single d4 + * + * @return A single d4 + */ + public static IDiceExpression d4() { + return d4(1); + } + + /** * Produce the specified number of 6-sided dice * * @param nDice @@ -74,6 +119,15 @@ public class PolyhedralDice { } /** + * Produce a single d6 + * + * @return A single d6 + */ + public static IDiceExpression d6() { + return d6(1); + } + + /** * Produce the specified number of 8-sided dice * * @param nDice @@ -83,4 +137,13 @@ public class PolyhedralDice { public static IDiceExpression d8(int nDice) { return new ComplexDice(nDice, 8); } + + /** + * Produce a single d8 + * + * @return A single d8 + */ + public static IDiceExpression d8() { + return d8(1); + } } diff --git a/dice-lang/src/main/java/bjc/dicelang/ast/nodes/LiteralDiceNode.java b/dice-lang/src/main/java/bjc/dicelang/ast/nodes/LiteralDiceNode.java index e689c7f..1a6d2bf 100644 --- a/dice-lang/src/main/java/bjc/dicelang/ast/nodes/LiteralDiceNode.java +++ b/dice-lang/src/main/java/bjc/dicelang/ast/nodes/LiteralDiceNode.java @@ -38,7 +38,9 @@ public class LiteralDiceNode implements IDiceASTNode { /** * Create a new node with the given value - * @param val The value for this node + * + * @param val + * The value for this node */ public LiteralDiceNode(int val) { this(Integer.toString(val)); @@ -126,8 +128,12 @@ public class LiteralDiceNode implements IDiceASTNode { try { return new ScalarDie(Integer.parseInt(literalData)); } catch (NumberFormatException nfex) { - throw new UnsupportedOperationException( + UnsupportedOperationException usex = new UnsupportedOperationException( "Found malformed leaf token " + this); + + usex.initCause(nfex); + + throw usex; } } } @@ -165,7 +171,8 @@ public class LiteralDiceNode implements IDiceASTNode { try { Integer.parseInt(value); return true; - } catch (NumberFormatException nfex) { + } catch (@SuppressWarnings("unused") NumberFormatException nfex) { + // We don't care about details return false; } } diff --git a/dice-lang/src/main/java/bjc/dicelang/ast/nodes/OperatorDiceNode.java b/dice-lang/src/main/java/bjc/dicelang/ast/nodes/OperatorDiceNode.java index e1eb316..2820cfb 100644 --- a/dice-lang/src/main/java/bjc/dicelang/ast/nodes/OperatorDiceNode.java +++ b/dice-lang/src/main/java/bjc/dicelang/ast/nodes/OperatorDiceNode.java @@ -38,7 +38,11 @@ public enum OperatorDiceNode implements IDiceASTNode { /** * Represents subtracting two nodes */ - SUBTRACT; + SUBTRACT, + /** + * Represents executing one statement in the context of the other + */ + LET; /** * Create a operator node from a string @@ -63,6 +67,8 @@ public enum OperatorDiceNode implements IDiceASTNode { return GROUP; case "c": return COMPOUND; + case "->": + return LET; default: throw new IllegalArgumentException( s + " is not a valid operator node"); diff --git a/dice-lang/src/main/java/bjc/dicelang/ast/optimization/DiceASTOptimizer.java b/dice-lang/src/main/java/bjc/dicelang/ast/optimization/DiceASTOptimizer.java index e6c62ee..f6be7ab 100644 --- a/dice-lang/src/main/java/bjc/dicelang/ast/optimization/DiceASTOptimizer.java +++ b/dice-lang/src/main/java/bjc/dicelang/ast/optimization/DiceASTOptimizer.java @@ -36,26 +36,26 @@ public class DiceASTOptimizer { @Override public AST<IDiceASTNode> apply(AST<IDiceASTNode> leftAST, AST<IDiceASTNode> rightAST) { - AST<IDiceASTNode> rightBranchOfLeftAST = - leftAST.applyToRight((rightSideAST) -> rightSideAST); - AST<IDiceASTNode> leftBranchOfLeftAST = - leftAST.applyToRight((rightSideAST) -> rightSideAST); + AST<IDiceASTNode> rightBranchOfLeftAST = leftAST + .applyToRight((rightSideAST) -> rightSideAST); + AST<IDiceASTNode> leftBranchOfLeftAST = leftAST + .applyToRight((rightSideAST) -> rightSideAST); boolean leftContainsNestedConstant = DiceASTOptimizer .checkNodeType(rightBranchOfLeftAST, LITERAL) && DiceASTOptimizer.isNodeConstant(leftAST); - boolean isRightConstant = - DiceASTOptimizer.checkNodeType(rightAST, 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<IDiceASTNode> newRightBranch = - new AST<>(new LiteralDiceNode(combinedValue)); + AST<IDiceASTNode> newRightBranch = new AST<>( + new LiteralDiceNode(combinedValue)); return new AST<>(type, leftBranchOfLeftAST, newRightBranch); @@ -82,13 +82,13 @@ public class DiceASTOptimizer { @Override public AST<IDiceASTNode> apply(AST<IDiceASTNode> leftAST, AST<IDiceASTNode> rightAST) { - boolean isLeftConstant = - DiceASTOptimizer.checkNodeType(leftAST, LITERAL) - && DiceASTOptimizer.isNodeConstant(leftAST); + boolean isLeftConstant = DiceASTOptimizer + .checkNodeType(leftAST, LITERAL) + && DiceASTOptimizer.isNodeConstant(leftAST); - boolean isRightConstant = - DiceASTOptimizer.checkNodeType(rightAST, LITERAL) - && DiceASTOptimizer.isNodeConstant(leftAST); + boolean isRightConstant = DiceASTOptimizer + .checkNodeType(rightAST, LITERAL) + && DiceASTOptimizer.isNodeConstant(leftAST); if (isLeftConstant) { if (isRightConstant) { @@ -107,10 +107,8 @@ public class DiceASTOptimizer { } } - private static Map<IDiceASTNode, BinaryOperator<AST<IDiceASTNode>>> - buildConstantCollapsers() { - Map<IDiceASTNode, BinaryOperator<AST<IDiceASTNode>>> operatorCollapsers = - new HashMap<>(); + private static Map<IDiceASTNode, BinaryOperator<AST<IDiceASTNode>>> buildConstantCollapsers() { + Map<IDiceASTNode, BinaryOperator<AST<IDiceASTNode>>> operatorCollapsers = new HashMap<>(); operatorCollapsers.put(OperatorDiceNode.ADD, new ArithmeticOperationCollapser(OperatorDiceNode.ADD, @@ -131,10 +129,8 @@ public class DiceASTOptimizer { return operatorCollapsers; } - private static Map<IDiceASTNode, BinaryOperator<AST<IDiceASTNode>>> - buildNestedConstantCollapsers() { - Map<IDiceASTNode, BinaryOperator<AST<IDiceASTNode>>> operatorCollapsers = - new HashMap<>(); + private static Map<IDiceASTNode, BinaryOperator<AST<IDiceASTNode>>> buildNestedConstantCollapsers() { + Map<IDiceASTNode, BinaryOperator<AST<IDiceASTNode>>> operatorCollapsers = new HashMap<>(); operatorCollapsers.put(OperatorDiceNode.ADD, new NestedArithmeticOperationCollapser( @@ -167,9 +163,9 @@ public class DiceASTOptimizer { IDiceExpression leaf) { if (leaf.canOptimize()) { return new LiteralDiceNode(Integer.toString(leaf.optimize())); - } else { - return node; } + + return node; } private static AST<IDiceASTNode> finishTree(AST<IDiceASTNode> tree) { @@ -184,14 +180,13 @@ public class DiceASTOptimizer { * @return The optimized tree */ public static AST<IDiceASTNode> optimizeTree(AST<IDiceASTNode> tree) { - AST<IDiceASTNode> astWithConstantsFolded = - tree.collapse(DiceASTOptimizer::collapseLeaf, - buildConstantCollapsers()::get, - DiceASTOptimizer::finishTree); + AST<IDiceASTNode> astWithConstantsFolded = tree.collapse( + DiceASTOptimizer::collapseLeaf, + buildConstantCollapsers()::get, + DiceASTOptimizer::finishTree); - AST<IDiceASTNode> astWithNestedConstantsFolded = - astWithConstantsFolded.collapse( - DiceASTOptimizer::collapseLeaf, + AST<IDiceASTNode> astWithNestedConstantsFolded = astWithConstantsFolded + .collapse(DiceASTOptimizer::collapseLeaf, buildNestedConstantCollapsers()::get, DiceASTOptimizer::finishTree); diff --git a/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTDefinedChecker.java b/dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTDefinedChecker.java index c26098c..4a20a82 100644 --- a/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTDefinedChecker.java +++ b/dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTDefinedChecker.java @@ -1,4 +1,4 @@ -package bjc.dicelang.ast; +package bjc.dicelang.old.ast; import java.util.Map; import java.util.function.Consumer; diff --git a/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTExpression.java b/dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTExpression.java index 3552926..7651093 100644 --- a/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTExpression.java +++ b/dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTExpression.java @@ -1,7 +1,8 @@ -package bjc.dicelang.ast; +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; @@ -24,6 +25,37 @@ import bjc.utils.parserutils.AST; * */ public class DiceASTExpression implements IDiceExpression { + private static final class ArithmeticCollapser + implements IOperatorCollapser { + private OperatorDiceNode type; + + private BinaryOperator<Integer> valueOp; + + public ArithmeticCollapser(OperatorDiceNode type, + BinaryOperator<Integer> valueOp) { + this.type = type; + this.valueOp = valueOp; + } + + @Override + public Pair<Integer, AST<IDiceASTNode>> apply( + Pair<Integer, AST<IDiceASTNode>> leftNode, + Pair<Integer, AST<IDiceASTNode>> 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<IDiceASTNode, String> { @@ -47,59 +79,26 @@ public class DiceASTExpression implements IDiceExpression { * The enviroment to evaluate bindings and such against * @return The operations to use when collapsing the AST */ - private static Map<IDiceASTNode, IOperatorCollapser> buildOperations( - Map<String, DiceASTExpression> enviroment) { - Map<IDiceASTNode, IOperatorCollapser> operatorCollapsers = new HashMap<>(); + private static Map<IDiceASTNode, IOperatorCollapser> + buildOperations(Map<String, DiceASTExpression> enviroment) { + Map<IDiceASTNode, IOperatorCollapser> operatorCollapsers = + new HashMap<>(); operatorCollapsers.put(OperatorDiceNode.ADD, - (leftNode, rightNode) -> { - return leftNode.merge((leftValue, leftAST) -> { - return rightNode.merge((rightValue, rightAST) -> { - return new Pair<>(leftValue + rightValue, - new AST<>(OperatorDiceNode.ADD, - leftAST, rightAST)); - }); - }); + new ArithmeticCollapser(OperatorDiceNode.ADD, + (left, right) -> left + right)); - }); operatorCollapsers.put(OperatorDiceNode.SUBTRACT, - (leftNode, rightNode) -> { - return leftNode.merge((leftValue, leftAST) -> { - return rightNode.merge((rightValue, rightAST) -> { - return new Pair<>(leftValue - rightValue, - new AST<>(OperatorDiceNode.SUBTRACT, - leftAST, rightAST)); - }); - }); - }); + new ArithmeticCollapser(OperatorDiceNode.SUBTRACT, + (left, right) -> left - right)); operatorCollapsers.put(OperatorDiceNode.MULTIPLY, - (leftNode, rightNode) -> { - return leftNode.merge((leftValue, leftAST) -> { - return rightNode.merge((rightValue, rightAST) -> { - return new Pair<>(leftValue * rightValue, - new AST<>(OperatorDiceNode.MULTIPLY, - leftAST, rightAST)); - }); - }); + new ArithmeticCollapser(OperatorDiceNode.MULTIPLY, + (left, right) -> left * right)); - }); operatorCollapsers.put(OperatorDiceNode.DIVIDE, - (leftNode, rightNode) -> { - return leftNode.merge((leftValue, leftAST) -> { - return rightNode.merge((rightValue, rightAST) -> { - if (rightValue == 0) { - throw new ArithmeticException( - "Attempted to divide by zero. The AST of the problem expression is " - + rightAST); - } - - return new Pair<>(leftValue / rightValue, - new AST<>(OperatorDiceNode.DIVIDE, - leftAST, rightAST)); - }); - }); - }); + new ArithmeticCollapser(OperatorDiceNode.DIVIDE, + (left, right) -> left / right)); operatorCollapsers.put(OperatorDiceNode.ASSIGN, (left, right) -> { return parseBinding(enviroment, left, right); @@ -111,9 +110,46 @@ public class DiceASTExpression implements IDiceExpression { operatorCollapsers.put(OperatorDiceNode.GROUP, DiceASTExpression::parseGroup); + operatorCollapsers.put(OperatorDiceNode.LET, (left, right) -> { + return doLet(enviroment, left, right); + }); + return operatorCollapsers; } + private static Pair<Integer, AST<IDiceASTNode>> doLet( + Map<String, DiceASTExpression> enviroment, + Pair<Integer, AST<IDiceASTNode>> left, + Pair<Integer, AST<IDiceASTNode>> 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); + } else { + // Left block has an assignment to handle + String varName = leftAST.applyToLeft((leftBranch) -> { + return getAssignmentVar(leftBranch); + }); + + return null; + } + }); + }); + } + + private static String getAssignmentVar(AST<IDiceASTNode> leftBranch) { + return leftBranch.applyToHead((node) -> { + return ((VariableDiceNode) node).getVariable(); + }); + } + + private static Boolean isAssignNode(IDiceASTNode node) { + return node.getType() == DiceASTType.OPERATOR + && node == OperatorDiceNode.ASSIGN; + } + private static Pair<Integer, AST<IDiceASTNode>> parseBinding( Map<String, DiceASTExpression> enviroment, Pair<Integer, AST<IDiceASTNode>> left, @@ -129,8 +165,9 @@ public class DiceASTExpression implements IDiceExpression { GenHolder<Boolean> selfReference = new GenHolder<>(false); - DiceASTReferenceChecker refChecker = new DiceASTReferenceChecker( - selfReference, variableName); + DiceASTReferenceChecker refChecker = + new DiceASTReferenceChecker(selfReference, + variableName); rightAST.traverse(TreeLinearizationMethod.PREORDER, refChecker); @@ -163,8 +200,8 @@ public class DiceASTExpression implements IDiceExpression { Pair<Integer, AST<IDiceASTNode>> rightNode) { return leftNode.merge((leftValue, leftAST) -> { return rightNode.merge((rightValue, rightAST) -> { - int compoundValue = Integer - .parseInt(Integer.toString(leftValue) + int compoundValue = + Integer.parseInt(Integer.toString(leftValue) + Integer.toString(rightValue)); return new Pair<>(compoundValue, new AST<>( @@ -190,8 +227,8 @@ public class DiceASTExpression implements IDiceExpression { + rightAST); } - int rolledValue = new ComplexDice(leftValue, rightValue) - .roll(); + int rolledValue = + new ComplexDice(leftValue, rightValue).roll(); return new Pair<>(rolledValue, new AST<>( OperatorDiceNode.GROUP, leftAST, rightAST)); @@ -231,8 +268,8 @@ public class DiceASTExpression implements IDiceExpression { * @return A pair consisting of the token's value and the AST it * represents */ - private Pair<Integer, AST<IDiceASTNode>> evaluateLeaf( - IDiceASTNode leafNode) { + private Pair<Integer, AST<IDiceASTNode>> + evaluateLeaf(IDiceASTNode leafNode) { if (leafNode.getType() == DiceASTType.VARIABLE) { VariableDiceNode node = (VariableDiceNode) leafNode; @@ -247,8 +284,8 @@ public class DiceASTExpression implements IDiceExpression { } } - private Pair<Integer, AST<IDiceASTNode>> parseVariable( - VariableDiceNode leafNode) { + private Pair<Integer, AST<IDiceASTNode>> + parseVariable(VariableDiceNode leafNode) { String varName = leafNode.getVariable(); if (env.containsKey(varName)) { @@ -276,8 +313,8 @@ public class DiceASTExpression implements IDiceExpression { */ @Override public int roll() { - Map<IDiceASTNode, IOperatorCollapser> operations = buildOperations( - env); + Map<IDiceASTNode, IOperatorCollapser> operations = + buildOperations(env); return ast.collapse(this::evaluateLeaf, operations::get, (returnedValue) -> returnedValue diff --git a/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTFlattener.java b/dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTFlattener.java index a54a088..a7500a8 100644 --- a/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTFlattener.java +++ b/dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTFlattener.java @@ -1,4 +1,4 @@ -package bjc.dicelang.ast; +package bjc.dicelang.old.ast; import java.util.HashMap; import java.util.Map; diff --git a/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTInliner.java b/dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTInliner.java index ef4a904..3a8e796 100644 --- a/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTInliner.java +++ b/dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTInliner.java @@ -1,4 +1,4 @@ -package bjc.dicelang.ast; +package bjc.dicelang.old.ast; import java.util.function.Function; @@ -7,6 +7,8 @@ import bjc.dicelang.ast.nodes.IDiceASTNode; import bjc.dicelang.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; /** @@ -19,9 +21,9 @@ import bjc.utils.parserutils.AST; public class DiceASTInliner { private static class NodeInliner implements Function<IDiceASTNode, AST<IDiceASTNode>> { - private FunctionalMap<String, AST<IDiceASTNode>> enviroment; + private IFunctionalMap<String, AST<IDiceASTNode>> enviroment; - public NodeInliner(FunctionalMap<String, AST<IDiceASTNode>> env) { + public NodeInliner(IFunctionalMap<String, AST<IDiceASTNode>> env) { enviroment = env; } @@ -29,13 +31,13 @@ public class DiceASTInliner { public AST<IDiceASTNode> apply(IDiceASTNode nod) { if (nod.getType() == DiceASTType.VARIABLE) { return expandNode((VariableDiceNode) nod); - } else { - return new AST<>(nod); } + + return new AST<>(nod); } - protected AST<IDiceASTNode> - expandNode(VariableDiceNode variableNode) { + protected AST<IDiceASTNode> expandNode( + VariableDiceNode variableNode) { String varName = variableNode.getVariable(); if (!enviroment.containsKey(varName)) { @@ -50,24 +52,24 @@ public class DiceASTInliner { private static final class SelectiveInliner extends NodeInliner { - private FunctionalList<String> variableNames; + private IFunctionalList<String> variableNames; public SelectiveInliner( - FunctionalMap<String, AST<IDiceASTNode>> env, - FunctionalList<String> varNames) { + IFunctionalMap<String, AST<IDiceASTNode>> env, + IFunctionalList<String> varNames) { super(env); variableNames = varNames; } @Override - protected AST<IDiceASTNode> - expandNode(VariableDiceNode variableNode) { + protected AST<IDiceASTNode> expandNode( + VariableDiceNode variableNode) { if (variableNames.contains(variableNode.getVariable())) { return super.expandNode(variableNode); - } else { - return new AST<>(variableNode); } + + return new AST<>(variableNode); } } @@ -81,7 +83,7 @@ public class DiceASTInliner { * @return The tree with references inlined */ public static AST<IDiceASTNode> inlineAST(AST<IDiceASTNode> tree, - FunctionalMap<String, AST<IDiceASTNode>> env) { + IFunctionalMap<String, AST<IDiceASTNode>> env) { return selectiveInline(tree, env); } @@ -112,7 +114,7 @@ public class DiceASTInliner { * @return An AST with the specified variables inlined */ public static AST<IDiceASTNode> selectiveInline(AST<IDiceASTNode> tree, - FunctionalMap<String, AST<IDiceASTNode>> env, + IFunctionalMap<String, AST<IDiceASTNode>> env, String... varNames) { return selectiveInline(tree, env, new FunctionalList<>(varNames)); } @@ -129,8 +131,8 @@ public class DiceASTInliner { * @return An AST with the specified variables inline */ public static AST<IDiceASTNode> selectiveInline(AST<IDiceASTNode> tree, - FunctionalMap<String, AST<IDiceASTNode>> env, - FunctionalList<String> varNames) { - return tree.expand(new SelectiveInliner(env, varNames)); + IFunctionalMap<String, AST<IDiceASTNode>> env, + IFunctionalList<String> varNames) { + return tree.flatMapTree(new SelectiveInliner(env, varNames)); } }
\ No newline at end of file diff --git a/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTParser.java b/dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTParser.java index fb77ad5..d8e13f7 100644 --- a/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTParser.java +++ b/dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTParser.java @@ -1,4 +1,4 @@ -package bjc.dicelang.ast; +package bjc.dicelang.old.ast; import java.util.Deque; import java.util.LinkedList; @@ -10,9 +10,10 @@ import bjc.dicelang.ast.nodes.IDiceASTNode; import bjc.dicelang.ast.nodes.LiteralDiceNode; import bjc.dicelang.ast.nodes.OperatorDiceNode; import bjc.dicelang.ast.nodes.VariableDiceNode; +import bjc.utils.data.IPair; import bjc.utils.data.Pair; -import bjc.utils.funcdata.FunctionalList; 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; @@ -56,7 +57,8 @@ public class DiceASTParser { try { Integer.parseInt(tok); return true; - } catch (NumberFormatException nfx) { + } catch (@SuppressWarnings("unused") NumberFormatException nfex) { + // We don't care about details return false; } } @@ -86,11 +88,11 @@ public class DiceASTParser { * The string to build from * @return An AST built from the passed in string */ - public AST<IDiceASTNode> buildAST(String exp) { - FunctionalList<String> tokens = - FunctionalStringTokenizer.fromString(exp).toList(); + public static AST<IDiceASTNode> buildAST(String exp) { + IFunctionalList<String> tokens = FunctionalStringTokenizer + .fromString(exp).toList(); - Deque<Pair<String, String>> ops = new LinkedList<>(); + Deque<IPair<String, String>> ops = new LinkedList<>(); ops.add(new Pair<>("+", "\\+")); ops.add(new Pair<>("-", "-")); @@ -98,19 +100,19 @@ public class DiceASTParser { ops.add(new Pair<>("/", "/")); ops.add(new Pair<>(":=", ":=")); - FunctionalList<String> semiExpandedTokens = - ListUtils.splitTokens(tokens, ops); + IFunctionalList<String> semiExpandedTokens = ListUtils + .splitTokens(tokens, ops); ops = new LinkedList<>(); ops.add(new Pair<>("(", "\\(")); ops.add(new Pair<>(")", "\\)")); - FunctionalList<String> fullyExpandedTokens = - ListUtils.deAffixTokens(semiExpandedTokens, ops); + IFunctionalList<String> fullyExpandedTokens = ListUtils + .deAffixTokens(semiExpandedTokens, ops); - FunctionalList<String> shunted = - yard.postfix(fullyExpandedTokens, (s) -> s); + IFunctionalList<String> shunted = yard.postfix(fullyExpandedTokens, + (s) -> s); AST<String> rawAST = TreeConstructor.constructTree(shunted, DiceASTParser::isOperator); diff --git a/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTReferenceChecker.java b/dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTReferenceChecker.java index 8fa4d55..e526ee9 100644 --- a/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTReferenceChecker.java +++ b/dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTReferenceChecker.java @@ -1,4 +1,4 @@ -package bjc.dicelang.ast; +package bjc.dicelang.old.ast; import java.util.function.Consumer; diff --git a/dice-lang/src/main/java/bjc/dicelang/ast/IOperatorCollapser.java b/dice-lang/src/main/java/bjc/dicelang/old/ast/IOperatorCollapser.java index c872583..65cb264 100644 --- a/dice-lang/src/main/java/bjc/dicelang/ast/IOperatorCollapser.java +++ b/dice-lang/src/main/java/bjc/dicelang/old/ast/IOperatorCollapser.java @@ -1,4 +1,4 @@ -package bjc.dicelang.ast; +package bjc.dicelang.old.ast; import java.util.function.BinaryOperator; @@ -14,5 +14,5 @@ import bjc.utils.parserutils.AST; */ public interface IOperatorCollapser extends BinaryOperator<Pair<Integer, AST<IDiceASTNode>>> { - + // Just an alias } |
