Skip to content

Commit

Permalink
#162: Converted ByOffset.getLowestOffsetValue() to be tail call recur…
Browse files Browse the repository at this point in the history
…sive. This completes reimplementation of Sub with trampoline.
  • Loading branch information
jvdb committed Jun 2, 2017
1 parent f554ad7 commit 706cc90
Showing 1 changed file with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static SafeTrampoline<ParseItem> findItemAtOffset(final ImmutableList<Par
final ParseItem head = items.head;
if (head.isValue() && matchesLocation(head.asValue(), offset, source)) { return complete(() -> head); }
if (head.isGraph()) {
final ParseValue value = getLowestOffsetValue(head.asGraph(), null).computeResult();
final ParseValue value = getLowestOffsetValue(ImmutableList.create(head.asGraph()), null).computeResult();
if (value != null && matchesLocation(value, offset, source)) { return complete(() -> head); }
}
return intermediate(() -> findItemAtOffset(items.tail, offset, source));
Expand All @@ -54,15 +54,19 @@ private static boolean matchesLocation(final ParseValue value, final long offset
return value.slice.offset == offset && value.slice.source.equals(source);
}

private static SafeTrampoline<ParseValue> getLowestOffsetValue(final ParseGraph graph, final ParseValue lowest) {
if (graph.isEmpty() || !graph.getDefinition().isLocal()) { return complete(() -> lowest); }
private static SafeTrampoline<ParseValue> getLowestOffsetValue(final ImmutableList<ParseGraph> graphList, final ParseValue lowest) {
if (graphList.isEmpty()) { return complete(() -> lowest); }
final ParseGraph graph = graphList.head;
if (graph.isEmpty() || !graph.getDefinition().isLocal()) {
return intermediate(() -> getLowestOffsetValue(graphList.tail, lowest));
}
if (graph.head.isValue()) {
return intermediate(() -> getLowestOffsetValue(graph.tail, lowest == null || lowest.slice.offset > graph.head.asValue().slice.offset ? graph.head.asValue() : lowest));
return intermediate(() -> getLowestOffsetValue(graphList.tail.add(graph.tail), lowest == null || lowest.slice.offset > graph.head.asValue().slice.offset ? graph.head.asValue() : lowest));
}
if (graph.head.isGraph()) {
return intermediate(() -> getLowestOffsetValue(graph.tail, getLowestOffsetValue(graph.head.asGraph(), lowest).computeResult()));
return intermediate(() -> getLowestOffsetValue(graphList.tail.add(graph.head.asGraph()).add(graph.tail), lowest));
}
return intermediate(() -> getLowestOffsetValue(graph.tail, lowest));
return intermediate(() -> getLowestOffsetValue(graphList.tail.add(graph.tail), lowest));
}

}

0 comments on commit 706cc90

Please sign in to comment.