Search in sources :

Example 6 with Node

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

the class InterpretFunLazy method lazy.

public Thunk_ lazy(Node node) {
    Node parsed = parse(node);
    Map<String, Thunk_> df = new HashMap<>();
    df.put(TermOp.AND___.name, binary((a, b) -> new Pair_(a, b)));
    df.put("fst", () -> new Fun_(in -> ((Pair_) in.get()).first_));
    df.put("if", () -> new Fun_(a -> () -> new Fun_(b -> () -> new Fun_(c -> a.get() == Atom.TRUE ? b : c))));
    df.put("snd", () -> new Fun_(in -> ((Pair_) in.get()).second));
    for (Entry<Operator, IntInt_Bool> e : TreeUtil.boolOperations.entrySet()) {
        IntInt_Bool fun = e.getValue();
        df.put(e.getKey().getName(), binary((a, b) -> b(fun.apply(compare(a.get(), b.get()), 0))));
    }
    for (Entry<Operator, IntInt_Int> e : TreeUtil.intOperations.entrySet()) {
        IntInt_Int fun = e.getValue();
        df.put(e.getKey().getName(), binary((a, b) -> Int.of(fun.apply(i(a), i(b)))));
    }
    List<String> keys = df.keySet().stream().sorted().collect(Collectors.toList());
    Lazy_ lazy0 = new Lazy_(0, IMap.empty());
    Frame frame = new Frame(null);
    for (String key : keys) {
        lazy0 = lazy0.put(Atom.of(key));
        frame.add(df.get(key));
    }
    return lazy0.lazy_(parsed).apply(frame);
}
Also used : IntInt_Bool(suite.node.util.TreeUtil.IntInt_Bool) APPLY(suite.fp.match.Matchers.APPLY) Prover(suite.lp.doer.Prover) DECONS(suite.fp.match.Matchers.DECONS) Mutable(suite.adt.Mutable) Fun(suite.util.FunUtil.Fun) Node(suite.node.Node) BOOLEAN(suite.fp.match.Matchers.BOOLEAN) VAR(suite.fp.match.Matchers.VAR) Map(java.util.Map) NUMBER(suite.fp.match.Matchers.NUMBER) ERROR(suite.fp.match.Matchers.ERROR) TREE(suite.fp.match.Matchers.TREE) IMap(suite.immutable.IMap) FUN(suite.fp.match.Matchers.FUN) To(suite.util.To) 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) 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) DEFVARS(suite.fp.match.Matchers.DEFVARS) Reference(suite.node.Reference) Suite(suite.Suite) Pattern(suite.BindArrayUtil.Pattern) TermOp(suite.node.io.TermOp) ATOM(suite.fp.match.Matchers.ATOM) WRAP(suite.fp.match.Matchers.WRAP) Streamlet(suite.streamlet.Streamlet) CHARS(suite.fp.match.Matchers.CHARS) Atom(suite.node.Atom) Operator(suite.node.io.Operator) Fail(suite.util.Fail) Str(suite.node.Str) BiFun(suite.util.FunUtil2.BiFun) Operator(suite.node.io.Operator) HashMap(java.util.HashMap) Node(suite.node.Node) IntInt_Int(suite.primitive.IntInt_Int) IntInt_Bool(suite.node.util.TreeUtil.IntInt_Bool)

Example 7 with Node

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

the class InterpretFunLazy0 method lazy.

public Thunk_ lazy(Node node) {
    Thunk_ error = () -> Fail.t("error termination");
    IMap<String, Thunk_> env = IMap.empty();
    env = env.put(Atom.TRUE.name, () -> Atom.TRUE);
    env = env.put(Atom.FALSE.name, () -> Atom.FALSE);
    env = env.put(TermOp.AND___.name, () -> new Fun_(a -> () -> new Fun_(b -> () -> new Pair_(a, b))));
    env = env.put(ERROR.name, error);
    env = env.put(FST__.name, () -> new Fun_(in -> ((Pair_) in.get()).first));
    env = env.put(SND__.name, () -> new Fun_(in -> ((Pair_) in.get()).second));
    for (Entry<Operator, IntInt_Bool> e : TreeUtil.boolOperations.entrySet()) {
        IntInt_Bool fun = e.getValue();
        env = env.put(e.getKey().getName(), () -> new Fun_(a -> () -> new Fun_(b -> () -> b(fun.apply(i(a), i(b))))));
    }
    for (Entry<Operator, IntInt_Int> e : TreeUtil.intOperations.entrySet()) {
        IntInt_Int fun = e.getValue();
        env = env.put(e.getKey().getName(), () -> new Fun_(a -> () -> new Fun_(b -> () -> Int.of(fun.apply(i(a), i(b))))));
    }
    return lazy_(node).apply(env);
}
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) IntInt_Int(suite.primitive.IntInt_Int) IntInt_Bool(suite.node.util.TreeUtil.IntInt_Bool)

