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());
}
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);
}
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);
}
}
Aggregations