Search in sources :

Example 6 with Dict

use of suite.node.Dict in project suite by stupidsing.

the class RecursiveFactorizerTest method treeNode.

private Node treeNode(Source<Node> g, Node name, List<Node> nodes) {
    List<Node> pairs = Read.from(nodes).map(node -> pairNode(node, g.source())).toList();
    Dict dict = new Dict();
    dict.map.put(Atom.of("name"), Reference.of(name));
    dict.map.put(Atom.of("pairs"), Reference.of(Tree.of(TermOp.OR____, pairs)));
    return Tree.of(TermOp.COLON_, Atom.of(FTree.class.getName()), dict);
}
Also used : FNode(suite.node.parser.FactorizeResult.FNode) Read(suite.streamlet.Read) Singleton(suite.node.util.Singleton) Fun(suite.util.FunUtil.Fun) Node(suite.node.Node) FTree(suite.node.parser.FactorizeResult.FTree) FileUtil(suite.os.FileUtil) Nodify(suite.util.Nodify) Dict(suite.node.Dict) FPair(suite.node.parser.FactorizeResult.FPair) Reference(suite.node.Reference) Source(suite.util.FunUtil.Source) Assert.assertTrue(org.junit.Assert.assertTrue) TermOp(suite.node.io.TermOp) FTerminal(suite.node.parser.FactorizeResult.FTerminal) Test(org.junit.Test) To(suite.util.To) Tree(suite.node.Tree) Iterate(suite.util.FunUtil.Iterate) List(java.util.List) Rewrite(suite.node.util.Rewrite) Assert.assertFalse(org.junit.Assert.assertFalse) Atom(suite.node.Atom) Operator(suite.node.io.Operator) Assert.assertEquals(org.junit.Assert.assertEquals) Str(suite.node.Str) Dict(suite.node.Dict) FNode(suite.node.parser.FactorizeResult.FNode) Node(suite.node.Node)

Example 7 with Dict

use of suite.node.Dict in project suite by stupidsing.

the class RecursiveFactorizerTest method terminalNode.

private Node terminalNode(String s) {
    Dict dict = new Dict();
    dict.map.put(Atom.of("chars"), Reference.of(new Str(s)));
    return Tree.of(TermOp.COLON_, Atom.of(FTerminal.class.getName()), dict);
}
Also used : Str(suite.node.Str) Dict(suite.node.Dict)

Example 8 with Dict

use of suite.node.Dict in project suite by stupidsing.

the class TypeChecker method getEnumType.

private Node getEnumType(Node name, Node type1) {
    Dict dict = new Dict();
    dict.map.put(name, Reference.of(type1));
    return dict;
}
Also used : Dict(suite.node.Dict)

Example 9 with Dict

use of suite.node.Dict in project suite by stupidsing.

the class Binder method bind.

public static boolean bind(Node n0, Node n1, Trail trail) {
    n0 = n0.finalNode();
    n1 = n1.finalNode();
    if (n0 == n1)
        return true;
    Class<? extends Node> clazz0 = n0.getClass();
    Class<? extends Node> clazz1 = n1.getClass();
    if (clazz0 == Reference.class) {
        trail.addBind((Reference) n0, n1);
        return true;
    } else if (clazz1 == Reference.class) {
        trail.addBind((Reference) n1, n0);
        return true;
    }
    if (clazz0 == Dict.class && clazz1 == Dict.class) {
        Map<Node, Reference> map0 = ((Dict) n0).map;
        Map<Node, Reference> map1 = ((Dict) n1).map;
        boolean b = true;
        for (Node key : List_.concat(map0.keySet(), map1.keySet())) {
            Node v0 = map0.computeIfAbsent(key, k -> new Reference());
            Node v1 = map1.computeIfAbsent(key, k -> new Reference());
            b &= bind(v0, v1, trail);
        }
        return b;
    } else if (clazz0 == Int.class && clazz1 == Int.class)
        return ((Int) n0).number == ((Int) n1).number;
    else if (clazz0 == Str.class && clazz1 == Str.class)
        return Objects.equals(((Str) n0).value, ((Str) n1).value);
    else if (Tree.class.isAssignableFrom(clazz0) && Tree.class.isAssignableFrom(clazz1)) {
        Tree t0 = (Tree) n0;
        Tree t1 = (Tree) n1;
        return // 
        t0.getOperator() == t1.getOperator() && // 
        bind(t0.getLeft(), t1.getLeft(), trail) && bind(t0.getRight(), t1.getRight(), trail);
    } else if (clazz0 == Tuple.class && clazz1 == Tuple.class) {
        Node[] nodes0 = ((Tuple) n0).nodes;
        Node[] nodes1 = ((Tuple) n1).nodes;
        boolean b = nodes0.length == nodes1.length;
        if (b) {
            for (int i = 0; i < nodes0.length; i++) b &= bind(nodes0[i], nodes1[i], trail);
        }
        return b;
    } else
        return false;
}
Also used : Str(suite.node.Str) Reference(suite.node.Reference) Dict(suite.node.Dict) Node(suite.node.Node) Tree(suite.node.Tree) Tuple(suite.node.Tuple)