Example 8 with Node

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

the class Intrinsics method drain.

public static Node drain(IntrinsicCallback callback, IPointer<Node> pointer) {
    Intrinsic drain = new Intrinsic() {

        public Node invoke(IntrinsicCallback callback1, List<Node> inputs) {
            IPointer<Node> pointer1 = Data.get(inputs.get(0));
            Node head;
            if ((head = pointer1.head()) != null) {
                Node left = callback1.enclose(Intrinsics.id_, head);
                Node right = callback1.enclose(this::invoke, new Data<>(pointer1.tail()));
                return Tree.of(TermOp.OR____, left, right);
            } else
                return Atom.NIL;
        }
    };
    return callback.yawn(callback.enclose(drain, new Data<>(pointer)));
}
Also used : Node(suite.node.Node) List(java.util.List) Data(suite.node.Data)

Example 9 with Node

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

the class InstructionExtractor method tupleToList.

private List<Node> tupleToList(Node node) {
    List<Node> results = new ArrayList<>();
    Tree tree;
    while ((tree = Tree.decompose(node, TermOp.TUPLE_)) != null) {
        results.add(tree.getLeft());
        node = tree.getRight();
    }
    results.add(node);
    return results;
}
Also used : Node(suite.node.Node) ArrayList(java.util.ArrayList) Tree(suite.node.Tree)

Example 10 with Node

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

the class InstructionExtractor method extractInstructions.

private void extractInstructions(Node snippet, List<List<Node>> rsList) {
    Deque<Node> deque = new ArrayDeque<>();
    deque.add(snippet);
    Tree tree;
    Node value;
    while (!deque.isEmpty()) if ((tree = Tree.decompose(deque.pop(), TermOp.AND___)) != null) {
        IdentityKey<Node> key = IdentityKey.of(tree);
        Integer ip = ipByLabelId.get(key);
        if (ip == null) {
            ipByLabelId.put(key, ip = rsList.size());
            List<Node> rs = tupleToList(tree.getLeft());
            if (rs.get(0) == FRAME)
                if ((value = label(rs.get(1))) != null) {
                    rsList.add(List.of(Atom.of("FRAME-BEGIN")));
                    extractInstructions(value, rsList);
                    rsList.add(List.of(Atom.of("FRAME-END")));
                } else
                    Fail.t("bad frame definition");
            else {
                rsList.add(rs);
                for (Node op : List_.right(rs, 1)) if ((value = label(op)) != null)
                    deque.push(value);
                deque.push(tree.getRight());
            }
        } else
            rsList.add(List.of(Atom.of("JUMP"), Int.of(ip)));
    }
}
Also used : IdentityKey(suite.adt.IdentityKey) Node(suite.node.Node) Tree(suite.node.Tree) ArrayList(java.util.ArrayList) List(java.util.List) ArrayDeque(java.util.ArrayDeque)

Aggregations

Node (suite.node.Node)139 Tree (suite.node.Tree)50 Reference (suite.node.Reference)41 Atom (suite.node.Atom)37 Int (suite.node.Int)33 ArrayList (java.util.ArrayList)32 Pair (suite.adt.pair.Pair)25 List (java.util.List)24 TermOp (suite.node.io.TermOp)21 Read (suite.streamlet.Read)21 Test (org.junit.Test)20 Map (java.util.Map)19 Suite (suite.Suite)18 Generalizer (suite.lp.doer.Generalizer)18 Fail (suite.util.Fail)18 Fun (suite.util.FunUtil.Fun)18 Str (suite.node.Str)17 Trail (suite.lp.Trail)16 RuleSet (suite.lp.kb.RuleSet)16 Tuple (suite.node.Tuple)16