Search in sources :

Example 6 with Operator

use of suite.node.io.Operator in project suite by stupidsing.

the class PrettyPrinter method prettyPrint_.

// op0 for avoiding unnecessary indenting; prec0 for parenthesizing
private void prettyPrint_(Node node, Operator op0, int prec0) {
    int x = getX(), y = getY();
    int length = lengthEstimator.getEstimatedLength(node);
    // line too long?
    if (node instanceof Tree) {
        Tree tree = (Tree) node;
        Operator op = tree.getOperator();
        int prec = op.getPrecedence();
        boolean isNeedPars = prec <= prec0;
        int parsIndent = 0, parsIndent0 = 0;
        if (isNeedPars) {
            parsIndent = currentLineIndent;
            parsIndent0 = incrementIndent();
            append("(");
        }
        if (lineLength < x + length)
            if (isLookingLikeList(op, node))
                prettyPrintList(op, node);
            else {
                Node left = tree.getLeft();
                Node right = tree.getRight();
                Assoc assoc = op.getAssoc();
                int leftPrec = prec - (assoc == Assoc.LEFT ? 1 : 0);
                int rightPrec = prec - (assoc == Assoc.RIGHT ? 1 : 0);
                if (op == TermOp.BRACES)
                    leftPrec = rightPrec = 0;
                Tree tree1 = Tree.decompose(right, op);
                Node r0 = tree1 != null ? tree1.getLeft() : null;
                int es0 = lengthEstimator.getEstimatedLength(left);
                int es1 = r0 != null ? lengthEstimator.getEstimatedLength(r0) : lineLength;
                int opLength = op.getName().length();
                // breaks "a + b + xxx" in the second operator
                if (// 
                assoc == Assoc.RIGHT && // 
                x + es0 + es1 + opLength < lineLength && r0 != preferLineBreakBeforeKeyword) {
                    prettyPrint_(left, op, leftPrec);
                    OperatorPosition opPos = appendOperator(op);
                    prettyPrint_(right, op, rightPrec);
                    closeBraces(op, opPos);
                } else {
                    // breaks after the operator
                    boolean isIncRightIndent = op != op0;
                    int indent0 = 0;
                    prettyPrint_(left, op, leftPrec);
                    if (isIncRightIndent)
                        indent0 = incrementIndent();
                    OperatorPosition opPos;
                    if (getLineSize() + lengthEstimator.getEstimatedLength(right) < squeezeLineLength)
                        opPos = appendOperator(op);
                    else
                        opPos = appendOperatorLineFeed(op);
                    prettyPrint_(right, op, rightPrec);
                    closeBraces(op, opPos);
                    if (isIncRightIndent)
                        revertIndent(indent0);
                }
            }
        else
            append(Formatter.dump(node));
        if (isNeedPars) {
            if (y != getY())
                nl(parsIndent);
            append(")");
            revertIndent(parsIndent0);
        }
    } else {
        if (node == lineBreakBeforeKeyword && !isLineBegin())
            nl();
        // space sufficient
        append(Formatter.dump(node));
    }
}
Also used : Operator(suite.node.io.Operator) Node(suite.node.Node) Tree(suite.node.Tree) Assoc(suite.node.io.Operator.Assoc)

Example 7 with Operator

use of suite.node.io.Operator in project suite by stupidsing.

the class IndentationPreprocessor method apply.

