Esempio n. 1
0
  public Node applyRules(Node node, ArrayList<RewriteRule> rules) {
    modified = true;
    int count = 0;
    while (modified) {
      count++;
      if (count > MAX_ITERATIONS) {
        throw new RuntimeException("Rewriter hangs!");
      }
      modified = false;

      node =
          Traverse.transform(
              node,
              n -> {
                for (RewriteRule rule : rules) {
                  Optional<Map<Hole, Node>> unifier = Unifier.unify(rule.getPattern(), n);
                  if (unifier.isPresent()) {
                    modified = true;
                    return rule.apply(n, unifier.get());
                  }
                }
                return n;
              });
    }
    return node;
  }
Esempio n. 2
0
 public static boolean traverseDepth(final TreeNode node, final Traverse traverse) {
   if (!traverse.accept(node)) return false;
   final int childCount = node.getChildCount();
   for (int i = 0; i < childCount; i++)
     if (!traverseDepth(node.getChildAt(i), traverse)) return false;
   return true;
 }