Search in sources :

Example 6 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)

Example 7 with Iterate

use of suite.util.FunUtil.Iterate in project suite by stupidsing.

the class FactorizeResult method rewrite.

public static FactorizeResult rewrite(FactorizeResult frfrom, FactorizeResult frto, FactorizeResult fr0) {
    Generalizer generalizer = new Generalizer();
    Iterate<Node> rewrite = n0 -> {
        Node[] m = Suite.pattern(FTerminal.class.getName() + ":.0").match(n0);
        Node n1 = m != null ? m[0] : null;
        Node n2 = n1 instanceof Dict ? ((Dict) n1).map.get(Atom.of("chars")) : null;
        Node n3 = n2 != null ? n2.finalNode() : null;
        String s = n3 instanceof Str ? ((Str) n3).value : null;
        boolean b = s != null && s.startsWith(ProverConstant.variablePrefix) && s.substring(1).matches("[0-9]*");
        return b ? generalizer.generalize(Atom.of(s)) : n0;
    };
    Fun<FactorizeResult, Node> parse = fr -> rw.rewrite(rewrite, nodify.nodify(FNode.class, fr.node));
    Node nodeFrom = parse.apply(frfrom);
    Node nodeTo = parse.apply(frto);
    FNode fn0 = fr0.node;
    Node node0 = nodify.nodify(FNode.class, fn0);
    Node nodex = rw.rewrite(nodeFrom, nodeTo, node0);
    FNode fnx = nodify.unnodify(FNode.class, nodex);
    return new FactorizeResult(fr0.pre, fnx, fr0.post);
}
Also used : Suite(suite.Suite) Singleton(suite.node.util.Singleton) Inspect(suite.inspect.Inspect) List_(suite.util.List_) To(suite.util.To) Chars(suite.primitive.Chars) Fun(suite.util.FunUtil.Fun) ArrayList(java.util.ArrayList) Node(suite.node.Node) CharsBuilder(suite.primitive.Chars.CharsBuilder) Iterate(suite.util.FunUtil.Iterate) List(java.util.List) ProverConstant(suite.lp.doer.ProverConstant) Rewrite(suite.node.util.Rewrite) Atom(suite.node.Atom) Nodify(suite.util.Nodify) Generalizer(suite.lp.doer.Generalizer) Dict(suite.node.Dict) Str(suite.node.Str) Str(suite.node.Str) Generalizer(suite.lp.doer.Generalizer) Dict(suite.node.Dict) Node(suite.node.Node)

Example 8 with Iterate

use of suite.util.FunUtil.Iterate in project suite by stupidsing.

the class FunctionalTemplateRenderer method render.

public String render(String template, Map<String, Node> inputs) {
    Iterate<String> wrapText = s -> " . append {" + Escaper.escape(s, '"') + "}";
    Iterate<String> wrapExpression = s -> !s.startsWith("=") ? s : " . append {" + s.substring(1) + "}";
    String fps = "id " + new TemplateRenderer(wrapText, wrapExpression).apply(template);
    Node fp0 = Suite.substitute("() | .0", Suite.parse(fps));
    Node fp1 = Read.from2(inputs).pairs().fold(fp0, (fp_, p) -> Suite.substitute("let .1 := .2 >> .0", fp_, Atom.of(p.t0), p.t1));
    Node fp2 = Suite.applyWriter(fp1);
    try (StringWriter sw = new StringWriter()) {
        Suite.evaluateFunToWriter(Suite.fcc(fp2), sw);
        return sw.toString();
    } catch (IOException ex) {
        return Fail.t(ex);
    }
}
Also used : Iterate(suite.util.FunUtil.Iterate) Suite(suite.Suite) Read(suite.streamlet.Read) Atom(suite.node.Atom) StringWriter(java.io.StringWriter) Map(java.util.Map) IOException(java.io.IOException) Fail(suite.util.Fail) Node(suite.node.Node) Escaper(suite.node.io.Escaper) StringWriter(java.io.StringWriter) Node(suite.node.Node) IOException(java.io.IOException)

Aggregations

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