Search in sources :

Example 1 with List_

use of suite.util.List_ in project suite by stupidsing.

the class Assembler method assemble.

public Bytes assemble(String in0) {
    Set<Character> whitespaces = Collections.singleton('\n');
    Fun<String, List<Run>> gct = CommentPreprocessor.groupCommentPreprocessor(whitespaces);
    Fun<String, List<Run>> lct = CommentPreprocessor.lineCommentPreprocessor(whitespaces);
    String in1 = Preprocess.transform(List.of(gct, lct), in0).t0;
    Generalizer generalizer = new Generalizer();
    List<String> lines = List.of(in1.split("\n"));
    Pair<String, String> pe;
    int start = 0;
    while (!(pe = String_.split2(lines.get(start), "=")).t1.isEmpty()) {
        generalizer.getVariable(Atom.of(pe.t0)).bound(Suite.parse(pe.t1));
        start++;
    }
    List<Pair<Reference, Node>> lnis = // 
    Read.from(// 
    List_.right(lines, start)).map(line -> {
        Pair<String, String> pt = String_.split2(line, "\t");
        String label = pt.t0;
        String command = pt.t1;
        Reference reference = String_.isNotBlank(label) ? generalizer.getVariable(Atom.of(label)) : null;
        Node instruction = generalizer.generalize(Suite.parse(command));
        return Pair.of(reference, instruction);
    }).toList();
    return assemble(generalizer, lnis);
}
Also used : Read(suite.streamlet.Read) SewingProverBuilder2(suite.lp.search.SewingProverBuilder2) Fun(suite.util.FunUtil.Fun) ArrayList(java.util.ArrayList) Node(suite.node.Node) CommentPreprocessor(suite.parser.CommentPreprocessor) String_(suite.util.String_) Preprocess(suite.text.Preprocess) Run(suite.text.Preprocess.Run) Binder(suite.lp.doer.Binder) RuleSet(suite.lp.kb.RuleSet) Generalizer(suite.lp.doer.Generalizer) Reference(suite.node.Reference) Suite(suite.Suite) Trail(suite.lp.Trail) Finder(suite.lp.search.ProverBuilder.Finder) Set(java.util.Set) TermOp(suite.node.io.TermOp) Bytes(suite.primitive.Bytes) BytesBuilder(suite.primitive.Bytes.BytesBuilder) List_(suite.util.List_) To(suite.util.To) Tree(suite.node.Tree) Pair(suite.adt.pair.Pair) List(java.util.List) Atom(suite.node.Atom) Int(suite.node.Int) Collections(java.util.Collections) Fail(suite.util.Fail) Reference(suite.node.Reference) Node(suite.node.Node) Generalizer(suite.lp.doer.Generalizer) ArrayList(java.util.ArrayList) List(java.util.List) Pair(suite.adt.pair.Pair)

Example 2 with List_

use of suite.util.List_ in project suite by stupidsing.

the class BuildLr method build.

private Blr build(IList<Pair<String, Set<String>>> ps, Grammar eg, Transition nextx) {
    Fun<Streamlet2<String, Transition>, Blr> mergeAll = st2 -> {
        Transition next = newTransition(readLookahead.readLookahead(eg, nextx.keySet()));
        State state1 = newState(nextx);
        st2.sink((egn, next1) -> {
            next.put_(egn, Pair.of(state1, null));
            merges.add(Pair.of(next, next1));
        });
        return new Blr(1, next);
    };
    Pair<String, Set<String>> k;
    Blr blr;
    switch(eg.type) {
        case AND___:
            if (!eg.children.isEmpty()) {
                Grammar tail = new Grammar(GrammarType.AND___, List_.right(eg.children, 1));
                Blr blr1 = build(ps, tail, nextx);
                Blr blr0 = build(ps, eg.children.get(0), blr1.next);
                blr = new Blr(blr0.nTokens + blr1.nTokens, blr0.next);
            } else
                blr = new Blr(0, nextx);
            break;
        case ENTITY:
            k = Pair.of(eg.content, nextx.keySet());
            Transition next1 = transitions.computeIfAbsent(k, k_ -> new Transition());
            blr = mergeAll.apply(Read.each2(eg.content, next1));
            break;
        case NAMED_:
            Reduce reduce = new Reduce();
            Transition next = newTransition(nextx.keySet(), Pair.of(null, reduce));
            Blr blr1 = build(ps, eg.children.get(0), next);
            reduce.n = blr1.nTokens;
            reduce.name = eg.content;
            blr = new Blr(1, blr1.next);
            break;
        case OR____:
            List<Pair<String, Transition>> pairs = new ArrayList<>();
            for (Grammar eg1 : Read.from(eg.children)) {
                String egn = "OR." + System.identityHashCode(eg1);
                pairs.add(Pair.of(egn, build(ps, new Grammar(GrammarType.NAMED_, egn, eg1), nextx).next));
            }
            blr = mergeAll.apply(Read.from2(pairs));
            break;
        case STRING:
            State state1 = newState(nextx);
            blr = new Blr(1, kv(eg.content, state1));
            break;
        default:
            blr = Fail.t("LR parser cannot recognize " + eg.type);
    }
    return blr;
}
Also used : Read(suite.streamlet.Read) Streamlet2(suite.streamlet.Streamlet2) Set(java.util.Set) List_(suite.util.List_) HashMap(java.util.HashMap) IList(suite.immutable.IList) Grammar(suite.ebnf.Grammar) Fun(suite.util.FunUtil.Fun) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Objects(java.util.Objects) Pair(suite.adt.pair.Pair) List(java.util.List) GrammarType(suite.ebnf.Grammar.GrammarType) Map(java.util.Map) Fail(suite.util.Fail) Set(java.util.Set) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) Grammar(suite.ebnf.Grammar) Streamlet2(suite.streamlet.Streamlet2) Pair(suite.adt.pair.Pair)

