diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-04-04 10:08:00 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-04-04 10:08:00 -0400 |
| commit | 66b3ea905d077577a32ed82983b0cd9e8ee10bea (patch) | |
| tree | e1038a380c0714d4dda986514b365b9e474d3c15 /dice-lang/src/main/java/bjc/dicelang/ast/DiceASTFreezer.java | |
| parent | b296ecf265120a0cac9cc5c558bdc60c6a27fff2 (diff) | |
Work on optimizations
Diffstat (limited to 'dice-lang/src/main/java/bjc/dicelang/ast/DiceASTFreezer.java')
| -rw-r--r-- | dice-lang/src/main/java/bjc/dicelang/ast/DiceASTFreezer.java | 136 |
1 files changed, 0 insertions, 136 deletions
diff --git a/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTFreezer.java b/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTFreezer.java deleted file mode 100644 index 6fa4883..0000000 --- a/dice-lang/src/main/java/bjc/dicelang/ast/DiceASTFreezer.java +++ /dev/null @@ -1,136 +0,0 @@ -package bjc.dicelang.ast; - -import java.util.function.Function; - -import bjc.dicelang.ast.nodes.DiceASTType; -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.parserutils.AST; - -/** - * Freeze references in a dice AST, replacing variable references with what - * the variables refer to - * - * @author ben - * - */ -public class DiceASTFreezer { - private static class NodeFreezer - implements Function<IDiceASTNode, AST<IDiceASTNode>> { - private FunctionalMap<String, AST<IDiceASTNode>> enviroment; - - public NodeFreezer(FunctionalMap<String, AST<IDiceASTNode>> env) { - enviroment = env; - } - - @Override - public AST<IDiceASTNode> apply(IDiceASTNode nod) { - if (nod.getType() == DiceASTType.VARIABLE) { - return expandNode((VariableDiceNode) nod); - } else { - return new AST<>(nod); - } - } - - protected AST<IDiceASTNode> - expandNode(VariableDiceNode variableNode) { - String varName = variableNode.getVariable(); - - if (!enviroment.containsKey(varName)) { - throw new IllegalArgumentException( - "Attempted to freeze reference" - + " to an undefined variable " + varName); - } - - return enviroment.get(varName); - } - } - - private static final class SelectiveFreezer extends NodeFreezer { - - private FunctionalList<String> variableNames; - - public SelectiveFreezer( - FunctionalMap<String, AST<IDiceASTNode>> env, - FunctionalList<String> varNames) { - super(env); - - variableNames = varNames; - } - - @Override - protected AST<IDiceASTNode> - expandNode(VariableDiceNode variableNode) { - if (variableNames.contains(variableNode.getVariable())) { - return super.expandNode(variableNode); - } else { - return new AST<>(variableNode); - } - } - } - - /** - * Freeze the references in an AST - * - * @param tree - * The tree to freeze references in - * @param env - * The enviroment to get reference values from - * @return The tree with references frozen - */ - public static AST<IDiceASTNode> freezeAST(AST<IDiceASTNode> tree, - FunctionalMap<String, AST<IDiceASTNode>> env) { - return selectiveFreeze(tree, env); - } - - /** - * Freeze the references in an expression backed by an AST - * - * @param tree - * The tree-backed expression to freeze references in - * @param env - * The enviroment to get reference values from - * @return The tree with references frozen - */ - public static AST<IDiceASTNode> freezeAST(DiceASTExpression tree, - FunctionalMap<String, DiceASTExpression> 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<IDiceASTNode> selectiveFreeze(AST<IDiceASTNode> tree, - FunctionalMap<String, AST<IDiceASTNode>> 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<IDiceASTNode> selectiveFreeze(AST<IDiceASTNode> tree, - FunctionalMap<String, AST<IDiceASTNode>> env, - FunctionalList<String> varNames) { - return tree.expand(new SelectiveFreezer(env, varNames)); - } -}
\ No newline at end of file |
