Search in sources :

Example 1 with Iterate

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());
}
Also used : Reference(suite.node.Reference) Read(suite.streamlet.Read) Tree(suite.node.Tree) Node(suite.node.Node) Iterate(suite.util.FunUtil.Iterate) Pair(suite.adt.pair.Pair) VariableMapper(suite.lp.sewing.VariableMapper) Atom(suite.node.Atom) Map(java.util.Map) ClonerFactory(suite.lp.doer.ClonerFactory) FunCreator(suite.jdk.gen.FunCreator) SwitchNode(suite.node.io.SwitchNode) FunExpr(suite.jdk.gen.FunExpression.FunExpr) FunFactory(suite.jdk.gen.FunFactory) Int(suite.node.Int) Tuple(suite.node.Tuple) Dict(suite.node.Dict) Str(suite.node.Str) Iterate(suite.util.FunUtil.Iterate) Node(suite.node.Node) SwitchNode(suite.node.io.SwitchNode) SwitchNode(suite.node.io.SwitchNode) Atom(suite.node.Atom) Int(suite.node.Int) Str(suite.node.Str) FunExpr(suite.jdk.gen.FunExpression.FunExpr) Tuple(suite.node.Tuple) Pair(suite.adt.pair.Pair)

Example 2 with Iterate

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);
}
Also used : OutputStream(java.io.OutputStream) PrintWriter(java.io.PrintWriter) Socket(java.net.Socket) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Constants(suite.Constants) Source(suite.util.FunUtil.Source) Bytes(suite.primitive.Bytes) Thread_(suite.util.Thread_) IOException(java.io.IOException) Test(org.junit.Test) To(suite.util.To) NioChannel(suite.net.nio.NioChannelFactory.NioChannel) InputStreamReader(java.io.InputStreamReader) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) TimeUnit(java.util.concurrent.TimeUnit) BufferedNioChannel(suite.net.nio.NioChannelFactory.BufferedNioChannel) Iterate(suite.util.FunUtil.Iterate) Charset(java.nio.charset.Charset) Closeable(java.io.Closeable) BufferedReader(java.io.BufferedReader) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) RequestResponseNioChannel(suite.net.nio.NioChannelFactory.RequestResponseNioChannel) InetSocketAddress(java.net.InetSocketAddress) Closeable(java.io.Closeable) RequestResponseNioChannel(suite.net.nio.NioChannelFactory.RequestResponseNioChannel) Bytes(suite.primitive.Bytes) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 3 with Iterate

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);
}
Also used : Reference(suite.node.Reference) Suite(suite.Suite) Flt_Flt(suite.primitive.Flt_Flt) Dump(suite.inspect.Dump) PrintlnFunExpr(suite.jdk.gen.FunExprM.PrintlnFunExpr) Source(suite.util.FunUtil.Source) Assert.assertTrue(org.junit.Assert.assertTrue) TermOp(suite.node.io.TermOp) Test(org.junit.Test) Fun(suite.util.FunUtil.Fun) ProfileFunExpr(suite.jdk.gen.FunExprM.ProfileFunExpr) Tree(suite.node.Tree) Node(suite.node.Node) IntSource(suite.primitive.IntPrimitives.IntSource) Iterate(suite.util.FunUtil.Iterate) BiPredicate(java.util.function.BiPredicate) LambdaInstance(suite.jdk.lambda.LambdaInstance) Map(java.util.Map) Type(org.apache.bcel.generic.Type) FunExpr(suite.jdk.gen.FunExpression.FunExpr) Int(suite.node.Int) LambdaInterface(suite.jdk.lambda.LambdaInterface) Int_Int(suite.primitive.Int_Int) Assert.assertEquals(org.junit.Assert.assertEquals) ProfileFunExpr(suite.jdk.gen.FunExprM.ProfileFunExpr) IntSource(suite.primitive.IntPrimitives.IntSource) PrintlnFunExpr(suite.jdk.gen.FunExprM.PrintlnFunExpr) ProfileFunExpr(suite.jdk.gen.FunExprM.ProfileFunExpr) FunExpr(suite.jdk.gen.FunExpression.FunExpr) Test(org.junit.Test)

Example 4 with Iterate

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);
}
Also used : FNode(suite.node.parser.FactorizeResult.FNode) Read(suite.streamlet.Read) Singleton(suite.node.util.Singleton) Fun(suite.util.FunUtil.Fun) Node(suite.node.Node) FTree(suite.node.parser.FactorizeResult.FTree) FileUtil(suite.os.FileUtil) Nodify(suite.util.Nodify) Dict(suite.node.Dict) FPair(suite.node.parser.FactorizeResult.FPair) Reference(suite.node.Reference) Source(suite.util.FunUtil.Source) Assert.assertTrue(org.junit.Assert.assertTrue) TermOp(suite.node.io.TermOp) FTerminal(suite.node.parser.FactorizeResult.FTerminal) Test(org.junit.Test) To(suite.util.To) Tree(suite.node.Tree) Iterate(suite.util.FunUtil.Iterate) List(java.util.List) Rewrite(suite.node.util.Rewrite) Assert.assertFalse(org.junit.Assert.assertFalse) Atom(suite.node.Atom) Operator(suite.node.io.Operator) Assert.assertEquals(org.junit.Assert.assertEquals) Str(suite.node.Str) FNode(suite.node.parser.FactorizeResult.FNode) FTerminal(suite.node.parser.FactorizeResult.FTerminal)

Example 5 with Iterate

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());
}
Also used : Iterate(suite.util.FunUtil.Iterate) Clone_(suite.lp.doer.ClonerFactory.Clone_) Map(java.util.Map) ClonerFactory(suite.lp.doer.ClonerFactory) EvaluatorFactory(suite.lp.doer.EvaluatorFactory) FunCreator(suite.jdk.gen.FunCreator) SwitchNode(suite.node.io.SwitchNode) FunExpr(suite.jdk.gen.FunExpression.FunExpr) FunFactory(suite.jdk.gen.FunFactory) Int(suite.node.Int) TreeUtil(suite.node.util.TreeUtil) Node(suite.node.Node) Iterate(suite.util.FunUtil.Iterate) SwitchNode(suite.node.io.SwitchNode) Node(suite.node.Node) Clone_(suite.lp.doer.ClonerFactory.Clone_) FunExpr(suite.jdk.gen.FunExpression.FunExpr)

Aggregations

Iterate (suite.util.FunUtil.Iterate)7 Node (suite.node.Node)6 Map (java.util.Map)4 Atom (suite.node.Atom)4 Assert.assertEquals (org.junit.Assert.assertEquals)3 Test (org.junit.Test)3 Suite (suite.Suite)3 FunExpr (suite.jdk.gen.FunExpression.FunExpr)3 Dict (suite.node.Dict)3 Int (suite.node.Int)3 Reference (suite.node.Reference)3 Str (suite.node.Str)3 Tree (suite.node.Tree)3 Read (suite.streamlet.Read)3 Fun (suite.util.FunUtil.Fun)3 Source (suite.util.FunUtil.Source)3 IOException (java.io.IOException)2 List (java.util.List)2 Assert.assertTrue (org.junit.Assert.assertTrue)2 FunCreator (suite.jdk.gen.FunCreator)2