Example 3 with List_

use of suite.util.List_ in project suite by stupidsing.

the class SewingProverImpl method compileCps.

private Cps compileCps(BinderFactory bf, Node node, Cps cpsx) {
    List<Node> list;
    Tree tree;
    Node[] m;
    Cps cps;
    if (1 < (list = TreeUtil.breakdown(TermOp.AND___, node)).size()) {
        cps = cpsx;
        for (Node n : List_.reverse(list)) cps = compileCps(bf, n, cps);
    } else if (1 < (list = TreeUtil.breakdown(TermOp.OR____, node)).size())
        cps = orCps(Read.from(list).map(n -> compileCps(bf, n, cpsx)));
    else if ((m = Suite.pattern(".0 = .1").match(node)) != null) {
        boolean b = complexity(m[0]) <= complexity(m[1]);
        Node n0 = b ? m[0] : m[1];
        Node n1 = b ? m[1] : m[0];
        Bind_ p = bf.binder(n1);
        Clone_ f = bf.cloner(n0);
        cps = rt -> p.test(rt, f.apply(rt.env)) ? cpsx : null;
    } else if ((m = Suite.pattern(".0 .1").match(node)) != null && m[0] instanceof Atom)
        cps = compileCpsCallPredicate(bf, ((Atom) m[0]).name, m[1], node, cpsx);
    else if (node instanceof Atom) {
        String name = ((Atom) node).name;
        if (String_.equals(name, ""))
            cps = cpsx;
        else if (String_.equals(name, "fail"))
            cps = rt -> null;
        else
            cps = compileCpsCallPredicate(bf, name, Atom.NIL, node, cpsx);
    } else if (node instanceof Reference) {
        Clone_ f = bf.cloner(node);
        cps = rt -> compileCps(passThru, f.apply(rt.env), cpsx);
    } else if ((tree = Tree.decompose(node)) != null)
        cps = compileCpsCallPredicate(bf, tree.getOperator().getName(), node, node, cpsx);
    else if (node instanceof Tuple)
        cps = compileCpsCallPredicate(bf, node, cpsx);
    else
        cps = Fail.t("cannot understand " + node);
    return cps;
}
Also used : Prover(suite.lp.doer.Prover) BindEnv(suite.lp.doer.BinderFactory.BindEnv) BuiltinPredicate(suite.lp.predicate.PredicateUtil.BuiltinPredicate) LogUtil(suite.os.LogUtil) Mutable(suite.adt.Mutable) BinderFactory(suite.lp.doer.BinderFactory) Node(suite.node.Node) ProverConstant(suite.lp.doer.ProverConstant) VariableMapper(suite.lp.sewing.VariableMapper) Suspend(suite.node.Suspend) Map(java.util.Map) Prototype(suite.lp.kb.Prototype) RuleSet(suite.lp.kb.RuleSet) Generalizer(suite.lp.doer.Generalizer) Bind_(suite.lp.doer.BinderFactory.Bind_) Cloner(suite.lp.doer.Cloner) Object_(suite.util.Object_) SystemPredicates(suite.lp.predicate.SystemPredicates) Tree(suite.node.Tree) Pair(suite.adt.pair.Pair) Friends.max(suite.util.Friends.max) List(java.util.List) Clone_(suite.lp.doer.ClonerFactory.Clone_) ListMultimap(suite.adt.map.ListMultimap) As(suite.streamlet.As) Int(suite.node.Int) TreeUtil(suite.node.util.TreeUtil) ProverConfig(suite.lp.Configuration.ProverConfig) Read(suite.streamlet.Read) Rule(suite.lp.kb.Rule) HashMap(java.util.HashMap) IList(suite.immutable.IList) ArrayList(java.util.ArrayList) Data(suite.node.Data) Formatter(suite.node.io.Formatter) String_(suite.util.String_) Rethrow(suite.util.Rethrow) Binder(suite.lp.doer.Binder) Tuple(suite.node.Tuple) Reference(suite.node.Reference) Suite(suite.Suite) SuiteException(suite.node.util.SuiteException) Iterator(java.util.Iterator) CompileExpressionImpl(suite.lp.compile.impl.CompileExpressionImpl) Source(suite.util.FunUtil.Source) TermOp(suite.node.io.TermOp) List_(suite.util.List_) Evaluate_(suite.lp.doer.EvaluatorFactory.Evaluate_) Rewrite(suite.node.util.Rewrite) Streamlet(suite.streamlet.Streamlet) Atom(suite.node.Atom) Sink(suite.util.FunUtil.Sink) ProverFactory(suite.lp.doer.ProverFactory) Env(suite.lp.sewing.Env) Fail(suite.util.Fail) Reference(suite.node.Reference) Node(suite.node.Node) Atom(suite.node.Atom) Bind_(suite.lp.doer.BinderFactory.Bind_) Clone_(suite.lp.doer.ClonerFactory.Clone_) Tree(suite.node.Tree) Tuple(suite.node.Tuple)

