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