@Override
public List<Run> apply(String in) {
    List<Run> runs = new ArrayList<>();
    int nLastIndents = 0;
    String lastIndent = "";
    int pos = 0;
    int length = in.length();
    while (pos < length) {
        int pos0 = pos;
        char ch;
        while (pos0 < length && (ch = in.charAt(pos0)) != '\n' && Character.isWhitespace(ch)) pos0++;
        Segment segment = ParseUtil.searchPosition(in.toCharArray(), Segment.of(pos0, length), "\n", Assoc.RIGHT, false);
        int pos1 = segment != null ? segment.start : length;
        // includes LF
        int pos2 = segment != null ? segment.end : length;
        String indent = in.substring(pos, pos0);
        String line = in.substring(pos0, pos1);
        int nIndents = pos0 - pos, lineLength = pos1 - pos0;
        if (!lastIndent.startsWith(indent) && !indent.startsWith(lastIndent))
            Fail.t("indent mismatch");
        if (lineLength != 0) {
            // ignore empty lines
            int startPos = 0, endPos = lineLength;
            lastIndent = indent;
            // find operators at beginning and end of line
            for (Operator operator : operators) {
                String name = operator.getName().trim();
                if (!name.isEmpty()) {
                    if (line.startsWith(name + " "))
                        startPos = max(startPos, name.length() + 1);
                    if (String_.equals(line, name))
                        startPos = max(startPos, name.length());
                    if (line.endsWith(name))
                        endPos = min(endPos, lineLength - name.length());
                }
            }
            if (// when a line has only one operator
            endPos < startPos)
                startPos = 0;
            // insert parentheses by line indentation
            while (nIndents < nLastIndents) {
                runs.add(new Run(") "));
                nLastIndents--;
            }
            runs.add(new Run(pos0, pos0 + startPos));
            while (nLastIndents < nIndents) {
                runs.add(new Run(" ("));
                nLastIndents++;
            }
            runs.add(new Run(pos0 + startPos, pos2));
            nLastIndents = nIndents;
        }
        pos = pos2;
    }
    while (0 < nLastIndents--) runs.add(new Run(") "));
    return runs;
}
Also used : Operator(suite.node.io.Operator) ArrayList(java.util.ArrayList) Run(suite.text.Preprocess.Run) Segment(suite.text.Segment)

Example 8 with Operator

use of suite.node.io.Operator in project suite by stupidsing.

the class InterpretFunEager method oper.

private Operator oper(Node type) {
    Operator operator;
    if (type == Atom.of("L"))
        operator = TermOp.OR____;
    else if (type == Atom.of("P"))
        operator = TermOp.AND___;
    else
        operator = Fail.t("unknown CONS type");
    return operator;
}
Also used : Operator(suite.node.io.Operator)

Example 9 with Operator

use of suite.node.io.Operator in project suite by stupidsing.

the class InterpretFunEager method eager.

