Skip to content

Commit

Permalink
#162: Reimplemented UnaryValueExpression with trampoline.
Browse files Browse the repository at this point in the history
  • Loading branch information
jvdb committed Jun 9, 2017
1 parent f1e1b60 commit 2fe9deb
Showing 1 changed file with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@

package io.parsingdata.metal.expression.value;

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 java.util.Objects;
import java.util.Optional;

import io.parsingdata.metal.SafeTrampoline;
import io.parsingdata.metal.Util;
import io.parsingdata.metal.data.ImmutableList;
import io.parsingdata.metal.data.ParseGraph;
Expand Down Expand Up @@ -51,12 +55,12 @@ public UnaryValueExpression(final ValueExpression operand) {

@Override
public ImmutableList<Optional<Value>> eval(final ParseGraph graph, final Encoding encoding) {
return eval(operand.eval(graph, encoding), graph, encoding);
return reverse(eval(operand.eval(graph, encoding), graph, encoding, new ImmutableList<>()).computeResult());
}

private ImmutableList<Optional<Value>> eval(final ImmutableList<Optional<Value>> values, final ParseGraph graph, final Encoding encoding) {
if (values.isEmpty()) { return values; }
return eval(values.tail, graph, encoding).add(values.head.isPresent() ? eval(values.head.get(), graph, encoding) : values.head);
private SafeTrampoline<ImmutableList<Optional<Value>>> eval(final ImmutableList<Optional<Value>> values, final ParseGraph graph, final Encoding encoding, final ImmutableList<Optional<Value>> result) {
if (values.isEmpty()) { return complete(() -> result); }
return intermediate(() -> eval(values.tail, graph, encoding, result.add(values.head.flatMap(value -> eval(value, graph, encoding)))));
}

public abstract Optional<Value> eval(final Value value, final ParseGraph graph, final Encoding encoding);
Expand Down

0 comments on commit 2fe9deb

Please sign in to comment.