use of suite.adt.pair.Pair 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.adt.pair.Pair 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.adt.pair.Pair in project suite by stupidsing.
the class LibraryMain method run.
protected boolean run(String[] args) {
Pair<Streamlet2<Path, Long>, Streamlet2<Path, Long>> partition = //
FileUtil.findPaths(Paths.get(inputDir)).filter(//
path -> fileExtensions.contains(FileUtil.getFileExtension(path))).map2(//
path -> Rethrow.ex(() -> Files.size(path))).partition((path, size) -> 0 < size);
// remove empty files
partition.t1.sink((path, size) -> {
try {
Files.delete(path);
} catch (IOException ex) {
Fail.t(ex);
}
});
Streamlet2<Path, FileInfo> path_fileInfos = //
partition.t0.map2((path, size) -> {
BasicFileAttributes attrs = Rethrow.ex(() -> Files.readAttributes(path, BasicFileAttributes.class));
// get all file information
List<String> tags = //
Ints_.range(//
path.getNameCount()).map(//
i -> path.getName(i).toString()).cons(//
To.string(attrs.lastModifiedTime().toInstant())).toList();
FileInfo fileInfo = new FileInfo();
fileInfo.md5 = Rethrow.ex(() -> Md5Crypt.md5Crypt(Files.readAllBytes(path)));
fileInfo.tags = tags;
return fileInfo;
});
// construct file listing
try (OutputStream os = FileUtil.out(inputDir + ".listing");
PrintWriter pw = new PrintWriter(os)) {
for (Pair<Path, FileInfo> path_fileInfo : path_fileInfos) pw.println(path_fileInfo.t0 + path_fileInfo.t1.md5);
} catch (IOException ex) {
Fail.t(ex);
}
//
path_fileInfos.map2((path, fileInfo) -> {
// move file to library, by md5
Path path1 = Paths.get(libraryDir, fileInfo.md5.substring(0, 2), fileInfo.md5);
FileUtil.mkdir(path1.getParent());
Rethrow.ex(() -> Files.move(path, path1, StandardCopyOption.REPLACE_EXISTING));
return fileInfo;
}).concatMap((path, fileInfo) -> Read.from(fileInfo.tags).map(tag -> {
// add to tag indices
Path path1 = Paths.get(tagsDir, tag, fileInfo.md5);
return Rethrow.ex(() -> {
Files.newOutputStream(path1).close();
return Pair.of(tag, fileInfo);
});
}));
return true;
}
use of suite.adt.pair.Pair in project suite by stupidsing.
the class P2InferType method captureLambdas.
private Funp captureLambdas(Funp node0) {
class Capture {
private Fun<String, Funp> accesses;
private ISet<String> locals;
private ISet<String> globals;
private Capture(Fun<String, Funp> accesses, ISet<String> locals, ISet<String> globals) {
this.accesses = accesses;
this.locals = locals;
this.globals = globals;
}
private Funp capture(Funp n) {
return inspect.rewrite(Funp.class, this::capture_, n);
}
private Funp capture_(Funp n) {
return //
n.<//
Funp>switch_().applyIf(FunpDefine.class, f -> f.apply((isPolyType, var, value, expr) -> {
Capture c1 = new Capture(accesses, locals.add(var), globals);
return FunpDefine.of(isPolyType, var, capture(value), c1.capture(expr));
})).applyIf(FunpDefineRec.class, f -> f.apply((vars, expr) -> {
List<Pair<String, Funp>> vars1 = new ArrayList<>();
ISet<String> locals1 = locals;
for (Pair<String, Funp> pair : vars) {
locals1 = locals1.add(pair.t0);
vars1.add(Pair.of(pair.t0, capture(pair.t1)));
}
Capture c1 = new Capture(accesses, locals1, globals);
return FunpDefineRec.of(vars1, c1.capture(expr));
})).applyIf(FunpGlobal.class, f -> f.apply((var, value, expr) -> {
Capture c1 = new Capture(accesses, locals, globals.add(var));
return FunpGlobal.of(var, capture(value), c1.capture(expr));
})).applyIf(FunpIterate.class, f -> f.apply((var, init, cond, iterate) -> {
Capture c1 = new Capture(accesses, locals.add(var), globals);
return FunpIterate.of(var, capture(init), c1.capture(cond), c1.capture(iterate));
})).applyIf(FunpLambda.class, f -> f.apply((var, expr) -> {
ISet<String> locals1 = ISet.empty();
String capn = "cap" + Util.temp();
FunpVariable cap = FunpVariable.of(capn);
FunpReference ref = FunpReference.of(cap);
Set<String> set = new HashSet<>();
List<Pair<String, Funp>> list = new ArrayList<>();
FunpStruct struct = FunpStruct.of(list);
Capture c1 = new Capture(v -> {
if (set.add(v))
list.add(Pair.of(v, FunpVariable.of(v)));
return FunpField.of(ref, v);
}, locals1.add(capn).add(var), globals);
return FunpGlobal.of(capn, struct, FunpLambdaCapture.of(var, capn, cap, c1.capture(expr)));
// TODO allocate cap on heap
// TODO free cap after use
})).applyIf(FunpVariable.class, f -> f.apply(var -> {
return locals.contains(var) || globals.contains(var) ? f : accesses.apply(var);
})).result();
}
}
return new Capture(v -> Fail.t(), ISet.empty(), ISet.empty()).capture(node0);
}
use of suite.adt.pair.Pair in project suite by stupidsing.
the class P3Optimize method optimize_.
private Funp optimize_(Funp n) {
return //
n.<//
Funp>switch_().applyIf(FunpCoerce.class, f -> f.apply((coerce, expr) -> {
return !(expr instanceof FunpDontCare) ? n : optimize(expr);
})).applyIf(FunpData.class, f -> f.apply(pairs -> {
return FunpData.of(Read.from2(pairs).concatMap((expr, range) -> {
Funp expr1 = optimize(expr);
int start = range.t0;
Streamlet<Pair<Funp, IntIntPair>> pairsx = new //
Switch<Streamlet<Pair<Funp, IntIntPair>>>(//
expr1).applyIf(FunpData.class, g -> g.apply(pairs1 -> {
return //
Read.from2(//
pairs1).map((exprc, range1) -> Pair.of(optimize(exprc), IntIntPair.of(start + range1.t0, start + range1.t1)));
})).result();
return pairsx != null ? pairsx : Read.each(Pair.of(expr1, range));
}).toList());
})).applyIf(FunpDeref.class, f -> f.apply(pointer -> {
return optimize(pointer).<Funp>switch_().applyIf(FunpReference.class, g -> g.expr).result();
})).applyIf(FunpIf.class, f -> f.apply((if_, then, else_) -> {
return //
optimize(if_).<Funp>switch_().applyIf(FunpBoolean.class, g -> g.apply(b -> {
return b ? then : else_;
})).result();
})).applyIf(FunpMemory.class, f -> f.apply((pointer, start, end) -> {
return //
optimize(pointer).<Funp>switch_().applyIf(FunpData.class, g -> g.apply(pairs -> {
for (Pair<Funp, IntIntPair> pair : pairs) {
IntIntPair range = pair.t1;
if (start == range.t0 && end == range.t1)
return pair.t0;
}
return null;
})).applyIf(FunpReference.class, g -> {
return FunpTree.of(TermOp.PLUS__, g.expr, FunpNumber.ofNumber(start));
}).result();
})).applyIf(FunpReference.class, f -> f.apply(expr -> {
return optimize(expr).<Funp>switch_().applyIf(FunpMemory.class, g -> g.pointer).result();
})).applyIf(FunpTree.class, f -> f.apply((operator, lhs, rhs) -> {
IntInt_Bool iib = TreeUtil.boolOperations.get(operator);
IntInt_Int iii = TreeUtil.intOperations.get(operator);
if (iib != null)
return evaluate(iib, lhs, rhs);
else if (iii != null)
return evaluate(iii, lhs, rhs);
else
return null;
})).applyIf(FunpTree2.class, f -> f.apply((operator, lhs, rhs) -> {
return evaluate(TreeUtil.tupleOperations.get(operator), lhs, rhs);
})).applyIf(FunpWhile.class, f -> f.apply((while_, do_, expr) -> {
return //
optimize(while_).<Funp>switch_().applyIf(FunpBoolean.class, g -> g.apply(b -> {
return b ? null : expr;
})).result();
})).result();
}
Aggregations