Example 10 with Dict

use of suite.node.Dict in project suite by stupidsing.

the class SewingClonerImpl method cloner.

@Override
public Clone_ cloner(Node node) {
    List<Clone_> funs = new ArrayList<>();
    Clone_ fun;
    while (true) {
        Node node0 = node;
        Tree tree;
        if (node0 instanceof Dict) {
            Clone_[][] array = // 
            Read.from2(// 
            ((Dict) node0).map).map(// 
            (key, value) -> new Clone_[] { cloner(key), cloner(value) }).toArray(Clone_[].class);
            int length = array.length;
            return env -> {
                @SuppressWarnings("unchecked") Pair<Node, Reference>[] pairs = new Pair[length];
                for (int i = 0; i < length; i++) pairs[i] = Pair.of(array[i][0].apply(env), Reference.of(array[i][1].apply(env)));
                return Dict.of(pairs);
            };
        } else if ((tree = Tree.decompose(node0)) != null) {
            Operator operator = tree.getOperator();
            if (operator != TermOp.OR____) {
                Clone_ f = cloner(tree.getLeft());
                funs.add(env -> Tree.of(operator, f.apply(env), null));
                node = tree.getRight();
                continue;
            } else {
                // delay generalizing for performance
                Clone_ lf = cloner(tree.getLeft());
                Clone_ rf = cloner(tree.getRight());
                fun = env -> Tree.of(operator, lf.apply(env), new Suspend(() -> rf.apply(env)));
            }
        } else if (node0 instanceof Reference) {
            int index = vm.computeIndex((Reference) node0);
            fun = env -> env.get(index);
        } else if (node0 instanceof Tuple) {
            Clone_[] ps = Read.from(((Tuple) node0).nodes).map(this::cloner).toArray(Clone_.class);
            int size = ps.length;
            fun = env -> {
                Node[] nodes = new Node[size];
                for (int i = 0; i < size; i++) nodes[i] = ps[i].apply(env);
                return Tuple.of(nodes);
            };
        } else
            fun = env -> node0;
        funs.add(fun);
        break;
    }
    if (1 < funs.size())
        return env -> {
            Tree t = Tree.of(null, null, null);
            Node node_ = t;
            for (Clone_ fun_ : funs) {
                Tree t_ = Tree.decompose(node_);
                Tree.forceSetRight(t_, fun_.apply(env));
                node_ = t_.getRight();
            }
            return t.getRight();
        };
    else
        return funs.get(0);
}
Also used : Reference(suite.node.Reference) Read(suite.streamlet.Read) TermOp(suite.node.io.TermOp) Tree(suite.node.Tree) ArrayList(java.util.ArrayList) Node(suite.node.Node) Pair(suite.adt.pair.Pair) List(java.util.List) VariableMapper(suite.lp.sewing.VariableMapper) Suspend(suite.node.Suspend) ClonerFactory(suite.lp.doer.ClonerFactory) Tuple(suite.node.Tuple) Operator(suite.node.io.Operator) Dict(suite.node.Dict) Operator(suite.node.io.Operator) Reference(suite.node.Reference) Node(suite.node.Node) ArrayList(java.util.ArrayList) Suspend(suite.node.Suspend) Dict(suite.node.Dict) Tree(suite.node.Tree) Tuple(suite.node.Tuple)

Aggregations

Dict (suite.node.Dict)12 Node (suite.node.Node)9 Reference (suite.node.Reference)8 Str (suite.node.Str)8 Tree (suite.node.Tree)8 Atom (suite.node.Atom)7 List (java.util.List)6 Tuple (suite.node.Tuple)6 ArrayList (java.util.ArrayList)5 Read (suite.streamlet.Read)5 HashSet (java.util.HashSet)4 Pair (suite.adt.pair.Pair)4 ProverConstant (suite.lp.doer.ProverConstant)4 Int (suite.node.Int)4 TermOp (suite.node.io.TermOp)4 Entry (java.util.Map.Entry)3 Set (java.util.Set)3 Operator (suite.node.io.Operator)3 Chars (suite.primitive.Chars)3 Fun (suite.util.FunUtil.Fun)3