public Node eager(Node node) {
    Node mode = isLazyify ? Atom.of("LAZY") : Atom.of("EAGER");
    Node query = Suite.substitute("source .in, fc-process-function .0 .in .out, sink .out", mode);
    RuleSet rs = Suite.newRuleSet(List.of("auto.sl", "fc/fc.sl"));
    Finder finder = new SewingProverBuilder2().build(rs).apply(query);
    Node parsed = FindUtil.collectSingle(finder, node);
    IntrinsicCallback ic = isLazyify ? lazyIntrinsicCallback() : Intrinsics.eagerIntrinsicCallback;
    Map<String, Node> df = new HashMap<>();
    df.put(TermOp.AND___.name, f2((a, b) -> Tree.of(TermOp.AND___, a, b)));
    df.put("+call%i-t1", f1(i -> fn(1, l -> Data.<Intrinsic>get(i).invoke(ic, l))));
    df.put("+call%i-t2", f1(i -> fn(2, l -> Data.<Intrinsic>get(i).invoke(ic, l))));
    df.put("+call%i-t3", f1(i -> fn(3, l -> Data.<Intrinsic>get(i).invoke(ic, l))));
    df.put("+call%i-v1", f1(i -> fn(1, l -> Data.<Intrinsic>get(i).invoke(ic, l))));
    df.put("+call%i-v2", f1(i -> fn(2, l -> Data.<Intrinsic>get(i).invoke(ic, l))));
    df.put("+call%i-v3", f1(i -> fn(3, l -> Data.<Intrinsic>get(i).invoke(ic, l))));
    df.put("+compare", f2((a, b) -> Int.of(Comparer.comparer.compare(a, b))));
    df.put("+get%i", f1(a -> new Data<>(Intrinsics.intrinsics.get(((Atom) a).name.split("!")[1]))));
    df.put("+is-list", f1(a -> b(Tree.decompose(a) != null)));
    df.put("+is-pair", f1(a -> b(Tree.decompose(a) != null)));
    df.put("+lcons", f2((a, b) -> Tree.of(TermOp.OR____, a, b)));
    df.put("+lhead", f1(a -> Tree.decompose(a).getLeft()));
    df.put("+ltail", f1(a -> Tree.decompose(a).getRight()));
    df.put("+pcons", f2((a, b) -> Tree.of(TermOp.AND___, a, b)));
    df.put("+pleft", f1(a -> Tree.decompose(a).getLeft()));
    df.put("+pright", f1(a -> Tree.decompose(a).getRight()));
    for (Entry<Operator, IntInt_Bool> e : TreeUtil.boolOperations.entrySet()) {
        IntInt_Bool fun = e.getValue();
        df.put(e.getKey().getName(), f2((a, b) -> b(fun.apply(compare(a, b), 0))));
    }
    for (Entry<Operator, IntInt_Int> e : TreeUtil.intOperations.entrySet()) {
        IntInt_Int fun = e.getValue();
        df.put(e.getKey().getName(), f2((a, b) -> Int.of(fun.apply(i(a), i(b)))));
    }
    List<String> keys = df.keySet().stream().sorted().collect(Collectors.toList());
    Eager_ eager0 = new Eager_(0, IMap.empty());
    Frame frame = new Frame(null);
    for (String key : keys) {
        eager0 = eager0.put(Atom.of(key));
        frame.add(df.get(key));
    }
    return eager0.eager_(parsed).apply(frame);
}
Also used : IntInt_Bool(suite.node.util.TreeUtil.IntInt_Bool) APPLY(suite.fp.match.Matchers.APPLY) DECONS(suite.fp.match.Matchers.DECONS) Fun(suite.util.FunUtil.Fun) BinOp(suite.util.FunUtil2.BinOp) Node(suite.node.Node) BOOLEAN(suite.fp.match.Matchers.BOOLEAN) VAR(suite.fp.match.Matchers.VAR) Map(java.util.Map) FindUtil(suite.lp.search.FindUtil) RuleSet(suite.lp.kb.RuleSet) NUMBER(suite.fp.match.Matchers.NUMBER) ERROR(suite.fp.match.Matchers.ERROR) IntrinsicCallback(suite.fp.intrinsic.Intrinsics.IntrinsicCallback) TREE(suite.fp.match.Matchers.TREE) IMap(suite.immutable.IMap) FUN(suite.fp.match.Matchers.FUN) To(suite.util.To) Intrinsic(suite.fp.intrinsic.Intrinsics.Intrinsic) Collectors(java.util.stream.Collectors) Matcher(suite.fp.match.Matcher) PRAGMA(suite.fp.match.Matchers.PRAGMA) Tree(suite.node.Tree) UNWRAP(suite.fp.match.Matchers.UNWRAP) IntInt_Int(suite.primitive.IntInt_Int) Iterate(suite.util.FunUtil.Iterate) Pair(suite.adt.pair.Pair) List(java.util.List) Entry(java.util.Map.Entry) Int(suite.node.Int) TreeUtil(suite.node.util.TreeUtil) IF(suite.fp.match.Matchers.IF) SewingProverBuilder2(suite.lp.search.SewingProverBuilder2) Comparer(suite.node.util.Comparer) HashMap(java.util.HashMap) TCO(suite.fp.match.Matchers.TCO) ArrayList(java.util.ArrayList) CONS(suite.fp.match.Matchers.CONS) Data(suite.node.Data) Formatter(suite.node.io.Formatter) Intrinsics(suite.fp.intrinsic.Intrinsics) DEFVARS(suite.fp.match.Matchers.DEFVARS) Suite(suite.Suite) Source(suite.util.FunUtil.Source) Finder(suite.lp.search.ProverBuilder.Finder) Pattern(suite.BindArrayUtil.Pattern) TermOp(suite.node.io.TermOp) ATOM(suite.fp.match.Matchers.ATOM) WRAP(suite.fp.match.Matchers.WRAP) CHARS(suite.fp.match.Matchers.CHARS) Atom(suite.node.Atom) Operator(suite.node.io.Operator) Fail(suite.util.Fail) Str(suite.node.Str) Operator(suite.node.io.Operator) RuleSet(suite.lp.kb.RuleSet) IntrinsicCallback(suite.fp.intrinsic.Intrinsics.IntrinsicCallback) HashMap(java.util.HashMap) Node(suite.node.Node) Finder(suite.lp.search.ProverBuilder.Finder) SewingProverBuilder2(suite.lp.search.SewingProverBuilder2) Data(suite.node.Data) Atom(suite.node.Atom) IntInt_Int(suite.primitive.IntInt_Int) Intrinsic(suite.fp.intrinsic.Intrinsics.Intrinsic) IntInt_Bool(suite.node.util.TreeUtil.IntInt_Bool)

Example 10 with Operator

use of suite.node.io.Operator in project suite by stupidsing.

the class InterpretFunLazy0 method lazy_.

