-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow `Group` clauses to take rejected groups and handle them in the same way as `Reject` and `Require`.
- Loading branch information
Showing
9 changed files
with
203 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* Add `OnReject` support to `Group` clauses |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
shesmu-server/src/main/java/ca/on/oicr/gsi/shesmu/compiler/FilterBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package ca.on.oicr.gsi.shesmu.compiler; | ||
|
||
import static ca.on.oicr.gsi.shesmu.compiler.BaseOliveBuilder.A_STREAM_TYPE; | ||
import static ca.on.oicr.gsi.shesmu.compiler.BaseOliveBuilder.METHOD_STREAM__FILTER; | ||
import static ca.on.oicr.gsi.shesmu.compiler.BaseOliveBuilder.METHOD_STREAM__ON_CLOSE; | ||
|
||
import java.util.List; | ||
import java.util.TreeSet; | ||
import java.util.stream.Stream; | ||
import org.objectweb.asm.Opcodes; | ||
import org.objectweb.asm.Type; | ||
import org.objectweb.asm.commons.GeneratorAdapter; | ||
|
||
public interface FilterBuilder { | ||
FilterBuilder SIMPLE = | ||
(line, column, renderer, streamType) -> { | ||
LambdaBuilder.pushVirtual( | ||
renderer, | ||
RegroupVariablesBuilder.METHOD_IS_OK.getName(), | ||
LambdaBuilder.predicate(streamType)); | ||
renderer.methodGen().invokeInterface(A_STREAM_TYPE, METHOD_STREAM__FILTER); | ||
}; | ||
|
||
static FilterBuilder of(List<RejectNode> rejectHandlers) { | ||
if (rejectHandlers.isEmpty()) { | ||
return SIMPLE; | ||
} | ||
final var freeVariables = new TreeSet<String>(); | ||
for (final var handler : rejectHandlers) { | ||
handler.collectFreeVariables(freeVariables); | ||
} | ||
return (line, column, renderer, streamType) -> { | ||
final var captures = | ||
rejectHandlers.stream() | ||
.flatMap(handler -> handler.requiredCaptures(renderer.root())) | ||
.toArray(LoadableValue[]::new); | ||
final var filterBuilder = | ||
new LambdaBuilder( | ||
renderer.root(), | ||
String.format("Okay? %d:%d 🔍", line, column), | ||
LambdaBuilder.predicate(streamType), | ||
Stream.concat( | ||
Stream.of(captures), | ||
renderer.allValues().filter(v -> freeVariables.contains(v.name()))) | ||
.toArray(LoadableValue[]::new)); | ||
|
||
final var filterRenderer = filterBuilder.renderer(streamType, null); | ||
filterRenderer.methodGen().visitCode(); | ||
filterRenderer.loadStream(); | ||
filterRenderer.methodGen().invokeVirtual(streamType, RegroupVariablesBuilder.METHOD_IS_OK); | ||
final var failPath = filterRenderer.methodGen().newLabel(); | ||
filterRenderer.methodGen().ifZCmp(GeneratorAdapter.EQ, failPath); | ||
filterRenderer.methodGen().push(true); | ||
filterRenderer.methodGen().returnValue(); | ||
filterRenderer.methodGen().mark(failPath); | ||
rejectHandlers.forEach(handler -> handler.render(renderer.root(), filterRenderer)); | ||
filterRenderer.methodGen().push(false); | ||
filterRenderer.methodGen().returnValue(); | ||
filterRenderer.methodGen().endMethod(); | ||
|
||
filterBuilder.push(renderer); | ||
renderer.methodGen().invokeInterface(A_STREAM_TYPE, METHOD_STREAM__FILTER); | ||
|
||
final var closeBuilder = | ||
new LambdaBuilder( | ||
renderer.root(), | ||
String.format("Okay? %d:%d 🗑️", line, column), | ||
LambdaBuilder.RUNNABLE, | ||
captures); | ||
final var closeRenderer = closeBuilder.renderer(); | ||
closeRenderer.methodGen().visitCode(); | ||
rejectHandlers.forEach(handler -> handler.renderOnClose(closeRenderer)); | ||
closeRenderer.methodGen().visitInsn(Opcodes.RETURN); | ||
closeRenderer.methodGen().visitMaxs(0, 0); | ||
closeRenderer.methodGen().visitEnd(); | ||
|
||
closeBuilder.push(renderer); | ||
renderer.methodGen().invokeInterface(A_STREAM_TYPE, METHOD_STREAM__ON_CLOSE); | ||
renderer.methodGen().checkCast(A_STREAM_TYPE); | ||
}; | ||
} | ||
|
||
void pushFilterMethod(int line, int column, Renderer renderer, Type streamType); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.