use of suite.primitive.adt.pair.IntIntPair in project suite by stupidsing.
the class DevMain method run.
private void run() {
String input = FileUtil.read("src/main/java/suite/dev/DevMain.java");
Text inputText = text(IRope.ropeList(input));
try (Termios termios = new Termios(libc)) {
termios.clear();
Keyboard keyboard = new Keyboard(libc);
Sink<State> redraw = state -> state.apply((st, undo, redo, text, oc, cc) -> cc.apply((cx, cy) -> oc.apply((ox, oy) -> {
String[] lines = //
Ints_.range(//
viewSizeY).map(//
screenY -> text.get(ox, oy + screenY, viewSizeX).replace('\t', ' ')).toArray(String.class);
termios.cursor(false);
for (int screenY = 0; screenY < viewSizeY; screenY++) {
termios.gotoxy(0, screenY);
termios.puts(lines[screenY]);
}
termios.gotoxy(cx - ox, cy - oy);
termios.cursor(true);
return null;
})));
State state0 = new State(null, null, inputText, c(0, 0), c(0, 0));
redraw.sink(state0);
FixieFun3<VK, Character, State, State> mutate = (vk, ch, state) -> //
state.apply((st, undo, redo, text, oc, cc) -> oc.apply((ox, oy) -> cc.apply((cx, cy) -> {
int ci = text.index(cx, cy);
if (vk == VK.LEFT_)
return st.cursor(ci - 1);
else if (vk == VK.RIGHT)
return st.cursor(ci + 1);
else if (vk == VK.UP___)
return st.cursor(cx, cy - 1);
else if (vk == VK.DOWN_)
return st.cursor(cx, cy + 1);
else if (vk == VK.PGUP_)
return st.cursor(cx, cy - viewSizeY);
else if (vk == VK.PGDN_)
return st.cursor(cx, cy + viewSizeY);
else if (vk == VK.HOME_)
return st.cursor(text.startOfLine(ci));
else if (vk == VK.END__)
return st.cursor(text.endOfLine(ci));
else if (vk == VK.CTRL_HOME_)
return st.cursor(0);
else if (vk == VK.CTRL_END__)
return st.cursor(text.length());
else if (vk == VK.CTRL_LEFT_)
return st.cursor(text.scanNext(ci, -1, ch_ -> !Character.isJavaIdentifierPart(ch_)));
else if (vk == VK.CTRL_RIGHT)
return st.cursor(text.scanNext(ci, 1, ch_ -> !Character.isJavaIdentifierPart(ch_)));
else if (vk == VK.CTRL_UP___)
return st.offset(ox, oy - 1).cursor(cx, cy - 1);
else if (vk == VK.CTRL_DOWN_)
return st.offset(ox, oy + 1).cursor(cx, cy + 1);
else if (vk == VK.F7___)
return st.offset(ox, max(cy - viewSizeY + 1, 0));
else if (vk == VK.F8___)
return st.offset(ox, min(cy, text.nLines()));
else if (vk == VK.ALT_J____) {
int index = text.endOfLine(ci);
Text text1 = text.splice(index, index + 1, empty);
return st.text(text1).cursor(index);
} else if (vk == VK.BKSP_) {
int index = ci;
return 0 < index ? st.splice(index - 1, index, empty) : st;
} else if (vk == VK.ALT_UP___) {
int i1 = text.startOfLine(ci);
if (0 < i1) {
int i0 = text.prevLine(i1);
int i2 = text.nextLine(i1);
return st.splice(i2, i2, text.text.subList(i0, i1)).splice(i0, i1, empty);
} else
return st;
} else if (vk == VK.ALT_DOWN_) {
int i0 = text.startOfLine(ci);
int i1 = text.nextLine(i0);
if (i1 < text.length()) {
int i2 = text.nextLine(i1);
return st.splice(i1, i2, empty).splice(i0, i0, text.text.subList(i1, i2));
} else
return st;
} else if (vk == VK.DEL__)
return st.splice(1, empty);
else if (vk == VK.CTRL_K____)
return st.splice(ci, text.endOfLine(ci), empty);
else if (vk == VK.CTRL_U____)
return st.splice(text.startOfLine(ci), ci, empty);
else if (vk == VK.CTRL_D____)
return st.splice(text.startOfLine(ci), text.nextLine(ci), empty);
else if (vk == VK.CTRL_Y____)
return redo != null ? redo : st;
else if (vk == VK.CTRL_Z____) {
State undo1 = undo != null ? undo : st;
return new State(undo1.undo, st, undo1.text, oc, undo1.cursorCoord);
} else if (vk == VK.CTRL_C____)
return Fail.t();
else if (ch != null)
if (ch == 13) {
int i0 = text.startOfLine(ci);
int ix = i0;
char ch_;
while ((ch_ = text.at(ix)) == ' ' || ch_ == '\t') ix++;
return st.splice(0, IRope.ropeList("\n").concat(text.text.subList(i0, ix)));
} else
return st.splice(0, IRope.ropeList(Character.toString(ch)));
else
return st;
}))).apply((st, undo, redo, text, oc, cc) -> oc.apply((ox, oy) -> cc.apply((cx, cy) -> {
IntIntPair cc_ = text.coord(sat(text.index(cx, cy), 0, text.length()));
return st.cursor(cc_.t0, cc_.t1);
}))).apply((st, undo, redo, text, oc, cc) -> oc.apply((ox, oy) -> cc.apply((cx, cy) -> {
int x0 = Math.max(0, cx - viewSizeX + 1);
int y0 = Math.max(0, cy - viewSizeY + 1);
int ox_ = sat(ox, x0, cx);
int oy_ = sat(oy, y0, cy);
return st.offset(ox_, oy_);
})));
keyboard.loop(signal -> //
signal.fold(state0, //
(state, pair_) -> pair_.map((vk, ch) -> mutate.apply(vk, ch, state))).wire(redraw));
}
}
use of suite.primitive.adt.pair.IntIntPair in project suite by stupidsing.
the class TopDownParse method parse.
public Ast parse(String entity, String s) {
Parse parse = new Parse(s);
Ast node = parse.parse(0, build(new Grammar(GrammarType.ENTITY, entity)));
if (node != null)
return node;
else {
IntIntPair pos = parse.findPosition(parse.errorPosition);
return Fail.t("syntax error for entity " + parse.errorEntity + " at " + pos);
}
}
use of suite.primitive.adt.pair.IntIntPair 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();
}
use of suite.primitive.adt.pair.IntIntPair in project suite by stupidsing.
the class IntIntMap1 method streamlet.
public IntObjStreamlet<Integer> streamlet() {
return new IntObjStreamlet<>(() -> {
IntIntSource source = source_();
IntIntPair pair0 = IntIntPair.of(0, 0);
return IntObjOutlet.of(new IntObjSource<Integer>() {
public boolean source2(IntObjPair<Integer> pair) {
boolean b = source.source2(pair0);
if (b)
pair.update(pair0.t0, pair0.t1);
return b;
}
});
});
}
use of suite.primitive.adt.pair.IntIntPair in project suite by stupidsing.
the class IntIntMap1 method forEach.
public void forEach(IntIntSink sink) {
IntIntPair pair = IntIntPair.of(0, 0);
IntIntSource source = source_();
while (source.source2(pair)) sink.sink2(pair.t0, pair.t1);
}
Aggregations