Skip to content

Commit

Permalink
#162: Reimplemented TokenRef (ValueExpression) with trampoline.
Browse files Browse the repository at this point in the history
  • Loading branch information
jvdb committed Jun 1, 2017
1 parent ac36e0a commit f554ad7
Showing 1 changed file with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

package io.parsingdata.metal.data.selection;

import static io.parsingdata.metal.SafeTrampoline.complete;
import static io.parsingdata.metal.Util.checkNotNull;

import io.parsingdata.metal.SafeTrampoline;
import io.parsingdata.metal.data.ImmutableList;
import io.parsingdata.metal.data.ParseGraph;
import io.parsingdata.metal.data.ParseItem;
Expand Down Expand Up @@ -68,20 +70,20 @@ 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);
return getAllValuesRecursive(graph, definition).computeResult();
}

private static ImmutableList<Value> getAllValuesRecursive(final ParseGraph graph, final Token definition) {
if (graph.isEmpty()) { return new ImmutableList<>(); }
final ImmutableList<Value> tailResults = getAllValuesRecursive(graph.tail, definition);
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();
final ParseItem head = graph.head;
if (head.isValue() && head.asValue().definition.equals(definition)) {
return tailResults.add(head.asValue());
return complete(() -> tailResults.add(head.asValue()));
}
if (head.isGraph()) {
return tailResults.add(getAllValuesRecursive(head.asGraph(), definition));
return complete(() -> tailResults.add(getAllValuesRecursive(head.asGraph(), definition).computeResult()));
}
return tailResults;
return complete(() -> tailResults);
}

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

0 comments on commit f554ad7

Please sign in to comment.