Example 4 with List_

use of suite.util.List_ in project suite by stupidsing.

the class TwoPassIndexerTest method test.

@Test
public void test() {
    List<String> filenames = // 
    FileUtil.findPaths(Paths.get("src/test/java")).map(// 
    Path::toAbsolutePath).map(// 
    Path::toString).filter(// 
    filename -> filename.endsWith(".java")).toList();
    TwoPassIndexer indexer = new TwoPassIndexer();
    for (String filename : filenames) indexer.pass0(filename, FileUtil.read(filename));
    for (String filename : filenames) indexer.pass1(filename, FileUtil.read(filename));
    Map<String, List<Reference>> map = indexer.getReferencesByWord();
    List<Entry<String, List<Reference>>> entries = // 
    List_.sort(// 
    map.entrySet(), (e0, e1) -> e1.getValue().size() - e0.getValue().size());
    System.out.println("Most popular key words:");
    for (int i = 0; i < 32; i++) {
        Entry<String, List<Reference>> entry = entries.get(i);
        System.out.println(String.format("%-5d \"%s\"", entry.getValue().size(), entry.getKey()));
    }
    System.out.println();
    for (Reference key : indexer.search("IOException")) System.out.println("IOException found in " + key);
}
Also used : Path(java.nio.file.Path) List(java.util.List) Paths(java.nio.file.Paths) Map(java.util.Map) Entry(java.util.Map.Entry) FileUtil(suite.os.FileUtil) List_(suite.util.List_) Test(org.junit.Test) Path(java.nio.file.Path) Reference(suite.text.TwoPassIndexer.Reference) Entry(java.util.Map.Entry) Reference(suite.text.TwoPassIndexer.Reference) List(java.util.List) Test(org.junit.Test)

Aggregations

List (java.util.List)4 List_ (suite.util.List_)4 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 Pair (suite.adt.pair.Pair)3 Read (suite.streamlet.Read)3 Fail (suite.util.Fail)3 HashMap (java.util.HashMap)2 Set (java.util.Set)2 Suite (suite.Suite)2 IList (suite.immutable.IList)2 Binder (suite.lp.doer.Binder)2 Generalizer (suite.lp.doer.Generalizer)2 RuleSet (suite.lp.kb.RuleSet)2 Atom (suite.node.Atom)2 Int (suite.node.Int)2 Node (suite.node.Node)2 Reference (suite.node.Reference)2 Tree (suite.node.Tree)2 TermOp (suite.node.io.TermOp)2