From 90d1cc6c9f47f1b6f74fb57e07865795a46c23b8 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Fri, 8 Apr 2016 13:29:48 -0400 Subject: Change to data interfaces, as well as prepare to rewrite parser --- .../java/bjc/dicelang/old/ast/DiceASTInliner.java | 138 +++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTInliner.java (limited to 'dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTInliner.java') diff --git a/dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTInliner.java b/dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTInliner.java new file mode 100644 index 0000000..3a8e796 --- /dev/null +++ b/dice-lang/src/main/java/bjc/dicelang/old/ast/DiceASTInliner.java @@ -0,0 +1,138 @@ +package bjc.dicelang.old.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.funcdata.IFunctionalList; +import bjc.utils.funcdata.IFunctionalMap; +import bjc.utils.parserutils.AST; + +/** + * Inline references in a dice AST, replacing variable references with what + * the variables refer to + * + * @author ben + * + */ +public class DiceASTInliner { + private static class NodeInliner + implements Function> { + private IFunctionalMap> enviroment; + + public NodeInliner(IFunctionalMap> env) { + enviroment = env; + } + + @Override + public AST apply(IDiceASTNode nod) { + if (nod.getType() == DiceASTType.VARIABLE) { + return expandNode((VariableDiceNode) nod); + } + + return new AST<>(nod); + } + + protected AST expandNode( + VariableDiceNode variableNode) { + String varName = variableNode.getVariable(); + + if (!enviroment.containsKey(varName)) { + throw new IllegalArgumentException( + "Attempted to freeze reference" + + " to an undefined variable " + varName); + } + + return enviroment.get(varName); + } + } + + private static final class SelectiveInliner extends NodeInliner { + + private IFunctionalList variableNames; + + public SelectiveInliner( + IFunctionalMap> env, + IFunctionalList varNames) { + super(env); + + variableNames = varNames; + } + + @Override + protected AST expandNode( + VariableDiceNode variableNode) { + if (variableNames.contains(variableNode.getVariable())) { + return super.expandNode(variableNode); + } + + return new AST<>(variableNode); + } + } + + /** + * Inline the references in an AST + * + * @param tree + * The tree to inline references in + * @param env + * The enviroment to get reference values from + * @return The tree with references inlined + */ + public static AST inlineAST(AST tree, + IFunctionalMap> env) { + return selectiveInline(tree, env); + } + + /** + * Inline the references in an expression backed by an AST + * + * @param tree + * The tree-backed expression to inline references in + * @param env + * The enviroment to get reference values from + * @return The tree with references inlined + */ + public static AST inlineAST(DiceASTExpression tree, + FunctionalMap env) { + return inlineAST(tree.getAst(), + env.mapValues(expression -> expression.getAst())); + } + + /** + * Inline references to specified variables + * + * @param tree + * The tree-backed expression to inline references in + * @param env + * The enviroment to resolve variables against + * @param varNames + * The names of the variables to inline + * @return An AST with the specified variables inlined + */ + public static AST selectiveInline(AST tree, + IFunctionalMap> env, + String... varNames) { + return selectiveInline(tree, env, new FunctionalList<>(varNames)); + } + + /** + * Inline references to specified variables + * + * @param tree + * The tree-backed expression to inline references in + * @param env + * The enviroment to resolve variables against + * @param varNames + * The names of the variables to inline + * @return An AST with the specified variables inline + */ + public static AST selectiveInline(AST tree, + IFunctionalMap> env, + IFunctionalList varNames) { + return tree.flatMapTree(new SelectiveInliner(env, varNames)); + } +} \ No newline at end of file -- cgit v1.2.3