Search in sources :

Example 1 with IMap

use of suite.immutable.IMap 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 2 with IMap

use of suite.immutable.IMap 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)

Example 3 with IMap

use of suite.immutable.IMap in project suite by stupidsing.

the class P0Parse method expandMacros.

private Node expandMacros(Node node0) {
    class Expand {

        private IMap<Prototype, Node[]> macros;

        private Expand(IMap<Prototype, Node[]> macros) {
            this.macros = macros;
        }

        private Node expand(Node node) {
            Tree tree;
            Node[] m;
            Node[] ht;
            if ((m = Suite.pattern("expand .0 := .1 >> .2").match(node)) != null) {
                Node head = m[0];
                return new Expand(macros.put(Prototype.of(head), new Node[] { head, m[1] })).expand(m[2]);
            } else if ((ht = macros.get(Prototype.of(node))) != null) {
                Generalizer g = new Generalizer();
                Node t0_ = g.generalize(ht[0]);
                Node t1_ = g.generalize(ht[1]);
                if (Binder.bind(node, t0_, new Trail()))
                    return expand(t1_);
            }
            if ((tree = Tree.decompose(node)) != null)
                return Tree.of(tree.getOperator(), expand(tree.getLeft()), expand(tree.getRight()));
            else
                return node;
        }
    }
    return new Expand(IMap.empty()).expand(node0);
}
Also used : IMap(suite.immutable.IMap) Generalizer(suite.lp.doer.Generalizer) Trail(suite.lp.Trail) Node(suite.node.Node) SwitchNode(suite.node.io.SwitchNode) Tree(suite.node.Tree) FunpTree(suite.funp.P0.FunpTree)

Example 4 with IMap

use of suite.immutable.IMap in project suite by stupidsing.

the class ServerMain method runHttpServer.

private void runHttpServer() {
    Authenticator authenticator = (username, password) -> Constants.secrets().prove(Suite.substitute("auth .0 .1", new Str(username), new Str(password)));
    HttpHandler handler0 = request -> HttpResponse.of(To.outlet(// 
    "" + // 
    "<html>" + "<br/>method = " + // 
    request.method + "<br/>server = " + // 
    request.server + "<br/>path = " + // 
    request.path + "<br/>attrs = " + // 
    HttpHeaderUtil.getAttrs(request.query) + "<br/>headers = " + // 
    request.headers + "</html>"));
    HttpHandler handler1 = HttpHandler.ofDispatch(// 
    IMap.<String, HttpHandler>empty().put("path", // 
    HttpHandler.ofPath(Constants.tmp)).put("site", HttpHandler.ofSession(authenticator, handler0)));
    new HttpServer().run(handler1);
}
Also used : HttpHandler(suite.http.HttpHandler) Schedule(suite.os.Schedule) ExecutableProgram(suite.util.RunUtil.ExecutableProgram) IMap(suite.immutable.IMap) LocalDateTime(java.time.LocalDateTime) Thread_(suite.util.Thread_) HttpHeaderUtil(suite.http.HttpHeaderUtil) To(suite.util.To) Scheduler(suite.os.Scheduler) RunUtil(suite.util.RunUtil) ArrayList(java.util.ArrayList) TelegramBot(suite.telegram.TelegramBot) List(java.util.List) HttpServer(suite.http.HttpServer) Authenticator(suite.http.HttpSessionController.Authenticator) LocalTime(java.time.LocalTime) HttpResponse(suite.http.HttpResponse) Str(suite.node.Str) Str(suite.node.Str) HttpHandler(suite.http.HttpHandler) HttpServer(suite.http.HttpServer) Authenticator(suite.http.HttpSessionController.Authenticator)

Aggregations

IMap (suite.immutable.IMap)4 Node (suite.node.Node)3 Tree (suite.node.Tree)3 Entry (java.util.Map.Entry)2 Suite (suite.Suite)2 Mutable (suite.adt.Mutable)2 Atom (suite.node.Atom)2 Int (suite.node.Int)2 Operator (suite.node.io.Operator)2 TermOp (suite.node.io.TermOp)2 TreeUtil (suite.node.util.TreeUtil)2 IntInt_Bool (suite.node.util.TreeUtil.IntInt_Bool)2 IntInt_Int (suite.primitive.IntInt_Int)2 Fail (suite.util.Fail)2 Fun (suite.util.FunUtil.Fun)2 Iterate (suite.util.FunUtil.Iterate)2 LocalDateTime (java.time.LocalDateTime)1 LocalTime (java.time.LocalTime)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1