use of suite.util.FunUtil.Iterate in project suite by stupidsing.
the class CompileClonerImpl method cloner.
@Override
public Clone_ cloner(Node node) {
FunCreator<Clone_> fc = FunCreator.of(Clone_.class, false);
return fc.create(new Iterate<>() {
private FunExpr env;
public FunExpr apply(FunExpr env) {
this.env = env;
return compile_(node);
}
private FunExpr compile_(Node node_) {
return new //
SwitchNode<FunExpr>(//
node_).applyIf(Atom.class, n -> {
return f.object(node_);
}).applyIf(Dict.class, n -> {
FunExpr[] exprs = //
Read.from2(//
n.map).map(//
(key, value) -> f.invokeStatic(Pair.class, "of", compile_(key), compile_(value))).toArray(FunExpr.class);
return f.invokeStatic(Dict.class, "of", f.array(Pair.class, exprs));
}).applyIf(Int.class, n -> {
return f.object(node_);
}).applyIf(Reference.class, n -> {
return env.field("refs").index(f.int_(vm.computeIndex(n)));
}).applyIf(Str.class, n -> {
return f.object(node_);
}).applyIf(Tree.class, tree -> {
FunExpr fe0 = compile_(tree.getLeft()).cast_(Node.class);
FunExpr fe1 = compile_(tree.getRight()).cast_(Node.class);
return f.invokeStatic(Tree.class, "of", f.object(tree.getOperator()), fe0, fe1);
}).applyIf(Tuple.class, n -> {
FunExpr[] exprs = Read.from(n.nodes).map(this::compile_).toArray(FunExpr.class);
return f.invokeStatic(Tuple.class, "of", f.array(Node.class, exprs));
}).nonNullResult();
}
}).apply(Map.ofEntries());
}
use of suite.util.FunUtil.Iterate in project suite by stupidsing.
the class NioDispatcherTest method testRequestResponse.
@Test
public void testRequestResponse() throws IOException, InterruptedException {
RequestResponseMatcher matcher = new RequestResponseMatcher();
ThreadPoolExecutor executor = Thread_.newExecutor();
Iterate<Bytes> handler = request -> request;
NioDispatcher<RequestResponseNioChannel> dispatcher = new NioDispatcherImpl<>(() -> NioChannelFactory.requestResponse(new RequestResponseNioChannel(), matcher, executor, handler));
dispatcher.start();
try (Closeable closeServer = dispatcher.listen(5151)) {
InetAddress localHost = InetAddress.getLocalHost();
InetSocketAddress address = new InetSocketAddress(localHost, 5151);
RequestResponseNioChannel client = dispatcher.connect(address);
for (String s : new String[] { "ABC", "WXYZ", "" }) {
byte[] bs = s.getBytes(Constants.charset);
Bytes request = Bytes.of(bs);
Bytes response = matcher.requestForResponse(client, request);
assertEquals(request, response);
System.out.println("Request '" + s + "' is okay");
}
} finally {
dispatcher.stop();
}
executor.awaitTermination(0, TimeUnit.SECONDS);
}
use of suite.util.FunUtil.Iterate in project suite by stupidsing.
the class FunCreatorTest method testProfile.
@Test
public void testProfile() {
Iterate<FunExpr> fun = i -> (ProfileFunExpr) f.profile(f.int_(1));
IntSource instance = LambdaInstance.of(IntSource.class, fun).newFun();
assertEquals(1, instance.source());
Dump.out(instance);
}
use of suite.util.FunUtil.Iterate in project suite by stupidsing.
the class RecursiveFactorizerTest method transform.
private FNode transform(FNode fn0) {
FTerminal from = new FTerminal(To.chars("ic-compile-better-option"));
FTerminal to = new FTerminal(To.chars("ic-new-compile-better-option"));
Iterate<FNode> fun = fn_ -> fn_.equals(from) ? to : null;
return transform(fn0, fun);
}
use of suite.util.FunUtil.Iterate in project suite by stupidsing.
the class CompileExpressionImpl method evaluator.
public Evaluate_ evaluator(Node node) {
FunCreator<Evaluate_> fc = FunCreator.of(Evaluate_.class, false);
return fc.create(new Iterate<>() {
private FunExpr env;
public FunExpr apply(FunExpr env) {
this.env = env;
return compile_(node);
}
private FunExpr compile_(Node node) {
return new //
SwitchNode<FunExpr>(//
node).match2(".0 + .1", (a, b) -> {
return compileOperator(a, b, "+");
}).match2(".0 - .1", (a, b) -> {
return compileOperator(a, b, "-");
}).match2(".0 * .1", (a, b) -> {
return compileOperator(a, b, "*");
}).match2(".0 / .1", (a, b) -> {
return compileOperator(a, b, "/");
}).match2(".0 and .1", (a, b) -> {
return compileOperator(a, b, "&&");
}).match2(".0 or .1", (a, b) -> {
return compileOperator(a, b, "||");
}).match2(".0 shl .1", (a, b) -> {
return compileOperator(a, b, "<<");
}).match2(".0 shr .1", (a, b) -> {
return compileOperator(a, b, ">>");
}).applyIf(Int.class, i -> {
return f.int_(i.number);
}).applyIf(Node.class, i -> {
Clone_ n_ = clonerFactory.cloner(node);
Evaluate_ evaluate = env -> TreeUtil.evaluate(n_.apply(env));
return f.object(evaluate).invoke("evaluate", env);
}).nonNullResult();
}
private FunExpr compileOperator(Node a, Node b, String op) {
FunExpr fe0 = compile_(a);
FunExpr fe1 = compile_(b);
return f.bi(op, fe0, fe1);
}
}).apply(Map.ofEntries());
}
Aggregations