private Fun<IMap<String, Thunk_>, Thunk_> lazy_(Node node) {
    Fun<IMap<String, Thunk_>, Thunk_> result;
    Tree tree;
    Node[] m;
    if ((m = Suite.pattern("define .0 := .1 >> .2").match(node)) != null) {
        String vk = v(m[0]);
        Fun<IMap<String, Thunk_>, Thunk_> value = lazy_(m[1]);
        Fun<IMap<String, Thunk_>, Thunk_> expr = lazy_(m[2]);
        result = env -> {
            Mutable<Thunk_> val = Mutable.nil();
            IMap<String, Thunk_> env1 = env.put(vk, () -> val.get().get());
            val.set(value.apply(env1)::get);
            return expr.apply(env1);
        };
    } else if ((m = Suite.pattern("if .0 then .1 else .2").match(node)) != null) {
        Fun<IMap<String, Thunk_>, Thunk_> if_ = lazy_(m[0]);
        Fun<IMap<String, Thunk_>, Thunk_> then_ = lazy_(m[1]);
        Fun<IMap<String, Thunk_>, Thunk_> else_ = lazy_(m[2]);
        result = env -> (if_.apply(env).get() == Atom.TRUE ? then_ : else_).apply(env);
    } else if ((m = Suite.pattern(".0 => .1").match(node)) != null) {
        String vk = v(m[0]);
        Fun<IMap<String, Thunk_>, Thunk_> value = lazy_(m[1]);
        result = env -> () -> new Fun_(in -> value.apply(env.put(vk, in)));
    } else if ((m = Suite.pattern(".0 {.1}").match(node)) != null) {
        Fun<IMap<String, Thunk_>, Thunk_> fun = lazy_(m[0]);
        Fun<IMap<String, Thunk_>, Thunk_> param = lazy_(m[1]);
        result = env -> fun(fun.apply(env).get()).apply(param.apply(env));
    } else if ((tree = Tree.decompose(node)) != null) {
        Operator operator = tree.getOperator();
        Fun<IMap<String, Thunk_>, Thunk_> p0 = lazy_(tree.getLeft());
        Fun<IMap<String, Thunk_>, Thunk_> p1 = lazy_(tree.getRight());
        result = env -> {
            Thunk_ r0 = env.get(operator.getName());
            Thunk_ r1 = fun(r0.get()).apply(p0.apply(env));
            Thunk_ r2 = fun(r1.get()).apply(p1.apply(env));
            return r2;
        };
    } else if (node instanceof Atom) {
        String vk = v(node);
        result = env -> env.get(vk);
    } else
        result = env -> () -> node;
    return result;
}
Also used : Suite(suite.Suite) IntInt_Bool(suite.node.util.TreeUtil.IntInt_Bool) IMap(suite.immutable.IMap) Mutable(suite.adt.Mutable) TermOp(suite.node.io.TermOp) Fun(suite.util.FunUtil.Fun) Tree(suite.node.Tree) Node(suite.node.Node) IntInt_Int(suite.primitive.IntInt_Int) Iterate(suite.util.FunUtil.Iterate) Atom(suite.node.Atom) Entry(java.util.Map.Entry) Int(suite.node.Int) Operator(suite.node.io.Operator) TreeUtil(suite.node.util.TreeUtil) Fail(suite.util.Fail) Operator(suite.node.io.Operator) Node(suite.node.Node) Atom(suite.node.Atom) IMap(suite.immutable.IMap) Tree(suite.node.Tree) Fun(suite.util.FunUtil.Fun)

Aggregations

Operator (suite.node.io.Operator)17 Tree (suite.node.Tree)12 Node (suite.node.Node)10 TermOp (suite.node.io.TermOp)8 ArrayList (java.util.ArrayList)7 Atom (suite.node.Atom)6 Int (suite.node.Int)6 TreeUtil (suite.node.util.TreeUtil)6 IntInt_Int (suite.primitive.IntInt_Int)6 List (java.util.List)5 Entry (java.util.Map.Entry)4 Suite (suite.Suite)4 Pair (suite.adt.pair.Pair)4 IMap (suite.immutable.IMap)4 IntInt_Bool (suite.node.util.TreeUtil.IntInt_Bool)4 Fail (suite.util.Fail)4 Fun (suite.util.FunUtil.Fun)4 Iterate (suite.util.FunUtil.Iterate)4 Mutable (suite.adt.Mutable)3 HashMap (java.util.HashMap)2