Search in sources :

Example 1 with IntIntPair

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));
    }
}
Also used : LibcJna(suite.ansi.LibcJna) Termios(suite.ansi.Termios) Friends.min(suite.util.Friends.min) Native(com.sun.jna.Native) IntIntPair(suite.primitive.adt.pair.IntIntPair) Predicate(java.util.function.Predicate) IRope(suite.immutable.IRope) Friends.max(suite.util.Friends.max) IntSink(suite.primitive.IntPrimitives.IntSink) Keyboard(suite.ansi.Keyboard) IRopeList(suite.immutable.IRope.IRopeList) FixieFun6(suite.adt.pair.Fixie_.FixieFun6) VK(suite.ansi.Keyboard.VK) FixieFun3(suite.adt.pair.Fixie_.FixieFun3) Sink(suite.util.FunUtil.Sink) FileUtil(suite.os.FileUtil) Chars_(suite.primitive.Chars_) IntsBuilder(suite.primitive.Ints.IntsBuilder) Ints_(suite.primitive.Ints_) IntMutable(suite.primitive.IntMutable) Fail(suite.util.Fail) Keyboard(suite.ansi.Keyboard) VK(suite.ansi.Keyboard.VK) Termios(suite.ansi.Termios) IntIntPair(suite.primitive.adt.pair.IntIntPair)

Example 2 with IntIntPair

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);
    }
}
Also used : Ast(suite.ebnf.Ebnf.Ast) Grammar(suite.ebnf.Grammar) IntIntPair(suite.primitive.adt.pair.IntIntPair)

Example 3 with IntIntPair

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();
}
Also used : IntInt_Bool(suite.node.util.TreeUtil.IntInt_Bool) Read(suite.streamlet.Read) Singleton(suite.node.util.Singleton) FunpCoerce(suite.funp.P0.FunpCoerce) IntIntPair(suite.primitive.adt.pair.IntIntPair) FunpBoolean(suite.funp.P0.FunpBoolean) Inspect(suite.inspect.Inspect) FunpTree(suite.funp.P0.FunpTree) TermOp(suite.node.io.TermOp) FunpReference(suite.funp.P0.FunpReference) FunpTree2(suite.funp.P0.FunpTree2) Funp(suite.funp.Funp_.Funp) IntInt_Int(suite.primitive.IntInt_Int) Pair(suite.adt.pair.Pair) Streamlet(suite.streamlet.Streamlet) FunpIf(suite.funp.P0.FunpIf) FunpNumber(suite.funp.P0.FunpNumber) FunpDeref(suite.funp.P0.FunpDeref) FunpMemory(suite.funp.P2.FunpMemory) FunpWhile(suite.funp.P2.FunpWhile) Switch(suite.util.Switch) FunpData(suite.funp.P2.FunpData) TreeUtil(suite.node.util.TreeUtil) FunpDontCare(suite.funp.P0.FunpDontCare) FunpDontCare(suite.funp.P0.FunpDontCare) FunpReference(suite.funp.P0.FunpReference) FunpWhile(suite.funp.P2.FunpWhile) FunpMemory(suite.funp.P2.FunpMemory) FunpBoolean(suite.funp.P0.FunpBoolean) Funp(suite.funp.Funp_.Funp) FunpDeref(suite.funp.P0.FunpDeref) Streamlet(suite.streamlet.Streamlet) FunpCoerce(suite.funp.P0.FunpCoerce) IntInt_Int(suite.primitive.IntInt_Int) FunpTree(suite.funp.P0.FunpTree) IntInt_Bool(suite.node.util.TreeUtil.IntInt_Bool) IntIntPair(suite.primitive.adt.pair.IntIntPair) IntIntPair(suite.primitive.adt.pair.IntIntPair) Pair(suite.adt.pair.Pair)

Example 4 with IntIntPair

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;
            }
        });
    });
}
Also used : IntObjStreamlet(suite.primitive.streamlet.IntObjStreamlet) IntIntSource(suite.primitive.IntIntSource) IntIntPair(suite.primitive.adt.pair.IntIntPair)

Example 5 with IntIntPair

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);
}
Also used : IntIntSource(suite.primitive.IntIntSource) IntIntPair(suite.primitive.adt.pair.IntIntPair)

Aggregations

IntIntPair (suite.primitive.adt.pair.IntIntPair)13 ArrayList (java.util.ArrayList)5 List (java.util.List)5 ArrayDeque (java.util.ArrayDeque)4 HashSet (java.util.HashSet)4 Reference (suite.node.Reference)4 IntIntSource (suite.primitive.IntIntSource)4 Deque (java.util.Deque)3 HashMap (java.util.HashMap)3 IdentityKey (suite.adt.IdentityKey)3 Pair (suite.adt.pair.Pair)3 IntObjMap (suite.primitive.adt.map.IntObjMap)3 Read (suite.streamlet.Read)3 Fail (suite.util.Fail)3 DataInputStream (java.io.DataInputStream)2 DataOutputStream (java.io.DataOutputStream)2 IOException (java.io.IOException)2 Map (java.util.Map)2 Entry (java.util.Map.Entry)2 Objects (java.util.Objects)2