Skip to content

Commit

Permalink
#162: Converted ByToken.getAllValuesRecursive() to be tail call recur…
Browse files Browse the repository at this point in the history
…sive. This completes reimplementation of TokenRef with trampoline.
  • Loading branch information
jvdb committed Jun 3, 2017
1 parent 505028d commit 3433026
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static ImmutableList<Value> getAllValues(final ParseGraph graph, final St
return reverse(getAllValuesRecursive(ImmutableList.create(graph), new ImmutableList<>(), name).computeResult());
}

private static SafeTrampoline<ImmutableList<Value>> getAllValuesRecursive(final ImmutableList<ParseGraph> graphList, ImmutableList<Value> values, final String name) {
private static SafeTrampoline<ImmutableList<Value>> getAllValuesRecursive(final ImmutableList<ParseGraph> graphList, final ImmutableList<Value> values, final String name) {
if (graphList.isEmpty()) { return complete(() -> values); }
final ParseGraph graph = graphList.head;
if (graph.isEmpty()) { return intermediate(() -> getAllValuesRecursive(graphList.tail, values, name)); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package io.parsingdata.metal.data.selection;

import static io.parsingdata.metal.SafeTrampoline.complete;
import static io.parsingdata.metal.SafeTrampoline.intermediate;
import static io.parsingdata.metal.Util.checkNotNull;
import static io.parsingdata.metal.data.transformation.Reversal.reverse;

import io.parsingdata.metal.SafeTrampoline;
import io.parsingdata.metal.data.ImmutableList;
Expand Down Expand Up @@ -70,20 +72,21 @@ private static ImmutableList<ParseItem> getAllRecursive(final ParseGraph graph,
public static ImmutableList<Value> getAllValues(final ParseGraph graph, final Token definition) {
checkNotNull(graph, "graph");
checkNotNull(definition, "definition");
return getAllValuesRecursive(graph, definition).computeResult();
return reverse(getAllValuesRecursive(ImmutableList.create(graph), new ImmutableList<>(), definition).computeResult());
}

private static SafeTrampoline<ImmutableList<Value>> getAllValuesRecursive(final ParseGraph graph, final Token definition) {
if (graph.isEmpty()) { return complete(ImmutableList::new); }
final ImmutableList<Value> tailResults = getAllValuesRecursive(graph.tail, definition).computeResult();
private static SafeTrampoline<ImmutableList<Value>> getAllValuesRecursive(final ImmutableList<ParseGraph> graphList, final ImmutableList<Value> values, final Token definition) {
if (graphList.isEmpty()) { return complete(() -> values); }
final ParseGraph graph = graphList.head;
if (graph.isEmpty()) { return intermediate(() -> getAllValuesRecursive(graphList.tail, values, definition)); }
final ParseItem head = graph.head;
if (head.isValue() && head.asValue().definition.equals(definition)) {
return complete(() -> tailResults.add(head.asValue()));
return intermediate(() -> getAllValuesRecursive(graphList.tail.add(graph.tail), values.add(head.asValue()), definition));
}
if (head.isGraph()) {
return complete(() -> tailResults.add(getAllValuesRecursive(head.asGraph(), definition).computeResult()));
return intermediate(() -> getAllValuesRecursive(graphList.tail.add(graph.tail).add(graph.head.asGraph()), values, definition));
}
return complete(() -> tailResults);
return intermediate(() -> getAllValuesRecursive(graphList.tail.add(graph.tail), values, definition));
}

public static ImmutableList<ParseItem> getAllRoots(final ParseGraph graph, final Token definition) {
Expand Down

0 comments on commit 3433026

Please sign in to comment.