summaryrefslogtreecommitdiff
path: root/dice-lang/src/bjc/dicelang/v1/ast/DiceASTReferenceSanitizer.java
diff options
context:
space:
mode:
Diffstat (limited to 'dice-lang/src/bjc/dicelang/v1/ast/DiceASTReferenceSanitizer.java')
-rw-r--r--dice-lang/src/bjc/dicelang/v1/ast/DiceASTReferenceSanitizer.java105
1 files changed, 45 insertions, 60 deletions
diff --git a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTReferenceSanitizer.java b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTReferenceSanitizer.java
index 79c0ce7..5bb07fd 100644
--- a/dice-lang/src/bjc/dicelang/v1/ast/DiceASTReferenceSanitizer.java
+++ b/dice-lang/src/bjc/dicelang/v1/ast/DiceASTReferenceSanitizer.java
@@ -12,9 +12,8 @@ import bjc.utils.data.TopDownTransformResult;
import bjc.utils.data.Tree;
/**
- * 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
+ * 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
*
@@ -23,8 +22,7 @@ public class DiceASTReferenceSanitizer {
private static ITree<IDiceASTNode> doSanitize(ITree<IDiceASTNode> ast,
IMap<String, ITree<IDiceASTNode>> enviroment) {
if (ast.getChildrenCount() != 2) {
- throw new UnsupportedOperationException(
- "Assignment must have two arguments.");
+ throw new UnsupportedOperationException("Assignment must have two arguments.");
}
ITree<IDiceASTNode> nameTree = ast.getChild(0);
@@ -36,8 +34,7 @@ public class DiceASTReferenceSanitizer {
nameTree.doForChildren((child) -> {
if (allSimpleVariables.getValue()) {
- boolean isSimple = DiceASTUtils
- .containsSimpleVariable(child);
+ boolean isSimple = DiceASTUtils.containsSimpleVariable(child);
allSimpleVariables.replace(isSimple);
}
@@ -50,8 +47,7 @@ public class DiceASTReferenceSanitizer {
}
if (valueTree.getHead() == OperatorDiceNode.ARRAY) {
- if (nameTree.getChildrenCount() != valueTree
- .getChildrenCount()) {
+ if (nameTree.getChildrenCount() != valueTree.getChildrenCount()) {
throw new UnsupportedOperationException(
"Array assignment between arrays must be"
+ " between two arrays of equal length");
@@ -67,24 +63,21 @@ public class DiceASTReferenceSanitizer {
if (valueTree.getHead() == OperatorDiceNode.ARRAY) {
IHolder<Integer> childCounter = new Identity<>(0);
- ITree<IDiceASTNode> returnTree = new Tree<>(
- OperatorDiceNode.ARRAY);
+ ITree<IDiceASTNode> returnTree = new Tree<>(OperatorDiceNode.ARRAY);
nameTree.doForChildren((child) -> {
String variableName = child.transformHead((node) -> {
return ((VariableDiceNode) node).getVariable();
});
- ITree<IDiceASTNode> currentValue = valueTree
- .getChild(childCounter.getValue());
+ ITree<IDiceASTNode> currentValue = valueTree.getChild(childCounter.getValue());
- ITree<IDiceASTNode> sanitizedSubtree = doSingleSanitize(
- ast, enviroment, child, currentValue,
- variableName);
+ ITree<IDiceASTNode> sanitizedSubtree = doSingleSanitize(ast, enviroment, child,
+ currentValue, variableName);
if (sanitizedSubtree == null) {
- ITree<IDiceASTNode> oldTree = new Tree<>(
- ast.getHead(), child, currentValue);
+ ITree<IDiceASTNode> oldTree = new Tree<>(ast.getHead(), child,
+ currentValue);
returnTree.addChild(oldTree);
} else {
@@ -97,18 +90,16 @@ public class DiceASTReferenceSanitizer {
return returnTree;
}
- ITree<IDiceASTNode> returnTree = new Tree<>(
- OperatorDiceNode.ARRAY);
+ ITree<IDiceASTNode> returnTree = new Tree<>(OperatorDiceNode.ARRAY);
nameTree.doForChildren((child) -> {
- String variableName = child.transformHead(
- (node) -> ((VariableDiceNode) node).getVariable());
+ String variableName = child
+ .transformHead((node) -> ((VariableDiceNode) node).getVariable());
- ITree<IDiceASTNode> sanitizedChild = doSingleSanitize(ast,
- enviroment, child, valueTree, variableName);
+ ITree<IDiceASTNode> sanitizedChild = doSingleSanitize(ast, enviroment, child, valueTree,
+ variableName);
if (sanitizedChild == null) {
- ITree<IDiceASTNode> oldTree = new Tree<>(ast.getHead(),
- child, valueTree);
+ ITree<IDiceASTNode> oldTree = new Tree<>(ast.getHead(), child, valueTree);
returnTree.addChild(oldTree);
} else {
@@ -119,11 +110,10 @@ public class DiceASTReferenceSanitizer {
return returnTree;
}
- String variableName = nameTree.transformHead(
- (node) -> ((VariableDiceNode) node).getVariable());
+ String variableName = nameTree.transformHead((node) -> ((VariableDiceNode) node).getVariable());
- ITree<IDiceASTNode> sanitizedTree = doSingleSanitize(ast,
- enviroment, nameTree, valueTree, variableName);
+ ITree<IDiceASTNode> sanitizedTree = doSingleSanitize(ast, enviroment, nameTree, valueTree,
+ variableName);
if (sanitizedTree == null) {
return ast;
@@ -132,22 +122,19 @@ public class DiceASTReferenceSanitizer {
return sanitizedTree;
}
- private static ITree<IDiceASTNode> doSingleSanitize(
- ITree<IDiceASTNode> ast,
- IMap<String, ITree<IDiceASTNode>> enviroment,
- ITree<IDiceASTNode> nameTree, ITree<IDiceASTNode> valueTree,
- String variableName) {
+ private static ITree<IDiceASTNode> doSingleSanitize(ITree<IDiceASTNode> ast,
+ IMap<String, ITree<IDiceASTNode>> enviroment, ITree<IDiceASTNode> nameTree,
+ ITree<IDiceASTNode> valueTree, String variableName) {
if (enviroment.containsKey(variableName)) {
// @ is a meta-variable standing for the left side of an
// assignment
- ITree<IDiceASTNode> oldVal = enviroment.put("@",
- enviroment.get(variableName));
+ ITree<IDiceASTNode> oldVal = enviroment.put("@", enviroment.get(variableName));
- // We should always inline out references to last, because it
+ // We should always inline out references to last,
+ // because it
// will always change
- ITree<IDiceASTNode> inlinedValue = DiceASTInliner
- .selectiveInline(valueTree, enviroment, variableName,
- "last", "@");
+ ITree<IDiceASTNode> inlinedValue = DiceASTInliner.selectiveInline(valueTree, enviroment,
+ variableName, "last", "@");
if (oldVal != null) {
enviroment.put("@", oldVal);
@@ -170,32 +157,30 @@ public class DiceASTReferenceSanitizer {
*/
public static ITree<IDiceASTNode> sanitize(ITree<IDiceASTNode> ast,
IMap<String, ITree<IDiceASTNode>> enviroment) {
- return ast.topDownTransform(
- DiceASTReferenceSanitizer::shouldSanitize, (subTree) -> {
- return doSanitize(subTree, enviroment);
- });
+ return ast.topDownTransform(DiceASTReferenceSanitizer::shouldSanitize, (subTree) -> {
+ return doSanitize(subTree, enviroment);
+ });
}
- private static TopDownTransformResult shouldSanitize(
- IDiceASTNode node) {
+ 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;
+ 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;
}
}
}