From c8ae1ec096f5d1ac6db4f3a0035f7da106444e4e Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sun, 3 Apr 2016 17:45:05 -0400 Subject: General code refactoring and maintenance --- .../main/java/bjc/dicelang/ast/DiceASTFreezer.java | 144 +++++++++++++-------- 1 file changed, 92 insertions(+), 52 deletions(-) (limited to 'dice-lang/src/main/java/bjc/dicelang/ast/DiceASTFreezer.java') diff --git a/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTFreezer.java b/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTFreezer.java index 0e2134b..bad24f8 100644 --- a/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTFreezer.java +++ b/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTFreezer.java @@ -1,7 +1,9 @@ package bjc.dicelang.ast; -import java.util.Map; +import java.util.function.Function; +import bjc.utils.funcdata.FunctionalList; +import bjc.utils.funcdata.FunctionalMap; import bjc.utils.parserutils.AST; /** @@ -12,32 +14,57 @@ import bjc.utils.parserutils.AST; * */ public class DiceASTFreezer { - /** - * Expand a reference - * - * @param vnode - * The node containing the reference to expand - * @param env - * The enviroment to expand against - * @return The expanded reference - */ - private static AST expandNode(VariableDiceNode vnode, - Map> env) { - return env.get(vnode.getVariable()); + private static class NodeFreezer + implements Function> { + private FunctionalMap> enviroment; + + public NodeFreezer(FunctionalMap> env) { + enviroment = env; + } + + @Override + public AST apply(IDiceASTNode nod) { + if (nod.getType() == DiceASTType.VARIABLE) { + return expandNode((VariableDiceNode) nod); + } else { + 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); + } } - /** - * Expand a reference - * - * @param vnode - * The node containing the reference to expand - * @param env - * The enviroment to expand against - * @return The expanded reference - */ - private static AST expandNode2(VariableDiceNode vnode, - Map env) { - return env.get(vnode.getVariable()).getAst(); + private static final class SelectiveFreezer extends NodeFreezer { + + private FunctionalList variableNames; + + public SelectiveFreezer( + FunctionalMap> env, + FunctionalList varNames) { + super(env); + variableNames = varNames; + } + + @Override + protected AST + expandNode(VariableDiceNode variableNode) { + if (variableNames.contains(variableNode.getVariable())) { + return super.expandNode(variableNode); + } else { + return new AST<>(variableNode); + } + } } /** @@ -49,20 +76,9 @@ public class DiceASTFreezer { * The enviroment to get reference values from * @return The tree with references frozen */ - @SuppressWarnings("unused") public static AST freezeAST(AST tree, - Map> env) { - return tree.collapse((nod) -> { - if (nod instanceof VariableDiceNode) { - return expandNode((VariableDiceNode) nod, env); - } else { - // Type is specified here so compiler can know the type - // we're using - return new AST(nod); - } - } , (op) -> (left, right) -> { - return new AST(op, left, right); - } , (r) -> r); + FunctionalMap> env) { + return selectiveFreeze(tree, env); } /** @@ -74,19 +90,43 @@ public class DiceASTFreezer { * The enviroment to get reference values from * @return The tree with references frozen */ - @SuppressWarnings("unused") public static AST freezeAST(DiceASTExpression tree, - Map env) { - return tree.getAst().collapse((nod) -> { - if (nod instanceof VariableDiceNode) { - return expandNode2((VariableDiceNode) nod, env); - } else { - // Type is specified here so compiler can know the type - // we're using - return new AST(nod); - } - } , (op) -> (left, right) -> { - return new AST(op, left, right); - } , (r) -> r); + FunctionalMap env) { + return freezeAST(tree.getAst(), + env.mapValues(expression -> expression.getAst())); + } + + /** + * Freeze references to specified variables + * + * @param tree + * The tree-backed expression to freeze references in + * @param env + * The enviroment to resolve variables against + * @param varNames + * The names of the variables to freeze + * @return An AST with the specified variables frozen + */ + public static AST selectiveFreeze(AST tree, + FunctionalMap> env, + String... varNames) { + return selectiveFreeze(tree, env, new FunctionalList<>(varNames)); + } + + /** + * Freeze references to specified variables + * + * @param tree + * The tree-backed expression to freeze references in + * @param env + * The enviroment to resolve variables against + * @param varNames + * The names of the variables to freeze + * @return An AST with the specified variables frozen + */ + public static AST selectiveFreeze(AST tree, + FunctionalMap> env, + FunctionalList varNames) { + return tree.expand(new SelectiveFreezer(env, varNames)); } -} +} \ No newline at end of file -- cgit v1.2.3